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 */
45 //#define SLLIN_LED_TRIGGER /* Enables led triggers */
47 #include <linux/module.h>
48 #include <linux/moduleparam.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/version.h>
67 #include "linux/lin_bus.h"
69 #if LINUX_VERSION_CODE >= KERNEL_VERSION(4,11,0)
70 #include <uapi/linux/sched/types.h>
73 /* Should be in include/linux/tty.h */
75 /* -------------------------------- */
77 #ifdef SLLIN_LED_TRIGGER
78 #define SLLIN_LED_NAME_SZ (IFNAMSIZ + 6)
79 #include <linux/leds.h>
81 enum sllin_led_event {
88 static unsigned long led_delay = 50;
89 module_param(led_delay, ulong, 0644);
90 MODULE_PARM_DESC(led_delay,
91 "blink delay time for activity leds (msecs, default: 50).");
92 #endif /* SLLIN_LED_TRIGGER */
94 static __initdata const char banner[] =
95 KERN_INFO "sllin: serial line LIN interface driver\n";
97 MODULE_ALIAS_LDISC(N_SLLIN);
98 MODULE_DESCRIPTION("serial line LIN interface");
99 MODULE_LICENSE("GPL");
100 MODULE_AUTHOR("Pavel Pisa <pisa@cmp.felk.cvut.cz>");
102 #define SLLIN_MAGIC 0x53CA
103 /* #define BREAK_BY_BAUD */
105 static bool master = true;
106 static int baudrate; /* Use LIN_DEFAULT_BAUDRATE when not set */
108 module_param(master, bool, 0444);
109 MODULE_PARM_DESC(master, "LIN interface is Master device");
110 module_param(baudrate, int, 0444);
111 MODULE_PARM_DESC(baudrate, "Baudrate of LIN interface");
113 static int maxdev = 10; /* MAX number of SLLIN channels;
114 This can be overridden with
115 insmod sllin.ko maxdev=nnn */
116 module_param(maxdev, int, 0444);
117 MODULE_PARM_DESC(maxdev, "Maximum number of sllin interfaces");
119 /* maximum buffer len to store whole LIN message*/
120 #define SLLIN_DATA_MAX 8
121 #define SLLIN_BUFF_LEN (1 /*break*/ + 1 /*sync*/ + 1 /*ID*/ + \
122 SLLIN_DATA_MAX + 1 /*checksum*/)
123 #define SLLIN_BUFF_BREAK 0
124 #define SLLIN_BUFF_SYNC 1
125 #define SLLIN_BUFF_ID 2
126 #define SLLIN_BUFF_DATA 3
128 #define SLLIN_SAMPLES_PER_CHAR 10
129 #define SLLIN_CHARS_TO_TIMEOUT 24
135 SLSTATE_RESPONSE_WAIT, /* Wait for response */
136 SLSTATE_RESPONSE_WAIT_BUS, /* Wait for response from LIN bus
137 only (CAN frames from network stack
138 are not processed in this moment) */
140 SLSTATE_RESPONSE_SENT,
143 struct sllin_conf_entry {
144 int dlc; /* Length of data in LIN frame */
145 canid_t frame_fl; /* LIN frame flags. Passed from userspace as
147 u8 data[8]; /* LIN frame data payload */
153 /* Various fields. */
154 struct tty_struct *tty; /* ptr to TTY structure */
155 struct net_device *dev; /* easy for intr handling */
158 /* LIN message buffer and actual processed data counts */
159 unsigned char rx_buff[SLLIN_BUFF_LEN]; /* LIN Rx buffer */
160 unsigned char tx_buff[SLLIN_BUFF_LEN]; /* LIN Tx buffer */
161 int rx_expect; /* expected number of Rx chars */
162 int rx_lim; /* maximum Rx chars for current frame */
163 int rx_cnt; /* message buffer Rx fill level */
164 int tx_lim; /* actual limit of bytes to Tx */
165 int tx_cnt; /* number of already Tx bytes */
166 char lin_master; /* node is a master node */
167 int lin_baud; /* LIN baudrate */
168 int lin_state; /* state */
169 char id_to_send; /* there is ID to be sent */
170 char data_to_send; /* there are data to be sent */
171 char resp_len_known; /* Length of the response is known */
172 char header_received;/* In Slave mode, set when header was already
174 char rx_len_unknown; /* We are not sure how much data will be sent to us --
175 we just guess the length */
177 unsigned long flags; /* Flag values/ mode etc */
178 #define SLF_INUSE 0 /* Channel in use */
179 #define SLF_ERROR 1 /* Parity, etc. error */
180 #define SLF_RXEVENT 2 /* Rx wake event */
181 #define SLF_TXEVENT 3 /* Tx wake event */
182 #define SLF_MSGEVENT 4 /* CAN message to sent */
183 #define SLF_TMOUTEVENT 5 /* Timeout on received data */
184 #define SLF_TXBUFF_RQ 6 /* Req. to send buffer to UART*/
185 #define SLF_TXBUFF_INPR 7 /* Above request in progress */
188 struct task_struct *kwthread;
189 wait_queue_head_t kwt_wq; /* Wait queue used by kwthread */
190 struct hrtimer rx_timer; /* RX timeout timer */
191 ktime_t rx_timer_timeout; /* RX timeout timer value */
192 struct sk_buff *tx_req_skb; /* Socket buffer with CAN frame
193 received from network stack*/
195 /* List with configurations for each of 0 to LIN_ID_MAX LIN IDs */
196 struct sllin_conf_entry linfr_cache[LIN_ID_MAX + 1];
197 spinlock_t linfr_lock; /* frame cache and buffers lock */
199 #ifdef SLLIN_LED_TRIGGER
200 struct led_trigger *tx_led_trig;
201 char tx_led_trig_name[SLLIN_LED_NAME_SZ];
202 struct led_trigger *rx_led_trig;
203 char rx_led_trig_name[SLLIN_LED_NAME_SZ];
204 struct led_trigger *rxtx_led_trig;
205 char rxtx_led_trig_name[SLLIN_LED_NAME_SZ];
209 static struct net_device **sllin_devs;
210 static int sllin_configure_frame_cache(struct sllin *sl, struct can_frame *cf);
211 static void sllin_slave_receive_buf(struct tty_struct *tty,
212 const unsigned char *cp, char *fp, int count);
213 static void sllin_master_receive_buf(struct tty_struct *tty,
214 const unsigned char *cp, char *fp, int count);
217 /* Values of two parity bits in LIN Protected
218 Identifier for each particular LIN ID */
219 const unsigned char sllin_id_parity_table[] = {
220 0x80, 0xc0, 0x40, 0x00, 0xc0, 0x80, 0x00, 0x40,
221 0x00, 0x40, 0xc0, 0x80, 0x40, 0x00, 0x80, 0xc0,
222 0x40, 0x00, 0x80, 0xc0, 0x00, 0x40, 0xc0, 0x80,
223 0xc0, 0x80, 0x00, 0x40, 0x80, 0xc0, 0x40, 0x00,
224 0x00, 0x40, 0xc0, 0x80, 0x40, 0x00, 0x80, 0xc0,
225 0x80, 0xc0, 0x40, 0x00, 0xc0, 0x80, 0x00, 0x40,
226 0xc0, 0x80, 0x00, 0x40, 0x80, 0xc0, 0x40, 0x00,
227 0x40, 0x00, 0x80, 0xc0, 0x00, 0x40, 0xc0, 0x80
230 #ifdef SLLIN_LED_TRIGGER
231 static void sllin_led_event(struct net_device *netdev, enum sllin_led_event event)
233 struct sllin *sl = netdev_priv(netdev);
236 case SLLIN_LED_EVENT_OPEN:
237 led_trigger_event(sl->tx_led_trig, LED_FULL);
238 led_trigger_event(sl->rx_led_trig, LED_FULL);
239 led_trigger_event(sl->rxtx_led_trig, LED_FULL);
241 case SLLIN_LED_EVENT_STOP:
242 led_trigger_event(sl->tx_led_trig, LED_OFF);
243 led_trigger_event(sl->rx_led_trig, LED_OFF);
244 led_trigger_event(sl->rxtx_led_trig, LED_OFF);
246 case SLLIN_LED_EVENT_TX:
248 led_trigger_blink_oneshot(sl->tx_led_trig,
249 &led_delay, &led_delay, 1);
250 led_trigger_blink_oneshot(sl->rxtx_led_trig,
251 &led_delay, &led_delay, 1);
254 case SLLIN_LED_EVENT_RX:
256 led_trigger_blink_oneshot(sl->rx_led_trig,
257 &led_delay, &led_delay, 1);
258 led_trigger_blink_oneshot(sl->rxtx_led_trig,
259 &led_delay, &led_delay, 1);
265 static void sllin_led_release(struct device *gendev, void *res)
267 struct sllin *sl = netdev_priv(to_net_dev(gendev));
269 led_trigger_unregister_simple(sl->tx_led_trig);
270 led_trigger_unregister_simple(sl->rx_led_trig);
271 led_trigger_unregister_simple(sl->rxtx_led_trig);
274 static void devm_sllin_led_init(struct net_device *netdev)
276 struct sllin *sl = netdev_priv(netdev);
279 res = devres_alloc(sllin_led_release, 0, GFP_KERNEL);
281 netdev_err(netdev, "cannot register LED triggers\n");
285 snprintf(sl->tx_led_trig_name, sizeof(sl->tx_led_trig_name),
286 "%s-tx", netdev->name);
287 snprintf(sl->rx_led_trig_name, sizeof(sl->rx_led_trig_name),
288 "%s-rx", netdev->name);
289 snprintf(sl->rxtx_led_trig_name, sizeof(sl->rxtx_led_trig_name),
290 "%s-rxtx", netdev->name);
292 led_trigger_register_simple(sl->tx_led_trig_name,
294 led_trigger_register_simple(sl->rx_led_trig_name,
296 led_trigger_register_simple(sl->rxtx_led_trig_name,
299 devres_add(&netdev->dev, res);
302 static struct sllin *netdev_priv_safe(struct net_device *dev)
306 if (sllin_devs == NULL)
309 for (i = 0; i < maxdev; ++i)
310 if (sllin_devs[i] == dev)
311 return netdev_priv(dev);
316 static int sllin_netdev_notifier_call(struct notifier_block *nb, unsigned long msg,
319 struct net_device *netdev = netdev_notifier_info_to_dev(ptr);
320 struct sllin *sl = netdev_priv_safe(netdev);
321 char name[SLLIN_LED_NAME_SZ];
326 if (!sl->tx_led_trig || !sl->rx_led_trig || !sl->rxtx_led_trig)
329 if (msg == NETDEV_CHANGENAME) {
330 snprintf(name, sizeof(name), "%s-tx", netdev->name);
331 led_trigger_rename_static(name, sl->tx_led_trig);
333 snprintf(name, sizeof(name), "%s-rx", netdev->name);
334 led_trigger_rename_static(name, sl->rx_led_trig);
336 snprintf(name, sizeof(name), "%s-rxtx", netdev->name);
337 led_trigger_rename_static(name, sl->rxtx_led_trig);
343 static struct notifier_block sllin_netdev_notifier __read_mostly = {
344 .notifier_call = sllin_netdev_notifier_call,
346 #endif /* SLLIN_LED_TRIGGER */
349 * sltty_change_speed() -- Change baudrate of Serial device belonging
352 * @tty: Pointer to TTY to change speed for.
353 * @speed: Integer value of new speed. It is possible to
354 * assign non-standard values, i.e. those which
355 * are not defined in termbits.h.
357 static int sltty_change_speed(struct tty_struct *tty, unsigned speed)
359 struct ktermios old_termios, termios;
362 #if LINUX_VERSION_CODE < KERNEL_VERSION(3, 12, 0)
363 mutex_lock(&tty->termios_mutex);
365 down_write(&tty->termios_rwsem);
368 #if LINUX_VERSION_CODE < KERNEL_VERSION(3, 7, 0)
369 old_termios = termios = *(tty->termios);
371 old_termios = termios = tty->termios;
374 cflag = CS8 | CREAD | CLOCAL | HUPCL;
375 cflag &= ~(CBAUD | CIBAUD);
377 termios.c_cflag = cflag;
381 /* Enable interrupt when UART-Break or Framing error received */
382 termios.c_iflag = BRKINT | INPCK;
383 #if LINUX_VERSION_CODE < KERNEL_VERSION(3, 7, 0)
384 *(tty->termios) = termios;
386 tty->termios = termios;
389 tty_encode_baud_rate(tty, speed, speed);
391 if (tty->ops->set_termios)
392 tty->ops->set_termios(tty, &old_termios);
394 #if LINUX_VERSION_CODE < KERNEL_VERSION(3, 12, 0)
395 mutex_unlock(&tty->termios_mutex);
397 up_write(&tty->termios_rwsem);
403 /* Send one can_frame to the network layer */
404 static void sllin_send_canfr(struct sllin *sl, canid_t id, char *data, int len)
412 memcpy(&cf.data, data, cf.can_dlc);
414 skb = dev_alloc_skb(sizeof(struct can_frame));
419 skb->protocol = htons(ETH_P_CAN);
420 skb->pkt_type = PACKET_BROADCAST;
421 skb->ip_summed = CHECKSUM_UNNECESSARY;
422 memcpy(skb_put(skb, sizeof(struct can_frame)),
423 &cf, sizeof(struct can_frame));
426 sl->dev->stats.rx_packets++;
427 sl->dev->stats.rx_bytes += cf.can_dlc;
429 #ifdef SLLIN_LED_TRIGGER
430 sllin_led_event(sl->dev, SLLIN_LED_EVENT_RX);
435 * sll_bump() -- Send data of received LIN frame (existing in sl->rx_buff)
440 static void sll_bump(struct sllin *sl)
442 int len = sl->rx_cnt - SLLIN_BUFF_DATA - 1; /* without checksum */
443 len = (len < 0) ? 0 : len;
445 sllin_send_canfr(sl, sl->rx_buff[SLLIN_BUFF_ID] & LIN_ID_MASK,
446 sl->rx_buff + SLLIN_BUFF_DATA, len);
449 static void sll_send_rtr(struct sllin *sl)
451 sllin_send_canfr(sl, (sl->rx_buff[SLLIN_BUFF_ID] & LIN_ID_MASK) |
452 CAN_RTR_FLAG, NULL, 0);
456 * Called by the driver when there's room for more data. If we have
457 * more packets to send, we send them here.
459 static void sllin_write_wakeup(struct tty_struct *tty)
463 struct sllin *sl = (struct sllin *) tty->disc_data;
465 /* First make sure we're connected. */
466 if (!sl || sl->magic != SLLIN_MAGIC || !netif_running(sl->dev))
469 set_bit(SLF_TXBUFF_RQ, &sl->flags);
471 if (unlikely(test_and_set_bit(SLF_TXBUFF_INPR, &sl->flags)))
472 return; /* ongoing concurrent processing */
474 clear_bit(SLF_TXBUFF_RQ, &sl->flags);
476 #if LINUX_VERSION_CODE < KERNEL_VERSION(3, 18, 0)
477 smp_mb__after_clear_bit();
479 smp_mb__after_atomic();
482 if (sl->lin_state != SLSTATE_BREAK_SENT)
483 remains = sl->tx_lim - sl->tx_cnt;
485 remains = SLLIN_BUFF_BREAK + 1 - sl->tx_cnt;
488 actual = tty->ops->write(tty, sl->tx_buff + sl->tx_cnt,
489 sl->tx_cnt - sl->tx_lim);
490 sl->tx_cnt += actual;
493 clear_bit(SLF_TXBUFF_INPR, &sl->flags);
494 #if LINUX_VERSION_CODE < KERNEL_VERSION(3, 18, 0)
495 smp_mb__after_clear_bit();
497 smp_mb__after_atomic();
500 } while (unlikely(test_bit(SLF_TXBUFF_RQ, &sl->flags)));
502 if ((remains > 0) && (actual >= 0)) {
503 netdev_dbg(sl->dev, "sllin_write_wakeup sent %d, remains %d, waiting\n",
504 sl->tx_cnt, sl->tx_lim - sl->tx_cnt);
508 clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
509 set_bit(SLF_TXEVENT, &sl->flags);
510 wake_up(&sl->kwt_wq);
512 netdev_dbg(sl->dev, "sllin_write_wakeup sent %d, wakeup\n", sl->tx_cnt);
516 * sll_xmit() -- Send a can_frame to a TTY queue.
518 * @skb: Pointer to Socket buffer to be sent.
519 * @dev: Network device where @skb will be sent.
521 static netdev_tx_t sll_xmit(struct sk_buff *skb, struct net_device *dev)
523 struct sllin *sl = netdev_priv(dev);
524 struct can_frame *cf;
526 if (skb->len != sizeof(struct can_frame))
529 spin_lock(&sl->lock);
530 if (!netif_running(dev)) {
531 netdev_warn(sl->dev, "xmit: iface is down\n");
534 if (sl->tty == NULL) {
535 netdev_warn(sl->dev, "xmit: no tty device connected\n");
539 cf = (struct can_frame *) skb->data;
540 if (cf->can_id & LIN_CTRL_FRAME) {
541 sllin_configure_frame_cache(sl, cf);
542 goto free_out_unlock;
545 netif_stop_queue(sl->dev);
547 sl->tx_req_skb = skb;
548 set_bit(SLF_MSGEVENT, &sl->flags);
549 wake_up(&sl->kwt_wq);
550 spin_unlock(&sl->lock);
555 spin_unlock(&sl->lock);
562 /******************************************
563 * Routines looking at netdevice side.
564 ******************************************/
566 /* Netdevice UP -> DOWN routine */
567 static int sll_close(struct net_device *dev)
569 struct sllin *sl = netdev_priv(dev);
571 spin_lock_bh(&sl->lock);
573 /* TTY discipline is running. */
574 clear_bit(TTY_DO_WRITE_WAKEUP, &sl->tty->flags);
576 netif_stop_queue(dev);
579 spin_unlock_bh(&sl->lock);
581 #ifdef SLLIN_LED_TRIGGER
582 sllin_led_event(dev, SLLIN_LED_EVENT_STOP);
587 /* Netdevice DOWN -> UP routine */
588 static int sll_open(struct net_device *dev)
590 struct sllin *sl = netdev_priv(dev);
592 netdev_dbg(sl->dev, "%s() invoked\n", __func__);
597 sl->flags &= (1 << SLF_INUSE);
598 netif_start_queue(dev);
600 #ifdef SLLIN_LED_TRIGGER
601 sllin_led_event(dev, SLLIN_LED_EVENT_OPEN);
606 /* Hook the destructor so we can free sllin devs at the right point in time */
607 static void sll_free_netdev(struct net_device *dev)
609 int i = dev->base_addr;
611 sllin_devs[i] = NULL;
614 static const struct net_device_ops sll_netdev_ops = {
615 .ndo_open = sll_open,
616 .ndo_stop = sll_close,
617 .ndo_start_xmit = sll_xmit,
620 static void sll_setup(struct net_device *dev)
622 dev->netdev_ops = &sll_netdev_ops;
623 #if LINUX_VERSION_CODE < KERNEL_VERSION(4,12,0)
624 dev->destructor = sll_free_netdev;
625 #else /* Linux 4.12.0+ */
626 dev->priv_destructor = sll_free_netdev;
627 #endif /* Linux 4.12.0+ */
629 dev->hard_header_len = 0;
631 dev->tx_queue_len = 10;
633 dev->mtu = sizeof(struct can_frame);
634 dev->type = ARPHRD_CAN;
636 /* New-style flags. */
637 dev->flags = IFF_NOARP;
638 dev->features = NETIF_F_HW_CSUM; /* NETIF_F_NO_CSUM;*/
641 /******************************************
642 Routines looking at TTY side.
643 ******************************************/
644 static void sllin_master_receive_buf(struct tty_struct *tty,
645 const unsigned char *cp, char *fp, int count)
647 struct sllin *sl = (struct sllin *) tty->disc_data;
649 /* Read the characters out of the buffer */
652 netdev_dbg(sl->dev, "sllin_master_receive_buf char 0x%02x ignored "
653 "due marker 0x%02x, flags 0x%lx\n",
654 *cp, *(fp-1), sl->flags);
656 /* i.e. Real error -- not Break */
657 if (sl->rx_cnt > SLLIN_BUFF_BREAK) {
658 set_bit(SLF_ERROR, &sl->flags);
659 wake_up(&sl->kwt_wq);
664 #ifndef BREAK_BY_BAUD
665 /* We didn't receive Break character -- fake it! */
666 if ((sl->rx_cnt == SLLIN_BUFF_BREAK) && (*cp == 0x55)) {
667 netdev_dbg(sl->dev, "LIN_RX[%d]: 0x00\n", sl->rx_cnt);
668 sl->rx_buff[sl->rx_cnt++] = 0x00;
670 #endif /* BREAK_BY_BAUD */
672 if (sl->rx_cnt < SLLIN_BUFF_LEN) {
673 netdev_dbg(sl->dev, "LIN_RX[%d]: 0x%02x\n", sl->rx_cnt, *cp);
674 sl->rx_buff[sl->rx_cnt++] = *cp++;
679 if (sl->rx_cnt >= sl->rx_expect) {
680 set_bit(SLF_RXEVENT, &sl->flags);
681 wake_up(&sl->kwt_wq);
682 netdev_dbg(sl->dev, "sllin_receive_buf count %d, wakeup\n", sl->rx_cnt);
684 netdev_dbg(sl->dev, "sllin_receive_buf count %d, waiting\n", sl->rx_cnt);
689 /*****************************************
690 * sllin message helper routines
691 *****************************************/
693 * sllin_report_error() -- Report an error by sending CAN frame
694 * with particular error flag set in can_id
697 * @err: Error flag to be sent.
699 static void sllin_report_error(struct sllin *sl, int err)
701 unsigned char *lin_buff;
705 case LIN_ERR_CHECKSUM:
706 sl->dev->stats.rx_crc_errors++;
709 case LIN_ERR_RX_TIMEOUT:
710 sl->dev->stats.rx_errors++;
713 case LIN_ERR_FRAMING:
714 sl->dev->stats.rx_frame_errors++;
718 lin_buff = (sl->lin_master) ? sl->tx_buff : sl->rx_buff;
719 lin_id = lin_buff[SLLIN_BUFF_ID] & LIN_ID_MASK;
720 sllin_send_canfr(sl, lin_id | CAN_EFF_FLAG |
721 (err & ~LIN_ID_MASK), NULL, 0);
725 * sllin_configure_frame_cache() -- Configure particular entry in linfr_cache
728 * @cf: Pointer to CAN frame sent to this driver
729 * holding configuration information
731 static int sllin_configure_frame_cache(struct sllin *sl, struct can_frame *cf)
734 struct sllin_conf_entry *sce;
736 if (!(cf->can_id & LIN_CTRL_FRAME))
739 sce = &sl->linfr_cache[cf->can_id & LIN_ID_MASK];
740 netdev_dbg(sl->dev, "Setting frame cache with EFF CAN frame. LIN ID = %d\n",
741 cf->can_id & LIN_ID_MASK);
743 spin_lock_irqsave(&sl->linfr_lock, flags);
745 sce->dlc = cf->can_dlc;
746 if (sce->dlc > SLLIN_DATA_MAX)
747 sce->dlc = SLLIN_DATA_MAX;
749 sce->frame_fl = (cf->can_id & ~LIN_ID_MASK) & CAN_EFF_MASK;
750 memcpy(sce->data, cf->data, cf->can_dlc);
752 spin_unlock_irqrestore(&sl->linfr_lock, flags);
758 * sllin_checksum() -- Count checksum for particular data
760 * @data: Pointer to the buffer containing whole LIN
761 * frame (i.e. including break and sync bytes).
762 * @length: Length of the buffer.
763 * @enhanced_fl: Flag determining whether Enhanced or Classic
764 * checksum should be counted.
766 static inline unsigned sllin_checksum(unsigned char *data, int length, int enhanced_fl)
776 for (; i < length; i++) {
785 #define SLLIN_STPMSG_RESPONLY (1) /* Message will be LIN Response only */
786 #define SLLIN_STPMSG_CHCKSUM_CLS (1 << 1)
787 #define SLLIN_STPMSG_CHCKSUM_ENH (1 << 2)
789 static int sllin_setup_msg(struct sllin *sl, int mode, int id,
790 unsigned char *data, int len)
792 if (id > LIN_ID_MASK)
795 if (!(mode & SLLIN_STPMSG_RESPONLY)) {
799 sl->rx_lim = SLLIN_BUFF_LEN;
802 sl->tx_buff[SLLIN_BUFF_BREAK] = 0;
803 sl->tx_buff[SLLIN_BUFF_SYNC] = 0x55;
804 sl->tx_buff[SLLIN_BUFF_ID] = id | sllin_id_parity_table[id];
805 sl->tx_lim = SLLIN_BUFF_DATA;
807 if ((data != NULL) && len) {
809 memcpy(sl->tx_buff + SLLIN_BUFF_DATA, data, len);
810 sl->tx_buff[sl->tx_lim] = sllin_checksum(sl->tx_buff,
811 sl->tx_lim, mode & SLLIN_STPMSG_CHCKSUM_ENH);
815 sl->rx_lim = SLLIN_BUFF_DATA + len + 1;
820 static void sllin_reset_buffs(struct sllin *sl)
824 sl->rx_lim = sl->lin_master ? 0 : SLLIN_BUFF_LEN;
827 sl->id_to_send = false;
828 sl->data_to_send = false;
832 * sllin_rx_validate() -- Validate received frame, i,e. check checksum
836 static int sllin_rx_validate(struct sllin *sl)
842 unsigned char rec_chcksm = sl->rx_buff[sl->rx_cnt - 1];
843 struct sllin_conf_entry *sce;
845 actual_id = sl->rx_buff[SLLIN_BUFF_ID] & LIN_ID_MASK;
846 sce = &sl->linfr_cache[actual_id];
848 spin_lock_irqsave(&sl->linfr_lock, flags);
850 ext_chcks_fl = sce->frame_fl & LIN_CHECKSUM_EXTENDED;
851 spin_unlock_irqrestore(&sl->linfr_lock, flags);
853 if (sllin_checksum(sl->rx_buff, sl->rx_cnt - 1, ext_chcks_fl) !=
856 /* Type of checksum is configured for particular frame */
860 if (sllin_checksum(sl->rx_buff, sl->rx_cnt - 1,
861 !ext_chcks_fl) != rec_chcksm) {
870 static void sllin_slave_finish_rx_msg(struct sllin *sl)
872 if (sllin_rx_validate(sl) == -1) {
873 netdev_dbg(sl->dev, "sllin: RX validation failed.\n");
874 sllin_report_error(sl, LIN_ERR_CHECKSUM);
876 /* Send CAN non-RTR frame with data */
877 netdev_dbg(sl->dev, "sllin: sending NON-RTR CAN frame with LIN payload.");
878 sll_bump(sl); /* send packet to the network layer */
880 /* Prepare for reception of new header */
882 sl->rx_expect = SLLIN_BUFF_ID + 1;
883 sl->rx_len_unknown = false; /* We do know exact length of the header */
884 sl->header_received = false;
887 static void sllin_slave_receive_buf(struct tty_struct *tty,
888 const unsigned char *cp, char *fp, int count)
890 struct sllin *sl = (struct sllin *) tty->disc_data;
892 struct sllin_conf_entry *sce;
895 /* Read the characters out of the buffer */
899 * If we don't know the length of the current message
900 * and received at least the LIN ID, we received here
901 * the break of the next message.
902 * Evaluate the previous one before continuing.
904 if ((sl->rx_len_unknown == true) &&
905 (sl->rx_cnt >= SLLIN_BUFF_ID))
907 hrtimer_cancel(&sl->rx_timer);
908 sllin_slave_finish_rx_msg(sl);
910 set_bit(SLF_RXEVENT, &sl->flags);
911 wake_up(&sl->kwt_wq);
914 netdev_dbg(sl->dev, "sllin_slave_receive_buf char 0x%02x ignored "
915 "due marker 0x%02x, flags 0x%lx\n",
916 *cp, *(fp-1), sl->flags);
920 sl->rx_expect = SLLIN_BUFF_ID + 1;
921 sl->rx_len_unknown = false; /* We do know exact length of the header */
922 sl->header_received = false;
925 if (sl->rx_cnt < SLLIN_BUFF_LEN) {
926 netdev_dbg(sl->dev, "LIN_RX[%d]: 0x%02x\n", sl->rx_cnt, *cp);
928 /* We did not receive break (0x00) character */
929 if ((sl->rx_cnt == SLLIN_BUFF_BREAK) && (*cp == 0x55)) {
930 sl->rx_buff[sl->rx_cnt++] = 0x00;
933 if (sl->rx_cnt == SLLIN_BUFF_SYNC) {
934 /* 'Duplicated' break character -- ignore */
940 /* Wrong sync character */
945 sl->rx_buff[sl->rx_cnt++] = *cp++;
948 /* Header received */
949 if ((sl->header_received == false) && (sl->rx_cnt >= (SLLIN_BUFF_ID + 1))) {
952 lin_id = sl->rx_buff[SLLIN_BUFF_ID] & LIN_ID_MASK;
953 sce = &sl->linfr_cache[lin_id];
955 spin_lock_irqsave(&sl->linfr_lock, flags);
957 sl->lin_state = SLSTATE_ID_RECEIVED;
958 /* Is the length of data set in frame cache? */
960 sl->rx_expect += sce->dlc + 1; /* + checksum */
961 sl->rx_len_unknown = false;
962 wake_up(&sl->kwt_wq);
964 sl->rx_expect += SLLIN_DATA_MAX + 1; /* + checksum */
965 sl->rx_len_unknown = true;
967 spin_unlock_irqrestore(&sl->linfr_lock, flags);
969 sl->header_received = true;
971 hrtimer_start(&sl->rx_timer,
972 ktime_add(ktime_get(), sl->rx_timer_timeout),
978 /* Response received */
979 if ((sl->header_received == true) &&
980 ((sl->rx_cnt >= sl->rx_expect))) {
982 hrtimer_cancel(&sl->rx_timer);
983 netdev_dbg(sl->dev, "Received LIN header & LIN response. "
984 "rx_cnt = %u, rx_expect = %u\n", sl->rx_cnt,
986 sllin_slave_finish_rx_msg(sl);
988 set_bit(SLF_RXEVENT, &sl->flags);
989 wake_up(&sl->kwt_wq);
994 static void sllin_receive_buf(struct tty_struct *tty,
995 const unsigned char *cp, char *fp, int count)
997 struct sllin *sl = (struct sllin *) tty->disc_data;
998 netdev_dbg(sl->dev, "sllin_receive_buf invoked, count = %u\n", count);
1000 if (!sl || sl->magic != SLLIN_MAGIC || !netif_running(sl->dev))
1004 sllin_master_receive_buf(tty, cp, fp, count);
1006 sllin_slave_receive_buf(tty, cp, fp, count);
1010 static int sllin_send_tx_buff(struct sllin *sl)
1012 struct tty_struct *tty = sl->tty;
1016 set_bit(SLF_TXBUFF_RQ, &sl->flags);
1018 if (unlikely(test_and_set_bit(SLF_TXBUFF_INPR, &sl->flags)))
1019 return 0; /* ongoing concurrent processing */
1021 clear_bit(SLF_TXBUFF_RQ, &sl->flags);
1022 #if LINUX_VERSION_CODE < KERNEL_VERSION(3, 18, 0)
1023 smp_mb__after_clear_bit();
1025 smp_mb__after_atomic();
1028 #ifdef BREAK_BY_BAUD
1029 if (sl->lin_state != SLSTATE_BREAK_SENT)
1030 remains = sl->tx_lim - sl->tx_cnt;
1034 remains = sl->tx_lim - sl->tx_cnt;
1037 res = tty->ops->write(tty, sl->tx_buff + sl->tx_cnt, remains);
1039 goto error_in_write;
1045 set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
1046 res = tty->ops->write(tty, sl->tx_buff + sl->tx_cnt, remains);
1048 clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
1049 goto error_in_write;
1056 netdev_dbg(sl->dev, "sllin_send_tx_buff sent %d, remains %d\n",
1057 sl->tx_cnt, remains);
1059 clear_bit(SLF_TXBUFF_INPR, &sl->flags);
1060 #if LINUX_VERSION_CODE < KERNEL_VERSION(3, 18, 0)
1061 smp_mb__after_clear_bit();
1063 smp_mb__after_atomic();
1066 } while (unlikely(test_bit(SLF_TXBUFF_RQ, &sl->flags)));
1068 #ifdef SLLIN_LED_TRIGGER
1069 sllin_led_event(sl->dev, SLLIN_LED_EVENT_TX);
1075 clear_bit(SLF_TXBUFF_INPR, &sl->flags);
1080 #ifdef BREAK_BY_BAUD
1081 static int sllin_send_break(struct sllin *sl)
1083 struct tty_struct *tty = sl->tty;
1084 unsigned long break_baud;
1087 break_baud = ((sl->lin_baud * 2) / 3);
1088 sltty_change_speed(tty, break_baud);
1090 tty->ops->flush_buffer(tty);
1091 sl->rx_cnt = SLLIN_BUFF_BREAK;
1093 sl->rx_expect = SLLIN_BUFF_BREAK + 1;
1094 sl->lin_state = SLSTATE_BREAK_SENT;
1096 res = sllin_send_tx_buff(sl);
1098 sl->lin_state = SLSTATE_IDLE;
1104 #else /* BREAK_BY_BAUD */
1106 static int sllin_send_break(struct sllin *sl)
1108 struct tty_struct *tty = sl->tty;
1110 unsigned long break_baud;
1111 unsigned long usleep_range_min;
1112 unsigned long usleep_range_max;
1114 break_baud = ((sl->lin_baud * 2) / 3);
1115 sl->rx_cnt = SLLIN_BUFF_BREAK;
1116 sl->rx_expect = SLLIN_BUFF_BREAK + 1;
1117 sl->lin_state = SLSTATE_BREAK_SENT;
1119 /* Do the break ourselves; Inspired by
1120 http://lxr.linux.no/#linux+v3.1.2/drivers/tty/tty_io.c#L2452 */
1121 retval = tty->ops->break_ctl(tty, -1);
1126 usleep_range_min = (1000000l * SLLIN_SAMPLES_PER_CHAR) / break_baud;
1127 usleep_range_max = usleep_range_min + 50;
1128 usleep_range(usleep_range_min, usleep_range_max);
1130 retval = tty->ops->break_ctl(tty, 0);
1131 usleep_range_min = (1000000l * 1 /* 1 bit */) / break_baud;
1132 usleep_range_max = usleep_range_min + 30;
1133 usleep_range(usleep_range_min, usleep_range_max);
1135 tty->ops->flush_buffer(tty);
1137 sl->tx_cnt = SLLIN_BUFF_SYNC;
1139 netdev_dbg(sl->dev, "Break sent.\n");
1140 set_bit(SLF_RXEVENT, &sl->flags);
1141 wake_up(&sl->kwt_wq);
1145 #endif /* BREAK_BY_BAUD */
1148 static enum hrtimer_restart sllin_rx_timeout_handler(struct hrtimer *hrtimer)
1150 struct sllin *sl = container_of(hrtimer, struct sllin, rx_timer);
1153 * Signal timeout when:
1154 * master: We did not receive as much characters as expected
1155 * slave: * we did not receive any data bytes at all
1156 * * we know the length and didn't receive enough
1158 if ((sl->lin_master) ||
1159 (sl->rx_cnt <= SLLIN_BUFF_DATA) ||
1160 ((!sl->rx_len_unknown) &&
1161 (sl->rx_cnt < sl->rx_expect))) {
1162 sllin_report_error(sl, LIN_ERR_RX_TIMEOUT);
1163 set_bit(SLF_TMOUTEVENT, &sl->flags);
1165 sllin_slave_finish_rx_msg(sl);
1166 set_bit(SLF_RXEVENT, &sl->flags);
1168 wake_up(&sl->kwt_wq);
1170 return HRTIMER_NORESTART;
1173 /*****************************************
1174 * sllin_kwthread - kernel worker thread
1175 *****************************************/
1177 static int sllin_kwthread(void *ptr)
1179 struct sllin *sl = (struct sllin *)ptr;
1180 struct tty_struct *tty = sl->tty;
1181 struct sched_param schparam = { .sched_priority = 40 };
1182 int tx_bytes = 0; /* Used for Network statistics */
1183 unsigned long flags;
1186 struct sllin_conf_entry *sce;
1188 netdev_dbg(sl->dev, "sllin_kwthread started.\n");
1189 sched_setscheduler(current, SCHED_FIFO, &schparam);
1191 clear_bit(SLF_ERROR, &sl->flags);
1192 sltty_change_speed(tty, sl->lin_baud);
1194 while (!kthread_should_stop()) {
1195 struct can_frame *cf;
1198 u8 lin_data_buff[SLLIN_DATA_MAX];
1201 if ((sl->lin_state == SLSTATE_IDLE) && sl->lin_master &&
1203 if (sllin_send_break(sl) < 0) {
1204 /* error processing */
1208 wait_event_killable(sl->kwt_wq, kthread_should_stop() ||
1209 test_bit(SLF_RXEVENT, &sl->flags) ||
1210 test_bit(SLF_TXEVENT, &sl->flags) ||
1211 test_bit(SLF_TMOUTEVENT, &sl->flags) ||
1212 test_bit(SLF_ERROR, &sl->flags) ||
1213 (sl->lin_state == SLSTATE_ID_RECEIVED) ||
1214 (((sl->lin_state == SLSTATE_IDLE) ||
1215 (sl->lin_state == SLSTATE_RESPONSE_WAIT))
1216 && test_bit(SLF_MSGEVENT, &sl->flags)));
1218 if (test_and_clear_bit(SLF_RXEVENT, &sl->flags)) {
1219 netdev_dbg(sl->dev, "sllin_kthread RXEVENT\n");
1222 if (test_and_clear_bit(SLF_ERROR, &sl->flags)) {
1223 unsigned long usleep_range_min;
1224 unsigned long usleep_range_max;
1225 hrtimer_cancel(&sl->rx_timer);
1226 netdev_dbg(sl->dev, "sllin_kthread ERROR\n");
1228 if (sl->lin_state != SLSTATE_IDLE)
1229 sllin_report_error(sl, LIN_ERR_FRAMING);
1231 usleep_range_min = (1000000l * SLLIN_SAMPLES_PER_CHAR * 10) /
1233 usleep_range_max = usleep_range_min + 50;
1234 usleep_range(usleep_range_min, usleep_range_max);
1235 sllin_reset_buffs(sl);
1236 sl->lin_state = SLSTATE_IDLE;
1239 if (test_and_clear_bit(SLF_TXEVENT, &sl->flags)) {
1240 netdev_dbg(sl->dev, "sllin_kthread TXEVENT\n");
1243 if (test_and_clear_bit(SLF_TMOUTEVENT, &sl->flags)) {
1244 netdev_dbg(sl->dev, "sllin_kthread TMOUTEVENT\n");
1245 sllin_reset_buffs(sl);
1247 sl->lin_state = SLSTATE_IDLE;
1250 switch (sl->lin_state) {
1252 if (!test_bit(SLF_MSGEVENT, &sl->flags))
1256 cf = (struct can_frame *)sl->tx_req_skb->data;
1258 if (cf->can_id & LIN_CHECKSUM_EXTENDED)
1259 mode |= SLLIN_STPMSG_CHCKSUM_ENH;
1261 /* SFF RTR CAN frame -> LIN header */
1262 if (cf->can_id & CAN_RTR_FLAG) {
1263 netdev_dbg(sl->dev, "%s: RTR SFF CAN frame, ID = %x\n",
1264 __func__, cf->can_id & LIN_ID_MASK);
1266 sce = &sl->linfr_cache[cf->can_id & LIN_ID_MASK];
1267 spin_lock_irqsave(&sl->linfr_lock, flags);
1268 if (sce->frame_fl & LIN_CHECKSUM_EXTENDED)
1269 mode |= SLLIN_STPMSG_CHCKSUM_ENH;
1271 /* Is there Slave response in linfr_cache to be sent? */
1272 if ((sce->frame_fl & LIN_CACHE_RESPONSE)
1273 && (sce->dlc > 0)) {
1275 if (sce->frame_fl & LIN_SINGLE_RESPONSE)
1276 sce->frame_fl &= ~LIN_CACHE_RESPONSE;
1278 netdev_dbg(sl->dev, "Sending LIN response from linfr_cache\n");
1280 lin_data = sce->data;
1282 if (lin_dlc > SLLIN_DATA_MAX)
1283 lin_dlc = SLLIN_DATA_MAX;
1284 memcpy(lin_data_buff, lin_data, lin_dlc);
1285 lin_data = lin_data_buff;
1290 spin_unlock_irqrestore(&sl->linfr_lock, flags);
1292 } else { /* SFF NON-RTR CAN frame -> LIN header + LIN response */
1293 netdev_dbg(sl->dev, "%s: NON-RTR SFF CAN frame, ID = %x\n",
1294 __func__, (int)cf->can_id & LIN_ID_MASK);
1296 sce = &sl->linfr_cache[cf->can_id & LIN_ID_MASK];
1297 if (sce->frame_fl & LIN_CHECKSUM_EXTENDED)
1298 mode |= SLLIN_STPMSG_CHCKSUM_ENH;
1300 lin_data = cf->data;
1301 lin_dlc = cf->can_dlc;
1302 if (lin_dlc > SLLIN_DATA_MAX)
1303 lin_dlc = SLLIN_DATA_MAX;
1307 if (sllin_setup_msg(sl, mode, cf->can_id & LIN_ID_MASK,
1308 lin_data, lin_dlc) != -1) {
1310 sl->id_to_send = true;
1311 sl->data_to_send = (lin_data != NULL) ? true : false;
1312 sl->resp_len_known = (lin_dlc > 0) ? true : false;
1313 sl->dev->stats.tx_packets++;
1314 sl->dev->stats.tx_bytes += tx_bytes;
1317 clear_bit(SLF_MSGEVENT, &sl->flags);
1318 kfree_skb(sl->tx_req_skb);
1319 netif_wake_queue(sl->dev);
1320 hrtimer_start(&sl->rx_timer,
1321 ktime_add(ktime_get(), sl->rx_timer_timeout),
1325 case SLSTATE_BREAK_SENT:
1326 #ifdef BREAK_BY_BAUD
1327 if (sl->rx_cnt <= SLLIN_BUFF_BREAK)
1330 res = sltty_change_speed(tty, sl->lin_baud);
1333 sl->lin_state = SLSTATE_ID_SENT;
1334 sllin_send_tx_buff(sl);
1337 case SLSTATE_ID_SENT:
1338 hrtimer_cancel(&sl->rx_timer);
1339 sl->id_to_send = false;
1340 if (sl->data_to_send) {
1341 sllin_send_tx_buff(sl);
1342 sl->lin_state = SLSTATE_RESPONSE_SENT;
1343 sl->rx_expect = sl->tx_lim;
1344 goto slstate_response_sent;
1346 if (sl->resp_len_known) {
1347 sl->rx_expect = sl->rx_lim;
1349 sl->rx_expect = SLLIN_BUFF_DATA + 2;
1351 sl->lin_state = SLSTATE_RESPONSE_WAIT;
1352 /* If we don't receive anything, timer will "unblock" us */
1353 hrtimer_start(&sl->rx_timer,
1354 ktime_add(ktime_get(), sl->rx_timer_timeout),
1356 goto slstate_response_wait;
1360 case SLSTATE_RESPONSE_WAIT:
1361 slstate_response_wait:
1362 if (test_bit(SLF_MSGEVENT, &sl->flags)) {
1363 unsigned char *lin_buff;
1364 cf = (struct can_frame *)sl->tx_req_skb->data;
1366 lin_buff = (sl->lin_master) ? sl->tx_buff : sl->rx_buff;
1367 if (cf->can_id == (lin_buff[SLLIN_BUFF_ID] & LIN_ID_MASK)) {
1368 hrtimer_cancel(&sl->rx_timer);
1369 netdev_dbg(sl->dev, "received LIN response in a CAN frame.\n");
1370 if (sllin_setup_msg(sl, SLLIN_STPMSG_RESPONLY,
1371 cf->can_id & LIN_ID_MASK,
1372 cf->data, cf->can_dlc) != -1) {
1374 sl->rx_expect = sl->tx_lim;
1375 sl->data_to_send = true;
1376 sl->dev->stats.tx_packets++;
1377 sl->dev->stats.tx_bytes += tx_bytes;
1379 if (!sl->lin_master) {
1380 sl->tx_cnt = SLLIN_BUFF_DATA;
1383 sllin_send_tx_buff(sl);
1384 clear_bit(SLF_MSGEVENT, &sl->flags);
1385 kfree_skb(sl->tx_req_skb);
1386 netif_wake_queue(sl->dev);
1388 sl->lin_state = SLSTATE_RESPONSE_SENT;
1389 goto slstate_response_sent;
1392 sl->lin_state = SLSTATE_RESPONSE_WAIT_BUS;
1396 /* Be aware, no BREAK here */
1397 case SLSTATE_RESPONSE_WAIT_BUS:
1398 if (sl->rx_cnt < sl->rx_expect)
1401 hrtimer_cancel(&sl->rx_timer);
1402 netdev_dbg(sl->dev, "response received ID %d len %d\n",
1403 sl->rx_buff[SLLIN_BUFF_ID], sl->rx_cnt - SLLIN_BUFF_DATA - 1);
1405 if (sllin_rx_validate(sl) == -1) {
1406 netdev_dbg(sl->dev, "RX validation failed.\n");
1407 sllin_report_error(sl, LIN_ERR_CHECKSUM);
1409 /* Send CAN non-RTR frame with data */
1410 netdev_dbg(sl->dev, "sending NON-RTR CAN frame with LIN payload.");
1411 sll_bump(sl); /* send packet to the network layer */
1414 sl->id_to_send = false;
1415 sl->lin_state = SLSTATE_IDLE;
1418 case SLSTATE_ID_RECEIVED:
1419 lin_id = sl->rx_buff[SLLIN_BUFF_ID] & LIN_ID_MASK;
1420 sce = &sl->linfr_cache[lin_id];
1421 spin_lock_irqsave(&sl->linfr_lock, flags);
1423 if ((sce->frame_fl & LIN_CACHE_RESPONSE)
1424 && (sce->dlc > 0)) {
1426 if (sce->frame_fl & LIN_SINGLE_RESPONSE)
1427 sce->frame_fl &= ~LIN_CACHE_RESPONSE;
1429 netdev_dbg(sl->dev, "Sending LIN response from linfr_cache\n");
1431 lin_data = sce->data;
1433 if (lin_dlc > SLLIN_DATA_MAX)
1434 lin_dlc = SLLIN_DATA_MAX;
1435 memcpy(lin_data_buff, lin_data, lin_dlc);
1436 lin_data = lin_data_buff;
1439 mode = SLLIN_STPMSG_RESPONLY;
1440 if (sce->frame_fl & LIN_CHECKSUM_EXTENDED)
1441 mode |= SLLIN_STPMSG_CHCKSUM_ENH;
1443 if (sllin_setup_msg(sl, mode, lin_id & LIN_ID_MASK,
1444 lin_data, lin_dlc) != -1) {
1446 sl->rx_expect = sl->tx_lim;
1447 sl->data_to_send = true;
1448 sl->dev->stats.tx_packets++;
1449 sl->dev->stats.tx_bytes += tx_bytes;
1450 sl->resp_len_known = true;
1452 if (!sl->lin_master) {
1453 sl->tx_cnt = SLLIN_BUFF_DATA;
1455 sllin_send_tx_buff(sl);
1458 hrtimer_start(&sl->rx_timer,
1459 ktime_add(ktime_get(), sl->rx_timer_timeout),
1462 spin_unlock_irqrestore(&sl->linfr_lock, flags);
1463 sl->lin_state = SLSTATE_IDLE;
1466 case SLSTATE_RESPONSE_SENT:
1467 slstate_response_sent:
1468 if (sl->rx_cnt < sl->tx_lim)
1471 hrtimer_cancel(&sl->rx_timer);
1472 sll_bump(sl); /* send packet to the network layer */
1473 netdev_dbg(sl->dev, "response sent ID %d len %d\n",
1474 sl->rx_buff[SLLIN_BUFF_ID], sl->rx_cnt - SLLIN_BUFF_DATA - 1);
1476 sl->id_to_send = false;
1477 sl->lin_state = SLSTATE_IDLE;
1482 hrtimer_cancel(&sl->rx_timer);
1483 netdev_dbg(sl->dev, "sllin_kwthread stopped.\n");
1489 /************************************
1490 * sllin_open helper routines.
1491 ************************************/
1493 /* Collect hanged up channels */
1494 static void sll_sync(void)
1497 struct net_device *dev;
1500 for (i = 0; i < maxdev; i++) {
1501 dev = sllin_devs[i];
1505 sl = netdev_priv(dev);
1508 if (dev->flags & IFF_UP)
1513 /* Find a free SLLIN channel, and link in this `tty' line. */
1514 static struct sllin *sll_alloc(dev_t line)
1517 struct net_device *dev = NULL;
1520 if (sllin_devs == NULL)
1521 return NULL; /* Master array missing ! */
1523 for (i = 0; i < maxdev; i++) {
1524 dev = sllin_devs[i];
1530 /* Sorry, too many, all slots in use */
1535 sl = netdev_priv(dev);
1536 if (test_bit(SLF_INUSE, &sl->flags)) {
1537 unregister_netdevice(dev);
1539 sllin_devs[i] = NULL;
1544 char name[IFNAMSIZ];
1545 sprintf(name, "sllin%d", i);
1547 #if (LINUX_VERSION_CODE < KERNEL_VERSION(3, 17, 0))
1548 dev = alloc_netdev(sizeof(*sl), name, sll_setup);
1550 dev = alloc_netdev(sizeof(*sl), name, NET_NAME_UNKNOWN, sll_setup);
1558 sl = netdev_priv(dev);
1559 /* Initialize channel control data */
1560 sl->magic = SLLIN_MAGIC;
1562 spin_lock_init(&sl->lock);
1563 spin_lock_init(&sl->linfr_lock);
1564 sllin_devs[i] = dev;
1570 * Open the high-level part of the SLLIN channel.
1571 * This function is called by the TTY module when the
1572 * SLLIN line discipline is called for. Because we are
1573 * sure the tty line exists, we only have to link it to
1574 * a free SLLIN channel...
1576 * Called in process context serialized from other ldisc calls.
1579 static int sllin_open(struct tty_struct *tty)
1584 pr_debug("sllin: %s() invoked\n", __func__);
1586 if (!capable(CAP_NET_ADMIN))
1589 if (tty->ops->write == NULL)
1592 /* RTnetlink lock is misused here to serialize concurrent
1593 opens of sllin channels. There are better ways, but it is
1598 /* Collect hanged up channels. */
1601 sl = tty->disc_data;
1604 /* First make sure we're not already connected. */
1605 if (sl && sl->magic == SLLIN_MAGIC)
1608 /* OK. Find a free SLLIN channel to use. */
1610 sl = sll_alloc(tty_devnum(tty));
1615 tty->disc_data = sl;
1616 sl->line = tty_devnum(tty);
1618 if (!test_bit(SLF_INUSE, &sl->flags)) {
1619 /* Perform the low-level SLLIN initialization. */
1620 sl->lin_master = master;
1622 pr_debug("sllin: Configured as MASTER\n");
1624 pr_debug("sllin: Configured as SLAVE\n");
1626 sllin_reset_buffs(sl);
1628 sl->lin_baud = (baudrate == 0) ? LIN_DEFAULT_BAUDRATE : baudrate;
1629 pr_debug("sllin: Baudrate set to %u\n", sl->lin_baud);
1631 sl->lin_state = SLSTATE_IDLE;
1633 hrtimer_init(&sl->rx_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1634 sl->rx_timer.function = sllin_rx_timeout_handler;
1635 /* timeval_to_ktime(msg_head->ival1); */
1636 sl->rx_timer_timeout = ns_to_ktime(
1637 (1000000000l / sl->lin_baud) *
1638 SLLIN_SAMPLES_PER_CHAR * SLLIN_CHARS_TO_TIMEOUT);
1640 set_bit(SLF_INUSE, &sl->flags);
1642 init_waitqueue_head(&sl->kwt_wq);
1643 sl->kwthread = kthread_run(sllin_kwthread, sl, "sllin");
1644 if (sl->kwthread == NULL)
1647 err = register_netdevice(sl->dev);
1649 goto err_free_chan_and_thread;
1651 #ifdef SLLIN_LED_TRIGGER
1652 devm_sllin_led_init(sl->dev);
1656 /* Done. We have linked the TTY line to a channel. */
1658 tty->receive_room = SLLIN_BUFF_LEN * 40; /* We don't flow control */
1660 /* TTY layer expects 0 on success */
1663 err_free_chan_and_thread:
1664 kthread_stop(sl->kwthread);
1665 sl->kwthread = NULL;
1669 tty->disc_data = NULL;
1670 clear_bit(SLF_INUSE, &sl->flags);
1675 /* Count references from TTY module */
1680 * Close down a SLLIN channel.
1681 * This means flushing out any pending queues, and then returning. This
1682 * call is serialized against other ldisc functions.
1684 * We also use this method for a hangup event.
1687 static void sllin_close(struct tty_struct *tty)
1689 struct sllin *sl = (struct sllin *) tty->disc_data;
1691 /* First make sure we're connected. */
1692 if (!sl || sl->magic != SLLIN_MAGIC || sl->tty != tty)
1695 kthread_stop(sl->kwthread);
1696 sl->kwthread = NULL;
1698 tty->disc_data = NULL;
1701 /* Flush network side */
1702 unregister_netdev(sl->dev);
1703 /* This will complete via sl_free_netdev */
1706 static int sllin_hangup(struct tty_struct *tty)
1712 /* Perform I/O control on an active SLLIN channel. */
1713 static int sllin_ioctl(struct tty_struct *tty, struct file *file,
1714 unsigned int cmd, unsigned long arg)
1716 struct sllin *sl = (struct sllin *) tty->disc_data;
1719 /* First make sure we're connected. */
1720 if (!sl || sl->magic != SLLIN_MAGIC)
1725 tmp = strlen(sl->dev->name) + 1;
1726 if (copy_to_user((void __user *)arg, sl->dev->name, tmp))
1734 return tty_mode_ioctl(tty, file, cmd, arg);
1738 static struct tty_ldisc_ops sll_ldisc = {
1739 .owner = THIS_MODULE,
1740 .magic = TTY_LDISC_MAGIC,
1743 .close = sllin_close,
1744 .hangup = sllin_hangup,
1745 .ioctl = sllin_ioctl,
1746 .receive_buf = sllin_receive_buf,
1747 .write_wakeup = sllin_write_wakeup,
1750 static int __init sllin_init(void)
1754 #ifdef SLLIN_LED_TRIGGER
1755 status = register_netdevice_notifier(&sllin_netdev_notifier);
1757 pr_err("sllin: can't register netdevice notifier\n");
1761 maxdev = 4; /* Sanity */
1764 pr_debug("sllin: %d dynamic interface channels.\n", maxdev);
1766 sllin_devs = kzalloc(sizeof(struct net_device *)*maxdev, GFP_KERNEL);
1768 pr_err("sllin: can't allocate sllin device array!\n");
1772 /* Fill in our line protocol discipline, and register it */
1773 status = tty_register_ldisc(N_SLLIN, &sll_ldisc);
1775 pr_err("sllin: can't register line discipline\n");
1779 #ifdef BREAK_BY_BAUD
1780 pr_debug("sllin: Break is generated by baud-rate change.");
1782 pr_debug("sllin: Break is generated manually with tiny sleep.");
1788 static void __exit sllin_exit(void)
1791 struct net_device *dev;
1793 unsigned long timeout = jiffies + HZ;
1796 if (sllin_devs == NULL)
1799 /* First of all: check for active disciplines and hangup them.
1803 msleep_interruptible(100);
1806 for (i = 0; i < maxdev; i++) {
1807 dev = sllin_devs[i];
1810 sl = netdev_priv(dev);
1811 spin_lock_bh(&sl->lock);
1814 tty_hangup(sl->tty);
1816 spin_unlock_bh(&sl->lock);
1818 } while (busy && time_before(jiffies, timeout));
1820 /* FIXME: hangup is async so we should wait when doing this second
1823 for (i = 0; i < maxdev; i++) {
1824 dev = sllin_devs[i];
1827 sllin_devs[i] = NULL;
1829 sl = netdev_priv(dev);
1831 netdev_dbg(sl->dev, "tty discipline still running\n");
1832 /* Intentionally leak the control block. */
1833 #if LINUX_VERSION_CODE < KERNEL_VERSION(4,12,0)
1834 dev->destructor = NULL;
1835 #else /* Linux 4.12.0+ */
1836 dev->priv_destructor = NULL;
1837 #endif /* Linux 4.12.0+ */
1840 unregister_netdev(dev);
1846 i = tty_unregister_ldisc(N_SLLIN);
1848 pr_err("sllin: can't unregister ldisc (err %d)\n", i);
1850 #ifdef SLLIN_LED_TRIGGER
1851 unregister_netdevice_notifier(&sllin_netdev_notifier);
1856 module_init(sllin_init);
1857 module_exit(sllin_exit);