]> rtime.felk.cvut.cz Git - socketcan-devel.git/blob - kernel/2.6/drivers/net/can/slcan.c
Added new skb->pkt_type in received skb's.
[socketcan-devel.git] / kernel / 2.6 / drivers / net / can / slcan.c
1 /*
2  * slcan.c - serial line CAN interface driver (using tty line discipline)
3  *
4  * Copyright (c) 2007 Volkswagen Group Electronic Research
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions, the following disclaimer and
12  *    the referenced file 'COPYING'.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. Neither the name of Volkswagen nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * Alternatively, provided that this notice is retained in full, this
21  * software may be distributed under the terms of the GNU General
22  * Public License ("GPL") version 2 as distributed in the 'COPYING'
23  * file from the main directory of the linux kernel source.
24  *
25  * The provided data structures and external interfaces from this code
26  * are not restricted to be used by modules with a GPL compatible license.
27  *
28  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
29  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
30  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
31  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
32  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
33  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
34  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
35  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
36  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
37  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
38  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
39  * DAMAGE.
40  *
41  * Send feedback to <socketcan-users@lists.berlios.de>
42  *
43  */
44
45 /*
46  * This file is derived from linux/drivers/net/slip.c
47  *
48  * Therefore it has the same (strange?) behaviour not to unregister the
49  * netdevice when detaching the tty. Is there any better solution?
50  *
51  * Do not try to attach, detach and re-attach a tty for this reason ...
52  *
53  * slip.c Authors: Laurence Culhane, <loz@holmes.demon.co.uk>
54  *                 Fred N. van Kempen, <waltje@uwalt.nl.mugnet.org>
55  */
56
57 #include <linux/module.h>
58 #include <linux/moduleparam.h>
59
60 #include <asm/system.h>
61 #include <asm/uaccess.h>
62 #include <linux/bitops.h>
63 #include <linux/string.h>
64 #include <linux/mm.h>
65 #include <linux/interrupt.h>
66 #include <linux/in.h>
67 #include <linux/tty.h>
68 #include <linux/errno.h>
69 #include <linux/netdevice.h>
70 #include <linux/etherdevice.h>
71 #include <linux/skbuff.h>
72 #include <linux/rtnetlink.h>
73 #include <linux/if_arp.h>
74 #include <linux/if_ether.h>
75 #include <linux/if_slip.h>
76 #include <linux/delay.h>
77 #include <linux/init.h>
78
79 #include <linux/can.h>
80
81 #include <linux/can/version.h> /* for RCSID. Removed by mkpatch script */
82 RCSID("$Id$");
83
84 static __initdata const char banner[] =
85         KERN_INFO "slcan: serial line CAN interface driver\n";
86
87 MODULE_ALIAS_LDISC(N_SLCAN);
88 MODULE_DESCRIPTION("serial line CAN interface");
89 MODULE_LICENSE("Dual BSD/GPL");
90 MODULE_AUTHOR("Oliver Hartkopp <oliver.hartkopp@volkswagen.de>");
91
92 #ifdef CONFIG_CAN_DEBUG_DEVICES
93 static int debug = 0;
94 module_param(debug, int, S_IRUGO);
95 #define DBG(args...)       (debug & 1 ? \
96                                (printk(KERN_DEBUG "slcan %s: ", __func__), \
97                                 printk(args)) : 0)
98 #else
99 #define DBG(args...)
100 #endif
101
102 #ifndef N_SLCAN
103 #error Your kernel does not support tty line discipline N_SLCAN
104 #endif
105 /*
106  * As there is currently no line discipline N_SLCAN in the mainstream kernel
107  * you will have to modify two kernel includes & recompile the kernel.
108  *
109  * Add this in include/asm/termios.h after the definition of N_HCI:
110  *        #define N_SLCAN         16 
111  *
112  * Increment NR_LDICS in include/linux/tty.h from 16 to 17
113  *
114  * NEW: Since Kernel 2.6.21 you only have to change include/linux/tty.h
115  *
116  */
117
118 #define SLC_CHECK_TRANSMIT
119 #define SLCAN_MAGIC 0x53CA
120
121 static int maxdev = 10;         /* MAX number of SLCAN channels;
122                                    This can be overridden with
123                                    insmod slcan.ko maxdev=nnn   */
124 module_param(maxdev, int, 0);
125 MODULE_PARM_DESC(maxdev, "Maximum number of slcan interfaces");
126
127 /* maximum rx buffer len: extended CAN frame with timestamp */
128 #define SLC_MTU (sizeof("T1111222281122334455667788EA5F\r")+1)
129
130 struct slcan {
131         int                     magic;
132
133         /* Various fields. */
134         struct tty_struct       *tty;           /* ptr to TTY structure      */
135         struct net_device       *dev;           /* easy for intr handling    */
136         spinlock_t              lock;
137
138         /* These are pointers to the malloc()ed frame buffers. */
139         unsigned char           rbuff[SLC_MTU]; /* receiver buffer           */
140         int                     rcount;         /* received chars counter    */
141         unsigned char           xbuff[SLC_MTU]; /* transmitter buffer        */
142         unsigned char           *xhead;         /* pointer to next XMIT byte */
143         int                     xleft;          /* bytes left in XMIT queue  */
144
145         /* SLCAN interface statistics. */
146         struct net_device_stats stats;
147
148         unsigned long           flags;          /* Flag values/ mode etc     */
149 #define SLF_INUSE               0               /* Channel in use            */
150 #define SLF_ERROR               1               /* Parity, etc. error        */
151
152         unsigned char           leased;
153         dev_t                   line;
154         pid_t                   pid;
155 };
156
157 static struct net_device **slcan_devs;
158
159  /************************************************************************
160   *                     SLCAN ENCAPSULATION FORMAT                       *
161   ************************************************************************/
162
163 /*
164  * A CAN frame has a can_id (11 bit standard frame format OR 29 bit extended
165  * frame format) a data length code (can_dlc) which can be from 0 to 8
166  * and up to <can_dlc> data bytes as payload.
167  * Additionally a CAN frame may become a remote transmission frame if the
168  * RTR-bit is set. This causes another ECU to send a CAN frame with the
169  * given can_id.
170  *
171  * The SLCAN ASCII representation of these different frame types is:
172  * <type> <id> <dlc> <data>*
173  *
174  * Extended frames (29 bit) are defined by capital characters in the type.
175  * RTR frames are defined as 'r' types - normal frames have 't' type:
176  * t => 11 bit data frame
177  * r => 11 bit RTR frame
178  * T => 29 bit data frame
179  * R => 29 bit RTR frame
180  *
181  * The <id> is 3 (standard) or 8 (extended) bytes in ASCII Hex (base64).
182  * The <dlc> is a one byte ASCII number ('0' - '8')
183  * The <data> section has at much ASCII Hex bytes as defined by the <dlc>
184  * 
185  * Examples:
186  *
187  * t1230 : can_id 0x123, can_dlc 0, no data
188  * t4563112233 : can_id 0x456, can_dlc 3, data 0x11 0x22 0x33
189  * T12ABCDEF2AA55 : extended can_id 0x12ABCDEF, can_dlc 2, data 0xAA 0x55
190  * r1230 : can_id 0x123, can_dlc 0, no data, remote transmission request
191  *
192  */
193
194  /************************************************************************
195   *                     STANDARD SLCAN DECAPSULATION                     *
196   ************************************************************************/
197
198 static int asc2nibble(char c) {
199
200         if ((c >= '0') && (c <= '9'))
201                 return c - '0';
202
203         if ((c >= 'A') && (c <= 'F'))
204                 return c - 'A' + 10;
205
206         if ((c >= 'a') && (c <= 'f'))
207                 return c - 'a' + 10;
208
209         return 16; /* error */
210 }
211
212 /* Send one completely decapsulated can_frame to the network layer */
213 static void slc_bump(struct slcan *sl)
214 {
215         struct sk_buff *skb;
216         struct can_frame cf;
217         int i, dlc_pos, tmp;
218         char cmd = sl->rbuff[0];
219
220         if ((cmd != 't') && (cmd != 'T') && (cmd != 'r') && (cmd != 'R'))
221                 return;
222
223         if (cmd & 0x20) /* tiny chars 'r' 't' => standard frame format */
224                 dlc_pos = 4; /* dlc position tiiid */
225         else
226                 dlc_pos = 9; /* dlc position Tiiiiiiiid */
227
228         if (!((sl->rbuff[dlc_pos] >= '0') && (sl->rbuff[dlc_pos] < '9')))
229                 return;
230
231         cf.can_dlc = sl->rbuff[dlc_pos] & 0x0F; /* get can_dlc */
232
233         sl->rbuff[dlc_pos] = 0; /* terminate can_id string */
234         cf.can_id = simple_strtoul(sl->rbuff+1, NULL, 16);
235         
236         if (!(cmd & 0x20)) /* NO tiny chars => extended frame format */
237                 cf.can_id |= CAN_EFF_FLAG;
238
239         if ((cmd | 0x20) == 'r') /* RTR frame */
240                 cf.can_id |= CAN_RTR_FLAG;
241
242         *(u64 *) (&cf.data) = 0; /* clear payload */
243
244         for (i = 0, dlc_pos++; i < cf.can_dlc; i++){
245                 
246                 if ((tmp = asc2nibble(sl->rbuff[dlc_pos++])) > 0x0F)
247                         return;
248                 cf.data[i] = (tmp << 4);
249                 if ((tmp = asc2nibble(sl->rbuff[dlc_pos++])) > 0x0F)
250                         return;
251                 cf.data[i] |= tmp;
252         }
253
254
255         skb = dev_alloc_skb(sizeof(struct can_frame));
256         if (!skb)
257                 return;
258
259         skb->dev = sl->dev;
260         skb->protocol = htons(ETH_P_CAN);
261         skb->pkt_type = PACKET_BROADCAST;
262         skb->ip_summed = CHECKSUM_UNNECESSARY;
263         memcpy(skb_put(skb, sizeof(struct can_frame)),
264                &cf, sizeof(struct can_frame));
265         netif_rx(skb);
266
267         sl->dev->last_rx = jiffies;
268         sl->stats.rx_packets++;
269         sl->stats.rx_bytes += cf.can_dlc;
270 }
271
272 /* parse tty input stream */
273 static void slcan_unesc(struct slcan *sl, unsigned char s)
274 {
275         if ((s == '\r') || (s == '\a')) { /* CR or BEL ends the pdu */
276                 if (!test_and_clear_bit(SLF_ERROR, &sl->flags) &&
277                     (sl->rcount > 4))  {
278                         slc_bump(sl);
279                 }
280                 sl->rcount = 0;
281         } else {
282                 if (!test_bit(SLF_ERROR, &sl->flags))  {
283                         if (sl->rcount < SLC_MTU)  {
284                                 sl->rbuff[sl->rcount++] = s;
285                                 return;
286                         } else {
287                                 sl->stats.rx_over_errors++;
288                                 set_bit(SLF_ERROR, &sl->flags);
289                         }
290                 }
291         }
292 }
293
294  /************************************************************************
295   *                     STANDARD SLCAN ENCAPSULATION                     *
296   ************************************************************************/
297
298 /* Encapsulate one can_frame and stuff into a TTY queue. */
299 static void slc_encaps(struct slcan *sl, struct can_frame *cf)
300 {
301         int actual, idx, i;
302         char cmd;
303
304         if (cf->can_id & CAN_RTR_FLAG)
305                 cmd = 'R'; /* becomes 'r' in standard frame format */
306         else
307                 cmd = 'T'; /* becomes 't' in standard frame format */
308
309         if (cf->can_id & CAN_EFF_FLAG)
310                 sprintf(sl->xbuff, "%c%08X%d", cmd,
311                         cf->can_id & CAN_EFF_MASK, cf->can_dlc);
312         else
313                 sprintf(sl->xbuff, "%c%03X%d", cmd | 0x20,
314                         cf->can_id & CAN_SFF_MASK, cf->can_dlc);
315
316         idx = strlen(sl->xbuff);
317
318         for (i = 0; i < cf->can_dlc; i++)
319                 sprintf(&sl->xbuff[idx + 2*i], "%02X", cf->data[i]);
320
321         DBG("ASCII frame = '%s'\n", sl->xbuff);
322
323         strcat(sl->xbuff, "\r"); /* add terminating character */
324
325         /* Order of next two lines is *very* important.
326          * When we are sending a little amount of data,
327          * the transfer may be completed inside driver.write()
328          * routine, because it's running with interrupts enabled.
329          * In this case we *never* got WRITE_WAKEUP event,
330          * if we did not request it before write operation.
331          *       14 Oct 1994  Dmitry Gorodchanin.
332          */
333         sl->tty->flags |= (1 << TTY_DO_WRITE_WAKEUP);
334         actual = sl->tty->driver->write(sl->tty, sl->xbuff, strlen(sl->xbuff));
335 #ifdef SLC_CHECK_TRANSMIT
336         sl->dev->trans_start = jiffies;
337 #endif
338         sl->xleft = strlen(sl->xbuff) - actual;
339         sl->xhead = sl->xbuff + actual;
340         sl->stats.tx_bytes += cf->can_dlc;
341 }
342
343 /*
344  * Called by the driver when there's room for more data.  If we have
345  * more packets to send, we send them here.
346  */
347 static void slcan_write_wakeup(struct tty_struct *tty)
348 {
349         int actual;
350         struct slcan *sl = (struct slcan *) tty->disc_data;
351
352         /* First make sure we're connected. */
353         if (!sl || sl->magic != SLCAN_MAGIC || !netif_running(sl->dev)) {
354                 return;
355         }
356         if (sl->xleft <= 0)  {
357                 /* Now serial buffer is almost free & we can start
358                  * transmission of another packet */
359                 sl->stats.tx_packets++;
360                 tty->flags &= ~(1 << TTY_DO_WRITE_WAKEUP);
361                 netif_wake_queue(sl->dev);
362                 return;
363         }
364
365         actual = tty->driver->write(tty, sl->xhead, sl->xleft);
366         sl->xleft -= actual;
367         sl->xhead += actual;
368 }
369
370 static void slc_tx_timeout(struct net_device *dev)
371 {
372         struct slcan *sl = netdev_priv(dev);
373
374         spin_lock(&sl->lock);
375
376         if (netif_queue_stopped(dev)) {
377                 if (!netif_running(dev))
378                         goto out;
379
380                 /* May be we must check transmitter timeout here ?
381                  *      14 Oct 1994 Dmitry Gorodchanin.
382                  */
383 #ifdef SLC_CHECK_TRANSMIT
384                 if (time_before(jiffies, dev->trans_start + 20 * HZ))  {
385                         /* 20 sec timeout not reached */
386                         goto out;
387                 }
388                 printk(KERN_WARNING "%s: transmit timed out, %s?\n", dev->name,
389                        (sl->tty->driver->chars_in_buffer(sl->tty) || sl->xleft)
390                        ? "bad line quality" : "driver error");
391                 sl->xleft = 0;
392                 sl->tty->flags &= ~(1 << TTY_DO_WRITE_WAKEUP);
393                 netif_wake_queue(sl->dev);
394 #endif
395         }
396 out:
397         spin_unlock(&sl->lock);
398 }
399
400
401 /******************************************
402  *   Routines looking at netdevice side.
403  ******************************************/
404
405 /* Send a can_frame to a TTY queue. */
406 static int slc_xmit(struct sk_buff *skb, struct net_device *dev)
407 {
408         struct slcan *sl = netdev_priv(dev);
409
410         if (skb->len != sizeof(struct can_frame))
411                 goto out;
412
413         spin_lock(&sl->lock);
414         if (!netif_running(dev))  {
415                 spin_unlock(&sl->lock);
416                 printk(KERN_WARNING "%s: xmit: iface is down\n", dev->name);
417                 goto out;
418         }
419
420         if (sl->tty == NULL) {
421                 spin_unlock(&sl->lock);
422                 goto out;
423         }
424
425         netif_stop_queue(sl->dev);
426         slc_encaps(sl, (struct can_frame *) skb->data); /* encaps & send */
427         spin_unlock(&sl->lock);
428
429 out:
430         kfree_skb(skb);
431         return 0;
432 }
433
434
435 /* Netdevice UP -> DOWN routine */
436 static int slc_close(struct net_device *dev)
437 {
438         struct slcan *sl = netdev_priv(dev);
439
440         spin_lock_bh(&sl->lock);
441         if (sl->tty) {
442                 /* TTY discipline is running. */
443                 sl->tty->flags &= ~(1 << TTY_DO_WRITE_WAKEUP);
444         }
445         netif_stop_queue(dev);
446         sl->rcount   = 0;
447         sl->xleft    = 0;
448         spin_unlock_bh(&sl->lock);
449
450         return 0;
451 }
452
453 /* Netdevice DOWN -> UP routine */
454 static int slc_open(struct net_device *dev)
455 {
456         struct slcan *sl = netdev_priv(dev);
457
458         if (sl->tty==NULL)
459                 return -ENODEV;
460
461         sl->flags &= (1 << SLF_INUSE);
462         netif_start_queue(dev);
463         return 0;
464 }
465
466 /* Netdevice get statistics request */
467 static struct net_device_stats *slc_get_stats(struct net_device *dev)
468 {
469         struct slcan *sl = netdev_priv(dev);
470
471         return (&sl->stats);
472 }
473
474 /* Netdevice register callback */
475 static void slc_setup(struct net_device *dev)
476 {
477         dev->open               = slc_open;
478         dev->destructor         = free_netdev;
479         dev->stop               = slc_close;
480         dev->get_stats          = slc_get_stats;
481         dev->hard_start_xmit    = slc_xmit;
482
483         dev->hard_header_len    = 0;
484         dev->addr_len           = 0;
485         dev->tx_queue_len       = 10;
486
487         SET_MODULE_OWNER(dev);
488
489         dev->mtu                = sizeof(struct can_frame);
490         dev->type               = ARPHRD_CAN;
491 #ifdef SLC_CHECK_TRANSMIT
492         dev->tx_timeout         = slc_tx_timeout;
493         dev->watchdog_timeo     = 20*HZ;
494 #endif
495
496         /* New-style flags. */
497         dev->flags              = IFF_NOARP;
498         dev->features           = NETIF_F_NO_CSUM;
499 }
500
501 /******************************************
502  * Routines looking at TTY side.
503  ******************************************/
504
505 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,16)
506 static int slcan_receive_room(struct tty_struct *tty)
507 {
508         return 65536;  /* We can handle an infinite amount of data. :-) */
509 }
510 #endif
511
512 /*
513  * Handle the 'receiver data ready' interrupt.
514  * This function is called by the 'tty_io' module in the kernel when
515  * a block of SLCAN data has been received, which can now be decapsulated
516  * and sent on to some IP layer for further processing. This will not
517  * be re-entered while running but other ldisc functions may be called
518  * in parallel
519  */
520
521 static void slcan_receive_buf(struct tty_struct *tty,
522                               const unsigned char *cp, char *fp, int count)
523 {
524         struct slcan *sl = (struct slcan *) tty->disc_data;
525
526         if (!sl || sl->magic != SLCAN_MAGIC ||
527             !netif_running(sl->dev))
528                 return;
529
530         /* Read the characters out of the buffer */
531         while (count--) {
532                 if (fp && *fp++) {
533                         if (!test_and_set_bit(SLF_ERROR, &sl->flags))  {
534                                 sl->stats.rx_errors++;
535                         }
536                         cp++;
537                         continue;
538                 }
539                 slcan_unesc(sl, *cp++);
540         }
541 }
542
543 /************************************
544  *  slcan_open helper routines.
545  ************************************/
546
547 /* Collect hanged up channels */
548
549 static void slc_sync(void)
550 {
551         int i;
552         struct net_device *dev;
553         struct slcan      *sl;
554
555         for (i = 0; i < maxdev; i++) {
556                 if ((dev = slcan_devs[i]) == NULL)
557                         break;
558
559                 sl = netdev_priv(dev);
560                 if (sl->tty || sl->leased)
561                         continue;
562                 if (dev->flags&IFF_UP)
563                         dev_close(dev);
564         }
565 }
566
567
568 /* Find a free SLCAN channel, and link in this `tty' line. */
569 static struct slcan *slc_alloc(dev_t line)
570 {
571         int i;
572         int sel = -1;
573         int score = -1;
574         struct net_device *dev = NULL;
575         struct slcan       *sl;
576
577         if (slcan_devs == NULL)
578                 return NULL;    /* Master array missing ! */
579
580         for (i = 0; i < maxdev; i++) {
581                 dev = slcan_devs[i];
582                 if (dev == NULL)
583                         break;
584
585                 sl = netdev_priv(dev);
586                 if (sl->leased) {
587                         if (sl->line != line)
588                                 continue;
589                         if (sl->tty)
590                                 return NULL;
591
592                         /* Clear ESCAPE & ERROR flags */
593                         sl->flags &= (1 << SLF_INUSE);
594                         return sl;
595                 }
596
597                 if (sl->tty)
598                         continue;
599
600                 if (current->pid == sl->pid) {
601                         if (sl->line == line && score < 3) {
602                                 sel = i;
603                                 score = 3;
604                                 continue;
605                         }
606                         if (score < 2) {
607                                 sel = i;
608                                 score = 2;
609                         }
610                         continue;
611                 }
612                 if (sl->line == line && score < 1) {
613                         sel = i;
614                         score = 1;
615                         continue;
616                 }
617                 if (score < 0) {
618                         sel = i;
619                         score = 0;
620                 }
621         }
622
623         if (sel >= 0) {
624                 i = sel;
625                 dev = slcan_devs[i];
626                 if (score > 1) {
627                         sl = netdev_priv(dev);
628                         sl->flags &= (1 << SLF_INUSE);
629                         return sl;
630                 }
631         }
632
633         /* Sorry, too many, all slots in use */
634         if (i >= maxdev)
635                 return NULL;
636
637         if (dev) {
638                 sl = netdev_priv(dev);
639                 if (test_bit(SLF_INUSE, &sl->flags)) {
640                         unregister_netdevice(dev);
641                         free_netdev(dev); /* new in slcan.c */
642                         dev = NULL;
643                         slcan_devs[i] = NULL;
644                 }
645         }
646
647         if (!dev) {
648                 char name[IFNAMSIZ];
649                 sprintf(name, "slc%d", i);
650
651                 dev = alloc_netdev(sizeof(*sl), name, slc_setup);
652                 if (!dev)
653                         return NULL;
654                 dev->base_addr  = i;
655         }
656
657         sl = netdev_priv(dev);
658
659         /* Initialize channel control data */
660         sl->magic       = SLCAN_MAGIC;
661         sl->dev         = dev;
662         spin_lock_init(&sl->lock);
663         slcan_devs[i] = dev;
664
665         return sl;
666 }
667
668 /*
669  * Open the high-level part of the SLCAN channel.
670  * This function is called by the TTY module when the
671  * SLCAN line discipline is called for.  Because we are
672  * sure the tty line exists, we only have to link it to
673  * a free SLCAN channel...
674  *
675  * Called in process context serialized from other ldisc calls.
676  */
677
678 static int slcan_open(struct tty_struct *tty)
679 {
680         struct slcan *sl;
681         int err;
682
683         if(!capable(CAP_NET_ADMIN))
684                 return -EPERM;
685
686         /* RTnetlink lock is misused here to serialize concurrent
687            opens of slcan channels. There are better ways, but it is
688            the simplest one.
689          */
690         rtnl_lock();
691
692         /* Collect hanged up channels. */
693         slc_sync();
694
695         sl = (struct slcan *) tty->disc_data;
696
697         err = -EEXIST;
698         /* First make sure we're not already connected. */
699         if (sl && sl->magic == SLCAN_MAGIC)
700                 goto err_exit;
701
702         /* OK.  Find a free SLCAN channel to use. */
703         err = -ENFILE;
704         if ((sl = slc_alloc(tty_devnum(tty))) == NULL)
705                 goto err_exit;
706
707         sl->tty = tty;
708         tty->disc_data = sl;
709         sl->line = tty_devnum(tty);
710         sl->pid = current->pid;
711
712 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,16)
713         /* FIXME: already done before we were called - seems this can go */
714         if (tty->driver->flush_buffer)
715                 tty->driver->flush_buffer(tty);
716 #endif
717
718         if (!test_bit(SLF_INUSE, &sl->flags)) {
719                 /* Perform the low-level SLCAN initialization. */
720                 sl->rcount   = 0;
721                 sl->xleft    = 0;
722
723                 set_bit(SLF_INUSE, &sl->flags);
724
725                 if ((err = register_netdevice(sl->dev)))
726                         goto err_free_chan;
727         }
728
729         /* Done.  We have linked the TTY line to a channel. */
730         rtnl_unlock();
731
732 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,16)
733         tty->receive_room = 65536;      /* We don't flow control */
734 #endif
735
736         return sl->dev->base_addr;
737
738 err_free_chan:
739         sl->tty = NULL;
740         tty->disc_data = NULL;
741         clear_bit(SLF_INUSE, &sl->flags);
742
743 err_exit:
744         rtnl_unlock();
745
746         /* Count references from TTY module */
747         return err;
748 }
749
750 /*
751
752   FIXME: 1,2 are fixed 3 was never true anyway.
753
754    Let me to blame a bit.
755    1. TTY module calls this funstion on soft interrupt.
756    2. TTY module calls this function WITH MASKED INTERRUPTS!
757    3. TTY module does not notify us about line discipline
758       shutdown,
759
760    Seems, now it is clean. The solution is to consider netdevice and
761    line discipline sides as two independent threads.
762
763    By-product (not desired): slc? does not feel hangups and remains open.
764    It is supposed, that user level program (dip, diald, slattach...)
765    will catch SIGHUP and make the rest of work.
766
767    I see no way to make more with current tty code. --ANK
768  */
769
770 /*
771  * Close down a SLCAN channel.
772  * This means flushing out any pending queues, and then returning. This
773  * call is serialized against other ldisc functions.
774  */
775 static void slcan_close(struct tty_struct *tty)
776 {
777         struct slcan *sl = (struct slcan *) tty->disc_data;
778
779         /* First make sure we're connected. */
780         if (!sl || sl->magic != SLCAN_MAGIC || sl->tty != tty)
781                 return;
782
783         tty->disc_data = NULL;
784         sl->tty = NULL;
785         if (!sl->leased)
786                 sl->line = 0;
787
788         /* Count references from TTY module */
789 }
790
791 /* Perform I/O control on an active SLCAN channel. */
792 static int slcan_ioctl(struct tty_struct *tty, struct file *file,
793                        unsigned int cmd, unsigned long arg)
794 {
795         struct slcan *sl = (struct slcan *) tty->disc_data;
796         unsigned int tmp;
797
798         /* First make sure we're connected. */
799         if (!sl || sl->magic != SLCAN_MAGIC) {
800                 return -EINVAL;
801         }
802
803         switch(cmd) {
804         case SIOCGIFNAME:
805                 tmp = strlen(sl->dev->name) + 1;
806                 if (copy_to_user((void __user *)arg, sl->dev->name, tmp))
807                         return -EFAULT;
808                 return 0;
809
810         case SIOCSIFHWADDR:
811                 return -EINVAL;
812
813
814         /* Allow stty to read, but not set, the serial port */
815         case TCGETS:
816         case TCGETA:
817                 return n_tty_ioctl(tty, file, cmd, arg);
818
819         default:
820                 return -ENOIOCTLCMD;
821         }
822 }
823
824 static struct tty_ldisc slc_ldisc = {
825         .owner          = THIS_MODULE,
826         .magic          = TTY_LDISC_MAGIC,
827         .name           = "slcan",
828         .open           = slcan_open,
829         .close          = slcan_close,
830         .ioctl          = slcan_ioctl,
831         .receive_buf    = slcan_receive_buf,
832 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,16)
833         .receive_room   = slcan_receive_room,
834 #endif
835         .write_wakeup   = slcan_write_wakeup,
836 };
837
838 /************************************
839  * general slcan module init/exit
840  ************************************/
841
842 static int __init slcan_init(void)
843 {
844         int status;
845
846         if (maxdev < 4)
847                 maxdev = 4; /* Sanity */
848
849         printk(banner);
850         printk(KERN_INFO "slcan: %d dynamic interface channels.\n", maxdev );
851
852         slcan_devs = kmalloc(sizeof(struct net_device *)*maxdev, GFP_KERNEL);
853         if (!slcan_devs) {
854                 printk(KERN_ERR "slcan: can't allocate slcan device array!\n");
855                 return -ENOMEM;
856         }
857
858         /* Clear the pointer array, we allocate devices when we need them */
859         memset(slcan_devs, 0, sizeof(struct net_device *)*maxdev);
860
861         /* Fill in our line protocol discipline, and register it */
862         if ((status = tty_register_ldisc(N_SLCAN, &slc_ldisc)) != 0)  {
863                 printk(KERN_ERR "slcan: can't register line discipline\n");
864                 kfree(slcan_devs);
865         }
866         return status;
867 }
868
869 static void __exit slcan_exit(void)
870 {
871         int i;
872         struct net_device *dev;
873         struct slcan *sl;
874         unsigned long timeout = jiffies + HZ;
875         int busy = 0;
876
877         if (slcan_devs == NULL)
878                 return;
879
880         /* First of all: check for active disciplines and hangup them.
881          */
882         do {
883                 if (busy)
884                         msleep_interruptible(100);
885
886                 busy = 0;
887                 for (i = 0; i < maxdev; i++) {
888                         dev = slcan_devs[i];
889                         if (!dev)
890                                 continue;
891                         sl = netdev_priv(dev);
892                         spin_lock_bh(&sl->lock);
893                         if (sl->tty) {
894                                 busy++;
895                                 tty_hangup(sl->tty);
896                         }
897                         spin_unlock_bh(&sl->lock);
898                 }
899         } while (busy && time_before(jiffies, timeout));
900
901
902         for (i = 0; i < maxdev; i++) {
903                 dev = slcan_devs[i];
904                 if (!dev)
905                         continue;
906                 slcan_devs[i] = NULL;
907
908                 sl = netdev_priv(dev);
909                 if (sl->tty) {
910                         printk(KERN_ERR "%s: tty discipline still running\n",
911                                dev->name);
912                         /* Intentionally leak the control block. */
913                         dev->destructor = NULL;
914                 }
915
916                 unregister_netdev(dev);
917         }
918
919         kfree(slcan_devs);
920         slcan_devs = NULL;
921
922         if ((i = tty_unregister_ldisc(N_SLCAN)))
923         {
924                 printk(KERN_ERR "slcan: can't unregister ldisc (err %d)\n", i);
925         }
926 }
927
928 module_init(slcan_init);
929 module_exit(slcan_exit);