2 * sllin.c - serial line LIN interface driver (using tty line discipline)
4 * This file is derived from linux/drivers/net/slip.c
6 * slip.c Authors : Laurence Culhane <loz@holmes.demon.co.uk>
7 * Fred N. van Kempen <waltje@uwalt.nl.mugnet.org>
8 * sllin.c Author : Oliver Hartkopp <socketcan@hartkopp.net>
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License as published by the
12 * Free Software Foundation; either version 2 of the License, or (at your
13 * option) any later version.
15 * This program is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 59 Temple Place, Suite 330, Boston, MA 02111-1307. You can also get it
23 * at http://www.gnu.org/licenses/gpl.html
25 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
38 * Send feedback to <socketcan-users@lists.berlios.de>
42 #include <linux/module.h>
43 #include <linux/moduleparam.h>
45 #include <asm/system.h>
46 #include <linux/uaccess.h>
47 #include <linux/bitops.h>
48 #include <linux/string.h>
49 #include <linux/tty.h>
50 #include <linux/errno.h>
51 #include <linux/netdevice.h>
52 #include <linux/skbuff.h>
53 #include <linux/rtnetlink.h>
54 #include <linux/if_arp.h>
55 #include <linux/if_ether.h>
56 #include <linux/sched.h>
57 #include <linux/delay.h>
58 #include <linux/init.h>
59 #include <linux/can.h>
60 #include <linux/kthread.h>
61 #include <linux/hrtimer.h>
63 /* Should be in include/linux/tty.h */
66 static __initdata const char banner[] =
67 KERN_INFO "sllin: serial line LIN interface driver\n";
69 MODULE_ALIAS_LDISC(N_SLLIN);
70 MODULE_DESCRIPTION("serial line LIN interface");
71 MODULE_LICENSE("GPL");
72 MODULE_AUTHOR("Oliver Hartkopp <socketcan@hartkopp.net>");
74 #define SLLIN_MAGIC 0x53CA
75 // #define BREAK_BY_BAUD
77 static int maxdev = 10; /* MAX number of SLLIN channels;
78 This can be overridden with
79 insmod sllin.ko maxdev=nnn */
80 module_param(maxdev, int, 0);
81 MODULE_PARM_DESC(maxdev, "Maximum number of sllin interfaces");
83 /* maximum buffer len to store whole LIN message*/
84 #define SLLIN_DATA_MAX 8
85 #define SLLIN_BUFF_LEN (1 /*break*/ + 1 /*sync*/ + 1 /*ID*/ + \
86 SLLIN_DATA_MAX + 1 /*checksum*/)
87 #define SLLIN_BUFF_BREAK 0
88 #define SLLIN_BUFF_SYNC 1
89 #define SLLIN_BUFF_ID 2
90 #define SLLIN_BUFF_DATA 3
92 #define SLLIN_ID_MASK 0x3f
93 #define SLLIN_ID_MAX SLLIN_ID_MASK
99 SLSTATE_RESPONSE_WAIT,
100 SLSTATE_RESPONSE_SENT,
103 struct sllin_conf_entry {
104 int dlc; /* Length of data in LIN frame */
105 #define SLLIN_CANFR_FLAGS_OFFS 6 /* Lower 6 bits in can_id correspond to LIN ID */
106 /* Save configuration for particualr LIN ID */
107 #define SLLIN_LIN_ID_CONF (1 << SLLIN_CANFR_FLAGS_OFFS)
108 /* Publisher of particular LIN response is SLLIN Master */
109 #define SLLIN_SRC_MASTER (1 << (SLLIN_CANFR_FLAGS_OFFS + 1))
110 #define SLLIN_SRC_SLAVE (1 << (SLLIN_CANFR_FLAGS_OFFS + 2))
111 #define SLLIN_SLAVE_LOCAL (1 << (SLLIN_CANFR_FLAGS_OFFS + 3))
112 #define SLLIN_SLAVE_REMOTE (1 << (SLLIN_CANFR_FLAGS_OFFS + 4))
113 #define SLLIN_LOC_SLAVE_CACHE (1 << (SLLIN_CANFR_FLAGS_OFFS + 5))
114 canid_t frame_fl; /* LIN frame flags. Passed from userspace as canid_t data type */
115 u8 data[8]; /* LIN frame data payload */
121 /* Various fields. */
122 struct tty_struct *tty; /* ptr to TTY structure */
123 struct net_device *dev; /* easy for intr handling */
126 /* LIN message buffer and actual processed data counts */
127 unsigned char rx_buff[SLLIN_BUFF_LEN]; /* LIN Rx buffer */
128 unsigned char tx_buff[SLLIN_BUFF_LEN]; /* LIN Tx buffer */
129 int rx_expect; /* expected number of Rx chars */
130 int rx_lim; /* maximum Rx chars for current frame */
131 int rx_cnt; /* message buffer Rx fill level */
132 int tx_lim; /* actual limit of bytes to Tx */
133 int tx_cnt; /* number of already Tx bytes */
134 char lin_master; /* node is a master node */
135 int lin_baud; /* LIN baudrate */
136 int lin_state; /* state */
137 char id_to_send; /* there is ID to be sent */
138 char data_to_send; /* there are data to be sent */
140 unsigned long flags; /* Flag values/ mode etc */
141 #define SLF_INUSE 0 /* Channel in use */
142 #define SLF_ERROR 1 /* Parity, etc. error */
143 #define SLF_RXEVENT 2 /* Rx wake event */
144 #define SLF_TXEVENT 3 /* Tx wake event */
145 #define SLF_MSGEVENT 4 /* CAN message to sent */
146 #define SLF_TMOUTEVENT 5 /* Timeout on received data */
149 struct task_struct *kwthread;
150 wait_queue_head_t kwt_wq;
151 struct hrtimer rx_timer; /* RX timeout timer */
152 ktime_t rx_timer_timeout; /* RX timeout timer value */
153 struct sk_buff *rec_skb; /* Socket buffer with received CAN frame */
155 struct sllin_conf_entry linfr_cache[SLLIN_ID_MAX + 1]; /* List with configurations for
156 each of 0 to SLLIN_ID_MAX LIN IDs */
159 static struct net_device **sllin_devs;
161 const unsigned char sllin_id_parity_table[] = {
162 0x80,0xc0,0x40,0x00,0xc0,0x80,0x00,0x40,
163 0x00,0x40,0xc0,0x80,0x40,0x00,0x80,0xc0,
164 0x40,0x00,0x80,0xc0,0x00,0x40,0xc0,0x80,
165 0xc0,0x80,0x00,0x40,0x80,0xc0,0x40,0x00,
166 0x00,0x40,0xc0,0x80,0x40,0x00,0x80,0xc0,
167 0x80,0xc0,0x40,0x00,0xc0,0x80,0x00,0x40,
168 0xc0,0x80,0x00,0x40,0x80,0xc0,0x40,0x00,
169 0x40,0x00,0x80,0xc0,0x00,0x40,0xc0,0x80
172 static int sltty_change_speed(struct tty_struct *tty, unsigned speed)
174 struct ktermios old_termios;
177 mutex_lock(&tty->termios_mutex);
179 old_termios = *(tty->termios);
180 cflag = tty->termios->c_cflag;
181 cflag &= ~(CBAUD | CIBAUD);
183 tty->termios->c_cflag = cflag;
185 tty_encode_baud_rate(tty, speed, speed);
187 if (tty->ops->set_termios)
188 tty->ops->set_termios(tty, &old_termios);
190 mutex_unlock(&tty->termios_mutex);
196 /* Send one completely decapsulated can_frame to the network layer */
197 static void sll_bump(struct sllin *sl)
202 cf.can_id = sl->rx_buff[SLLIN_BUFF_ID] & SLLIN_ID_MASK;
203 cf.can_dlc = sl->rx_cnt - SLLIN_BUFF_DATA;
204 memcpy(&cf.data, sl->rx_buff + SLLIN_BUFF_DATA, cf.can_dlc);
206 skb = dev_alloc_skb(sizeof(struct can_frame));
211 skb->protocol = htons(ETH_P_CAN);
212 skb->pkt_type = PACKET_BROADCAST;
213 skb->ip_summed = CHECKSUM_UNNECESSARY;
214 memcpy(skb_put(skb, sizeof(struct can_frame)),
215 &cf, sizeof(struct can_frame));
218 sl->dev->stats.rx_packets++;
219 sl->dev->stats.rx_bytes += cf.can_dlc;
223 /************************************************************************
224 * STANDARD SLLIN ENCAPSULATION *
225 ************************************************************************/
227 /* Convert particular CAN frame into LIN frame and send it to TTY queue. */
228 static void sll_encaps(struct sllin *sl, struct can_frame *cf)
230 // int actual, idx, i;
231 // char lframe[16] = {0x00, 0x55}; /* Fake break, Sync byte */
232 // struct tty_struct *tty = sl->tty;
234 // pr_debug("sllin: %s() invoked\n", __FUNCTION__);
236 // /* We do care only about SFF frames */
237 // if (cf->can_id & CAN_EFF_FLAG)
240 // /* Send only header */
241 // if (cf->can_id & CAN_RTR_FLAG) {
242 // pr_debug("sllin: %s() RTR CAN frame\n", __FUNCTION__);
243 // lframe[2] = (u8)cf->can_id; /* Get one byte LIN ID */
245 // sltty_change_speed(tty, sl->lin_baud * 2 / 3);
246 // tty->ops->write(tty, &lframe[0], 1);
247 // sltty_change_speed(tty, sl->lin_baud);
248 // tty->ops->write(tty, &lframe[1], 1);
249 // tty->ops->write(tty, &lframe[2], 1);
251 // pr_debug("sllin: %s() non-RTR CAN frame\n", __FUNCTION__);
252 // /* idx = strlen(sl->xbuff);
254 // for (i = 0; i < cf->can_dlc; i++)
255 // sprintf(&sl->xbuff[idx + 2*i], "%02X", cf->data[i]);
257 // * Order of next two lines is *very* important.
258 // * When we are sending a little amount of data,
259 // * the transfer may be completed inside the ops->write()
260 // * routine, because it's running with interrupts enabled.
261 // * In this case we *never* got WRITE_WAKEUP event,
262 // * if we did not request it before write operation.
263 // * 14 Oct 1994 Dmitry Gorodchanin.
265 // set_bit(TTY_DO_WRITE_WAKEUP, &sl->tty->flags);
266 // actual = sl->tty->ops->write(sl->tty, sl->xbuff, strlen(sl->xbuff));
267 // sl->xleft = strlen(sl->xbuff) - actual;
268 // sl->xhead = sl->xbuff + actual;
269 // sl->dev->stats.tx_bytes += cf->can_dlc;
276 * Called by the driver when there's room for more data. If we have
277 * more packets to send, we send them here.
279 static void sllin_write_wakeup(struct tty_struct *tty)
283 struct sllin *sl = (struct sllin *) tty->disc_data;
285 /* First make sure we're connected. */
286 if (!sl || sl->magic != SLLIN_MAGIC || !netif_running(sl->dev))
289 if (sl->lin_state != SLSTATE_BREAK_SENT)
290 remains = sl->tx_lim - sl->tx_cnt;
292 remains = SLLIN_BUFF_BREAK + 1 - sl->tx_cnt;
295 actual = tty->ops->write(tty, sl->tx_buff + sl->tx_cnt, sl->tx_cnt - sl->tx_lim);
296 sl->tx_cnt += actual;
298 if (sl->tx_cnt < sl->tx_lim) {
299 printk(KERN_INFO "sllin_write_wakeup sent %d, remains %d, waiting\n",
300 sl->tx_cnt, sl->tx_lim - sl->tx_cnt);
305 clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
306 set_bit(SLF_TXEVENT, &sl->flags);
307 wake_up(&sl->kwt_wq);
309 printk(KERN_INFO "sllin_write_wakeup sent %d, wakeup\n", sl->tx_cnt);
312 /* Send a can_frame to a TTY queue. */
313 static netdev_tx_t sll_xmit(struct sk_buff *skb, struct net_device *dev)
315 struct sllin *sl = netdev_priv(dev);
317 if (skb->len != sizeof(struct can_frame))
320 spin_lock(&sl->lock);
321 if (!netif_running(dev)) {
322 spin_unlock(&sl->lock);
323 printk(KERN_WARNING "%s: xmit: iface is down\n", dev->name);
326 if (sl->tty == NULL) {
327 spin_unlock(&sl->lock);
331 netif_stop_queue(sl->dev);
334 set_bit(SLF_MSGEVENT, &sl->flags);
335 wake_up(&sl->kwt_wq);
336 spin_unlock(&sl->lock);
345 /******************************************
346 * Routines looking at netdevice side.
347 ******************************************/
349 /* Netdevice UP -> DOWN routine */
350 static int sll_close(struct net_device *dev)
352 struct sllin *sl = netdev_priv(dev);
354 spin_lock_bh(&sl->lock);
356 /* TTY discipline is running. */
357 clear_bit(TTY_DO_WRITE_WAKEUP, &sl->tty->flags);
359 netif_stop_queue(dev);
362 spin_unlock_bh(&sl->lock);
367 /* Netdevice DOWN -> UP routine */
368 static int sll_open(struct net_device *dev)
370 struct sllin *sl = netdev_priv(dev);
372 pr_debug("sllin: %s() invoked\n", __FUNCTION__);
377 sl->flags &= (1 << SLF_INUSE);
378 netif_start_queue(dev);
382 /* Hook the destructor so we can free sllin devs at the right point in time */
383 static void sll_free_netdev(struct net_device *dev)
385 int i = dev->base_addr;
387 sllin_devs[i] = NULL;
390 static const struct net_device_ops sll_netdev_ops = {
391 .ndo_open = sll_open,
392 .ndo_stop = sll_close,
393 .ndo_start_xmit = sll_xmit,
396 static void sll_setup(struct net_device *dev)
398 dev->netdev_ops = &sll_netdev_ops;
399 dev->destructor = sll_free_netdev;
401 dev->hard_header_len = 0;
403 dev->tx_queue_len = 10;
405 dev->mtu = sizeof(struct can_frame);
406 dev->type = ARPHRD_CAN;
408 /* New-style flags. */
409 dev->flags = IFF_NOARP;
410 dev->features = NETIF_F_NO_CSUM;
413 /******************************************
414 Routines looking at TTY side.
415 ******************************************/
418 * Handle the 'receiver data ready' interrupt.
419 * This function is called by the 'tty_io' module in the kernel when
420 * a block of SLLIN data has been received, which can now be decapsulated
421 * and sent on to some IP layer for further processing. This will not
422 * be re-entered while running but other ldisc functions may be called
426 static void sllin_receive_buf(struct tty_struct *tty,
427 const unsigned char *cp, char *fp, int count)
429 struct sllin *sl = (struct sllin *) tty->disc_data;
431 printk(KERN_INFO "sllin_receive_buf invoked\n");
433 if (!sl || sl->magic != SLLIN_MAGIC || !netif_running(sl->dev))
436 /* Read the characters out of the buffer */
439 if (!test_and_set_bit(SLF_ERROR, &sl->flags))
440 sl->dev->stats.rx_errors++;
441 printk(KERN_INFO "sllin_receive_buf char 0x%02x ignored "
442 "due marker 0x%02x, flags 0x%lx\n",
443 *cp, *(fp-1), sl->flags);
448 if (sl->rx_cnt < SLLIN_BUFF_LEN) {
449 #ifndef BREAK_BY_BAUD
450 /* We didn't receive Break character */
451 if ((sl->rx_cnt == SLLIN_BUFF_BREAK) && (*cp == 0x55)) {
452 sl->rx_buff[sl->rx_cnt++] = 0x00;
455 printk(KERN_INFO "LIN_RX[%d]: 0x%02x\n", sl->rx_cnt, *cp);
456 sl->rx_buff[sl->rx_cnt++] = *cp++;
460 if (sl->rx_cnt >= sl->rx_expect) {
461 set_bit(SLF_RXEVENT, &sl->flags);
462 wake_up(&sl->kwt_wq);
463 printk(KERN_INFO "sllin_receive_buf count %d, wakeup\n", sl->rx_cnt);
465 printk(KERN_INFO "sllin_receive_buf count %d, waiting\n", sl->rx_cnt);
469 /*****************************************
470 * sllin message helper routines
471 *****************************************/
472 int sllin_configure_frame_cache(struct sllin *sl, struct can_frame *cf)
474 struct sllin_conf_entry *sce;
475 if (!(cf->can_id & SLLIN_LIN_ID_CONF))
478 sce = &sl->linfr_cache[cf->can_id & SLLIN_ID_MASK];
479 printk(KERN_INFO "Setting frame cache with EFF CAN frame. "
480 "LIN ID = %d\n", cf->can_id & SLLIN_ID_MASK);
482 sce->dlc = (cf->can_dlc > 8) ? 8 : cf->can_dlc;
483 sce->frame_fl = (cf->can_id & ~SLLIN_ID_MASK) & CAN_EFF_MASK;
484 memcpy(sce->data, cf->data, cf->can_dlc);
489 int sllin_setup_msg(struct sllin *sl, int mode, int id,
490 unsigned char *data, int len)
492 if (id > SLLIN_ID_MASK)
498 sl->rx_lim = SLLIN_BUFF_LEN;
500 sl->tx_buff[SLLIN_BUFF_BREAK] = 0;
501 sl->tx_buff[SLLIN_BUFF_SYNC] = 0x55;
502 sl->tx_buff[SLLIN_BUFF_ID] = id | sllin_id_parity_table[id];
503 sl->tx_lim = SLLIN_BUFF_DATA;
505 if ((data != NULL) && len) {
510 memcpy(sl->tx_buff + SLLIN_BUFF_DATA, data, len);
511 /* compute data parity there */
512 for (i = SLLIN_BUFF_DATA; i < sl->tx_lim; i++) {
513 csum += sl->tx_buff[i];
518 sl->tx_buff[sl->tx_lim++] = csum;
521 sl->rx_lim = SLLIN_BUFF_DATA + len + 1;
527 int sllin_send_tx_buff(struct sllin *sl)
529 struct tty_struct *tty = sl->tty;
534 if (sl->lin_state != SLSTATE_BREAK_SENT)
535 remains = sl->tx_lim - sl->tx_cnt;
539 remains = sl->tx_lim - sl->tx_cnt;
542 res = tty->ops->write(tty, sl->tx_buff + sl->tx_cnt, remains);
550 set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
551 res = tty->ops->write(tty, sl->tx_buff + sl->tx_cnt, remains);
553 clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
561 printk(KERN_INFO "sllin_send_tx_buff sent %d, remains %d\n",
562 sl->tx_cnt, remains);
568 int sllin_send_break(struct sllin *sl)
570 struct tty_struct *tty = sl->tty;
571 unsigned long break_baud;
574 break_baud = ((sl->lin_baud * 2) / 3);
575 sltty_change_speed(tty, break_baud);
577 tty->ops->flush_buffer(tty);
578 sl->rx_cnt = SLLIN_BUFF_BREAK;
580 sl->rx_expect = SLLIN_BUFF_BREAK + 1;
581 sl->lin_state = SLSTATE_BREAK_SENT;
583 res = sllin_send_tx_buff(sl);
585 sl->lin_state = SLSTATE_IDLE;
591 #else /* BREAK_BY_BAUD */
593 int sllin_send_break(struct sllin *sl)
595 struct tty_struct *tty = sl->tty;
596 unsigned long break_baud;
600 sl->rx_cnt = SLLIN_BUFF_BREAK;
601 sl->rx_expect = SLLIN_BUFF_BREAK + 1;
602 sl->lin_state = SLSTATE_BREAK_SENT;
604 /* Do the break ourselves; Inspired by
605 http://lxr.linux.no/#linux+v3.1.2/drivers/tty/tty_io.c#L2452 */
606 retval = tty->ops->break_ctl(tty, -1);
611 usleep_range(650, 750);
613 retval = tty->ops->break_ctl(tty, 0);
614 usleep_range(50, 100);
616 tty->ops->flush_buffer(tty);
618 sl->tx_cnt = SLLIN_BUFF_SYNC;
620 printk(KERN_INFO "sllin: Break sent.\n");
621 set_bit(SLF_RXEVENT, &sl->flags);
622 wake_up(&sl->kwt_wq);
626 #endif /* BREAK_BY_BAUD */
629 static enum hrtimer_restart sllin_rx_timeout_handler(struct hrtimer *hrtimer)
631 struct sllin *sl = container_of(hrtimer, struct sllin, rx_timer);
633 set_bit(SLF_TMOUTEVENT, &sl->flags);
634 wake_up(&sl->kwt_wq);
636 return HRTIMER_NORESTART;
640 /*****************************************
641 * sllin_kwthread - kernel worker thread
642 *****************************************/
644 int sllin_kwthread(void *ptr)
646 struct sllin *sl = (struct sllin *)ptr;
647 struct tty_struct *tty = sl->tty;
648 struct sched_param schparam = { .sched_priority = 40 };
650 struct can_frame *cf;
653 int tx_bytes = 0; /* Used for Network statistics */
656 printk(KERN_INFO "sllin: sllin_kwthread started.\n");
657 sched_setscheduler(current, SCHED_FIFO, &schparam);
659 clear_bit(SLF_ERROR, &sl->flags);
660 sltty_change_speed(tty, sl->lin_baud);
662 while (!kthread_should_stop()) {
663 if ((sl->lin_state == SLSTATE_IDLE) && sl->lin_master &&
665 if(sllin_send_break(sl) < 0) {
666 /* error processing */
670 wait_event_killable(sl->kwt_wq, kthread_should_stop() ||
671 test_bit(SLF_RXEVENT, &sl->flags) ||
672 test_bit(SLF_TXEVENT, &sl->flags) ||
673 test_bit(SLF_TMOUTEVENT, &sl->flags) ||
674 ((sl->lin_state == SLSTATE_IDLE) && test_bit(SLF_MSGEVENT, &sl->flags)));
676 if (test_and_clear_bit(SLF_RXEVENT, &sl->flags)) {
677 printk(KERN_INFO "sllin_kthread RXEVENT \n");
680 if (test_and_clear_bit(SLF_TXEVENT, &sl->flags)) {
681 printk(KERN_INFO "sllin_kthread TXEVENT \n");
684 if (test_and_clear_bit(SLF_TMOUTEVENT, &sl->flags)) {
685 printk(KERN_INFO "sllin_kthread TMOUTEVENT \n");
688 sl->rx_lim = sl->lin_master ? 0 : SLLIN_BUFF_LEN;
691 sl->id_to_send = false;
692 sl->data_to_send = false;
694 sl->lin_state = SLSTATE_IDLE;
697 if ((sl->lin_state == SLSTATE_IDLE) && test_bit(SLF_MSGEVENT, &sl->flags)) {
698 cf = (struct can_frame *)sl->rec_skb->data;
700 /* EFF CAN frame -> "Configuration" frame */
701 if (cf->can_id & CAN_EFF_FLAG) {
702 sllin_configure_frame_cache(sl, cf);
704 } /* SFF RTR CAN frame -> LIN header */
705 else if (cf->can_id & CAN_RTR_FLAG) {
706 printk(KERN_INFO "%s: RTR SFF CAN frame, ID = %x\n",
707 __FUNCTION__, cf->can_id & SLLIN_ID_MASK);
709 /* Is there Slave response in linfr_cache to be sent? */
710 if ((sl->linfr_cache[cf->can_id & SLLIN_ID_MASK].frame_fl &
711 SLLIN_LOC_SLAVE_CACHE)
712 && (sl->linfr_cache[cf->can_id & SLLIN_ID_MASK].dlc > 0)) {
714 printk(KERN_INFO "Sending LIN response from linfr_cache\n");
715 lin_data = &sl->linfr_cache[cf->can_id & SLLIN_ID_MASK].data;
716 lin_dlc = sl->linfr_cache[cf->can_id & SLLIN_ID_MASK].dlc;
721 } else { /* SFF NON-RTR CAN frame -> LIN header + LIN response */
722 printk(KERN_INFO "%s: NON-RTR SFF CAN frame, ID = %x\n",
723 __FUNCTION__, (int)cf->can_id & SLLIN_ID_MASK);
726 lin_dlc = tx_bytes = cf->can_dlc;
729 if (sllin_setup_msg(sl, 0, cf->can_id & SLLIN_ID_MASK,
730 lin_data, lin_dlc) != -1) {
732 sl->id_to_send = true;
733 sl->data_to_send = (lin_dlc > 0) ? true : false;
734 sl->dev->stats.tx_packets++;
735 sl->dev->stats.tx_bytes += tx_bytes;
739 clear_bit(SLF_MSGEVENT, &sl->flags);
740 kfree_skb(sl->rec_skb);
741 netif_wake_queue(sl->dev);
744 switch (sl->lin_state) {
745 case SLSTATE_BREAK_SENT:
747 if (sl->rx_cnt <= SLLIN_BUFF_BREAK)
750 res = sltty_change_speed(tty, sl->lin_baud);
753 sl->lin_state = SLSTATE_ID_SENT;
754 sllin_send_tx_buff(sl);
757 case SLSTATE_ID_SENT:
758 sl->id_to_send = false;
759 if (sl->data_to_send) {
760 sllin_send_tx_buff(sl);
761 sl->lin_state = SLSTATE_RESPONSE_SENT;
762 sl->rx_expect = sl->tx_lim;
763 goto slstate_response_sent;
765 sl->rx_expect = SLLIN_BUFF_DATA + 2;
766 sl->lin_state = SLSTATE_RESPONSE_WAIT;
767 /* If we don't receive anything, timer will "unblock" us */
768 hrtimer_start(&sl->rx_timer,
769 ktime_add(ktime_get(), sl->rx_timer_timeout),
771 goto slstate_response_wait;
775 case SLSTATE_RESPONSE_WAIT:
776 slstate_response_wait:
777 if (sl->rx_cnt < sl->rx_expect)
780 hrtimer_cancel(&sl->rx_timer);
781 printk(KERN_INFO "sllin: response received ID %d len %d\n",
782 sl->rx_buff[SLLIN_BUFF_ID], sl->rx_cnt - SLLIN_BUFF_DATA - 1);
783 // FIXME: check checksum in sl->rx_buff
785 // send CAN non-RTR frame with data
786 printk(KERN_INFO "sllin: sending NON-RTR CAN frame with LIN payload.");
787 sll_bump(sl); //send packet to the network layer
788 sl->id_to_send = false;
789 sl->lin_state = SLSTATE_IDLE;
792 case SLSTATE_RESPONSE_SENT:
793 slstate_response_sent:
794 if (sl->rx_cnt < sl->tx_lim)
797 sll_bump(sl); //send packet to the network layer
798 printk(KERN_INFO "sllin: response sent ID %d len %d\n",
799 sl->rx_buff[SLLIN_BUFF_ID], sl->rx_cnt - SLLIN_BUFF_DATA - 1);
801 sl->id_to_send = false;
802 sl->lin_state = SLSTATE_IDLE;
809 /* sl->dev->stats.tx_packets++; send frames statistic */
810 /* netif_wake_queue(sl->dev); allow next Tx packet arrival */
813 hrtimer_cancel(&sl->rx_timer);
814 printk(KERN_INFO "sllin: sllin_kwthread stopped.\n");
820 /************************************
821 * sllin_open helper routines.
822 ************************************/
824 /* Collect hanged up channels */
825 static void sll_sync(void)
828 struct net_device *dev;
831 for (i = 0; i < maxdev; i++) {
836 sl = netdev_priv(dev);
839 if (dev->flags & IFF_UP)
844 /* Find a free SLLIN channel, and link in this `tty' line. */
845 static struct sllin *sll_alloc(dev_t line)
848 struct net_device *dev = NULL;
851 if (sllin_devs == NULL)
852 return NULL; /* Master array missing ! */
854 for (i = 0; i < maxdev; i++) {
861 /* Sorry, too many, all slots in use */
866 sl = netdev_priv(dev);
867 if (test_bit(SLF_INUSE, &sl->flags)) {
868 unregister_netdevice(dev);
870 sllin_devs[i] = NULL;
876 sprintf(name, "sllin%d", i);
878 dev = alloc_netdev(sizeof(*sl), name, sll_setup);
884 sl = netdev_priv(dev);
885 /* Initialize channel control data */
886 sl->magic = SLLIN_MAGIC;
888 spin_lock_init(&sl->lock);
895 * Open the high-level part of the SLLIN channel.
896 * This function is called by the TTY module when the
897 * SLLIN line discipline is called for. Because we are
898 * sure the tty line exists, we only have to link it to
899 * a free SLLIN channel...
901 * Called in process context serialized from other ldisc calls.
904 static int sllin_open(struct tty_struct *tty)
908 pr_debug("sllin: %s() invoked\n", __FUNCTION__);
910 if (!capable(CAP_NET_ADMIN))
913 if (tty->ops->write == NULL)
916 /* RTnetlink lock is misused here to serialize concurrent
917 opens of sllin channels. There are better ways, but it is
922 /* Collect hanged up channels. */
928 /* First make sure we're not already connected. */
929 if (sl && sl->magic == SLLIN_MAGIC)
932 /* OK. Find a free SLLIN channel to use. */
934 sl = sll_alloc(tty_devnum(tty));
940 sl->line = tty_devnum(tty);
942 if (!test_bit(SLF_INUSE, &sl->flags)) {
943 /* Perform the low-level SLLIN initialization. */
944 sl->lin_master = true;
948 sl->rx_lim = sl->lin_master ? 0 : SLLIN_BUFF_LEN;
951 sl->id_to_send = false;
952 sl->data_to_send = false;
954 sl->lin_baud = 19200;
956 sl->lin_state = SLSTATE_IDLE;
958 #define SAMPLES_PER_CHAR 10
959 #define CHARS_TO_TIMEOUT 12
960 hrtimer_init(&sl->rx_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
961 sl->rx_timer.function = sllin_rx_timeout_handler;
962 /* timeval_to_ktime(msg_head->ival1); */
963 sl->rx_timer_timeout = ns_to_ktime(
964 (1000000000 / sl->lin_baud) *
965 SAMPLES_PER_CHAR * CHARS_TO_TIMEOUT);
967 set_bit(SLF_INUSE, &sl->flags);
969 init_waitqueue_head(&sl->kwt_wq);
970 sl->kwthread = kthread_run(sllin_kwthread, sl, "sllin");
971 if (sl->kwthread == NULL)
974 err = register_netdevice(sl->dev);
976 goto err_free_chan_and_thread;
979 /* Done. We have linked the TTY line to a channel. */
981 tty->receive_room = SLLIN_BUFF_LEN * 40; /* We don't flow control */
983 /* TTY layer expects 0 on success */
986 err_free_chan_and_thread:
987 kthread_stop(sl->kwthread);
992 tty->disc_data = NULL;
993 clear_bit(SLF_INUSE, &sl->flags);
998 /* Count references from TTY module */
1003 * Close down a SLLIN channel.
1004 * This means flushing out any pending queues, and then returning. This
1005 * call is serialized against other ldisc functions.
1007 * We also use this method for a hangup event.
1010 static void sllin_close(struct tty_struct *tty)
1012 struct sllin *sl = (struct sllin *) tty->disc_data;
1014 /* First make sure we're connected. */
1015 if (!sl || sl->magic != SLLIN_MAGIC || sl->tty != tty)
1018 kthread_stop(sl->kwthread);
1019 sl->kwthread = NULL;
1021 tty->disc_data = NULL;
1024 /* Flush network side */
1025 unregister_netdev(sl->dev);
1026 /* This will complete via sl_free_netdev */
1029 static int sllin_hangup(struct tty_struct *tty)
1035 /* Perform I/O control on an active SLLIN channel. */
1036 static int sllin_ioctl(struct tty_struct *tty, struct file *file,
1037 unsigned int cmd, unsigned long arg)
1039 struct sllin *sl = (struct sllin *) tty->disc_data;
1042 /* First make sure we're connected. */
1043 if (!sl || sl->magic != SLLIN_MAGIC)
1048 tmp = strlen(sl->dev->name) + 1;
1049 if (copy_to_user((void __user *)arg, sl->dev->name, tmp))
1057 return tty_mode_ioctl(tty, file, cmd, arg);
1061 static struct tty_ldisc_ops sll_ldisc = {
1062 .owner = THIS_MODULE,
1063 .magic = TTY_LDISC_MAGIC,
1066 .close = sllin_close,
1067 .hangup = sllin_hangup,
1068 .ioctl = sllin_ioctl,
1069 .receive_buf = sllin_receive_buf,
1070 .write_wakeup = sllin_write_wakeup,
1073 static int __init sllin_init(void)
1078 maxdev = 4; /* Sanity */
1081 printk(KERN_INFO "sllin: %d dynamic interface channels.\n", maxdev);
1083 sllin_devs = kzalloc(sizeof(struct net_device *)*maxdev, GFP_KERNEL);
1085 printk(KERN_ERR "sllin: can't allocate sllin device array!\n");
1089 /* Fill in our line protocol discipline, and register it */
1090 status = tty_register_ldisc(N_SLLIN, &sll_ldisc);
1092 printk(KERN_ERR "sllin: can't register line discipline\n");
1098 static void __exit sllin_exit(void)
1101 struct net_device *dev;
1103 unsigned long timeout = jiffies + HZ;
1106 if (sllin_devs == NULL)
1109 /* First of all: check for active disciplines and hangup them.
1113 msleep_interruptible(100);
1116 for (i = 0; i < maxdev; i++) {
1117 dev = sllin_devs[i];
1120 sl = netdev_priv(dev);
1121 spin_lock_bh(&sl->lock);
1124 tty_hangup(sl->tty);
1126 spin_unlock_bh(&sl->lock);
1128 } while (busy && time_before(jiffies, timeout));
1130 /* FIXME: hangup is async so we should wait when doing this second
1133 for (i = 0; i < maxdev; i++) {
1134 dev = sllin_devs[i];
1137 sllin_devs[i] = NULL;
1139 sl = netdev_priv(dev);
1141 printk(KERN_ERR "%s: tty discipline still running\n",
1143 /* Intentionally leak the control block. */
1144 dev->destructor = NULL;
1147 unregister_netdev(dev);
1153 i = tty_unregister_ldisc(N_SLLIN);
1155 printk(KERN_ERR "sllin: can't unregister ldisc (err %d)\n", i);
1158 module_init(sllin_init);
1159 module_exit(sllin_exit);