]> rtime.felk.cvut.cz Git - linux-lin.git/blob - sllin/sllin.c
2088794e4ccbeb5a779c3095e719a2e90cf71d70
[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  * Idea:       Oliver Hartkopp <oliver.hartkopp@volkswagen.de>
36  * Copyright:  (c) 2011 Czech Technical University in Prague
37  *             (c) 2011 Volkswagen Group Research
38  * Authors:    Pavel Pisa <pisa@cmp.felk.cvut.cz>
39  *             Rostislav Lisovy <lisovy@kormus.cz>
40  *             Michal Sojka <sojkam1@fel.cvut.cz>
41  * Funded by:  Volkswagen Group Research
42  */
43
44 //#define DEBUG                 1 /* Enables pr_debug() printouts */
45 //#define SLLIN_LED_TRIGGER /* Enables led triggers */
46
47 #include <linux/module.h>
48 #include <linux/moduleparam.h>
49
50 #include <linux/uaccess.h>
51 #include <linux/bitops.h>
52 #include <linux/string.h>
53 #include <linux/tty.h>
54 #include <linux/errno.h>
55 #include <linux/netdevice.h>
56 #include <linux/skbuff.h>
57 #include <linux/rtnetlink.h>
58 #include <linux/if_arp.h>
59 #include <linux/if_ether.h>
60 #include <linux/sched.h>
61 #include <linux/delay.h>
62 #include <linux/init.h>
63 #include <linux/can.h>
64 #include <linux/kthread.h>
65 #include <linux/hrtimer.h>
66 #include <linux/version.h>
67 #include "linux/lin_bus.h"
68
69 /* Should be in include/linux/tty.h */
70 #define N_SLLIN                 25
71 /* -------------------------------- */
72
73 #ifdef SLLIN_LED_TRIGGER
74 #define SLLIN_LED_NAME_SZ (IFNAMSIZ + 6)
75 #include <linux/leds.h>
76
77 enum sllin_led_event {
78         SLLIN_LED_EVENT_OPEN,
79         SLLIN_LED_EVENT_STOP,
80         SLLIN_LED_EVENT_TX,
81         SLLIN_LED_EVENT_RX
82 };
83
84 static unsigned long led_delay = 50;
85 module_param(led_delay, ulong, 0644);
86 MODULE_PARM_DESC(led_delay,
87                  "blink delay time for activity leds (msecs, default: 50).");
88 #endif /* SLLIN_LED_TRIGGER */
89
90 static __initdata const char banner[] =
91         KERN_INFO "sllin: serial line LIN interface driver\n";
92
93 MODULE_ALIAS_LDISC(N_SLLIN);
94 MODULE_DESCRIPTION("serial line LIN interface");
95 MODULE_LICENSE("GPL");
96 MODULE_AUTHOR("Pavel Pisa <pisa@cmp.felk.cvut.cz>");
97
98 #define SLLIN_MAGIC             0x53CA
99 /* #define BREAK_BY_BAUD */
100
101 static bool master = true;
102 static int baudrate; /* Use LIN_DEFAULT_BAUDRATE when not set */
103
104 module_param(master, bool, 0444);
105 MODULE_PARM_DESC(master, "LIN interface is Master device");
106 module_param(baudrate, int, 0444);
107 MODULE_PARM_DESC(baudrate, "Baudrate of LIN interface");
108
109 static int maxdev = 10;         /* MAX number of SLLIN channels;
110                                    This can be overridden with
111                                    insmod sllin.ko maxdev=nnn   */
112 module_param(maxdev, int, 0444);
113 MODULE_PARM_DESC(maxdev, "Maximum number of sllin interfaces");
114
115 /* maximum buffer len to store whole LIN message*/
116 #define SLLIN_DATA_MAX          8
117 #define SLLIN_BUFF_LEN          (1 /*break*/ + 1 /*sync*/ + 1 /*ID*/ + \
118                                 SLLIN_DATA_MAX + 1 /*checksum*/)
119 #define SLLIN_BUFF_BREAK        0
120 #define SLLIN_BUFF_SYNC         1
121 #define SLLIN_BUFF_ID           2
122 #define SLLIN_BUFF_DATA         3
123
124 #define SLLIN_SAMPLES_PER_CHAR  10
125 #define SLLIN_CHARS_TO_TIMEOUT  24
126
127 enum slstate {
128         SLSTATE_IDLE = 0,
129         SLSTATE_BREAK_SENT,
130         SLSTATE_ID_SENT,
131         SLSTATE_RESPONSE_WAIT, /* Wait for response */
132         SLSTATE_RESPONSE_WAIT_BUS, /* Wait for response from LIN bus
133                                 only (CAN frames from network stack
134                                 are not processed in this moment) */
135         SLSTATE_ID_RECEIVED,
136         SLSTATE_RESPONSE_SENT,
137 };
138
139 struct sllin_conf_entry {
140         int dlc;                /* Length of data in LIN frame */
141         canid_t frame_fl;       /* LIN frame flags. Passed from userspace as
142                                    canid_t data type */
143         u8 data[8];             /* LIN frame data payload */
144 };
145
146 struct sllin {
147         int                     magic;
148
149         /* Various fields. */
150         struct tty_struct       *tty;           /* ptr to TTY structure      */
151         struct net_device       *dev;           /* easy for intr handling    */
152         spinlock_t              lock;
153
154         /* LIN message buffer and actual processed data counts */
155         unsigned char           rx_buff[SLLIN_BUFF_LEN]; /* LIN Rx buffer */
156         unsigned char           tx_buff[SLLIN_BUFF_LEN]; /* LIN Tx buffer */
157         int                     rx_expect;      /* expected number of Rx chars */
158         int                     rx_lim;         /* maximum Rx chars for current frame */
159         int                     rx_cnt;         /* message buffer Rx fill level  */
160         int                     tx_lim;         /* actual limit of bytes to Tx */
161         int                     tx_cnt;         /* number of already Tx bytes */
162         char                    lin_master;     /* node is a master node */
163         int                     lin_baud;       /* LIN baudrate */
164         int                     lin_state;      /* state */
165         char                    id_to_send;     /* there is ID to be sent */
166         char                    data_to_send;   /* there are data to be sent */
167         char                    resp_len_known; /* Length of the response is known */
168         char                    header_received;/* In Slave mode, set when header was already
169                                                    received */
170         char                    rx_len_unknown; /* We are not sure how much data will be sent to us --
171                                                    we just guess the length */
172
173         unsigned long           flags;          /* Flag values/ mode etc     */
174 #define SLF_INUSE               0               /* Channel in use            */
175 #define SLF_ERROR               1               /* Parity, etc. error        */
176 #define SLF_RXEVENT             2               /* Rx wake event             */
177 #define SLF_TXEVENT             3               /* Tx wake event             */
178 #define SLF_MSGEVENT            4               /* CAN message to sent       */
179 #define SLF_TMOUTEVENT          5               /* Timeout on received data  */
180 #define SLF_TXBUFF_RQ           6               /* Req. to send buffer to UART*/
181 #define SLF_TXBUFF_INPR         7               /* Above request in progress */
182
183         dev_t                   line;
184         struct task_struct      *kwthread;
185         wait_queue_head_t       kwt_wq;         /* Wait queue used by kwthread */
186         struct hrtimer          rx_timer;       /* RX timeout timer */
187         ktime_t                 rx_timer_timeout; /* RX timeout timer value */
188         struct sk_buff          *tx_req_skb;    /* Socket buffer with CAN frame
189                                                 received from network stack*/
190
191         /* List with configurations for each of 0 to LIN_ID_MAX LIN IDs */
192         struct sllin_conf_entry linfr_cache[LIN_ID_MAX + 1];
193         spinlock_t              linfr_lock;     /* frame cache and buffers lock */
194
195 #ifdef SLLIN_LED_TRIGGER
196         struct led_trigger *tx_led_trig;
197         char                tx_led_trig_name[SLLIN_LED_NAME_SZ];
198         struct led_trigger *rx_led_trig;
199         char                rx_led_trig_name[SLLIN_LED_NAME_SZ];
200         struct led_trigger *rxtx_led_trig;
201         char                rxtx_led_trig_name[SLLIN_LED_NAME_SZ];
202 #endif
203 };
204
205 static struct net_device **sllin_devs;
206 static int sllin_configure_frame_cache(struct sllin *sl, struct can_frame *cf);
207 static void sllin_slave_receive_buf(struct tty_struct *tty,
208                               const unsigned char *cp, char *fp, int count);
209 static void sllin_master_receive_buf(struct tty_struct *tty,
210                               const unsigned char *cp, char *fp, int count);
211
212
213 /* Values of two parity bits in LIN Protected
214    Identifier for each particular LIN ID */
215 const unsigned char sllin_id_parity_table[] = {
216         0x80, 0xc0, 0x40, 0x00, 0xc0, 0x80, 0x00, 0x40,
217         0x00, 0x40, 0xc0, 0x80, 0x40, 0x00, 0x80, 0xc0,
218         0x40, 0x00, 0x80, 0xc0, 0x00, 0x40, 0xc0, 0x80,
219         0xc0, 0x80, 0x00, 0x40, 0x80, 0xc0, 0x40, 0x00,
220         0x00, 0x40, 0xc0, 0x80, 0x40, 0x00, 0x80, 0xc0,
221         0x80, 0xc0, 0x40, 0x00, 0xc0, 0x80, 0x00, 0x40,
222         0xc0, 0x80, 0x00, 0x40, 0x80, 0xc0, 0x40, 0x00,
223         0x40, 0x00, 0x80, 0xc0, 0x00, 0x40, 0xc0, 0x80
224 };
225
226 #ifdef SLLIN_LED_TRIGGER
227 static void sllin_led_event(struct net_device *netdev, enum sllin_led_event event)
228 {
229         struct sllin *sl = netdev_priv(netdev);
230
231         switch (event) {
232         case SLLIN_LED_EVENT_OPEN:
233                 led_trigger_event(sl->tx_led_trig,   LED_FULL);
234                 led_trigger_event(sl->rx_led_trig,   LED_FULL);
235                 led_trigger_event(sl->rxtx_led_trig, LED_FULL);
236                 break;
237         case SLLIN_LED_EVENT_STOP:
238                 led_trigger_event(sl->tx_led_trig,   LED_OFF);
239                 led_trigger_event(sl->rx_led_trig,   LED_OFF);
240                 led_trigger_event(sl->rxtx_led_trig, LED_OFF);
241                 break;
242         case SLLIN_LED_EVENT_TX:
243                 if (led_delay) {
244                         led_trigger_blink_oneshot(sl->tx_led_trig,
245                                                   &led_delay, &led_delay, 1);
246                         led_trigger_blink_oneshot(sl->rxtx_led_trig,
247                                                   &led_delay, &led_delay, 1);
248                 }
249                 break;
250         case SLLIN_LED_EVENT_RX:
251                 if (led_delay) {
252                         led_trigger_blink_oneshot(sl->rx_led_trig,
253                                                   &led_delay, &led_delay, 1);
254                         led_trigger_blink_oneshot(sl->rxtx_led_trig,
255                                                   &led_delay, &led_delay, 1);
256                 }
257                 break;
258         }
259 }
260
261 static void sllin_led_release(struct device *gendev, void *res)
262 {
263         struct sllin *sl = netdev_priv(to_net_dev(gendev));
264
265         led_trigger_unregister_simple(sl->tx_led_trig);
266         led_trigger_unregister_simple(sl->rx_led_trig);
267         led_trigger_unregister_simple(sl->rxtx_led_trig);
268 }
269
270 static void devm_sllin_led_init(struct net_device *netdev)
271 {
272         struct sllin *sl = netdev_priv(netdev);
273         void *res;
274
275         res = devres_alloc(sllin_led_release, 0, GFP_KERNEL);
276         if (!res) {
277                 netdev_err(netdev, "cannot register LED triggers\n");
278                 return;
279         }
280
281         snprintf(sl->tx_led_trig_name, sizeof(sl->tx_led_trig_name),
282                  "%s-tx", netdev->name);
283         snprintf(sl->rx_led_trig_name, sizeof(sl->rx_led_trig_name),
284                  "%s-rx", netdev->name);
285         snprintf(sl->rxtx_led_trig_name, sizeof(sl->rxtx_led_trig_name),
286                  "%s-rxtx", netdev->name);
287
288         led_trigger_register_simple(sl->tx_led_trig_name,
289                                     &sl->tx_led_trig);
290         led_trigger_register_simple(sl->rx_led_trig_name,
291                                     &sl->rx_led_trig);
292         led_trigger_register_simple(sl->rxtx_led_trig_name,
293                                     &sl->rxtx_led_trig);
294
295         devres_add(&netdev->dev, res);
296 }
297
298 static struct sllin *netdev_priv_safe(struct net_device *dev)
299 {
300         int i;
301
302         if (sllin_devs == NULL)
303                 return NULL;
304
305         for (i = 0; i < maxdev; ++i)
306                 if (sllin_devs[i] == dev)
307                         return netdev_priv(dev);
308
309         return NULL;
310 }
311
312 static int sllin_netdev_notifier_call(struct notifier_block *nb, unsigned long msg,
313                               void *ptr)
314 {
315         struct net_device *netdev = netdev_notifier_info_to_dev(ptr);
316         struct sllin *sl = netdev_priv_safe(netdev);
317         char name[SLLIN_LED_NAME_SZ];
318
319         if (!sl)
320                 return NOTIFY_DONE;
321
322         if (!sl->tx_led_trig || !sl->rx_led_trig || !sl->rxtx_led_trig)
323                 return NOTIFY_DONE;
324
325         if (msg == NETDEV_CHANGENAME) {
326                 snprintf(name, sizeof(name), "%s-tx", netdev->name);
327                 led_trigger_rename_static(name, sl->tx_led_trig);
328
329                 snprintf(name, sizeof(name), "%s-rx", netdev->name);
330                 led_trigger_rename_static(name, sl->rx_led_trig);
331
332                 snprintf(name, sizeof(name), "%s-rxtx", netdev->name);
333                 led_trigger_rename_static(name, sl->rxtx_led_trig);
334         }
335
336         return NOTIFY_DONE;
337 }
338
339 static struct notifier_block sllin_netdev_notifier __read_mostly = {
340         .notifier_call = sllin_netdev_notifier_call,
341 };
342 #endif /* SLLIN_LED_TRIGGER */
343
344 /**
345  * sltty_change_speed() -- Change baudrate of Serial device belonging
346  *                         to particular @tty
347  *
348  * @tty:        Pointer to TTY to change speed for.
349  * @speed:      Integer value of new speed. It is possible to
350  *              assign non-standard values, i.e. those which
351  *              are not defined in termbits.h.
352  */
353 static int sltty_change_speed(struct tty_struct *tty, unsigned speed)
354 {
355         struct ktermios old_termios, termios;
356         int cflag;
357
358 #if LINUX_VERSION_CODE < KERNEL_VERSION(3, 12, 0)
359         mutex_lock(&tty->termios_mutex);
360 #else
361         down_write(&tty->termios_rwsem);
362 #endif
363
364 #if LINUX_VERSION_CODE < KERNEL_VERSION(3, 7, 0)
365         old_termios = termios = *(tty->termios);
366 #else
367         old_termios = termios = tty->termios;
368 #endif
369
370         cflag = CS8 | CREAD | CLOCAL | HUPCL;
371         cflag &= ~(CBAUD | CIBAUD);
372         cflag |= BOTHER;
373         termios.c_cflag = cflag;
374         termios.c_oflag = 0;
375         termios.c_lflag = 0;
376
377         /* Enable interrupt when UART-Break or Framing error received */
378         termios.c_iflag = BRKINT | INPCK;
379 #if LINUX_VERSION_CODE < KERNEL_VERSION(3, 7, 0)
380         *(tty->termios) = termios;
381 #else
382         tty->termios = termios;
383 #endif
384
385         tty_encode_baud_rate(tty, speed, speed);
386
387         if (tty->ops->set_termios)
388                 tty->ops->set_termios(tty, &old_termios);
389
390 #if LINUX_VERSION_CODE < KERNEL_VERSION(3, 12, 0)
391         mutex_unlock(&tty->termios_mutex);
392 #else
393         up_write(&tty->termios_rwsem);
394 #endif
395
396         return 0;
397 }
398
399 /* Send one can_frame to the network layer */
400 static void sllin_send_canfr(struct sllin *sl, canid_t id, char *data, int len)
401 {
402         struct sk_buff *skb;
403         struct can_frame cf;
404
405         cf.can_id = id;
406         cf.can_dlc = len;
407         if (cf.can_dlc > 0)
408                 memcpy(&cf.data, data, cf.can_dlc);
409
410         skb = dev_alloc_skb(sizeof(struct can_frame));
411         if (!skb)
412                 return;
413
414         skb->dev = sl->dev;
415         skb->protocol = htons(ETH_P_CAN);
416         skb->pkt_type = PACKET_BROADCAST;
417         skb->ip_summed = CHECKSUM_UNNECESSARY;
418         memcpy(skb_put(skb, sizeof(struct can_frame)),
419                &cf, sizeof(struct can_frame));
420         netif_rx(skb);
421
422         sl->dev->stats.rx_packets++;
423         sl->dev->stats.rx_bytes += cf.can_dlc;
424
425 #ifdef SLLIN_LED_TRIGGER
426         sllin_led_event(sl->dev, SLLIN_LED_EVENT_RX);
427 #endif
428 }
429
430 /**
431  * sll_bump() -- Send data of received LIN frame (existing in sl->rx_buff)
432  *               as CAN frame
433  *
434  * @sl:
435  */
436 static void sll_bump(struct sllin *sl)
437 {
438         int len = sl->rx_cnt - SLLIN_BUFF_DATA - 1; /* without checksum */
439         len = (len < 0) ? 0 : len;
440
441         sllin_send_canfr(sl, sl->rx_buff[SLLIN_BUFF_ID] & LIN_ID_MASK,
442                 sl->rx_buff + SLLIN_BUFF_DATA, len);
443 }
444
445 static void sll_send_rtr(struct sllin *sl)
446 {
447         sllin_send_canfr(sl, (sl->rx_buff[SLLIN_BUFF_ID] & LIN_ID_MASK) |
448                 CAN_RTR_FLAG, NULL, 0);
449 }
450
451 /*
452  * Called by the driver when there's room for more data.  If we have
453  * more packets to send, we send them here.
454  */
455 static void sllin_write_wakeup(struct tty_struct *tty)
456 {
457         int actual = 0;
458         int remains;
459         struct sllin *sl = (struct sllin *) tty->disc_data;
460
461         /* First make sure we're connected. */
462         if (!sl || sl->magic != SLLIN_MAGIC || !netif_running(sl->dev))
463                 return;
464
465         set_bit(SLF_TXBUFF_RQ, &sl->flags);
466         do {
467                 if (unlikely(test_and_set_bit(SLF_TXBUFF_INPR, &sl->flags)))
468                         return; /* ongoing concurrent processing */
469
470                 clear_bit(SLF_TXBUFF_RQ, &sl->flags);
471
472 #if LINUX_VERSION_CODE < KERNEL_VERSION(3, 18, 0)
473                 smp_mb__after_clear_bit();
474 #else
475                 smp_mb__after_atomic();
476 #endif
477
478                 if (sl->lin_state != SLSTATE_BREAK_SENT)
479                         remains = sl->tx_lim - sl->tx_cnt;
480                 else
481                         remains = SLLIN_BUFF_BREAK + 1 - sl->tx_cnt;
482
483                 if (remains > 0) {
484                         actual = tty->ops->write(tty, sl->tx_buff + sl->tx_cnt,
485                                 sl->tx_cnt - sl->tx_lim);
486                         sl->tx_cnt += actual;
487                         remains -= actual;
488                 }
489                 clear_bit(SLF_TXBUFF_INPR, &sl->flags);
490 #if LINUX_VERSION_CODE < KERNEL_VERSION(3, 18, 0)
491                 smp_mb__after_clear_bit();
492 #else
493                 smp_mb__after_atomic();
494 #endif
495
496         } while (unlikely(test_bit(SLF_TXBUFF_RQ, &sl->flags)));
497
498         if ((remains > 0) && (actual >= 0)) {
499                 netdev_dbg(sl->dev, "sllin_write_wakeup sent %d, remains %d, waiting\n",
500                         sl->tx_cnt, sl->tx_lim - sl->tx_cnt);
501                 return;
502         }
503
504         clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
505         set_bit(SLF_TXEVENT, &sl->flags);
506         wake_up(&sl->kwt_wq);
507
508         netdev_dbg(sl->dev, "sllin_write_wakeup sent %d, wakeup\n", sl->tx_cnt);
509 }
510
511 /**
512  * sll_xmit() -- Send a can_frame to a TTY queue.
513  *
514  * @skb: Pointer to Socket buffer to be sent.
515  * @dev: Network device where @skb will be sent.
516  */
517 static netdev_tx_t sll_xmit(struct sk_buff *skb, struct net_device *dev)
518 {
519         struct sllin *sl = netdev_priv(dev);
520         struct can_frame *cf;
521
522         if (skb->len != sizeof(struct can_frame))
523                 goto err_out;
524
525         spin_lock(&sl->lock);
526         if (!netif_running(dev))  {
527                 netdev_warn(sl->dev, "xmit: iface is down\n");
528                 goto err_out_unlock;
529         }
530         if (sl->tty == NULL) {
531                 netdev_warn(sl->dev, "xmit: no tty device connected\n");
532                 goto err_out_unlock;
533         }
534
535         cf = (struct can_frame *) skb->data;
536         if (cf->can_id & LIN_CTRL_FRAME) {
537                 sllin_configure_frame_cache(sl, cf);
538                 goto free_out_unlock;
539         }
540
541         netif_stop_queue(sl->dev);
542
543         sl->tx_req_skb = skb;
544         set_bit(SLF_MSGEVENT, &sl->flags);
545         wake_up(&sl->kwt_wq);
546         spin_unlock(&sl->lock);
547         return NETDEV_TX_OK;
548
549 free_out_unlock:
550 err_out_unlock:
551         spin_unlock(&sl->lock);
552 err_out:
553         kfree_skb(skb);
554         return NETDEV_TX_OK;
555 }
556
557
558 /******************************************
559  *   Routines looking at netdevice side.
560  ******************************************/
561
562 /* Netdevice UP -> DOWN routine */
563 static int sll_close(struct net_device *dev)
564 {
565         struct sllin *sl = netdev_priv(dev);
566
567         spin_lock_bh(&sl->lock);
568         if (sl->tty) {
569                 /* TTY discipline is running. */
570                 clear_bit(TTY_DO_WRITE_WAKEUP, &sl->tty->flags);
571         }
572         netif_stop_queue(dev);
573         sl->rx_expect = 0;
574         sl->tx_lim    = 0;
575         spin_unlock_bh(&sl->lock);
576
577 #ifdef SLLIN_LED_TRIGGER
578         sllin_led_event(dev, SLLIN_LED_EVENT_STOP);
579 #endif
580         return 0;
581 }
582
583 /* Netdevice DOWN -> UP routine */
584 static int sll_open(struct net_device *dev)
585 {
586         struct sllin *sl = netdev_priv(dev);
587
588         netdev_dbg(sl->dev, "%s() invoked\n", __func__);
589
590         if (sl->tty == NULL)
591                 return -ENODEV;
592
593         sl->flags &= (1 << SLF_INUSE);
594         netif_start_queue(dev);
595
596 #ifdef SLLIN_LED_TRIGGER
597         sllin_led_event(dev, SLLIN_LED_EVENT_OPEN);
598 #endif
599         return 0;
600 }
601
602 /* Hook the destructor so we can free sllin devs at the right point in time */
603 static void sll_free_netdev(struct net_device *dev)
604 {
605         int i = dev->base_addr;
606         free_netdev(dev);
607         sllin_devs[i] = NULL;
608 }
609
610 static const struct net_device_ops sll_netdev_ops = {
611         .ndo_open               = sll_open,
612         .ndo_stop               = sll_close,
613         .ndo_start_xmit         = sll_xmit,
614 };
615
616 static void sll_setup(struct net_device *dev)
617 {
618         dev->netdev_ops         = &sll_netdev_ops;
619         dev->destructor         = sll_free_netdev;
620
621         dev->hard_header_len    = 0;
622         dev->addr_len           = 0;
623         dev->tx_queue_len       = 10;
624
625         dev->mtu                = sizeof(struct can_frame);
626         dev->type               = ARPHRD_CAN;
627
628         /* New-style flags. */
629         dev->flags              = IFF_NOARP;
630         dev->features           = NETIF_F_HW_CSUM; /* NETIF_F_NO_CSUM;*/
631 }
632
633 /******************************************
634   Routines looking at TTY side.
635  ******************************************/
636 static void sllin_master_receive_buf(struct tty_struct *tty,
637                               const unsigned char *cp, char *fp, int count)
638 {
639         struct sllin *sl = (struct sllin *) tty->disc_data;
640
641         /* Read the characters out of the buffer */
642         while (count--) {
643                 if (fp && *fp++) {
644                         netdev_dbg(sl->dev, "sllin_master_receive_buf char 0x%02x ignored "
645                                 "due marker 0x%02x, flags 0x%lx\n",
646                                 *cp, *(fp-1), sl->flags);
647
648                         /* i.e. Real error -- not Break */
649                         if (sl->rx_cnt > SLLIN_BUFF_BREAK) {
650                                 set_bit(SLF_ERROR, &sl->flags);
651                                 wake_up(&sl->kwt_wq);
652                                 return;
653                         }
654                 }
655
656 #ifndef BREAK_BY_BAUD
657                 /* We didn't receive Break character -- fake it! */
658                 if ((sl->rx_cnt == SLLIN_BUFF_BREAK) && (*cp == 0x55)) {
659                         netdev_dbg(sl->dev, "LIN_RX[%d]: 0x00\n", sl->rx_cnt);
660                         sl->rx_buff[sl->rx_cnt++] = 0x00;
661                 }
662 #endif /* BREAK_BY_BAUD */
663
664                 if (sl->rx_cnt < SLLIN_BUFF_LEN) {
665                         netdev_dbg(sl->dev, "LIN_RX[%d]: 0x%02x\n", sl->rx_cnt, *cp);
666                         sl->rx_buff[sl->rx_cnt++] = *cp++;
667                 }
668         }
669
670
671         if (sl->rx_cnt >= sl->rx_expect) {
672                 set_bit(SLF_RXEVENT, &sl->flags);
673                 wake_up(&sl->kwt_wq);
674                 netdev_dbg(sl->dev, "sllin_receive_buf count %d, wakeup\n", sl->rx_cnt);
675         } else {
676                 netdev_dbg(sl->dev, "sllin_receive_buf count %d, waiting\n", sl->rx_cnt);
677         }
678 }
679
680
681 /*****************************************
682  *  sllin message helper routines
683  *****************************************/
684 /**
685  * sllin_report_error() -- Report an error by sending CAN frame
686  *      with particular error flag set in can_id
687  *
688  * @sl:
689  * @err: Error flag to be sent.
690  */
691 static void sllin_report_error(struct sllin *sl, int err)
692 {
693         unsigned char *lin_buff;
694         int lin_id;
695
696         switch (err) {
697         case LIN_ERR_CHECKSUM:
698                 sl->dev->stats.rx_crc_errors++;
699                 break;
700
701         case LIN_ERR_RX_TIMEOUT:
702                 sl->dev->stats.rx_errors++;
703                 break;
704
705         case LIN_ERR_FRAMING:
706                 sl->dev->stats.rx_frame_errors++;
707                 break;
708         }
709
710         lin_buff = (sl->lin_master) ? sl->tx_buff : sl->rx_buff;
711         lin_id = lin_buff[SLLIN_BUFF_ID] & LIN_ID_MASK;
712         sllin_send_canfr(sl, lin_id | CAN_EFF_FLAG |
713                 (err & ~LIN_ID_MASK), NULL, 0);
714 }
715
716 /**
717  * sllin_configure_frame_cache() -- Configure particular entry in linfr_cache
718  *
719  * @sl:
720  * @cf: Pointer to CAN frame sent to this driver
721  *      holding configuration information
722  */
723 static int sllin_configure_frame_cache(struct sllin *sl, struct can_frame *cf)
724 {
725         unsigned long flags;
726         struct sllin_conf_entry *sce;
727
728         if (!(cf->can_id & LIN_CTRL_FRAME))
729                 return -1;
730
731         sce = &sl->linfr_cache[cf->can_id & LIN_ID_MASK];
732         netdev_dbg(sl->dev, "Setting frame cache with EFF CAN frame. LIN ID = %d\n",
733                 cf->can_id & LIN_ID_MASK);
734
735         spin_lock_irqsave(&sl->linfr_lock, flags);
736
737         sce->dlc = cf->can_dlc;
738         if (sce->dlc > SLLIN_DATA_MAX)
739                 sce->dlc = SLLIN_DATA_MAX;
740
741         sce->frame_fl = (cf->can_id & ~LIN_ID_MASK) & CAN_EFF_MASK;
742         memcpy(sce->data, cf->data, cf->can_dlc);
743
744         spin_unlock_irqrestore(&sl->linfr_lock, flags);
745
746         return 0;
747 }
748
749 /**
750  * sllin_checksum() -- Count checksum for particular data
751  *
752  * @data:        Pointer to the buffer containing whole LIN
753  *               frame (i.e. including break and sync bytes).
754  * @length:      Length of the buffer.
755  * @enhanced_fl: Flag determining whether Enhanced or Classic
756  *               checksum should be counted.
757  */
758 static inline unsigned sllin_checksum(unsigned char *data, int length, int enhanced_fl)
759 {
760         unsigned csum = 0;
761         int i;
762
763         if (enhanced_fl)
764                 i = SLLIN_BUFF_ID;
765         else
766                 i = SLLIN_BUFF_DATA;
767
768         for (; i < length; i++) {
769                 csum += data[i];
770                 if (csum > 255)
771                         csum -= 255;
772         }
773
774         return ~csum & 0xff;
775 }
776
777 #define SLLIN_STPMSG_RESPONLY           (1) /* Message will be LIN Response only */
778 #define SLLIN_STPMSG_CHCKSUM_CLS        (1 << 1)
779 #define SLLIN_STPMSG_CHCKSUM_ENH        (1 << 2)
780
781 static int sllin_setup_msg(struct sllin *sl, int mode, int id,
782                 unsigned char *data, int len)
783 {
784         if (id > LIN_ID_MASK)
785                 return -1;
786
787         if (!(mode & SLLIN_STPMSG_RESPONLY)) {
788                 sl->rx_cnt = 0;
789                 sl->tx_cnt = 0;
790                 sl->rx_expect = 0;
791                 sl->rx_lim = SLLIN_BUFF_LEN;
792         }
793
794         sl->tx_buff[SLLIN_BUFF_BREAK] = 0;
795         sl->tx_buff[SLLIN_BUFF_SYNC]  = 0x55;
796         sl->tx_buff[SLLIN_BUFF_ID]    = id | sllin_id_parity_table[id];
797         sl->tx_lim = SLLIN_BUFF_DATA;
798
799         if ((data != NULL) && len) {
800                 sl->tx_lim += len;
801                 memcpy(sl->tx_buff + SLLIN_BUFF_DATA, data, len);
802                 sl->tx_buff[sl->tx_lim] = sllin_checksum(sl->tx_buff,
803                                 sl->tx_lim, mode & SLLIN_STPMSG_CHCKSUM_ENH);
804                 sl->tx_lim++;
805         }
806         if (len != 0)
807                 sl->rx_lim = SLLIN_BUFF_DATA + len + 1;
808
809         return 0;
810 }
811
812 static void sllin_reset_buffs(struct sllin *sl)
813 {
814         sl->rx_cnt = 0;
815         sl->rx_expect = 0;
816         sl->rx_lim = sl->lin_master ? 0 : SLLIN_BUFF_LEN;
817         sl->tx_cnt = 0;
818         sl->tx_lim = 0;
819         sl->id_to_send = false;
820         sl->data_to_send = false;
821 }
822
823 /**
824  * sllin_rx_validate() -- Validate received frame, i,e. check checksum
825  *
826  * @sl:
827  */
828 static int sllin_rx_validate(struct sllin *sl)
829 {
830         unsigned long flags;
831         int actual_id;
832         int ext_chcks_fl;
833         int lin_dlc;
834         unsigned char rec_chcksm = sl->rx_buff[sl->rx_cnt - 1];
835         struct sllin_conf_entry *sce;
836
837         actual_id = sl->rx_buff[SLLIN_BUFF_ID] & LIN_ID_MASK;
838         sce = &sl->linfr_cache[actual_id];
839
840         spin_lock_irqsave(&sl->linfr_lock, flags);
841         lin_dlc = sce->dlc;
842         ext_chcks_fl = sce->frame_fl & LIN_CHECKSUM_EXTENDED;
843         spin_unlock_irqrestore(&sl->linfr_lock, flags);
844
845         if (sllin_checksum(sl->rx_buff, sl->rx_cnt - 1, ext_chcks_fl) !=
846                 rec_chcksm) {
847
848                 /* Type of checksum is configured for particular frame */
849                 if (lin_dlc > 0) {
850                         return -1;
851                 } else {
852                         if (sllin_checksum(sl->rx_buff, sl->rx_cnt - 1,
853                                 !ext_chcks_fl) != rec_chcksm) {
854                                 return -1;
855                         }
856                 }
857         }
858
859         return 0;
860 }
861
862 static void sllin_slave_finish_rx_msg(struct sllin *sl)
863 {
864         if (sllin_rx_validate(sl) == -1) {
865                 netdev_dbg(sl->dev, "sllin: RX validation failed.\n");
866                 sllin_report_error(sl, LIN_ERR_CHECKSUM);
867         } else {
868                 /* Send CAN non-RTR frame with data */
869                 netdev_dbg(sl->dev, "sllin: sending NON-RTR CAN frame with LIN payload.");
870                 sll_bump(sl); /* send packet to the network layer */
871         }
872         /* Prepare for reception of new header */
873         sl->rx_cnt = 0;
874         sl->rx_expect = SLLIN_BUFF_ID + 1;
875         sl->rx_len_unknown = false; /* We do know exact length of the header */
876         sl->header_received = false;
877 }
878
879 static void sllin_slave_receive_buf(struct tty_struct *tty,
880                               const unsigned char *cp, char *fp, int count)
881 {
882         struct sllin *sl = (struct sllin *) tty->disc_data;
883         int lin_id;
884         struct sllin_conf_entry *sce;
885
886
887         /* Read the characters out of the buffer */
888         while (count--) {
889                 if (fp && *fp++) {
890                         /*
891                          * If we don't know the length of the current message
892                          * and received at least the LIN ID, we received here
893                          * the break of the next message.
894                          * Evaluate the previous one before continuing.
895                          */
896                         if ((sl->rx_len_unknown == true) &&
897                                 (sl->rx_cnt >= SLLIN_BUFF_ID))
898                         {
899                                 hrtimer_cancel(&sl->rx_timer);
900                                 sllin_slave_finish_rx_msg(sl);
901
902                                 set_bit(SLF_RXEVENT, &sl->flags);
903                                 wake_up(&sl->kwt_wq);
904                         }
905
906                         netdev_dbg(sl->dev, "sllin_slave_receive_buf char 0x%02x ignored "
907                                 "due marker 0x%02x, flags 0x%lx\n",
908                                 *cp, *(fp-1), sl->flags);
909
910                         /* Received Break */
911                         sl->rx_cnt = 0;
912                         sl->rx_expect = SLLIN_BUFF_ID + 1;
913                         sl->rx_len_unknown = false; /* We do know exact length of the header */
914                         sl->header_received = false;
915                 }
916
917                 if (sl->rx_cnt < SLLIN_BUFF_LEN) {
918                         netdev_dbg(sl->dev, "LIN_RX[%d]: 0x%02x\n", sl->rx_cnt, *cp);
919
920                         /* We did not receive break (0x00) character */
921                         if ((sl->rx_cnt == SLLIN_BUFF_BREAK) && (*cp == 0x55)) {
922                                 sl->rx_buff[sl->rx_cnt++] = 0x00;
923                         }
924
925                         if (sl->rx_cnt == SLLIN_BUFF_SYNC) {
926                                 /* 'Duplicated' break character -- ignore */
927                                 if (*cp == 0x00) {
928                                         cp++;
929                                         continue;
930                                 }
931
932                                 /* Wrong sync character */
933                                 if (*cp != 0x55)
934                                         break;
935                         }
936
937                         sl->rx_buff[sl->rx_cnt++] = *cp++;
938                 }
939
940                 /* Header received */
941                 if ((sl->header_received == false) && (sl->rx_cnt >= (SLLIN_BUFF_ID + 1))) {
942                         unsigned long flags;
943
944                         lin_id = sl->rx_buff[SLLIN_BUFF_ID] & LIN_ID_MASK;
945                         sce = &sl->linfr_cache[lin_id];
946
947                         spin_lock_irqsave(&sl->linfr_lock, flags);
948
949                         sl->lin_state = SLSTATE_ID_RECEIVED;
950                         /* Is the length of data set in frame cache? */
951                         if (sce->dlc > 0) {
952                                 sl->rx_expect += sce->dlc + 1; /* + checksum */
953                                 sl->rx_len_unknown = false;
954                                 wake_up(&sl->kwt_wq);
955                         } else {
956                                 sl->rx_expect += SLLIN_DATA_MAX + 1; /* + checksum */
957                                 sl->rx_len_unknown = true;
958                         }
959                         spin_unlock_irqrestore(&sl->linfr_lock, flags);
960
961                         sl->header_received = true;
962
963                         hrtimer_start(&sl->rx_timer,
964                                 ktime_add(ktime_get(), sl->rx_timer_timeout),
965                                 HRTIMER_MODE_ABS);
966                         sll_send_rtr(sl);
967                         continue;
968                 }
969
970                 /* Response received */
971                 if ((sl->header_received == true) &&
972                         ((sl->rx_cnt >= sl->rx_expect))) {
973
974                         hrtimer_cancel(&sl->rx_timer);
975                         netdev_dbg(sl->dev, "Received LIN header & LIN response. "
976                                         "rx_cnt = %u, rx_expect = %u\n", sl->rx_cnt,
977                                         sl->rx_expect);
978                         sllin_slave_finish_rx_msg(sl);
979
980                         set_bit(SLF_RXEVENT, &sl->flags);
981                         wake_up(&sl->kwt_wq);
982                 }
983         }
984 }
985
986 static void sllin_receive_buf(struct tty_struct *tty,
987                               const unsigned char *cp, char *fp, int count)
988 {
989         struct sllin *sl = (struct sllin *) tty->disc_data;
990         netdev_dbg(sl->dev, "sllin_receive_buf invoked, count = %u\n", count);
991
992         if (!sl || sl->magic != SLLIN_MAGIC || !netif_running(sl->dev))
993                 return;
994
995         if (sl->lin_master)
996                 sllin_master_receive_buf(tty, cp, fp, count);
997         else
998                 sllin_slave_receive_buf(tty, cp, fp, count);
999
1000 }
1001
1002 static int sllin_send_tx_buff(struct sllin *sl)
1003 {
1004         struct tty_struct *tty = sl->tty;
1005         int remains;
1006         int res;
1007
1008         set_bit(SLF_TXBUFF_RQ, &sl->flags);
1009         do {
1010                 if (unlikely(test_and_set_bit(SLF_TXBUFF_INPR, &sl->flags)))
1011                         return 0;       /* ongoing concurrent processing */
1012
1013                 clear_bit(SLF_TXBUFF_RQ, &sl->flags);
1014 #if LINUX_VERSION_CODE < KERNEL_VERSION(3, 18, 0)
1015                 smp_mb__after_clear_bit();
1016 #else
1017                 smp_mb__after_atomic();
1018 #endif
1019
1020 #ifdef BREAK_BY_BAUD
1021                 if (sl->lin_state != SLSTATE_BREAK_SENT)
1022                         remains = sl->tx_lim - sl->tx_cnt;
1023                 else
1024                         remains = 1;
1025 #else
1026                 remains = sl->tx_lim - sl->tx_cnt;
1027 #endif
1028
1029                 res = tty->ops->write(tty, sl->tx_buff + sl->tx_cnt, remains);
1030                 if (res < 0)
1031                         goto error_in_write;
1032
1033                 remains -= res;
1034                 sl->tx_cnt += res;
1035
1036                 if (remains > 0) {
1037                         set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
1038                         res = tty->ops->write(tty, sl->tx_buff + sl->tx_cnt, remains);
1039                         if (res < 0) {
1040                                 clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
1041                                 goto error_in_write;
1042                         }
1043
1044                         remains -= res;
1045                         sl->tx_cnt += res;
1046                 }
1047
1048                 netdev_dbg(sl->dev, "sllin_send_tx_buff sent %d, remains %d\n",
1049                                 sl->tx_cnt, remains);
1050
1051                 clear_bit(SLF_TXBUFF_INPR, &sl->flags);
1052 #if LINUX_VERSION_CODE < KERNEL_VERSION(3, 18, 0)
1053                 smp_mb__after_clear_bit();
1054 #else
1055                 smp_mb__after_atomic();
1056 #endif
1057
1058         } while (unlikely(test_bit(SLF_TXBUFF_RQ, &sl->flags)));
1059
1060 #ifdef SLLIN_LED_TRIGGER
1061         sllin_led_event(sl->dev, SLLIN_LED_EVENT_TX);
1062 #endif
1063
1064         return 0;
1065
1066 error_in_write:
1067         clear_bit(SLF_TXBUFF_INPR, &sl->flags);
1068         return -1;
1069
1070 }
1071
1072 #ifdef BREAK_BY_BAUD
1073 static int sllin_send_break(struct sllin *sl)
1074 {
1075         struct tty_struct *tty = sl->tty;
1076         unsigned long break_baud;
1077         int res;
1078
1079         break_baud = ((sl->lin_baud * 2) / 3);
1080         sltty_change_speed(tty, break_baud);
1081
1082         tty->ops->flush_buffer(tty);
1083         sl->rx_cnt = SLLIN_BUFF_BREAK;
1084
1085         sl->rx_expect = SLLIN_BUFF_BREAK + 1;
1086         sl->lin_state = SLSTATE_BREAK_SENT;
1087
1088         res = sllin_send_tx_buff(sl);
1089         if (res < 0) {
1090                 sl->lin_state = SLSTATE_IDLE;
1091                 return res;
1092         }
1093
1094         return 0;
1095 }
1096 #else /* BREAK_BY_BAUD */
1097
1098 static int sllin_send_break(struct sllin *sl)
1099 {
1100         struct tty_struct *tty = sl->tty;
1101         int retval;
1102         unsigned long break_baud;
1103         unsigned long usleep_range_min;
1104         unsigned long usleep_range_max;
1105
1106         break_baud = ((sl->lin_baud * 2) / 3);
1107         sl->rx_cnt = SLLIN_BUFF_BREAK;
1108         sl->rx_expect = SLLIN_BUFF_BREAK + 1;
1109         sl->lin_state = SLSTATE_BREAK_SENT;
1110
1111         /* Do the break ourselves; Inspired by
1112            http://lxr.linux.no/#linux+v3.1.2/drivers/tty/tty_io.c#L2452 */
1113         retval = tty->ops->break_ctl(tty, -1);
1114         if (retval)
1115                 return retval;
1116
1117         /* udelay(712); */
1118         usleep_range_min = (1000000l * SLLIN_SAMPLES_PER_CHAR) / break_baud;
1119         usleep_range_max = usleep_range_min + 50;
1120         usleep_range(usleep_range_min, usleep_range_max);
1121
1122         retval = tty->ops->break_ctl(tty, 0);
1123         usleep_range_min = (1000000l * 1 /* 1 bit */) / break_baud;
1124         usleep_range_max = usleep_range_min + 30;
1125         usleep_range(usleep_range_min, usleep_range_max);
1126
1127         tty->ops->flush_buffer(tty);
1128
1129         sl->tx_cnt = SLLIN_BUFF_SYNC;
1130
1131         netdev_dbg(sl->dev, "Break sent.\n");
1132         set_bit(SLF_RXEVENT, &sl->flags);
1133         wake_up(&sl->kwt_wq);
1134
1135         return 0;
1136 }
1137 #endif /* BREAK_BY_BAUD */
1138
1139
1140 static enum hrtimer_restart sllin_rx_timeout_handler(struct hrtimer *hrtimer)
1141 {
1142         struct sllin *sl = container_of(hrtimer, struct sllin, rx_timer);
1143
1144         /*
1145          * Signal timeout when:
1146          * master: We did not receive as much characters as expected
1147          * slave: * we did not receive any data bytes at all
1148          *        * we know the length and didn't receive enough
1149          */
1150         if ((sl->lin_master) ||
1151                         (sl->rx_cnt <= SLLIN_BUFF_DATA) ||
1152                         ((!sl->rx_len_unknown) &&
1153                         (sl->rx_cnt < sl->rx_expect))) {
1154                 sllin_report_error(sl, LIN_ERR_RX_TIMEOUT);
1155                 set_bit(SLF_TMOUTEVENT, &sl->flags);
1156         } else {
1157                 sllin_slave_finish_rx_msg(sl);
1158                 set_bit(SLF_RXEVENT, &sl->flags);
1159         }
1160         wake_up(&sl->kwt_wq);
1161
1162         return HRTIMER_NORESTART;
1163 }
1164
1165 /*****************************************
1166  *  sllin_kwthread - kernel worker thread
1167  *****************************************/
1168
1169 static int sllin_kwthread(void *ptr)
1170 {
1171         struct sllin *sl = (struct sllin *)ptr;
1172         struct tty_struct *tty = sl->tty;
1173         struct sched_param schparam = { .sched_priority = 40 };
1174         int tx_bytes = 0; /* Used for Network statistics */
1175         unsigned long flags;
1176         int mode;
1177         int lin_id;
1178         struct sllin_conf_entry *sce;
1179
1180         netdev_dbg(sl->dev, "sllin_kwthread started.\n");
1181         sched_setscheduler(current, SCHED_FIFO, &schparam);
1182
1183         clear_bit(SLF_ERROR, &sl->flags);
1184         sltty_change_speed(tty, sl->lin_baud);
1185
1186         while (!kthread_should_stop()) {
1187                 struct can_frame *cf;
1188                 u8 *lin_data;
1189                 int lin_dlc;
1190                 u8 lin_data_buff[SLLIN_DATA_MAX];
1191
1192
1193                 if ((sl->lin_state == SLSTATE_IDLE) && sl->lin_master &&
1194                         sl->id_to_send) {
1195                         if (sllin_send_break(sl) < 0) {
1196                                 /* error processing */
1197                         }
1198                 }
1199
1200                 wait_event_killable(sl->kwt_wq, kthread_should_stop() ||
1201                         test_bit(SLF_RXEVENT, &sl->flags) ||
1202                         test_bit(SLF_TXEVENT, &sl->flags) ||
1203                         test_bit(SLF_TMOUTEVENT, &sl->flags) ||
1204                         test_bit(SLF_ERROR, &sl->flags) ||
1205                         (sl->lin_state == SLSTATE_ID_RECEIVED) ||
1206                         (((sl->lin_state == SLSTATE_IDLE) ||
1207                                 (sl->lin_state == SLSTATE_RESPONSE_WAIT))
1208                                 && test_bit(SLF_MSGEVENT, &sl->flags)));
1209
1210                 if (test_and_clear_bit(SLF_RXEVENT, &sl->flags)) {
1211                         netdev_dbg(sl->dev, "sllin_kthread RXEVENT\n");
1212                 }
1213
1214                 if (test_and_clear_bit(SLF_ERROR, &sl->flags)) {
1215                         unsigned long usleep_range_min;
1216                         unsigned long usleep_range_max;
1217                         hrtimer_cancel(&sl->rx_timer);
1218                         netdev_dbg(sl->dev, "sllin_kthread ERROR\n");
1219
1220                         if (sl->lin_state != SLSTATE_IDLE)
1221                                 sllin_report_error(sl, LIN_ERR_FRAMING);
1222
1223                         usleep_range_min = (1000000l * SLLIN_SAMPLES_PER_CHAR * 10) /
1224                                                 sl->lin_baud;
1225                         usleep_range_max = usleep_range_min + 50;
1226                         usleep_range(usleep_range_min, usleep_range_max);
1227                         sllin_reset_buffs(sl);
1228                         sl->lin_state = SLSTATE_IDLE;
1229                 }
1230
1231                 if (test_and_clear_bit(SLF_TXEVENT, &sl->flags)) {
1232                         netdev_dbg(sl->dev, "sllin_kthread TXEVENT\n");
1233                 }
1234
1235                 if (test_and_clear_bit(SLF_TMOUTEVENT, &sl->flags)) {
1236                         netdev_dbg(sl->dev, "sllin_kthread TMOUTEVENT\n");
1237                         sllin_reset_buffs(sl);
1238
1239                         sl->lin_state = SLSTATE_IDLE;
1240                 }
1241
1242                 switch (sl->lin_state) {
1243                 case SLSTATE_IDLE:
1244                         if (!test_bit(SLF_MSGEVENT, &sl->flags))
1245                                 break;
1246
1247                         mode = 0;
1248                         cf = (struct can_frame *)sl->tx_req_skb->data;
1249
1250                         if (cf->can_id & LIN_CHECKSUM_EXTENDED)
1251                                 mode |= SLLIN_STPMSG_CHCKSUM_ENH;
1252
1253                         /* SFF RTR CAN frame -> LIN header */
1254                         if (cf->can_id & CAN_RTR_FLAG) {
1255                                 netdev_dbg(sl->dev, "%s: RTR SFF CAN frame, ID = %x\n",
1256                                         __func__, cf->can_id & LIN_ID_MASK);
1257
1258                                 sce = &sl->linfr_cache[cf->can_id & LIN_ID_MASK];
1259                                 spin_lock_irqsave(&sl->linfr_lock, flags);
1260                                 if (sce->frame_fl & LIN_CHECKSUM_EXTENDED)
1261                                         mode |= SLLIN_STPMSG_CHCKSUM_ENH;
1262
1263                                 /* Is there Slave response in linfr_cache to be sent? */
1264                                 if ((sce->frame_fl & LIN_CACHE_RESPONSE)
1265                                         && (sce->dlc > 0)) {
1266
1267                                         if (sce->frame_fl & LIN_SINGLE_RESPONSE)
1268                                                 sce->frame_fl &= ~LIN_CACHE_RESPONSE;
1269
1270                                         netdev_dbg(sl->dev, "Sending LIN response from linfr_cache\n");
1271
1272                                         lin_data = sce->data;
1273                                         lin_dlc = sce->dlc;
1274                                         if (lin_dlc > SLLIN_DATA_MAX)
1275                                                 lin_dlc = SLLIN_DATA_MAX;
1276                                         memcpy(lin_data_buff, lin_data, lin_dlc);
1277                                         lin_data = lin_data_buff;
1278                                 } else {
1279                                         lin_data = NULL;
1280                                         lin_dlc = sce->dlc;
1281                                 }
1282                                 spin_unlock_irqrestore(&sl->linfr_lock, flags);
1283
1284                         } else { /* SFF NON-RTR CAN frame -> LIN header + LIN response */
1285                                 netdev_dbg(sl->dev, "%s: NON-RTR SFF CAN frame, ID = %x\n",
1286                                         __func__, (int)cf->can_id & LIN_ID_MASK);
1287
1288                                 sce = &sl->linfr_cache[cf->can_id & LIN_ID_MASK];
1289                                 if (sce->frame_fl & LIN_CHECKSUM_EXTENDED)
1290                                         mode |= SLLIN_STPMSG_CHCKSUM_ENH;
1291
1292                                 lin_data = cf->data;
1293                                 lin_dlc = cf->can_dlc;
1294                                 if (lin_dlc > SLLIN_DATA_MAX)
1295                                         lin_dlc = SLLIN_DATA_MAX;
1296                                 tx_bytes = lin_dlc;
1297                         }
1298
1299                         if (sllin_setup_msg(sl, mode, cf->can_id & LIN_ID_MASK,
1300                                 lin_data, lin_dlc) != -1) {
1301
1302                                 sl->id_to_send = true;
1303                                 sl->data_to_send = (lin_data != NULL) ? true : false;
1304                                 sl->resp_len_known = (lin_dlc > 0) ? true : false;
1305                                 sl->dev->stats.tx_packets++;
1306                                 sl->dev->stats.tx_bytes += tx_bytes;
1307                         }
1308
1309                         clear_bit(SLF_MSGEVENT, &sl->flags);
1310                         kfree_skb(sl->tx_req_skb);
1311                         netif_wake_queue(sl->dev);
1312                         hrtimer_start(&sl->rx_timer,
1313                                 ktime_add(ktime_get(), sl->rx_timer_timeout),
1314                                 HRTIMER_MODE_ABS);
1315                         break;
1316
1317                 case SLSTATE_BREAK_SENT:
1318 #ifdef BREAK_BY_BAUD
1319                         if (sl->rx_cnt <= SLLIN_BUFF_BREAK)
1320                                 continue;
1321
1322                         res = sltty_change_speed(tty, sl->lin_baud);
1323 #endif
1324
1325                         sl->lin_state = SLSTATE_ID_SENT;
1326                         sllin_send_tx_buff(sl);
1327                         break;
1328
1329                 case SLSTATE_ID_SENT:
1330                         hrtimer_cancel(&sl->rx_timer);
1331                         sl->id_to_send = false;
1332                         if (sl->data_to_send) {
1333                                 sllin_send_tx_buff(sl);
1334                                 sl->lin_state = SLSTATE_RESPONSE_SENT;
1335                                 sl->rx_expect = sl->tx_lim;
1336                                 goto slstate_response_sent;
1337                         } else {
1338                                 if (sl->resp_len_known) {
1339                                         sl->rx_expect = sl->rx_lim;
1340                                 } else {
1341                                         sl->rx_expect = SLLIN_BUFF_DATA + 2;
1342                                 }
1343                                 sl->lin_state = SLSTATE_RESPONSE_WAIT;
1344                                 /* If we don't receive anything, timer will "unblock" us */
1345                                 hrtimer_start(&sl->rx_timer,
1346                                         ktime_add(ktime_get(), sl->rx_timer_timeout),
1347                                         HRTIMER_MODE_ABS);
1348                                 goto slstate_response_wait;
1349                         }
1350                         break;
1351
1352                 case SLSTATE_RESPONSE_WAIT:
1353 slstate_response_wait:
1354                         if (test_bit(SLF_MSGEVENT, &sl->flags)) {
1355                                 unsigned char *lin_buff;
1356                                 cf = (struct can_frame *)sl->tx_req_skb->data;
1357
1358                                 lin_buff = (sl->lin_master) ? sl->tx_buff : sl->rx_buff;
1359                                 if (cf->can_id == (lin_buff[SLLIN_BUFF_ID] & LIN_ID_MASK)) {
1360                                         hrtimer_cancel(&sl->rx_timer);
1361                                         netdev_dbg(sl->dev, "received LIN response in a CAN frame.\n");
1362                                         if (sllin_setup_msg(sl, SLLIN_STPMSG_RESPONLY,
1363                                                 cf->can_id & LIN_ID_MASK,
1364                                                 cf->data, cf->can_dlc) != -1) {
1365
1366                                                 sl->rx_expect = sl->tx_lim;
1367                                                 sl->data_to_send = true;
1368                                                 sl->dev->stats.tx_packets++;
1369                                                 sl->dev->stats.tx_bytes += tx_bytes;
1370
1371                                                 if (!sl->lin_master) {
1372                                                         sl->tx_cnt = SLLIN_BUFF_DATA;
1373                                                 }
1374
1375                                                 sllin_send_tx_buff(sl);
1376                                                 clear_bit(SLF_MSGEVENT, &sl->flags);
1377                                                 kfree_skb(sl->tx_req_skb);
1378                                                 netif_wake_queue(sl->dev);
1379
1380                                                 sl->lin_state = SLSTATE_RESPONSE_SENT;
1381                                                 goto slstate_response_sent;
1382                                         }
1383                                 } else {
1384                                         sl->lin_state = SLSTATE_RESPONSE_WAIT_BUS;
1385                                 }
1386                         }
1387
1388                         /* Be aware, no BREAK here */
1389                 case SLSTATE_RESPONSE_WAIT_BUS:
1390                         if (sl->rx_cnt < sl->rx_expect)
1391                                 continue;
1392
1393                         hrtimer_cancel(&sl->rx_timer);
1394                         netdev_dbg(sl->dev, "response received ID %d len %d\n",
1395                                 sl->rx_buff[SLLIN_BUFF_ID], sl->rx_cnt - SLLIN_BUFF_DATA - 1);
1396
1397                         if (sllin_rx_validate(sl) == -1) {
1398                                 netdev_dbg(sl->dev, "RX validation failed.\n");
1399                                 sllin_report_error(sl, LIN_ERR_CHECKSUM);
1400                         } else {
1401                                 /* Send CAN non-RTR frame with data */
1402                                 netdev_dbg(sl->dev, "sending NON-RTR CAN frame with LIN payload.");
1403                                 sll_bump(sl); /* send packet to the network layer */
1404                         }
1405
1406                         sl->id_to_send = false;
1407                         sl->lin_state = SLSTATE_IDLE;
1408                         break;
1409
1410                 case SLSTATE_ID_RECEIVED:
1411                         lin_id = sl->rx_buff[SLLIN_BUFF_ID] & LIN_ID_MASK;
1412                         sce = &sl->linfr_cache[lin_id];
1413                         spin_lock_irqsave(&sl->linfr_lock, flags);
1414
1415                         if ((sce->frame_fl & LIN_CACHE_RESPONSE)
1416                                         && (sce->dlc > 0)) {
1417
1418                                 if (sce->frame_fl & LIN_SINGLE_RESPONSE)
1419                                         sce->frame_fl &= ~LIN_CACHE_RESPONSE;
1420
1421                                 netdev_dbg(sl->dev, "Sending LIN response from linfr_cache\n");
1422
1423                                 lin_data = sce->data;
1424                                 lin_dlc = sce->dlc;
1425                                 if (lin_dlc > SLLIN_DATA_MAX)
1426                                         lin_dlc = SLLIN_DATA_MAX;
1427                                 memcpy(lin_data_buff, lin_data, lin_dlc);
1428                                 lin_data = lin_data_buff;
1429                                 tx_bytes = lin_dlc;
1430
1431                                 mode = SLLIN_STPMSG_RESPONLY;
1432                                 if (sce->frame_fl & LIN_CHECKSUM_EXTENDED)
1433                                         mode |= SLLIN_STPMSG_CHCKSUM_ENH;
1434
1435                                 if (sllin_setup_msg(sl, mode, lin_id & LIN_ID_MASK,
1436                                         lin_data, lin_dlc) != -1) {
1437
1438                                         sl->rx_expect = sl->tx_lim;
1439                                         sl->data_to_send = true;
1440                                         sl->dev->stats.tx_packets++;
1441                                         sl->dev->stats.tx_bytes += tx_bytes;
1442                                         sl->resp_len_known = true;
1443
1444                                         if (!sl->lin_master) {
1445                                                 sl->tx_cnt = SLLIN_BUFF_DATA;
1446                                         }
1447                                         sllin_send_tx_buff(sl);
1448                                 }
1449
1450                                 hrtimer_start(&sl->rx_timer,
1451                                         ktime_add(ktime_get(), sl->rx_timer_timeout),
1452                                         HRTIMER_MODE_ABS);
1453                         }
1454                         spin_unlock_irqrestore(&sl->linfr_lock, flags);
1455                         sl->lin_state = SLSTATE_IDLE;
1456                         break;
1457
1458                 case SLSTATE_RESPONSE_SENT:
1459 slstate_response_sent:
1460                         if (sl->rx_cnt < sl->tx_lim)
1461                                 continue;
1462
1463                         hrtimer_cancel(&sl->rx_timer);
1464                         sll_bump(sl); /* send packet to the network layer */
1465                         netdev_dbg(sl->dev, "response sent ID %d len %d\n",
1466                                 sl->rx_buff[SLLIN_BUFF_ID], sl->rx_cnt - SLLIN_BUFF_DATA - 1);
1467
1468                         sl->id_to_send = false;
1469                         sl->lin_state = SLSTATE_IDLE;
1470                         break;
1471                 }
1472         }
1473
1474         hrtimer_cancel(&sl->rx_timer);
1475         netdev_dbg(sl->dev, "sllin_kwthread stopped.\n");
1476
1477         return 0;
1478 }
1479
1480
1481 /************************************
1482  *  sllin_open helper routines.
1483  ************************************/
1484
1485 /* Collect hanged up channels */
1486 static void sll_sync(void)
1487 {
1488         int i;
1489         struct net_device *dev;
1490         struct sllin      *sl;
1491
1492         for (i = 0; i < maxdev; i++) {
1493                 dev = sllin_devs[i];
1494                 if (dev == NULL)
1495                         break;
1496
1497                 sl = netdev_priv(dev);
1498                 if (sl->tty)
1499                         continue;
1500                 if (dev->flags & IFF_UP)
1501                         dev_close(dev);
1502         }
1503 }
1504
1505 /* Find a free SLLIN channel, and link in this `tty' line. */
1506 static struct sllin *sll_alloc(dev_t line)
1507 {
1508         int i;
1509         struct net_device *dev = NULL;
1510         struct sllin       *sl;
1511
1512         if (sllin_devs == NULL)
1513                 return NULL;    /* Master array missing ! */
1514
1515         for (i = 0; i < maxdev; i++) {
1516                 dev = sllin_devs[i];
1517                 if (dev == NULL)
1518                         break;
1519
1520         }
1521
1522         /* Sorry, too many, all slots in use */
1523         if (i >= maxdev)
1524                 return NULL;
1525
1526         if (dev) {
1527                 sl = netdev_priv(dev);
1528                 if (test_bit(SLF_INUSE, &sl->flags)) {
1529                         unregister_netdevice(dev);
1530                         dev = NULL;
1531                         sllin_devs[i] = NULL;
1532                 }
1533         }
1534
1535         if (!dev) {
1536                 char name[IFNAMSIZ];
1537                 sprintf(name, "sllin%d", i);
1538
1539 #if (LINUX_VERSION_CODE < KERNEL_VERSION(3, 17, 0))
1540                 dev = alloc_netdev(sizeof(*sl), name, sll_setup);
1541 #else
1542                 dev = alloc_netdev(sizeof(*sl), name, NET_NAME_UNKNOWN, sll_setup);
1543 #endif
1544
1545                 if (!dev)
1546                         return NULL;
1547                 dev->base_addr  = i;
1548         }
1549
1550         sl = netdev_priv(dev);
1551         /* Initialize channel control data */
1552         sl->magic = SLLIN_MAGIC;
1553         sl->dev = dev;
1554         spin_lock_init(&sl->lock);
1555         spin_lock_init(&sl->linfr_lock);
1556         sllin_devs[i] = dev;
1557
1558         return sl;
1559 }
1560
1561 /*
1562  * Open the high-level part of the SLLIN channel.
1563  * This function is called by the TTY module when the
1564  * SLLIN line discipline is called for.  Because we are
1565  * sure the tty line exists, we only have to link it to
1566  * a free SLLIN channel...
1567  *
1568  * Called in process context serialized from other ldisc calls.
1569  */
1570
1571 static int sllin_open(struct tty_struct *tty)
1572 {
1573         struct sllin *sl;
1574         int err;
1575
1576         pr_debug("sllin: %s() invoked\n", __func__);
1577
1578         if (!capable(CAP_NET_ADMIN))
1579                 return -EPERM;
1580
1581         if (tty->ops->write == NULL)
1582                 return -EOPNOTSUPP;
1583
1584         /* RTnetlink lock is misused here to serialize concurrent
1585            opens of sllin channels. There are better ways, but it is
1586            the simplest one.
1587          */
1588         rtnl_lock();
1589
1590         /* Collect hanged up channels. */
1591         sll_sync();
1592
1593         sl = tty->disc_data;
1594
1595         err = -EEXIST;
1596         /* First make sure we're not already connected. */
1597         if (sl && sl->magic == SLLIN_MAGIC)
1598                 goto err_exit;
1599
1600         /* OK.  Find a free SLLIN channel to use. */
1601         err = -ENFILE;
1602         sl = sll_alloc(tty_devnum(tty));
1603         if (sl == NULL)
1604                 goto err_exit;
1605
1606         sl->tty = tty;
1607         tty->disc_data = sl;
1608         sl->line = tty_devnum(tty);
1609
1610         if (!test_bit(SLF_INUSE, &sl->flags)) {
1611                 /* Perform the low-level SLLIN initialization. */
1612                 sl->lin_master = master;
1613                 if (master)
1614                         pr_debug("sllin: Configured as MASTER\n");
1615                 else
1616                         pr_debug("sllin: Configured as SLAVE\n");
1617
1618                 sllin_reset_buffs(sl);
1619
1620                 sl->lin_baud = (baudrate == 0) ? LIN_DEFAULT_BAUDRATE : baudrate;
1621                 pr_debug("sllin: Baudrate set to %u\n", sl->lin_baud);
1622
1623                 sl->lin_state = SLSTATE_IDLE;
1624
1625                 hrtimer_init(&sl->rx_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1626                 sl->rx_timer.function = sllin_rx_timeout_handler;
1627                 /* timeval_to_ktime(msg_head->ival1); */
1628                 sl->rx_timer_timeout = ns_to_ktime(
1629                         (1000000000l / sl->lin_baud) *
1630                         SLLIN_SAMPLES_PER_CHAR * SLLIN_CHARS_TO_TIMEOUT);
1631
1632                 set_bit(SLF_INUSE, &sl->flags);
1633
1634                 init_waitqueue_head(&sl->kwt_wq);
1635                 sl->kwthread = kthread_run(sllin_kwthread, sl, "sllin");
1636                 if (sl->kwthread == NULL)
1637                         goto err_free_chan;
1638
1639                 err = register_netdevice(sl->dev);
1640                 if (err)
1641                         goto err_free_chan_and_thread;
1642
1643 #ifdef SLLIN_LED_TRIGGER
1644                 devm_sllin_led_init(sl->dev);
1645 #endif
1646         }
1647
1648         /* Done.  We have linked the TTY line to a channel. */
1649         rtnl_unlock();
1650         tty->receive_room = SLLIN_BUFF_LEN * 40; /* We don't flow control */
1651
1652         /* TTY layer expects 0 on success */
1653         return 0;
1654
1655 err_free_chan_and_thread:
1656         kthread_stop(sl->kwthread);
1657         sl->kwthread = NULL;
1658
1659 err_free_chan:
1660         sl->tty = NULL;
1661         tty->disc_data = NULL;
1662         clear_bit(SLF_INUSE, &sl->flags);
1663
1664 err_exit:
1665         rtnl_unlock();
1666
1667         /* Count references from TTY module */
1668         return err;
1669 }
1670
1671 /*
1672  * Close down a SLLIN channel.
1673  * This means flushing out any pending queues, and then returning. This
1674  * call is serialized against other ldisc functions.
1675  *
1676  * We also use this method for a hangup event.
1677  */
1678
1679 static void sllin_close(struct tty_struct *tty)
1680 {
1681         struct sllin *sl = (struct sllin *) tty->disc_data;
1682
1683         /* First make sure we're connected. */
1684         if (!sl || sl->magic != SLLIN_MAGIC || sl->tty != tty)
1685                 return;
1686
1687         kthread_stop(sl->kwthread);
1688         sl->kwthread = NULL;
1689
1690         tty->disc_data = NULL;
1691         sl->tty = NULL;
1692
1693         /* Flush network side */
1694         unregister_netdev(sl->dev);
1695         /* This will complete via sl_free_netdev */
1696 }
1697
1698 static int sllin_hangup(struct tty_struct *tty)
1699 {
1700         sllin_close(tty);
1701         return 0;
1702 }
1703
1704 /* Perform I/O control on an active SLLIN channel. */
1705 static int sllin_ioctl(struct tty_struct *tty, struct file *file,
1706                        unsigned int cmd, unsigned long arg)
1707 {
1708         struct sllin *sl = (struct sllin *) tty->disc_data;
1709         unsigned int tmp;
1710
1711         /* First make sure we're connected. */
1712         if (!sl || sl->magic != SLLIN_MAGIC)
1713                 return -EINVAL;
1714
1715         switch (cmd) {
1716         case SIOCGIFNAME:
1717                 tmp = strlen(sl->dev->name) + 1;
1718                 if (copy_to_user((void __user *)arg, sl->dev->name, tmp))
1719                         return -EFAULT;
1720                 return 0;
1721
1722         case SIOCSIFHWADDR:
1723                 return -EINVAL;
1724
1725         default:
1726                 return tty_mode_ioctl(tty, file, cmd, arg);
1727         }
1728 }
1729
1730 static struct tty_ldisc_ops sll_ldisc = {
1731         .owner          = THIS_MODULE,
1732         .magic          = TTY_LDISC_MAGIC,
1733         .name           = "sllin",
1734         .open           = sllin_open,
1735         .close          = sllin_close,
1736         .hangup         = sllin_hangup,
1737         .ioctl          = sllin_ioctl,
1738         .receive_buf    = sllin_receive_buf,
1739         .write_wakeup   = sllin_write_wakeup,
1740 };
1741
1742 static int __init sllin_init(void)
1743 {
1744         int status;
1745
1746 #ifdef SLLIN_LED_TRIGGER
1747         status = register_netdevice_notifier(&sllin_netdev_notifier);
1748         if (status)
1749                 pr_err("sllin: can't register netdevice notifier\n");
1750 #endif
1751
1752         if (maxdev < 4)
1753                 maxdev = 4; /* Sanity */
1754
1755         printk(banner);
1756         pr_debug("sllin: %d dynamic interface channels.\n", maxdev);
1757
1758         sllin_devs = kzalloc(sizeof(struct net_device *)*maxdev, GFP_KERNEL);
1759         if (!sllin_devs) {
1760                 pr_err("sllin: can't allocate sllin device array!\n");
1761                 return -ENOMEM;
1762         }
1763
1764         /* Fill in our line protocol discipline, and register it */
1765         status = tty_register_ldisc(N_SLLIN, &sll_ldisc);
1766         if (status)  {
1767                 pr_err("sllin: can't register line discipline\n");
1768                 kfree(sllin_devs);
1769         }
1770
1771 #ifdef BREAK_BY_BAUD
1772         pr_debug("sllin: Break is generated by baud-rate change.");
1773 #else
1774         pr_debug("sllin: Break is generated manually with tiny sleep.");
1775 #endif
1776
1777         return status;
1778 }
1779
1780 static void __exit sllin_exit(void)
1781 {
1782         int i;
1783         struct net_device *dev;
1784         struct sllin *sl;
1785         unsigned long timeout = jiffies + HZ;
1786         int busy = 0;
1787
1788         if (sllin_devs == NULL)
1789                 return;
1790
1791         /* First of all: check for active disciplines and hangup them.
1792          */
1793         do {
1794                 if (busy)
1795                         msleep_interruptible(100);
1796
1797                 busy = 0;
1798                 for (i = 0; i < maxdev; i++) {
1799                         dev = sllin_devs[i];
1800                         if (!dev)
1801                                 continue;
1802                         sl = netdev_priv(dev);
1803                         spin_lock_bh(&sl->lock);
1804                         if (sl->tty) {
1805                                 busy++;
1806                                 tty_hangup(sl->tty);
1807                         }
1808                         spin_unlock_bh(&sl->lock);
1809                 }
1810         } while (busy && time_before(jiffies, timeout));
1811
1812         /* FIXME: hangup is async so we should wait when doing this second
1813            phase */
1814
1815         for (i = 0; i < maxdev; i++) {
1816                 dev = sllin_devs[i];
1817                 if (!dev)
1818                         continue;
1819                 sllin_devs[i] = NULL;
1820
1821                 sl = netdev_priv(dev);
1822                 if (sl->tty) {
1823                         netdev_dbg(sl->dev, "tty discipline still running\n");
1824                         /* Intentionally leak the control block. */
1825                         dev->destructor = NULL;
1826                 }
1827
1828                 unregister_netdev(dev);
1829         }
1830
1831         kfree(sllin_devs);
1832         sllin_devs = NULL;
1833
1834         i = tty_unregister_ldisc(N_SLLIN);
1835         if (i)
1836                 pr_err("sllin: can't unregister ldisc (err %d)\n", i);
1837
1838 #ifdef SLLIN_LED_TRIGGER
1839         unregister_netdevice_notifier(&sllin_netdev_notifier);
1840 #endif
1841
1842 }
1843
1844 module_init(sllin_init);
1845 module_exit(sllin_exit);