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))
118 #define SLLIN_CHECKSUM_EXTENDED (1 << (SLLIN_CANFR_FLAGS_OFFS + 6))
120 #define SLLIN_ERR_RX_TIMEOUT (1 << (SLLIN_CANFR_FLAGS_OFFS + 7))
121 #define SLLIN_ERR_CHECKSUM (1 << (SLLIN_CANFR_FLAGS_OFFS + 8))
122 //#define SLLIN_ERR_FRAMING (1 << (SLLIN_CANFR_FLAGS_OFFS + 9))
124 canid_t frame_fl; /* LIN frame flags. Passed from userspace as canid_t data type */
125 u8 data[8]; /* LIN frame data payload */
131 /* Various fields. */
132 struct tty_struct *tty; /* ptr to TTY structure */
133 struct net_device *dev; /* easy for intr handling */
136 /* LIN message buffer and actual processed data counts */
137 unsigned char rx_buff[SLLIN_BUFF_LEN]; /* LIN Rx buffer */
138 unsigned char tx_buff[SLLIN_BUFF_LEN]; /* LIN Tx buffer */
139 int rx_expect; /* expected number of Rx chars */
140 int rx_lim; /* maximum Rx chars for current frame */
141 int rx_cnt; /* message buffer Rx fill level */
142 int tx_lim; /* actual limit of bytes to Tx */
143 int tx_cnt; /* number of already Tx bytes */
144 char lin_master; /* node is a master node */
145 int lin_baud; /* LIN baudrate */
146 int lin_state; /* state */
147 char id_to_send; /* there is ID to be sent */
148 char data_to_send; /* there are data to be sent */
149 char resp_len_known; /* Length of the response is known */
151 unsigned long flags; /* Flag values/ mode etc */
152 #define SLF_INUSE 0 /* Channel in use */
153 #define SLF_ERROR 1 /* Parity, etc. error */
154 #define SLF_RXEVENT 2 /* Rx wake event */
155 #define SLF_TXEVENT 3 /* Tx wake event */
156 #define SLF_MSGEVENT 4 /* CAN message to sent */
157 #define SLF_TMOUTEVENT 5 /* Timeout on received data */
160 struct task_struct *kwthread;
161 wait_queue_head_t kwt_wq;
162 struct hrtimer rx_timer; /* RX timeout timer */
163 ktime_t rx_timer_timeout; /* RX timeout timer value */
164 struct sk_buff *tx_req_skb; /* Socket buffer with CAN frame received
167 struct sllin_conf_entry linfr_cache[SLLIN_ID_MAX + 1]; /* List with configurations for
168 each of 0 to SLLIN_ID_MAX LIN IDs */
171 static struct net_device **sllin_devs;
172 static int sllin_configure_frame_cache(struct sllin *sl, struct can_frame *cf);
175 const unsigned char sllin_id_parity_table[] = {
176 0x80,0xc0,0x40,0x00,0xc0,0x80,0x00,0x40,
177 0x00,0x40,0xc0,0x80,0x40,0x00,0x80,0xc0,
178 0x40,0x00,0x80,0xc0,0x00,0x40,0xc0,0x80,
179 0xc0,0x80,0x00,0x40,0x80,0xc0,0x40,0x00,
180 0x00,0x40,0xc0,0x80,0x40,0x00,0x80,0xc0,
181 0x80,0xc0,0x40,0x00,0xc0,0x80,0x00,0x40,
182 0xc0,0x80,0x00,0x40,0x80,0xc0,0x40,0x00,
183 0x40,0x00,0x80,0xc0,0x00,0x40,0xc0,0x80
186 static int sltty_change_speed(struct tty_struct *tty, unsigned speed)
188 struct ktermios old_termios;
191 mutex_lock(&tty->termios_mutex);
193 old_termios = *(tty->termios);
194 cflag = tty->termios->c_cflag;
195 cflag &= ~(CBAUD | CIBAUD);
197 tty->termios->c_cflag = cflag;
199 tty_encode_baud_rate(tty, speed, speed);
201 if (tty->ops->set_termios)
202 tty->ops->set_termios(tty, &old_termios);
204 mutex_unlock(&tty->termios_mutex);
210 /* Send one can_frame to the network layer */
211 static void sllin_send_canfr(struct sllin *sl, canid_t id, char *data, int len)
218 if (cf.can_dlc > 0) {
219 memcpy(&cf.data, data, cf.can_dlc);
222 skb = dev_alloc_skb(sizeof(struct can_frame));
227 skb->protocol = htons(ETH_P_CAN);
228 skb->pkt_type = PACKET_BROADCAST;
229 skb->ip_summed = CHECKSUM_UNNECESSARY;
230 memcpy(skb_put(skb, sizeof(struct can_frame)),
231 &cf, sizeof(struct can_frame));
234 sl->dev->stats.rx_packets++;
235 sl->dev->stats.rx_bytes += cf.can_dlc;
240 static void sll_bump(struct sllin *sl)
242 sllin_send_canfr(sl, sl->rx_buff[SLLIN_BUFF_ID] & SLLIN_ID_MASK,
243 sl->rx_buff + SLLIN_BUFF_DATA,
244 sl->rx_cnt - SLLIN_BUFF_DATA - 1); /* without checksum */
248 * Called by the driver when there's room for more data. If we have
249 * more packets to send, we send them here.
251 static void sllin_write_wakeup(struct tty_struct *tty)
255 struct sllin *sl = (struct sllin *) tty->disc_data;
257 /* First make sure we're connected. */
258 if (!sl || sl->magic != SLLIN_MAGIC || !netif_running(sl->dev))
261 if (sl->lin_state != SLSTATE_BREAK_SENT)
262 remains = sl->tx_lim - sl->tx_cnt;
264 remains = SLLIN_BUFF_BREAK + 1 - sl->tx_cnt;
267 actual = tty->ops->write(tty, sl->tx_buff + sl->tx_cnt, sl->tx_cnt - sl->tx_lim);
268 sl->tx_cnt += actual;
270 if (sl->tx_cnt < sl->tx_lim) {
271 printk(KERN_INFO "sllin_write_wakeup sent %d, remains %d, waiting\n",
272 sl->tx_cnt, sl->tx_lim - sl->tx_cnt);
277 clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
278 set_bit(SLF_TXEVENT, &sl->flags);
279 wake_up(&sl->kwt_wq);
281 printk(KERN_INFO "sllin_write_wakeup sent %d, wakeup\n", sl->tx_cnt);
284 /* Send a can_frame to a TTY queue. */
285 static netdev_tx_t sll_xmit(struct sk_buff *skb, struct net_device *dev)
287 struct sllin *sl = netdev_priv(dev);
288 struct can_frame *cf;
290 if (skb->len != sizeof(struct can_frame))
293 spin_lock(&sl->lock);
294 if (!netif_running(dev)) {
295 printk(KERN_WARNING "%s: xmit: iface is down\n", dev->name);
298 if (sl->tty == NULL) {
302 cf = (struct can_frame *) skb->data;
303 if (cf->can_id & SLLIN_CTRL_FRAME) {
304 sllin_configure_frame_cache(sl, cf);
305 goto free_out_unlock;
308 netif_stop_queue(sl->dev);
310 sl->tx_req_skb = skb;
311 set_bit(SLF_MSGEVENT, &sl->flags);
312 wake_up(&sl->kwt_wq);
313 spin_unlock(&sl->lock);
318 spin_unlock(&sl->lock);
325 /******************************************
326 * Routines looking at netdevice side.
327 ******************************************/
329 /* Netdevice UP -> DOWN routine */
330 static int sll_close(struct net_device *dev)
332 struct sllin *sl = netdev_priv(dev);
334 spin_lock_bh(&sl->lock);
336 /* TTY discipline is running. */
337 clear_bit(TTY_DO_WRITE_WAKEUP, &sl->tty->flags);
339 netif_stop_queue(dev);
342 spin_unlock_bh(&sl->lock);
347 /* Netdevice DOWN -> UP routine */
348 static int sll_open(struct net_device *dev)
350 struct sllin *sl = netdev_priv(dev);
352 pr_debug("sllin: %s() invoked\n", __FUNCTION__);
357 sl->flags &= (1 << SLF_INUSE);
358 netif_start_queue(dev);
362 /* Hook the destructor so we can free sllin devs at the right point in time */
363 static void sll_free_netdev(struct net_device *dev)
365 int i = dev->base_addr;
367 sllin_devs[i] = NULL;
370 static const struct net_device_ops sll_netdev_ops = {
371 .ndo_open = sll_open,
372 .ndo_stop = sll_close,
373 .ndo_start_xmit = sll_xmit,
376 static void sll_setup(struct net_device *dev)
378 dev->netdev_ops = &sll_netdev_ops;
379 dev->destructor = sll_free_netdev;
381 dev->hard_header_len = 0;
383 dev->tx_queue_len = 10;
385 dev->mtu = sizeof(struct can_frame);
386 dev->type = ARPHRD_CAN;
388 /* New-style flags. */
389 dev->flags = IFF_NOARP;
390 dev->features = NETIF_F_NO_CSUM;
393 /******************************************
394 Routines looking at TTY side.
395 ******************************************/
398 * Handle the 'receiver data ready' interrupt.
399 * This function is called by the 'tty_io' module in the kernel when
400 * a block of SLLIN data has been received, which can now be decapsulated
401 * and sent on to some IP layer for further processing. This will not
402 * be re-entered while running but other ldisc functions may be called
406 static void sllin_receive_buf(struct tty_struct *tty,
407 const unsigned char *cp, char *fp, int count)
409 struct sllin *sl = (struct sllin *) tty->disc_data;
411 printk(KERN_INFO "sllin_receive_buf invoked\n");
413 if (!sl || sl->magic != SLLIN_MAGIC || !netif_running(sl->dev))
416 /* Read the characters out of the buffer */
419 if (!test_and_set_bit(SLF_ERROR, &sl->flags))
420 sl->dev->stats.rx_errors++;
421 printk(KERN_INFO "sllin_receive_buf char 0x%02x ignored "
422 "due marker 0x%02x, flags 0x%lx\n",
423 *cp, *(fp-1), sl->flags);
428 if (sl->rx_cnt < SLLIN_BUFF_LEN) {
429 #ifndef BREAK_BY_BAUD
430 /* We didn't receive Break character */
431 if ((sl->rx_cnt == SLLIN_BUFF_BREAK) && (*cp == 0x55)) {
432 sl->rx_buff[sl->rx_cnt++] = 0x00;
435 printk(KERN_INFO "LIN_RX[%d]: 0x%02x\n", sl->rx_cnt, *cp);
436 sl->rx_buff[sl->rx_cnt++] = *cp++;
440 if (sl->rx_cnt >= sl->rx_expect) {
441 set_bit(SLF_RXEVENT, &sl->flags);
442 wake_up(&sl->kwt_wq);
443 printk(KERN_INFO "sllin_receive_buf count %d, wakeup\n", sl->rx_cnt);
445 printk(KERN_INFO "sllin_receive_buf count %d, waiting\n", sl->rx_cnt);
449 /*****************************************
450 * sllin message helper routines
451 *****************************************/
452 void sllin_report_error(struct sllin *sl, int err)
454 sllin_send_canfr(sl, 0 | CAN_EFF_FLAG |
455 (err & ~SLLIN_ID_MASK), NULL, 0);
458 static int sllin_configure_frame_cache(struct sllin *sl, struct can_frame *cf)
460 struct sllin_conf_entry *sce;
461 if (!(cf->can_id & SLLIN_LIN_ID_CONF))
464 sce = &sl->linfr_cache[cf->can_id & SLLIN_ID_MASK];
465 printk(KERN_INFO "Setting frame cache with EFF CAN frame. "
466 "LIN ID = %d\n", cf->can_id & SLLIN_ID_MASK);
468 sce->dlc = cf->can_dlc;
469 if (sce->dlc > SLLIN_DATA_MAX)
470 sce->dlc = SLLIN_DATA_MAX;
472 sce->frame_fl = (cf->can_id & ~SLLIN_ID_MASK) & CAN_EFF_MASK;
473 memcpy(sce->data, cf->data, cf->can_dlc);
478 static inline unsigned sllin_checksum(unsigned char *data, int length, int enhanced_fl)
489 for (; i < length; i++) {
498 #define SLLIN_STPMSG_RESPONLY (1) /* Message will be LIN Response only */
499 #define SLLIN_STPMSG_CHCKSUM_CLS (1 << 1)
500 #define SLLIN_STPMSG_CHCKSUM_ENH (1 << 2)
502 int sllin_setup_msg(struct sllin *sl, int mode, int id,
503 unsigned char *data, int len)
505 if (id > SLLIN_ID_MASK)
508 if (!(mode & SLLIN_STPMSG_RESPONLY)) {
512 sl->rx_lim = SLLIN_BUFF_LEN;
515 sl->tx_buff[SLLIN_BUFF_BREAK] = 0;
516 sl->tx_buff[SLLIN_BUFF_SYNC] = 0x55;
517 sl->tx_buff[SLLIN_BUFF_ID] = id | sllin_id_parity_table[id];
518 sl->tx_lim = SLLIN_BUFF_DATA;
520 if ((data != NULL) && len) {
522 memcpy(sl->tx_buff + SLLIN_BUFF_DATA, data, len);
523 sl->tx_buff[sl->tx_lim++] = sllin_checksum(sl->tx_buff,
524 sl->tx_lim, mode & SLLIN_STPMSG_CHCKSUM_ENH);
527 sl->rx_lim = SLLIN_BUFF_DATA + len + 1;
533 int sllin_send_tx_buff(struct sllin *sl)
535 struct tty_struct *tty = sl->tty;
540 if (sl->lin_state != SLSTATE_BREAK_SENT)
541 remains = sl->tx_lim - sl->tx_cnt;
545 remains = sl->tx_lim - sl->tx_cnt;
548 res = tty->ops->write(tty, sl->tx_buff + sl->tx_cnt, remains);
556 set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
557 res = tty->ops->write(tty, sl->tx_buff + sl->tx_cnt, remains);
559 clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
567 printk(KERN_INFO "sllin_send_tx_buff sent %d, remains %d\n",
568 sl->tx_cnt, remains);
574 int sllin_send_break(struct sllin *sl)
576 struct tty_struct *tty = sl->tty;
577 unsigned long break_baud;
580 break_baud = ((sl->lin_baud * 2) / 3);
581 sltty_change_speed(tty, break_baud);
583 tty->ops->flush_buffer(tty);
584 sl->rx_cnt = SLLIN_BUFF_BREAK;
586 sl->rx_expect = SLLIN_BUFF_BREAK + 1;
587 sl->lin_state = SLSTATE_BREAK_SENT;
589 res = sllin_send_tx_buff(sl);
591 sl->lin_state = SLSTATE_IDLE;
597 #else /* BREAK_BY_BAUD */
599 int sllin_send_break(struct sllin *sl)
601 struct tty_struct *tty = sl->tty;
603 unsigned long break_baud;
604 unsigned long usleep_range_min;
605 unsigned long usleep_range_max;
607 break_baud = ((sl->lin_baud * 2) / 3);
608 sl->rx_cnt = SLLIN_BUFF_BREAK;
609 sl->rx_expect = SLLIN_BUFF_BREAK + 1;
610 sl->lin_state = SLSTATE_BREAK_SENT;
612 /* Do the break ourselves; Inspired by
613 http://lxr.linux.no/#linux+v3.1.2/drivers/tty/tty_io.c#L2452 */
614 retval = tty->ops->break_ctl(tty, -1);
619 usleep_range_min = (1000000l * SLLIN_SAMPLES_PER_CHAR) / break_baud;
620 usleep_range_max = usleep_range_min + 50;
621 usleep_range(usleep_range_min, usleep_range_max);
623 retval = tty->ops->break_ctl(tty, 0);
624 usleep_range_min = (1000000l * 1 /* 1 bit */) / break_baud;
625 usleep_range_max = usleep_range_min + 30;
626 usleep_range(usleep_range_min, usleep_range_max);
628 tty->ops->flush_buffer(tty);
630 sl->tx_cnt = SLLIN_BUFF_SYNC;
632 printk(KERN_INFO "sllin: Break sent.\n");
633 set_bit(SLF_RXEVENT, &sl->flags);
634 wake_up(&sl->kwt_wq);
638 #endif /* BREAK_BY_BAUD */
641 static enum hrtimer_restart sllin_rx_timeout_handler(struct hrtimer *hrtimer)
643 struct sllin *sl = container_of(hrtimer, struct sllin, rx_timer);
645 sllin_report_error(sl, SLLIN_ERR_RX_TIMEOUT);
646 set_bit(SLF_TMOUTEVENT, &sl->flags);
647 wake_up(&sl->kwt_wq);
649 return HRTIMER_NORESTART;
652 static int sllin_rx_validate(struct sllin *sl)
657 unsigned char rec_chcksm = sl->rx_buff[sl->rx_cnt - 1];
658 struct sllin_conf_entry *scf;
660 actual_id = sl->rx_buff[SLLIN_BUFF_ID] & SLLIN_ID_MASK;
661 scf = &sl->linfr_cache[actual_id];
663 ext_chcks_fl = scf->frame_fl & SLLIN_CHECKSUM_EXTENDED;
665 if (sllin_checksum(sl->rx_buff, sl->rx_cnt - 1, ext_chcks_fl) !=
668 /* Type of checksum is configured for particular frame */
672 if (sllin_checksum(sl->rx_buff, sl->rx_cnt - 1,
673 !ext_chcks_fl) != rec_chcksm) {
682 /*****************************************
683 * sllin_kwthread - kernel worker thread
684 *****************************************/
686 int sllin_kwthread(void *ptr)
688 struct sllin *sl = (struct sllin *)ptr;
689 struct tty_struct *tty = sl->tty;
690 struct sched_param schparam = { .sched_priority = 40 };
691 int tx_bytes = 0; /* Used for Network statistics */
694 printk(KERN_INFO "sllin: sllin_kwthread started.\n");
695 sched_setscheduler(current, SCHED_FIFO, &schparam);
697 clear_bit(SLF_ERROR, &sl->flags);
698 sltty_change_speed(tty, sl->lin_baud);
700 while (!kthread_should_stop()) {
701 struct can_frame *cf;
704 u8 lin_data_buff[SLLIN_DATA_MAX];
707 if ((sl->lin_state == SLSTATE_IDLE) && sl->lin_master &&
709 if(sllin_send_break(sl) < 0) {
710 /* error processing */
714 wait_event_killable(sl->kwt_wq, kthread_should_stop() ||
715 test_bit(SLF_RXEVENT, &sl->flags) ||
716 test_bit(SLF_TXEVENT, &sl->flags) ||
717 test_bit(SLF_TMOUTEVENT, &sl->flags) ||
718 (((sl->lin_state == SLSTATE_IDLE) ||
719 (sl->lin_state == SLSTATE_RESPONSE_WAIT))
720 && test_bit(SLF_MSGEVENT, &sl->flags)));
722 if (test_and_clear_bit(SLF_RXEVENT, &sl->flags)) {
723 printk(KERN_INFO "sllin_kthread RXEVENT \n");
726 if (test_and_clear_bit(SLF_TXEVENT, &sl->flags)) {
727 printk(KERN_INFO "sllin_kthread TXEVENT \n");
730 if (test_and_clear_bit(SLF_TMOUTEVENT, &sl->flags)) {
731 printk(KERN_INFO "sllin_kthread TMOUTEVENT \n");
734 sl->rx_lim = sl->lin_master ? 0 : SLLIN_BUFF_LEN;
737 sl->id_to_send = false;
738 sl->data_to_send = false;
740 sl->lin_state = SLSTATE_IDLE;
743 switch (sl->lin_state) {
745 if (!test_bit(SLF_MSGEVENT, &sl->flags))
748 cf = (struct can_frame *)sl->tx_req_skb->data;
750 /* SFF RTR CAN frame -> LIN header */
751 if (cf->can_id & CAN_RTR_FLAG) {
752 spin_lock(&sl->lock);
753 printk(KERN_INFO "%s: RTR SFF CAN frame, ID = %x\n",
754 __FUNCTION__, cf->can_id & SLLIN_ID_MASK);
756 /* Is there Slave response in linfr_cache to be sent? */
757 if ((sl->linfr_cache[cf->can_id & SLLIN_ID_MASK].frame_fl &
758 SLLIN_LOC_SLAVE_CACHE)
759 && (sl->linfr_cache[cf->can_id & SLLIN_ID_MASK].dlc > 0)) {
761 printk(KERN_INFO "Sending LIN response from linfr_cache\n");
762 lin_data = sl->linfr_cache[cf->can_id & SLLIN_ID_MASK].data;
763 lin_dlc = sl->linfr_cache[cf->can_id & SLLIN_ID_MASK].dlc;
764 if (lin_dlc > SLLIN_DATA_MAX)
765 lin_dlc = SLLIN_DATA_MAX;
766 memcpy(lin_data_buff, lin_data, lin_dlc);
767 lin_data = lin_data_buff;
770 lin_dlc = sl->linfr_cache[cf->can_id & SLLIN_ID_MASK].dlc;
772 spin_unlock(&sl->lock);
773 } else { /* SFF NON-RTR CAN frame -> LIN header + LIN response */
774 printk(KERN_INFO "%s: NON-RTR SFF CAN frame, ID = %x\n",
775 __FUNCTION__, (int)cf->can_id & SLLIN_ID_MASK);
778 lin_dlc = cf->can_dlc;
779 if (lin_dlc > SLLIN_DATA_MAX)
780 lin_dlc = SLLIN_DATA_MAX;
784 if (sllin_setup_msg(sl, 0, cf->can_id & SLLIN_ID_MASK,
785 lin_data, lin_dlc) != -1) {
787 sl->id_to_send = true;
788 sl->data_to_send = (lin_data != NULL) ? true : false;
789 sl->resp_len_known = (lin_dlc > 0) ? true : false;
790 sl->dev->stats.tx_packets++;
791 sl->dev->stats.tx_bytes += tx_bytes;
794 clear_bit(SLF_MSGEVENT, &sl->flags);
795 kfree_skb(sl->tx_req_skb);
796 netif_wake_queue(sl->dev);
799 case SLSTATE_BREAK_SENT:
801 if (sl->rx_cnt <= SLLIN_BUFF_BREAK)
804 res = sltty_change_speed(tty, sl->lin_baud);
807 sl->lin_state = SLSTATE_ID_SENT;
808 sllin_send_tx_buff(sl);
811 case SLSTATE_ID_SENT:
812 sl->id_to_send = false;
813 if (sl->data_to_send) {
814 sllin_send_tx_buff(sl);
815 sl->lin_state = SLSTATE_RESPONSE_SENT;
816 sl->rx_expect = sl->tx_lim;
817 goto slstate_response_sent;
819 if (sl->resp_len_known) {
820 sl->rx_expect = sl->rx_lim;
822 sl->rx_expect = SLLIN_BUFF_DATA + 2;
824 sl->lin_state = SLSTATE_RESPONSE_WAIT;
825 /* If we don't receive anything, timer will "unblock" us */
826 hrtimer_start(&sl->rx_timer,
827 ktime_add(ktime_get(), sl->rx_timer_timeout),
829 goto slstate_response_wait;
833 case SLSTATE_RESPONSE_WAIT:
834 slstate_response_wait:
835 if (test_bit(SLF_MSGEVENT, &sl->flags)) {
836 unsigned char *lin_buff;
837 cf = (struct can_frame *)sl->tx_req_skb->data;
839 lin_buff = (sl->lin_master) ? sl->tx_buff : sl->rx_buff;
840 if (cf->can_id == (lin_buff[SLLIN_BUFF_ID] & SLLIN_ID_MASK)) {
841 if (sllin_setup_msg(sl, SLLIN_STPMSG_RESPONLY,
842 cf->can_id & SLLIN_ID_MASK,
843 cf->data, cf->can_dlc) != -1) {
845 sl->rx_expect = sl->tx_lim;
846 sl->data_to_send = true;
847 sl->dev->stats.tx_packets++;
848 sl->dev->stats.tx_bytes += tx_bytes;
850 if (!sl->lin_master) {
851 sl->tx_cnt = SLLIN_BUFF_DATA;
854 sllin_send_tx_buff(sl);
855 clear_bit(SLF_MSGEVENT, &sl->flags);
856 kfree_skb(sl->tx_req_skb);
857 netif_wake_queue(sl->dev);
859 sl->lin_state = SLSTATE_RESPONSE_SENT;
860 goto slstate_response_sent;
863 sl->lin_state = SLSTATE_RESPONSE_WAIT_BUS;
867 case SLSTATE_RESPONSE_WAIT_BUS:
868 if (sl->rx_cnt < sl->rx_expect)
871 hrtimer_cancel(&sl->rx_timer);
872 printk(KERN_INFO "sllin: response received ID %d len %d\n",
873 sl->rx_buff[SLLIN_BUFF_ID], sl->rx_cnt - SLLIN_BUFF_DATA - 1);
875 if (sllin_rx_validate(sl) == -1) {
876 printk("sllin: RX validation failed.\n");
877 sllin_report_error(sl, SLLIN_ERR_CHECKSUM);
878 //FIXME tx_stat.err++
880 // send CAN non-RTR frame with data
881 printk(KERN_INFO "sllin: sending NON-RTR CAN"
882 "frame with LIN payload.");
883 sll_bump(sl); //send packet to the network layer
886 sl->id_to_send = false;
887 sl->lin_state = SLSTATE_IDLE;
890 case SLSTATE_RESPONSE_SENT:
891 slstate_response_sent:
892 if (sl->rx_cnt < sl->tx_lim)
895 sll_bump(sl); //send packet to the network layer
896 printk(KERN_INFO "sllin: response sent ID %d len %d\n",
897 sl->rx_buff[SLLIN_BUFF_ID], sl->rx_cnt - SLLIN_BUFF_DATA - 1);
899 sl->id_to_send = false;
900 sl->lin_state = SLSTATE_IDLE;
907 /* sl->dev->stats.tx_packets++; send frames statistic */
908 /* netif_wake_queue(sl->dev); allow next Tx packet arrival */
911 hrtimer_cancel(&sl->rx_timer);
912 printk(KERN_INFO "sllin: sllin_kwthread stopped.\n");
918 /************************************
919 * sllin_open helper routines.
920 ************************************/
922 /* Collect hanged up channels */
923 static void sll_sync(void)
926 struct net_device *dev;
929 for (i = 0; i < maxdev; i++) {
934 sl = netdev_priv(dev);
937 if (dev->flags & IFF_UP)
942 /* Find a free SLLIN channel, and link in this `tty' line. */
943 static struct sllin *sll_alloc(dev_t line)
946 struct net_device *dev = NULL;
949 if (sllin_devs == NULL)
950 return NULL; /* Master array missing ! */
952 for (i = 0; i < maxdev; i++) {
959 /* Sorry, too many, all slots in use */
964 sl = netdev_priv(dev);
965 if (test_bit(SLF_INUSE, &sl->flags)) {
966 unregister_netdevice(dev);
968 sllin_devs[i] = NULL;
974 sprintf(name, "sllin%d", i);
976 dev = alloc_netdev(sizeof(*sl), name, sll_setup);
982 sl = netdev_priv(dev);
983 /* Initialize channel control data */
984 sl->magic = SLLIN_MAGIC;
986 spin_lock_init(&sl->lock);
993 * Open the high-level part of the SLLIN channel.
994 * This function is called by the TTY module when the
995 * SLLIN line discipline is called for. Because we are
996 * sure the tty line exists, we only have to link it to
997 * a free SLLIN channel...
999 * Called in process context serialized from other ldisc calls.
1002 static int sllin_open(struct tty_struct *tty)
1006 pr_debug("sllin: %s() invoked\n", __FUNCTION__);
1008 if (!capable(CAP_NET_ADMIN))
1011 if (tty->ops->write == NULL)
1014 /* RTnetlink lock is misused here to serialize concurrent
1015 opens of sllin channels. There are better ways, but it is
1020 /* Collect hanged up channels. */
1023 sl = tty->disc_data;
1026 /* First make sure we're not already connected. */
1027 if (sl && sl->magic == SLLIN_MAGIC)
1030 /* OK. Find a free SLLIN channel to use. */
1032 sl = sll_alloc(tty_devnum(tty));
1037 tty->disc_data = sl;
1038 sl->line = tty_devnum(tty);
1040 if (!test_bit(SLF_INUSE, &sl->flags)) {
1041 /* Perform the low-level SLLIN initialization. */
1042 sl->lin_master = true;
1046 sl->rx_lim = sl->lin_master ? 0 : SLLIN_BUFF_LEN;
1049 sl->id_to_send = false;
1050 sl->data_to_send = false;
1052 sl->lin_baud = 19200;
1054 sl->lin_state = SLSTATE_IDLE;
1056 hrtimer_init(&sl->rx_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1057 sl->rx_timer.function = sllin_rx_timeout_handler;
1058 /* timeval_to_ktime(msg_head->ival1); */
1059 sl->rx_timer_timeout = ns_to_ktime(
1060 (1000000000l / sl->lin_baud) *
1061 SLLIN_SAMPLES_PER_CHAR * SLLIN_CHARS_TO_TIMEOUT);
1063 set_bit(SLF_INUSE, &sl->flags);
1065 init_waitqueue_head(&sl->kwt_wq);
1066 sl->kwthread = kthread_run(sllin_kwthread, sl, "sllin");
1067 if (sl->kwthread == NULL)
1070 err = register_netdevice(sl->dev);
1072 goto err_free_chan_and_thread;
1075 /* Done. We have linked the TTY line to a channel. */
1077 tty->receive_room = SLLIN_BUFF_LEN * 40; /* We don't flow control */
1079 /* TTY layer expects 0 on success */
1082 err_free_chan_and_thread:
1083 kthread_stop(sl->kwthread);
1084 sl->kwthread = NULL;
1088 tty->disc_data = NULL;
1089 clear_bit(SLF_INUSE, &sl->flags);
1094 /* Count references from TTY module */
1099 * Close down a SLLIN channel.
1100 * This means flushing out any pending queues, and then returning. This
1101 * call is serialized against other ldisc functions.
1103 * We also use this method for a hangup event.
1106 static void sllin_close(struct tty_struct *tty)
1108 struct sllin *sl = (struct sllin *) tty->disc_data;
1110 /* First make sure we're connected. */
1111 if (!sl || sl->magic != SLLIN_MAGIC || sl->tty != tty)
1114 kthread_stop(sl->kwthread);
1115 sl->kwthread = NULL;
1117 tty->disc_data = NULL;
1120 /* Flush network side */
1121 unregister_netdev(sl->dev);
1122 /* This will complete via sl_free_netdev */
1125 static int sllin_hangup(struct tty_struct *tty)
1131 /* Perform I/O control on an active SLLIN channel. */
1132 static int sllin_ioctl(struct tty_struct *tty, struct file *file,
1133 unsigned int cmd, unsigned long arg)
1135 struct sllin *sl = (struct sllin *) tty->disc_data;
1138 /* First make sure we're connected. */
1139 if (!sl || sl->magic != SLLIN_MAGIC)
1144 tmp = strlen(sl->dev->name) + 1;
1145 if (copy_to_user((void __user *)arg, sl->dev->name, tmp))
1153 return tty_mode_ioctl(tty, file, cmd, arg);
1157 static struct tty_ldisc_ops sll_ldisc = {
1158 .owner = THIS_MODULE,
1159 .magic = TTY_LDISC_MAGIC,
1162 .close = sllin_close,
1163 .hangup = sllin_hangup,
1164 .ioctl = sllin_ioctl,
1165 .receive_buf = sllin_receive_buf,
1166 .write_wakeup = sllin_write_wakeup,
1169 static int __init sllin_init(void)
1174 maxdev = 4; /* Sanity */
1177 printk(KERN_INFO "sllin: %d dynamic interface channels.\n", maxdev);
1179 sllin_devs = kzalloc(sizeof(struct net_device *)*maxdev, GFP_KERNEL);
1181 printk(KERN_ERR "sllin: can't allocate sllin device array!\n");
1185 /* Fill in our line protocol discipline, and register it */
1186 status = tty_register_ldisc(N_SLLIN, &sll_ldisc);
1188 printk(KERN_ERR "sllin: can't register line discipline\n");
1192 #ifdef BREAK_BY_BAUD
1193 printk(KERN_INFO "sllin: Break is generated by baud-rate change.");
1195 printk(KERN_INFO "sllin: Break is generated manually with tiny sleep.");
1201 static void __exit sllin_exit(void)
1204 struct net_device *dev;
1206 unsigned long timeout = jiffies + HZ;
1209 if (sllin_devs == NULL)
1212 /* First of all: check for active disciplines and hangup them.
1216 msleep_interruptible(100);
1219 for (i = 0; i < maxdev; i++) {
1220 dev = sllin_devs[i];
1223 sl = netdev_priv(dev);
1224 spin_lock_bh(&sl->lock);
1227 tty_hangup(sl->tty);
1229 spin_unlock_bh(&sl->lock);
1231 } while (busy && time_before(jiffies, timeout));
1233 /* FIXME: hangup is async so we should wait when doing this second
1236 for (i = 0; i < maxdev; i++) {
1237 dev = sllin_devs[i];
1240 sllin_devs[i] = NULL;
1242 sl = netdev_priv(dev);
1244 printk(KERN_ERR "%s: tty discipline still running\n",
1246 /* Intentionally leak the control block. */
1247 dev->destructor = NULL;
1250 unregister_netdev(dev);
1256 i = tty_unregister_ldisc(N_SLLIN);
1258 printk(KERN_ERR "sllin: can't unregister ldisc (err %d)\n", i);
1261 module_init(sllin_init);
1262 module_exit(sllin_exit);