1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Copyright (C) 2008-2010 4 * 5 * - Kurt Van Dijck, EIA Electronics 6 */ 7 8 #include <linux/module.h> 9 #include <linux/interrupt.h> 10 #include <asm/io.h> 11 12 #include "softing.h" 13 14 #define TX_ECHO_SKB_MAX (((TXMAX+1)/2)-1) 15 16 /* 17 * test is a specific CAN netdev 18 * is online (ie. up 'n running, not sleeping, not busoff 19 */ 20 static inline int canif_is_active(struct net_device *netdev) 21 { 22 struct can_priv *can = netdev_priv(netdev); 23 24 if (!netif_running(netdev)) 25 return 0; 26 return (can->state <= CAN_STATE_ERROR_PASSIVE); 27 } 28 29 /* reset DPRAM */ 30 static inline void softing_set_reset_dpram(struct softing *card) 31 { 32 if (card->pdat->generation >= 2) { 33 spin_lock_bh(&card->spin); 34 iowrite8(ioread8(&card->dpram[DPRAM_V2_RESET]) & ~1, 35 &card->dpram[DPRAM_V2_RESET]); 36 spin_unlock_bh(&card->spin); 37 } 38 } 39 40 static inline void softing_clr_reset_dpram(struct softing *card) 41 { 42 if (card->pdat->generation >= 2) { 43 spin_lock_bh(&card->spin); 44 iowrite8(ioread8(&card->dpram[DPRAM_V2_RESET]) | 1, 45 &card->dpram[DPRAM_V2_RESET]); 46 spin_unlock_bh(&card->spin); 47 } 48 } 49 50 /* trigger the tx queue-ing */ 51 static netdev_tx_t softing_netdev_start_xmit(struct sk_buff *skb, 52 struct net_device *dev) 53 { 54 struct softing_priv *priv = netdev_priv(dev); 55 struct softing *card = priv->card; 56 int ret; 57 uint8_t *ptr; 58 uint8_t fifo_wr, fifo_rd; 59 struct can_frame *cf = (struct can_frame *)skb->data; 60 uint8_t buf[DPRAM_TX_SIZE]; 61 62 if (can_dropped_invalid_skb(dev, skb)) 63 return NETDEV_TX_OK; 64 65 spin_lock(&card->spin); 66 67 ret = NETDEV_TX_BUSY; 68 if (!card->fw.up || 69 (card->tx.pending >= TXMAX) || 70 (priv->tx.pending >= TX_ECHO_SKB_MAX)) 71 goto xmit_done; 72 fifo_wr = ioread8(&card->dpram[DPRAM_TX_WR]); 73 fifo_rd = ioread8(&card->dpram[DPRAM_TX_RD]); 74 if (fifo_wr == fifo_rd) 75 /* fifo full */ 76 goto xmit_done; 77 memset(buf, 0, sizeof(buf)); 78 ptr = buf; 79 *ptr = CMD_TX; 80 if (cf->can_id & CAN_RTR_FLAG) 81 *ptr |= CMD_RTR; 82 if (cf->can_id & CAN_EFF_FLAG) 83 *ptr |= CMD_XTD; 84 if (priv->index) 85 *ptr |= CMD_BUS2; 86 ++ptr; 87 *ptr++ = cf->len; 88 *ptr++ = (cf->can_id >> 0); 89 *ptr++ = (cf->can_id >> 8); 90 if (cf->can_id & CAN_EFF_FLAG) { 91 *ptr++ = (cf->can_id >> 16); 92 *ptr++ = (cf->can_id >> 24); 93 } else { 94 /* increment 1, not 2 as you might think */ 95 ptr += 1; 96 } 97 if (!(cf->can_id & CAN_RTR_FLAG)) 98 memcpy(ptr, &cf->data[0], cf->len); 99 memcpy_toio(&card->dpram[DPRAM_TX + DPRAM_TX_SIZE * fifo_wr], 100 buf, DPRAM_TX_SIZE); 101 if (++fifo_wr >= DPRAM_TX_CNT) 102 fifo_wr = 0; 103 iowrite8(fifo_wr, &card->dpram[DPRAM_TX_WR]); 104 card->tx.last_bus = priv->index; 105 ++card->tx.pending; 106 ++priv->tx.pending; 107 can_put_echo_skb(skb, dev, priv->tx.echo_put, 0); 108 ++priv->tx.echo_put; 109 if (priv->tx.echo_put >= TX_ECHO_SKB_MAX) 110 priv->tx.echo_put = 0; 111 /* can_put_echo_skb() saves the skb, safe to return TX_OK */ 112 ret = NETDEV_TX_OK; 113 xmit_done: 114 spin_unlock(&card->spin); 115 if (card->tx.pending >= TXMAX) { 116 int j; 117 for (j = 0; j < ARRAY_SIZE(card->net); ++j) { 118 if (card->net[j]) 119 netif_stop_queue(card->net[j]); 120 } 121 } 122 if (ret != NETDEV_TX_OK) 123 netif_stop_queue(dev); 124 125 return ret; 126 } 127 128 /* 129 * shortcut for skb delivery 130 */ 131 int softing_netdev_rx(struct net_device *netdev, const struct can_frame *msg, 132 ktime_t ktime) 133 { 134 struct sk_buff *skb; 135 struct can_frame *cf; 136 137 skb = alloc_can_skb(netdev, &cf); 138 if (!skb) 139 return -ENOMEM; 140 memcpy(cf, msg, sizeof(*msg)); 141 skb->tstamp = ktime; 142 return netif_rx(skb); 143 } 144 145 /* 146 * softing_handle_1 147 * pop 1 entry from the DPRAM queue, and process 148 */ 149 static int softing_handle_1(struct softing *card) 150 { 151 struct net_device *netdev; 152 struct softing_priv *priv; 153 ktime_t ktime; 154 struct can_frame msg; 155 int cnt = 0, lost_msg; 156 uint8_t fifo_rd, fifo_wr, cmd; 157 uint8_t *ptr; 158 uint32_t tmp_u32; 159 uint8_t buf[DPRAM_RX_SIZE]; 160 161 memset(&msg, 0, sizeof(msg)); 162 /* test for lost msgs */ 163 lost_msg = ioread8(&card->dpram[DPRAM_RX_LOST]); 164 if (lost_msg) { 165 int j; 166 /* reset condition */ 167 iowrite8(0, &card->dpram[DPRAM_RX_LOST]); 168 /* prepare msg */ 169 msg.can_id = CAN_ERR_FLAG | CAN_ERR_CRTL; 170 msg.len = CAN_ERR_DLC; 171 msg.data[1] = CAN_ERR_CRTL_RX_OVERFLOW; 172 /* 173 * service to all buses, we don't know which it was applicable 174 * but only service buses that are online 175 */ 176 for (j = 0; j < ARRAY_SIZE(card->net); ++j) { 177 netdev = card->net[j]; 178 if (!netdev) 179 continue; 180 if (!canif_is_active(netdev)) 181 /* a dead bus has no overflows */ 182 continue; 183 ++netdev->stats.rx_over_errors; 184 softing_netdev_rx(netdev, &msg, 0); 185 } 186 /* prepare for other use */ 187 memset(&msg, 0, sizeof(msg)); 188 ++cnt; 189 } 190 191 fifo_rd = ioread8(&card->dpram[DPRAM_RX_RD]); 192 fifo_wr = ioread8(&card->dpram[DPRAM_RX_WR]); 193 194 if (++fifo_rd >= DPRAM_RX_CNT) 195 fifo_rd = 0; 196 if (fifo_wr == fifo_rd) 197 return cnt; 198 199 memcpy_fromio(buf, &card->dpram[DPRAM_RX + DPRAM_RX_SIZE*fifo_rd], 200 DPRAM_RX_SIZE); 201 mb(); 202 /* trigger dual port RAM */ 203 iowrite8(fifo_rd, &card->dpram[DPRAM_RX_RD]); 204 205 ptr = buf; 206 cmd = *ptr++; 207 if (cmd == 0xff) 208 /* not quite useful, probably the card has got out */ 209 return 0; 210 netdev = card->net[0]; 211 if (cmd & CMD_BUS2) 212 netdev = card->net[1]; 213 priv = netdev_priv(netdev); 214 215 if (cmd & CMD_ERR) { 216 uint8_t can_state, state; 217 218 state = *ptr++; 219 220 msg.can_id = CAN_ERR_FLAG; 221 msg.len = CAN_ERR_DLC; 222 223 if (state & SF_MASK_BUSOFF) { 224 can_state = CAN_STATE_BUS_OFF; 225 msg.can_id |= CAN_ERR_BUSOFF; 226 state = STATE_BUSOFF; 227 } else if (state & SF_MASK_EPASSIVE) { 228 can_state = CAN_STATE_ERROR_PASSIVE; 229 msg.can_id |= CAN_ERR_CRTL; 230 msg.data[1] = CAN_ERR_CRTL_TX_PASSIVE; 231 state = STATE_EPASSIVE; 232 } else { 233 can_state = CAN_STATE_ERROR_ACTIVE; 234 msg.can_id |= CAN_ERR_CRTL; 235 state = STATE_EACTIVE; 236 } 237 /* update DPRAM */ 238 iowrite8(state, &card->dpram[priv->index ? 239 DPRAM_INFO_BUSSTATE2 : DPRAM_INFO_BUSSTATE]); 240 /* timestamp */ 241 tmp_u32 = le32_to_cpup((void *)ptr); 242 ptr += 4; 243 ktime = softing_raw2ktime(card, tmp_u32); 244 245 ++netdev->stats.rx_errors; 246 /* update internal status */ 247 if (can_state != priv->can.state) { 248 priv->can.state = can_state; 249 if (can_state == CAN_STATE_ERROR_PASSIVE) 250 ++priv->can.can_stats.error_passive; 251 else if (can_state == CAN_STATE_BUS_OFF) { 252 /* this calls can_close_cleanup() */ 253 ++priv->can.can_stats.bus_off; 254 can_bus_off(netdev); 255 netif_stop_queue(netdev); 256 } 257 /* trigger socketcan */ 258 softing_netdev_rx(netdev, &msg, ktime); 259 } 260 261 } else { 262 if (cmd & CMD_RTR) 263 msg.can_id |= CAN_RTR_FLAG; 264 msg.len = can_cc_dlc2len(*ptr++); 265 if (cmd & CMD_XTD) { 266 msg.can_id |= CAN_EFF_FLAG; 267 msg.can_id |= le32_to_cpup((void *)ptr); 268 ptr += 4; 269 } else { 270 msg.can_id |= le16_to_cpup((void *)ptr); 271 ptr += 2; 272 } 273 /* timestamp */ 274 tmp_u32 = le32_to_cpup((void *)ptr); 275 ptr += 4; 276 ktime = softing_raw2ktime(card, tmp_u32); 277 if (!(msg.can_id & CAN_RTR_FLAG)) 278 memcpy(&msg.data[0], ptr, 8); 279 ptr += 8; 280 /* update socket */ 281 if (cmd & CMD_ACK) { 282 /* acknowledge, was tx msg */ 283 struct sk_buff *skb; 284 skb = priv->can.echo_skb[priv->tx.echo_get]; 285 if (skb) 286 skb->tstamp = ktime; 287 can_get_echo_skb(netdev, priv->tx.echo_get, NULL); 288 ++priv->tx.echo_get; 289 if (priv->tx.echo_get >= TX_ECHO_SKB_MAX) 290 priv->tx.echo_get = 0; 291 if (priv->tx.pending) 292 --priv->tx.pending; 293 if (card->tx.pending) 294 --card->tx.pending; 295 ++netdev->stats.tx_packets; 296 if (!(msg.can_id & CAN_RTR_FLAG)) 297 netdev->stats.tx_bytes += msg.len; 298 } else { 299 int ret; 300 301 ret = softing_netdev_rx(netdev, &msg, ktime); 302 if (ret == NET_RX_SUCCESS) { 303 ++netdev->stats.rx_packets; 304 if (!(msg.can_id & CAN_RTR_FLAG)) 305 netdev->stats.rx_bytes += msg.len; 306 } else { 307 ++netdev->stats.rx_dropped; 308 } 309 } 310 } 311 ++cnt; 312 return cnt; 313 } 314 315 /* 316 * real interrupt handler 317 */ 318 static irqreturn_t softing_irq_thread(int irq, void *dev_id) 319 { 320 struct softing *card = (struct softing *)dev_id; 321 struct net_device *netdev; 322 struct softing_priv *priv; 323 int j, offset, work_done; 324 325 work_done = 0; 326 spin_lock_bh(&card->spin); 327 while (softing_handle_1(card) > 0) { 328 ++card->irq.svc_count; 329 ++work_done; 330 } 331 spin_unlock_bh(&card->spin); 332 /* resume tx queue's */ 333 offset = card->tx.last_bus; 334 for (j = 0; j < ARRAY_SIZE(card->net); ++j) { 335 if (card->tx.pending >= TXMAX) 336 break; 337 netdev = card->net[(j + offset + 1) % card->pdat->nbus]; 338 if (!netdev) 339 continue; 340 priv = netdev_priv(netdev); 341 if (!canif_is_active(netdev)) 342 /* it makes no sense to wake dead buses */ 343 continue; 344 if (priv->tx.pending >= TX_ECHO_SKB_MAX) 345 continue; 346 ++work_done; 347 netif_wake_queue(netdev); 348 } 349 return work_done ? IRQ_HANDLED : IRQ_NONE; 350 } 351 352 /* 353 * interrupt routines: 354 * schedule the 'real interrupt handler' 355 */ 356 static irqreturn_t softing_irq_v2(int irq, void *dev_id) 357 { 358 struct softing *card = (struct softing *)dev_id; 359 uint8_t ir; 360 361 ir = ioread8(&card->dpram[DPRAM_V2_IRQ_TOHOST]); 362 iowrite8(0, &card->dpram[DPRAM_V2_IRQ_TOHOST]); 363 return (1 == ir) ? IRQ_WAKE_THREAD : IRQ_NONE; 364 } 365 366 static irqreturn_t softing_irq_v1(int irq, void *dev_id) 367 { 368 struct softing *card = (struct softing *)dev_id; 369 uint8_t ir; 370 371 ir = ioread8(&card->dpram[DPRAM_IRQ_TOHOST]); 372 iowrite8(0, &card->dpram[DPRAM_IRQ_TOHOST]); 373 return ir ? IRQ_WAKE_THREAD : IRQ_NONE; 374 } 375 376 /* 377 * netdev/candev interoperability 378 */ 379 static int softing_netdev_open(struct net_device *ndev) 380 { 381 int ret; 382 383 /* check or determine and set bittime */ 384 ret = open_candev(ndev); 385 if (ret) 386 return ret; 387 388 ret = softing_startstop(ndev, 1); 389 if (ret < 0) 390 close_candev(ndev); 391 392 return ret; 393 } 394 395 static int softing_netdev_stop(struct net_device *ndev) 396 { 397 int ret; 398 399 netif_stop_queue(ndev); 400 401 /* softing cycle does close_candev() */ 402 ret = softing_startstop(ndev, 0); 403 return ret; 404 } 405 406 static int softing_candev_set_mode(struct net_device *ndev, enum can_mode mode) 407 { 408 int ret; 409 410 switch (mode) { 411 case CAN_MODE_START: 412 /* softing_startstop does close_candev() */ 413 ret = softing_startstop(ndev, 1); 414 return ret; 415 case CAN_MODE_STOP: 416 case CAN_MODE_SLEEP: 417 return -EOPNOTSUPP; 418 } 419 return 0; 420 } 421 422 /* 423 * Softing device management helpers 424 */ 425 int softing_enable_irq(struct softing *card, int enable) 426 { 427 int ret; 428 429 if (!card->irq.nr) { 430 return 0; 431 } else if (card->irq.requested && !enable) { 432 free_irq(card->irq.nr, card); 433 card->irq.requested = 0; 434 } else if (!card->irq.requested && enable) { 435 ret = request_threaded_irq(card->irq.nr, 436 (card->pdat->generation >= 2) ? 437 softing_irq_v2 : softing_irq_v1, 438 softing_irq_thread, IRQF_SHARED, 439 dev_name(&card->pdev->dev), card); 440 if (ret) { 441 dev_alert(&card->pdev->dev, 442 "request_threaded_irq(%u) failed\n", 443 card->irq.nr); 444 return ret; 445 } 446 card->irq.requested = 1; 447 } 448 return 0; 449 } 450 451 static void softing_card_shutdown(struct softing *card) 452 { 453 int fw_up = 0; 454 455 if (mutex_lock_interruptible(&card->fw.lock)) { 456 /* return -ERESTARTSYS */; 457 } 458 fw_up = card->fw.up; 459 card->fw.up = 0; 460 461 if (card->irq.requested && card->irq.nr) { 462 free_irq(card->irq.nr, card); 463 card->irq.requested = 0; 464 } 465 if (fw_up) { 466 if (card->pdat->enable_irq) 467 card->pdat->enable_irq(card->pdev, 0); 468 softing_set_reset_dpram(card); 469 if (card->pdat->reset) 470 card->pdat->reset(card->pdev, 1); 471 } 472 mutex_unlock(&card->fw.lock); 473 } 474 475 static int softing_card_boot(struct softing *card) 476 { 477 int ret, j; 478 static const uint8_t stream[] = { 479 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, }; 480 unsigned char back[sizeof(stream)]; 481 482 if (mutex_lock_interruptible(&card->fw.lock)) 483 return -ERESTARTSYS; 484 if (card->fw.up) { 485 mutex_unlock(&card->fw.lock); 486 return 0; 487 } 488 /* reset board */ 489 if (card->pdat->enable_irq) 490 card->pdat->enable_irq(card->pdev, 1); 491 /* boot card */ 492 softing_set_reset_dpram(card); 493 if (card->pdat->reset) 494 card->pdat->reset(card->pdev, 1); 495 for (j = 0; (j + sizeof(stream)) < card->dpram_size; 496 j += sizeof(stream)) { 497 498 memcpy_toio(&card->dpram[j], stream, sizeof(stream)); 499 /* flush IO cache */ 500 mb(); 501 memcpy_fromio(back, &card->dpram[j], sizeof(stream)); 502 503 if (!memcmp(back, stream, sizeof(stream))) 504 continue; 505 /* memory is not equal */ 506 dev_alert(&card->pdev->dev, "dpram failed at 0x%04x\n", j); 507 ret = -EIO; 508 goto failed; 509 } 510 wmb(); 511 /* load boot firmware */ 512 ret = softing_load_fw(card->pdat->boot.fw, card, card->dpram, 513 card->dpram_size, 514 card->pdat->boot.offs - card->pdat->boot.addr); 515 if (ret < 0) 516 goto failed; 517 /* load loader firmware */ 518 ret = softing_load_fw(card->pdat->load.fw, card, card->dpram, 519 card->dpram_size, 520 card->pdat->load.offs - card->pdat->load.addr); 521 if (ret < 0) 522 goto failed; 523 524 if (card->pdat->reset) 525 card->pdat->reset(card->pdev, 0); 526 softing_clr_reset_dpram(card); 527 ret = softing_bootloader_command(card, 0, "card boot"); 528 if (ret < 0) 529 goto failed; 530 ret = softing_load_app_fw(card->pdat->app.fw, card); 531 if (ret < 0) 532 goto failed; 533 534 ret = softing_chip_poweron(card); 535 if (ret < 0) 536 goto failed; 537 538 card->fw.up = 1; 539 mutex_unlock(&card->fw.lock); 540 return 0; 541 failed: 542 card->fw.up = 0; 543 if (card->pdat->enable_irq) 544 card->pdat->enable_irq(card->pdev, 0); 545 softing_set_reset_dpram(card); 546 if (card->pdat->reset) 547 card->pdat->reset(card->pdev, 1); 548 mutex_unlock(&card->fw.lock); 549 return ret; 550 } 551 552 /* 553 * netdev sysfs 554 */ 555 static ssize_t show_chip(struct device *dev, struct device_attribute *attr, 556 char *buf) 557 { 558 struct net_device *ndev = to_net_dev(dev); 559 struct softing_priv *priv = netdev2softing(ndev); 560 561 return sprintf(buf, "%i\n", priv->chip); 562 } 563 564 static ssize_t show_output(struct device *dev, struct device_attribute *attr, 565 char *buf) 566 { 567 struct net_device *ndev = to_net_dev(dev); 568 struct softing_priv *priv = netdev2softing(ndev); 569 570 return sprintf(buf, "0x%02x\n", priv->output); 571 } 572 573 static ssize_t store_output(struct device *dev, struct device_attribute *attr, 574 const char *buf, size_t count) 575 { 576 struct net_device *ndev = to_net_dev(dev); 577 struct softing_priv *priv = netdev2softing(ndev); 578 struct softing *card = priv->card; 579 unsigned long val; 580 int ret; 581 582 ret = kstrtoul(buf, 0, &val); 583 if (ret < 0) 584 return ret; 585 val &= 0xFF; 586 587 ret = mutex_lock_interruptible(&card->fw.lock); 588 if (ret) 589 return -ERESTARTSYS; 590 if (netif_running(ndev)) { 591 mutex_unlock(&card->fw.lock); 592 return -EBUSY; 593 } 594 priv->output = val; 595 mutex_unlock(&card->fw.lock); 596 return count; 597 } 598 599 static const DEVICE_ATTR(chip, 0444, show_chip, NULL); 600 static const DEVICE_ATTR(output, 0644, show_output, store_output); 601 602 static const struct attribute *const netdev_sysfs_attrs[] = { 603 &dev_attr_chip.attr, 604 &dev_attr_output.attr, 605 NULL, 606 }; 607 static const struct attribute_group netdev_sysfs_group = { 608 .name = NULL, 609 .attrs = (struct attribute **)netdev_sysfs_attrs, 610 }; 611 612 static const struct net_device_ops softing_netdev_ops = { 613 .ndo_open = softing_netdev_open, 614 .ndo_stop = softing_netdev_stop, 615 .ndo_start_xmit = softing_netdev_start_xmit, 616 .ndo_change_mtu = can_change_mtu, 617 }; 618 619 static const struct can_bittiming_const softing_btr_const = { 620 .name = "softing", 621 .tseg1_min = 1, 622 .tseg1_max = 16, 623 .tseg2_min = 1, 624 .tseg2_max = 8, 625 .sjw_max = 4, /* overruled */ 626 .brp_min = 1, 627 .brp_max = 32, /* overruled */ 628 .brp_inc = 1, 629 }; 630 631 632 static struct net_device *softing_netdev_create(struct softing *card, 633 uint16_t chip_id) 634 { 635 struct net_device *netdev; 636 struct softing_priv *priv; 637 638 netdev = alloc_candev(sizeof(*priv), TX_ECHO_SKB_MAX); 639 if (!netdev) { 640 dev_alert(&card->pdev->dev, "alloc_candev failed\n"); 641 return NULL; 642 } 643 priv = netdev_priv(netdev); 644 priv->netdev = netdev; 645 priv->card = card; 646 memcpy(&priv->btr_const, &softing_btr_const, sizeof(priv->btr_const)); 647 priv->btr_const.brp_max = card->pdat->max_brp; 648 priv->btr_const.sjw_max = card->pdat->max_sjw; 649 priv->can.bittiming_const = &priv->btr_const; 650 priv->can.clock.freq = 8000000; 651 priv->chip = chip_id; 652 priv->output = softing_default_output(netdev); 653 SET_NETDEV_DEV(netdev, &card->pdev->dev); 654 655 netdev->flags |= IFF_ECHO; 656 netdev->netdev_ops = &softing_netdev_ops; 657 priv->can.do_set_mode = softing_candev_set_mode; 658 priv->can.ctrlmode_supported = CAN_CTRLMODE_3_SAMPLES; 659 660 return netdev; 661 } 662 663 static int softing_netdev_register(struct net_device *netdev) 664 { 665 int ret; 666 667 ret = register_candev(netdev); 668 if (ret) { 669 dev_alert(&netdev->dev, "register failed\n"); 670 return ret; 671 } 672 if (sysfs_create_group(&netdev->dev.kobj, &netdev_sysfs_group) < 0) 673 netdev_alert(netdev, "sysfs group failed\n"); 674 675 return 0; 676 } 677 678 static void softing_netdev_cleanup(struct net_device *netdev) 679 { 680 sysfs_remove_group(&netdev->dev.kobj, &netdev_sysfs_group); 681 unregister_candev(netdev); 682 free_candev(netdev); 683 } 684 685 /* 686 * sysfs for Platform device 687 */ 688 #define DEV_ATTR_RO(name, member) \ 689 static ssize_t show_##name(struct device *dev, \ 690 struct device_attribute *attr, char *buf) \ 691 { \ 692 struct softing *card = dev_get_drvdata(dev); \ 693 return sprintf(buf, "%u\n", card->member); \ 694 } \ 695 static DEVICE_ATTR(name, 0444, show_##name, NULL) 696 697 #define DEV_ATTR_RO_STR(name, member) \ 698 static ssize_t show_##name(struct device *dev, \ 699 struct device_attribute *attr, char *buf) \ 700 { \ 701 struct softing *card = dev_get_drvdata(dev); \ 702 return sprintf(buf, "%s\n", card->member); \ 703 } \ 704 static DEVICE_ATTR(name, 0444, show_##name, NULL) 705 706 DEV_ATTR_RO(serial, id.serial); 707 DEV_ATTR_RO_STR(firmware, pdat->app.fw); 708 DEV_ATTR_RO(firmware_version, id.fw_version); 709 DEV_ATTR_RO_STR(hardware, pdat->name); 710 DEV_ATTR_RO(hardware_version, id.hw_version); 711 DEV_ATTR_RO(license, id.license); 712 713 static struct attribute *softing_pdev_attrs[] = { 714 &dev_attr_serial.attr, 715 &dev_attr_firmware.attr, 716 &dev_attr_firmware_version.attr, 717 &dev_attr_hardware.attr, 718 &dev_attr_hardware_version.attr, 719 &dev_attr_license.attr, 720 NULL, 721 }; 722 723 static const struct attribute_group softing_pdev_group = { 724 .name = NULL, 725 .attrs = softing_pdev_attrs, 726 }; 727 728 /* 729 * platform driver 730 */ 731 static int softing_pdev_remove(struct platform_device *pdev) 732 { 733 struct softing *card = platform_get_drvdata(pdev); 734 int j; 735 736 /* first, disable card*/ 737 softing_card_shutdown(card); 738 739 for (j = 0; j < ARRAY_SIZE(card->net); ++j) { 740 if (!card->net[j]) 741 continue; 742 softing_netdev_cleanup(card->net[j]); 743 card->net[j] = NULL; 744 } 745 sysfs_remove_group(&pdev->dev.kobj, &softing_pdev_group); 746 747 iounmap(card->dpram); 748 kfree(card); 749 return 0; 750 } 751 752 static int softing_pdev_probe(struct platform_device *pdev) 753 { 754 const struct softing_platform_data *pdat = dev_get_platdata(&pdev->dev); 755 struct softing *card; 756 struct net_device *netdev; 757 struct softing_priv *priv; 758 struct resource *pres; 759 int ret; 760 int j; 761 762 if (!pdat) { 763 dev_warn(&pdev->dev, "no platform data\n"); 764 return -EINVAL; 765 } 766 if (pdat->nbus > ARRAY_SIZE(card->net)) { 767 dev_warn(&pdev->dev, "%u nets??\n", pdat->nbus); 768 return -EINVAL; 769 } 770 771 card = kzalloc(sizeof(*card), GFP_KERNEL); 772 if (!card) 773 return -ENOMEM; 774 card->pdat = pdat; 775 card->pdev = pdev; 776 platform_set_drvdata(pdev, card); 777 mutex_init(&card->fw.lock); 778 spin_lock_init(&card->spin); 779 780 ret = -EINVAL; 781 pres = platform_get_resource(pdev, IORESOURCE_MEM, 0); 782 if (!pres) 783 goto platform_resource_failed; 784 card->dpram_phys = pres->start; 785 card->dpram_size = resource_size(pres); 786 card->dpram = ioremap(card->dpram_phys, card->dpram_size); 787 if (!card->dpram) { 788 dev_alert(&card->pdev->dev, "dpram ioremap failed\n"); 789 goto ioremap_failed; 790 } 791 792 pres = platform_get_resource(pdev, IORESOURCE_IRQ, 0); 793 if (pres) 794 card->irq.nr = pres->start; 795 796 /* reset card */ 797 ret = softing_card_boot(card); 798 if (ret < 0) { 799 dev_alert(&pdev->dev, "failed to boot\n"); 800 goto boot_failed; 801 } 802 803 /* only now, the chip's are known */ 804 card->id.freq = card->pdat->freq; 805 806 ret = sysfs_create_group(&pdev->dev.kobj, &softing_pdev_group); 807 if (ret < 0) { 808 dev_alert(&card->pdev->dev, "sysfs failed\n"); 809 goto sysfs_failed; 810 } 811 812 for (j = 0; j < ARRAY_SIZE(card->net); ++j) { 813 card->net[j] = netdev = 814 softing_netdev_create(card, card->id.chip[j]); 815 if (!netdev) { 816 dev_alert(&pdev->dev, "failed to make can[%i]", j); 817 ret = -ENOMEM; 818 goto netdev_failed; 819 } 820 netdev->dev_id = j; 821 priv = netdev_priv(card->net[j]); 822 priv->index = j; 823 ret = softing_netdev_register(netdev); 824 if (ret) { 825 free_candev(netdev); 826 card->net[j] = NULL; 827 dev_alert(&card->pdev->dev, 828 "failed to register can[%i]\n", j); 829 goto netdev_failed; 830 } 831 } 832 dev_info(&card->pdev->dev, "%s ready.\n", card->pdat->name); 833 return 0; 834 835 netdev_failed: 836 for (j = 0; j < ARRAY_SIZE(card->net); ++j) { 837 if (!card->net[j]) 838 continue; 839 softing_netdev_cleanup(card->net[j]); 840 } 841 sysfs_remove_group(&pdev->dev.kobj, &softing_pdev_group); 842 sysfs_failed: 843 softing_card_shutdown(card); 844 boot_failed: 845 iounmap(card->dpram); 846 ioremap_failed: 847 platform_resource_failed: 848 kfree(card); 849 return ret; 850 } 851 852 static struct platform_driver softing_driver = { 853 .driver = { 854 .name = "softing", 855 }, 856 .probe = softing_pdev_probe, 857 .remove = softing_pdev_remove, 858 }; 859 860 module_platform_driver(softing_driver); 861 862 MODULE_ALIAS("platform:softing"); 863 MODULE_DESCRIPTION("Softing DPRAM CAN driver"); 864 MODULE_AUTHOR("Kurt Van Dijck <kurt.van.dijck@eia.be>"); 865 MODULE_LICENSE("GPL v2"); 866