| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 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 <[email protected]>"); |
| 67 |
MODULE_AUTHOR("Dario Binacchi <[email protected]>"); |
| 68 |
|
| 69 |
|
| 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 |
|
| 94 |
struct tty_struct *tty; |
| 95 |
struct net_device *dev; |
| 96 |
spinlock_t lock; |
| 97 |
struct work_struct tx_work; |
| 98 |
|
| 99 |
|
| 100 |
unsigned char rbuff[SLCAN_MTU]; |
| 101 |
int rcount; |
| 102 |
unsigned char xbuff[SLCAN_MTU]; |
| 103 |
unsigned char *xhead; |
| 104 |
int xleft; |
| 105 |
|
| 106 |
unsigned long flags; |
| 107 |
#define SLF_ERROR 0 |
| 108 |
#define SLF_XCMD 1 |
| 109 |
unsigned long cmd_flags; |
| 110 |
#define CF_ERR_RST 0 |
| 111 |
wait_queue_head_t xcmd_wait; |
| 112 |
|
| 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 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 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 |
|
| 204 |
cf->len = sl->rbuff[SLCAN_CMD_LEN + SLCAN_SFF_ID_LEN]; |
| 205 |
sl->rbuff[SLCAN_CMD_LEN + SLCAN_SFF_ID_LEN] = 0; |
| 206 |
|
| 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 |
|
| 215 |
cf->len = sl->rbuff[SLCAN_CMD_LEN + SLCAN_EFF_ID_LEN]; |
| 216 |
sl->rbuff[SLCAN_CMD_LEN + SLCAN_EFF_ID_LEN] = 0; |
| 217 |
|
| 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 |
|
| 230 |
if (cf->len >= '0' && cf->len < '9') |
| 231 |
cf->len -= '0'; |
| 232 |
else |
| 233 |
goto decode_failed; |
| 234 |
|
| 235 |
|
| 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 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 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 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 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 |
|
| 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 |
|
| 472 |
static void slcan_unesc(struct slcan *sl, unsigned char s) |
| 473 |
{ |
| 474 |
if ((s == '\r') || (s == '\a')) { |
| 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 |
|
| 494 |
|
| 495 |
|
| 496 |
|
| 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'; |
| 508 |
else |
| 509 |
*pos = 'T'; |
| 510 |
|
| 511 |
|
| 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; |
| 517 |
id &= CAN_SFF_MASK; |
| 518 |
endpos = pos + SLCAN_SFF_ID_LEN; |
| 519 |
} |
| 520 |
|
| 521 |
|
| 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 |
|
| 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 |
|
| 544 |
|
| 545 |
|
| 546 |
|
| 547 |
|
| 548 |
|
| 549 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 581 |
|
| 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 |
|
| 597 |
|
| 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 |
|
| 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); |
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 708 |
|
| 709 |
|
| 710 |
|
| 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 |
|
| 728 |
|
| 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 |
|
| 782 |
|
| 783 |
|
| 784 |
|
| 785 |
|
| 786 |
|
| 787 |
|
| 788 |
|
| 789 |
|
| 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 |
|
| 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 |
|
| 812 |
|
| 813 |
|
| 814 |
|
| 815 |
|
| 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 |
|
| 836 |
tty->receive_room = 65536; |
| 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 |
|
| 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 |
|
| 849 |
sl->dev = dev; |
| 850 |
dev->netdev_ops = &slcan_netdev_ops; |
| 851 |
dev->ethtool_ops = &slcan_ethtool_ops; |
| 852 |
|
| 853 |
|
| 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 |
|
| 866 |
return 0; |
| 867 |
} |
| 868 |
|
| 869 |
|
| 870 |
|
| 871 |
|
| 872 |
|
| 873 |
|
| 874 |
|
| 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 |
|
| 884 |
|
| 885 |
|
| 886 |
flush_work(&sl->tx_work); |
| 887 |
|
| 888 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 948 |
|
| 949 |
|
| 950 |
tty_unregister_ldisc(&slcan_ldisc); |
| 951 |
} |
| 952 |
|
| 953 |
module_init(slcan_init); |
| 954 |
module_exit(slcan_exit); |