blob: 65e54fd0a8d74f875a3ab7f7cb6d792f14709850 [file] [log] [blame]
Oliver Hartkoppa1044e32010-12-02 10:57:59 +00001/*
2 * slcan.c - serial line CAN 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 * slcan.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>
David S. Miller84b3cdc2010-12-08 18:41:03 -080056#include <linux/sched.h>
Oliver Hartkoppa1044e32010-12-02 10:57:59 +000057#include <linux/delay.h>
58#include <linux/init.h>
Andy Shevchenko81fc70d2011-07-17 22:26:37 +000059#include <linux/kernel.h>
Oliver Hartkoppa1044e32010-12-02 10:57:59 +000060#include <linux/can.h>
61
62static __initdata const char banner[] =
63 KERN_INFO "slcan: serial line CAN interface driver\n";
64
65MODULE_ALIAS_LDISC(N_SLCAN);
66MODULE_DESCRIPTION("serial line CAN interface");
67MODULE_LICENSE("GPL");
68MODULE_AUTHOR("Oliver Hartkopp <socketcan@hartkopp.net>");
69
70#define SLCAN_MAGIC 0x53CA
71
72static int maxdev = 10; /* MAX number of SLCAN channels;
73 This can be overridden with
74 insmod slcan.ko maxdev=nnn */
75module_param(maxdev, int, 0);
76MODULE_PARM_DESC(maxdev, "Maximum number of slcan interfaces");
77
78/* maximum rx buffer len: extended CAN frame with timestamp */
79#define SLC_MTU (sizeof("T1111222281122334455667788EA5F\r")+1)
80
81struct slcan {
82 int magic;
83
84 /* Various fields. */
85 struct tty_struct *tty; /* ptr to TTY structure */
86 struct net_device *dev; /* easy for intr handling */
87 spinlock_t lock;
88
89 /* These are pointers to the malloc()ed frame buffers. */
90 unsigned char rbuff[SLC_MTU]; /* receiver buffer */
91 int rcount; /* received chars counter */
92 unsigned char xbuff[SLC_MTU]; /* transmitter buffer */
93 unsigned char *xhead; /* pointer to next XMIT byte */
94 int xleft; /* bytes left in XMIT queue */
95
96 unsigned long flags; /* Flag values/ mode etc */
97#define SLF_INUSE 0 /* Channel in use */
98#define SLF_ERROR 1 /* Parity, etc. error */
99
100 unsigned char leased;
101 dev_t line;
102 pid_t pid;
103};
104
105static struct net_device **slcan_devs;
106
107 /************************************************************************
108 * SLCAN ENCAPSULATION FORMAT *
109 ************************************************************************/
110
111/*
112 * A CAN frame has a can_id (11 bit standard frame format OR 29 bit extended
113 * frame format) a data length code (can_dlc) which can be from 0 to 8
114 * and up to <can_dlc> data bytes as payload.
115 * Additionally a CAN frame may become a remote transmission frame if the
116 * RTR-bit is set. This causes another ECU to send a CAN frame with the
117 * given can_id.
118 *
119 * The SLCAN ASCII representation of these different frame types is:
120 * <type> <id> <dlc> <data>*
121 *
122 * Extended frames (29 bit) are defined by capital characters in the type.
123 * RTR frames are defined as 'r' types - normal frames have 't' type:
124 * t => 11 bit data frame
125 * r => 11 bit RTR frame
126 * T => 29 bit data frame
127 * R => 29 bit RTR frame
128 *
129 * The <id> is 3 (standard) or 8 (extended) bytes in ASCII Hex (base64).
130 * The <dlc> is a one byte ASCII number ('0' - '8')
131 * The <data> section has at much ASCII Hex bytes as defined by the <dlc>
132 *
133 * Examples:
134 *
135 * t1230 : can_id 0x123, can_dlc 0, no data
136 * t4563112233 : can_id 0x456, can_dlc 3, data 0x11 0x22 0x33
137 * T12ABCDEF2AA55 : extended can_id 0x12ABCDEF, can_dlc 2, data 0xAA 0x55
138 * r1230 : can_id 0x123, can_dlc 0, no data, remote transmission request
139 *
140 */
141
142 /************************************************************************
143 * STANDARD SLCAN DECAPSULATION *
144 ************************************************************************/
145
Oliver Hartkoppa1044e32010-12-02 10:57:59 +0000146/* Send one completely decapsulated can_frame to the network layer */
147static void slc_bump(struct slcan *sl)
148{
149 struct sk_buff *skb;
150 struct can_frame cf;
151 int i, dlc_pos, tmp;
152 unsigned long ultmp;
153 char cmd = sl->rbuff[0];
154
155 if ((cmd != 't') && (cmd != 'T') && (cmd != 'r') && (cmd != 'R'))
156 return;
157
158 if (cmd & 0x20) /* tiny chars 'r' 't' => standard frame format */
159 dlc_pos = 4; /* dlc position tiiid */
160 else
161 dlc_pos = 9; /* dlc position Tiiiiiiiid */
162
163 if (!((sl->rbuff[dlc_pos] >= '0') && (sl->rbuff[dlc_pos] < '9')))
164 return;
165
166 cf.can_dlc = sl->rbuff[dlc_pos] - '0'; /* get can_dlc from ASCII val */
167
168 sl->rbuff[dlc_pos] = 0; /* terminate can_id string */
169
170 if (strict_strtoul(sl->rbuff+1, 16, &ultmp))
171 return;
172
173 cf.can_id = ultmp;
174
175 if (!(cmd & 0x20)) /* NO tiny chars => extended frame format */
176 cf.can_id |= CAN_EFF_FLAG;
177
178 if ((cmd | 0x20) == 'r') /* RTR frame */
179 cf.can_id |= CAN_RTR_FLAG;
180
181 *(u64 *) (&cf.data) = 0; /* clear payload */
182
183 for (i = 0, dlc_pos++; i < cf.can_dlc; i++) {
Andy Shevchenko81fc70d2011-07-17 22:26:37 +0000184 tmp = hex_to_bin(sl->rbuff[dlc_pos++]);
185 if (tmp < 0)
Oliver Hartkoppa1044e32010-12-02 10:57:59 +0000186 return;
187 cf.data[i] = (tmp << 4);
Andy Shevchenko81fc70d2011-07-17 22:26:37 +0000188 tmp = hex_to_bin(sl->rbuff[dlc_pos++]);
189 if (tmp < 0)
Oliver Hartkoppa1044e32010-12-02 10:57:59 +0000190 return;
191 cf.data[i] |= tmp;
192 }
193
Oliver Hartkoppa1044e32010-12-02 10:57:59 +0000194 skb = dev_alloc_skb(sizeof(struct can_frame));
195 if (!skb)
196 return;
197
198 skb->dev = sl->dev;
199 skb->protocol = htons(ETH_P_CAN);
200 skb->pkt_type = PACKET_BROADCAST;
201 skb->ip_summed = CHECKSUM_UNNECESSARY;
202 memcpy(skb_put(skb, sizeof(struct can_frame)),
203 &cf, sizeof(struct can_frame));
204 netif_rx(skb);
205
206 sl->dev->stats.rx_packets++;
207 sl->dev->stats.rx_bytes += cf.can_dlc;
208}
209
210/* parse tty input stream */
211static void slcan_unesc(struct slcan *sl, unsigned char s)
212{
213
214 if ((s == '\r') || (s == '\a')) { /* CR or BEL ends the pdu */
215 if (!test_and_clear_bit(SLF_ERROR, &sl->flags) &&
216 (sl->rcount > 4)) {
217 slc_bump(sl);
218 }
219 sl->rcount = 0;
220 } else {
221 if (!test_bit(SLF_ERROR, &sl->flags)) {
222 if (sl->rcount < SLC_MTU) {
223 sl->rbuff[sl->rcount++] = s;
224 return;
225 } else {
226 sl->dev->stats.rx_over_errors++;
227 set_bit(SLF_ERROR, &sl->flags);
228 }
229 }
230 }
231}
232
233 /************************************************************************
234 * STANDARD SLCAN ENCAPSULATION *
235 ************************************************************************/
236
237/* Encapsulate one can_frame and stuff into a TTY queue. */
238static void slc_encaps(struct slcan *sl, struct can_frame *cf)
239{
240 int actual, idx, i;
241 char cmd;
242
243 if (cf->can_id & CAN_RTR_FLAG)
244 cmd = 'R'; /* becomes 'r' in standard frame format */
245 else
246 cmd = 'T'; /* becomes 't' in standard frame format */
247
248 if (cf->can_id & CAN_EFF_FLAG)
249 sprintf(sl->xbuff, "%c%08X%d", cmd,
250 cf->can_id & CAN_EFF_MASK, cf->can_dlc);
251 else
252 sprintf(sl->xbuff, "%c%03X%d", cmd | 0x20,
253 cf->can_id & CAN_SFF_MASK, cf->can_dlc);
254
255 idx = strlen(sl->xbuff);
256
257 for (i = 0; i < cf->can_dlc; i++)
258 sprintf(&sl->xbuff[idx + 2*i], "%02X", cf->data[i]);
259
260 strcat(sl->xbuff, "\r"); /* add terminating character */
261
262 /* Order of next two lines is *very* important.
263 * When we are sending a little amount of data,
264 * the transfer may be completed inside the ops->write()
265 * routine, because it's running with interrupts enabled.
266 * In this case we *never* got WRITE_WAKEUP event,
267 * if we did not request it before write operation.
268 * 14 Oct 1994 Dmitry Gorodchanin.
269 */
270 set_bit(TTY_DO_WRITE_WAKEUP, &sl->tty->flags);
271 actual = sl->tty->ops->write(sl->tty, sl->xbuff, strlen(sl->xbuff));
272 sl->xleft = strlen(sl->xbuff) - actual;
273 sl->xhead = sl->xbuff + actual;
274 sl->dev->stats.tx_bytes += cf->can_dlc;
275}
276
277/*
278 * Called by the driver when there's room for more data. If we have
279 * more packets to send, we send them here.
280 */
281static void slcan_write_wakeup(struct tty_struct *tty)
282{
283 int actual;
284 struct slcan *sl = (struct slcan *) tty->disc_data;
285
286 /* First make sure we're connected. */
287 if (!sl || sl->magic != SLCAN_MAGIC || !netif_running(sl->dev))
288 return;
289
290 if (sl->xleft <= 0) {
291 /* Now serial buffer is almost free & we can start
292 * transmission of another packet */
293 sl->dev->stats.tx_packets++;
294 clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
295 netif_wake_queue(sl->dev);
296 return;
297 }
298
299 actual = tty->ops->write(tty, sl->xhead, sl->xleft);
300 sl->xleft -= actual;
301 sl->xhead += actual;
302}
303
304/* Send a can_frame to a TTY queue. */
305static netdev_tx_t slc_xmit(struct sk_buff *skb, struct net_device *dev)
306{
307 struct slcan *sl = netdev_priv(dev);
308
309 if (skb->len != sizeof(struct can_frame))
310 goto out;
311
312 spin_lock(&sl->lock);
313 if (!netif_running(dev)) {
314 spin_unlock(&sl->lock);
315 printk(KERN_WARNING "%s: xmit: iface is down\n", dev->name);
316 goto out;
317 }
318 if (sl->tty == NULL) {
319 spin_unlock(&sl->lock);
320 goto out;
321 }
322
323 netif_stop_queue(sl->dev);
324 slc_encaps(sl, (struct can_frame *) skb->data); /* encaps & send */
325 spin_unlock(&sl->lock);
326
327out:
328 kfree_skb(skb);
329 return NETDEV_TX_OK;
330}
331
332
333/******************************************
334 * Routines looking at netdevice side.
335 ******************************************/
336
337/* Netdevice UP -> DOWN routine */
338static int slc_close(struct net_device *dev)
339{
340 struct slcan *sl = netdev_priv(dev);
341
342 spin_lock_bh(&sl->lock);
343 if (sl->tty) {
344 /* TTY discipline is running. */
345 clear_bit(TTY_DO_WRITE_WAKEUP, &sl->tty->flags);
346 }
347 netif_stop_queue(dev);
348 sl->rcount = 0;
349 sl->xleft = 0;
350 spin_unlock_bh(&sl->lock);
351
352 return 0;
353}
354
355/* Netdevice DOWN -> UP routine */
356static int slc_open(struct net_device *dev)
357{
358 struct slcan *sl = netdev_priv(dev);
359
360 if (sl->tty == NULL)
361 return -ENODEV;
362
363 sl->flags &= (1 << SLF_INUSE);
364 netif_start_queue(dev);
365 return 0;
366}
367
368/* Hook the destructor so we can free slcan devs at the right point in time */
369static void slc_free_netdev(struct net_device *dev)
370{
371 int i = dev->base_addr;
372 free_netdev(dev);
373 slcan_devs[i] = NULL;
374}
375
376static const struct net_device_ops slc_netdev_ops = {
377 .ndo_open = slc_open,
378 .ndo_stop = slc_close,
379 .ndo_start_xmit = slc_xmit,
380};
381
382static void slc_setup(struct net_device *dev)
383{
384 dev->netdev_ops = &slc_netdev_ops;
385 dev->destructor = slc_free_netdev;
386
387 dev->hard_header_len = 0;
388 dev->addr_len = 0;
389 dev->tx_queue_len = 10;
390
391 dev->mtu = sizeof(struct can_frame);
392 dev->type = ARPHRD_CAN;
393
394 /* New-style flags. */
395 dev->flags = IFF_NOARP;
396 dev->features = NETIF_F_NO_CSUM;
397}
398
399/******************************************
400 Routines looking at TTY side.
401 ******************************************/
402
403/*
404 * Handle the 'receiver data ready' interrupt.
405 * This function is called by the 'tty_io' module in the kernel when
406 * a block of SLCAN data has been received, which can now be decapsulated
407 * and sent on to some IP layer for further processing. This will not
408 * be re-entered while running but other ldisc functions may be called
409 * in parallel
410 */
411
Linus Torvalds55db4c62011-06-04 06:33:24 +0900412static void slcan_receive_buf(struct tty_struct *tty,
Oliver Hartkoppa1044e32010-12-02 10:57:59 +0000413 const unsigned char *cp, char *fp, int count)
414{
415 struct slcan *sl = (struct slcan *) tty->disc_data;
416
417 if (!sl || sl->magic != SLCAN_MAGIC || !netif_running(sl->dev))
Linus Torvalds55db4c62011-06-04 06:33:24 +0900418 return;
Oliver Hartkoppa1044e32010-12-02 10:57:59 +0000419
420 /* Read the characters out of the buffer */
Linus Torvalds55db4c62011-06-04 06:33:24 +0900421 while (count--) {
Oliver Hartkoppa1044e32010-12-02 10:57:59 +0000422 if (fp && *fp++) {
423 if (!test_and_set_bit(SLF_ERROR, &sl->flags))
424 sl->dev->stats.rx_errors++;
425 cp++;
426 continue;
427 }
428 slcan_unesc(sl, *cp++);
429 }
430}
431
432/************************************
433 * slcan_open helper routines.
434 ************************************/
435
436/* Collect hanged up channels */
437static void slc_sync(void)
438{
439 int i;
440 struct net_device *dev;
441 struct slcan *sl;
442
443 for (i = 0; i < maxdev; i++) {
444 dev = slcan_devs[i];
445 if (dev == NULL)
446 break;
447
448 sl = netdev_priv(dev);
449 if (sl->tty || sl->leased)
450 continue;
451 if (dev->flags & IFF_UP)
452 dev_close(dev);
453 }
454}
455
456/* Find a free SLCAN channel, and link in this `tty' line. */
457static struct slcan *slc_alloc(dev_t line)
458{
459 int i;
Oliver Hartkopp2af4ba82011-07-14 21:49:19 +0000460 char name[IFNAMSIZ];
Oliver Hartkoppa1044e32010-12-02 10:57:59 +0000461 struct net_device *dev = NULL;
462 struct slcan *sl;
463
Oliver Hartkoppa1044e32010-12-02 10:57:59 +0000464 for (i = 0; i < maxdev; i++) {
465 dev = slcan_devs[i];
466 if (dev == NULL)
467 break;
468
469 }
470
471 /* Sorry, too many, all slots in use */
472 if (i >= maxdev)
473 return NULL;
474
Oliver Hartkopp2af4ba82011-07-14 21:49:19 +0000475 sprintf(name, "slcan%d", i);
476 dev = alloc_netdev(sizeof(*sl), name, slc_setup);
477 if (!dev)
478 return NULL;
Oliver Hartkoppa1044e32010-12-02 10:57:59 +0000479
Oliver Hartkopp2af4ba82011-07-14 21:49:19 +0000480 dev->base_addr = i;
Oliver Hartkoppa1044e32010-12-02 10:57:59 +0000481 sl = netdev_priv(dev);
482
483 /* Initialize channel control data */
484 sl->magic = SLCAN_MAGIC;
485 sl->dev = dev;
486 spin_lock_init(&sl->lock);
487 slcan_devs[i] = dev;
488
489 return sl;
490}
491
492/*
493 * Open the high-level part of the SLCAN channel.
494 * This function is called by the TTY module when the
495 * SLCAN line discipline is called for. Because we are
496 * sure the tty line exists, we only have to link it to
497 * a free SLCAN channel...
498 *
499 * Called in process context serialized from other ldisc calls.
500 */
501
502static int slcan_open(struct tty_struct *tty)
503{
504 struct slcan *sl;
505 int err;
506
507 if (!capable(CAP_NET_ADMIN))
508 return -EPERM;
509
510 if (tty->ops->write == NULL)
511 return -EOPNOTSUPP;
512
513 /* RTnetlink lock is misused here to serialize concurrent
514 opens of slcan channels. There are better ways, but it is
515 the simplest one.
516 */
517 rtnl_lock();
518
519 /* Collect hanged up channels. */
520 slc_sync();
521
522 sl = tty->disc_data;
523
524 err = -EEXIST;
525 /* First make sure we're not already connected. */
526 if (sl && sl->magic == SLCAN_MAGIC)
527 goto err_exit;
528
529 /* OK. Find a free SLCAN channel to use. */
530 err = -ENFILE;
531 sl = slc_alloc(tty_devnum(tty));
532 if (sl == NULL)
533 goto err_exit;
534
535 sl->tty = tty;
536 tty->disc_data = sl;
537 sl->line = tty_devnum(tty);
538 sl->pid = current->pid;
539
540 if (!test_bit(SLF_INUSE, &sl->flags)) {
541 /* Perform the low-level SLCAN initialization. */
542 sl->rcount = 0;
543 sl->xleft = 0;
544
545 set_bit(SLF_INUSE, &sl->flags);
546
547 err = register_netdevice(sl->dev);
548 if (err)
549 goto err_free_chan;
550 }
551
552 /* Done. We have linked the TTY line to a channel. */
553 rtnl_unlock();
554 tty->receive_room = 65536; /* We don't flow control */
Oliver Hartkopp0d4420a2011-05-10 13:12:30 -0700555
556 /* TTY layer expects 0 on success */
557 return 0;
Oliver Hartkoppa1044e32010-12-02 10:57:59 +0000558
559err_free_chan:
560 sl->tty = NULL;
561 tty->disc_data = NULL;
562 clear_bit(SLF_INUSE, &sl->flags);
563
564err_exit:
565 rtnl_unlock();
566
567 /* Count references from TTY module */
568 return err;
569}
570
571/*
572 * Close down a SLCAN channel.
573 * This means flushing out any pending queues, and then returning. This
574 * call is serialized against other ldisc functions.
575 *
576 * We also use this method for a hangup event.
577 */
578
579static void slcan_close(struct tty_struct *tty)
580{
581 struct slcan *sl = (struct slcan *) tty->disc_data;
582
583 /* First make sure we're connected. */
584 if (!sl || sl->magic != SLCAN_MAGIC || sl->tty != tty)
585 return;
586
587 tty->disc_data = NULL;
588 sl->tty = NULL;
589 if (!sl->leased)
590 sl->line = 0;
591
592 /* Flush network side */
593 unregister_netdev(sl->dev);
594 /* This will complete via sl_free_netdev */
595}
596
597static int slcan_hangup(struct tty_struct *tty)
598{
599 slcan_close(tty);
600 return 0;
601}
602
603/* Perform I/O control on an active SLCAN channel. */
604static int slcan_ioctl(struct tty_struct *tty, struct file *file,
605 unsigned int cmd, unsigned long arg)
606{
607 struct slcan *sl = (struct slcan *) tty->disc_data;
608 unsigned int tmp;
609
610 /* First make sure we're connected. */
611 if (!sl || sl->magic != SLCAN_MAGIC)
612 return -EINVAL;
613
614 switch (cmd) {
615 case SIOCGIFNAME:
616 tmp = strlen(sl->dev->name) + 1;
617 if (copy_to_user((void __user *)arg, sl->dev->name, tmp))
618 return -EFAULT;
619 return 0;
620
621 case SIOCSIFHWADDR:
622 return -EINVAL;
623
624 default:
625 return tty_mode_ioctl(tty, file, cmd, arg);
626 }
627}
628
629static struct tty_ldisc_ops slc_ldisc = {
630 .owner = THIS_MODULE,
631 .magic = TTY_LDISC_MAGIC,
632 .name = "slcan",
633 .open = slcan_open,
634 .close = slcan_close,
635 .hangup = slcan_hangup,
636 .ioctl = slcan_ioctl,
637 .receive_buf = slcan_receive_buf,
638 .write_wakeup = slcan_write_wakeup,
639};
640
641static int __init slcan_init(void)
642{
643 int status;
644
645 if (maxdev < 4)
646 maxdev = 4; /* Sanity */
647
648 printk(banner);
649 printk(KERN_INFO "slcan: %d dynamic interface channels.\n", maxdev);
650
651 slcan_devs = kzalloc(sizeof(struct net_device *)*maxdev, GFP_KERNEL);
652 if (!slcan_devs) {
653 printk(KERN_ERR "slcan: can't allocate slcan device array!\n");
654 return -ENOMEM;
655 }
656
657 /* Fill in our line protocol discipline, and register it */
658 status = tty_register_ldisc(N_SLCAN, &slc_ldisc);
659 if (status) {
660 printk(KERN_ERR "slcan: can't register line discipline\n");
661 kfree(slcan_devs);
662 }
663 return status;
664}
665
666static void __exit slcan_exit(void)
667{
668 int i;
669 struct net_device *dev;
670 struct slcan *sl;
671 unsigned long timeout = jiffies + HZ;
672 int busy = 0;
673
674 if (slcan_devs == NULL)
675 return;
676
677 /* First of all: check for active disciplines and hangup them.
678 */
679 do {
680 if (busy)
681 msleep_interruptible(100);
682
683 busy = 0;
684 for (i = 0; i < maxdev; i++) {
685 dev = slcan_devs[i];
686 if (!dev)
687 continue;
688 sl = netdev_priv(dev);
689 spin_lock_bh(&sl->lock);
690 if (sl->tty) {
691 busy++;
692 tty_hangup(sl->tty);
693 }
694 spin_unlock_bh(&sl->lock);
695 }
696 } while (busy && time_before(jiffies, timeout));
697
698 /* FIXME: hangup is async so we should wait when doing this second
699 phase */
700
701 for (i = 0; i < maxdev; i++) {
702 dev = slcan_devs[i];
703 if (!dev)
704 continue;
705 slcan_devs[i] = NULL;
706
707 sl = netdev_priv(dev);
708 if (sl->tty) {
709 printk(KERN_ERR "%s: tty discipline still running\n",
710 dev->name);
711 /* Intentionally leak the control block. */
712 dev->destructor = NULL;
713 }
714
715 unregister_netdev(dev);
716 }
717
718 kfree(slcan_devs);
719 slcan_devs = NULL;
720
721 i = tty_unregister_ldisc(N_SLCAN);
722 if (i)
723 printk(KERN_ERR "slcan: can't unregister ldisc (err %d)\n", i);
724}
725
726module_init(slcan_init);
727module_exit(slcan_exit);