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 /* Should be in include/linux/tty.h */
71 /* -------------------------------- */
73 #ifdef SLLIN_LED_TRIGGER
74 #define SLLIN_LED_NAME_SZ (IFNAMSIZ + 6)
75 #include <linux/leds.h>
77 enum sllin_led_event {
84 static unsigned long led_delay = 50;
85 module_param(led_delay, ulong, 0644);
86 MODULE_PARM_DESC(led_delay,
87 "blink delay time for activity leds (msecs, default: 50).");
88 #endif /* SLLIN_LED_TRIGGER */
90 static __initdata const char banner[] =
91 KERN_INFO "sllin: serial line LIN interface driver\n";
93 MODULE_ALIAS_LDISC(N_SLLIN);
94 MODULE_DESCRIPTION("serial line LIN interface");
95 MODULE_LICENSE("GPL");
96 MODULE_AUTHOR("Pavel Pisa <pisa@cmp.felk.cvut.cz>");
98 #define SLLIN_MAGIC 0x53CA
99 /* #define BREAK_BY_BAUD */
101 static bool master = true;
102 static int baudrate; /* Use LIN_DEFAULT_BAUDRATE when not set */
104 module_param(master, bool, 0444);
105 MODULE_PARM_DESC(master, "LIN interface is Master device");
106 module_param(baudrate, int, 0444);
107 MODULE_PARM_DESC(baudrate, "Baudrate of LIN interface");
109 static int maxdev = 10; /* MAX number of SLLIN channels;
110 This can be overridden with
111 insmod sllin.ko maxdev=nnn */
112 module_param(maxdev, int, 0444);
113 MODULE_PARM_DESC(maxdev, "Maximum number of sllin interfaces");
115 /* maximum buffer len to store whole LIN message*/
116 #define SLLIN_DATA_MAX 8
117 #define SLLIN_BUFF_LEN (1 /*break*/ + 1 /*sync*/ + 1 /*ID*/ + \
118 SLLIN_DATA_MAX + 1 /*checksum*/)
119 #define SLLIN_BUFF_BREAK 0
120 #define SLLIN_BUFF_SYNC 1
121 #define SLLIN_BUFF_ID 2
122 #define SLLIN_BUFF_DATA 3
124 #define SLLIN_SAMPLES_PER_CHAR 10
125 #define SLLIN_CHARS_TO_TIMEOUT 24
131 SLSTATE_RESPONSE_WAIT, /* Wait for response */
132 SLSTATE_RESPONSE_WAIT_BUS, /* Wait for response from LIN bus
133 only (CAN frames from network stack
134 are not processed in this moment) */
136 SLSTATE_RESPONSE_SENT,
139 struct sllin_conf_entry {
140 int dlc; /* Length of data in LIN frame */
141 canid_t frame_fl; /* LIN frame flags. Passed from userspace as
143 u8 data[8]; /* LIN frame data payload */
149 /* Various fields. */
150 struct tty_struct *tty; /* ptr to TTY structure */
151 struct net_device *dev; /* easy for intr handling */
154 /* LIN message buffer and actual processed data counts */
155 unsigned char rx_buff[SLLIN_BUFF_LEN]; /* LIN Rx buffer */
156 unsigned char tx_buff[SLLIN_BUFF_LEN]; /* LIN Tx buffer */
157 int rx_expect; /* expected number of Rx chars */
158 int rx_lim; /* maximum Rx chars for current frame */
159 int rx_cnt; /* message buffer Rx fill level */
160 int tx_lim; /* actual limit of bytes to Tx */
161 int tx_cnt; /* number of already Tx bytes */
162 char lin_master; /* node is a master node */
163 int lin_baud; /* LIN baudrate */
164 int lin_state; /* state */
165 char id_to_send; /* there is ID to be sent */
166 char data_to_send; /* there are data to be sent */
167 char resp_len_known; /* Length of the response is known */
168 char header_received;/* In Slave mode, set when header was already
170 char rx_len_unknown; /* We are not sure how much data will be sent to us --
171 we just guess the length */
173 unsigned long flags; /* Flag values/ mode etc */
174 #define SLF_INUSE 0 /* Channel in use */
175 #define SLF_ERROR 1 /* Parity, etc. error */
176 #define SLF_RXEVENT 2 /* Rx wake event */
177 #define SLF_TXEVENT 3 /* Tx wake event */
178 #define SLF_MSGEVENT 4 /* CAN message to sent */
179 #define SLF_TMOUTEVENT 5 /* Timeout on received data */
180 #define SLF_TXBUFF_RQ 6 /* Req. to send buffer to UART*/
181 #define SLF_TXBUFF_INPR 7 /* Above request in progress */
184 struct task_struct *kwthread;
185 wait_queue_head_t kwt_wq; /* Wait queue used by kwthread */
186 struct hrtimer rx_timer; /* RX timeout timer */
187 ktime_t rx_timer_timeout; /* RX timeout timer value */
188 struct sk_buff *tx_req_skb; /* Socket buffer with CAN frame
189 received from network stack*/
191 /* List with configurations for each of 0 to LIN_ID_MAX LIN IDs */
192 struct sllin_conf_entry linfr_cache[LIN_ID_MAX + 1];
193 spinlock_t linfr_lock; /* frame cache and buffers lock */
195 #ifdef SLLIN_LED_TRIGGER
196 struct led_trigger *tx_led_trig;
197 char tx_led_trig_name[SLLIN_LED_NAME_SZ];
198 struct led_trigger *rx_led_trig;
199 char rx_led_trig_name[SLLIN_LED_NAME_SZ];
200 struct led_trigger *rxtx_led_trig;
201 char rxtx_led_trig_name[SLLIN_LED_NAME_SZ];
205 static struct net_device **sllin_devs;
206 static int sllin_configure_frame_cache(struct sllin *sl, struct can_frame *cf);
207 static void sllin_slave_receive_buf(struct tty_struct *tty,
208 const unsigned char *cp, char *fp, int count);
209 static void sllin_master_receive_buf(struct tty_struct *tty,
210 const unsigned char *cp, char *fp, int count);
213 /* Values of two parity bits in LIN Protected
214 Identifier for each particular LIN ID */
215 const unsigned char sllin_id_parity_table[] = {
216 0x80, 0xc0, 0x40, 0x00, 0xc0, 0x80, 0x00, 0x40,
217 0x00, 0x40, 0xc0, 0x80, 0x40, 0x00, 0x80, 0xc0,
218 0x40, 0x00, 0x80, 0xc0, 0x00, 0x40, 0xc0, 0x80,
219 0xc0, 0x80, 0x00, 0x40, 0x80, 0xc0, 0x40, 0x00,
220 0x00, 0x40, 0xc0, 0x80, 0x40, 0x00, 0x80, 0xc0,
221 0x80, 0xc0, 0x40, 0x00, 0xc0, 0x80, 0x00, 0x40,
222 0xc0, 0x80, 0x00, 0x40, 0x80, 0xc0, 0x40, 0x00,
223 0x40, 0x00, 0x80, 0xc0, 0x00, 0x40, 0xc0, 0x80
226 #ifdef SLLIN_LED_TRIGGER
227 static void sllin_led_event(struct net_device *netdev, enum sllin_led_event event)
229 struct sllin *sl = netdev_priv(netdev);
232 case SLLIN_LED_EVENT_OPEN:
233 led_trigger_event(sl->tx_led_trig, LED_FULL);
234 led_trigger_event(sl->rx_led_trig, LED_FULL);
235 led_trigger_event(sl->rxtx_led_trig, LED_FULL);
237 case SLLIN_LED_EVENT_STOP:
238 led_trigger_event(sl->tx_led_trig, LED_OFF);
239 led_trigger_event(sl->rx_led_trig, LED_OFF);
240 led_trigger_event(sl->rxtx_led_trig, LED_OFF);
242 case SLLIN_LED_EVENT_TX:
244 led_trigger_blink_oneshot(sl->tx_led_trig,
245 &led_delay, &led_delay, 1);
246 led_trigger_blink_oneshot(sl->rxtx_led_trig,
247 &led_delay, &led_delay, 1);
250 case SLLIN_LED_EVENT_RX:
252 led_trigger_blink_oneshot(sl->rx_led_trig,
253 &led_delay, &led_delay, 1);
254 led_trigger_blink_oneshot(sl->rxtx_led_trig,
255 &led_delay, &led_delay, 1);
261 static void sllin_led_release(struct device *gendev, void *res)
263 struct sllin *sl = netdev_priv(to_net_dev(gendev));
265 led_trigger_unregister_simple(sl->tx_led_trig);
266 led_trigger_unregister_simple(sl->rx_led_trig);
267 led_trigger_unregister_simple(sl->rxtx_led_trig);
270 static void devm_sllin_led_init(struct net_device *netdev)
272 struct sllin *sl = netdev_priv(netdev);
275 res = devres_alloc(sllin_led_release, 0, GFP_KERNEL);
277 netdev_err(netdev, "cannot register LED triggers\n");
281 snprintf(sl->tx_led_trig_name, sizeof(sl->tx_led_trig_name),
282 "%s-tx", netdev->name);
283 snprintf(sl->rx_led_trig_name, sizeof(sl->rx_led_trig_name),
284 "%s-rx", netdev->name);
285 snprintf(sl->rxtx_led_trig_name, sizeof(sl->rxtx_led_trig_name),
286 "%s-rxtx", netdev->name);
288 led_trigger_register_simple(sl->tx_led_trig_name,
290 led_trigger_register_simple(sl->rx_led_trig_name,
292 led_trigger_register_simple(sl->rxtx_led_trig_name,
295 devres_add(&netdev->dev, res);
298 static struct sllin *netdev_priv_safe(struct net_device *dev)
302 if (sllin_devs == NULL)
305 for (i = 0; i < maxdev; ++i)
306 if (sllin_devs[i] == dev)
307 return netdev_priv(dev);
312 static int sllin_netdev_notifier_call(struct notifier_block *nb, unsigned long msg,
315 struct net_device *netdev = netdev_notifier_info_to_dev(ptr);
316 struct sllin *sl = netdev_priv_safe(netdev);
317 char name[SLLIN_LED_NAME_SZ];
322 if (!sl->tx_led_trig || !sl->rx_led_trig || !sl->rxtx_led_trig)
325 if (msg == NETDEV_CHANGENAME) {
326 snprintf(name, sizeof(name), "%s-tx", netdev->name);
327 led_trigger_rename_static(name, sl->tx_led_trig);
329 snprintf(name, sizeof(name), "%s-rx", netdev->name);
330 led_trigger_rename_static(name, sl->rx_led_trig);
332 snprintf(name, sizeof(name), "%s-rxtx", netdev->name);
333 led_trigger_rename_static(name, sl->rxtx_led_trig);
339 static struct notifier_block sllin_netdev_notifier __read_mostly = {
340 .notifier_call = sllin_netdev_notifier_call,
342 #endif /* SLLIN_LED_TRIGGER */
345 * sltty_change_speed() -- Change baudrate of Serial device belonging
348 * @tty: Pointer to TTY to change speed for.
349 * @speed: Integer value of new speed. It is possible to
350 * assign non-standard values, i.e. those which
351 * are not defined in termbits.h.
353 static int sltty_change_speed(struct tty_struct *tty, unsigned speed)
355 struct ktermios old_termios, termios;
358 #if LINUX_VERSION_CODE < KERNEL_VERSION(3, 12, 0)
359 mutex_lock(&tty->termios_mutex);
361 down_write(&tty->termios_rwsem);
364 #if LINUX_VERSION_CODE < KERNEL_VERSION(3, 7, 0)
365 old_termios = termios = *(tty->termios);
367 old_termios = termios = tty->termios;
370 cflag = CS8 | CREAD | CLOCAL | HUPCL;
371 cflag &= ~(CBAUD | CIBAUD);
373 termios.c_cflag = cflag;
377 /* Enable interrupt when UART-Break or Framing error received */
378 termios.c_iflag = BRKINT | INPCK;
379 #if LINUX_VERSION_CODE < KERNEL_VERSION(3, 7, 0)
380 *(tty->termios) = termios;
382 tty->termios = termios;
385 tty_encode_baud_rate(tty, speed, speed);
387 if (tty->ops->set_termios)
388 tty->ops->set_termios(tty, &old_termios);
390 #if LINUX_VERSION_CODE < KERNEL_VERSION(3, 12, 0)
391 mutex_unlock(&tty->termios_mutex);
393 up_write(&tty->termios_rwsem);
399 /* Send one can_frame to the network layer */
400 static void sllin_send_canfr(struct sllin *sl, canid_t id, char *data, int len)
408 memcpy(&cf.data, data, cf.can_dlc);
410 skb = dev_alloc_skb(sizeof(struct can_frame));
415 skb->protocol = htons(ETH_P_CAN);
416 skb->pkt_type = PACKET_BROADCAST;
417 skb->ip_summed = CHECKSUM_UNNECESSARY;
418 memcpy(skb_put(skb, sizeof(struct can_frame)),
419 &cf, sizeof(struct can_frame));
422 sl->dev->stats.rx_packets++;
423 sl->dev->stats.rx_bytes += cf.can_dlc;
425 #ifdef SLLIN_LED_TRIGGER
426 sllin_led_event(sl->dev, SLLIN_LED_EVENT_RX);
431 * sll_bump() -- Send data of received LIN frame (existing in sl->rx_buff)
436 static void sll_bump(struct sllin *sl)
438 int len = sl->rx_cnt - SLLIN_BUFF_DATA - 1; /* without checksum */
439 len = (len < 0) ? 0 : len;
441 sllin_send_canfr(sl, sl->rx_buff[SLLIN_BUFF_ID] & LIN_ID_MASK,
442 sl->rx_buff + SLLIN_BUFF_DATA, len);
445 static void sll_send_rtr(struct sllin *sl)
447 sllin_send_canfr(sl, (sl->rx_buff[SLLIN_BUFF_ID] & LIN_ID_MASK) |
448 CAN_RTR_FLAG, NULL, 0);
452 * Called by the driver when there's room for more data. If we have
453 * more packets to send, we send them here.
455 static void sllin_write_wakeup(struct tty_struct *tty)
459 struct sllin *sl = (struct sllin *) tty->disc_data;
461 /* First make sure we're connected. */
462 if (!sl || sl->magic != SLLIN_MAGIC || !netif_running(sl->dev))
465 set_bit(SLF_TXBUFF_RQ, &sl->flags);
467 if (unlikely(test_and_set_bit(SLF_TXBUFF_INPR, &sl->flags)))
468 return; /* ongoing concurrent processing */
470 clear_bit(SLF_TXBUFF_RQ, &sl->flags);
472 #if LINUX_VERSION_CODE < KERNEL_VERSION(3, 18, 0)
473 smp_mb__after_clear_bit();
475 smp_mb__after_atomic();
478 if (sl->lin_state != SLSTATE_BREAK_SENT)
479 remains = sl->tx_lim - sl->tx_cnt;
481 remains = SLLIN_BUFF_BREAK + 1 - sl->tx_cnt;
484 actual = tty->ops->write(tty, sl->tx_buff + sl->tx_cnt,
485 sl->tx_cnt - sl->tx_lim);
486 sl->tx_cnt += actual;
489 clear_bit(SLF_TXBUFF_INPR, &sl->flags);
490 #if LINUX_VERSION_CODE < KERNEL_VERSION(3, 18, 0)
491 smp_mb__after_clear_bit();
493 smp_mb__after_atomic();
496 } while (unlikely(test_bit(SLF_TXBUFF_RQ, &sl->flags)));
498 if ((remains > 0) && (actual >= 0)) {
499 netdev_dbg(sl->dev, "sllin_write_wakeup sent %d, remains %d, waiting\n",
500 sl->tx_cnt, sl->tx_lim - sl->tx_cnt);
504 clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
505 set_bit(SLF_TXEVENT, &sl->flags);
506 wake_up(&sl->kwt_wq);
508 netdev_dbg(sl->dev, "sllin_write_wakeup sent %d, wakeup\n", sl->tx_cnt);
512 * sll_xmit() -- Send a can_frame to a TTY queue.
514 * @skb: Pointer to Socket buffer to be sent.
515 * @dev: Network device where @skb will be sent.
517 static netdev_tx_t sll_xmit(struct sk_buff *skb, struct net_device *dev)
519 struct sllin *sl = netdev_priv(dev);
520 struct can_frame *cf;
522 if (skb->len != sizeof(struct can_frame))
525 spin_lock(&sl->lock);
526 if (!netif_running(dev)) {
527 netdev_warn(sl->dev, "xmit: iface is down\n");
530 if (sl->tty == NULL) {
531 netdev_warn(sl->dev, "xmit: no tty device connected\n");
535 cf = (struct can_frame *) skb->data;
536 if (cf->can_id & LIN_CTRL_FRAME) {
537 sllin_configure_frame_cache(sl, cf);
538 goto free_out_unlock;
541 netif_stop_queue(sl->dev);
543 sl->tx_req_skb = skb;
544 set_bit(SLF_MSGEVENT, &sl->flags);
545 wake_up(&sl->kwt_wq);
546 spin_unlock(&sl->lock);
551 spin_unlock(&sl->lock);
558 /******************************************
559 * Routines looking at netdevice side.
560 ******************************************/
562 /* Netdevice UP -> DOWN routine */
563 static int sll_close(struct net_device *dev)
565 struct sllin *sl = netdev_priv(dev);
567 spin_lock_bh(&sl->lock);
569 /* TTY discipline is running. */
570 clear_bit(TTY_DO_WRITE_WAKEUP, &sl->tty->flags);
572 netif_stop_queue(dev);
575 spin_unlock_bh(&sl->lock);
577 #ifdef SLLIN_LED_TRIGGER
578 sllin_led_event(dev, SLLIN_LED_EVENT_STOP);
583 /* Netdevice DOWN -> UP routine */
584 static int sll_open(struct net_device *dev)
586 struct sllin *sl = netdev_priv(dev);
588 netdev_dbg(sl->dev, "%s() invoked\n", __func__);
593 sl->flags &= (1 << SLF_INUSE);
594 netif_start_queue(dev);
596 #ifdef SLLIN_LED_TRIGGER
597 sllin_led_event(dev, SLLIN_LED_EVENT_OPEN);
602 /* Hook the destructor so we can free sllin devs at the right point in time */
603 static void sll_free_netdev(struct net_device *dev)
605 int i = dev->base_addr;
607 sllin_devs[i] = NULL;
610 static const struct net_device_ops sll_netdev_ops = {
611 .ndo_open = sll_open,
612 .ndo_stop = sll_close,
613 .ndo_start_xmit = sll_xmit,
616 static void sll_setup(struct net_device *dev)
618 dev->netdev_ops = &sll_netdev_ops;
619 dev->destructor = sll_free_netdev;
621 dev->hard_header_len = 0;
623 dev->tx_queue_len = 10;
625 dev->mtu = sizeof(struct can_frame);
626 dev->type = ARPHRD_CAN;
628 /* New-style flags. */
629 dev->flags = IFF_NOARP;
630 dev->features = NETIF_F_HW_CSUM; /* NETIF_F_NO_CSUM;*/
633 /******************************************
634 Routines looking at TTY side.
635 ******************************************/
636 static void sllin_master_receive_buf(struct tty_struct *tty,
637 const unsigned char *cp, char *fp, int count)
639 struct sllin *sl = (struct sllin *) tty->disc_data;
641 /* Read the characters out of the buffer */
644 netdev_dbg(sl->dev, "sllin_master_receive_buf char 0x%02x ignored "
645 "due marker 0x%02x, flags 0x%lx\n",
646 *cp, *(fp-1), sl->flags);
648 /* i.e. Real error -- not Break */
649 if (sl->rx_cnt > SLLIN_BUFF_BREAK) {
650 set_bit(SLF_ERROR, &sl->flags);
651 wake_up(&sl->kwt_wq);
656 #ifndef BREAK_BY_BAUD
657 /* We didn't receive Break character -- fake it! */
658 if ((sl->rx_cnt == SLLIN_BUFF_BREAK) && (*cp == 0x55)) {
659 netdev_dbg(sl->dev, "LIN_RX[%d]: 0x00\n", sl->rx_cnt);
660 sl->rx_buff[sl->rx_cnt++] = 0x00;
662 #endif /* BREAK_BY_BAUD */
664 if (sl->rx_cnt < SLLIN_BUFF_LEN) {
665 netdev_dbg(sl->dev, "LIN_RX[%d]: 0x%02x\n", sl->rx_cnt, *cp);
666 sl->rx_buff[sl->rx_cnt++] = *cp++;
671 if (sl->rx_cnt >= sl->rx_expect) {
672 set_bit(SLF_RXEVENT, &sl->flags);
673 wake_up(&sl->kwt_wq);
674 netdev_dbg(sl->dev, "sllin_receive_buf count %d, wakeup\n", sl->rx_cnt);
676 netdev_dbg(sl->dev, "sllin_receive_buf count %d, waiting\n", sl->rx_cnt);
681 /*****************************************
682 * sllin message helper routines
683 *****************************************/
685 * sllin_report_error() -- Report an error by sending CAN frame
686 * with particular error flag set in can_id
689 * @err: Error flag to be sent.
691 static void sllin_report_error(struct sllin *sl, int err)
693 unsigned char *lin_buff;
697 case LIN_ERR_CHECKSUM:
698 sl->dev->stats.rx_crc_errors++;
701 case LIN_ERR_RX_TIMEOUT:
702 sl->dev->stats.rx_errors++;
705 case LIN_ERR_FRAMING:
706 sl->dev->stats.rx_frame_errors++;
710 lin_buff = (sl->lin_master) ? sl->tx_buff : sl->rx_buff;
711 lin_id = lin_buff[SLLIN_BUFF_ID] & LIN_ID_MASK;
712 sllin_send_canfr(sl, lin_id | CAN_EFF_FLAG |
713 (err & ~LIN_ID_MASK), NULL, 0);
717 * sllin_configure_frame_cache() -- Configure particular entry in linfr_cache
720 * @cf: Pointer to CAN frame sent to this driver
721 * holding configuration information
723 static int sllin_configure_frame_cache(struct sllin *sl, struct can_frame *cf)
726 struct sllin_conf_entry *sce;
728 if (!(cf->can_id & LIN_CTRL_FRAME))
731 sce = &sl->linfr_cache[cf->can_id & LIN_ID_MASK];
732 netdev_dbg(sl->dev, "Setting frame cache with EFF CAN frame. LIN ID = %d\n",
733 cf->can_id & LIN_ID_MASK);
735 spin_lock_irqsave(&sl->linfr_lock, flags);
737 sce->dlc = cf->can_dlc;
738 if (sce->dlc > SLLIN_DATA_MAX)
739 sce->dlc = SLLIN_DATA_MAX;
741 sce->frame_fl = (cf->can_id & ~LIN_ID_MASK) & CAN_EFF_MASK;
742 memcpy(sce->data, cf->data, cf->can_dlc);
744 spin_unlock_irqrestore(&sl->linfr_lock, flags);
750 * sllin_checksum() -- Count checksum for particular data
752 * @data: Pointer to the buffer containing whole LIN
753 * frame (i.e. including break and sync bytes).
754 * @length: Length of the buffer.
755 * @enhanced_fl: Flag determining whether Enhanced or Classic
756 * checksum should be counted.
758 static inline unsigned sllin_checksum(unsigned char *data, int length, int enhanced_fl)
768 for (; i < length; i++) {
777 #define SLLIN_STPMSG_RESPONLY (1) /* Message will be LIN Response only */
778 #define SLLIN_STPMSG_CHCKSUM_CLS (1 << 1)
779 #define SLLIN_STPMSG_CHCKSUM_ENH (1 << 2)
781 static int sllin_setup_msg(struct sllin *sl, int mode, int id,
782 unsigned char *data, int len)
784 if (id > LIN_ID_MASK)
787 if (!(mode & SLLIN_STPMSG_RESPONLY)) {
791 sl->rx_lim = SLLIN_BUFF_LEN;
794 sl->tx_buff[SLLIN_BUFF_BREAK] = 0;
795 sl->tx_buff[SLLIN_BUFF_SYNC] = 0x55;
796 sl->tx_buff[SLLIN_BUFF_ID] = id | sllin_id_parity_table[id];
797 sl->tx_lim = SLLIN_BUFF_DATA;
799 if ((data != NULL) && len) {
801 memcpy(sl->tx_buff + SLLIN_BUFF_DATA, data, len);
802 sl->tx_buff[sl->tx_lim] = sllin_checksum(sl->tx_buff,
803 sl->tx_lim, mode & SLLIN_STPMSG_CHCKSUM_ENH);
807 sl->rx_lim = SLLIN_BUFF_DATA + len + 1;
812 static void sllin_reset_buffs(struct sllin *sl)
816 sl->rx_lim = sl->lin_master ? 0 : SLLIN_BUFF_LEN;
819 sl->id_to_send = false;
820 sl->data_to_send = false;
824 * sllin_rx_validate() -- Validate received frame, i,e. check checksum
828 static int sllin_rx_validate(struct sllin *sl)
834 unsigned char rec_chcksm = sl->rx_buff[sl->rx_cnt - 1];
835 struct sllin_conf_entry *sce;
837 actual_id = sl->rx_buff[SLLIN_BUFF_ID] & LIN_ID_MASK;
838 sce = &sl->linfr_cache[actual_id];
840 spin_lock_irqsave(&sl->linfr_lock, flags);
842 ext_chcks_fl = sce->frame_fl & LIN_CHECKSUM_EXTENDED;
843 spin_unlock_irqrestore(&sl->linfr_lock, flags);
845 if (sllin_checksum(sl->rx_buff, sl->rx_cnt - 1, ext_chcks_fl) !=
848 /* Type of checksum is configured for particular frame */
852 if (sllin_checksum(sl->rx_buff, sl->rx_cnt - 1,
853 !ext_chcks_fl) != rec_chcksm) {
862 static void sllin_slave_finish_rx_msg(struct sllin *sl)
864 if (sllin_rx_validate(sl) == -1) {
865 netdev_dbg(sl->dev, "sllin: RX validation failed.\n");
866 sllin_report_error(sl, LIN_ERR_CHECKSUM);
868 /* Send CAN non-RTR frame with data */
869 netdev_dbg(sl->dev, "sllin: sending NON-RTR CAN frame with LIN payload.");
870 sll_bump(sl); /* send packet to the network layer */
872 /* Prepare for reception of new header */
874 sl->rx_expect = SLLIN_BUFF_ID + 1;
875 sl->rx_len_unknown = false; /* We do know exact length of the header */
876 sl->header_received = false;
879 static void sllin_slave_receive_buf(struct tty_struct *tty,
880 const unsigned char *cp, char *fp, int count)
882 struct sllin *sl = (struct sllin *) tty->disc_data;
884 struct sllin_conf_entry *sce;
887 /* Read the characters out of the buffer */
891 * If we don't know the length of the current message
892 * and received at least the LIN ID, we received here
893 * the break of the next message.
894 * Evaluate the previous one before continuing.
896 if ((sl->rx_len_unknown == true) &&
897 (sl->rx_cnt >= SLLIN_BUFF_ID))
899 hrtimer_cancel(&sl->rx_timer);
900 sllin_slave_finish_rx_msg(sl);
902 set_bit(SLF_RXEVENT, &sl->flags);
903 wake_up(&sl->kwt_wq);
906 netdev_dbg(sl->dev, "sllin_slave_receive_buf char 0x%02x ignored "
907 "due marker 0x%02x, flags 0x%lx\n",
908 *cp, *(fp-1), sl->flags);
912 sl->rx_expect = SLLIN_BUFF_ID + 1;
913 sl->rx_len_unknown = false; /* We do know exact length of the header */
914 sl->header_received = false;
917 if (sl->rx_cnt < SLLIN_BUFF_LEN) {
918 netdev_dbg(sl->dev, "LIN_RX[%d]: 0x%02x\n", sl->rx_cnt, *cp);
920 /* We did not receive break (0x00) character */
921 if ((sl->rx_cnt == SLLIN_BUFF_BREAK) && (*cp == 0x55)) {
922 sl->rx_buff[sl->rx_cnt++] = 0x00;
925 if (sl->rx_cnt == SLLIN_BUFF_SYNC) {
926 /* 'Duplicated' break character -- ignore */
932 /* Wrong sync character */
937 sl->rx_buff[sl->rx_cnt++] = *cp++;
940 /* Header received */
941 if ((sl->header_received == false) && (sl->rx_cnt >= (SLLIN_BUFF_ID + 1))) {
944 lin_id = sl->rx_buff[SLLIN_BUFF_ID] & LIN_ID_MASK;
945 sce = &sl->linfr_cache[lin_id];
947 spin_lock_irqsave(&sl->linfr_lock, flags);
949 sl->lin_state = SLSTATE_ID_RECEIVED;
950 /* Is the length of data set in frame cache? */
952 sl->rx_expect += sce->dlc + 1; /* + checksum */
953 sl->rx_len_unknown = false;
954 wake_up(&sl->kwt_wq);
956 sl->rx_expect += SLLIN_DATA_MAX + 1; /* + checksum */
957 sl->rx_len_unknown = true;
959 spin_unlock_irqrestore(&sl->linfr_lock, flags);
961 sl->header_received = true;
963 hrtimer_start(&sl->rx_timer,
964 ktime_add(ktime_get(), sl->rx_timer_timeout),
970 /* Response received */
971 if ((sl->header_received == true) &&
972 ((sl->rx_cnt >= sl->rx_expect))) {
974 hrtimer_cancel(&sl->rx_timer);
975 netdev_dbg(sl->dev, "Received LIN header & LIN response. "
976 "rx_cnt = %u, rx_expect = %u\n", sl->rx_cnt,
978 sllin_slave_finish_rx_msg(sl);
980 set_bit(SLF_RXEVENT, &sl->flags);
981 wake_up(&sl->kwt_wq);
986 static void sllin_receive_buf(struct tty_struct *tty,
987 const unsigned char *cp, char *fp, int count)
989 struct sllin *sl = (struct sllin *) tty->disc_data;
990 netdev_dbg(sl->dev, "sllin_receive_buf invoked, count = %u\n", count);
992 if (!sl || sl->magic != SLLIN_MAGIC || !netif_running(sl->dev))
996 sllin_master_receive_buf(tty, cp, fp, count);
998 sllin_slave_receive_buf(tty, cp, fp, count);
1002 static int sllin_send_tx_buff(struct sllin *sl)
1004 struct tty_struct *tty = sl->tty;
1008 set_bit(SLF_TXBUFF_RQ, &sl->flags);
1010 if (unlikely(test_and_set_bit(SLF_TXBUFF_INPR, &sl->flags)))
1011 return 0; /* ongoing concurrent processing */
1013 clear_bit(SLF_TXBUFF_RQ, &sl->flags);
1014 #if LINUX_VERSION_CODE < KERNEL_VERSION(3, 18, 0)
1015 smp_mb__after_clear_bit();
1017 smp_mb__after_atomic();
1020 #ifdef BREAK_BY_BAUD
1021 if (sl->lin_state != SLSTATE_BREAK_SENT)
1022 remains = sl->tx_lim - sl->tx_cnt;
1026 remains = sl->tx_lim - sl->tx_cnt;
1029 res = tty->ops->write(tty, sl->tx_buff + sl->tx_cnt, remains);
1031 goto error_in_write;
1037 set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
1038 res = tty->ops->write(tty, sl->tx_buff + sl->tx_cnt, remains);
1040 clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
1041 goto error_in_write;
1048 netdev_dbg(sl->dev, "sllin_send_tx_buff sent %d, remains %d\n",
1049 sl->tx_cnt, remains);
1051 clear_bit(SLF_TXBUFF_INPR, &sl->flags);
1052 #if LINUX_VERSION_CODE < KERNEL_VERSION(3, 18, 0)
1053 smp_mb__after_clear_bit();
1055 smp_mb__after_atomic();
1058 } while (unlikely(test_bit(SLF_TXBUFF_RQ, &sl->flags)));
1060 #ifdef SLLIN_LED_TRIGGER
1061 sllin_led_event(sl->dev, SLLIN_LED_EVENT_TX);
1067 clear_bit(SLF_TXBUFF_INPR, &sl->flags);
1072 #ifdef BREAK_BY_BAUD
1073 static int sllin_send_break(struct sllin *sl)
1075 struct tty_struct *tty = sl->tty;
1076 unsigned long break_baud;
1079 break_baud = ((sl->lin_baud * 2) / 3);
1080 sltty_change_speed(tty, break_baud);
1082 tty->ops->flush_buffer(tty);
1083 sl->rx_cnt = SLLIN_BUFF_BREAK;
1085 sl->rx_expect = SLLIN_BUFF_BREAK + 1;
1086 sl->lin_state = SLSTATE_BREAK_SENT;
1088 res = sllin_send_tx_buff(sl);
1090 sl->lin_state = SLSTATE_IDLE;
1096 #else /* BREAK_BY_BAUD */
1098 static int sllin_send_break(struct sllin *sl)
1100 struct tty_struct *tty = sl->tty;
1102 unsigned long break_baud;
1103 unsigned long usleep_range_min;
1104 unsigned long usleep_range_max;
1106 break_baud = ((sl->lin_baud * 2) / 3);
1107 sl->rx_cnt = SLLIN_BUFF_BREAK;
1108 sl->rx_expect = SLLIN_BUFF_BREAK + 1;
1109 sl->lin_state = SLSTATE_BREAK_SENT;
1111 /* Do the break ourselves; Inspired by
1112 http://lxr.linux.no/#linux+v3.1.2/drivers/tty/tty_io.c#L2452 */
1113 retval = tty->ops->break_ctl(tty, -1);
1118 usleep_range_min = (1000000l * SLLIN_SAMPLES_PER_CHAR) / break_baud;
1119 usleep_range_max = usleep_range_min + 50;
1120 usleep_range(usleep_range_min, usleep_range_max);
1122 retval = tty->ops->break_ctl(tty, 0);
1123 usleep_range_min = (1000000l * 1 /* 1 bit */) / break_baud;
1124 usleep_range_max = usleep_range_min + 30;
1125 usleep_range(usleep_range_min, usleep_range_max);
1127 tty->ops->flush_buffer(tty);
1129 sl->tx_cnt = SLLIN_BUFF_SYNC;
1131 netdev_dbg(sl->dev, "Break sent.\n");
1132 set_bit(SLF_RXEVENT, &sl->flags);
1133 wake_up(&sl->kwt_wq);
1137 #endif /* BREAK_BY_BAUD */
1140 static enum hrtimer_restart sllin_rx_timeout_handler(struct hrtimer *hrtimer)
1142 struct sllin *sl = container_of(hrtimer, struct sllin, rx_timer);
1145 * Signal timeout when:
1146 * master: We did not receive as much characters as expected
1147 * slave: * we did not receive any data bytes at all
1148 * * we know the length and didn't receive enough
1150 if ((sl->lin_master) ||
1151 (sl->rx_cnt <= SLLIN_BUFF_DATA) ||
1152 ((!sl->rx_len_unknown) &&
1153 (sl->rx_cnt < sl->rx_expect))) {
1154 sllin_report_error(sl, LIN_ERR_RX_TIMEOUT);
1155 set_bit(SLF_TMOUTEVENT, &sl->flags);
1157 sllin_slave_finish_rx_msg(sl);
1158 set_bit(SLF_RXEVENT, &sl->flags);
1160 wake_up(&sl->kwt_wq);
1162 return HRTIMER_NORESTART;
1165 /*****************************************
1166 * sllin_kwthread - kernel worker thread
1167 *****************************************/
1169 static int sllin_kwthread(void *ptr)
1171 struct sllin *sl = (struct sllin *)ptr;
1172 struct tty_struct *tty = sl->tty;
1173 struct sched_param schparam = { .sched_priority = 40 };
1174 int tx_bytes = 0; /* Used for Network statistics */
1175 unsigned long flags;
1178 struct sllin_conf_entry *sce;
1180 netdev_dbg(sl->dev, "sllin_kwthread started.\n");
1181 sched_setscheduler(current, SCHED_FIFO, &schparam);
1183 clear_bit(SLF_ERROR, &sl->flags);
1184 sltty_change_speed(tty, sl->lin_baud);
1186 while (!kthread_should_stop()) {
1187 struct can_frame *cf;
1190 u8 lin_data_buff[SLLIN_DATA_MAX];
1193 if ((sl->lin_state == SLSTATE_IDLE) && sl->lin_master &&
1195 if (sllin_send_break(sl) < 0) {
1196 /* error processing */
1200 wait_event_killable(sl->kwt_wq, kthread_should_stop() ||
1201 test_bit(SLF_RXEVENT, &sl->flags) ||
1202 test_bit(SLF_TXEVENT, &sl->flags) ||
1203 test_bit(SLF_TMOUTEVENT, &sl->flags) ||
1204 test_bit(SLF_ERROR, &sl->flags) ||
1205 (sl->lin_state == SLSTATE_ID_RECEIVED) ||
1206 (((sl->lin_state == SLSTATE_IDLE) ||
1207 (sl->lin_state == SLSTATE_RESPONSE_WAIT))
1208 && test_bit(SLF_MSGEVENT, &sl->flags)));
1210 if (test_and_clear_bit(SLF_RXEVENT, &sl->flags)) {
1211 netdev_dbg(sl->dev, "sllin_kthread RXEVENT\n");
1214 if (test_and_clear_bit(SLF_ERROR, &sl->flags)) {
1215 unsigned long usleep_range_min;
1216 unsigned long usleep_range_max;
1217 hrtimer_cancel(&sl->rx_timer);
1218 netdev_dbg(sl->dev, "sllin_kthread ERROR\n");
1220 if (sl->lin_state != SLSTATE_IDLE)
1221 sllin_report_error(sl, LIN_ERR_FRAMING);
1223 usleep_range_min = (1000000l * SLLIN_SAMPLES_PER_CHAR * 10) /
1225 usleep_range_max = usleep_range_min + 50;
1226 usleep_range(usleep_range_min, usleep_range_max);
1227 sllin_reset_buffs(sl);
1228 sl->lin_state = SLSTATE_IDLE;
1231 if (test_and_clear_bit(SLF_TXEVENT, &sl->flags)) {
1232 netdev_dbg(sl->dev, "sllin_kthread TXEVENT\n");
1235 if (test_and_clear_bit(SLF_TMOUTEVENT, &sl->flags)) {
1236 netdev_dbg(sl->dev, "sllin_kthread TMOUTEVENT\n");
1237 sllin_reset_buffs(sl);
1239 sl->lin_state = SLSTATE_IDLE;
1242 switch (sl->lin_state) {
1244 if (!test_bit(SLF_MSGEVENT, &sl->flags))
1248 cf = (struct can_frame *)sl->tx_req_skb->data;
1250 if (cf->can_id & LIN_CHECKSUM_EXTENDED)
1251 mode |= SLLIN_STPMSG_CHCKSUM_ENH;
1253 /* SFF RTR CAN frame -> LIN header */
1254 if (cf->can_id & CAN_RTR_FLAG) {
1255 netdev_dbg(sl->dev, "%s: RTR SFF CAN frame, ID = %x\n",
1256 __func__, cf->can_id & LIN_ID_MASK);
1258 sce = &sl->linfr_cache[cf->can_id & LIN_ID_MASK];
1259 spin_lock_irqsave(&sl->linfr_lock, flags);
1260 if (sce->frame_fl & LIN_CHECKSUM_EXTENDED)
1261 mode |= SLLIN_STPMSG_CHCKSUM_ENH;
1263 /* Is there Slave response in linfr_cache to be sent? */
1264 if ((sce->frame_fl & LIN_CACHE_RESPONSE)
1265 && (sce->dlc > 0)) {
1267 if (sce->frame_fl & LIN_SINGLE_RESPONSE)
1268 sce->frame_fl &= ~LIN_CACHE_RESPONSE;
1270 netdev_dbg(sl->dev, "Sending LIN response from linfr_cache\n");
1272 lin_data = sce->data;
1274 if (lin_dlc > SLLIN_DATA_MAX)
1275 lin_dlc = SLLIN_DATA_MAX;
1276 memcpy(lin_data_buff, lin_data, lin_dlc);
1277 lin_data = lin_data_buff;
1282 spin_unlock_irqrestore(&sl->linfr_lock, flags);
1284 } else { /* SFF NON-RTR CAN frame -> LIN header + LIN response */
1285 netdev_dbg(sl->dev, "%s: NON-RTR SFF CAN frame, ID = %x\n",
1286 __func__, (int)cf->can_id & LIN_ID_MASK);
1288 sce = &sl->linfr_cache[cf->can_id & LIN_ID_MASK];
1289 if (sce->frame_fl & LIN_CHECKSUM_EXTENDED)
1290 mode |= SLLIN_STPMSG_CHCKSUM_ENH;
1292 lin_data = cf->data;
1293 lin_dlc = cf->can_dlc;
1294 if (lin_dlc > SLLIN_DATA_MAX)
1295 lin_dlc = SLLIN_DATA_MAX;
1299 if (sllin_setup_msg(sl, mode, cf->can_id & LIN_ID_MASK,
1300 lin_data, lin_dlc) != -1) {
1302 sl->id_to_send = true;
1303 sl->data_to_send = (lin_data != NULL) ? true : false;
1304 sl->resp_len_known = (lin_dlc > 0) ? true : false;
1305 sl->dev->stats.tx_packets++;
1306 sl->dev->stats.tx_bytes += tx_bytes;
1309 clear_bit(SLF_MSGEVENT, &sl->flags);
1310 kfree_skb(sl->tx_req_skb);
1311 netif_wake_queue(sl->dev);
1312 hrtimer_start(&sl->rx_timer,
1313 ktime_add(ktime_get(), sl->rx_timer_timeout),
1317 case SLSTATE_BREAK_SENT:
1318 #ifdef BREAK_BY_BAUD
1319 if (sl->rx_cnt <= SLLIN_BUFF_BREAK)
1322 res = sltty_change_speed(tty, sl->lin_baud);
1325 sl->lin_state = SLSTATE_ID_SENT;
1326 sllin_send_tx_buff(sl);
1329 case SLSTATE_ID_SENT:
1330 hrtimer_cancel(&sl->rx_timer);
1331 sl->id_to_send = false;
1332 if (sl->data_to_send) {
1333 sllin_send_tx_buff(sl);
1334 sl->lin_state = SLSTATE_RESPONSE_SENT;
1335 sl->rx_expect = sl->tx_lim;
1336 goto slstate_response_sent;
1338 if (sl->resp_len_known) {
1339 sl->rx_expect = sl->rx_lim;
1341 sl->rx_expect = SLLIN_BUFF_DATA + 2;
1343 sl->lin_state = SLSTATE_RESPONSE_WAIT;
1344 /* If we don't receive anything, timer will "unblock" us */
1345 hrtimer_start(&sl->rx_timer,
1346 ktime_add(ktime_get(), sl->rx_timer_timeout),
1348 goto slstate_response_wait;
1352 case SLSTATE_RESPONSE_WAIT:
1353 slstate_response_wait:
1354 if (test_bit(SLF_MSGEVENT, &sl->flags)) {
1355 unsigned char *lin_buff;
1356 cf = (struct can_frame *)sl->tx_req_skb->data;
1358 lin_buff = (sl->lin_master) ? sl->tx_buff : sl->rx_buff;
1359 if (cf->can_id == (lin_buff[SLLIN_BUFF_ID] & LIN_ID_MASK)) {
1360 hrtimer_cancel(&sl->rx_timer);
1361 netdev_dbg(sl->dev, "received LIN response in a CAN frame.\n");
1362 if (sllin_setup_msg(sl, SLLIN_STPMSG_RESPONLY,
1363 cf->can_id & LIN_ID_MASK,
1364 cf->data, cf->can_dlc) != -1) {
1366 sl->rx_expect = sl->tx_lim;
1367 sl->data_to_send = true;
1368 sl->dev->stats.tx_packets++;
1369 sl->dev->stats.tx_bytes += tx_bytes;
1371 if (!sl->lin_master) {
1372 sl->tx_cnt = SLLIN_BUFF_DATA;
1375 sllin_send_tx_buff(sl);
1376 clear_bit(SLF_MSGEVENT, &sl->flags);
1377 kfree_skb(sl->tx_req_skb);
1378 netif_wake_queue(sl->dev);
1380 sl->lin_state = SLSTATE_RESPONSE_SENT;
1381 goto slstate_response_sent;
1384 sl->lin_state = SLSTATE_RESPONSE_WAIT_BUS;
1388 /* Be aware, no BREAK here */
1389 case SLSTATE_RESPONSE_WAIT_BUS:
1390 if (sl->rx_cnt < sl->rx_expect)
1393 hrtimer_cancel(&sl->rx_timer);
1394 netdev_dbg(sl->dev, "response received ID %d len %d\n",
1395 sl->rx_buff[SLLIN_BUFF_ID], sl->rx_cnt - SLLIN_BUFF_DATA - 1);
1397 if (sllin_rx_validate(sl) == -1) {
1398 netdev_dbg(sl->dev, "RX validation failed.\n");
1399 sllin_report_error(sl, LIN_ERR_CHECKSUM);
1401 /* Send CAN non-RTR frame with data */
1402 netdev_dbg(sl->dev, "sending NON-RTR CAN frame with LIN payload.");
1403 sll_bump(sl); /* send packet to the network layer */
1406 sl->id_to_send = false;
1407 sl->lin_state = SLSTATE_IDLE;
1410 case SLSTATE_ID_RECEIVED:
1411 lin_id = sl->rx_buff[SLLIN_BUFF_ID] & LIN_ID_MASK;
1412 sce = &sl->linfr_cache[lin_id];
1413 spin_lock_irqsave(&sl->linfr_lock, flags);
1415 if ((sce->frame_fl & LIN_CACHE_RESPONSE)
1416 && (sce->dlc > 0)) {
1418 if (sce->frame_fl & LIN_SINGLE_RESPONSE)
1419 sce->frame_fl &= ~LIN_CACHE_RESPONSE;
1421 netdev_dbg(sl->dev, "Sending LIN response from linfr_cache\n");
1423 lin_data = sce->data;
1425 if (lin_dlc > SLLIN_DATA_MAX)
1426 lin_dlc = SLLIN_DATA_MAX;
1427 memcpy(lin_data_buff, lin_data, lin_dlc);
1428 lin_data = lin_data_buff;
1431 mode = SLLIN_STPMSG_RESPONLY;
1432 if (sce->frame_fl & LIN_CHECKSUM_EXTENDED)
1433 mode |= SLLIN_STPMSG_CHCKSUM_ENH;
1435 if (sllin_setup_msg(sl, mode, lin_id & LIN_ID_MASK,
1436 lin_data, lin_dlc) != -1) {
1438 sl->rx_expect = sl->tx_lim;
1439 sl->data_to_send = true;
1440 sl->dev->stats.tx_packets++;
1441 sl->dev->stats.tx_bytes += tx_bytes;
1442 sl->resp_len_known = true;
1444 if (!sl->lin_master) {
1445 sl->tx_cnt = SLLIN_BUFF_DATA;
1447 sllin_send_tx_buff(sl);
1450 hrtimer_start(&sl->rx_timer,
1451 ktime_add(ktime_get(), sl->rx_timer_timeout),
1454 spin_unlock_irqrestore(&sl->linfr_lock, flags);
1455 sl->lin_state = SLSTATE_IDLE;
1458 case SLSTATE_RESPONSE_SENT:
1459 slstate_response_sent:
1460 if (sl->rx_cnt < sl->tx_lim)
1463 hrtimer_cancel(&sl->rx_timer);
1464 sll_bump(sl); /* send packet to the network layer */
1465 netdev_dbg(sl->dev, "response sent ID %d len %d\n",
1466 sl->rx_buff[SLLIN_BUFF_ID], sl->rx_cnt - SLLIN_BUFF_DATA - 1);
1468 sl->id_to_send = false;
1469 sl->lin_state = SLSTATE_IDLE;
1474 hrtimer_cancel(&sl->rx_timer);
1475 netdev_dbg(sl->dev, "sllin_kwthread stopped.\n");
1481 /************************************
1482 * sllin_open helper routines.
1483 ************************************/
1485 /* Collect hanged up channels */
1486 static void sll_sync(void)
1489 struct net_device *dev;
1492 for (i = 0; i < maxdev; i++) {
1493 dev = sllin_devs[i];
1497 sl = netdev_priv(dev);
1500 if (dev->flags & IFF_UP)
1505 /* Find a free SLLIN channel, and link in this `tty' line. */
1506 static struct sllin *sll_alloc(dev_t line)
1509 struct net_device *dev = NULL;
1512 if (sllin_devs == NULL)
1513 return NULL; /* Master array missing ! */
1515 for (i = 0; i < maxdev; i++) {
1516 dev = sllin_devs[i];
1522 /* Sorry, too many, all slots in use */
1527 sl = netdev_priv(dev);
1528 if (test_bit(SLF_INUSE, &sl->flags)) {
1529 unregister_netdevice(dev);
1531 sllin_devs[i] = NULL;
1536 char name[IFNAMSIZ];
1537 sprintf(name, "sllin%d", i);
1539 #if (LINUX_VERSION_CODE < KERNEL_VERSION(3, 17, 0))
1540 dev = alloc_netdev(sizeof(*sl), name, sll_setup);
1542 dev = alloc_netdev(sizeof(*sl), name, NET_NAME_UNKNOWN, sll_setup);
1550 sl = netdev_priv(dev);
1551 /* Initialize channel control data */
1552 sl->magic = SLLIN_MAGIC;
1554 spin_lock_init(&sl->lock);
1555 spin_lock_init(&sl->linfr_lock);
1556 sllin_devs[i] = dev;
1562 * Open the high-level part of the SLLIN channel.
1563 * This function is called by the TTY module when the
1564 * SLLIN line discipline is called for. Because we are
1565 * sure the tty line exists, we only have to link it to
1566 * a free SLLIN channel...
1568 * Called in process context serialized from other ldisc calls.
1571 static int sllin_open(struct tty_struct *tty)
1576 pr_debug("sllin: %s() invoked\n", __func__);
1578 if (!capable(CAP_NET_ADMIN))
1581 if (tty->ops->write == NULL)
1584 /* RTnetlink lock is misused here to serialize concurrent
1585 opens of sllin channels. There are better ways, but it is
1590 /* Collect hanged up channels. */
1593 sl = tty->disc_data;
1596 /* First make sure we're not already connected. */
1597 if (sl && sl->magic == SLLIN_MAGIC)
1600 /* OK. Find a free SLLIN channel to use. */
1602 sl = sll_alloc(tty_devnum(tty));
1607 tty->disc_data = sl;
1608 sl->line = tty_devnum(tty);
1610 if (!test_bit(SLF_INUSE, &sl->flags)) {
1611 /* Perform the low-level SLLIN initialization. */
1612 sl->lin_master = master;
1614 pr_debug("sllin: Configured as MASTER\n");
1616 pr_debug("sllin: Configured as SLAVE\n");
1618 sllin_reset_buffs(sl);
1620 sl->lin_baud = (baudrate == 0) ? LIN_DEFAULT_BAUDRATE : baudrate;
1621 pr_debug("sllin: Baudrate set to %u\n", sl->lin_baud);
1623 sl->lin_state = SLSTATE_IDLE;
1625 hrtimer_init(&sl->rx_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1626 sl->rx_timer.function = sllin_rx_timeout_handler;
1627 /* timeval_to_ktime(msg_head->ival1); */
1628 sl->rx_timer_timeout = ns_to_ktime(
1629 (1000000000l / sl->lin_baud) *
1630 SLLIN_SAMPLES_PER_CHAR * SLLIN_CHARS_TO_TIMEOUT);
1632 set_bit(SLF_INUSE, &sl->flags);
1634 init_waitqueue_head(&sl->kwt_wq);
1635 sl->kwthread = kthread_run(sllin_kwthread, sl, "sllin");
1636 if (sl->kwthread == NULL)
1639 err = register_netdevice(sl->dev);
1641 goto err_free_chan_and_thread;
1643 #ifdef SLLIN_LED_TRIGGER
1644 devm_sllin_led_init(sl->dev);
1648 /* Done. We have linked the TTY line to a channel. */
1650 tty->receive_room = SLLIN_BUFF_LEN * 40; /* We don't flow control */
1652 /* TTY layer expects 0 on success */
1655 err_free_chan_and_thread:
1656 kthread_stop(sl->kwthread);
1657 sl->kwthread = NULL;
1661 tty->disc_data = NULL;
1662 clear_bit(SLF_INUSE, &sl->flags);
1667 /* Count references from TTY module */
1672 * Close down a SLLIN channel.
1673 * This means flushing out any pending queues, and then returning. This
1674 * call is serialized against other ldisc functions.
1676 * We also use this method for a hangup event.
1679 static void sllin_close(struct tty_struct *tty)
1681 struct sllin *sl = (struct sllin *) tty->disc_data;
1683 /* First make sure we're connected. */
1684 if (!sl || sl->magic != SLLIN_MAGIC || sl->tty != tty)
1687 kthread_stop(sl->kwthread);
1688 sl->kwthread = NULL;
1690 tty->disc_data = NULL;
1693 /* Flush network side */
1694 unregister_netdev(sl->dev);
1695 /* This will complete via sl_free_netdev */
1698 static int sllin_hangup(struct tty_struct *tty)
1704 /* Perform I/O control on an active SLLIN channel. */
1705 static int sllin_ioctl(struct tty_struct *tty, struct file *file,
1706 unsigned int cmd, unsigned long arg)
1708 struct sllin *sl = (struct sllin *) tty->disc_data;
1711 /* First make sure we're connected. */
1712 if (!sl || sl->magic != SLLIN_MAGIC)
1717 tmp = strlen(sl->dev->name) + 1;
1718 if (copy_to_user((void __user *)arg, sl->dev->name, tmp))
1726 return tty_mode_ioctl(tty, file, cmd, arg);
1730 static struct tty_ldisc_ops sll_ldisc = {
1731 .owner = THIS_MODULE,
1732 .magic = TTY_LDISC_MAGIC,
1735 .close = sllin_close,
1736 .hangup = sllin_hangup,
1737 .ioctl = sllin_ioctl,
1738 .receive_buf = sllin_receive_buf,
1739 .write_wakeup = sllin_write_wakeup,
1742 static int __init sllin_init(void)
1746 #ifdef SLLIN_LED_TRIGGER
1747 status = register_netdevice_notifier(&sllin_netdev_notifier);
1749 pr_err("sllin: can't register netdevice notifier\n");
1753 maxdev = 4; /* Sanity */
1756 pr_debug("sllin: %d dynamic interface channels.\n", maxdev);
1758 sllin_devs = kzalloc(sizeof(struct net_device *)*maxdev, GFP_KERNEL);
1760 pr_err("sllin: can't allocate sllin device array!\n");
1764 /* Fill in our line protocol discipline, and register it */
1765 status = tty_register_ldisc(N_SLLIN, &sll_ldisc);
1767 pr_err("sllin: can't register line discipline\n");
1771 #ifdef BREAK_BY_BAUD
1772 pr_debug("sllin: Break is generated by baud-rate change.");
1774 pr_debug("sllin: Break is generated manually with tiny sleep.");
1780 static void __exit sllin_exit(void)
1783 struct net_device *dev;
1785 unsigned long timeout = jiffies + HZ;
1788 if (sllin_devs == NULL)
1791 /* First of all: check for active disciplines and hangup them.
1795 msleep_interruptible(100);
1798 for (i = 0; i < maxdev; i++) {
1799 dev = sllin_devs[i];
1802 sl = netdev_priv(dev);
1803 spin_lock_bh(&sl->lock);
1806 tty_hangup(sl->tty);
1808 spin_unlock_bh(&sl->lock);
1810 } while (busy && time_before(jiffies, timeout));
1812 /* FIXME: hangup is async so we should wait when doing this second
1815 for (i = 0; i < maxdev; i++) {
1816 dev = sllin_devs[i];
1819 sllin_devs[i] = NULL;
1821 sl = netdev_priv(dev);
1823 netdev_dbg(sl->dev, "tty discipline still running\n");
1824 /* Intentionally leak the control block. */
1825 dev->destructor = NULL;
1828 unregister_netdev(dev);
1834 i = tty_unregister_ldisc(N_SLLIN);
1836 pr_err("sllin: can't unregister ldisc (err %d)\n", i);
1838 #ifdef SLLIN_LED_TRIGGER
1839 unregister_netdevice_notifier(&sllin_netdev_notifier);
1844 module_init(sllin_init);
1845 module_exit(sllin_exit);