]> rtime.felk.cvut.cz Git - linux-lin.git/blob - sllin/sllin.c
sllin: Added authors.
[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
46 #include <linux/module.h>
47 #include <linux/moduleparam.h>
48
49 #include <asm/system.h>
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/lin_bus.h"
67
68 /* Should be in include/linux/tty.h */
69 #define N_SLLIN                 25
70 /* -------------------------------- */
71
72 static __initdata const char banner[] =
73         KERN_INFO "sllin: serial line LIN interface driver\n";
74
75 MODULE_ALIAS_LDISC(N_SLLIN);
76 MODULE_DESCRIPTION("serial line LIN interface");
77 MODULE_LICENSE("GPL");
78 MODULE_AUTHOR("Pavel Pisa <pisa@cmp.felk.cvut.cz>");
79
80 #define SLLIN_MAGIC             0x53CA
81 /* #define BREAK_BY_BAUD */
82
83 static int maxdev = 10;         /* MAX number of SLLIN channels;
84                                    This can be overridden with
85                                    insmod sllin.ko maxdev=nnn   */
86 module_param(maxdev, int, 0);
87 MODULE_PARM_DESC(maxdev, "Maximum number of sllin interfaces");
88
89 /* maximum buffer len to store whole LIN message*/
90 #define SLLIN_DATA_MAX          8
91 #define SLLIN_BUFF_LEN          (1 /*break*/ + 1 /*sync*/ + 1 /*ID*/ + \
92                                 SLLIN_DATA_MAX + 1 /*checksum*/)
93 #define SLLIN_BUFF_BREAK        0
94 #define SLLIN_BUFF_SYNC         1
95 #define SLLIN_BUFF_ID           2
96 #define SLLIN_BUFF_DATA         3
97
98 #define SLLIN_SAMPLES_PER_CHAR  10
99 #define SLLIN_CHARS_TO_TIMEOUT  24
100
101 enum slstate {
102         SLSTATE_IDLE = 0,
103         SLSTATE_BREAK_SENT,
104         SLSTATE_ID_SENT,
105         SLSTATE_RESPONSE_WAIT, /* Wait for response */
106         SLSTATE_RESPONSE_WAIT_BUS, /* Wait for response from LIN bus
107                                 only (CAN frames from network stack
108                                 are not processed in this moment) */
109         SLSTATE_RESPONSE_SENT,
110 };
111
112 struct sllin_conf_entry {
113         int dlc;                /* Length of data in LIN frame */
114         canid_t frame_fl;       /* LIN frame flags. Passed from userspace as
115                                    canid_t data type */
116         u8 data[8];             /* LIN frame data payload */
117 };
118
119 struct sllin {
120         int                     magic;
121
122         /* Various fields. */
123         struct tty_struct       *tty;           /* ptr to TTY structure      */
124         struct net_device       *dev;           /* easy for intr handling    */
125         spinlock_t              lock;
126
127         /* LIN message buffer and actual processed data counts */
128         unsigned char           rx_buff[SLLIN_BUFF_LEN]; /* LIN Rx buffer */
129         unsigned char           tx_buff[SLLIN_BUFF_LEN]; /* LIN Tx buffer */
130         int                     rx_expect;      /* expected number of Rx chars */
131         int                     rx_lim;         /* maximum Rx chars for current frame */
132         int                     rx_cnt;         /* message buffer Rx fill level  */
133         int                     tx_lim;         /* actual limit of bytes to Tx */
134         int                     tx_cnt;         /* number of already Tx bytes */
135         char                    lin_master;     /* node is a master node */
136         int                     lin_baud;       /* LIN baudrate */
137         int                     lin_state;      /* state */
138         char                    id_to_send;     /* there is ID to be sent */
139         char                    data_to_send;   /* there are data to be sent */
140         char                    resp_len_known; /* Length of the response is known */
141
142         unsigned long           flags;          /* Flag values/ mode etc     */
143 #define SLF_INUSE               0               /* Channel in use            */
144 #define SLF_ERROR               1               /* Parity, etc. error        */
145 #define SLF_RXEVENT             2               /* Rx wake event             */
146 #define SLF_TXEVENT             3               /* Tx wake event             */
147 #define SLF_MSGEVENT            4               /* CAN message to sent       */
148 #define SLF_TMOUTEVENT          5               /* Timeout on received data  */
149
150         dev_t                   line;
151         struct task_struct      *kwthread;
152         wait_queue_head_t       kwt_wq;         /* Wait queue used by kwthread */
153         struct hrtimer          rx_timer;       /* RX timeout timer */
154         ktime_t                 rx_timer_timeout; /* RX timeout timer value */
155         struct sk_buff          *tx_req_skb;    /* Socket buffer with CAN frame
156                                                 received from network stack*/
157
158         /* List with configurations for each of 0 to LIN_ID_MAX LIN IDs */
159         struct sllin_conf_entry linfr_cache[LIN_ID_MAX + 1];
160 };
161
162 static struct net_device **sllin_devs;
163 static int sllin_configure_frame_cache(struct sllin *sl, struct can_frame *cf);
164
165
166 /* Values of two parity bits in LIN Protected
167    Identifier for each particular LIN ID */
168 const unsigned char sllin_id_parity_table[] = {
169         0x80, 0xc0, 0x40, 0x00, 0xc0, 0x80, 0x00, 0x40,
170         0x00, 0x40, 0xc0, 0x80, 0x40, 0x00, 0x80, 0xc0,
171         0x40, 0x00, 0x80, 0xc0, 0x00, 0x40, 0xc0, 0x80,
172         0xc0, 0x80, 0x00, 0x40, 0x80, 0xc0, 0x40, 0x00,
173         0x00, 0x40, 0xc0, 0x80, 0x40, 0x00, 0x80, 0xc0,
174         0x80, 0xc0, 0x40, 0x00, 0xc0, 0x80, 0x00, 0x40,
175         0xc0, 0x80, 0x00, 0x40, 0x80, 0xc0, 0x40, 0x00,
176         0x40, 0x00, 0x80, 0xc0, 0x00, 0x40, 0xc0, 0x80
177 };
178
179 /**
180  * sltty_change_speed() -- Change baudrate of Serial device belonging
181  *                         to particular @tty
182  *
183  * @tty:        Pointer to TTY to change speed for.
184  * @speed:      Integer value of new speed. It is possible to
185  *              assign non-standard values, i.e. those which
186  *              are not defined in termbits.h.
187  */
188 static int sltty_change_speed(struct tty_struct *tty, unsigned speed)
189 {
190         struct ktermios old_termios;
191         int cflag;
192
193         mutex_lock(&tty->termios_mutex);
194
195         old_termios = *(tty->termios);
196
197         cflag = CS8 | CREAD | CLOCAL | HUPCL;
198         cflag &= ~(CBAUD | CIBAUD);
199         cflag |= BOTHER;
200         tty->termios->c_cflag = cflag;
201         tty->termios->c_oflag = 0;
202         tty->termios->c_lflag = 0;
203
204         /* Enable interrupt when UART-Break or Framing error received */
205         tty->termios->c_iflag = BRKINT | INPCK;
206
207         tty_encode_baud_rate(tty, speed, speed);
208
209         if (tty->ops->set_termios)
210                 tty->ops->set_termios(tty, &old_termios);
211
212         mutex_unlock(&tty->termios_mutex);
213
214         return 0;
215 }
216
217 /* Send one can_frame to the network layer */
218 static void sllin_send_canfr(struct sllin *sl, canid_t id, char *data, int len)
219 {
220         struct sk_buff *skb;
221         struct can_frame cf;
222
223         cf.can_id = id;
224         cf.can_dlc = len;
225         if (cf.can_dlc > 0) {
226                 memcpy(&cf.data, data, cf.can_dlc);
227         }
228
229         skb = dev_alloc_skb(sizeof(struct can_frame));
230         if (!skb)
231                 return;
232
233         skb->dev = sl->dev;
234         skb->protocol = htons(ETH_P_CAN);
235         skb->pkt_type = PACKET_BROADCAST;
236         skb->ip_summed = CHECKSUM_UNNECESSARY;
237         memcpy(skb_put(skb, sizeof(struct can_frame)),
238                &cf, sizeof(struct can_frame));
239         netif_rx(skb);
240
241         sl->dev->stats.rx_packets++;
242         sl->dev->stats.rx_bytes += cf.can_dlc;
243 }
244
245 /**
246  * sll_bump() -- Send data of received LIN frame (existing in sl->rx_buff)
247  *               as CAN frame
248  *
249  * @sl:
250  */
251 static void sll_bump(struct sllin *sl)
252 {
253         sllin_send_canfr(sl, sl->rx_buff[SLLIN_BUFF_ID] & LIN_ID_MASK,
254                 sl->rx_buff + SLLIN_BUFF_DATA,
255                 sl->rx_cnt - SLLIN_BUFF_DATA - 1); /* without checksum */
256 }
257
258 /*
259  * Called by the driver when there's room for more data.  If we have
260  * more packets to send, we send them here.
261  */
262 static void sllin_write_wakeup(struct tty_struct *tty)
263 {
264         int actual;
265         int remains;
266         struct sllin *sl = (struct sllin *) tty->disc_data;
267
268         /* First make sure we're connected. */
269         if (!sl || sl->magic != SLLIN_MAGIC || !netif_running(sl->dev))
270                 return;
271
272         if (sl->lin_state != SLSTATE_BREAK_SENT)
273                 remains = sl->tx_lim - sl->tx_cnt;
274         else
275                 remains = SLLIN_BUFF_BREAK + 1 - sl->tx_cnt;
276
277         if (remains > 0) {
278                 actual = tty->ops->write(tty, sl->tx_buff + sl->tx_cnt,
279                         sl->tx_cnt - sl->tx_lim);
280                 sl->tx_cnt += actual;
281
282                 if (sl->tx_cnt < sl->tx_lim) {
283                         pr_debug("sllin: sllin_write_wakeup sent %d, "
284                                 "remains %d, waiting\n",
285                                 sl->tx_cnt, sl->tx_lim - sl->tx_cnt);
286                         return;
287                 }
288         }
289
290         clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
291         set_bit(SLF_TXEVENT, &sl->flags);
292         wake_up(&sl->kwt_wq);
293
294         pr_debug("sllin: sllin_write_wakeup sent %d, wakeup\n", sl->tx_cnt);
295 }
296
297 /**
298  * sll_xmit() -- Send a can_frame to a TTY queue.
299  *
300  * @skb: Pointer to Socket buffer to be sent.
301  * @dev: Network device where @skb will be sent.
302  */
303 static netdev_tx_t sll_xmit(struct sk_buff *skb, struct net_device *dev)
304 {
305         struct sllin *sl = netdev_priv(dev);
306         struct can_frame *cf;
307
308         if (skb->len != sizeof(struct can_frame))
309                 goto err_out;
310
311         spin_lock(&sl->lock);
312         if (!netif_running(dev))  {
313                 printk(KERN_WARNING "%s: xmit: iface is down\n", dev->name);
314                 goto err_out_unlock;
315         }
316         if (sl->tty == NULL) {
317                 goto err_out_unlock;
318         }
319
320         cf = (struct can_frame *) skb->data;
321         if (cf->can_id & LIN_CTRL_FRAME) {
322                 sllin_configure_frame_cache(sl, cf);
323                 goto free_out_unlock;
324         }
325
326         netif_stop_queue(sl->dev);
327
328         sl->tx_req_skb = skb;
329         set_bit(SLF_MSGEVENT, &sl->flags);
330         wake_up(&sl->kwt_wq);
331         spin_unlock(&sl->lock);
332         return NETDEV_TX_OK;
333
334 free_out_unlock:
335 err_out_unlock:
336         spin_unlock(&sl->lock);
337 err_out:
338         kfree_skb(skb);
339         return NETDEV_TX_OK;
340 }
341
342
343 /******************************************
344  *   Routines looking at netdevice side.
345  ******************************************/
346
347 /* Netdevice UP -> DOWN routine */
348 static int sll_close(struct net_device *dev)
349 {
350         struct sllin *sl = netdev_priv(dev);
351
352         spin_lock_bh(&sl->lock);
353         if (sl->tty) {
354                 /* TTY discipline is running. */
355                 clear_bit(TTY_DO_WRITE_WAKEUP, &sl->tty->flags);
356         }
357         netif_stop_queue(dev);
358         sl->rx_expect = 0;
359         sl->tx_lim    = 0;
360         spin_unlock_bh(&sl->lock);
361
362         return 0;
363 }
364
365 /* Netdevice DOWN -> UP routine */
366 static int sll_open(struct net_device *dev)
367 {
368         struct sllin *sl = netdev_priv(dev);
369
370         pr_debug("sllin: %s() invoked\n", __FUNCTION__);
371
372         if (sl->tty == NULL)
373                 return -ENODEV;
374
375         sl->flags &= (1 << SLF_INUSE);
376         netif_start_queue(dev);
377         return 0;
378 }
379
380 /* Hook the destructor so we can free sllin devs at the right point in time */
381 static void sll_free_netdev(struct net_device *dev)
382 {
383         int i = dev->base_addr;
384         free_netdev(dev);
385         sllin_devs[i] = NULL;
386 }
387
388 static const struct net_device_ops sll_netdev_ops = {
389         .ndo_open               = sll_open,
390         .ndo_stop               = sll_close,
391         .ndo_start_xmit         = sll_xmit,
392 };
393
394 static void sll_setup(struct net_device *dev)
395 {
396         dev->netdev_ops         = &sll_netdev_ops;
397         dev->destructor         = sll_free_netdev;
398
399         dev->hard_header_len    = 0;
400         dev->addr_len           = 0;
401         dev->tx_queue_len       = 10;
402
403         dev->mtu                = sizeof(struct can_frame);
404         dev->type               = ARPHRD_CAN;
405
406         /* New-style flags. */
407         dev->flags              = IFF_NOARP;
408         dev->features           = NETIF_F_NO_CSUM;
409 }
410
411 /******************************************
412   Routines looking at TTY side.
413  ******************************************/
414
415 static void sllin_receive_buf(struct tty_struct *tty,
416                               const unsigned char *cp, char *fp, int count)
417 {
418         struct sllin *sl = (struct sllin *) tty->disc_data;
419
420         pr_debug("sllin: sllin_receive_buf invoked\n");
421
422         if (!sl || sl->magic != SLLIN_MAGIC || !netif_running(sl->dev))
423                 return;
424
425         /* Read the characters out of the buffer */
426         while (count--) {
427                 if (fp && *fp++) {
428                         if (sl->rx_cnt > SLLIN_BUFF_BREAK) {
429                                 set_bit(SLF_ERROR, &sl->flags);
430
431                                 pr_debug("sllin: sllin_receive_buf char 0x%02x ignored "
432                                         "due marker 0x%02x, flags 0x%lx\n",
433                                         *cp, *(fp-1), sl->flags);
434
435                                 if (sl->lin_master == true) {
436                                         wake_up(&sl->kwt_wq);
437                                         return;
438                                 }
439
440                                 cp++;
441                                 continue;
442                         }
443                 }
444
445                 if (sl->rx_cnt < SLLIN_BUFF_LEN) {
446 #ifndef BREAK_BY_BAUD
447                         /* We didn't receive Break character */
448                         if ((sl->rx_cnt == SLLIN_BUFF_BREAK) && (*cp == 0x55)) {
449                                 sl->rx_buff[sl->rx_cnt++] = 0x00;
450                         }
451 #endif
452                         pr_debug("sllin: LIN_RX[%d]: 0x%02x\n", sl->rx_cnt, *cp);
453                         sl->rx_buff[sl->rx_cnt++] = *cp++;
454                 }
455         }
456
457         if (sl->rx_cnt >= sl->rx_expect) {
458                 set_bit(SLF_RXEVENT, &sl->flags);
459                 wake_up(&sl->kwt_wq);
460                 pr_debug("sllin: sllin_receive_buf count %d, wakeup\n", sl->rx_cnt);
461         } else {
462                 pr_debug("sllin: sllin_receive_buf count %d, waiting\n", sl->rx_cnt);
463         }
464 }
465
466 /*****************************************
467  *  sllin message helper routines
468  *****************************************/
469 /**
470  * sllin_report_error() -- Report an error by sending CAN frame
471  *      with particular error flag set in can_id
472  *
473  * @sl:
474  * @err: Error flag to be sent.
475  */
476 void sllin_report_error(struct sllin *sl, int err)
477 {
478         switch (err) {
479                 case LIN_ERR_CHECKSUM:
480                         sl->dev->stats.rx_crc_errors++;
481                         break;
482
483                 case LIN_ERR_RX_TIMEOUT:                
484                         sl->dev->stats.rx_errors++;
485                         break;
486
487                 case LIN_ERR_FRAMING:
488                         sl->dev->stats.rx_frame_errors++;
489                         break;
490         }
491
492         sllin_send_canfr(sl, 0 | CAN_EFF_FLAG |
493                 (err & ~LIN_ID_MASK), NULL, 0);
494 }
495
496 /**
497  * sllin_configure_frame_cache() -- Configure particular entry in linfr_cache
498  *
499  * @sl:
500  * @cf: Pointer to CAN frame sent to this driver
501  *      holding configuration information
502  */
503 static int sllin_configure_frame_cache(struct sllin *sl, struct can_frame *cf)
504 {
505         struct sllin_conf_entry *sce;
506         if (!(cf->can_id & LIN_ID_CONF))
507                 return -1;
508
509         sce = &sl->linfr_cache[cf->can_id & LIN_ID_MASK];
510         pr_debug("sllin: Setting frame cache with EFF CAN frame. "
511                 "LIN ID = %d\n", cf->can_id & LIN_ID_MASK);
512
513         sce->dlc = cf->can_dlc;
514         if (sce->dlc > SLLIN_DATA_MAX)
515                 sce->dlc = SLLIN_DATA_MAX;
516
517         sce->frame_fl = (cf->can_id & ~LIN_ID_MASK) & CAN_EFF_MASK;
518         memcpy(sce->data, cf->data, cf->can_dlc);
519
520         return 0;
521 }
522
523 /**
524  * sllin_checksum() -- Count checksum for particular data
525  *
526  * @data:        Pointer to the buffer containing whole LIN
527  *               frame (i.e. including break and sync bytes).
528  * @length:      Length of the buffer.
529  * @enhanced_fl: Flag determining whether Enhanced or Classic
530  *               checksum should be counted.
531  */
532 static inline unsigned sllin_checksum(unsigned char *data, int length, int enhanced_fl)
533 {
534         unsigned csum = 0;
535         int i;
536
537         if (enhanced_fl) {
538                 i = SLLIN_BUFF_ID;
539         } else {
540                 i = SLLIN_BUFF_DATA;
541         }
542
543         for (; i < length; i++) {
544                 csum += data[i];
545                 if (csum > 255)
546                         csum -= 255;
547         }
548
549         return ~csum & 0xff;
550 }
551
552 #define SLLIN_STPMSG_RESPONLY           (1) /* Message will be LIN Response only */
553 #define SLLIN_STPMSG_CHCKSUM_CLS        (1 << 1)
554 #define SLLIN_STPMSG_CHCKSUM_ENH        (1 << 2)
555
556 int sllin_setup_msg(struct sllin *sl, int mode, int id,
557                 unsigned char *data, int len)
558 {
559         if (id > LIN_ID_MASK)
560                 return -1;
561
562         if (!(mode & SLLIN_STPMSG_RESPONLY)) {
563                 sl->rx_cnt = 0;
564                 sl->tx_cnt = 0;
565                 sl->rx_expect = 0;
566                 sl->rx_lim = SLLIN_BUFF_LEN;
567         }
568
569         sl->tx_buff[SLLIN_BUFF_BREAK] = 0;
570         sl->tx_buff[SLLIN_BUFF_SYNC]  = 0x55;
571         sl->tx_buff[SLLIN_BUFF_ID]    = id | sllin_id_parity_table[id];
572         sl->tx_lim = SLLIN_BUFF_DATA;
573
574         if ((data != NULL) && len) {
575                 sl->tx_lim += len;
576                 memcpy(sl->tx_buff + SLLIN_BUFF_DATA, data, len);
577                 sl->tx_buff[sl->tx_lim++] = sllin_checksum(sl->tx_buff,
578                                 sl->tx_lim, mode & SLLIN_STPMSG_CHCKSUM_ENH);
579         }
580         if (len != 0)
581                 sl->rx_lim = SLLIN_BUFF_DATA + len + 1;
582
583         return 0;
584 }
585
586 static void sllin_reset_buffs(struct sllin *sl)
587 {
588         sl->rx_cnt = 0;
589         sl->rx_expect = 0;
590         sl->rx_lim = sl->lin_master ? 0 : SLLIN_BUFF_LEN;
591         sl->tx_cnt = 0;
592         sl->tx_lim = 0;
593         sl->id_to_send = false;
594         sl->data_to_send = false;
595 }
596
597 int sllin_send_tx_buff(struct sllin *sl)
598 {
599         struct tty_struct *tty = sl->tty;
600         int remains;
601         int res;
602
603 #ifdef BREAK_BY_BAUD
604         if (sl->lin_state != SLSTATE_BREAK_SENT)
605                 remains = sl->tx_lim - sl->tx_cnt;
606         else
607                 remains = 1;
608 #else
609         remains = sl->tx_lim - sl->tx_cnt;
610 #endif
611
612         res = tty->ops->write(tty, sl->tx_buff + sl->tx_cnt, remains);
613         if (res < 0)
614                 return -1;
615
616         remains -= res;
617         sl->tx_cnt += res;
618
619         if (remains > 0) {
620                 set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
621                 res = tty->ops->write(tty, sl->tx_buff + sl->tx_cnt, remains);
622                 if (res < 0) {
623                         clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
624                         return -1;
625                 }
626
627                 remains -= res;
628                 sl->tx_cnt += res;
629         }
630
631         pr_debug("sllin: sllin_send_tx_buff sent %d, remains %d\n",
632                         sl->tx_cnt, remains);
633
634         return 0;
635 }
636
637 #ifdef BREAK_BY_BAUD
638 int sllin_send_break(struct sllin *sl)
639 {
640         struct tty_struct *tty = sl->tty;
641         unsigned long break_baud;
642         int res;
643
644         break_baud = ((sl->lin_baud * 2) / 3);
645         sltty_change_speed(tty, break_baud);
646
647         tty->ops->flush_buffer(tty);
648         sl->rx_cnt = SLLIN_BUFF_BREAK;
649
650         sl->rx_expect = SLLIN_BUFF_BREAK + 1;
651         sl->lin_state = SLSTATE_BREAK_SENT;
652
653         res = sllin_send_tx_buff(sl);
654         if (res < 0) {
655                 sl->lin_state = SLSTATE_IDLE;
656                 return res;
657         }
658
659         return 0;
660 }
661 #else /* BREAK_BY_BAUD */
662
663 int sllin_send_break(struct sllin *sl)
664 {
665         struct tty_struct *tty = sl->tty;
666         int retval;
667         unsigned long break_baud;
668         unsigned long usleep_range_min;
669         unsigned long usleep_range_max;
670
671         break_baud = ((sl->lin_baud * 2) / 3);
672         sl->rx_cnt = SLLIN_BUFF_BREAK;
673         sl->rx_expect = SLLIN_BUFF_BREAK + 1;
674         sl->lin_state = SLSTATE_BREAK_SENT;
675
676         /* Do the break ourselves; Inspired by
677            http://lxr.linux.no/#linux+v3.1.2/drivers/tty/tty_io.c#L2452 */
678         retval = tty->ops->break_ctl(tty, -1);
679         if (retval)
680                 return retval;
681
682         /* udelay(712); */
683         usleep_range_min = (1000000l * SLLIN_SAMPLES_PER_CHAR) / break_baud;
684         usleep_range_max = usleep_range_min + 50;
685         usleep_range(usleep_range_min, usleep_range_max);
686
687         retval = tty->ops->break_ctl(tty, 0);
688         usleep_range_min = (1000000l * 1 /* 1 bit */) / break_baud;
689         usleep_range_max = usleep_range_min + 30;
690         usleep_range(usleep_range_min, usleep_range_max);
691
692         tty->ops->flush_buffer(tty);
693
694         sl->tx_cnt = SLLIN_BUFF_SYNC;
695
696         pr_debug("sllin: Break sent.\n");
697         set_bit(SLF_RXEVENT, &sl->flags);
698         wake_up(&sl->kwt_wq);
699
700         return 0;
701 }
702 #endif /* BREAK_BY_BAUD */
703
704
705 static enum hrtimer_restart sllin_rx_timeout_handler(struct hrtimer *hrtimer)
706 {
707         struct sllin *sl = container_of(hrtimer, struct sllin, rx_timer);
708
709         sllin_report_error(sl, LIN_ERR_RX_TIMEOUT);
710         set_bit(SLF_TMOUTEVENT, &sl->flags);
711         wake_up(&sl->kwt_wq);
712
713         return HRTIMER_NORESTART;
714 }
715
716 /**
717  * sllin_rx_validate() -- Validate received frame, i,e. check checksum
718  *
719  * @sl:
720  */
721 static int sllin_rx_validate(struct sllin *sl)
722 {
723         int actual_id;
724         int ext_chcks_fl;
725         int lin_dlc;
726         unsigned char rec_chcksm = sl->rx_buff[sl->rx_cnt - 1];
727         struct sllin_conf_entry *scf;
728
729         actual_id = sl->rx_buff[SLLIN_BUFF_ID] & LIN_ID_MASK;
730         scf = &sl->linfr_cache[actual_id];
731         lin_dlc = scf->dlc;
732         ext_chcks_fl = scf->frame_fl & LIN_CHECKSUM_EXTENDED;
733
734         if (sllin_checksum(sl->rx_buff, sl->rx_cnt - 1, ext_chcks_fl) !=
735                 rec_chcksm) {
736
737                 /* Type of checksum is configured for particular frame */
738                 if (lin_dlc > 0) {
739                         return -1;
740                 } else {
741                         if (sllin_checksum(sl->rx_buff, sl->rx_cnt - 1,
742                                 !ext_chcks_fl) != rec_chcksm) {
743                                 return -1;
744                         }
745                 }
746         }
747
748         return 0;
749 }
750
751 /*****************************************
752  *  sllin_kwthread - kernel worker thread
753  *****************************************/
754
755 int sllin_kwthread(void *ptr)
756 {
757         struct sllin *sl = (struct sllin *)ptr;
758         struct tty_struct *tty = sl->tty;
759         struct sched_param schparam = { .sched_priority = 40 };
760         int tx_bytes = 0; /* Used for Network statistics */
761
762
763         pr_debug("sllin: sllin_kwthread started.\n");
764         sched_setscheduler(current, SCHED_FIFO, &schparam);
765
766         clear_bit(SLF_ERROR, &sl->flags);
767         sltty_change_speed(tty, sl->lin_baud);
768
769         while (!kthread_should_stop()) {
770                 struct can_frame *cf;
771                 u8 *lin_data;
772                 int lin_dlc;
773                 u8 lin_data_buff[SLLIN_DATA_MAX];
774
775
776                 if ((sl->lin_state == SLSTATE_IDLE) && sl->lin_master &&
777                         sl->id_to_send) {
778                         if(sllin_send_break(sl) < 0) {
779                                 /* error processing */
780                         }
781                 }
782
783                 wait_event_killable(sl->kwt_wq, kthread_should_stop() ||
784                         test_bit(SLF_RXEVENT, &sl->flags) ||
785                         test_bit(SLF_TXEVENT, &sl->flags) ||
786                         test_bit(SLF_TMOUTEVENT, &sl->flags) ||
787                         test_bit(SLF_ERROR, &sl->flags) ||
788                         (((sl->lin_state == SLSTATE_IDLE) ||
789                                 (sl->lin_state == SLSTATE_RESPONSE_WAIT))
790                                 && test_bit(SLF_MSGEVENT, &sl->flags)));
791
792                 if (test_and_clear_bit(SLF_RXEVENT, &sl->flags)) {
793                         pr_debug("sllin: sllin_kthread RXEVENT\n");
794                 }
795
796                 if (test_and_clear_bit(SLF_ERROR, &sl->flags)) {
797                         unsigned long usleep_range_min;
798                         unsigned long usleep_range_max;
799                         hrtimer_cancel(&sl->rx_timer);
800                         pr_debug("sllin: sllin_kthread ERROR\n");
801
802                         if (sl->lin_state != SLSTATE_IDLE)
803                                 sllin_report_error(sl, LIN_ERR_FRAMING);
804
805                         usleep_range_min = (1000000l * SLLIN_SAMPLES_PER_CHAR * 10) /
806                                                 sl->lin_baud;
807                         usleep_range_max = usleep_range_min + 50;
808                         usleep_range(usleep_range_min, usleep_range_max);
809                         sllin_reset_buffs(sl);
810                         sl->lin_state = SLSTATE_IDLE;
811                 }
812
813                 if (test_and_clear_bit(SLF_TXEVENT, &sl->flags)) {
814                         pr_debug("sllin: sllin_kthread TXEVENT\n");
815                 }
816
817                 if (test_and_clear_bit(SLF_TMOUTEVENT, &sl->flags)) {
818                         pr_debug("sllin: sllin_kthread TMOUTEVENT\n");
819                         sllin_reset_buffs(sl);
820
821                         sl->lin_state = SLSTATE_IDLE;
822                 }
823
824                 switch (sl->lin_state) {
825                         case SLSTATE_IDLE:
826                                 if (!test_bit(SLF_MSGEVENT, &sl->flags))
827                                         break;
828
829                                 cf = (struct can_frame *)sl->tx_req_skb->data;
830
831                                 /* SFF RTR CAN frame -> LIN header */
832                                 if (cf->can_id & CAN_RTR_FLAG) {
833                                         spin_lock(&sl->lock);
834                                         pr_debug("sllin: %s: RTR SFF CAN frame, ID = %x\n",
835                                                 __FUNCTION__, cf->can_id & LIN_ID_MASK);
836
837                                         /* Is there Slave response in linfr_cache to be sent? */
838                                         if ((sl->linfr_cache[cf->can_id & LIN_ID_MASK].frame_fl &
839                                                 LIN_LOC_SLAVE_CACHE)
840                                                 && (sl->linfr_cache[cf->can_id & LIN_ID_MASK].dlc > 0)) {
841
842                                                 pr_debug("sllin: Sending LIN response from linfr_cache\n");
843                                                 lin_data = sl->linfr_cache[cf->can_id & LIN_ID_MASK].data;
844                                                 lin_dlc = sl->linfr_cache[cf->can_id & LIN_ID_MASK].dlc;
845                                                 if (lin_dlc > SLLIN_DATA_MAX)
846                                                         lin_dlc = SLLIN_DATA_MAX;
847                                                 memcpy(lin_data_buff, lin_data, lin_dlc);
848                                                 lin_data = lin_data_buff;
849                                         } else {
850                                                 lin_data = NULL;
851                                                 lin_dlc = sl->linfr_cache[cf->can_id & LIN_ID_MASK].dlc;
852                                         }
853                                         spin_unlock(&sl->lock);
854                                 } else { /* SFF NON-RTR CAN frame -> LIN header + LIN response */
855                                         pr_debug("sllin: %s: NON-RTR SFF CAN frame, ID = %x\n",
856                                                 __FUNCTION__, (int)cf->can_id & LIN_ID_MASK);
857
858                                         lin_data = cf->data;
859                                         lin_dlc = cf->can_dlc;
860                                         if (lin_dlc > SLLIN_DATA_MAX)
861                                                 lin_dlc = SLLIN_DATA_MAX;
862                                         tx_bytes = lin_dlc;
863                                 }
864
865                                 if (sllin_setup_msg(sl, 0, cf->can_id & LIN_ID_MASK,
866                                         lin_data, lin_dlc) != -1) {
867
868                                         sl->id_to_send = true;
869                                         sl->data_to_send = (lin_data != NULL) ? true : false;
870                                         sl->resp_len_known = (lin_dlc > 0) ? true : false;
871                                         sl->dev->stats.tx_packets++;
872                                         sl->dev->stats.tx_bytes += tx_bytes;
873                                 }
874
875                                 clear_bit(SLF_MSGEVENT, &sl->flags);
876                                 kfree_skb(sl->tx_req_skb);
877                                 netif_wake_queue(sl->dev);
878                                 hrtimer_start(&sl->rx_timer,
879                                         ktime_add(ktime_get(), sl->rx_timer_timeout),
880                                         HRTIMER_MODE_ABS);
881                                 break;
882
883                         case SLSTATE_BREAK_SENT:
884 #ifdef BREAK_BY_BAUD
885                                 if (sl->rx_cnt <= SLLIN_BUFF_BREAK)
886                                         continue;
887
888                                 res = sltty_change_speed(tty, sl->lin_baud);
889 #endif
890
891                                 sl->lin_state = SLSTATE_ID_SENT;
892                                 sllin_send_tx_buff(sl);
893                                 break;
894
895                         case SLSTATE_ID_SENT:
896                                 hrtimer_cancel(&sl->rx_timer);
897                                 sl->id_to_send = false;
898                                 if (sl->data_to_send) {
899                                         sllin_send_tx_buff(sl);
900                                         sl->lin_state = SLSTATE_RESPONSE_SENT;
901                                         sl->rx_expect = sl->tx_lim;
902                                         goto slstate_response_sent;
903                                 } else {
904                                         if (sl->resp_len_known) {
905                                                 sl->rx_expect = sl->rx_lim;
906                                         } else {
907                                                 sl->rx_expect = SLLIN_BUFF_DATA + 2;
908                                         }
909                                         sl->lin_state = SLSTATE_RESPONSE_WAIT;
910                                         /* If we don't receive anything, timer will "unblock" us */
911                                         hrtimer_start(&sl->rx_timer,
912                                                 ktime_add(ktime_get(), sl->rx_timer_timeout),
913                                                 HRTIMER_MODE_ABS);
914                                         goto slstate_response_wait;
915                                 }
916                                 break;
917
918                         case SLSTATE_RESPONSE_WAIT:
919                         slstate_response_wait:
920                                 if (test_bit(SLF_MSGEVENT, &sl->flags)) {
921                                         unsigned char *lin_buff;
922                                         cf = (struct can_frame *)sl->tx_req_skb->data;
923
924                                         lin_buff = (sl->lin_master) ? sl->tx_buff : sl->rx_buff;
925                                         if (cf->can_id == (lin_buff[SLLIN_BUFF_ID] & LIN_ID_MASK)) {
926                                                 hrtimer_cancel(&sl->rx_timer);
927                                                 pr_debug("sllin: received LIN response in a CAN frame.\n");
928                                                 if (sllin_setup_msg(sl, SLLIN_STPMSG_RESPONLY,
929                                                         cf->can_id & LIN_ID_MASK,
930                                                         cf->data, cf->can_dlc) != -1) {
931
932                                                         sl->rx_expect = sl->tx_lim;
933                                                         sl->data_to_send = true;
934                                                         sl->dev->stats.tx_packets++;
935                                                         sl->dev->stats.tx_bytes += tx_bytes;
936
937                                                         if (!sl->lin_master) {
938                                                                 sl->tx_cnt = SLLIN_BUFF_DATA;
939                                                         }
940
941                                                         sllin_send_tx_buff(sl);
942                                                         clear_bit(SLF_MSGEVENT, &sl->flags);
943                                                         kfree_skb(sl->tx_req_skb);
944                                                         netif_wake_queue(sl->dev);
945
946                                                         sl->lin_state = SLSTATE_RESPONSE_SENT;
947                                                         goto slstate_response_sent;
948                                                 }
949                                         } else {
950                                                 sl->lin_state = SLSTATE_RESPONSE_WAIT_BUS;
951                                         }
952                                 }
953
954                                 /* Be aware, no BREAK here */
955                         case SLSTATE_RESPONSE_WAIT_BUS:
956                                 if (sl->rx_cnt < sl->rx_expect)
957                                         continue;
958
959                                 hrtimer_cancel(&sl->rx_timer);
960                                 pr_debug("sllin: response received ID %d len %d\n",
961                                         sl->rx_buff[SLLIN_BUFF_ID], sl->rx_cnt - SLLIN_BUFF_DATA - 1);
962
963                                 if (sllin_rx_validate(sl) == -1) {
964                                         pr_debug("sllin: RX validation failed.\n");
965                                         sllin_report_error(sl, LIN_ERR_CHECKSUM);
966                                 } else {
967                                         /* Send CAN non-RTR frame with data */
968                                         pr_debug("sllin: sending NON-RTR CAN"
969                                                 "frame with LIN payload.");
970                                         sll_bump(sl); /* send packet to the network layer */
971                                 }
972
973                                 sl->id_to_send = false;
974                                 sl->lin_state = SLSTATE_IDLE;
975                                 break;
976
977                         case SLSTATE_RESPONSE_SENT:
978                         slstate_response_sent:
979                                 if (sl->rx_cnt < sl->tx_lim)
980                                         continue;
981
982                                 hrtimer_cancel(&sl->rx_timer);
983                                 sll_bump(sl); /* send packet to the network layer */
984                                 pr_debug("sllin: response sent ID %d len %d\n",
985                                         sl->rx_buff[SLLIN_BUFF_ID], sl->rx_cnt - SLLIN_BUFF_DATA - 1);
986
987                                 sl->id_to_send = false;
988                                 sl->lin_state = SLSTATE_IDLE;
989                                 break;
990                 }
991         }
992
993         hrtimer_cancel(&sl->rx_timer);
994         pr_debug("sllin: sllin_kwthread stopped.\n");
995
996         return 0;
997 }
998
999
1000 /************************************
1001  *  sllin_open helper routines.
1002  ************************************/
1003
1004 /* Collect hanged up channels */
1005 static void sll_sync(void)
1006 {
1007         int i;
1008         struct net_device *dev;
1009         struct sllin      *sl;
1010
1011         for (i = 0; i < maxdev; i++) {
1012                 dev = sllin_devs[i];
1013                 if (dev == NULL)
1014                         break;
1015
1016                 sl = netdev_priv(dev);
1017                 if (sl->tty)
1018                         continue;
1019                 if (dev->flags & IFF_UP)
1020                         dev_close(dev);
1021         }
1022 }
1023
1024 /* Find a free SLLIN channel, and link in this `tty' line. */
1025 static struct sllin *sll_alloc(dev_t line)
1026 {
1027         int i;
1028         struct net_device *dev = NULL;
1029         struct sllin       *sl;
1030
1031         if (sllin_devs == NULL)
1032                 return NULL;    /* Master array missing ! */
1033
1034         for (i = 0; i < maxdev; i++) {
1035                 dev = sllin_devs[i];
1036                 if (dev == NULL)
1037                         break;
1038
1039         }
1040
1041         /* Sorry, too many, all slots in use */
1042         if (i >= maxdev)
1043                 return NULL;
1044
1045         if (dev) {
1046                 sl = netdev_priv(dev);
1047                 if (test_bit(SLF_INUSE, &sl->flags)) {
1048                         unregister_netdevice(dev);
1049                         dev = NULL;
1050                         sllin_devs[i] = NULL;
1051                 }
1052         }
1053
1054         if (!dev) {
1055                 char name[IFNAMSIZ];
1056                 sprintf(name, "sllin%d", i);
1057
1058                 dev = alloc_netdev(sizeof(*sl), name, sll_setup);
1059                 if (!dev)
1060                         return NULL;
1061                 dev->base_addr  = i;
1062         }
1063
1064         sl = netdev_priv(dev);
1065         /* Initialize channel control data */
1066         sl->magic = SLLIN_MAGIC;
1067         sl->dev = dev;
1068         spin_lock_init(&sl->lock);
1069         sllin_devs[i] = dev;
1070
1071         return sl;
1072 }
1073
1074 /*
1075  * Open the high-level part of the SLLIN channel.
1076  * This function is called by the TTY module when the
1077  * SLLIN line discipline is called for.  Because we are
1078  * sure the tty line exists, we only have to link it to
1079  * a free SLLIN channel...
1080  *
1081  * Called in process context serialized from other ldisc calls.
1082  */
1083
1084 static int sllin_open(struct tty_struct *tty)
1085 {
1086         struct sllin *sl;
1087         int err;
1088         pr_debug("sllin: %s() invoked\n", __FUNCTION__);
1089
1090         if (!capable(CAP_NET_ADMIN))
1091                 return -EPERM;
1092
1093         if (tty->ops->write == NULL)
1094                 return -EOPNOTSUPP;
1095
1096         /* RTnetlink lock is misused here to serialize concurrent
1097            opens of sllin channels. There are better ways, but it is
1098            the simplest one.
1099          */
1100         rtnl_lock();
1101
1102         /* Collect hanged up channels. */
1103         sll_sync();
1104
1105         sl = tty->disc_data;
1106
1107         err = -EEXIST;
1108         /* First make sure we're not already connected. */
1109         if (sl && sl->magic == SLLIN_MAGIC)
1110                 goto err_exit;
1111
1112         /* OK.  Find a free SLLIN channel to use. */
1113         err = -ENFILE;
1114         sl = sll_alloc(tty_devnum(tty));
1115         if (sl == NULL)
1116                 goto err_exit;
1117
1118         sl->tty = tty;
1119         tty->disc_data = sl;
1120         sl->line = tty_devnum(tty);
1121
1122         if (!test_bit(SLF_INUSE, &sl->flags)) {
1123                 /* Perform the low-level SLLIN initialization. */
1124                 sl->lin_master = true;
1125
1126                 sllin_reset_buffs(sl);
1127
1128                 sl->lin_baud  = 19200;
1129
1130                 sl->lin_state = SLSTATE_IDLE;
1131
1132                 hrtimer_init(&sl->rx_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1133                 sl->rx_timer.function = sllin_rx_timeout_handler;
1134                 /* timeval_to_ktime(msg_head->ival1); */
1135                 sl->rx_timer_timeout = ns_to_ktime(
1136                         (1000000000l / sl->lin_baud) *
1137                         SLLIN_SAMPLES_PER_CHAR * SLLIN_CHARS_TO_TIMEOUT);
1138
1139                 set_bit(SLF_INUSE, &sl->flags);
1140
1141                 init_waitqueue_head(&sl->kwt_wq);
1142                 sl->kwthread = kthread_run(sllin_kwthread, sl, "sllin");
1143                 if (sl->kwthread == NULL)
1144                         goto err_free_chan;
1145
1146                 err = register_netdevice(sl->dev);
1147                 if (err)
1148                         goto err_free_chan_and_thread;
1149         }
1150
1151         /* Done.  We have linked the TTY line to a channel. */
1152         rtnl_unlock();
1153         tty->receive_room = SLLIN_BUFF_LEN * 40; /* We don't flow control */
1154
1155         /* TTY layer expects 0 on success */
1156         return 0;
1157
1158 err_free_chan_and_thread:
1159         kthread_stop(sl->kwthread);
1160         sl->kwthread = NULL;
1161
1162 err_free_chan:
1163         sl->tty = NULL;
1164         tty->disc_data = NULL;
1165         clear_bit(SLF_INUSE, &sl->flags);
1166
1167 err_exit:
1168         rtnl_unlock();
1169
1170         /* Count references from TTY module */
1171         return err;
1172 }
1173
1174 /*
1175  * Close down a SLLIN channel.
1176  * This means flushing out any pending queues, and then returning. This
1177  * call is serialized against other ldisc functions.
1178  *
1179  * We also use this method for a hangup event.
1180  */
1181
1182 static void sllin_close(struct tty_struct *tty)
1183 {
1184         struct sllin *sl = (struct sllin *) tty->disc_data;
1185
1186         /* First make sure we're connected. */
1187         if (!sl || sl->magic != SLLIN_MAGIC || sl->tty != tty)
1188                 return;
1189
1190         kthread_stop(sl->kwthread);
1191         sl->kwthread = NULL;
1192
1193         tty->disc_data = NULL;
1194         sl->tty = NULL;
1195
1196         /* Flush network side */
1197         unregister_netdev(sl->dev);
1198         /* This will complete via sl_free_netdev */
1199 }
1200
1201 static int sllin_hangup(struct tty_struct *tty)
1202 {
1203         sllin_close(tty);
1204         return 0;
1205 }
1206
1207 /* Perform I/O control on an active SLLIN channel. */
1208 static int sllin_ioctl(struct tty_struct *tty, struct file *file,
1209                        unsigned int cmd, unsigned long arg)
1210 {
1211         struct sllin *sl = (struct sllin *) tty->disc_data;
1212         unsigned int tmp;
1213
1214         /* First make sure we're connected. */
1215         if (!sl || sl->magic != SLLIN_MAGIC)
1216                 return -EINVAL;
1217
1218         switch (cmd) {
1219         case SIOCGIFNAME:
1220                 tmp = strlen(sl->dev->name) + 1;
1221                 if (copy_to_user((void __user *)arg, sl->dev->name, tmp))
1222                         return -EFAULT;
1223                 return 0;
1224
1225         case SIOCSIFHWADDR:
1226                 return -EINVAL;
1227
1228         default:
1229                 return tty_mode_ioctl(tty, file, cmd, arg);
1230         }
1231 }
1232
1233 static struct tty_ldisc_ops sll_ldisc = {
1234         .owner          = THIS_MODULE,
1235         .magic          = TTY_LDISC_MAGIC,
1236         .name           = "sllin",
1237         .open           = sllin_open,
1238         .close          = sllin_close,
1239         .hangup         = sllin_hangup,
1240         .ioctl          = sllin_ioctl,
1241         .receive_buf    = sllin_receive_buf,
1242         .write_wakeup   = sllin_write_wakeup,
1243 };
1244
1245 static int __init sllin_init(void)
1246 {
1247         int status;
1248
1249         if (maxdev < 4)
1250                 maxdev = 4; /* Sanity */
1251
1252         printk(banner);
1253         pr_debug("sllin: %d dynamic interface channels.\n", maxdev);
1254
1255         sllin_devs = kzalloc(sizeof(struct net_device *)*maxdev, GFP_KERNEL);
1256         if (!sllin_devs) {
1257                 printk(KERN_ERR "sllin: can't allocate sllin device array!\n");
1258                 return -ENOMEM;
1259         }
1260
1261         /* Fill in our line protocol discipline, and register it */
1262         status = tty_register_ldisc(N_SLLIN, &sll_ldisc);
1263         if (status)  {
1264                 printk(KERN_ERR "sllin: can't register line discipline\n");
1265                 kfree(sllin_devs);
1266         }
1267
1268 #ifdef BREAK_BY_BAUD
1269         pr_debug("sllin: Break is generated by baud-rate change.");
1270 #else
1271         pr_debug("sllin: Break is generated manually with tiny sleep.");
1272 #endif
1273
1274         return status;
1275 }
1276
1277 static void __exit sllin_exit(void)
1278 {
1279         int i;
1280         struct net_device *dev;
1281         struct sllin *sl;
1282         unsigned long timeout = jiffies + HZ;
1283         int busy = 0;
1284
1285         if (sllin_devs == NULL)
1286                 return;
1287
1288         /* First of all: check for active disciplines and hangup them.
1289          */
1290         do {
1291                 if (busy)
1292                         msleep_interruptible(100);
1293
1294                 busy = 0;
1295                 for (i = 0; i < maxdev; i++) {
1296                         dev = sllin_devs[i];
1297                         if (!dev)
1298                                 continue;
1299                         sl = netdev_priv(dev);
1300                         spin_lock_bh(&sl->lock);
1301                         if (sl->tty) {
1302                                 busy++;
1303                                 tty_hangup(sl->tty);
1304                         }
1305                         spin_unlock_bh(&sl->lock);
1306                 }
1307         } while (busy && time_before(jiffies, timeout));
1308
1309         /* FIXME: hangup is async so we should wait when doing this second
1310            phase */
1311
1312         for (i = 0; i < maxdev; i++) {
1313                 dev = sllin_devs[i];
1314                 if (!dev)
1315                         continue;
1316                 sllin_devs[i] = NULL;
1317
1318                 sl = netdev_priv(dev);
1319                 if (sl->tty) {
1320                         printk(KERN_ERR "%s: tty discipline still running\n",
1321                                dev->name);
1322                         /* Intentionally leak the control block. */
1323                         dev->destructor = NULL;
1324                 }
1325
1326                 unregister_netdev(dev);
1327         }
1328
1329         kfree(sllin_devs);
1330         sllin_devs = NULL;
1331
1332         i = tty_unregister_ldisc(N_SLLIN);
1333         if (i)
1334                 printk(KERN_ERR "sllin: can't unregister ldisc (err %d)\n", i);
1335 }
1336
1337 module_init(sllin_init);
1338 module_exit(sllin_exit);