1 /* 2 * slcan.c - serial line CAN interface driver (using tty line discipline) 3 * 4 * This file is derived from linux/drivers/net/slip/slip.c and got 5 * inspiration from linux/drivers/net/can/can327.c for the rework made 6 * on the line discipline code. 7 * 8 * slip.c Authors : Laurence Culhane <loz@holmes.demon.co.uk> 9 * Fred N. van Kempen <waltje@uwalt.nl.mugnet.org> 10 * slcan.c Author : Oliver Hartkopp <socketcan@hartkopp.net> 11 * can327.c Author : Max Staudt <max-linux@enpas.org> 12 * 13 * This program is free software; you can redistribute it and/or modify it 14 * under the terms of the GNU General Public License as published by the 15 * Free Software Foundation; either version 2 of the License, or (at your 16 * option) any later version. 17 * 18 * This program is distributed in the hope that it will be useful, but 19 * WITHOUT ANY WARRANTY; without even the implied warranty of 20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 21 * General Public License for more details. 22 * 23 * You should have received a copy of the GNU General Public License along 24 * with this program; if not, see http://www.gnu.org/licenses/gpl.html 25 * 26 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 27 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 28 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 29 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 30 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 31 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 32 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 33 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 34 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 35 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 36 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH 37 * DAMAGE. 38 * 39 */ 40 41 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 42 43 #include <linux/module.h> 44 45 #include <linux/uaccess.h> 46 #include <linux/bitops.h> 47 #include <linux/string.h> 48 #include <linux/tty.h> 49 #include <linux/errno.h> 50 #include <linux/netdevice.h> 51 #include <linux/skbuff.h> 52 #include <linux/rtnetlink.h> 53 #include <linux/hex.h> 54 #include <linux/init.h> 55 #include <linux/kernel.h> 56 #include <linux/workqueue.h> 57 #include <linux/can.h> 58 #include <linux/can/dev.h> 59 #include <linux/can/skb.h> 60 61 #include "slcan.h" 62 63 MODULE_ALIAS_LDISC(N_SLCAN); 64 MODULE_DESCRIPTION("serial line CAN interface"); 65 MODULE_LICENSE("GPL"); 66 MODULE_AUTHOR("Oliver Hartkopp <socketcan@hartkopp.net>"); 67 MODULE_AUTHOR("Dario Binacchi <dario.binacchi@amarulasolutions.com>"); 68 69 /* maximum rx buffer len: extended CAN frame with timestamp */ 70 #define SLCAN_MTU (sizeof("T1111222281122334455667788EA5F\r") + 1) 71 72 #define SLCAN_CMD_LEN 1 73 #define SLCAN_SFF_ID_LEN 3 74 #define SLCAN_EFF_ID_LEN 8 75 #define SLCAN_DATA_LENGTH_LEN 1 76 #define SLCAN_ERROR_LEN 1 77 #define SLCAN_STATE_LEN 1 78 #define SLCAN_STATE_BE_RXCNT_LEN 3 79 #define SLCAN_STATE_BE_TXCNT_LEN 3 80 #define SLCAN_STATE_MSG_LEN (SLCAN_CMD_LEN + \ 81 SLCAN_STATE_LEN + \ 82 SLCAN_STATE_BE_RXCNT_LEN + \ 83 SLCAN_STATE_BE_TXCNT_LEN) 84 #define SLCAN_ERROR_MSG_LEN_MIN (SLCAN_CMD_LEN + \ 85 SLCAN_ERROR_LEN + \ 86 SLCAN_DATA_LENGTH_LEN) 87 #define SLCAN_FRAME_MSG_LEN_MIN (SLCAN_CMD_LEN + \ 88 SLCAN_SFF_ID_LEN + \ 89 SLCAN_DATA_LENGTH_LEN) 90 struct slcan { 91 struct can_priv can; 92 93 /* Various fields. */ 94 struct tty_struct *tty; /* ptr to TTY structure */ 95 struct net_device *dev; /* easy for intr handling */ 96 spinlock_t lock; 97 struct work_struct tx_work; /* Flushes transmit buffer */ 98 99 /* These are pointers to the malloc()ed frame buffers. */ 100 unsigned char rbuff[SLCAN_MTU]; /* receiver buffer */ 101 int rcount; /* received chars counter */ 102 unsigned char xbuff[SLCAN_MTU]; /* transmitter buffer*/ 103 unsigned char *xhead; /* pointer to next XMIT byte */ 104 int xleft; /* bytes left in XMIT queue */ 105 106 unsigned long flags; /* Flag values/ mode etc */ 107 #define SLF_ERROR 0 /* Parity, etc. error */ 108 #define SLF_XCMD 1 /* Command transmission */ 109 unsigned long cmd_flags; /* Command flags */ 110 #define CF_ERR_RST 0 /* Reset errors on open */ 111 wait_queue_head_t xcmd_wait; /* Wait queue for commands */ 112 /* transmission */ 113 }; 114 115 static const u32 slcan_bitrate_const[] = { 116 10000, 20000, 50000, 100000, 125000, 117 250000, 500000, 800000, 1000000 118 }; 119 120 bool slcan_err_rst_on_open(struct net_device *ndev) 121 { 122 struct slcan *sl = netdev_priv(ndev); 123 124 return !!test_bit(CF_ERR_RST, &sl->cmd_flags); 125 } 126 127 int slcan_enable_err_rst_on_open(struct net_device *ndev, bool on) 128 { 129 struct slcan *sl = netdev_priv(ndev); 130 131 if (netif_running(ndev)) 132 return -EBUSY; 133 134 if (on) 135 set_bit(CF_ERR_RST, &sl->cmd_flags); 136 else 137 clear_bit(CF_ERR_RST, &sl->cmd_flags); 138 139 return 0; 140 } 141 142 /************************************************************************* 143 * SLCAN ENCAPSULATION FORMAT * 144 *************************************************************************/ 145 146 /* A CAN frame has a can_id (11 bit standard frame format OR 29 bit extended 147 * frame format) a data length code (len) which can be from 0 to 8 148 * and up to <len> data bytes as payload. 149 * Additionally a CAN frame may become a remote transmission frame if the 150 * RTR-bit is set. This causes another ECU to send a CAN frame with the 151 * given can_id. 152 * 153 * The SLCAN ASCII representation of these different frame types is: 154 * <type> <id> <dlc> <data>* 155 * 156 * Extended frames (29 bit) are defined by capital characters in the type. 157 * RTR frames are defined as 'r' types - normal frames have 't' type: 158 * t => 11 bit data frame 159 * r => 11 bit RTR frame 160 * T => 29 bit data frame 161 * R => 29 bit RTR frame 162 * 163 * The <id> is 3 (standard) or 8 (extended) bytes in ASCII Hex (base64). 164 * The <dlc> is a one byte ASCII number ('0' - '8') 165 * The <data> section has at much ASCII Hex bytes as defined by the <dlc> 166 * 167 * Examples: 168 * 169 * t1230 : can_id 0x123, len 0, no data 170 * t4563112233 : can_id 0x456, len 3, data 0x11 0x22 0x33 171 * T12ABCDEF2AA55 : extended can_id 0x12ABCDEF, len 2, data 0xAA 0x55 172 * r1230 : can_id 0x123, len 0, no data, remote transmission request 173 * 174 */ 175 176 /************************************************************************* 177 * STANDARD SLCAN DECAPSULATION * 178 *************************************************************************/ 179 180 /* Send one completely decapsulated can_frame to the network layer */ 181 static void slcan_bump_frame(struct slcan *sl) 182 { 183 struct sk_buff *skb; 184 struct can_frame *cf; 185 int i, tmp; 186 u32 tmpid; 187 char *cmd = sl->rbuff; 188 189 if (sl->rcount < SLCAN_FRAME_MSG_LEN_MIN) 190 return; 191 192 skb = alloc_can_skb(sl->dev, &cf); 193 if (unlikely(!skb)) { 194 sl->dev->stats.rx_dropped++; 195 return; 196 } 197 198 switch (*cmd) { 199 case 'r': 200 cf->can_id = CAN_RTR_FLAG; 201 fallthrough; 202 case 't': 203 /* store dlc ASCII value and terminate SFF CAN ID string */ 204 cf->len = sl->rbuff[SLCAN_CMD_LEN + SLCAN_SFF_ID_LEN]; 205 sl->rbuff[SLCAN_CMD_LEN + SLCAN_SFF_ID_LEN] = 0; 206 /* point to payload data behind the dlc */ 207 cmd += SLCAN_CMD_LEN + SLCAN_SFF_ID_LEN + 1; 208 break; 209 case 'R': 210 cf->can_id = CAN_RTR_FLAG; 211 fallthrough; 212 case 'T': 213 cf->can_id |= CAN_EFF_FLAG; 214 /* store dlc ASCII value and terminate EFF CAN ID string */ 215 cf->len = sl->rbuff[SLCAN_CMD_LEN + SLCAN_EFF_ID_LEN]; 216 sl->rbuff[SLCAN_CMD_LEN + SLCAN_EFF_ID_LEN] = 0; 217 /* point to payload data behind the dlc */ 218 cmd += SLCAN_CMD_LEN + SLCAN_EFF_ID_LEN + 1; 219 break; 220 default: 221 goto decode_failed; 222 } 223 224 if (kstrtou32(sl->rbuff + SLCAN_CMD_LEN, 16, &tmpid)) 225 goto decode_failed; 226 227 cf->can_id |= tmpid; 228 229 /* get len from sanitized ASCII value */ 230 if (cf->len >= '0' && cf->len < '9') 231 cf->len -= '0'; 232 else 233 goto decode_failed; 234 235 /* RTR frames may have a dlc > 0 but they never have any data bytes */ 236 if (!(cf->can_id & CAN_RTR_FLAG)) { 237 for (i = 0; i < cf->len; i++) { 238 tmp = hex_to_bin(*cmd++); 239 if (tmp < 0) 240 goto decode_failed; 241 242 cf->data[i] = (tmp << 4); 243 tmp = hex_to_bin(*cmd++); 244 if (tmp < 0) 245 goto decode_failed; 246 247 cf->data[i] |= tmp; 248 } 249 } 250 251 sl->dev->stats.rx_packets++; 252 if (!(cf->can_id & CAN_RTR_FLAG)) 253 sl->dev->stats.rx_bytes += cf->len; 254 255 netif_rx(skb); 256 return; 257 258 decode_failed: 259 sl->dev->stats.rx_errors++; 260 dev_kfree_skb(skb); 261 } 262 263 /* A change state frame must contain state info and receive and transmit 264 * error counters. 265 * 266 * Examples: 267 * 268 * sb256256 : state bus-off: rx counter 256, tx counter 256 269 * sa057033 : state active, rx counter 57, tx counter 33 270 */ 271 static void slcan_bump_state(struct slcan *sl) 272 { 273 struct net_device *dev = sl->dev; 274 struct sk_buff *skb; 275 struct can_frame *cf; 276 char *cmd = sl->rbuff; 277 u32 rxerr, txerr; 278 enum can_state state, rx_state, tx_state; 279 280 switch (cmd[1]) { 281 case 'a': 282 state = CAN_STATE_ERROR_ACTIVE; 283 break; 284 case 'w': 285 state = CAN_STATE_ERROR_WARNING; 286 break; 287 case 'p': 288 state = CAN_STATE_ERROR_PASSIVE; 289 break; 290 case 'b': 291 state = CAN_STATE_BUS_OFF; 292 break; 293 default: 294 return; 295 } 296 297 if (state == sl->can.state || sl->rcount != SLCAN_STATE_MSG_LEN) 298 return; 299 300 cmd += SLCAN_STATE_BE_RXCNT_LEN + SLCAN_CMD_LEN + 1; 301 cmd[SLCAN_STATE_BE_TXCNT_LEN] = 0; 302 if (kstrtou32(cmd, 10, &txerr)) 303 return; 304 305 *cmd = 0; 306 cmd -= SLCAN_STATE_BE_RXCNT_LEN; 307 if (kstrtou32(cmd, 10, &rxerr)) 308 return; 309 310 skb = alloc_can_err_skb(dev, &cf); 311 312 tx_state = txerr >= rxerr ? state : 0; 313 rx_state = txerr <= rxerr ? state : 0; 314 can_change_state(dev, cf, tx_state, rx_state); 315 316 if (state == CAN_STATE_BUS_OFF) { 317 can_bus_off(dev); 318 } else if (skb) { 319 cf->can_id |= CAN_ERR_CNT; 320 cf->data[6] = txerr; 321 cf->data[7] = rxerr; 322 } 323 324 if (skb) 325 netif_rx(skb); 326 } 327 328 /* An error frame can contain more than one type of error. 329 * 330 * Examples: 331 * 332 * e1a : len 1, errors: ACK error 333 * e3bcO: len 3, errors: Bit0 error, CRC error, Tx overrun error 334 */ 335 static void slcan_bump_err(struct slcan *sl) 336 { 337 struct net_device *dev = sl->dev; 338 struct sk_buff *skb; 339 struct can_frame *cf; 340 char *cmd = sl->rbuff; 341 bool rx_errors = false, tx_errors = false, rx_over_errors = false; 342 int i, len; 343 344 if (sl->rcount < SLCAN_ERROR_MSG_LEN_MIN) 345 return; 346 347 /* get len from sanitized ASCII value */ 348 len = cmd[1]; 349 if (len >= '0' && len < '9') 350 len -= '0'; 351 else 352 return; 353 354 if ((len + SLCAN_CMD_LEN + 1) > sl->rcount) 355 return; 356 357 skb = alloc_can_err_skb(dev, &cf); 358 359 if (skb) 360 cf->can_id |= CAN_ERR_PROT | CAN_ERR_BUSERROR; 361 362 cmd += SLCAN_CMD_LEN + 1; 363 for (i = 0; i < len; i++, cmd++) { 364 switch (*cmd) { 365 case 'a': 366 netdev_dbg(dev, "ACK error\n"); 367 tx_errors = true; 368 if (skb) { 369 cf->can_id |= CAN_ERR_ACK; 370 cf->data[3] = CAN_ERR_PROT_LOC_ACK; 371 } 372 373 break; 374 case 'b': 375 netdev_dbg(dev, "Bit0 error\n"); 376 tx_errors = true; 377 if (skb) 378 cf->data[2] |= CAN_ERR_PROT_BIT0; 379 380 break; 381 case 'B': 382 netdev_dbg(dev, "Bit1 error\n"); 383 tx_errors = true; 384 if (skb) 385 cf->data[2] |= CAN_ERR_PROT_BIT1; 386 387 break; 388 case 'c': 389 netdev_dbg(dev, "CRC error\n"); 390 rx_errors = true; 391 if (skb) { 392 cf->data[2] |= CAN_ERR_PROT_BIT; 393 cf->data[3] = CAN_ERR_PROT_LOC_CRC_SEQ; 394 } 395 396 break; 397 case 'f': 398 netdev_dbg(dev, "Form Error\n"); 399 rx_errors = true; 400 if (skb) 401 cf->data[2] |= CAN_ERR_PROT_FORM; 402 403 break; 404 case 'o': 405 netdev_dbg(dev, "Rx overrun error\n"); 406 rx_over_errors = true; 407 rx_errors = true; 408 if (skb) { 409 cf->can_id |= CAN_ERR_CRTL; 410 cf->data[1] = CAN_ERR_CRTL_RX_OVERFLOW; 411 } 412 413 break; 414 case 'O': 415 netdev_dbg(dev, "Tx overrun error\n"); 416 tx_errors = true; 417 if (skb) { 418 cf->can_id |= CAN_ERR_CRTL; 419 cf->data[1] = CAN_ERR_CRTL_TX_OVERFLOW; 420 } 421 422 break; 423 case 's': 424 netdev_dbg(dev, "Stuff error\n"); 425 rx_errors = true; 426 if (skb) 427 cf->data[2] |= CAN_ERR_PROT_STUFF; 428 429 break; 430 default: 431 if (skb) 432 dev_kfree_skb(skb); 433 434 return; 435 } 436 } 437 438 if (rx_errors) 439 dev->stats.rx_errors++; 440 441 if (rx_over_errors) 442 dev->stats.rx_over_errors++; 443 444 if (tx_errors) 445 dev->stats.tx_errors++; 446 447 if (skb) 448 netif_rx(skb); 449 } 450 451 static void slcan_bump(struct slcan *sl) 452 { 453 switch (sl->rbuff[0]) { 454 case 'r': 455 fallthrough; 456 case 't': 457 fallthrough; 458 case 'R': 459 fallthrough; 460 case 'T': 461 return slcan_bump_frame(sl); 462 case 'e': 463 return slcan_bump_err(sl); 464 case 's': 465 return slcan_bump_state(sl); 466 default: 467 return; 468 } 469 } 470 471 /* parse tty input stream */ 472 static void slcan_unesc(struct slcan *sl, unsigned char s) 473 { 474 if ((s == '\r') || (s == '\a')) { /* CR or BEL ends the pdu */ 475 if (!test_and_clear_bit(SLF_ERROR, &sl->flags)) 476 slcan_bump(sl); 477 478 sl->rcount = 0; 479 } else { 480 if (!test_bit(SLF_ERROR, &sl->flags)) { 481 if (sl->rcount < SLCAN_MTU) { 482 sl->rbuff[sl->rcount++] = s; 483 return; 484 } 485 486 sl->dev->stats.rx_over_errors++; 487 set_bit(SLF_ERROR, &sl->flags); 488 } 489 } 490 } 491 492 /************************************************************************* 493 * STANDARD SLCAN ENCAPSULATION * 494 *************************************************************************/ 495 496 /* Encapsulate one can_frame and stuff into a TTY queue. */ 497 static void slcan_encaps(struct slcan *sl, struct can_frame *cf) 498 { 499 int actual, i; 500 unsigned char *pos; 501 unsigned char *endpos; 502 canid_t id = cf->can_id; 503 504 pos = sl->xbuff; 505 506 if (cf->can_id & CAN_RTR_FLAG) 507 *pos = 'R'; /* becomes 'r' in standard frame format (SFF) */ 508 else 509 *pos = 'T'; /* becomes 't' in standard frame format (SSF) */ 510 511 /* determine number of chars for the CAN-identifier */ 512 if (cf->can_id & CAN_EFF_FLAG) { 513 id &= CAN_EFF_MASK; 514 endpos = pos + SLCAN_EFF_ID_LEN; 515 } else { 516 *pos |= 0x20; /* convert R/T to lower case for SFF */ 517 id &= CAN_SFF_MASK; 518 endpos = pos + SLCAN_SFF_ID_LEN; 519 } 520 521 /* build 3 (SFF) or 8 (EFF) digit CAN identifier */ 522 pos++; 523 while (endpos >= pos) { 524 *endpos-- = hex_asc_upper[id & 0xf]; 525 id >>= 4; 526 } 527 528 pos += (cf->can_id & CAN_EFF_FLAG) ? 529 SLCAN_EFF_ID_LEN : SLCAN_SFF_ID_LEN; 530 531 *pos++ = cf->len + '0'; 532 533 /* RTR frames may have a dlc > 0 but they never have any data bytes */ 534 if (!(cf->can_id & CAN_RTR_FLAG)) { 535 for (i = 0; i < cf->len; i++) 536 pos = hex_byte_pack_upper(pos, cf->data[i]); 537 538 sl->dev->stats.tx_bytes += cf->len; 539 } 540 541 *pos++ = '\r'; 542 543 /* Order of next two lines is *very* important. 544 * When we are sending a little amount of data, 545 * the transfer may be completed inside the ops->write() 546 * routine, because it's running with interrupts enabled. 547 * In this case we *never* got WRITE_WAKEUP event, 548 * if we did not request it before write operation. 549 * 14 Oct 1994 Dmitry Gorodchanin. 550 */ 551 set_bit(TTY_DO_WRITE_WAKEUP, &sl->tty->flags); 552 actual = sl->tty->ops->write(sl->tty, sl->xbuff, pos - sl->xbuff); 553 sl->xleft = (pos - sl->xbuff) - actual; 554 sl->xhead = sl->xbuff + actual; 555 } 556 557 /* Write out any remaining transmit buffer. Scheduled when tty is writable */ 558 static void slcan_transmit(struct work_struct *work) 559 { 560 struct slcan *sl = container_of(work, struct slcan, tx_work); 561 int actual; 562 563 spin_lock_bh(&sl->lock); 564 /* First make sure we're connected. */ 565 if (unlikely(!netif_running(sl->dev)) && 566 likely(!test_bit(SLF_XCMD, &sl->flags))) { 567 spin_unlock_bh(&sl->lock); 568 return; 569 } 570 571 if (sl->xleft <= 0) { 572 if (unlikely(test_bit(SLF_XCMD, &sl->flags))) { 573 clear_bit(SLF_XCMD, &sl->flags); 574 clear_bit(TTY_DO_WRITE_WAKEUP, &sl->tty->flags); 575 spin_unlock_bh(&sl->lock); 576 wake_up(&sl->xcmd_wait); 577 return; 578 } 579 580 /* Now serial buffer is almost free & we can start 581 * transmission of another packet 582 */ 583 sl->dev->stats.tx_packets++; 584 clear_bit(TTY_DO_WRITE_WAKEUP, &sl->tty->flags); 585 spin_unlock_bh(&sl->lock); 586 netif_wake_queue(sl->dev); 587 return; 588 } 589 590 actual = sl->tty->ops->write(sl->tty, sl->xhead, sl->xleft); 591 sl->xleft -= actual; 592 sl->xhead += actual; 593 spin_unlock_bh(&sl->lock); 594 } 595 596 /* Called by the driver when there's room for more data. 597 * Schedule the transmit. 598 */ 599 static void slcan_write_wakeup(struct tty_struct *tty) 600 { 601 struct slcan *sl = tty->disc_data; 602 603 schedule_work(&sl->tx_work); 604 } 605 606 /* Send a can_frame to a TTY queue. */ 607 static netdev_tx_t slcan_netdev_xmit(struct sk_buff *skb, 608 struct net_device *dev) 609 { 610 struct slcan *sl = netdev_priv(dev); 611 612 if (can_dev_dropped_skb(dev, skb)) 613 return NETDEV_TX_OK; 614 615 spin_lock(&sl->lock); 616 if (!netif_running(dev)) { 617 spin_unlock(&sl->lock); 618 netdev_warn(dev, "xmit: iface is down\n"); 619 goto out; 620 } 621 if (!sl->tty) { 622 spin_unlock(&sl->lock); 623 goto out; 624 } 625 626 netif_stop_queue(sl->dev); 627 slcan_encaps(sl, (struct can_frame *)skb->data); /* encaps & send */ 628 spin_unlock(&sl->lock); 629 630 skb_tx_timestamp(skb); 631 632 out: 633 kfree_skb(skb); 634 return NETDEV_TX_OK; 635 } 636 637 /****************************************** 638 * Routines looking at netdevice side. 639 ******************************************/ 640 641 static int slcan_transmit_cmd(struct slcan *sl, const unsigned char *cmd) 642 { 643 int ret, actual, n; 644 645 spin_lock(&sl->lock); 646 if (!sl->tty) { 647 spin_unlock(&sl->lock); 648 return -ENODEV; 649 } 650 651 n = scnprintf(sl->xbuff, sizeof(sl->xbuff), "%s", cmd); 652 set_bit(TTY_DO_WRITE_WAKEUP, &sl->tty->flags); 653 actual = sl->tty->ops->write(sl->tty, sl->xbuff, n); 654 sl->xleft = n - actual; 655 sl->xhead = sl->xbuff + actual; 656 set_bit(SLF_XCMD, &sl->flags); 657 spin_unlock(&sl->lock); 658 ret = wait_event_interruptible_timeout(sl->xcmd_wait, 659 !test_bit(SLF_XCMD, &sl->flags), 660 HZ); 661 clear_bit(SLF_XCMD, &sl->flags); 662 if (ret == -ERESTARTSYS) 663 return ret; 664 665 if (ret == 0) 666 return -ETIMEDOUT; 667 668 return 0; 669 } 670 671 /* Netdevice UP -> DOWN routine */ 672 static int slcan_netdev_close(struct net_device *dev) 673 { 674 struct slcan *sl = netdev_priv(dev); 675 int err; 676 677 if (sl->can.bittiming.bitrate && 678 sl->can.bittiming.bitrate != CAN_BITRATE_UNKNOWN) { 679 err = slcan_transmit_cmd(sl, "C\r"); 680 if (err) 681 netdev_warn(dev, 682 "failed to send close command 'C\\r'\n"); 683 } 684 685 /* TTY discipline is running. */ 686 clear_bit(TTY_DO_WRITE_WAKEUP, &sl->tty->flags); 687 flush_work(&sl->tx_work); 688 689 netif_stop_queue(dev); 690 sl->rcount = 0; 691 sl->xleft = 0; 692 close_candev(dev); 693 sl->can.state = CAN_STATE_STOPPED; 694 if (sl->can.bittiming.bitrate == CAN_BITRATE_UNKNOWN) 695 sl->can.bittiming.bitrate = CAN_BITRATE_UNSET; 696 697 return 0; 698 } 699 700 /* Netdevice DOWN -> UP routine */ 701 static int slcan_netdev_open(struct net_device *dev) 702 { 703 struct slcan *sl = netdev_priv(dev); 704 unsigned char cmd[SLCAN_MTU]; 705 int err, s; 706 707 /* The baud rate is not set with the command 708 * `ip link set <iface> type can bitrate <baud>' and therefore 709 * can.bittiming.bitrate is CAN_BITRATE_UNSET (0), causing 710 * open_candev() to fail. So let's set to a fake value. 711 */ 712 if (sl->can.bittiming.bitrate == CAN_BITRATE_UNSET) 713 sl->can.bittiming.bitrate = CAN_BITRATE_UNKNOWN; 714 715 err = open_candev(dev); 716 if (err) { 717 netdev_err(dev, "failed to open can device\n"); 718 return err; 719 } 720 721 if (sl->can.bittiming.bitrate != CAN_BITRATE_UNKNOWN) { 722 for (s = 0; s < ARRAY_SIZE(slcan_bitrate_const); s++) { 723 if (sl->can.bittiming.bitrate == slcan_bitrate_const[s]) 724 break; 725 } 726 727 /* The CAN framework has already validate the bitrate value, 728 * so we can avoid to check if `s' has been properly set. 729 */ 730 snprintf(cmd, sizeof(cmd), "C\rS%d\r", s); 731 err = slcan_transmit_cmd(sl, cmd); 732 if (err) { 733 netdev_err(dev, 734 "failed to send bitrate command 'C\\rS%d\\r'\n", 735 s); 736 goto cmd_transmit_failed; 737 } 738 739 if (test_bit(CF_ERR_RST, &sl->cmd_flags)) { 740 err = slcan_transmit_cmd(sl, "F\r"); 741 if (err) { 742 netdev_err(dev, 743 "failed to send error command 'F\\r'\n"); 744 goto cmd_transmit_failed; 745 } 746 } 747 748 if (sl->can.ctrlmode & CAN_CTRLMODE_LISTENONLY) { 749 err = slcan_transmit_cmd(sl, "L\r"); 750 if (err) { 751 netdev_err(dev, 752 "failed to send listen-only command 'L\\r'\n"); 753 goto cmd_transmit_failed; 754 } 755 } else { 756 err = slcan_transmit_cmd(sl, "O\r"); 757 if (err) { 758 netdev_err(dev, 759 "failed to send open command 'O\\r'\n"); 760 goto cmd_transmit_failed; 761 } 762 } 763 } 764 765 sl->can.state = CAN_STATE_ERROR_ACTIVE; 766 netif_start_queue(dev); 767 return 0; 768 769 cmd_transmit_failed: 770 close_candev(dev); 771 return err; 772 } 773 774 static const struct net_device_ops slcan_netdev_ops = { 775 .ndo_open = slcan_netdev_open, 776 .ndo_stop = slcan_netdev_close, 777 .ndo_start_xmit = slcan_netdev_xmit, 778 }; 779 780 /****************************************** 781 * Routines looking at TTY side. 782 ******************************************/ 783 784 /* Handle the 'receiver data ready' interrupt. 785 * This function is called by the 'tty_io' module in the kernel when 786 * a block of SLCAN data has been received, which can now be decapsulated 787 * and sent on to some IP layer for further processing. This will not 788 * be re-entered while running but other ldisc functions may be called 789 * in parallel 790 */ 791 static void slcan_receive_buf(struct tty_struct *tty, const u8 *cp, 792 const u8 *fp, size_t count) 793 { 794 struct slcan *sl = tty->disc_data; 795 796 if (!netif_running(sl->dev)) 797 return; 798 799 /* Read the characters out of the buffer */ 800 while (count--) { 801 if (fp && *fp++) { 802 if (!test_and_set_bit(SLF_ERROR, &sl->flags)) 803 sl->dev->stats.rx_errors++; 804 cp++; 805 continue; 806 } 807 slcan_unesc(sl, *cp++); 808 } 809 } 810 811 /* Open the high-level part of the SLCAN channel. 812 * This function is called by the TTY module when the 813 * SLCAN line discipline is called for. 814 * 815 * Called in process context serialized from other ldisc calls. 816 */ 817 static int slcan_open(struct tty_struct *tty) 818 { 819 struct net_device *dev; 820 struct slcan *sl; 821 int err; 822 823 if (!capable(CAP_NET_ADMIN)) 824 return -EPERM; 825 826 if (!tty->ops->write) 827 return -EOPNOTSUPP; 828 829 dev = alloc_candev(sizeof(*sl), 1); 830 if (!dev) 831 return -ENFILE; 832 833 sl = netdev_priv(dev); 834 835 /* Configure TTY interface */ 836 tty->receive_room = 65536; /* We don't flow control */ 837 sl->rcount = 0; 838 sl->xleft = 0; 839 spin_lock_init(&sl->lock); 840 INIT_WORK(&sl->tx_work, slcan_transmit); 841 init_waitqueue_head(&sl->xcmd_wait); 842 843 /* Configure CAN metadata */ 844 sl->can.bitrate_const = slcan_bitrate_const; 845 sl->can.bitrate_const_cnt = ARRAY_SIZE(slcan_bitrate_const); 846 sl->can.ctrlmode_supported = CAN_CTRLMODE_LISTENONLY; 847 848 /* Configure netdev interface */ 849 sl->dev = dev; 850 dev->netdev_ops = &slcan_netdev_ops; 851 dev->ethtool_ops = &slcan_ethtool_ops; 852 853 /* Mark ldisc channel as alive */ 854 sl->tty = tty; 855 tty->disc_data = sl; 856 857 err = register_candev(dev); 858 if (err) { 859 free_candev(dev); 860 pr_err("can't register candev\n"); 861 return err; 862 } 863 864 netdev_info(dev, "slcan on %s.\n", tty->name); 865 /* TTY layer expects 0 on success */ 866 return 0; 867 } 868 869 /* Close down a SLCAN channel. 870 * This means flushing out any pending queues, and then returning. This 871 * call is serialized against other ldisc functions. 872 * Once this is called, no other ldisc function of ours is entered. 873 * 874 * We also use this method for a hangup event. 875 */ 876 static void slcan_close(struct tty_struct *tty) 877 { 878 struct slcan *sl = tty->disc_data; 879 880 unregister_candev(sl->dev); 881 882 /* 883 * The netdev needn't be UP (so .ndo_stop() is not called). Hence make 884 * sure this is not running before freeing it up. 885 */ 886 flush_work(&sl->tx_work); 887 888 /* Mark channel as dead */ 889 spin_lock_bh(&sl->lock); 890 tty->disc_data = NULL; 891 sl->tty = NULL; 892 spin_unlock_bh(&sl->lock); 893 894 netdev_info(sl->dev, "slcan off %s.\n", tty->name); 895 free_candev(sl->dev); 896 } 897 898 /* Perform I/O control on an active SLCAN channel. */ 899 static int slcan_ioctl(struct tty_struct *tty, unsigned int cmd, 900 unsigned long arg) 901 { 902 struct slcan *sl = tty->disc_data; 903 unsigned int tmp; 904 905 switch (cmd) { 906 case SIOCGIFNAME: 907 tmp = strlen(sl->dev->name) + 1; 908 if (copy_to_user((void __user *)arg, sl->dev->name, tmp)) 909 return -EFAULT; 910 return 0; 911 912 case SIOCSIFHWADDR: 913 return -EINVAL; 914 915 default: 916 return tty_mode_ioctl(tty, cmd, arg); 917 } 918 } 919 920 static struct tty_ldisc_ops slcan_ldisc = { 921 .owner = THIS_MODULE, 922 .num = N_SLCAN, 923 .name = KBUILD_MODNAME, 924 .open = slcan_open, 925 .close = slcan_close, 926 .ioctl = slcan_ioctl, 927 .receive_buf = slcan_receive_buf, 928 .write_wakeup = slcan_write_wakeup, 929 }; 930 931 static int __init slcan_init(void) 932 { 933 int status; 934 935 pr_info("serial line CAN interface driver\n"); 936 937 /* Fill in our line protocol discipline, and register it */ 938 status = tty_register_ldisc(&slcan_ldisc); 939 if (status) 940 pr_err("can't register line discipline\n"); 941 942 return status; 943 } 944 945 static void __exit slcan_exit(void) 946 { 947 /* This will only be called when all channels have been closed by 948 * userspace - tty_ldisc.c takes care of the module's refcount. 949 */ 950 tty_unregister_ldisc(&slcan_ldisc); 951 } 952 953 module_init(slcan_init); 954 module_exit(slcan_exit); 955