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