]> rtime.felk.cvut.cz Git - linux-lin.git/blob - sllin/sllin.c
b2580f184b382308b3007254e390dfcac28db154
[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, 0);
105 MODULE_PARM_DESC(master, "LIN interface is Master device");
106 module_param(baudrate, int, 0);
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, 0);
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                                 struct sllin_conf_entry *sce;
1256
1257                                 netdev_dbg(sl->dev, "%s: RTR SFF CAN frame, ID = %x\n",
1258                                         __func__, cf->can_id & LIN_ID_MASK);
1259
1260                                 sce = &sl->linfr_cache[cf->can_id & LIN_ID_MASK];
1261                                 spin_lock_irqsave(&sl->linfr_lock, flags);
1262                                 if (sce->frame_fl & LIN_CHECKSUM_EXTENDED)
1263                                         mode |= SLLIN_STPMSG_CHCKSUM_ENH;
1264
1265                                 /* Is there Slave response in linfr_cache to be sent? */
1266                                 if ((sce->frame_fl & LIN_CACHE_RESPONSE)
1267                                         && (sce->dlc > 0)) {
1268
1269                                         if (sce->frame_fl & LIN_SINGLE_RESPONSE)
1270                                                 sce->frame_fl &= ~LIN_CACHE_RESPONSE;
1271
1272                                         netdev_dbg(sl->dev, "Sending LIN response from linfr_cache\n");
1273
1274                                         lin_data = sce->data;
1275                                         lin_dlc = sce->dlc;
1276                                         if (lin_dlc > SLLIN_DATA_MAX)
1277                                                 lin_dlc = SLLIN_DATA_MAX;
1278                                         memcpy(lin_data_buff, lin_data, lin_dlc);
1279                                         lin_data = lin_data_buff;
1280                                 } else {
1281                                         lin_data = NULL;
1282                                         lin_dlc = sce->dlc;
1283                                 }
1284                                 spin_unlock_irqrestore(&sl->linfr_lock, flags);
1285
1286                         } else { /* SFF NON-RTR CAN frame -> LIN header + LIN response */
1287                                 struct sllin_conf_entry *sce;
1288
1289                                 netdev_dbg(sl->dev, "%s: NON-RTR SFF CAN frame, ID = %x\n",
1290                                         __func__, (int)cf->can_id & LIN_ID_MASK);
1291
1292                                 sce = &sl->linfr_cache[cf->can_id & LIN_ID_MASK];
1293                                 if (sce->frame_fl & LIN_CHECKSUM_EXTENDED)
1294                                         mode |= SLLIN_STPMSG_CHCKSUM_ENH;
1295
1296                                 lin_data = cf->data;
1297                                 lin_dlc = cf->can_dlc;
1298                                 if (lin_dlc > SLLIN_DATA_MAX)
1299                                         lin_dlc = SLLIN_DATA_MAX;
1300                                 tx_bytes = lin_dlc;
1301                         }
1302
1303                         if (sllin_setup_msg(sl, mode, cf->can_id & LIN_ID_MASK,
1304                                 lin_data, lin_dlc) != -1) {
1305
1306                                 sl->id_to_send = true;
1307                                 sl->data_to_send = (lin_data != NULL) ? true : false;
1308                                 sl->resp_len_known = (lin_dlc > 0) ? true : false;
1309                                 sl->dev->stats.tx_packets++;
1310                                 sl->dev->stats.tx_bytes += tx_bytes;
1311                         }
1312
1313                         clear_bit(SLF_MSGEVENT, &sl->flags);
1314                         kfree_skb(sl->tx_req_skb);
1315                         netif_wake_queue(sl->dev);
1316                         hrtimer_start(&sl->rx_timer,
1317                                 ktime_add(ktime_get(), sl->rx_timer_timeout),
1318                                 HRTIMER_MODE_ABS);
1319                         break;
1320
1321                 case SLSTATE_BREAK_SENT:
1322 #ifdef BREAK_BY_BAUD
1323                         if (sl->rx_cnt <= SLLIN_BUFF_BREAK)
1324                                 continue;
1325
1326                         res = sltty_change_speed(tty, sl->lin_baud);
1327 #endif
1328
1329                         sl->lin_state = SLSTATE_ID_SENT;
1330                         sllin_send_tx_buff(sl);
1331                         break;
1332
1333                 case SLSTATE_ID_SENT:
1334                         hrtimer_cancel(&sl->rx_timer);
1335                         sl->id_to_send = false;
1336                         if (sl->data_to_send) {
1337                                 sllin_send_tx_buff(sl);
1338                                 sl->lin_state = SLSTATE_RESPONSE_SENT;
1339                                 sl->rx_expect = sl->tx_lim;
1340                                 goto slstate_response_sent;
1341                         } else {
1342                                 if (sl->resp_len_known) {
1343                                         sl->rx_expect = sl->rx_lim;
1344                                 } else {
1345                                         sl->rx_expect = SLLIN_BUFF_DATA + 2;
1346                                 }
1347                                 sl->lin_state = SLSTATE_RESPONSE_WAIT;
1348                                 /* If we don't receive anything, timer will "unblock" us */
1349                                 hrtimer_start(&sl->rx_timer,
1350                                         ktime_add(ktime_get(), sl->rx_timer_timeout),
1351                                         HRTIMER_MODE_ABS);
1352                                 goto slstate_response_wait;
1353                         }
1354                         break;
1355
1356                 case SLSTATE_RESPONSE_WAIT:
1357 slstate_response_wait:
1358                         if (test_bit(SLF_MSGEVENT, &sl->flags)) {
1359                                 unsigned char *lin_buff;
1360                                 cf = (struct can_frame *)sl->tx_req_skb->data;
1361
1362                                 lin_buff = (sl->lin_master) ? sl->tx_buff : sl->rx_buff;
1363                                 if (cf->can_id == (lin_buff[SLLIN_BUFF_ID] & LIN_ID_MASK)) {
1364                                         hrtimer_cancel(&sl->rx_timer);
1365                                         netdev_dbg(sl->dev, "received LIN response in a CAN frame.\n");
1366                                         if (sllin_setup_msg(sl, SLLIN_STPMSG_RESPONLY,
1367                                                 cf->can_id & LIN_ID_MASK,
1368                                                 cf->data, cf->can_dlc) != -1) {
1369
1370                                                 sl->rx_expect = sl->tx_lim;
1371                                                 sl->data_to_send = true;
1372                                                 sl->dev->stats.tx_packets++;
1373                                                 sl->dev->stats.tx_bytes += tx_bytes;
1374
1375                                                 if (!sl->lin_master) {
1376                                                         sl->tx_cnt = SLLIN_BUFF_DATA;
1377                                                 }
1378
1379                                                 sllin_send_tx_buff(sl);
1380                                                 clear_bit(SLF_MSGEVENT, &sl->flags);
1381                                                 kfree_skb(sl->tx_req_skb);
1382                                                 netif_wake_queue(sl->dev);
1383
1384                                                 sl->lin_state = SLSTATE_RESPONSE_SENT;
1385                                                 goto slstate_response_sent;
1386                                         }
1387                                 } else {
1388                                         sl->lin_state = SLSTATE_RESPONSE_WAIT_BUS;
1389                                 }
1390                         }
1391
1392                         /* Be aware, no BREAK here */
1393                 case SLSTATE_RESPONSE_WAIT_BUS:
1394                         if (sl->rx_cnt < sl->rx_expect)
1395                                 continue;
1396
1397                         hrtimer_cancel(&sl->rx_timer);
1398                         netdev_dbg(sl->dev, "response received ID %d len %d\n",
1399                                 sl->rx_buff[SLLIN_BUFF_ID], sl->rx_cnt - SLLIN_BUFF_DATA - 1);
1400
1401                         if (sllin_rx_validate(sl) == -1) {
1402                                 netdev_dbg(sl->dev, "RX validation failed.\n");
1403                                 sllin_report_error(sl, LIN_ERR_CHECKSUM);
1404                         } else {
1405                                 /* Send CAN non-RTR frame with data */
1406                                 netdev_dbg(sl->dev, "sending NON-RTR CAN frame with LIN payload.");
1407                                 sll_bump(sl); /* send packet to the network layer */
1408                         }
1409
1410                         sl->id_to_send = false;
1411                         sl->lin_state = SLSTATE_IDLE;
1412                         break;
1413
1414                 case SLSTATE_ID_RECEIVED:
1415                         lin_id = sl->rx_buff[SLLIN_BUFF_ID] & LIN_ID_MASK;
1416                         sce = &sl->linfr_cache[lin_id];
1417                         spin_lock_irqsave(&sl->linfr_lock, flags);
1418
1419                         if ((sce->frame_fl & LIN_CACHE_RESPONSE)
1420                                         && (sce->dlc > 0)) {
1421
1422                                 if (sce->frame_fl & LIN_SINGLE_RESPONSE)
1423                                         sce->frame_fl &= ~LIN_CACHE_RESPONSE;
1424
1425                                 netdev_dbg(sl->dev, "Sending LIN response from linfr_cache\n");
1426
1427                                 lin_data = sce->data;
1428                                 lin_dlc = sce->dlc;
1429                                 if (lin_dlc > SLLIN_DATA_MAX)
1430                                         lin_dlc = SLLIN_DATA_MAX;
1431                                 memcpy(lin_data_buff, lin_data, lin_dlc);
1432                                 lin_data = lin_data_buff;
1433                                 tx_bytes = lin_dlc;
1434
1435                                 mode = SLLIN_STPMSG_RESPONLY;
1436                                 if (sce->frame_fl & LIN_CHECKSUM_EXTENDED)
1437                                         mode |= SLLIN_STPMSG_CHCKSUM_ENH;
1438
1439                                 if (sllin_setup_msg(sl, mode, lin_id & LIN_ID_MASK,
1440                                         lin_data, lin_dlc) != -1) {
1441
1442                                         sl->rx_expect = sl->tx_lim;
1443                                         sl->data_to_send = true;
1444                                         sl->dev->stats.tx_packets++;
1445                                         sl->dev->stats.tx_bytes += tx_bytes;
1446                                         sl->resp_len_known = true;
1447
1448                                         if (!sl->lin_master) {
1449                                                 sl->tx_cnt = SLLIN_BUFF_DATA;
1450                                         }
1451                                         sllin_send_tx_buff(sl);
1452                                 }
1453
1454                                 hrtimer_start(&sl->rx_timer,
1455                                         ktime_add(ktime_get(), sl->rx_timer_timeout),
1456                                         HRTIMER_MODE_ABS);
1457                         }
1458                         spin_unlock_irqrestore(&sl->linfr_lock, flags);
1459                         sl->lin_state = SLSTATE_IDLE;
1460                         break;
1461
1462                 case SLSTATE_RESPONSE_SENT:
1463 slstate_response_sent:
1464                         if (sl->rx_cnt < sl->tx_lim)
1465                                 continue;
1466
1467                         hrtimer_cancel(&sl->rx_timer);
1468                         sll_bump(sl); /* send packet to the network layer */
1469                         netdev_dbg(sl->dev, "response sent ID %d len %d\n",
1470                                 sl->rx_buff[SLLIN_BUFF_ID], sl->rx_cnt - SLLIN_BUFF_DATA - 1);
1471
1472                         sl->id_to_send = false;
1473                         sl->lin_state = SLSTATE_IDLE;
1474                         break;
1475                 }
1476         }
1477
1478         hrtimer_cancel(&sl->rx_timer);
1479         netdev_dbg(sl->dev, "sllin_kwthread stopped.\n");
1480
1481         return 0;
1482 }
1483
1484
1485 /************************************
1486  *  sllin_open helper routines.
1487  ************************************/
1488
1489 /* Collect hanged up channels */
1490 static void sll_sync(void)
1491 {
1492         int i;
1493         struct net_device *dev;
1494         struct sllin      *sl;
1495
1496         for (i = 0; i < maxdev; i++) {
1497                 dev = sllin_devs[i];
1498                 if (dev == NULL)
1499                         break;
1500
1501                 sl = netdev_priv(dev);
1502                 if (sl->tty)
1503                         continue;
1504                 if (dev->flags & IFF_UP)
1505                         dev_close(dev);
1506         }
1507 }
1508
1509 /* Find a free SLLIN channel, and link in this `tty' line. */
1510 static struct sllin *sll_alloc(dev_t line)
1511 {
1512         int i;
1513         struct net_device *dev = NULL;
1514         struct sllin       *sl;
1515
1516         if (sllin_devs == NULL)
1517                 return NULL;    /* Master array missing ! */
1518
1519         for (i = 0; i < maxdev; i++) {
1520                 dev = sllin_devs[i];
1521                 if (dev == NULL)
1522                         break;
1523
1524         }
1525
1526         /* Sorry, too many, all slots in use */
1527         if (i >= maxdev)
1528                 return NULL;
1529
1530         if (dev) {
1531                 sl = netdev_priv(dev);
1532                 if (test_bit(SLF_INUSE, &sl->flags)) {
1533                         unregister_netdevice(dev);
1534                         dev = NULL;
1535                         sllin_devs[i] = NULL;
1536                 }
1537         }
1538
1539         if (!dev) {
1540                 char name[IFNAMSIZ];
1541                 sprintf(name, "sllin%d", i);
1542
1543 #if (LINUX_VERSION_CODE < KERNEL_VERSION(3, 17, 0))
1544                 dev = alloc_netdev(sizeof(*sl), name, sll_setup);
1545 #else
1546                 dev = alloc_netdev(sizeof(*sl), name, NET_NAME_UNKNOWN, sll_setup);
1547 #endif
1548
1549                 if (!dev)
1550                         return NULL;
1551                 dev->base_addr  = i;
1552         }
1553
1554         sl = netdev_priv(dev);
1555         /* Initialize channel control data */
1556         sl->magic = SLLIN_MAGIC;
1557         sl->dev = dev;
1558         spin_lock_init(&sl->lock);
1559         spin_lock_init(&sl->linfr_lock);
1560         sllin_devs[i] = dev;
1561
1562         return sl;
1563 }
1564
1565 /*
1566  * Open the high-level part of the SLLIN channel.
1567  * This function is called by the TTY module when the
1568  * SLLIN line discipline is called for.  Because we are
1569  * sure the tty line exists, we only have to link it to
1570  * a free SLLIN channel...
1571  *
1572  * Called in process context serialized from other ldisc calls.
1573  */
1574
1575 static int sllin_open(struct tty_struct *tty)
1576 {
1577         struct sllin *sl;
1578         int err;
1579
1580         pr_debug("sllin: %s() invoked\n", __func__);
1581
1582         if (!capable(CAP_NET_ADMIN))
1583                 return -EPERM;
1584
1585         if (tty->ops->write == NULL)
1586                 return -EOPNOTSUPP;
1587
1588         /* RTnetlink lock is misused here to serialize concurrent
1589            opens of sllin channels. There are better ways, but it is
1590            the simplest one.
1591          */
1592         rtnl_lock();
1593
1594         /* Collect hanged up channels. */
1595         sll_sync();
1596
1597         sl = tty->disc_data;
1598
1599         err = -EEXIST;
1600         /* First make sure we're not already connected. */
1601         if (sl && sl->magic == SLLIN_MAGIC)
1602                 goto err_exit;
1603
1604         /* OK.  Find a free SLLIN channel to use. */
1605         err = -ENFILE;
1606         sl = sll_alloc(tty_devnum(tty));
1607         if (sl == NULL)
1608                 goto err_exit;
1609
1610         sl->tty = tty;
1611         tty->disc_data = sl;
1612         sl->line = tty_devnum(tty);
1613
1614         if (!test_bit(SLF_INUSE, &sl->flags)) {
1615                 /* Perform the low-level SLLIN initialization. */
1616                 sl->lin_master = master;
1617                 if (master)
1618                         pr_debug("sllin: Configured as MASTER\n");
1619                 else
1620                         pr_debug("sllin: Configured as SLAVE\n");
1621
1622                 sllin_reset_buffs(sl);
1623
1624                 sl->lin_baud = (baudrate == 0) ? LIN_DEFAULT_BAUDRATE : baudrate;
1625                 pr_debug("sllin: Baudrate set to %u\n", sl->lin_baud);
1626
1627                 sl->lin_state = SLSTATE_IDLE;
1628
1629                 hrtimer_init(&sl->rx_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1630                 sl->rx_timer.function = sllin_rx_timeout_handler;
1631                 /* timeval_to_ktime(msg_head->ival1); */
1632                 sl->rx_timer_timeout = ns_to_ktime(
1633                         (1000000000l / sl->lin_baud) *
1634                         SLLIN_SAMPLES_PER_CHAR * SLLIN_CHARS_TO_TIMEOUT);
1635
1636                 set_bit(SLF_INUSE, &sl->flags);
1637
1638                 init_waitqueue_head(&sl->kwt_wq);
1639                 sl->kwthread = kthread_run(sllin_kwthread, sl, "sllin");
1640                 if (sl->kwthread == NULL)
1641                         goto err_free_chan;
1642
1643                 err = register_netdevice(sl->dev);
1644                 if (err)
1645                         goto err_free_chan_and_thread;
1646
1647 #ifdef SLLIN_LED_TRIGGER
1648                 devm_sllin_led_init(sl->dev);
1649 #endif
1650         }
1651
1652         /* Done.  We have linked the TTY line to a channel. */
1653         rtnl_unlock();
1654         tty->receive_room = SLLIN_BUFF_LEN * 40; /* We don't flow control */
1655
1656         /* TTY layer expects 0 on success */
1657         return 0;
1658
1659 err_free_chan_and_thread:
1660         kthread_stop(sl->kwthread);
1661         sl->kwthread = NULL;
1662
1663 err_free_chan:
1664         sl->tty = NULL;
1665         tty->disc_data = NULL;
1666         clear_bit(SLF_INUSE, &sl->flags);
1667
1668 err_exit:
1669         rtnl_unlock();
1670
1671         /* Count references from TTY module */
1672         return err;
1673 }
1674
1675 /*
1676  * Close down a SLLIN channel.
1677  * This means flushing out any pending queues, and then returning. This
1678  * call is serialized against other ldisc functions.
1679  *
1680  * We also use this method for a hangup event.
1681  */
1682
1683 static void sllin_close(struct tty_struct *tty)
1684 {
1685         struct sllin *sl = (struct sllin *) tty->disc_data;
1686
1687         /* First make sure we're connected. */
1688         if (!sl || sl->magic != SLLIN_MAGIC || sl->tty != tty)
1689                 return;
1690
1691         kthread_stop(sl->kwthread);
1692         sl->kwthread = NULL;
1693
1694         tty->disc_data = NULL;
1695         sl->tty = NULL;
1696
1697         /* Flush network side */
1698         unregister_netdev(sl->dev);
1699         /* This will complete via sl_free_netdev */
1700 }
1701
1702 static int sllin_hangup(struct tty_struct *tty)
1703 {
1704         sllin_close(tty);
1705         return 0;
1706 }
1707
1708 /* Perform I/O control on an active SLLIN channel. */
1709 static int sllin_ioctl(struct tty_struct *tty, struct file *file,
1710                        unsigned int cmd, unsigned long arg)
1711 {
1712         struct sllin *sl = (struct sllin *) tty->disc_data;
1713         unsigned int tmp;
1714
1715         /* First make sure we're connected. */
1716         if (!sl || sl->magic != SLLIN_MAGIC)
1717                 return -EINVAL;
1718
1719         switch (cmd) {
1720         case SIOCGIFNAME:
1721                 tmp = strlen(sl->dev->name) + 1;
1722                 if (copy_to_user((void __user *)arg, sl->dev->name, tmp))
1723                         return -EFAULT;
1724                 return 0;
1725
1726         case SIOCSIFHWADDR:
1727                 return -EINVAL;
1728
1729         default:
1730                 return tty_mode_ioctl(tty, file, cmd, arg);
1731         }
1732 }
1733
1734 static struct tty_ldisc_ops sll_ldisc = {
1735         .owner          = THIS_MODULE,
1736         .magic          = TTY_LDISC_MAGIC,
1737         .name           = "sllin",
1738         .open           = sllin_open,
1739         .close          = sllin_close,
1740         .hangup         = sllin_hangup,
1741         .ioctl          = sllin_ioctl,
1742         .receive_buf    = sllin_receive_buf,
1743         .write_wakeup   = sllin_write_wakeup,
1744 };
1745
1746 static int __init sllin_init(void)
1747 {
1748         int status;
1749
1750 #ifdef SLLIN_LED_TRIGGER
1751         status = register_netdevice_notifier(&sllin_netdev_notifier);
1752         if (status)
1753                 pr_err("sllin: can't register netdevice notifier\n");
1754 #endif
1755
1756         if (maxdev < 4)
1757                 maxdev = 4; /* Sanity */
1758
1759         printk(banner);
1760         pr_debug("sllin: %d dynamic interface channels.\n", maxdev);
1761
1762         sllin_devs = kzalloc(sizeof(struct net_device *)*maxdev, GFP_KERNEL);
1763         if (!sllin_devs) {
1764                 pr_err("sllin: can't allocate sllin device array!\n");
1765                 return -ENOMEM;
1766         }
1767
1768         /* Fill in our line protocol discipline, and register it */
1769         status = tty_register_ldisc(N_SLLIN, &sll_ldisc);
1770         if (status)  {
1771                 pr_err("sllin: can't register line discipline\n");
1772                 kfree(sllin_devs);
1773         }
1774
1775 #ifdef BREAK_BY_BAUD
1776         pr_debug("sllin: Break is generated by baud-rate change.");
1777 #else
1778         pr_debug("sllin: Break is generated manually with tiny sleep.");
1779 #endif
1780
1781         return status;
1782 }
1783
1784 static void __exit sllin_exit(void)
1785 {
1786         int i;
1787         struct net_device *dev;
1788         struct sllin *sl;
1789         unsigned long timeout = jiffies + HZ;
1790         int busy = 0;
1791
1792         if (sllin_devs == NULL)
1793                 return;
1794
1795         /* First of all: check for active disciplines and hangup them.
1796          */
1797         do {
1798                 if (busy)
1799                         msleep_interruptible(100);
1800
1801                 busy = 0;
1802                 for (i = 0; i < maxdev; i++) {
1803                         dev = sllin_devs[i];
1804                         if (!dev)
1805                                 continue;
1806                         sl = netdev_priv(dev);
1807                         spin_lock_bh(&sl->lock);
1808                         if (sl->tty) {
1809                                 busy++;
1810                                 tty_hangup(sl->tty);
1811                         }
1812                         spin_unlock_bh(&sl->lock);
1813                 }
1814         } while (busy && time_before(jiffies, timeout));
1815
1816         /* FIXME: hangup is async so we should wait when doing this second
1817            phase */
1818
1819         for (i = 0; i < maxdev; i++) {
1820                 dev = sllin_devs[i];
1821                 if (!dev)
1822                         continue;
1823                 sllin_devs[i] = NULL;
1824
1825                 sl = netdev_priv(dev);
1826                 if (sl->tty) {
1827                         netdev_dbg(sl->dev, "tty discipline still running\n");
1828                         /* Intentionally leak the control block. */
1829                         dev->destructor = NULL;
1830                 }
1831
1832                 unregister_netdev(dev);
1833         }
1834
1835         kfree(sllin_devs);
1836         sllin_devs = NULL;
1837
1838         i = tty_unregister_ldisc(N_SLLIN);
1839         if (i)
1840                 pr_err("sllin: can't unregister ldisc (err %d)\n", i);
1841
1842 #ifdef SLLIN_LED_TRIGGER
1843         unregister_netdevice_notifier(&sllin_netdev_notifier);
1844 #endif
1845
1846 }
1847
1848 module_init(sllin_init);
1849 module_exit(sllin_exit);