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 * Idea: Oliver Hartkopp <oliver.hartkopp@volkswagen.de>
36 * Copyright: (c) 2011 Czech Technical University in Prague
37 * (c) 2011 Volkswagen Group Research
38 * Authors: Pavel Pisa <pisa@cmp.felk.cvut.cz>
39 * Rostislav Lisovy <lisovy@kormus.cz>
40 * Michal Sojka <sojkam1@fel.cvut.cz>
41 * Funded by: Volkswagen Group Research
44 #define DEBUG 1 /* Enables pr_debug() printouts */
46 #include <linux/module.h>
47 #include <linux/moduleparam.h>
49 #include <asm/system.h>
50 #include <linux/uaccess.h>
51 #include <linux/bitops.h>
52 #include <linux/string.h>
53 #include <linux/tty.h>
54 #include <linux/errno.h>
55 #include <linux/netdevice.h>
56 #include <linux/skbuff.h>
57 #include <linux/rtnetlink.h>
58 #include <linux/if_arp.h>
59 #include <linux/if_ether.h>
60 #include <linux/sched.h>
61 #include <linux/delay.h>
62 #include <linux/init.h>
63 #include <linux/can.h>
64 #include <linux/kthread.h>
65 #include <linux/hrtimer.h>
66 #include "linux/lin_bus.h"
68 /* Should be in include/linux/tty.h */
70 /* -------------------------------- */
72 static __initdata const char banner[] =
73 KERN_INFO "sllin: serial line LIN interface driver\n";
75 MODULE_ALIAS_LDISC(N_SLLIN);
76 MODULE_DESCRIPTION("serial line LIN interface");
77 MODULE_LICENSE("GPL");
78 MODULE_AUTHOR("Pavel Pisa <pisa@cmp.felk.cvut.cz>");
80 #define SLLIN_MAGIC 0x53CA
81 /* #define BREAK_BY_BAUD */
83 static int maxdev = 10; /* MAX number of SLLIN channels;
84 This can be overridden with
85 insmod sllin.ko maxdev=nnn */
86 module_param(maxdev, int, 0);
87 MODULE_PARM_DESC(maxdev, "Maximum number of sllin interfaces");
89 /* maximum buffer len to store whole LIN message*/
90 #define SLLIN_DATA_MAX 8
91 #define SLLIN_BUFF_LEN (1 /*break*/ + 1 /*sync*/ + 1 /*ID*/ + \
92 SLLIN_DATA_MAX + 1 /*checksum*/)
93 #define SLLIN_BUFF_BREAK 0
94 #define SLLIN_BUFF_SYNC 1
95 #define SLLIN_BUFF_ID 2
96 #define SLLIN_BUFF_DATA 3
98 #define SLLIN_SAMPLES_PER_CHAR 10
99 #define SLLIN_CHARS_TO_TIMEOUT 24
105 SLSTATE_RESPONSE_WAIT, /* Wait for response */
106 SLSTATE_RESPONSE_WAIT_BUS, /* Wait for response from LIN bus
107 only (CAN frames from network stack
108 are not processed in this moment) */
109 SLSTATE_RESPONSE_SENT,
112 struct sllin_conf_entry {
113 int dlc; /* Length of data in LIN frame */
114 canid_t frame_fl; /* LIN frame flags. Passed from userspace as
116 u8 data[8]; /* LIN frame data payload */
122 /* Various fields. */
123 struct tty_struct *tty; /* ptr to TTY structure */
124 struct net_device *dev; /* easy for intr handling */
127 /* LIN message buffer and actual processed data counts */
128 unsigned char rx_buff[SLLIN_BUFF_LEN]; /* LIN Rx buffer */
129 unsigned char tx_buff[SLLIN_BUFF_LEN]; /* LIN Tx buffer */
130 int rx_expect; /* expected number of Rx chars */
131 int rx_lim; /* maximum Rx chars for current frame */
132 int rx_cnt; /* message buffer Rx fill level */
133 int tx_lim; /* actual limit of bytes to Tx */
134 int tx_cnt; /* number of already Tx bytes */
135 char lin_master; /* node is a master node */
136 int lin_baud; /* LIN baudrate */
137 int lin_state; /* state */
138 char id_to_send; /* there is ID to be sent */
139 char data_to_send; /* there are data to be sent */
140 char resp_len_known; /* Length of the response is known */
142 unsigned long flags; /* Flag values/ mode etc */
143 #define SLF_INUSE 0 /* Channel in use */
144 #define SLF_ERROR 1 /* Parity, etc. error */
145 #define SLF_RXEVENT 2 /* Rx wake event */
146 #define SLF_TXEVENT 3 /* Tx wake event */
147 #define SLF_MSGEVENT 4 /* CAN message to sent */
148 #define SLF_TMOUTEVENT 5 /* Timeout on received data */
151 struct task_struct *kwthread;
152 wait_queue_head_t kwt_wq; /* Wait queue used by kwthread */
153 struct hrtimer rx_timer; /* RX timeout timer */
154 ktime_t rx_timer_timeout; /* RX timeout timer value */
155 struct sk_buff *tx_req_skb; /* Socket buffer with CAN frame
156 received from network stack*/
158 /* List with configurations for each of 0 to LIN_ID_MAX LIN IDs */
159 struct sllin_conf_entry linfr_cache[LIN_ID_MAX + 1];
162 static struct net_device **sllin_devs;
163 static int sllin_configure_frame_cache(struct sllin *sl, struct can_frame *cf);
166 /* Values of two parity bits in LIN Protected
167 Identifier for each particular LIN ID */
168 const unsigned char sllin_id_parity_table[] = {
169 0x80, 0xc0, 0x40, 0x00, 0xc0, 0x80, 0x00, 0x40,
170 0x00, 0x40, 0xc0, 0x80, 0x40, 0x00, 0x80, 0xc0,
171 0x40, 0x00, 0x80, 0xc0, 0x00, 0x40, 0xc0, 0x80,
172 0xc0, 0x80, 0x00, 0x40, 0x80, 0xc0, 0x40, 0x00,
173 0x00, 0x40, 0xc0, 0x80, 0x40, 0x00, 0x80, 0xc0,
174 0x80, 0xc0, 0x40, 0x00, 0xc0, 0x80, 0x00, 0x40,
175 0xc0, 0x80, 0x00, 0x40, 0x80, 0xc0, 0x40, 0x00,
176 0x40, 0x00, 0x80, 0xc0, 0x00, 0x40, 0xc0, 0x80
180 * sltty_change_speed() -- Change baudrate of Serial device belonging
183 * @tty: Pointer to TTY to change speed for.
184 * @speed: Integer value of new speed. It is possible to
185 * assign non-standard values, i.e. those which
186 * are not defined in termbits.h.
188 static int sltty_change_speed(struct tty_struct *tty, unsigned speed)
190 struct ktermios old_termios;
193 mutex_lock(&tty->termios_mutex);
195 old_termios = *(tty->termios);
197 cflag = CS8 | CREAD | CLOCAL | HUPCL;
198 cflag &= ~(CBAUD | CIBAUD);
200 tty->termios->c_cflag = cflag;
201 tty->termios->c_oflag = 0;
202 tty->termios->c_lflag = 0;
204 /* Enable interrupt when UART-Break or Framing error received */
205 tty->termios->c_iflag = BRKINT | INPCK;
207 tty_encode_baud_rate(tty, speed, speed);
209 if (tty->ops->set_termios)
210 tty->ops->set_termios(tty, &old_termios);
212 mutex_unlock(&tty->termios_mutex);
217 /* Send one can_frame to the network layer */
218 static void sllin_send_canfr(struct sllin *sl, canid_t id, char *data, int len)
225 if (cf.can_dlc > 0) {
226 memcpy(&cf.data, data, cf.can_dlc);
229 skb = dev_alloc_skb(sizeof(struct can_frame));
234 skb->protocol = htons(ETH_P_CAN);
235 skb->pkt_type = PACKET_BROADCAST;
236 skb->ip_summed = CHECKSUM_UNNECESSARY;
237 memcpy(skb_put(skb, sizeof(struct can_frame)),
238 &cf, sizeof(struct can_frame));
241 sl->dev->stats.rx_packets++;
242 sl->dev->stats.rx_bytes += cf.can_dlc;
246 * sll_bump() -- Send data of received LIN frame (existing in sl->rx_buff)
251 static void sll_bump(struct sllin *sl)
253 sllin_send_canfr(sl, sl->rx_buff[SLLIN_BUFF_ID] & LIN_ID_MASK,
254 sl->rx_buff + SLLIN_BUFF_DATA,
255 sl->rx_cnt - SLLIN_BUFF_DATA - 1); /* without checksum */
259 * Called by the driver when there's room for more data. If we have
260 * more packets to send, we send them here.
262 static void sllin_write_wakeup(struct tty_struct *tty)
266 struct sllin *sl = (struct sllin *) tty->disc_data;
268 /* First make sure we're connected. */
269 if (!sl || sl->magic != SLLIN_MAGIC || !netif_running(sl->dev))
272 if (sl->lin_state != SLSTATE_BREAK_SENT)
273 remains = sl->tx_lim - sl->tx_cnt;
275 remains = SLLIN_BUFF_BREAK + 1 - sl->tx_cnt;
278 actual = tty->ops->write(tty, sl->tx_buff + sl->tx_cnt,
279 sl->tx_cnt - sl->tx_lim);
280 sl->tx_cnt += actual;
282 if (sl->tx_cnt < sl->tx_lim) {
283 pr_debug("sllin: sllin_write_wakeup sent %d, "
284 "remains %d, waiting\n",
285 sl->tx_cnt, sl->tx_lim - sl->tx_cnt);
290 clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
291 set_bit(SLF_TXEVENT, &sl->flags);
292 wake_up(&sl->kwt_wq);
294 pr_debug("sllin: sllin_write_wakeup sent %d, wakeup\n", sl->tx_cnt);
298 * sll_xmit() -- Send a can_frame to a TTY queue.
300 * @skb: Pointer to Socket buffer to be sent.
301 * @dev: Network device where @skb will be sent.
303 static netdev_tx_t sll_xmit(struct sk_buff *skb, struct net_device *dev)
305 struct sllin *sl = netdev_priv(dev);
306 struct can_frame *cf;
308 if (skb->len != sizeof(struct can_frame))
311 spin_lock(&sl->lock);
312 if (!netif_running(dev)) {
313 printk(KERN_WARNING "%s: xmit: iface is down\n", dev->name);
316 if (sl->tty == NULL) {
320 cf = (struct can_frame *) skb->data;
321 if (cf->can_id & LIN_CTRL_FRAME) {
322 sllin_configure_frame_cache(sl, cf);
323 goto free_out_unlock;
326 netif_stop_queue(sl->dev);
328 sl->tx_req_skb = skb;
329 set_bit(SLF_MSGEVENT, &sl->flags);
330 wake_up(&sl->kwt_wq);
331 spin_unlock(&sl->lock);
336 spin_unlock(&sl->lock);
343 /******************************************
344 * Routines looking at netdevice side.
345 ******************************************/
347 /* Netdevice UP -> DOWN routine */
348 static int sll_close(struct net_device *dev)
350 struct sllin *sl = netdev_priv(dev);
352 spin_lock_bh(&sl->lock);
354 /* TTY discipline is running. */
355 clear_bit(TTY_DO_WRITE_WAKEUP, &sl->tty->flags);
357 netif_stop_queue(dev);
360 spin_unlock_bh(&sl->lock);
365 /* Netdevice DOWN -> UP routine */
366 static int sll_open(struct net_device *dev)
368 struct sllin *sl = netdev_priv(dev);
370 pr_debug("sllin: %s() invoked\n", __FUNCTION__);
375 sl->flags &= (1 << SLF_INUSE);
376 netif_start_queue(dev);
380 /* Hook the destructor so we can free sllin devs at the right point in time */
381 static void sll_free_netdev(struct net_device *dev)
383 int i = dev->base_addr;
385 sllin_devs[i] = NULL;
388 static const struct net_device_ops sll_netdev_ops = {
389 .ndo_open = sll_open,
390 .ndo_stop = sll_close,
391 .ndo_start_xmit = sll_xmit,
394 static void sll_setup(struct net_device *dev)
396 dev->netdev_ops = &sll_netdev_ops;
397 dev->destructor = sll_free_netdev;
399 dev->hard_header_len = 0;
401 dev->tx_queue_len = 10;
403 dev->mtu = sizeof(struct can_frame);
404 dev->type = ARPHRD_CAN;
406 /* New-style flags. */
407 dev->flags = IFF_NOARP;
408 dev->features = NETIF_F_NO_CSUM;
411 /******************************************
412 Routines looking at TTY side.
413 ******************************************/
415 static void sllin_receive_buf(struct tty_struct *tty,
416 const unsigned char *cp, char *fp, int count)
418 struct sllin *sl = (struct sllin *) tty->disc_data;
420 pr_debug("sllin: sllin_receive_buf invoked\n");
422 if (!sl || sl->magic != SLLIN_MAGIC || !netif_running(sl->dev))
425 /* Read the characters out of the buffer */
428 if (sl->rx_cnt > SLLIN_BUFF_BREAK) {
429 set_bit(SLF_ERROR, &sl->flags);
431 pr_debug("sllin: sllin_receive_buf char 0x%02x ignored "
432 "due marker 0x%02x, flags 0x%lx\n",
433 *cp, *(fp-1), sl->flags);
435 if (sl->lin_master == true) {
436 wake_up(&sl->kwt_wq);
445 if (sl->rx_cnt < SLLIN_BUFF_LEN) {
446 #ifndef BREAK_BY_BAUD
447 /* We didn't receive Break character */
448 if ((sl->rx_cnt == SLLIN_BUFF_BREAK) && (*cp == 0x55)) {
449 sl->rx_buff[sl->rx_cnt++] = 0x00;
452 pr_debug("sllin: LIN_RX[%d]: 0x%02x\n", sl->rx_cnt, *cp);
453 sl->rx_buff[sl->rx_cnt++] = *cp++;
457 if (sl->rx_cnt >= sl->rx_expect) {
458 set_bit(SLF_RXEVENT, &sl->flags);
459 wake_up(&sl->kwt_wq);
460 pr_debug("sllin: sllin_receive_buf count %d, wakeup\n", sl->rx_cnt);
462 pr_debug("sllin: sllin_receive_buf count %d, waiting\n", sl->rx_cnt);
466 /*****************************************
467 * sllin message helper routines
468 *****************************************/
470 * sllin_report_error() -- Report an error by sending CAN frame
471 * with particular error flag set in can_id
474 * @err: Error flag to be sent.
476 void sllin_report_error(struct sllin *sl, int err)
479 case LIN_ERR_CHECKSUM:
480 sl->dev->stats.rx_crc_errors++;
483 case LIN_ERR_RX_TIMEOUT:
484 sl->dev->stats.rx_errors++;
487 case LIN_ERR_FRAMING:
488 sl->dev->stats.rx_frame_errors++;
492 sllin_send_canfr(sl, 0 | CAN_EFF_FLAG |
493 (err & ~LIN_ID_MASK), NULL, 0);
497 * sllin_configure_frame_cache() -- Configure particular entry in linfr_cache
500 * @cf: Pointer to CAN frame sent to this driver
501 * holding configuration information
503 static int sllin_configure_frame_cache(struct sllin *sl, struct can_frame *cf)
505 struct sllin_conf_entry *sce;
506 if (!(cf->can_id & LIN_ID_CONF))
509 sce = &sl->linfr_cache[cf->can_id & LIN_ID_MASK];
510 pr_debug("sllin: Setting frame cache with EFF CAN frame. "
511 "LIN ID = %d\n", cf->can_id & LIN_ID_MASK);
513 sce->dlc = cf->can_dlc;
514 if (sce->dlc > SLLIN_DATA_MAX)
515 sce->dlc = SLLIN_DATA_MAX;
517 sce->frame_fl = (cf->can_id & ~LIN_ID_MASK) & CAN_EFF_MASK;
518 memcpy(sce->data, cf->data, cf->can_dlc);
524 * sllin_checksum() -- Count checksum for particular data
526 * @data: Pointer to the buffer containing whole LIN
527 * frame (i.e. including break and sync bytes).
528 * @length: Length of the buffer.
529 * @enhanced_fl: Flag determining whether Enhanced or Classic
530 * checksum should be counted.
532 static inline unsigned sllin_checksum(unsigned char *data, int length, int enhanced_fl)
543 for (; i < length; i++) {
552 #define SLLIN_STPMSG_RESPONLY (1) /* Message will be LIN Response only */
553 #define SLLIN_STPMSG_CHCKSUM_CLS (1 << 1)
554 #define SLLIN_STPMSG_CHCKSUM_ENH (1 << 2)
556 int sllin_setup_msg(struct sllin *sl, int mode, int id,
557 unsigned char *data, int len)
559 if (id > LIN_ID_MASK)
562 if (!(mode & SLLIN_STPMSG_RESPONLY)) {
566 sl->rx_lim = SLLIN_BUFF_LEN;
569 sl->tx_buff[SLLIN_BUFF_BREAK] = 0;
570 sl->tx_buff[SLLIN_BUFF_SYNC] = 0x55;
571 sl->tx_buff[SLLIN_BUFF_ID] = id | sllin_id_parity_table[id];
572 sl->tx_lim = SLLIN_BUFF_DATA;
574 if ((data != NULL) && len) {
576 memcpy(sl->tx_buff + SLLIN_BUFF_DATA, data, len);
577 sl->tx_buff[sl->tx_lim++] = sllin_checksum(sl->tx_buff,
578 sl->tx_lim, mode & SLLIN_STPMSG_CHCKSUM_ENH);
581 sl->rx_lim = SLLIN_BUFF_DATA + len + 1;
586 static void sllin_reset_buffs(struct sllin *sl)
590 sl->rx_lim = sl->lin_master ? 0 : SLLIN_BUFF_LEN;
593 sl->id_to_send = false;
594 sl->data_to_send = false;
597 int sllin_send_tx_buff(struct sllin *sl)
599 struct tty_struct *tty = sl->tty;
604 if (sl->lin_state != SLSTATE_BREAK_SENT)
605 remains = sl->tx_lim - sl->tx_cnt;
609 remains = sl->tx_lim - sl->tx_cnt;
612 res = tty->ops->write(tty, sl->tx_buff + sl->tx_cnt, remains);
620 set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
621 res = tty->ops->write(tty, sl->tx_buff + sl->tx_cnt, remains);
623 clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
631 pr_debug("sllin: sllin_send_tx_buff sent %d, remains %d\n",
632 sl->tx_cnt, remains);
638 int sllin_send_break(struct sllin *sl)
640 struct tty_struct *tty = sl->tty;
641 unsigned long break_baud;
644 break_baud = ((sl->lin_baud * 2) / 3);
645 sltty_change_speed(tty, break_baud);
647 tty->ops->flush_buffer(tty);
648 sl->rx_cnt = SLLIN_BUFF_BREAK;
650 sl->rx_expect = SLLIN_BUFF_BREAK + 1;
651 sl->lin_state = SLSTATE_BREAK_SENT;
653 res = sllin_send_tx_buff(sl);
655 sl->lin_state = SLSTATE_IDLE;
661 #else /* BREAK_BY_BAUD */
663 int sllin_send_break(struct sllin *sl)
665 struct tty_struct *tty = sl->tty;
667 unsigned long break_baud;
668 unsigned long usleep_range_min;
669 unsigned long usleep_range_max;
671 break_baud = ((sl->lin_baud * 2) / 3);
672 sl->rx_cnt = SLLIN_BUFF_BREAK;
673 sl->rx_expect = SLLIN_BUFF_BREAK + 1;
674 sl->lin_state = SLSTATE_BREAK_SENT;
676 /* Do the break ourselves; Inspired by
677 http://lxr.linux.no/#linux+v3.1.2/drivers/tty/tty_io.c#L2452 */
678 retval = tty->ops->break_ctl(tty, -1);
683 usleep_range_min = (1000000l * SLLIN_SAMPLES_PER_CHAR) / break_baud;
684 usleep_range_max = usleep_range_min + 50;
685 usleep_range(usleep_range_min, usleep_range_max);
687 retval = tty->ops->break_ctl(tty, 0);
688 usleep_range_min = (1000000l * 1 /* 1 bit */) / break_baud;
689 usleep_range_max = usleep_range_min + 30;
690 usleep_range(usleep_range_min, usleep_range_max);
692 tty->ops->flush_buffer(tty);
694 sl->tx_cnt = SLLIN_BUFF_SYNC;
696 pr_debug("sllin: Break sent.\n");
697 set_bit(SLF_RXEVENT, &sl->flags);
698 wake_up(&sl->kwt_wq);
702 #endif /* BREAK_BY_BAUD */
705 static enum hrtimer_restart sllin_rx_timeout_handler(struct hrtimer *hrtimer)
707 struct sllin *sl = container_of(hrtimer, struct sllin, rx_timer);
709 sllin_report_error(sl, LIN_ERR_RX_TIMEOUT);
710 set_bit(SLF_TMOUTEVENT, &sl->flags);
711 wake_up(&sl->kwt_wq);
713 return HRTIMER_NORESTART;
717 * sllin_rx_validate() -- Validate received frame, i,e. check checksum
721 static int sllin_rx_validate(struct sllin *sl)
726 unsigned char rec_chcksm = sl->rx_buff[sl->rx_cnt - 1];
727 struct sllin_conf_entry *scf;
729 actual_id = sl->rx_buff[SLLIN_BUFF_ID] & LIN_ID_MASK;
730 scf = &sl->linfr_cache[actual_id];
732 ext_chcks_fl = scf->frame_fl & LIN_CHECKSUM_EXTENDED;
734 if (sllin_checksum(sl->rx_buff, sl->rx_cnt - 1, ext_chcks_fl) !=
737 /* Type of checksum is configured for particular frame */
741 if (sllin_checksum(sl->rx_buff, sl->rx_cnt - 1,
742 !ext_chcks_fl) != rec_chcksm) {
751 /*****************************************
752 * sllin_kwthread - kernel worker thread
753 *****************************************/
755 int sllin_kwthread(void *ptr)
757 struct sllin *sl = (struct sllin *)ptr;
758 struct tty_struct *tty = sl->tty;
759 struct sched_param schparam = { .sched_priority = 40 };
760 int tx_bytes = 0; /* Used for Network statistics */
763 pr_debug("sllin: sllin_kwthread started.\n");
764 sched_setscheduler(current, SCHED_FIFO, &schparam);
766 clear_bit(SLF_ERROR, &sl->flags);
767 sltty_change_speed(tty, sl->lin_baud);
769 while (!kthread_should_stop()) {
770 struct can_frame *cf;
773 u8 lin_data_buff[SLLIN_DATA_MAX];
776 if ((sl->lin_state == SLSTATE_IDLE) && sl->lin_master &&
778 if(sllin_send_break(sl) < 0) {
779 /* error processing */
783 wait_event_killable(sl->kwt_wq, kthread_should_stop() ||
784 test_bit(SLF_RXEVENT, &sl->flags) ||
785 test_bit(SLF_TXEVENT, &sl->flags) ||
786 test_bit(SLF_TMOUTEVENT, &sl->flags) ||
787 test_bit(SLF_ERROR, &sl->flags) ||
788 (((sl->lin_state == SLSTATE_IDLE) ||
789 (sl->lin_state == SLSTATE_RESPONSE_WAIT))
790 && test_bit(SLF_MSGEVENT, &sl->flags)));
792 if (test_and_clear_bit(SLF_RXEVENT, &sl->flags)) {
793 pr_debug("sllin: sllin_kthread RXEVENT\n");
796 if (test_and_clear_bit(SLF_ERROR, &sl->flags)) {
797 unsigned long usleep_range_min;
798 unsigned long usleep_range_max;
799 hrtimer_cancel(&sl->rx_timer);
800 pr_debug("sllin: sllin_kthread ERROR\n");
802 if (sl->lin_state != SLSTATE_IDLE)
803 sllin_report_error(sl, LIN_ERR_FRAMING);
805 usleep_range_min = (1000000l * SLLIN_SAMPLES_PER_CHAR * 10) /
807 usleep_range_max = usleep_range_min + 50;
808 usleep_range(usleep_range_min, usleep_range_max);
809 sllin_reset_buffs(sl);
810 sl->lin_state = SLSTATE_IDLE;
813 if (test_and_clear_bit(SLF_TXEVENT, &sl->flags)) {
814 pr_debug("sllin: sllin_kthread TXEVENT\n");
817 if (test_and_clear_bit(SLF_TMOUTEVENT, &sl->flags)) {
818 pr_debug("sllin: sllin_kthread TMOUTEVENT\n");
819 sllin_reset_buffs(sl);
821 sl->lin_state = SLSTATE_IDLE;
824 switch (sl->lin_state) {
826 if (!test_bit(SLF_MSGEVENT, &sl->flags))
829 cf = (struct can_frame *)sl->tx_req_skb->data;
831 /* SFF RTR CAN frame -> LIN header */
832 if (cf->can_id & CAN_RTR_FLAG) {
833 spin_lock(&sl->lock);
834 pr_debug("sllin: %s: RTR SFF CAN frame, ID = %x\n",
835 __FUNCTION__, cf->can_id & LIN_ID_MASK);
837 /* Is there Slave response in linfr_cache to be sent? */
838 if ((sl->linfr_cache[cf->can_id & LIN_ID_MASK].frame_fl &
840 && (sl->linfr_cache[cf->can_id & LIN_ID_MASK].dlc > 0)) {
842 pr_debug("sllin: Sending LIN response from linfr_cache\n");
843 lin_data = sl->linfr_cache[cf->can_id & LIN_ID_MASK].data;
844 lin_dlc = sl->linfr_cache[cf->can_id & LIN_ID_MASK].dlc;
845 if (lin_dlc > SLLIN_DATA_MAX)
846 lin_dlc = SLLIN_DATA_MAX;
847 memcpy(lin_data_buff, lin_data, lin_dlc);
848 lin_data = lin_data_buff;
851 lin_dlc = sl->linfr_cache[cf->can_id & LIN_ID_MASK].dlc;
853 spin_unlock(&sl->lock);
854 } else { /* SFF NON-RTR CAN frame -> LIN header + LIN response */
855 pr_debug("sllin: %s: NON-RTR SFF CAN frame, ID = %x\n",
856 __FUNCTION__, (int)cf->can_id & LIN_ID_MASK);
859 lin_dlc = cf->can_dlc;
860 if (lin_dlc > SLLIN_DATA_MAX)
861 lin_dlc = SLLIN_DATA_MAX;
865 if (sllin_setup_msg(sl, 0, cf->can_id & LIN_ID_MASK,
866 lin_data, lin_dlc) != -1) {
868 sl->id_to_send = true;
869 sl->data_to_send = (lin_data != NULL) ? true : false;
870 sl->resp_len_known = (lin_dlc > 0) ? true : false;
871 sl->dev->stats.tx_packets++;
872 sl->dev->stats.tx_bytes += tx_bytes;
875 clear_bit(SLF_MSGEVENT, &sl->flags);
876 kfree_skb(sl->tx_req_skb);
877 netif_wake_queue(sl->dev);
878 hrtimer_start(&sl->rx_timer,
879 ktime_add(ktime_get(), sl->rx_timer_timeout),
883 case SLSTATE_BREAK_SENT:
885 if (sl->rx_cnt <= SLLIN_BUFF_BREAK)
888 res = sltty_change_speed(tty, sl->lin_baud);
891 sl->lin_state = SLSTATE_ID_SENT;
892 sllin_send_tx_buff(sl);
895 case SLSTATE_ID_SENT:
896 hrtimer_cancel(&sl->rx_timer);
897 sl->id_to_send = false;
898 if (sl->data_to_send) {
899 sllin_send_tx_buff(sl);
900 sl->lin_state = SLSTATE_RESPONSE_SENT;
901 sl->rx_expect = sl->tx_lim;
902 goto slstate_response_sent;
904 if (sl->resp_len_known) {
905 sl->rx_expect = sl->rx_lim;
907 sl->rx_expect = SLLIN_BUFF_DATA + 2;
909 sl->lin_state = SLSTATE_RESPONSE_WAIT;
910 /* If we don't receive anything, timer will "unblock" us */
911 hrtimer_start(&sl->rx_timer,
912 ktime_add(ktime_get(), sl->rx_timer_timeout),
914 goto slstate_response_wait;
918 case SLSTATE_RESPONSE_WAIT:
919 slstate_response_wait:
920 if (test_bit(SLF_MSGEVENT, &sl->flags)) {
921 unsigned char *lin_buff;
922 cf = (struct can_frame *)sl->tx_req_skb->data;
924 lin_buff = (sl->lin_master) ? sl->tx_buff : sl->rx_buff;
925 if (cf->can_id == (lin_buff[SLLIN_BUFF_ID] & LIN_ID_MASK)) {
926 hrtimer_cancel(&sl->rx_timer);
927 pr_debug("sllin: received LIN response in a CAN frame.\n");
928 if (sllin_setup_msg(sl, SLLIN_STPMSG_RESPONLY,
929 cf->can_id & LIN_ID_MASK,
930 cf->data, cf->can_dlc) != -1) {
932 sl->rx_expect = sl->tx_lim;
933 sl->data_to_send = true;
934 sl->dev->stats.tx_packets++;
935 sl->dev->stats.tx_bytes += tx_bytes;
937 if (!sl->lin_master) {
938 sl->tx_cnt = SLLIN_BUFF_DATA;
941 sllin_send_tx_buff(sl);
942 clear_bit(SLF_MSGEVENT, &sl->flags);
943 kfree_skb(sl->tx_req_skb);
944 netif_wake_queue(sl->dev);
946 sl->lin_state = SLSTATE_RESPONSE_SENT;
947 goto slstate_response_sent;
950 sl->lin_state = SLSTATE_RESPONSE_WAIT_BUS;
954 /* Be aware, no BREAK here */
955 case SLSTATE_RESPONSE_WAIT_BUS:
956 if (sl->rx_cnt < sl->rx_expect)
959 hrtimer_cancel(&sl->rx_timer);
960 pr_debug("sllin: response received ID %d len %d\n",
961 sl->rx_buff[SLLIN_BUFF_ID], sl->rx_cnt - SLLIN_BUFF_DATA - 1);
963 if (sllin_rx_validate(sl) == -1) {
964 pr_debug("sllin: RX validation failed.\n");
965 sllin_report_error(sl, LIN_ERR_CHECKSUM);
967 /* Send CAN non-RTR frame with data */
968 pr_debug("sllin: sending NON-RTR CAN"
969 "frame with LIN payload.");
970 sll_bump(sl); /* send packet to the network layer */
973 sl->id_to_send = false;
974 sl->lin_state = SLSTATE_IDLE;
977 case SLSTATE_RESPONSE_SENT:
978 slstate_response_sent:
979 if (sl->rx_cnt < sl->tx_lim)
982 hrtimer_cancel(&sl->rx_timer);
983 sll_bump(sl); /* send packet to the network layer */
984 pr_debug("sllin: response sent ID %d len %d\n",
985 sl->rx_buff[SLLIN_BUFF_ID], sl->rx_cnt - SLLIN_BUFF_DATA - 1);
987 sl->id_to_send = false;
988 sl->lin_state = SLSTATE_IDLE;
993 hrtimer_cancel(&sl->rx_timer);
994 pr_debug("sllin: sllin_kwthread stopped.\n");
1000 /************************************
1001 * sllin_open helper routines.
1002 ************************************/
1004 /* Collect hanged up channels */
1005 static void sll_sync(void)
1008 struct net_device *dev;
1011 for (i = 0; i < maxdev; i++) {
1012 dev = sllin_devs[i];
1016 sl = netdev_priv(dev);
1019 if (dev->flags & IFF_UP)
1024 /* Find a free SLLIN channel, and link in this `tty' line. */
1025 static struct sllin *sll_alloc(dev_t line)
1028 struct net_device *dev = NULL;
1031 if (sllin_devs == NULL)
1032 return NULL; /* Master array missing ! */
1034 for (i = 0; i < maxdev; i++) {
1035 dev = sllin_devs[i];
1041 /* Sorry, too many, all slots in use */
1046 sl = netdev_priv(dev);
1047 if (test_bit(SLF_INUSE, &sl->flags)) {
1048 unregister_netdevice(dev);
1050 sllin_devs[i] = NULL;
1055 char name[IFNAMSIZ];
1056 sprintf(name, "sllin%d", i);
1058 dev = alloc_netdev(sizeof(*sl), name, sll_setup);
1064 sl = netdev_priv(dev);
1065 /* Initialize channel control data */
1066 sl->magic = SLLIN_MAGIC;
1068 spin_lock_init(&sl->lock);
1069 sllin_devs[i] = dev;
1075 * Open the high-level part of the SLLIN channel.
1076 * This function is called by the TTY module when the
1077 * SLLIN line discipline is called for. Because we are
1078 * sure the tty line exists, we only have to link it to
1079 * a free SLLIN channel...
1081 * Called in process context serialized from other ldisc calls.
1084 static int sllin_open(struct tty_struct *tty)
1088 pr_debug("sllin: %s() invoked\n", __FUNCTION__);
1090 if (!capable(CAP_NET_ADMIN))
1093 if (tty->ops->write == NULL)
1096 /* RTnetlink lock is misused here to serialize concurrent
1097 opens of sllin channels. There are better ways, but it is
1102 /* Collect hanged up channels. */
1105 sl = tty->disc_data;
1108 /* First make sure we're not already connected. */
1109 if (sl && sl->magic == SLLIN_MAGIC)
1112 /* OK. Find a free SLLIN channel to use. */
1114 sl = sll_alloc(tty_devnum(tty));
1119 tty->disc_data = sl;
1120 sl->line = tty_devnum(tty);
1122 if (!test_bit(SLF_INUSE, &sl->flags)) {
1123 /* Perform the low-level SLLIN initialization. */
1124 sl->lin_master = true;
1126 sllin_reset_buffs(sl);
1128 sl->lin_baud = 19200;
1130 sl->lin_state = SLSTATE_IDLE;
1132 hrtimer_init(&sl->rx_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1133 sl->rx_timer.function = sllin_rx_timeout_handler;
1134 /* timeval_to_ktime(msg_head->ival1); */
1135 sl->rx_timer_timeout = ns_to_ktime(
1136 (1000000000l / sl->lin_baud) *
1137 SLLIN_SAMPLES_PER_CHAR * SLLIN_CHARS_TO_TIMEOUT);
1139 set_bit(SLF_INUSE, &sl->flags);
1141 init_waitqueue_head(&sl->kwt_wq);
1142 sl->kwthread = kthread_run(sllin_kwthread, sl, "sllin");
1143 if (sl->kwthread == NULL)
1146 err = register_netdevice(sl->dev);
1148 goto err_free_chan_and_thread;
1151 /* Done. We have linked the TTY line to a channel. */
1153 tty->receive_room = SLLIN_BUFF_LEN * 40; /* We don't flow control */
1155 /* TTY layer expects 0 on success */
1158 err_free_chan_and_thread:
1159 kthread_stop(sl->kwthread);
1160 sl->kwthread = NULL;
1164 tty->disc_data = NULL;
1165 clear_bit(SLF_INUSE, &sl->flags);
1170 /* Count references from TTY module */
1175 * Close down a SLLIN channel.
1176 * This means flushing out any pending queues, and then returning. This
1177 * call is serialized against other ldisc functions.
1179 * We also use this method for a hangup event.
1182 static void sllin_close(struct tty_struct *tty)
1184 struct sllin *sl = (struct sllin *) tty->disc_data;
1186 /* First make sure we're connected. */
1187 if (!sl || sl->magic != SLLIN_MAGIC || sl->tty != tty)
1190 kthread_stop(sl->kwthread);
1191 sl->kwthread = NULL;
1193 tty->disc_data = NULL;
1196 /* Flush network side */
1197 unregister_netdev(sl->dev);
1198 /* This will complete via sl_free_netdev */
1201 static int sllin_hangup(struct tty_struct *tty)
1207 /* Perform I/O control on an active SLLIN channel. */
1208 static int sllin_ioctl(struct tty_struct *tty, struct file *file,
1209 unsigned int cmd, unsigned long arg)
1211 struct sllin *sl = (struct sllin *) tty->disc_data;
1214 /* First make sure we're connected. */
1215 if (!sl || sl->magic != SLLIN_MAGIC)
1220 tmp = strlen(sl->dev->name) + 1;
1221 if (copy_to_user((void __user *)arg, sl->dev->name, tmp))
1229 return tty_mode_ioctl(tty, file, cmd, arg);
1233 static struct tty_ldisc_ops sll_ldisc = {
1234 .owner = THIS_MODULE,
1235 .magic = TTY_LDISC_MAGIC,
1238 .close = sllin_close,
1239 .hangup = sllin_hangup,
1240 .ioctl = sllin_ioctl,
1241 .receive_buf = sllin_receive_buf,
1242 .write_wakeup = sllin_write_wakeup,
1245 static int __init sllin_init(void)
1250 maxdev = 4; /* Sanity */
1253 pr_debug("sllin: %d dynamic interface channels.\n", maxdev);
1255 sllin_devs = kzalloc(sizeof(struct net_device *)*maxdev, GFP_KERNEL);
1257 printk(KERN_ERR "sllin: can't allocate sllin device array!\n");
1261 /* Fill in our line protocol discipline, and register it */
1262 status = tty_register_ldisc(N_SLLIN, &sll_ldisc);
1264 printk(KERN_ERR "sllin: can't register line discipline\n");
1268 #ifdef BREAK_BY_BAUD
1269 pr_debug("sllin: Break is generated by baud-rate change.");
1271 pr_debug("sllin: Break is generated manually with tiny sleep.");
1277 static void __exit sllin_exit(void)
1280 struct net_device *dev;
1282 unsigned long timeout = jiffies + HZ;
1285 if (sllin_devs == NULL)
1288 /* First of all: check for active disciplines and hangup them.
1292 msleep_interruptible(100);
1295 for (i = 0; i < maxdev; i++) {
1296 dev = sllin_devs[i];
1299 sl = netdev_priv(dev);
1300 spin_lock_bh(&sl->lock);
1303 tty_hangup(sl->tty);
1305 spin_unlock_bh(&sl->lock);
1307 } while (busy && time_before(jiffies, timeout));
1309 /* FIXME: hangup is async so we should wait when doing this second
1312 for (i = 0; i < maxdev; i++) {
1313 dev = sllin_devs[i];
1316 sllin_devs[i] = NULL;
1318 sl = netdev_priv(dev);
1320 printk(KERN_ERR "%s: tty discipline still running\n",
1322 /* Intentionally leak the control block. */
1323 dev->destructor = NULL;
1326 unregister_netdev(dev);
1332 i = tty_unregister_ldisc(N_SLLIN);
1334 printk(KERN_ERR "sllin: can't unregister ldisc (err %d)\n", i);
1337 module_init(sllin_init);
1338 module_exit(sllin_exit);