]> rtime.felk.cvut.cz Git - linux-lin.git/blob - sllin/sllin.c
sllin: Checking of checksum on received LIN frames.
[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 drivers/net/can/slcan.c
5  * slcan.c Author: Oliver Hartkopp <socketcan@hartkopp.net>
6  *
7  * This program is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU General Public License as published by the
9  * Free Software Foundation; either version 2 of the License, or (at your
10  * option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along
18  * with this program; if not, write to the Free Software Foundation, Inc.,
19  * 59 Temple Place, Suite 330, Boston, MA 02111-1307. You can also get it
20  * at http://www.gnu.org/licenses/gpl.html
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
33  * DAMAGE.
34  *
35  * Send feedback to <socketcan-users@lists.berlios.de>
36  *
37  */
38
39 #include <linux/module.h>
40 #include <linux/moduleparam.h>
41
42 #include <asm/system.h>
43 #include <linux/uaccess.h>
44 #include <linux/bitops.h>
45 #include <linux/string.h>
46 #include <linux/tty.h>
47 #include <linux/errno.h>
48 #include <linux/netdevice.h>
49 #include <linux/skbuff.h>
50 #include <linux/rtnetlink.h>
51 #include <linux/if_arp.h>
52 #include <linux/if_ether.h>
53 #include <linux/sched.h>
54 #include <linux/delay.h>
55 #include <linux/init.h>
56 #include <linux/can.h>
57 #include <linux/kthread.h>
58 #include <linux/hrtimer.h>
59
60 /* Should be in include/linux/tty.h */
61 #define N_SLLIN         25
62
63 static __initdata const char banner[] =
64         KERN_INFO "sllin: serial line LIN interface driver\n";
65
66 MODULE_ALIAS_LDISC(N_SLLIN);
67 MODULE_DESCRIPTION("serial line LIN interface");
68 MODULE_LICENSE("GPL");
69 MODULE_AUTHOR("");
70
71 #define SLLIN_MAGIC 0x53CA
72 // #define BREAK_BY_BAUD
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 buffer len to store whole LIN message*/
81 #define SLLIN_DATA_MAX   8
82 #define SLLIN_BUFF_LEN  (1 /*break*/ + 1 /*sync*/ + 1 /*ID*/ + \
83                          SLLIN_DATA_MAX + 1 /*checksum*/)
84 #define SLLIN_BUFF_BREAK 0
85 #define SLLIN_BUFF_SYNC  1
86 #define SLLIN_BUFF_ID    2
87 #define SLLIN_BUFF_DATA  3
88
89 #define SLLIN_ID_MASK           0x3f
90 #define SLLIN_ID_MAX            SLLIN_ID_MASK
91 #define SLLIN_CTRL_FRAME        CAN_EFF_FLAG
92
93 #define SLLIN_SAMPLES_PER_CHAR  10
94 #define SLLIN_CHARS_TO_TIMEOUT  12
95
96 enum slstate {
97         SLSTATE_IDLE = 0,
98         SLSTATE_BREAK_SENT,
99         SLSTATE_ID_SENT,
100         SLSTATE_RESPONSE_WAIT, /* Wait for response */
101         SLSTATE_RESPONSE_WAIT_BUS, /* Wait for response from LIN bus
102                                 only (CAN frames from network stack
103                                 are not processed in this moment) */
104         SLSTATE_RESPONSE_SENT,
105 };
106
107 struct sllin_conf_entry {
108         int dlc;                /* Length of data in LIN frame */
109 #define SLLIN_CANFR_FLAGS_OFFS  6 /* Lower 6 bits in can_id correspond to LIN ID */
110 /* Save configuration for particular LIN ID */
111 #define SLLIN_LIN_ID_CONF       (1 <<  SLLIN_CANFR_FLAGS_OFFS)
112 /* Publisher of particular LIN response is SLLIN Master */
113 #define SLLIN_SRC_MASTER        (1 << (SLLIN_CANFR_FLAGS_OFFS + 1))
114 #define SLLIN_SRC_SLAVE         (1 << (SLLIN_CANFR_FLAGS_OFFS + 2))
115 #define SLLIN_SLAVE_LOCAL       (1 << (SLLIN_CANFR_FLAGS_OFFS + 3))
116 #define SLLIN_SLAVE_REMOTE      (1 << (SLLIN_CANFR_FLAGS_OFFS + 4))
117 #define SLLIN_LOC_SLAVE_CACHE   (1 << (SLLIN_CANFR_FLAGS_OFFS + 5))
118 #define SLLIN_CHECKSUM_EXTENDED (1 << (SLLIN_CANFR_FLAGS_OFFS + 6))
119
120 #define SLLIN_ERR_RX_TIMEOUT    (1 << (SLLIN_CANFR_FLAGS_OFFS + 7))
121 #define SLLIN_ERR_CHECKSUM      (1 << (SLLIN_CANFR_FLAGS_OFFS + 8))
122 //#define SLLIN_ERR_FRAMING     (1 << (SLLIN_CANFR_FLAGS_OFFS + 9))
123
124         canid_t frame_fl;       /* LIN frame flags. Passed from userspace as canid_t data type */
125         u8 data[8];             /* LIN frame data payload */
126 };
127
128 struct sllin {
129         int                     magic;
130
131         /* Various fields. */
132         struct tty_struct       *tty;           /* ptr to TTY structure      */
133         struct net_device       *dev;           /* easy for intr handling    */
134         spinlock_t              lock;
135
136         /* LIN message buffer and actual processed data counts */
137         unsigned char           rx_buff[SLLIN_BUFF_LEN]; /* LIN Rx buffer */
138         unsigned char           tx_buff[SLLIN_BUFF_LEN]; /* LIN Tx buffer */
139         int                     rx_expect;      /* expected number of Rx chars */
140         int                     rx_lim;         /* maximum Rx chars for current frame */
141         int                     rx_cnt;         /* message buffer Rx fill level  */
142         int                     tx_lim;         /* actual limit of bytes to Tx */
143         int                     tx_cnt;         /* number of already Tx bytes */
144         char                    lin_master;     /* node is a master node */
145         int                     lin_baud;       /* LIN baudrate */
146         int                     lin_state;      /* state */
147         char                    id_to_send;     /* there is ID to be sent */
148         char                    data_to_send;   /* there are data to be sent */
149         char                    resp_len_known; /* Length of the response is known */
150
151         unsigned long           flags;          /* Flag values/ mode etc     */
152 #define SLF_INUSE               0               /* Channel in use            */
153 #define SLF_ERROR               1               /* Parity, etc. error        */
154 #define SLF_RXEVENT             2               /* Rx wake event             */
155 #define SLF_TXEVENT             3               /* Tx wake event             */
156 #define SLF_MSGEVENT            4               /* CAN message to sent       */
157 #define SLF_TMOUTEVENT          5               /* Timeout on received data  */
158
159         dev_t                   line;
160         struct task_struct      *kwthread;
161         wait_queue_head_t       kwt_wq;
162         struct hrtimer          rx_timer;       /* RX timeout timer */
163         ktime_t                 rx_timer_timeout; /* RX timeout timer value */
164         struct sk_buff          *tx_req_skb;    /* Socket buffer with CAN frame received
165                                                 from network stack*/
166
167         struct sllin_conf_entry linfr_cache[SLLIN_ID_MAX + 1]; /* List with configurations for
168                                                 each of 0 to SLLIN_ID_MAX LIN IDs */
169 };
170
171 static struct net_device **sllin_devs;
172 static int sllin_configure_frame_cache(struct sllin *sl, struct can_frame *cf);
173
174
175 const unsigned char sllin_id_parity_table[] = {
176         0x80,0xc0,0x40,0x00,0xc0,0x80,0x00,0x40,
177         0x00,0x40,0xc0,0x80,0x40,0x00,0x80,0xc0,
178         0x40,0x00,0x80,0xc0,0x00,0x40,0xc0,0x80,
179         0xc0,0x80,0x00,0x40,0x80,0xc0,0x40,0x00,
180         0x00,0x40,0xc0,0x80,0x40,0x00,0x80,0xc0,
181         0x80,0xc0,0x40,0x00,0xc0,0x80,0x00,0x40,
182         0xc0,0x80,0x00,0x40,0x80,0xc0,0x40,0x00,
183         0x40,0x00,0x80,0xc0,0x00,0x40,0xc0,0x80
184 };
185
186 static int sltty_change_speed(struct tty_struct *tty, unsigned speed)
187 {
188         struct ktermios old_termios;
189         int cflag;
190
191         mutex_lock(&tty->termios_mutex);
192
193         old_termios = *(tty->termios);
194         cflag = tty->termios->c_cflag;
195         cflag &= ~(CBAUD | CIBAUD);
196         cflag |= BOTHER;
197         tty->termios->c_cflag = cflag;
198
199         tty_encode_baud_rate(tty, speed, speed);
200
201         if (tty->ops->set_termios)
202                 tty->ops->set_termios(tty, &old_termios);
203
204         mutex_unlock(&tty->termios_mutex);
205
206         return 0;
207 }
208
209
210 /* Send one can_frame to the network layer */
211 static void sllin_send_canfr(struct sllin *sl, canid_t id, char *data, int len)
212 {
213         struct sk_buff *skb;
214         struct can_frame cf;
215
216         cf.can_id = id;
217         cf.can_dlc = len;
218         if (cf.can_dlc > 0) {
219                 memcpy(&cf.data, data, cf.can_dlc);
220         }
221
222         skb = dev_alloc_skb(sizeof(struct can_frame));
223         if (!skb)
224                 return;
225
226         skb->dev = sl->dev;
227         skb->protocol = htons(ETH_P_CAN);
228         skb->pkt_type = PACKET_BROADCAST;
229         skb->ip_summed = CHECKSUM_UNNECESSARY;
230         memcpy(skb_put(skb, sizeof(struct can_frame)),
231                &cf, sizeof(struct can_frame));
232         netif_rx(skb);
233
234         sl->dev->stats.rx_packets++;
235         sl->dev->stats.rx_bytes += cf.can_dlc;
236
237
238 }
239
240 static void sll_bump(struct sllin *sl)
241 {
242         sllin_send_canfr(sl, sl->rx_buff[SLLIN_BUFF_ID] & SLLIN_ID_MASK,
243                 sl->rx_buff + SLLIN_BUFF_DATA,
244                 sl->rx_cnt - SLLIN_BUFF_DATA - 1); /* without checksum */
245 }
246
247 /*
248  * Called by the driver when there's room for more data.  If we have
249  * more packets to send, we send them here.
250  */
251 static void sllin_write_wakeup(struct tty_struct *tty)
252 {
253         int actual;
254         int remains;
255         struct sllin *sl = (struct sllin *) tty->disc_data;
256
257         /* First make sure we're connected. */
258         if (!sl || sl->magic != SLLIN_MAGIC || !netif_running(sl->dev))
259                 return;
260
261         if (sl->lin_state != SLSTATE_BREAK_SENT)
262                 remains = sl->tx_lim - sl->tx_cnt;
263         else
264                 remains = SLLIN_BUFF_BREAK + 1 - sl->tx_cnt;
265
266         if (remains > 0) {
267                 actual = tty->ops->write(tty, sl->tx_buff + sl->tx_cnt, sl->tx_cnt - sl->tx_lim);
268                 sl->tx_cnt += actual;
269
270                 if (sl->tx_cnt < sl->tx_lim) {
271                         printk(KERN_INFO "sllin_write_wakeup sent %d, remains %d, waiting\n",
272                                 sl->tx_cnt, sl->tx_lim - sl->tx_cnt);
273                         return;
274                 }
275         }
276
277         clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
278         set_bit(SLF_TXEVENT, &sl->flags);
279         wake_up(&sl->kwt_wq);
280
281         printk(KERN_INFO "sllin_write_wakeup sent %d, wakeup\n", sl->tx_cnt);
282 }
283
284 /* Send a can_frame to a TTY queue. */
285 static netdev_tx_t sll_xmit(struct sk_buff *skb, struct net_device *dev)
286 {
287         struct sllin *sl = netdev_priv(dev);
288         struct can_frame *cf;
289
290         if (skb->len != sizeof(struct can_frame))
291                 goto err_out;
292
293         spin_lock(&sl->lock);
294         if (!netif_running(dev))  {
295                 printk(KERN_WARNING "%s: xmit: iface is down\n", dev->name);
296                 goto err_out_unlock;
297         }
298         if (sl->tty == NULL) {
299                 goto err_out_unlock;
300         }
301
302         cf = (struct can_frame *) skb->data;
303         if (cf->can_id & SLLIN_CTRL_FRAME) {
304                 sllin_configure_frame_cache(sl, cf);
305                 goto free_out_unlock;
306         }
307
308         netif_stop_queue(sl->dev);
309
310         sl->tx_req_skb = skb;
311         set_bit(SLF_MSGEVENT, &sl->flags);
312         wake_up(&sl->kwt_wq);
313         spin_unlock(&sl->lock);
314         return NETDEV_TX_OK;
315
316 free_out_unlock:
317 err_out_unlock:
318         spin_unlock(&sl->lock);
319 err_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->rx_expect = 0;
341         sl->tx_lim    = 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         printk(KERN_INFO "sllin_receive_buf invoked\n");
412
413         if (!sl || sl->magic != SLLIN_MAGIC || !netif_running(sl->dev))
414                 return;
415
416         /* Read the characters out of the buffer */
417         while (count--) {
418                 if (fp && *fp++) {
419                         if (!test_and_set_bit(SLF_ERROR, &sl->flags))
420                                 sl->dev->stats.rx_errors++;
421                         printk(KERN_INFO "sllin_receive_buf char 0x%02x ignored "
422                                 "due marker 0x%02x, flags 0x%lx\n",
423                                 *cp, *(fp-1), sl->flags);
424                         cp++;
425                         continue;
426                 }
427
428                 if (sl->rx_cnt < SLLIN_BUFF_LEN) {
429 #ifndef BREAK_BY_BAUD
430                         /* We didn't receive Break character */
431                         if ((sl->rx_cnt == SLLIN_BUFF_BREAK) && (*cp == 0x55)) {
432                                 sl->rx_buff[sl->rx_cnt++] = 0x00;
433                         }
434 #endif
435                         printk(KERN_INFO "LIN_RX[%d]: 0x%02x\n", sl->rx_cnt, *cp);
436                         sl->rx_buff[sl->rx_cnt++] = *cp++;
437                 }
438         }
439
440         if (sl->rx_cnt >= sl->rx_expect) {
441                 set_bit(SLF_RXEVENT, &sl->flags);
442                 wake_up(&sl->kwt_wq);
443                 printk(KERN_INFO "sllin_receive_buf count %d, wakeup\n", sl->rx_cnt);
444         } else {
445                 printk(KERN_INFO "sllin_receive_buf count %d, waiting\n", sl->rx_cnt);
446         }
447 }
448
449 /*****************************************
450  *  sllin message helper routines
451  *****************************************/
452 void sllin_report_error(struct sllin *sl, int err)
453 {
454         sllin_send_canfr(sl, 0 | CAN_EFF_FLAG | 
455                 (err & ~SLLIN_ID_MASK), NULL, 0);
456 }
457
458 static int sllin_configure_frame_cache(struct sllin *sl, struct can_frame *cf)
459 {
460         struct sllin_conf_entry *sce;
461         if (!(cf->can_id & SLLIN_LIN_ID_CONF))
462                 return -1;
463
464         sce = &sl->linfr_cache[cf->can_id & SLLIN_ID_MASK];
465         printk(KERN_INFO "Setting frame cache with EFF CAN frame. "
466                 "LIN ID = %d\n", cf->can_id & SLLIN_ID_MASK);
467
468         sce->dlc = cf->can_dlc;
469         if (sce->dlc > SLLIN_DATA_MAX)
470                 sce->dlc = SLLIN_DATA_MAX;
471
472         sce->frame_fl = (cf->can_id & ~SLLIN_ID_MASK) & CAN_EFF_MASK;
473         memcpy(sce->data, cf->data, cf->can_dlc);
474
475         return 0;
476 }
477
478 static inline unsigned sllin_checksum(unsigned char *data, int length, int enhanced_fl)
479 {
480         unsigned csum = 0;
481         int i;
482
483         if (enhanced_fl) {
484                 i = SLLIN_BUFF_ID;
485         } else {
486                 i = SLLIN_BUFF_DATA;
487         }
488         
489         for (; i < length; i++) {
490                 csum += data[i];
491                 if (csum > 255)
492                         csum -= 255;
493         }
494
495         return ~csum & 0xff;
496 }
497
498 #define SLLIN_STPMSG_RESPONLY           (1) /* Message will be LIN Response only */
499 #define SLLIN_STPMSG_CHCKSUM_CLS        (1 << 1)
500 #define SLLIN_STPMSG_CHCKSUM_ENH        (1 << 2)
501
502 int sllin_setup_msg(struct sllin *sl, int mode, int id,
503                 unsigned char *data, int len)
504 {
505         if (id > SLLIN_ID_MASK)
506                 return -1;
507
508         if (!(mode & SLLIN_STPMSG_RESPONLY)) {
509                 sl->rx_cnt = 0;
510                 sl->tx_cnt = 0;
511                 sl->rx_expect = 0;
512                 sl->rx_lim = SLLIN_BUFF_LEN;
513         }
514
515         sl->tx_buff[SLLIN_BUFF_BREAK] = 0;
516         sl->tx_buff[SLLIN_BUFF_SYNC]  = 0x55;
517         sl->tx_buff[SLLIN_BUFF_ID]    = id | sllin_id_parity_table[id];
518         sl->tx_lim = SLLIN_BUFF_DATA;
519
520         if ((data != NULL) && len) {
521                 sl->tx_lim += len;
522                 memcpy(sl->tx_buff + SLLIN_BUFF_DATA, data, len);
523                 sl->tx_buff[sl->tx_lim++] = sllin_checksum(sl->tx_buff,
524                                 sl->tx_lim, mode & SLLIN_STPMSG_CHCKSUM_ENH);
525         }
526         if (len != 0)
527                 sl->rx_lim = SLLIN_BUFF_DATA + len + 1;
528
529         return 0;
530 }
531
532
533 int sllin_send_tx_buff(struct sllin *sl)
534 {
535         struct tty_struct *tty = sl->tty;
536         int remains;
537         int res;
538
539 #ifdef BREAK_BY_BAUD
540         if (sl->lin_state != SLSTATE_BREAK_SENT)
541                 remains = sl->tx_lim - sl->tx_cnt;
542         else
543                 remains = 1;
544 #else
545         remains = sl->tx_lim - sl->tx_cnt;
546 #endif
547
548         res = tty->ops->write(tty, sl->tx_buff + sl->tx_cnt, remains);
549         if (res < 0)
550                 return -1;
551
552         remains -= res;
553         sl->tx_cnt += res;
554
555         if (remains > 0) {
556                 set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
557                 res = tty->ops->write(tty, sl->tx_buff + sl->tx_cnt, remains);
558                 if (res < 0) {
559                         clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
560                         return -1;
561                 }
562                 
563                 remains -= res;
564                 sl->tx_cnt += res;
565         }
566
567         printk(KERN_INFO "sllin_send_tx_buff sent %d, remains %d\n",
568                         sl->tx_cnt, remains);
569
570         return 0;
571 }
572
573 #ifdef BREAK_BY_BAUD
574 int sllin_send_break(struct sllin *sl)
575 {
576         struct tty_struct *tty = sl->tty;
577         unsigned long break_baud;
578         int res;
579
580         break_baud = ((sl->lin_baud * 2) / 3);
581         sltty_change_speed(tty, break_baud);
582
583         tty->ops->flush_buffer(tty);
584         sl->rx_cnt = SLLIN_BUFF_BREAK;
585
586         sl->rx_expect = SLLIN_BUFF_BREAK + 1;
587         sl->lin_state = SLSTATE_BREAK_SENT;
588
589         res = sllin_send_tx_buff(sl);
590         if (res < 0) {
591                 sl->lin_state = SLSTATE_IDLE;
592                 return res;
593         }
594
595         return 0;
596 }
597 #else /* BREAK_BY_BAUD */
598
599 int sllin_send_break(struct sllin *sl)
600 {
601         struct tty_struct *tty = sl->tty;
602         int retval;
603         unsigned long break_baud;
604         unsigned long usleep_range_min;
605         unsigned long usleep_range_max;
606
607         break_baud = ((sl->lin_baud * 2) / 3);
608         sl->rx_cnt = SLLIN_BUFF_BREAK;
609         sl->rx_expect = SLLIN_BUFF_BREAK + 1;
610         sl->lin_state = SLSTATE_BREAK_SENT;
611
612         /* Do the break ourselves; Inspired by 
613            http://lxr.linux.no/#linux+v3.1.2/drivers/tty/tty_io.c#L2452 */
614         retval = tty->ops->break_ctl(tty, -1);
615         if (retval)
616                 return retval;
617
618         //udelay(712);
619         usleep_range_min = (1000000l * SLLIN_SAMPLES_PER_CHAR) / break_baud;
620         usleep_range_max = usleep_range_min + 50;
621         usleep_range(usleep_range_min, usleep_range_max);
622
623         retval = tty->ops->break_ctl(tty, 0);
624         usleep_range_min = (1000000l * 1 /* 1 bit */) / break_baud;
625         usleep_range_max = usleep_range_min + 30;
626         usleep_range(usleep_range_min, usleep_range_max);
627         
628         tty->ops->flush_buffer(tty);
629
630         sl->tx_cnt = SLLIN_BUFF_SYNC;
631
632         printk(KERN_INFO "sllin: Break sent.\n");
633         set_bit(SLF_RXEVENT, &sl->flags);
634         wake_up(&sl->kwt_wq);
635
636         return 0;
637 }
638 #endif /* BREAK_BY_BAUD */
639
640
641 static enum hrtimer_restart sllin_rx_timeout_handler(struct hrtimer *hrtimer)
642 {
643         struct sllin *sl = container_of(hrtimer, struct sllin, rx_timer);
644
645         sllin_report_error(sl, SLLIN_ERR_RX_TIMEOUT);
646         set_bit(SLF_TMOUTEVENT, &sl->flags);
647         wake_up(&sl->kwt_wq);
648
649         return HRTIMER_NORESTART;
650 }
651
652 static int sllin_rx_validate(struct sllin *sl)
653 {
654         int actual_id;
655         int ext_chcks_fl;
656         int lin_dlc;
657         unsigned char rec_chcksm = sl->rx_buff[sl->rx_cnt - 1];
658         struct sllin_conf_entry *scf;   
659
660         actual_id = sl->rx_buff[SLLIN_BUFF_ID] & SLLIN_ID_MASK; 
661         scf = &sl->linfr_cache[actual_id];
662         lin_dlc = scf->dlc;
663         ext_chcks_fl = scf->frame_fl & SLLIN_CHECKSUM_EXTENDED;
664
665         if (sllin_checksum(sl->rx_buff, sl->rx_cnt - 1, ext_chcks_fl) != 
666                 rec_chcksm) {
667
668                 /* Type of checksum is configured for particular frame */
669                 if (lin_dlc > 0) {
670                         return -1;
671                 } else {
672                         if (sllin_checksum(sl->rx_buff, sl->rx_cnt - 1,
673                                 !ext_chcks_fl) != rec_chcksm) {
674                                 return -1;
675                         }
676                 }
677         }
678
679         return 0;
680 }
681
682 /*****************************************
683  *  sllin_kwthread - kernel worker thread
684  *****************************************/
685
686 int sllin_kwthread(void *ptr)
687 {
688         struct sllin *sl = (struct sllin *)ptr;
689         struct tty_struct *tty = sl->tty;
690         struct sched_param schparam = { .sched_priority = 40 };
691         int tx_bytes = 0; /* Used for Network statistics */
692
693
694         printk(KERN_INFO "sllin: sllin_kwthread started.\n");
695         sched_setscheduler(current, SCHED_FIFO, &schparam);
696
697         clear_bit(SLF_ERROR, &sl->flags);
698         sltty_change_speed(tty, sl->lin_baud);
699
700         while (!kthread_should_stop()) {
701                 struct can_frame *cf;
702                 u8 *lin_data;
703                 int lin_dlc;
704                 u8 lin_data_buff[SLLIN_DATA_MAX];
705
706
707                 if ((sl->lin_state == SLSTATE_IDLE) && sl->lin_master &&
708                         sl->id_to_send) {
709                         if(sllin_send_break(sl) < 0) {
710                                 /* error processing */
711                         }
712                 }
713
714                 wait_event_killable(sl->kwt_wq, kthread_should_stop() ||
715                         test_bit(SLF_RXEVENT, &sl->flags) ||
716                         test_bit(SLF_TXEVENT, &sl->flags) ||
717                         test_bit(SLF_TMOUTEVENT, &sl->flags) ||
718                         (((sl->lin_state == SLSTATE_IDLE) || 
719                                 (sl->lin_state == SLSTATE_RESPONSE_WAIT)) 
720                                 && test_bit(SLF_MSGEVENT, &sl->flags)));
721
722                 if (test_and_clear_bit(SLF_RXEVENT, &sl->flags)) {
723                         printk(KERN_INFO "sllin_kthread RXEVENT \n");
724                 }
725
726                 if (test_and_clear_bit(SLF_TXEVENT, &sl->flags)) {
727                         printk(KERN_INFO "sllin_kthread TXEVENT \n");
728                 }
729
730                 if (test_and_clear_bit(SLF_TMOUTEVENT, &sl->flags)) {
731                         printk(KERN_INFO "sllin_kthread TMOUTEVENT \n");
732                         sl->rx_cnt = 0;
733                         sl->rx_expect = 0;
734                         sl->rx_lim = sl->lin_master ? 0 : SLLIN_BUFF_LEN;
735                         sl->tx_cnt = 0;
736                         sl->tx_lim = 0;
737                         sl->id_to_send = false;
738                         sl->data_to_send = false;
739                         
740                         sl->lin_state = SLSTATE_IDLE;
741                 }
742
743                 switch (sl->lin_state) {
744                         case SLSTATE_IDLE:
745                                 if (!test_bit(SLF_MSGEVENT, &sl->flags))
746                                         break;
747
748                                 cf = (struct can_frame *)sl->tx_req_skb->data;
749
750                                 /* SFF RTR CAN frame -> LIN header */ 
751                                 if (cf->can_id & CAN_RTR_FLAG) {
752                                         spin_lock(&sl->lock);
753                                         printk(KERN_INFO "%s: RTR SFF CAN frame, ID = %x\n",
754                                                 __FUNCTION__, cf->can_id & SLLIN_ID_MASK);
755
756                                         /* Is there Slave response in linfr_cache to be sent? */
757                                         if ((sl->linfr_cache[cf->can_id & SLLIN_ID_MASK].frame_fl & 
758                                                 SLLIN_LOC_SLAVE_CACHE) 
759                                                 && (sl->linfr_cache[cf->can_id & SLLIN_ID_MASK].dlc > 0)) {
760                                                 
761                                                 printk(KERN_INFO "Sending LIN response from linfr_cache\n");
762                                                 lin_data = sl->linfr_cache[cf->can_id & SLLIN_ID_MASK].data;
763                                                 lin_dlc = sl->linfr_cache[cf->can_id & SLLIN_ID_MASK].dlc;
764                                                 if (lin_dlc > SLLIN_DATA_MAX)
765                                                         lin_dlc = SLLIN_DATA_MAX;
766                                                 memcpy(lin_data_buff, lin_data, lin_dlc);
767                                                 lin_data = lin_data_buff;
768                                         } else {
769                                                 lin_data = NULL;
770                                                 lin_dlc = sl->linfr_cache[cf->can_id & SLLIN_ID_MASK].dlc;
771                                         }
772                                         spin_unlock(&sl->lock);
773                                 } else { /* SFF NON-RTR CAN frame -> LIN header + LIN response */
774                                         printk(KERN_INFO "%s: NON-RTR SFF CAN frame, ID = %x\n",
775                                                 __FUNCTION__, (int)cf->can_id & SLLIN_ID_MASK);
776
777                                         lin_data = cf->data;
778                                         lin_dlc = cf->can_dlc;
779                                         if (lin_dlc > SLLIN_DATA_MAX)
780                                                 lin_dlc = SLLIN_DATA_MAX;
781                                         tx_bytes = lin_dlc;
782                                 }
783
784                                 if (sllin_setup_msg(sl, 0, cf->can_id & SLLIN_ID_MASK,
785                                         lin_data, lin_dlc) != -1) {
786
787                                         sl->id_to_send = true;
788                                         sl->data_to_send = (lin_data != NULL) ? true : false;
789                                         sl->resp_len_known = (lin_dlc > 0) ? true : false;
790                                         sl->dev->stats.tx_packets++;
791                                         sl->dev->stats.tx_bytes += tx_bytes;
792                                 }
793
794                                 clear_bit(SLF_MSGEVENT, &sl->flags);
795                                 kfree_skb(sl->tx_req_skb);
796                                 netif_wake_queue(sl->dev);
797                                 break;
798
799                         case SLSTATE_BREAK_SENT:
800 #ifdef BREAK_BY_BAUD
801                                 if (sl->rx_cnt <= SLLIN_BUFF_BREAK)
802                                         continue;
803
804                                 res = sltty_change_speed(tty, sl->lin_baud);
805 #endif
806
807                                 sl->lin_state = SLSTATE_ID_SENT;
808                                 sllin_send_tx_buff(sl);
809                                 break;
810
811                         case SLSTATE_ID_SENT:
812                                 sl->id_to_send = false;
813                                 if (sl->data_to_send) {
814                                         sllin_send_tx_buff(sl);
815                                         sl->lin_state = SLSTATE_RESPONSE_SENT;
816                                         sl->rx_expect = sl->tx_lim;
817                                         goto slstate_response_sent;
818                                 } else {
819                                         if (sl->resp_len_known) {
820                                                 sl->rx_expect = sl->rx_lim;
821                                         } else {
822                                                 sl->rx_expect = SLLIN_BUFF_DATA + 2;
823                                         }
824                                         sl->lin_state = SLSTATE_RESPONSE_WAIT;
825                                         /* If we don't receive anything, timer will "unblock" us */
826                                         hrtimer_start(&sl->rx_timer, 
827                                                 ktime_add(ktime_get(), sl->rx_timer_timeout),
828                                                 HRTIMER_MODE_ABS);
829                                         goto slstate_response_wait;
830                                 }
831                                 break;
832
833                         case SLSTATE_RESPONSE_WAIT:
834                         slstate_response_wait:
835                                 if (test_bit(SLF_MSGEVENT, &sl->flags)) {
836                                         unsigned char *lin_buff;
837                                         cf = (struct can_frame *)sl->tx_req_skb->data;
838
839                                         lin_buff = (sl->lin_master) ? sl->tx_buff : sl->rx_buff;
840                                         if (cf->can_id == (lin_buff[SLLIN_BUFF_ID] & SLLIN_ID_MASK)) {
841                                                 if (sllin_setup_msg(sl, SLLIN_STPMSG_RESPONLY, 
842                                                         cf->can_id & SLLIN_ID_MASK,
843                                                         cf->data, cf->can_dlc) != -1) {
844
845                                                         sl->rx_expect = sl->tx_lim;
846                                                         sl->data_to_send = true;
847                                                         sl->dev->stats.tx_packets++;
848                                                         sl->dev->stats.tx_bytes += tx_bytes;
849
850                                                         if (!sl->lin_master) {
851                                                                 sl->tx_cnt = SLLIN_BUFF_DATA;
852                                                         }
853                                                         
854                                                         sllin_send_tx_buff(sl);
855                                                         clear_bit(SLF_MSGEVENT, &sl->flags);
856                                                         kfree_skb(sl->tx_req_skb);
857                                                         netif_wake_queue(sl->dev);
858
859                                                         sl->lin_state = SLSTATE_RESPONSE_SENT;
860                                                         goto slstate_response_sent;
861                                                 }
862                                         } else {
863                                                 sl->lin_state = SLSTATE_RESPONSE_WAIT_BUS;
864                                         }
865                                 }
866
867                         case SLSTATE_RESPONSE_WAIT_BUS:
868                                 if (sl->rx_cnt < sl->rx_expect)
869                                         continue;
870                         
871                                 hrtimer_cancel(&sl->rx_timer);
872                                 printk(KERN_INFO "sllin: response received ID %d len %d\n",
873                                         sl->rx_buff[SLLIN_BUFF_ID], sl->rx_cnt - SLLIN_BUFF_DATA - 1);
874
875                                 if (sllin_rx_validate(sl) == -1) {
876                                         printk("sllin: RX validation failed.\n");
877                                         sllin_report_error(sl, SLLIN_ERR_CHECKSUM);
878                                         //FIXME tx_stat.err++
879                                 } else {
880                                         // send CAN non-RTR frame with data
881                                         printk(KERN_INFO "sllin: sending NON-RTR CAN"
882                                                 "frame with LIN payload.");
883                                         sll_bump(sl); //send packet to the network layer
884                                 }
885
886                                 sl->id_to_send = false;
887                                 sl->lin_state = SLSTATE_IDLE;
888                                 break;
889                         
890                         case SLSTATE_RESPONSE_SENT:
891                         slstate_response_sent:
892                                 if (sl->rx_cnt < sl->tx_lim)
893                                         continue;
894                                 
895                                 sll_bump(sl); //send packet to the network layer
896                                 printk(KERN_INFO "sllin: response sent ID %d len %d\n",
897                                         sl->rx_buff[SLLIN_BUFF_ID], sl->rx_cnt - SLLIN_BUFF_DATA - 1);
898
899                                 sl->id_to_send = false;
900                                 sl->lin_state = SLSTATE_IDLE;
901                                 break;
902                 }
903
904
905
906
907                 /* sl->dev->stats.tx_packets++; send frames statistic */
908                 /* netif_wake_queue(sl->dev); allow next Tx packet arrival */
909         }
910
911         hrtimer_cancel(&sl->rx_timer);
912         printk(KERN_INFO "sllin: sllin_kwthread stopped.\n");
913
914         return 0;
915 }
916
917
918 /************************************
919  *  sllin_open helper routines.
920  ************************************/
921
922 /* Collect hanged up channels */
923 static void sll_sync(void)
924 {
925         int i;
926         struct net_device *dev;
927         struct sllin      *sl;
928
929         for (i = 0; i < maxdev; i++) {
930                 dev = sllin_devs[i];
931                 if (dev == NULL)
932                         break;
933
934                 sl = netdev_priv(dev);
935                 if (sl->tty)
936                         continue;
937                 if (dev->flags & IFF_UP)
938                         dev_close(dev);
939         }
940 }
941
942 /* Find a free SLLIN channel, and link in this `tty' line. */
943 static struct sllin *sll_alloc(dev_t line)
944 {
945         int i;
946         struct net_device *dev = NULL;
947         struct sllin       *sl;
948
949         if (sllin_devs == NULL)
950                 return NULL;    /* Master array missing ! */
951
952         for (i = 0; i < maxdev; i++) {
953                 dev = sllin_devs[i];
954                 if (dev == NULL)
955                         break;
956
957         }
958
959         /* Sorry, too many, all slots in use */
960         if (i >= maxdev)
961                 return NULL;
962
963         if (dev) {
964                 sl = netdev_priv(dev);
965                 if (test_bit(SLF_INUSE, &sl->flags)) {
966                         unregister_netdevice(dev);
967                         dev = NULL;
968                         sllin_devs[i] = NULL;
969                 }
970         }
971
972         if (!dev) {
973                 char name[IFNAMSIZ];
974                 sprintf(name, "sllin%d", i);
975
976                 dev = alloc_netdev(sizeof(*sl), name, sll_setup);
977                 if (!dev)
978                         return NULL;
979                 dev->base_addr  = i;
980         }
981
982         sl = netdev_priv(dev);
983         /* Initialize channel control data */
984         sl->magic = SLLIN_MAGIC;
985         sl->dev = dev;
986         spin_lock_init(&sl->lock);
987         sllin_devs[i] = dev;
988
989         return sl;
990 }
991
992 /*
993  * Open the high-level part of the SLLIN channel.
994  * This function is called by the TTY module when the
995  * SLLIN line discipline is called for.  Because we are
996  * sure the tty line exists, we only have to link it to
997  * a free SLLIN channel...
998  *
999  * Called in process context serialized from other ldisc calls.
1000  */
1001
1002 static int sllin_open(struct tty_struct *tty)
1003 {
1004         struct sllin *sl;
1005         int err;
1006         pr_debug("sllin: %s() invoked\n", __FUNCTION__);
1007
1008         if (!capable(CAP_NET_ADMIN))
1009                 return -EPERM;
1010
1011         if (tty->ops->write == NULL)
1012                 return -EOPNOTSUPP;
1013
1014         /* RTnetlink lock is misused here to serialize concurrent
1015            opens of sllin channels. There are better ways, but it is
1016            the simplest one.
1017          */
1018         rtnl_lock();
1019
1020         /* Collect hanged up channels. */
1021         sll_sync();
1022
1023         sl = tty->disc_data;
1024
1025         err = -EEXIST;
1026         /* First make sure we're not already connected. */
1027         if (sl && sl->magic == SLLIN_MAGIC)
1028                 goto err_exit;
1029
1030         /* OK.  Find a free SLLIN channel to use. */
1031         err = -ENFILE;
1032         sl = sll_alloc(tty_devnum(tty));
1033         if (sl == NULL)
1034                 goto err_exit;
1035
1036         sl->tty = tty;
1037         tty->disc_data = sl;
1038         sl->line = tty_devnum(tty);
1039
1040         if (!test_bit(SLF_INUSE, &sl->flags)) {
1041                 /* Perform the low-level SLLIN initialization. */
1042                 sl->lin_master = true;
1043
1044                 sl->rx_cnt = 0;
1045                 sl->rx_expect = 0;
1046                 sl->rx_lim = sl->lin_master ? 0 : SLLIN_BUFF_LEN;
1047                 sl->tx_cnt = 0;
1048                 sl->tx_lim = 0;
1049                 sl->id_to_send = false;
1050                 sl->data_to_send = false;
1051
1052                 sl->lin_baud  = 19200;
1053
1054                 sl->lin_state = SLSTATE_IDLE;
1055
1056                 hrtimer_init(&sl->rx_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1057                 sl->rx_timer.function = sllin_rx_timeout_handler;
1058                 /* timeval_to_ktime(msg_head->ival1); */
1059                 sl->rx_timer_timeout = ns_to_ktime(
1060                         (1000000000l / sl->lin_baud) * 
1061                         SLLIN_SAMPLES_PER_CHAR * SLLIN_CHARS_TO_TIMEOUT); 
1062  
1063                 set_bit(SLF_INUSE, &sl->flags);
1064
1065                 init_waitqueue_head(&sl->kwt_wq);
1066                 sl->kwthread = kthread_run(sllin_kwthread, sl, "sllin");
1067                 if (sl->kwthread == NULL)
1068                         goto err_free_chan;
1069
1070                 err = register_netdevice(sl->dev);
1071                 if (err)
1072                         goto err_free_chan_and_thread;
1073         }
1074
1075         /* Done.  We have linked the TTY line to a channel. */
1076         rtnl_unlock();
1077         tty->receive_room = SLLIN_BUFF_LEN * 40;        /* We don't flow control */
1078
1079         /* TTY layer expects 0 on success */
1080         return 0;
1081
1082 err_free_chan_and_thread:
1083         kthread_stop(sl->kwthread);
1084         sl->kwthread = NULL;
1085
1086 err_free_chan:
1087         sl->tty = NULL;
1088         tty->disc_data = NULL;
1089         clear_bit(SLF_INUSE, &sl->flags);
1090
1091 err_exit:
1092         rtnl_unlock();
1093
1094         /* Count references from TTY module */
1095         return err;
1096 }
1097
1098 /*
1099  * Close down a SLLIN channel.
1100  * This means flushing out any pending queues, and then returning. This
1101  * call is serialized against other ldisc functions.
1102  *
1103  * We also use this method for a hangup event.
1104  */
1105
1106 static void sllin_close(struct tty_struct *tty)
1107 {
1108         struct sllin *sl = (struct sllin *) tty->disc_data;
1109
1110         /* First make sure we're connected. */
1111         if (!sl || sl->magic != SLLIN_MAGIC || sl->tty != tty)
1112                 return;
1113
1114         kthread_stop(sl->kwthread);
1115         sl->kwthread = NULL;
1116
1117         tty->disc_data = NULL;
1118         sl->tty = NULL;
1119
1120         /* Flush network side */
1121         unregister_netdev(sl->dev);
1122         /* This will complete via sl_free_netdev */
1123 }
1124
1125 static int sllin_hangup(struct tty_struct *tty)
1126 {
1127         sllin_close(tty);
1128         return 0;
1129 }
1130
1131 /* Perform I/O control on an active SLLIN channel. */
1132 static int sllin_ioctl(struct tty_struct *tty, struct file *file,
1133                        unsigned int cmd, unsigned long arg)
1134 {
1135         struct sllin *sl = (struct sllin *) tty->disc_data;
1136         unsigned int tmp;
1137
1138         /* First make sure we're connected. */
1139         if (!sl || sl->magic != SLLIN_MAGIC)
1140                 return -EINVAL;
1141
1142         switch (cmd) {
1143         case SIOCGIFNAME:
1144                 tmp = strlen(sl->dev->name) + 1;
1145                 if (copy_to_user((void __user *)arg, sl->dev->name, tmp))
1146                         return -EFAULT;
1147                 return 0;
1148
1149         case SIOCSIFHWADDR:
1150                 return -EINVAL;
1151
1152         default:
1153                 return tty_mode_ioctl(tty, file, cmd, arg);
1154         }
1155 }
1156
1157 static struct tty_ldisc_ops sll_ldisc = {
1158         .owner          = THIS_MODULE,
1159         .magic          = TTY_LDISC_MAGIC,
1160         .name           = "sllin",
1161         .open           = sllin_open,
1162         .close          = sllin_close,
1163         .hangup         = sllin_hangup,
1164         .ioctl          = sllin_ioctl,
1165         .receive_buf    = sllin_receive_buf,
1166         .write_wakeup   = sllin_write_wakeup,
1167 };
1168
1169 static int __init sllin_init(void)
1170 {
1171         int status;
1172
1173         if (maxdev < 4)
1174                 maxdev = 4; /* Sanity */
1175
1176         printk(banner);
1177         printk(KERN_INFO "sllin: %d dynamic interface channels.\n", maxdev);
1178
1179         sllin_devs = kzalloc(sizeof(struct net_device *)*maxdev, GFP_KERNEL);
1180         if (!sllin_devs) {
1181                 printk(KERN_ERR "sllin: can't allocate sllin device array!\n");
1182                 return -ENOMEM;
1183         }
1184
1185         /* Fill in our line protocol discipline, and register it */
1186         status = tty_register_ldisc(N_SLLIN, &sll_ldisc);
1187         if (status)  {
1188                 printk(KERN_ERR "sllin: can't register line discipline\n");
1189                 kfree(sllin_devs);
1190         }
1191
1192 #ifdef BREAK_BY_BAUD
1193         printk(KERN_INFO "sllin: Break is generated by baud-rate change.");
1194 #else
1195         printk(KERN_INFO "sllin: Break is generated manually with tiny sleep.");
1196 #endif
1197
1198         return status;
1199 }
1200
1201 static void __exit sllin_exit(void)
1202 {
1203         int i;
1204         struct net_device *dev;
1205         struct sllin *sl;
1206         unsigned long timeout = jiffies + HZ;
1207         int busy = 0;
1208
1209         if (sllin_devs == NULL)
1210                 return;
1211
1212         /* First of all: check for active disciplines and hangup them.
1213          */
1214         do {
1215                 if (busy)
1216                         msleep_interruptible(100);
1217
1218                 busy = 0;
1219                 for (i = 0; i < maxdev; i++) {
1220                         dev = sllin_devs[i];
1221                         if (!dev)
1222                                 continue;
1223                         sl = netdev_priv(dev);
1224                         spin_lock_bh(&sl->lock);
1225                         if (sl->tty) {
1226                                 busy++;
1227                                 tty_hangup(sl->tty);
1228                         }
1229                         spin_unlock_bh(&sl->lock);
1230                 }
1231         } while (busy && time_before(jiffies, timeout));
1232
1233         /* FIXME: hangup is async so we should wait when doing this second
1234            phase */
1235
1236         for (i = 0; i < maxdev; i++) {
1237                 dev = sllin_devs[i];
1238                 if (!dev)
1239                         continue;
1240                 sllin_devs[i] = NULL;
1241
1242                 sl = netdev_priv(dev);
1243                 if (sl->tty) {
1244                         printk(KERN_ERR "%s: tty discipline still running\n",
1245                                dev->name);
1246                         /* Intentionally leak the control block. */
1247                         dev->destructor = NULL;
1248                 }
1249
1250                 unregister_netdev(dev);
1251         }
1252
1253         kfree(sllin_devs);
1254         sllin_devs = NULL;
1255
1256         i = tty_unregister_ldisc(N_SLLIN);
1257         if (i)
1258                 printk(KERN_ERR "sllin: can't unregister ldisc (err %d)\n", i);
1259 }
1260
1261 module_init(sllin_init);
1262 module_exit(sllin_exit);