2 * sllin.c - serial line LIN interface driver (using tty line discipline)
4 * This file is derived from drivers/net/can/slcan.c
5 * slcan.c Author: Oliver Hartkopp <socketcan@hartkopp.net>
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.
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.
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
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
35 * Send feedback to <socketcan-users@lists.berlios.de>
39 #include <linux/module.h>
40 #include <linux/moduleparam.h>
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>
60 /* Should be in include/linux/tty.h */
63 static __initdata const char banner[] =
64 KERN_INFO "sllin: serial line LIN interface driver\n";
66 MODULE_ALIAS_LDISC(N_SLLIN);
67 MODULE_DESCRIPTION("serial line LIN interface");
68 MODULE_LICENSE("GPL");
71 #define SLLIN_MAGIC 0x53CA
72 // #define BREAK_BY_BAUD
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");
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
89 #define SLLIN_ID_MASK 0x3f
90 #define SLLIN_ID_MAX SLLIN_ID_MASK
96 SLSTATE_RESPONSE_WAIT,
97 SLSTATE_RESPONSE_SENT,
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 */
118 /* Various fields. */
119 struct tty_struct *tty; /* ptr to TTY structure */
120 struct net_device *dev; /* easy for intr handling */
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 */
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 */
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 */
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 */
156 static struct net_device **sllin_devs;
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
169 static int sltty_change_speed(struct tty_struct *tty, unsigned speed)
171 struct ktermios old_termios;
174 mutex_lock(&tty->termios_mutex);
176 old_termios = *(tty->termios);
177 cflag = tty->termios->c_cflag;
178 cflag &= ~(CBAUD | CIBAUD);
180 tty->termios->c_cflag = cflag;
182 tty_encode_baud_rate(tty, speed, speed);
184 if (tty->ops->set_termios)
185 tty->ops->set_termios(tty, &old_termios);
187 mutex_unlock(&tty->termios_mutex);
193 /* Send one completely decapsulated can_frame to the network layer */
194 static void sll_bump(struct sllin *sl)
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);
203 skb = dev_alloc_skb(sizeof(struct can_frame));
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));
215 sl->dev->stats.rx_packets++;
216 sl->dev->stats.rx_bytes += cf.can_dlc;
220 * Called by the driver when there's room for more data. If we have
221 * more packets to send, we send them here.
223 static void sllin_write_wakeup(struct tty_struct *tty)
227 struct sllin *sl = (struct sllin *) tty->disc_data;
229 /* First make sure we're connected. */
230 if (!sl || sl->magic != SLLIN_MAGIC || !netif_running(sl->dev))
233 if (sl->lin_state != SLSTATE_BREAK_SENT)
234 remains = sl->tx_lim - sl->tx_cnt;
236 remains = SLLIN_BUFF_BREAK + 1 - sl->tx_cnt;
239 actual = tty->ops->write(tty, sl->tx_buff + sl->tx_cnt, sl->tx_cnt - sl->tx_lim);
240 sl->tx_cnt += actual;
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);
249 clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
250 set_bit(SLF_TXEVENT, &sl->flags);
251 wake_up(&sl->kwt_wq);
253 printk(KERN_INFO "sllin_write_wakeup sent %d, wakeup\n", sl->tx_cnt);
256 /* Send a can_frame to a TTY queue. */
257 static netdev_tx_t sll_xmit(struct sk_buff *skb, struct net_device *dev)
259 struct sllin *sl = netdev_priv(dev);
261 if (skb->len != sizeof(struct can_frame))
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);
270 if (sl->tty == NULL) {
271 spin_unlock(&sl->lock);
275 netif_stop_queue(sl->dev);
278 set_bit(SLF_MSGEVENT, &sl->flags);
279 wake_up(&sl->kwt_wq);
280 spin_unlock(&sl->lock);
289 /******************************************
290 * Routines looking at netdevice side.
291 ******************************************/
293 /* Netdevice UP -> DOWN routine */
294 static int sll_close(struct net_device *dev)
296 struct sllin *sl = netdev_priv(dev);
298 spin_lock_bh(&sl->lock);
300 /* TTY discipline is running. */
301 clear_bit(TTY_DO_WRITE_WAKEUP, &sl->tty->flags);
303 netif_stop_queue(dev);
306 spin_unlock_bh(&sl->lock);
311 /* Netdevice DOWN -> UP routine */
312 static int sll_open(struct net_device *dev)
314 struct sllin *sl = netdev_priv(dev);
316 pr_debug("sllin: %s() invoked\n", __FUNCTION__);
321 sl->flags &= (1 << SLF_INUSE);
322 netif_start_queue(dev);
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)
329 int i = dev->base_addr;
331 sllin_devs[i] = NULL;
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,
340 static void sll_setup(struct net_device *dev)
342 dev->netdev_ops = &sll_netdev_ops;
343 dev->destructor = sll_free_netdev;
345 dev->hard_header_len = 0;
347 dev->tx_queue_len = 10;
349 dev->mtu = sizeof(struct can_frame);
350 dev->type = ARPHRD_CAN;
352 /* New-style flags. */
353 dev->flags = IFF_NOARP;
354 dev->features = NETIF_F_NO_CSUM;
357 /******************************************
358 Routines looking at TTY side.
359 ******************************************/
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
370 static void sllin_receive_buf(struct tty_struct *tty,
371 const unsigned char *cp, char *fp, int count)
373 struct sllin *sl = (struct sllin *) tty->disc_data;
375 printk(KERN_INFO "sllin_receive_buf invoked\n");
377 if (!sl || sl->magic != SLLIN_MAGIC || !netif_running(sl->dev))
380 /* Read the characters out of the buffer */
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);
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;
399 printk(KERN_INFO "LIN_RX[%d]: 0x%02x\n", sl->rx_cnt, *cp);
400 sl->rx_buff[sl->rx_cnt++] = *cp++;
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);
409 printk(KERN_INFO "sllin_receive_buf count %d, waiting\n", sl->rx_cnt);
413 /*****************************************
414 * sllin message helper routines
415 *****************************************/
416 int sllin_configure_frame_cache(struct sllin *sl, struct can_frame *cf)
418 struct sllin_conf_entry *sce;
419 if (!(cf->can_id & SLLIN_LIN_ID_CONF))
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);
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);
433 int sllin_setup_msg(struct sllin *sl, int mode, int id,
434 unsigned char *data, int len)
436 if (id > SLLIN_ID_MASK)
442 sl->rx_lim = SLLIN_BUFF_LEN;
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;
449 if ((data != NULL) && 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];
462 sl->tx_buff[sl->tx_lim++] = csum;
465 sl->rx_lim = SLLIN_BUFF_DATA + len + 1;
471 int sllin_send_tx_buff(struct sllin *sl)
473 struct tty_struct *tty = sl->tty;
478 if (sl->lin_state != SLSTATE_BREAK_SENT)
479 remains = sl->tx_lim - sl->tx_cnt;
483 remains = sl->tx_lim - sl->tx_cnt;
486 res = tty->ops->write(tty, sl->tx_buff + sl->tx_cnt, remains);
494 set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
495 res = tty->ops->write(tty, sl->tx_buff + sl->tx_cnt, remains);
497 clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
505 printk(KERN_INFO "sllin_send_tx_buff sent %d, remains %d\n",
506 sl->tx_cnt, remains);
512 int sllin_send_break(struct sllin *sl)
514 struct tty_struct *tty = sl->tty;
515 unsigned long break_baud;
518 break_baud = ((sl->lin_baud * 2) / 3);
519 sltty_change_speed(tty, break_baud);
521 tty->ops->flush_buffer(tty);
522 sl->rx_cnt = SLLIN_BUFF_BREAK;
524 sl->rx_expect = SLLIN_BUFF_BREAK + 1;
525 sl->lin_state = SLSTATE_BREAK_SENT;
527 res = sllin_send_tx_buff(sl);
529 sl->lin_state = SLSTATE_IDLE;
535 #else /* BREAK_BY_BAUD */
537 int sllin_send_break(struct sllin *sl)
539 struct tty_struct *tty = sl->tty;
540 unsigned long break_baud;
544 sl->rx_cnt = SLLIN_BUFF_BREAK;
545 sl->rx_expect = SLLIN_BUFF_BREAK + 1;
546 sl->lin_state = SLSTATE_BREAK_SENT;
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);
555 usleep_range(650, 750);
557 retval = tty->ops->break_ctl(tty, 0);
558 usleep_range(50, 100);
560 tty->ops->flush_buffer(tty);
562 sl->tx_cnt = SLLIN_BUFF_SYNC;
564 printk(KERN_INFO "sllin: Break sent.\n");
565 set_bit(SLF_RXEVENT, &sl->flags);
566 wake_up(&sl->kwt_wq);
570 #endif /* BREAK_BY_BAUD */
573 static enum hrtimer_restart sllin_rx_timeout_handler(struct hrtimer *hrtimer)
575 struct sllin *sl = container_of(hrtimer, struct sllin, rx_timer);
577 set_bit(SLF_TMOUTEVENT, &sl->flags);
578 wake_up(&sl->kwt_wq);
580 return HRTIMER_NORESTART;
584 /*****************************************
585 * sllin_kwthread - kernel worker thread
586 *****************************************/
588 int sllin_kwthread(void *ptr)
590 struct sllin *sl = (struct sllin *)ptr;
591 struct tty_struct *tty = sl->tty;
592 struct sched_param schparam = { .sched_priority = 40 };
594 struct can_frame *cf;
597 int tx_bytes = 0; /* Used for Network statistics */
600 printk(KERN_INFO "sllin: sllin_kwthread started.\n");
601 sched_setscheduler(current, SCHED_FIFO, &schparam);
603 clear_bit(SLF_ERROR, &sl->flags);
604 sltty_change_speed(tty, sl->lin_baud);
606 while (!kthread_should_stop()) {
607 if ((sl->lin_state == SLSTATE_IDLE) && sl->lin_master &&
609 if(sllin_send_break(sl) < 0) {
610 /* error processing */
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)));
620 if (test_and_clear_bit(SLF_RXEVENT, &sl->flags)) {
621 printk(KERN_INFO "sllin_kthread RXEVENT \n");
624 if (test_and_clear_bit(SLF_TXEVENT, &sl->flags)) {
625 printk(KERN_INFO "sllin_kthread TXEVENT \n");
628 if (test_and_clear_bit(SLF_TMOUTEVENT, &sl->flags)) {
629 printk(KERN_INFO "sllin_kthread TMOUTEVENT \n");
632 sl->rx_lim = sl->lin_master ? 0 : SLLIN_BUFF_LEN;
635 sl->id_to_send = false;
636 sl->data_to_send = false;
638 sl->lin_state = SLSTATE_IDLE;
641 if ((sl->lin_state == SLSTATE_IDLE) && test_bit(SLF_MSGEVENT, &sl->flags)) {
642 cf = (struct can_frame *)sl->rec_skb->data;
644 /* EFF CAN frame -> "Configuration" frame */
645 if (cf->can_id & CAN_EFF_FLAG) {
646 sllin_configure_frame_cache(sl, cf);
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);
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)) {
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;
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);
670 lin_dlc = tx_bytes = cf->can_dlc;
673 if (sllin_setup_msg(sl, 0, cf->can_id & SLLIN_ID_MASK,
674 lin_data, lin_dlc) != -1) {
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;
683 clear_bit(SLF_MSGEVENT, &sl->flags);
684 kfree_skb(sl->rec_skb);
685 netif_wake_queue(sl->dev);
688 switch (sl->lin_state) {
689 case SLSTATE_BREAK_SENT:
691 if (sl->rx_cnt <= SLLIN_BUFF_BREAK)
694 res = sltty_change_speed(tty, sl->lin_baud);
697 sl->lin_state = SLSTATE_ID_SENT;
698 sllin_send_tx_buff(sl);
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;
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),
715 goto slstate_response_wait;
719 case SLSTATE_RESPONSE_WAIT:
720 slstate_response_wait:
721 if (sl->rx_cnt < sl->rx_expect)
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
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;
736 case SLSTATE_RESPONSE_SENT:
737 slstate_response_sent:
738 if (sl->rx_cnt < sl->tx_lim)
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);
745 sl->id_to_send = false;
746 sl->lin_state = SLSTATE_IDLE;
753 /* sl->dev->stats.tx_packets++; send frames statistic */
754 /* netif_wake_queue(sl->dev); allow next Tx packet arrival */
757 hrtimer_cancel(&sl->rx_timer);
758 printk(KERN_INFO "sllin: sllin_kwthread stopped.\n");
764 /************************************
765 * sllin_open helper routines.
766 ************************************/
768 /* Collect hanged up channels */
769 static void sll_sync(void)
772 struct net_device *dev;
775 for (i = 0; i < maxdev; i++) {
780 sl = netdev_priv(dev);
783 if (dev->flags & IFF_UP)
788 /* Find a free SLLIN channel, and link in this `tty' line. */
789 static struct sllin *sll_alloc(dev_t line)
792 struct net_device *dev = NULL;
795 if (sllin_devs == NULL)
796 return NULL; /* Master array missing ! */
798 for (i = 0; i < maxdev; i++) {
805 /* Sorry, too many, all slots in use */
810 sl = netdev_priv(dev);
811 if (test_bit(SLF_INUSE, &sl->flags)) {
812 unregister_netdevice(dev);
814 sllin_devs[i] = NULL;
820 sprintf(name, "sllin%d", i);
822 dev = alloc_netdev(sizeof(*sl), name, sll_setup);
828 sl = netdev_priv(dev);
829 /* Initialize channel control data */
830 sl->magic = SLLIN_MAGIC;
832 spin_lock_init(&sl->lock);
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...
845 * Called in process context serialized from other ldisc calls.
848 static int sllin_open(struct tty_struct *tty)
852 pr_debug("sllin: %s() invoked\n", __FUNCTION__);
854 if (!capable(CAP_NET_ADMIN))
857 if (tty->ops->write == NULL)
860 /* RTnetlink lock is misused here to serialize concurrent
861 opens of sllin channels. There are better ways, but it is
866 /* Collect hanged up channels. */
872 /* First make sure we're not already connected. */
873 if (sl && sl->magic == SLLIN_MAGIC)
876 /* OK. Find a free SLLIN channel to use. */
878 sl = sll_alloc(tty_devnum(tty));
884 sl->line = tty_devnum(tty);
886 if (!test_bit(SLF_INUSE, &sl->flags)) {
887 /* Perform the low-level SLLIN initialization. */
888 sl->lin_master = true;
892 sl->rx_lim = sl->lin_master ? 0 : SLLIN_BUFF_LEN;
895 sl->id_to_send = false;
896 sl->data_to_send = false;
898 sl->lin_baud = 19200;
900 sl->lin_state = SLSTATE_IDLE;
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);
911 set_bit(SLF_INUSE, &sl->flags);
913 init_waitqueue_head(&sl->kwt_wq);
914 sl->kwthread = kthread_run(sllin_kwthread, sl, "sllin");
915 if (sl->kwthread == NULL)
918 err = register_netdevice(sl->dev);
920 goto err_free_chan_and_thread;
923 /* Done. We have linked the TTY line to a channel. */
925 tty->receive_room = SLLIN_BUFF_LEN * 40; /* We don't flow control */
927 /* TTY layer expects 0 on success */
930 err_free_chan_and_thread:
931 kthread_stop(sl->kwthread);
936 tty->disc_data = NULL;
937 clear_bit(SLF_INUSE, &sl->flags);
942 /* Count references from TTY module */
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.
951 * We also use this method for a hangup event.
954 static void sllin_close(struct tty_struct *tty)
956 struct sllin *sl = (struct sllin *) tty->disc_data;
958 /* First make sure we're connected. */
959 if (!sl || sl->magic != SLLIN_MAGIC || sl->tty != tty)
962 kthread_stop(sl->kwthread);
965 tty->disc_data = NULL;
968 /* Flush network side */
969 unregister_netdev(sl->dev);
970 /* This will complete via sl_free_netdev */
973 static int sllin_hangup(struct tty_struct *tty)
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)
983 struct sllin *sl = (struct sllin *) tty->disc_data;
986 /* First make sure we're connected. */
987 if (!sl || sl->magic != SLLIN_MAGIC)
992 tmp = strlen(sl->dev->name) + 1;
993 if (copy_to_user((void __user *)arg, sl->dev->name, tmp))
1001 return tty_mode_ioctl(tty, file, cmd, arg);
1005 static struct tty_ldisc_ops sll_ldisc = {
1006 .owner = THIS_MODULE,
1007 .magic = TTY_LDISC_MAGIC,
1010 .close = sllin_close,
1011 .hangup = sllin_hangup,
1012 .ioctl = sllin_ioctl,
1013 .receive_buf = sllin_receive_buf,
1014 .write_wakeup = sllin_write_wakeup,
1017 static int __init sllin_init(void)
1022 maxdev = 4; /* Sanity */
1025 printk(KERN_INFO "sllin: %d dynamic interface channels.\n", maxdev);
1027 sllin_devs = kzalloc(sizeof(struct net_device *)*maxdev, GFP_KERNEL);
1029 printk(KERN_ERR "sllin: can't allocate sllin device array!\n");
1033 /* Fill in our line protocol discipline, and register it */
1034 status = tty_register_ldisc(N_SLLIN, &sll_ldisc);
1036 printk(KERN_ERR "sllin: can't register line discipline\n");
1042 static void __exit sllin_exit(void)
1045 struct net_device *dev;
1047 unsigned long timeout = jiffies + HZ;
1050 if (sllin_devs == NULL)
1053 /* First of all: check for active disciplines and hangup them.
1057 msleep_interruptible(100);
1060 for (i = 0; i < maxdev; i++) {
1061 dev = sllin_devs[i];
1064 sl = netdev_priv(dev);
1065 spin_lock_bh(&sl->lock);
1068 tty_hangup(sl->tty);
1070 spin_unlock_bh(&sl->lock);
1072 } while (busy && time_before(jiffies, timeout));
1074 /* FIXME: hangup is async so we should wait when doing this second
1077 for (i = 0; i < maxdev; i++) {
1078 dev = sllin_devs[i];
1081 sllin_devs[i] = NULL;
1083 sl = netdev_priv(dev);
1085 printk(KERN_ERR "%s: tty discipline still running\n",
1087 /* Intentionally leak the control block. */
1088 dev->destructor = NULL;
1091 unregister_netdev(dev);
1097 i = tty_unregister_ldisc(N_SLLIN);
1099 printk(KERN_ERR "sllin: can't unregister ldisc (err %d)\n", i);
1102 module_init(sllin_init);
1103 module_exit(sllin_exit);