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
91 #define SLLIN_CTRL_FRAME CAN_EFF_FLAG
93 #define SLLIN_SAMPLES_PER_CHAR 10
94 #define SLLIN_CHARS_TO_TIMEOUT 12
100 SLSTATE_RESPONSE_WAIT, /* Wait for response */
101 SLSTATE_RESPONSE_WAIT_BUS, /* Wait for response from LIN bus
102 only (CAN frames from network stack
103 are not processed in this moment) */
104 SLSTATE_RESPONSE_SENT,
107 struct sllin_conf_entry {
108 int dlc; /* Length of data in LIN frame */
109 #define SLLIN_CANFR_FLAGS_OFFS 6 /* Lower 6 bits in can_id correspond to LIN ID */
110 /* Save configuration for particular LIN ID */
111 #define SLLIN_LIN_ID_CONF (1 << SLLIN_CANFR_FLAGS_OFFS)
112 /* Publisher of particular LIN response is SLLIN Master */
113 #define SLLIN_SRC_MASTER (1 << (SLLIN_CANFR_FLAGS_OFFS + 1))
114 #define SLLIN_SRC_SLAVE (1 << (SLLIN_CANFR_FLAGS_OFFS + 2))
115 #define SLLIN_SLAVE_LOCAL (1 << (SLLIN_CANFR_FLAGS_OFFS + 3))
116 #define SLLIN_SLAVE_REMOTE (1 << (SLLIN_CANFR_FLAGS_OFFS + 4))
117 #define SLLIN_LOC_SLAVE_CACHE (1 << (SLLIN_CANFR_FLAGS_OFFS + 5))
119 #define SLLIN_ERR_RX_TIMEOUT (1 << (SLLIN_CANFR_FLAGS_OFFS + 6))
120 #define SLLIN_ERR_CHECKSUM (1 << (SLLIN_CANFR_FLAGS_OFFS + 7))
121 //#define SLLIN_ERR_FRAMING (1 << (SLLIN_CANFR_FLAGS_OFFS + 8))
123 canid_t frame_fl; /* LIN frame flags. Passed from userspace as canid_t data type */
124 u8 data[8]; /* LIN frame data payload */
130 /* Various fields. */
131 struct tty_struct *tty; /* ptr to TTY structure */
132 struct net_device *dev; /* easy for intr handling */
135 /* LIN message buffer and actual processed data counts */
136 unsigned char rx_buff[SLLIN_BUFF_LEN]; /* LIN Rx buffer */
137 unsigned char tx_buff[SLLIN_BUFF_LEN]; /* LIN Tx buffer */
138 int rx_expect; /* expected number of Rx chars */
139 int rx_lim; /* maximum Rx chars for current frame */
140 int rx_cnt; /* message buffer Rx fill level */
141 int tx_lim; /* actual limit of bytes to Tx */
142 int tx_cnt; /* number of already Tx bytes */
143 char lin_master; /* node is a master node */
144 int lin_baud; /* LIN baudrate */
145 int lin_state; /* state */
146 char id_to_send; /* there is ID to be sent */
147 char data_to_send; /* there are data to be sent */
148 char resp_len_known; /* Length of the response is known */
150 unsigned long flags; /* Flag values/ mode etc */
151 #define SLF_INUSE 0 /* Channel in use */
152 #define SLF_ERROR 1 /* Parity, etc. error */
153 #define SLF_RXEVENT 2 /* Rx wake event */
154 #define SLF_TXEVENT 3 /* Tx wake event */
155 #define SLF_MSGEVENT 4 /* CAN message to sent */
156 #define SLF_TMOUTEVENT 5 /* Timeout on received data */
159 struct task_struct *kwthread;
160 wait_queue_head_t kwt_wq;
161 struct hrtimer rx_timer; /* RX timeout timer */
162 ktime_t rx_timer_timeout; /* RX timeout timer value */
163 struct sk_buff *tx_req_skb; /* Socket buffer with CAN frame received
166 struct sllin_conf_entry linfr_cache[SLLIN_ID_MAX + 1]; /* List with configurations for
167 each of 0 to SLLIN_ID_MAX LIN IDs */
170 static struct net_device **sllin_devs;
171 static int sllin_configure_frame_cache(struct sllin *sl, struct can_frame *cf);
174 const unsigned char sllin_id_parity_table[] = {
175 0x80,0xc0,0x40,0x00,0xc0,0x80,0x00,0x40,
176 0x00,0x40,0xc0,0x80,0x40,0x00,0x80,0xc0,
177 0x40,0x00,0x80,0xc0,0x00,0x40,0xc0,0x80,
178 0xc0,0x80,0x00,0x40,0x80,0xc0,0x40,0x00,
179 0x00,0x40,0xc0,0x80,0x40,0x00,0x80,0xc0,
180 0x80,0xc0,0x40,0x00,0xc0,0x80,0x00,0x40,
181 0xc0,0x80,0x00,0x40,0x80,0xc0,0x40,0x00,
182 0x40,0x00,0x80,0xc0,0x00,0x40,0xc0,0x80
185 static int sltty_change_speed(struct tty_struct *tty, unsigned speed)
187 struct ktermios old_termios;
190 mutex_lock(&tty->termios_mutex);
192 old_termios = *(tty->termios);
193 cflag = tty->termios->c_cflag;
194 cflag &= ~(CBAUD | CIBAUD);
196 tty->termios->c_cflag = cflag;
198 tty_encode_baud_rate(tty, speed, speed);
200 if (tty->ops->set_termios)
201 tty->ops->set_termios(tty, &old_termios);
203 mutex_unlock(&tty->termios_mutex);
209 /* Send one can_frame to the network layer */
210 static void sllin_send_canfr(struct sllin *sl, canid_t id, char *data, int len)
217 if (cf.can_dlc > 0) {
218 memcpy(&cf.data, data, cf.can_dlc);
221 skb = dev_alloc_skb(sizeof(struct can_frame));
226 skb->protocol = htons(ETH_P_CAN);
227 skb->pkt_type = PACKET_BROADCAST;
228 skb->ip_summed = CHECKSUM_UNNECESSARY;
229 memcpy(skb_put(skb, sizeof(struct can_frame)),
230 &cf, sizeof(struct can_frame));
233 sl->dev->stats.rx_packets++;
234 sl->dev->stats.rx_bytes += cf.can_dlc;
239 static void sll_bump(struct sllin *sl)
241 sllin_send_canfr(sl, sl->rx_buff[SLLIN_BUFF_ID] & SLLIN_ID_MASK,
242 sl->rx_buff + SLLIN_BUFF_DATA,
243 sl->rx_cnt - SLLIN_BUFF_DATA);
247 * Called by the driver when there's room for more data. If we have
248 * more packets to send, we send them here.
250 static void sllin_write_wakeup(struct tty_struct *tty)
254 struct sllin *sl = (struct sllin *) tty->disc_data;
256 /* First make sure we're connected. */
257 if (!sl || sl->magic != SLLIN_MAGIC || !netif_running(sl->dev))
260 if (sl->lin_state != SLSTATE_BREAK_SENT)
261 remains = sl->tx_lim - sl->tx_cnt;
263 remains = SLLIN_BUFF_BREAK + 1 - sl->tx_cnt;
266 actual = tty->ops->write(tty, sl->tx_buff + sl->tx_cnt, sl->tx_cnt - sl->tx_lim);
267 sl->tx_cnt += actual;
269 if (sl->tx_cnt < sl->tx_lim) {
270 printk(KERN_INFO "sllin_write_wakeup sent %d, remains %d, waiting\n",
271 sl->tx_cnt, sl->tx_lim - sl->tx_cnt);
276 clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
277 set_bit(SLF_TXEVENT, &sl->flags);
278 wake_up(&sl->kwt_wq);
280 printk(KERN_INFO "sllin_write_wakeup sent %d, wakeup\n", sl->tx_cnt);
283 /* Send a can_frame to a TTY queue. */
284 static netdev_tx_t sll_xmit(struct sk_buff *skb, struct net_device *dev)
286 struct sllin *sl = netdev_priv(dev);
287 struct can_frame *cf;
289 if (skb->len != sizeof(struct can_frame))
292 spin_lock(&sl->lock);
293 if (!netif_running(dev)) {
294 printk(KERN_WARNING "%s: xmit: iface is down\n", dev->name);
297 if (sl->tty == NULL) {
301 cf = (struct can_frame *) skb->data;
302 if (cf->can_id & SLLIN_CTRL_FRAME) {
303 sllin_configure_frame_cache(sl, cf);
304 goto free_out_unlock;
307 netif_stop_queue(sl->dev);
309 sl->tx_req_skb = skb;
310 set_bit(SLF_MSGEVENT, &sl->flags);
311 wake_up(&sl->kwt_wq);
312 spin_unlock(&sl->lock);
317 spin_unlock(&sl->lock);
324 /******************************************
325 * Routines looking at netdevice side.
326 ******************************************/
328 /* Netdevice UP -> DOWN routine */
329 static int sll_close(struct net_device *dev)
331 struct sllin *sl = netdev_priv(dev);
333 spin_lock_bh(&sl->lock);
335 /* TTY discipline is running. */
336 clear_bit(TTY_DO_WRITE_WAKEUP, &sl->tty->flags);
338 netif_stop_queue(dev);
341 spin_unlock_bh(&sl->lock);
346 /* Netdevice DOWN -> UP routine */
347 static int sll_open(struct net_device *dev)
349 struct sllin *sl = netdev_priv(dev);
351 pr_debug("sllin: %s() invoked\n", __FUNCTION__);
356 sl->flags &= (1 << SLF_INUSE);
357 netif_start_queue(dev);
361 /* Hook the destructor so we can free sllin devs at the right point in time */
362 static void sll_free_netdev(struct net_device *dev)
364 int i = dev->base_addr;
366 sllin_devs[i] = NULL;
369 static const struct net_device_ops sll_netdev_ops = {
370 .ndo_open = sll_open,
371 .ndo_stop = sll_close,
372 .ndo_start_xmit = sll_xmit,
375 static void sll_setup(struct net_device *dev)
377 dev->netdev_ops = &sll_netdev_ops;
378 dev->destructor = sll_free_netdev;
380 dev->hard_header_len = 0;
382 dev->tx_queue_len = 10;
384 dev->mtu = sizeof(struct can_frame);
385 dev->type = ARPHRD_CAN;
387 /* New-style flags. */
388 dev->flags = IFF_NOARP;
389 dev->features = NETIF_F_NO_CSUM;
392 /******************************************
393 Routines looking at TTY side.
394 ******************************************/
397 * Handle the 'receiver data ready' interrupt.
398 * This function is called by the 'tty_io' module in the kernel when
399 * a block of SLLIN data has been received, which can now be decapsulated
400 * and sent on to some IP layer for further processing. This will not
401 * be re-entered while running but other ldisc functions may be called
405 static void sllin_receive_buf(struct tty_struct *tty,
406 const unsigned char *cp, char *fp, int count)
408 struct sllin *sl = (struct sllin *) tty->disc_data;
410 printk(KERN_INFO "sllin_receive_buf invoked\n");
412 if (!sl || sl->magic != SLLIN_MAGIC || !netif_running(sl->dev))
415 /* Read the characters out of the buffer */
418 if (!test_and_set_bit(SLF_ERROR, &sl->flags))
419 sl->dev->stats.rx_errors++;
420 printk(KERN_INFO "sllin_receive_buf char 0x%02x ignored "
421 "due marker 0x%02x, flags 0x%lx\n",
422 *cp, *(fp-1), sl->flags);
427 if (sl->rx_cnt < SLLIN_BUFF_LEN) {
428 #ifndef BREAK_BY_BAUD
429 /* We didn't receive Break character */
430 if ((sl->rx_cnt == SLLIN_BUFF_BREAK) && (*cp == 0x55)) {
431 sl->rx_buff[sl->rx_cnt++] = 0x00;
434 printk(KERN_INFO "LIN_RX[%d]: 0x%02x\n", sl->rx_cnt, *cp);
435 sl->rx_buff[sl->rx_cnt++] = *cp++;
439 if (sl->rx_cnt >= sl->rx_expect) {
440 set_bit(SLF_RXEVENT, &sl->flags);
441 wake_up(&sl->kwt_wq);
442 printk(KERN_INFO "sllin_receive_buf count %d, wakeup\n", sl->rx_cnt);
444 printk(KERN_INFO "sllin_receive_buf count %d, waiting\n", sl->rx_cnt);
448 /*****************************************
449 * sllin message helper routines
450 *****************************************/
451 void sllin_report_error(struct sllin *sl, int err)
453 sllin_send_canfr(sl, 0 | CAN_EFF_FLAG |
454 (err & ~SLLIN_ID_MASK), NULL, 0);
457 static int sllin_configure_frame_cache(struct sllin *sl, struct can_frame *cf)
459 struct sllin_conf_entry *sce;
460 if (!(cf->can_id & SLLIN_LIN_ID_CONF))
463 sce = &sl->linfr_cache[cf->can_id & SLLIN_ID_MASK];
464 printk(KERN_INFO "Setting frame cache with EFF CAN frame. "
465 "LIN ID = %d\n", cf->can_id & SLLIN_ID_MASK);
467 sce->dlc = cf->can_dlc;
468 if (sce->dlc > SLLIN_DATA_MAX)
469 sce->dlc = SLLIN_DATA_MAX;
471 sce->frame_fl = (cf->can_id & ~SLLIN_ID_MASK) & CAN_EFF_MASK;
472 memcpy(sce->data, cf->data, cf->can_dlc);
477 #define SLLIN_STPMSG_RESPONLY (1) /* Message will be LIN Response only */
478 #define SLLIN_STPMSG_CHCKSUMV1 (1 << 1)
479 #define SLLIN_STPMSG_CHCKSUMV2 (1 << 2)
481 int sllin_setup_msg(struct sllin *sl, int mode, int id,
482 unsigned char *data, int len)
484 if (id > SLLIN_ID_MASK)
487 if (!(mode & SLLIN_STPMSG_RESPONLY)) {
491 sl->rx_lim = SLLIN_BUFF_LEN;
494 sl->tx_buff[SLLIN_BUFF_BREAK] = 0;
495 sl->tx_buff[SLLIN_BUFF_SYNC] = 0x55;
496 sl->tx_buff[SLLIN_BUFF_ID] = id | sllin_id_parity_table[id];
497 sl->tx_lim = SLLIN_BUFF_DATA;
499 if ((data != NULL) && len) {
504 memcpy(sl->tx_buff + SLLIN_BUFF_DATA, data, len);
505 /* compute data parity there */
506 for (i = SLLIN_BUFF_DATA; i < sl->tx_lim; i++) {
507 csum += sl->tx_buff[i];
512 sl->tx_buff[sl->tx_lim++] = csum;
515 sl->rx_lim = SLLIN_BUFF_DATA + len + 1;
521 int sllin_send_tx_buff(struct sllin *sl)
523 struct tty_struct *tty = sl->tty;
528 if (sl->lin_state != SLSTATE_BREAK_SENT)
529 remains = sl->tx_lim - sl->tx_cnt;
533 remains = sl->tx_lim - sl->tx_cnt;
536 res = tty->ops->write(tty, sl->tx_buff + sl->tx_cnt, remains);
544 set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
545 res = tty->ops->write(tty, sl->tx_buff + sl->tx_cnt, remains);
547 clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
555 printk(KERN_INFO "sllin_send_tx_buff sent %d, remains %d\n",
556 sl->tx_cnt, remains);
562 int sllin_send_break(struct sllin *sl)
564 struct tty_struct *tty = sl->tty;
565 unsigned long break_baud;
568 break_baud = ((sl->lin_baud * 2) / 3);
569 sltty_change_speed(tty, break_baud);
571 tty->ops->flush_buffer(tty);
572 sl->rx_cnt = SLLIN_BUFF_BREAK;
574 sl->rx_expect = SLLIN_BUFF_BREAK + 1;
575 sl->lin_state = SLSTATE_BREAK_SENT;
577 res = sllin_send_tx_buff(sl);
579 sl->lin_state = SLSTATE_IDLE;
585 #else /* BREAK_BY_BAUD */
587 int sllin_send_break(struct sllin *sl)
589 struct tty_struct *tty = sl->tty;
592 unsigned long break_baud;
593 unsigned long usleep_range_min;
594 unsigned long usleep_range_max;
596 break_baud = ((sl->lin_baud * 2) / 3);
597 sl->rx_cnt = SLLIN_BUFF_BREAK;
598 sl->rx_expect = SLLIN_BUFF_BREAK + 1;
599 sl->lin_state = SLSTATE_BREAK_SENT;
601 /* Do the break ourselves; Inspired by
602 http://lxr.linux.no/#linux+v3.1.2/drivers/tty/tty_io.c#L2452 */
603 retval = tty->ops->break_ctl(tty, -1);
608 usleep_range_min = (1000000l * SLLIN_SAMPLES_PER_CHAR) / break_baud;
609 usleep_range_max = usleep_range_min + 50;
610 usleep_range(usleep_range_min, usleep_range_max);
612 retval = tty->ops->break_ctl(tty, 0);
613 usleep_range_min = (1000000l * 1 /* 1 bit */) / break_baud;
614 usleep_range_max = usleep_range_min + 30;
615 usleep_range(usleep_range_min, usleep_range_max);
617 tty->ops->flush_buffer(tty);
619 sl->tx_cnt = SLLIN_BUFF_SYNC;
621 printk(KERN_INFO "sllin: Break sent.\n");
622 set_bit(SLF_RXEVENT, &sl->flags);
623 wake_up(&sl->kwt_wq);
627 #endif /* BREAK_BY_BAUD */
630 static enum hrtimer_restart sllin_rx_timeout_handler(struct hrtimer *hrtimer)
632 struct sllin *sl = container_of(hrtimer, struct sllin, rx_timer);
634 sllin_report_error(sl, SLLIN_ERR_RX_TIMEOUT);
635 set_bit(SLF_TMOUTEVENT, &sl->flags);
636 wake_up(&sl->kwt_wq);
638 return HRTIMER_NORESTART;
642 /*****************************************
643 * sllin_kwthread - kernel worker thread
644 *****************************************/
646 int sllin_kwthread(void *ptr)
648 struct sllin *sl = (struct sllin *)ptr;
649 struct tty_struct *tty = sl->tty;
650 struct sched_param schparam = { .sched_priority = 40 };
651 int tx_bytes = 0; /* Used for Network statistics */
654 printk(KERN_INFO "sllin: sllin_kwthread started.\n");
655 sched_setscheduler(current, SCHED_FIFO, &schparam);
657 clear_bit(SLF_ERROR, &sl->flags);
658 sltty_change_speed(tty, sl->lin_baud);
660 while (!kthread_should_stop()) {
662 struct can_frame *cf;
665 u8 lin_data_buff[SLLIN_DATA_MAX];
668 if ((sl->lin_state == SLSTATE_IDLE) && sl->lin_master &&
670 if(sllin_send_break(sl) < 0) {
671 /* error processing */
675 wait_event_killable(sl->kwt_wq, kthread_should_stop() ||
676 test_bit(SLF_RXEVENT, &sl->flags) ||
677 test_bit(SLF_TXEVENT, &sl->flags) ||
678 test_bit(SLF_TMOUTEVENT, &sl->flags) ||
679 (((sl->lin_state == SLSTATE_IDLE) ||
680 (sl->lin_state == SLSTATE_RESPONSE_WAIT))
681 && test_bit(SLF_MSGEVENT, &sl->flags)));
683 if (test_and_clear_bit(SLF_RXEVENT, &sl->flags)) {
684 printk(KERN_INFO "sllin_kthread RXEVENT \n");
687 if (test_and_clear_bit(SLF_TXEVENT, &sl->flags)) {
688 printk(KERN_INFO "sllin_kthread TXEVENT \n");
691 if (test_and_clear_bit(SLF_TMOUTEVENT, &sl->flags)) {
692 printk(KERN_INFO "sllin_kthread TMOUTEVENT \n");
695 sl->rx_lim = sl->lin_master ? 0 : SLLIN_BUFF_LEN;
698 sl->id_to_send = false;
699 sl->data_to_send = false;
701 sl->lin_state = SLSTATE_IDLE;
704 switch (sl->lin_state) {
706 if (!test_bit(SLF_MSGEVENT, &sl->flags))
709 cf = (struct can_frame *)sl->tx_req_skb->data;
711 /* SFF RTR CAN frame -> LIN header */
712 if (cf->can_id & CAN_RTR_FLAG) {
713 spin_lock(&sl->lock);
714 printk(KERN_INFO "%s: RTR SFF CAN frame, ID = %x\n",
715 __FUNCTION__, cf->can_id & SLLIN_ID_MASK);
717 /* Is there Slave response in linfr_cache to be sent? */
718 if ((sl->linfr_cache[cf->can_id & SLLIN_ID_MASK].frame_fl &
719 SLLIN_LOC_SLAVE_CACHE)
720 && (sl->linfr_cache[cf->can_id & SLLIN_ID_MASK].dlc > 0)) {
722 printk(KERN_INFO "Sending LIN response from linfr_cache\n");
723 lin_data = sl->linfr_cache[cf->can_id & SLLIN_ID_MASK].data;
724 lin_dlc = sl->linfr_cache[cf->can_id & SLLIN_ID_MASK].dlc;
725 if (lin_dlc > SLLIN_DATA_MAX)
726 lin_dlc = SLLIN_DATA_MAX;
727 memcpy(lin_data_buff, lin_data, lin_dlc);
728 lin_data = lin_data_buff;
731 lin_dlc = sl->linfr_cache[cf->can_id & SLLIN_ID_MASK].dlc;
733 spin_unlock(&sl->lock);
734 } else { /* SFF NON-RTR CAN frame -> LIN header + LIN response */
735 printk(KERN_INFO "%s: NON-RTR SFF CAN frame, ID = %x\n",
736 __FUNCTION__, (int)cf->can_id & SLLIN_ID_MASK);
739 lin_dlc = cf->can_dlc;
740 if (lin_dlc > SLLIN_DATA_MAX)
741 lin_dlc = SLLIN_DATA_MAX;
745 if (sllin_setup_msg(sl, 0, cf->can_id & SLLIN_ID_MASK,
746 lin_data, lin_dlc) != -1) {
748 sl->id_to_send = true;
749 sl->data_to_send = (lin_data != NULL) ? true : false;
750 sl->resp_len_known = (lin_dlc > 0) ? true : false;
751 sl->dev->stats.tx_packets++;
752 sl->dev->stats.tx_bytes += tx_bytes;
755 clear_bit(SLF_MSGEVENT, &sl->flags);
756 kfree_skb(sl->tx_req_skb);
757 netif_wake_queue(sl->dev);
760 case SLSTATE_BREAK_SENT:
762 if (sl->rx_cnt <= SLLIN_BUFF_BREAK)
765 res = sltty_change_speed(tty, sl->lin_baud);
768 sl->lin_state = SLSTATE_ID_SENT;
769 sllin_send_tx_buff(sl);
772 case SLSTATE_ID_SENT:
773 sl->id_to_send = false;
774 if (sl->data_to_send) {
775 sllin_send_tx_buff(sl);
776 sl->lin_state = SLSTATE_RESPONSE_SENT;
777 sl->rx_expect = sl->tx_lim;
778 goto slstate_response_sent;
780 if (sl->resp_len_known) {
781 sl->rx_expect = sl->rx_lim;
783 sl->rx_expect = SLLIN_BUFF_DATA + 2;
785 sl->lin_state = SLSTATE_RESPONSE_WAIT;
786 /* If we don't receive anything, timer will "unblock" us */
787 hrtimer_start(&sl->rx_timer,
788 ktime_add(ktime_get(), sl->rx_timer_timeout),
790 goto slstate_response_wait;
794 case SLSTATE_RESPONSE_WAIT:
795 slstate_response_wait:
796 if (test_bit(SLF_MSGEVENT, &sl->flags)) {
797 unsigned char *lin_buff;
798 cf = (struct can_frame *)sl->tx_req_skb->data;
800 lin_buff = (sl->lin_master) ? sl->tx_buff : sl->rx_buff;
801 if (cf->can_id == (lin_buff[SLLIN_BUFF_ID] & SLLIN_ID_MASK)) {
802 if (sllin_setup_msg(sl, SLLIN_STPMSG_RESPONLY,
803 cf->can_id & SLLIN_ID_MASK,
804 cf->data, cf->can_dlc) != -1) {
806 sl->rx_expect = sl->tx_lim;
807 sl->data_to_send = true;
808 sl->dev->stats.tx_packets++;
809 sl->dev->stats.tx_bytes += tx_bytes;
811 if (!sl->lin_master) {
812 sl->tx_cnt = SLLIN_BUFF_DATA;
815 sllin_send_tx_buff(sl);
816 clear_bit(SLF_MSGEVENT, &sl->flags);
817 kfree_skb(sl->tx_req_skb);
818 netif_wake_queue(sl->dev);
820 sl->lin_state = SLSTATE_RESPONSE_SENT;
821 goto slstate_response_sent;
824 sl->lin_state = SLSTATE_RESPONSE_WAIT_BUS;
828 case SLSTATE_RESPONSE_WAIT_BUS:
829 if (sl->rx_cnt < sl->rx_expect)
832 hrtimer_cancel(&sl->rx_timer);
833 printk(KERN_INFO "sllin: response received ID %d len %d\n",
834 sl->rx_buff[SLLIN_BUFF_ID], sl->rx_cnt - SLLIN_BUFF_DATA - 1);
835 // FIXME: check checksum in sl->rx_buff
837 // send CAN non-RTR frame with data
838 printk(KERN_INFO "sllin: sending NON-RTR CAN frame with LIN payload.");
839 sll_bump(sl); //send packet to the network layer
840 sl->id_to_send = false;
841 sl->lin_state = SLSTATE_IDLE;
844 case SLSTATE_RESPONSE_SENT:
845 slstate_response_sent:
846 if (sl->rx_cnt < sl->tx_lim)
849 sll_bump(sl); //send packet to the network layer
850 printk(KERN_INFO "sllin: response sent ID %d len %d\n",
851 sl->rx_buff[SLLIN_BUFF_ID], sl->rx_cnt - SLLIN_BUFF_DATA - 1);
853 sl->id_to_send = false;
854 sl->lin_state = SLSTATE_IDLE;
861 /* sl->dev->stats.tx_packets++; send frames statistic */
862 /* netif_wake_queue(sl->dev); allow next Tx packet arrival */
865 hrtimer_cancel(&sl->rx_timer);
866 printk(KERN_INFO "sllin: sllin_kwthread stopped.\n");
872 /************************************
873 * sllin_open helper routines.
874 ************************************/
876 /* Collect hanged up channels */
877 static void sll_sync(void)
880 struct net_device *dev;
883 for (i = 0; i < maxdev; i++) {
888 sl = netdev_priv(dev);
891 if (dev->flags & IFF_UP)
896 /* Find a free SLLIN channel, and link in this `tty' line. */
897 static struct sllin *sll_alloc(dev_t line)
900 struct net_device *dev = NULL;
903 if (sllin_devs == NULL)
904 return NULL; /* Master array missing ! */
906 for (i = 0; i < maxdev; i++) {
913 /* Sorry, too many, all slots in use */
918 sl = netdev_priv(dev);
919 if (test_bit(SLF_INUSE, &sl->flags)) {
920 unregister_netdevice(dev);
922 sllin_devs[i] = NULL;
928 sprintf(name, "sllin%d", i);
930 dev = alloc_netdev(sizeof(*sl), name, sll_setup);
936 sl = netdev_priv(dev);
937 /* Initialize channel control data */
938 sl->magic = SLLIN_MAGIC;
940 spin_lock_init(&sl->lock);
947 * Open the high-level part of the SLLIN channel.
948 * This function is called by the TTY module when the
949 * SLLIN line discipline is called for. Because we are
950 * sure the tty line exists, we only have to link it to
951 * a free SLLIN channel...
953 * Called in process context serialized from other ldisc calls.
956 static int sllin_open(struct tty_struct *tty)
960 pr_debug("sllin: %s() invoked\n", __FUNCTION__);
962 if (!capable(CAP_NET_ADMIN))
965 if (tty->ops->write == NULL)
968 /* RTnetlink lock is misused here to serialize concurrent
969 opens of sllin channels. There are better ways, but it is
974 /* Collect hanged up channels. */
980 /* First make sure we're not already connected. */
981 if (sl && sl->magic == SLLIN_MAGIC)
984 /* OK. Find a free SLLIN channel to use. */
986 sl = sll_alloc(tty_devnum(tty));
992 sl->line = tty_devnum(tty);
994 if (!test_bit(SLF_INUSE, &sl->flags)) {
995 /* Perform the low-level SLLIN initialization. */
996 sl->lin_master = true;
1000 sl->rx_lim = sl->lin_master ? 0 : SLLIN_BUFF_LEN;
1003 sl->id_to_send = false;
1004 sl->data_to_send = false;
1006 sl->lin_baud = 19200;
1008 sl->lin_state = SLSTATE_IDLE;
1010 hrtimer_init(&sl->rx_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1011 sl->rx_timer.function = sllin_rx_timeout_handler;
1012 /* timeval_to_ktime(msg_head->ival1); */
1013 sl->rx_timer_timeout = ns_to_ktime(
1014 (1000000000l / sl->lin_baud) *
1015 SLLIN_SAMPLES_PER_CHAR * SLLIN_CHARS_TO_TIMEOUT);
1017 set_bit(SLF_INUSE, &sl->flags);
1019 init_waitqueue_head(&sl->kwt_wq);
1020 sl->kwthread = kthread_run(sllin_kwthread, sl, "sllin");
1021 if (sl->kwthread == NULL)
1024 err = register_netdevice(sl->dev);
1026 goto err_free_chan_and_thread;
1029 /* Done. We have linked the TTY line to a channel. */
1031 tty->receive_room = SLLIN_BUFF_LEN * 40; /* We don't flow control */
1033 /* TTY layer expects 0 on success */
1036 err_free_chan_and_thread:
1037 kthread_stop(sl->kwthread);
1038 sl->kwthread = NULL;
1042 tty->disc_data = NULL;
1043 clear_bit(SLF_INUSE, &sl->flags);
1048 /* Count references from TTY module */
1053 * Close down a SLLIN channel.
1054 * This means flushing out any pending queues, and then returning. This
1055 * call is serialized against other ldisc functions.
1057 * We also use this method for a hangup event.
1060 static void sllin_close(struct tty_struct *tty)
1062 struct sllin *sl = (struct sllin *) tty->disc_data;
1064 /* First make sure we're connected. */
1065 if (!sl || sl->magic != SLLIN_MAGIC || sl->tty != tty)
1068 kthread_stop(sl->kwthread);
1069 sl->kwthread = NULL;
1071 tty->disc_data = NULL;
1074 /* Flush network side */
1075 unregister_netdev(sl->dev);
1076 /* This will complete via sl_free_netdev */
1079 static int sllin_hangup(struct tty_struct *tty)
1085 /* Perform I/O control on an active SLLIN channel. */
1086 static int sllin_ioctl(struct tty_struct *tty, struct file *file,
1087 unsigned int cmd, unsigned long arg)
1089 struct sllin *sl = (struct sllin *) tty->disc_data;
1092 /* First make sure we're connected. */
1093 if (!sl || sl->magic != SLLIN_MAGIC)
1098 tmp = strlen(sl->dev->name) + 1;
1099 if (copy_to_user((void __user *)arg, sl->dev->name, tmp))
1107 return tty_mode_ioctl(tty, file, cmd, arg);
1111 static struct tty_ldisc_ops sll_ldisc = {
1112 .owner = THIS_MODULE,
1113 .magic = TTY_LDISC_MAGIC,
1116 .close = sllin_close,
1117 .hangup = sllin_hangup,
1118 .ioctl = sllin_ioctl,
1119 .receive_buf = sllin_receive_buf,
1120 .write_wakeup = sllin_write_wakeup,
1123 static int __init sllin_init(void)
1128 maxdev = 4; /* Sanity */
1131 printk(KERN_INFO "sllin: %d dynamic interface channels.\n", maxdev);
1133 sllin_devs = kzalloc(sizeof(struct net_device *)*maxdev, GFP_KERNEL);
1135 printk(KERN_ERR "sllin: can't allocate sllin device array!\n");
1139 /* Fill in our line protocol discipline, and register it */
1140 status = tty_register_ldisc(N_SLLIN, &sll_ldisc);
1142 printk(KERN_ERR "sllin: can't register line discipline\n");
1148 static void __exit sllin_exit(void)
1151 struct net_device *dev;
1153 unsigned long timeout = jiffies + HZ;
1156 if (sllin_devs == NULL)
1159 /* First of all: check for active disciplines and hangup them.
1163 msleep_interruptible(100);
1166 for (i = 0; i < maxdev; i++) {
1167 dev = sllin_devs[i];
1170 sl = netdev_priv(dev);
1171 spin_lock_bh(&sl->lock);
1174 tty_hangup(sl->tty);
1176 spin_unlock_bh(&sl->lock);
1178 } while (busy && time_before(jiffies, timeout));
1180 /* FIXME: hangup is async so we should wait when doing this second
1183 for (i = 0; i < maxdev; i++) {
1184 dev = sllin_devs[i];
1187 sllin_devs[i] = NULL;
1189 sl = netdev_priv(dev);
1191 printk(KERN_ERR "%s: tty discipline still running\n",
1193 /* Intentionally leak the control block. */
1194 dev->destructor = NULL;
1197 unregister_netdev(dev);
1203 i = tty_unregister_ldisc(N_SLLIN);
1205 printk(KERN_ERR "sllin: can't unregister ldisc (err %d)\n", i);
1208 module_init(sllin_init);
1209 module_exit(sllin_exit);