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 if (tty != NULL && tty->ops != NULL && tty->ops->flush_buffer != NULL) {
1091 tty->ops->flush_buffer(tty);
1093 netdev_dbg(sl->dev, "flush_buffer is not implemented.\n");
1095 sl->rx_cnt = SLLIN_BUFF_BREAK;
1097 sl->rx_expect = SLLIN_BUFF_BREAK + 1;
1098 sl->lin_state = SLSTATE_BREAK_SENT;
1100 res = sllin_send_tx_buff(sl);
1102 sl->lin_state = SLSTATE_IDLE;
1108 #else /* BREAK_BY_BAUD */
1110 static int sllin_send_break(struct sllin *sl)
1112 struct tty_struct *tty = sl->tty;
1114 unsigned long break_baud;
1115 unsigned long usleep_range_min;
1116 unsigned long usleep_range_max;
1118 break_baud = ((sl->lin_baud * 2) / 3);
1119 sl->rx_cnt = SLLIN_BUFF_BREAK;
1120 sl->rx_expect = SLLIN_BUFF_BREAK + 1;
1121 sl->lin_state = SLSTATE_BREAK_SENT;
1123 /* Do the break ourselves; Inspired by
1124 http://lxr.linux.no/#linux+v3.1.2/drivers/tty/tty_io.c#L2452 */
1125 retval = tty->ops->break_ctl(tty, -1);
1130 usleep_range_min = (1000000l * SLLIN_SAMPLES_PER_CHAR) / break_baud;
1131 usleep_range_max = usleep_range_min + 50;
1132 usleep_range(usleep_range_min, usleep_range_max);
1134 retval = tty->ops->break_ctl(tty, 0);
1135 usleep_range_min = (1000000l * 1 /* 1 bit */) / break_baud;
1136 usleep_range_max = usleep_range_min + 30;
1137 usleep_range(usleep_range_min, usleep_range_max);
1139 if (tty != NULL && tty->ops != NULL && tty->ops->flush_buffer != NULL) {
1140 tty->ops->flush_buffer(tty);
1142 netdev_dbg(sl->dev, "flush_buffer is not implemented.\n");
1145 sl->tx_cnt = SLLIN_BUFF_SYNC;
1147 netdev_dbg(sl->dev, "Break sent.\n");
1148 set_bit(SLF_RXEVENT, &sl->flags);
1149 wake_up(&sl->kwt_wq);
1153 #endif /* BREAK_BY_BAUD */
1156 static enum hrtimer_restart sllin_rx_timeout_handler(struct hrtimer *hrtimer)
1158 struct sllin *sl = container_of(hrtimer, struct sllin, rx_timer);
1161 * Signal timeout when:
1162 * master: We did not receive as much characters as expected
1163 * slave: * we did not receive any data bytes at all
1164 * * we know the length and didn't receive enough
1166 if ((sl->lin_master) ||
1167 (sl->rx_cnt <= SLLIN_BUFF_DATA) ||
1168 ((!sl->rx_len_unknown) &&
1169 (sl->rx_cnt < sl->rx_expect))) {
1170 sllin_report_error(sl, LIN_ERR_RX_TIMEOUT);
1171 set_bit(SLF_TMOUTEVENT, &sl->flags);
1173 sllin_slave_finish_rx_msg(sl);
1174 set_bit(SLF_RXEVENT, &sl->flags);
1176 wake_up(&sl->kwt_wq);
1178 return HRTIMER_NORESTART;
1181 /*****************************************
1182 * sllin_kwthread - kernel worker thread
1183 *****************************************/
1185 static int sllin_kwthread(void *ptr)
1187 struct sllin *sl = (struct sllin *)ptr;
1188 struct tty_struct *tty = sl->tty;
1189 struct sched_param schparam = { .sched_priority = 40 };
1190 int tx_bytes = 0; /* Used for Network statistics */
1191 unsigned long flags;
1194 struct sllin_conf_entry *sce;
1196 netdev_dbg(sl->dev, "sllin_kwthread started.\n");
1197 sched_setscheduler(current, SCHED_FIFO, &schparam);
1199 clear_bit(SLF_ERROR, &sl->flags);
1200 sltty_change_speed(tty, sl->lin_baud);
1202 while (!kthread_should_stop()) {
1203 struct can_frame *cf;
1206 u8 lin_data_buff[SLLIN_DATA_MAX];
1209 if ((sl->lin_state == SLSTATE_IDLE) && sl->lin_master &&
1211 if (sllin_send_break(sl) < 0) {
1212 /* error processing */
1216 wait_event_killable(sl->kwt_wq, kthread_should_stop() ||
1217 test_bit(SLF_RXEVENT, &sl->flags) ||
1218 test_bit(SLF_TXEVENT, &sl->flags) ||
1219 test_bit(SLF_TMOUTEVENT, &sl->flags) ||
1220 test_bit(SLF_ERROR, &sl->flags) ||
1221 (sl->lin_state == SLSTATE_ID_RECEIVED) ||
1222 (((sl->lin_state == SLSTATE_IDLE) ||
1223 (sl->lin_state == SLSTATE_RESPONSE_WAIT))
1224 && test_bit(SLF_MSGEVENT, &sl->flags)));
1226 if (test_and_clear_bit(SLF_RXEVENT, &sl->flags)) {
1227 netdev_dbg(sl->dev, "sllin_kthread RXEVENT\n");
1230 if (test_and_clear_bit(SLF_ERROR, &sl->flags)) {
1231 unsigned long usleep_range_min;
1232 unsigned long usleep_range_max;
1233 hrtimer_cancel(&sl->rx_timer);
1234 netdev_dbg(sl->dev, "sllin_kthread ERROR\n");
1236 if (sl->lin_state != SLSTATE_IDLE)
1237 sllin_report_error(sl, LIN_ERR_FRAMING);
1239 usleep_range_min = (1000000l * SLLIN_SAMPLES_PER_CHAR * 10) /
1241 usleep_range_max = usleep_range_min + 50;
1242 usleep_range(usleep_range_min, usleep_range_max);
1243 sllin_reset_buffs(sl);
1244 sl->lin_state = SLSTATE_IDLE;
1247 if (test_and_clear_bit(SLF_TXEVENT, &sl->flags)) {
1248 netdev_dbg(sl->dev, "sllin_kthread TXEVENT\n");
1251 if (test_and_clear_bit(SLF_TMOUTEVENT, &sl->flags)) {
1252 netdev_dbg(sl->dev, "sllin_kthread TMOUTEVENT\n");
1253 sllin_reset_buffs(sl);
1255 sl->lin_state = SLSTATE_IDLE;
1258 switch (sl->lin_state) {
1260 if (!test_bit(SLF_MSGEVENT, &sl->flags))
1264 cf = (struct can_frame *)sl->tx_req_skb->data;
1266 if (cf->can_id & LIN_CHECKSUM_EXTENDED)
1267 mode |= SLLIN_STPMSG_CHCKSUM_ENH;
1269 /* SFF RTR CAN frame -> LIN header */
1270 if (cf->can_id & CAN_RTR_FLAG) {
1271 netdev_dbg(sl->dev, "%s: RTR SFF CAN frame, ID = %x\n",
1272 __func__, cf->can_id & LIN_ID_MASK);
1274 sce = &sl->linfr_cache[cf->can_id & LIN_ID_MASK];
1275 spin_lock_irqsave(&sl->linfr_lock, flags);
1276 if (sce->frame_fl & LIN_CHECKSUM_EXTENDED)
1277 mode |= SLLIN_STPMSG_CHCKSUM_ENH;
1279 /* Is there Slave response in linfr_cache to be sent? */
1280 if ((sce->frame_fl & LIN_CACHE_RESPONSE)
1281 && (sce->dlc > 0)) {
1283 if (sce->frame_fl & LIN_SINGLE_RESPONSE)
1284 sce->frame_fl &= ~LIN_CACHE_RESPONSE;
1286 netdev_dbg(sl->dev, "Sending LIN response from linfr_cache\n");
1288 lin_data = sce->data;
1290 if (lin_dlc > SLLIN_DATA_MAX)
1291 lin_dlc = SLLIN_DATA_MAX;
1292 memcpy(lin_data_buff, lin_data, lin_dlc);
1293 lin_data = lin_data_buff;
1298 spin_unlock_irqrestore(&sl->linfr_lock, flags);
1300 } else { /* SFF NON-RTR CAN frame -> LIN header + LIN response */
1301 netdev_dbg(sl->dev, "%s: NON-RTR SFF CAN frame, ID = %x\n",
1302 __func__, (int)cf->can_id & LIN_ID_MASK);
1304 sce = &sl->linfr_cache[cf->can_id & LIN_ID_MASK];
1305 if (sce->frame_fl & LIN_CHECKSUM_EXTENDED)
1306 mode |= SLLIN_STPMSG_CHCKSUM_ENH;
1308 lin_data = cf->data;
1309 lin_dlc = cf->can_dlc;
1310 if (lin_dlc > SLLIN_DATA_MAX)
1311 lin_dlc = SLLIN_DATA_MAX;
1315 if (sllin_setup_msg(sl, mode, cf->can_id & LIN_ID_MASK,
1316 lin_data, lin_dlc) != -1) {
1318 sl->id_to_send = true;
1319 sl->data_to_send = (lin_data != NULL) ? true : false;
1320 sl->resp_len_known = (lin_dlc > 0) ? true : false;
1321 sl->dev->stats.tx_packets++;
1322 sl->dev->stats.tx_bytes += tx_bytes;
1325 clear_bit(SLF_MSGEVENT, &sl->flags);
1326 kfree_skb(sl->tx_req_skb);
1327 netif_wake_queue(sl->dev);
1328 hrtimer_start(&sl->rx_timer,
1329 ktime_add(ktime_get(), sl->rx_timer_timeout),
1333 case SLSTATE_BREAK_SENT:
1334 #ifdef BREAK_BY_BAUD
1335 if (sl->rx_cnt <= SLLIN_BUFF_BREAK)
1338 res = sltty_change_speed(tty, sl->lin_baud);
1341 sl->lin_state = SLSTATE_ID_SENT;
1342 sllin_send_tx_buff(sl);
1345 case SLSTATE_ID_SENT:
1346 hrtimer_cancel(&sl->rx_timer);
1347 sl->id_to_send = false;
1348 if (sl->data_to_send) {
1349 sllin_send_tx_buff(sl);
1350 sl->lin_state = SLSTATE_RESPONSE_SENT;
1351 sl->rx_expect = sl->tx_lim;
1352 goto slstate_response_sent;
1354 if (sl->resp_len_known) {
1355 sl->rx_expect = sl->rx_lim;
1357 sl->rx_expect = SLLIN_BUFF_DATA + 2;
1359 sl->lin_state = SLSTATE_RESPONSE_WAIT;
1360 /* If we don't receive anything, timer will "unblock" us */
1361 hrtimer_start(&sl->rx_timer,
1362 ktime_add(ktime_get(), sl->rx_timer_timeout),
1364 goto slstate_response_wait;
1368 case SLSTATE_RESPONSE_WAIT:
1369 slstate_response_wait:
1370 if (test_bit(SLF_MSGEVENT, &sl->flags)) {
1371 unsigned char *lin_buff;
1372 cf = (struct can_frame *)sl->tx_req_skb->data;
1374 lin_buff = (sl->lin_master) ? sl->tx_buff : sl->rx_buff;
1375 if (cf->can_id == (lin_buff[SLLIN_BUFF_ID] & LIN_ID_MASK)) {
1376 hrtimer_cancel(&sl->rx_timer);
1377 netdev_dbg(sl->dev, "received LIN response in a CAN frame.\n");
1378 if (sllin_setup_msg(sl, SLLIN_STPMSG_RESPONLY,
1379 cf->can_id & LIN_ID_MASK,
1380 cf->data, cf->can_dlc) != -1) {
1382 sl->rx_expect = sl->tx_lim;
1383 sl->data_to_send = true;
1384 sl->dev->stats.tx_packets++;
1385 sl->dev->stats.tx_bytes += tx_bytes;
1387 if (!sl->lin_master) {
1388 sl->tx_cnt = SLLIN_BUFF_DATA;
1391 sllin_send_tx_buff(sl);
1392 clear_bit(SLF_MSGEVENT, &sl->flags);
1393 kfree_skb(sl->tx_req_skb);
1394 netif_wake_queue(sl->dev);
1396 sl->lin_state = SLSTATE_RESPONSE_SENT;
1397 goto slstate_response_sent;
1400 sl->lin_state = SLSTATE_RESPONSE_WAIT_BUS;
1404 /* Be aware, no BREAK here */
1405 case SLSTATE_RESPONSE_WAIT_BUS:
1406 if (sl->rx_cnt < sl->rx_expect)
1409 hrtimer_cancel(&sl->rx_timer);
1410 netdev_dbg(sl->dev, "response received ID %d len %d\n",
1411 sl->rx_buff[SLLIN_BUFF_ID], sl->rx_cnt - SLLIN_BUFF_DATA - 1);
1413 if (sllin_rx_validate(sl) == -1) {
1414 netdev_dbg(sl->dev, "RX validation failed.\n");
1415 sllin_report_error(sl, LIN_ERR_CHECKSUM);
1417 /* Send CAN non-RTR frame with data */
1418 netdev_dbg(sl->dev, "sending NON-RTR CAN frame with LIN payload.");
1419 sll_bump(sl); /* send packet to the network layer */
1422 sl->id_to_send = false;
1423 sl->lin_state = SLSTATE_IDLE;
1426 case SLSTATE_ID_RECEIVED:
1427 lin_id = sl->rx_buff[SLLIN_BUFF_ID] & LIN_ID_MASK;
1428 sce = &sl->linfr_cache[lin_id];
1429 spin_lock_irqsave(&sl->linfr_lock, flags);
1431 if ((sce->frame_fl & LIN_CACHE_RESPONSE)
1432 && (sce->dlc > 0)) {
1434 if (sce->frame_fl & LIN_SINGLE_RESPONSE)
1435 sce->frame_fl &= ~LIN_CACHE_RESPONSE;
1437 netdev_dbg(sl->dev, "Sending LIN response from linfr_cache\n");
1439 lin_data = sce->data;
1441 if (lin_dlc > SLLIN_DATA_MAX)
1442 lin_dlc = SLLIN_DATA_MAX;
1443 memcpy(lin_data_buff, lin_data, lin_dlc);
1444 lin_data = lin_data_buff;
1447 mode = SLLIN_STPMSG_RESPONLY;
1448 if (sce->frame_fl & LIN_CHECKSUM_EXTENDED)
1449 mode |= SLLIN_STPMSG_CHCKSUM_ENH;
1451 if (sllin_setup_msg(sl, mode, lin_id & LIN_ID_MASK,
1452 lin_data, lin_dlc) != -1) {
1454 sl->rx_expect = sl->tx_lim;
1455 sl->data_to_send = true;
1456 sl->dev->stats.tx_packets++;
1457 sl->dev->stats.tx_bytes += tx_bytes;
1458 sl->resp_len_known = true;
1460 if (!sl->lin_master) {
1461 sl->tx_cnt = SLLIN_BUFF_DATA;
1463 sllin_send_tx_buff(sl);
1466 hrtimer_start(&sl->rx_timer,
1467 ktime_add(ktime_get(), sl->rx_timer_timeout),
1470 spin_unlock_irqrestore(&sl->linfr_lock, flags);
1471 sl->lin_state = SLSTATE_IDLE;
1474 case SLSTATE_RESPONSE_SENT:
1475 slstate_response_sent:
1476 if (sl->rx_cnt < sl->tx_lim)
1479 hrtimer_cancel(&sl->rx_timer);
1480 sll_bump(sl); /* send packet to the network layer */
1481 netdev_dbg(sl->dev, "response sent ID %d len %d\n",
1482 sl->rx_buff[SLLIN_BUFF_ID], sl->rx_cnt - SLLIN_BUFF_DATA - 1);
1484 sl->id_to_send = false;
1485 sl->lin_state = SLSTATE_IDLE;
1490 hrtimer_cancel(&sl->rx_timer);
1491 netdev_dbg(sl->dev, "sllin_kwthread stopped.\n");
1497 /************************************
1498 * sllin_open helper routines.
1499 ************************************/
1501 /* Collect hanged up channels */
1502 static void sll_sync(void)
1505 struct net_device *dev;
1508 for (i = 0; i < maxdev; i++) {
1509 dev = sllin_devs[i];
1513 sl = netdev_priv(dev);
1516 if (dev->flags & IFF_UP)
1521 /* Find a free SLLIN channel, and link in this `tty' line. */
1522 static struct sllin *sll_alloc(dev_t line)
1525 struct net_device *dev = NULL;
1528 if (sllin_devs == NULL)
1529 return NULL; /* Master array missing ! */
1531 for (i = 0; i < maxdev; i++) {
1532 dev = sllin_devs[i];
1538 /* Sorry, too many, all slots in use */
1543 sl = netdev_priv(dev);
1544 if (test_bit(SLF_INUSE, &sl->flags)) {
1545 unregister_netdevice(dev);
1547 sllin_devs[i] = NULL;
1552 char name[IFNAMSIZ];
1553 sprintf(name, "sllin%d", i);
1555 #if (LINUX_VERSION_CODE < KERNEL_VERSION(3, 17, 0))
1556 dev = alloc_netdev(sizeof(*sl), name, sll_setup);
1558 dev = alloc_netdev(sizeof(*sl), name, NET_NAME_UNKNOWN, sll_setup);
1566 sl = netdev_priv(dev);
1567 /* Initialize channel control data */
1568 sl->magic = SLLIN_MAGIC;
1570 spin_lock_init(&sl->lock);
1571 spin_lock_init(&sl->linfr_lock);
1572 sllin_devs[i] = dev;
1578 * Open the high-level part of the SLLIN channel.
1579 * This function is called by the TTY module when the
1580 * SLLIN line discipline is called for. Because we are
1581 * sure the tty line exists, we only have to link it to
1582 * a free SLLIN channel...
1584 * Called in process context serialized from other ldisc calls.
1587 static int sllin_open(struct tty_struct *tty)
1592 pr_debug("sllin: %s() invoked\n", __func__);
1594 if (!capable(CAP_NET_ADMIN))
1597 if (tty->ops->write == NULL)
1600 /* RTnetlink lock is misused here to serialize concurrent
1601 opens of sllin channels. There are better ways, but it is
1606 /* Collect hanged up channels. */
1609 sl = tty->disc_data;
1612 /* First make sure we're not already connected. */
1613 if (sl && sl->magic == SLLIN_MAGIC)
1616 /* OK. Find a free SLLIN channel to use. */
1618 sl = sll_alloc(tty_devnum(tty));
1623 tty->disc_data = sl;
1624 sl->line = tty_devnum(tty);
1626 if (!test_bit(SLF_INUSE, &sl->flags)) {
1627 /* Perform the low-level SLLIN initialization. */
1628 sl->lin_master = master;
1630 pr_debug("sllin: Configured as MASTER\n");
1632 pr_debug("sllin: Configured as SLAVE\n");
1634 sllin_reset_buffs(sl);
1636 sl->lin_baud = (baudrate == 0) ? LIN_DEFAULT_BAUDRATE : baudrate;
1637 pr_debug("sllin: Baudrate set to %u\n", sl->lin_baud);
1639 sl->lin_state = SLSTATE_IDLE;
1641 hrtimer_init(&sl->rx_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1642 sl->rx_timer.function = sllin_rx_timeout_handler;
1643 /* timeval_to_ktime(msg_head->ival1); */
1644 sl->rx_timer_timeout = ns_to_ktime(
1645 (1000000000l / sl->lin_baud) *
1646 SLLIN_SAMPLES_PER_CHAR * SLLIN_CHARS_TO_TIMEOUT);
1648 set_bit(SLF_INUSE, &sl->flags);
1650 init_waitqueue_head(&sl->kwt_wq);
1651 sl->kwthread = kthread_run(sllin_kwthread, sl, "sllin");
1652 if (sl->kwthread == NULL)
1655 err = register_netdevice(sl->dev);
1657 goto err_free_chan_and_thread;
1659 #ifdef SLLIN_LED_TRIGGER
1660 devm_sllin_led_init(sl->dev);
1664 /* Done. We have linked the TTY line to a channel. */
1666 tty->receive_room = SLLIN_BUFF_LEN * 40; /* We don't flow control */
1668 /* TTY layer expects 0 on success */
1671 err_free_chan_and_thread:
1672 kthread_stop(sl->kwthread);
1673 sl->kwthread = NULL;
1677 tty->disc_data = NULL;
1678 clear_bit(SLF_INUSE, &sl->flags);
1683 /* Count references from TTY module */
1688 * Close down a SLLIN channel.
1689 * This means flushing out any pending queues, and then returning. This
1690 * call is serialized against other ldisc functions.
1692 * We also use this method for a hangup event.
1695 static void sllin_close(struct tty_struct *tty)
1697 struct sllin *sl = (struct sllin *) tty->disc_data;
1699 /* First make sure we're connected. */
1700 if (!sl || sl->magic != SLLIN_MAGIC || sl->tty != tty)
1703 kthread_stop(sl->kwthread);
1704 sl->kwthread = NULL;
1706 tty->disc_data = NULL;
1709 /* Flush network side */
1710 unregister_netdev(sl->dev);
1711 /* This will complete via sl_free_netdev */
1714 static int sllin_hangup(struct tty_struct *tty)
1720 /* Perform I/O control on an active SLLIN channel. */
1721 static int sllin_ioctl(struct tty_struct *tty, struct file *file,
1722 unsigned int cmd, unsigned long arg)
1724 struct sllin *sl = (struct sllin *) tty->disc_data;
1727 /* First make sure we're connected. */
1728 if (!sl || sl->magic != SLLIN_MAGIC)
1733 tmp = strlen(sl->dev->name) + 1;
1734 if (copy_to_user((void __user *)arg, sl->dev->name, tmp))
1742 return tty_mode_ioctl(tty, file, cmd, arg);
1746 static struct tty_ldisc_ops sll_ldisc = {
1747 .owner = THIS_MODULE,
1748 .magic = TTY_LDISC_MAGIC,
1751 .close = sllin_close,
1752 .hangup = sllin_hangup,
1753 .ioctl = sllin_ioctl,
1754 .receive_buf = sllin_receive_buf,
1755 .write_wakeup = sllin_write_wakeup,
1758 static int __init sllin_init(void)
1762 #ifdef SLLIN_LED_TRIGGER
1763 status = register_netdevice_notifier(&sllin_netdev_notifier);
1765 pr_err("sllin: can't register netdevice notifier\n");
1769 maxdev = 4; /* Sanity */
1772 pr_debug("sllin: %d dynamic interface channels.\n", maxdev);
1774 sllin_devs = kzalloc(sizeof(struct net_device *)*maxdev, GFP_KERNEL);
1776 pr_err("sllin: can't allocate sllin device array!\n");
1780 /* Fill in our line protocol discipline, and register it */
1781 status = tty_register_ldisc(N_SLLIN, &sll_ldisc);
1783 pr_err("sllin: can't register line discipline\n");
1787 #ifdef BREAK_BY_BAUD
1788 pr_debug("sllin: Break is generated by baud-rate change.");
1790 pr_debug("sllin: Break is generated manually with tiny sleep.");
1796 static void __exit sllin_exit(void)
1799 struct net_device *dev;
1801 unsigned long timeout = jiffies + HZ;
1804 if (sllin_devs == NULL)
1807 /* First of all: check for active disciplines and hangup them.
1811 msleep_interruptible(100);
1814 for (i = 0; i < maxdev; i++) {
1815 dev = sllin_devs[i];
1818 sl = netdev_priv(dev);
1819 spin_lock_bh(&sl->lock);
1822 tty_hangup(sl->tty);
1824 spin_unlock_bh(&sl->lock);
1826 } while (busy && time_before(jiffies, timeout));
1828 /* FIXME: hangup is async so we should wait when doing this second
1831 for (i = 0; i < maxdev; i++) {
1832 dev = sllin_devs[i];
1835 sllin_devs[i] = NULL;
1837 sl = netdev_priv(dev);
1839 netdev_dbg(sl->dev, "tty discipline still running\n");
1840 /* Intentionally leak the control block. */
1841 #if LINUX_VERSION_CODE < KERNEL_VERSION(4,12,0)
1842 dev->destructor = NULL;
1843 #else /* Linux 4.12.0+ */
1844 dev->priv_destructor = NULL;
1845 #endif /* Linux 4.12.0+ */
1848 unregister_netdev(dev);
1854 i = tty_unregister_ldisc(N_SLLIN);
1856 pr_err("sllin: can't unregister ldisc (err %d)\n", i);
1858 #ifdef SLLIN_LED_TRIGGER
1859 unregister_netdevice_notifier(&sllin_netdev_notifier);
1864 module_init(sllin_init);
1865 module_exit(sllin_exit);