root/drivers/net/can/softing/softing_main.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. canif_is_active
  2. softing_set_reset_dpram
  3. softing_clr_reset_dpram
  4. softing_netdev_start_xmit
  5. softing_netdev_rx
  6. softing_handle_1
  7. softing_irq_thread
  8. softing_irq_v2
  9. softing_irq_v1
  10. softing_netdev_open
  11. softing_netdev_stop
  12. softing_candev_set_mode
  13. softing_enable_irq
  14. softing_card_shutdown
  15. softing_card_boot
  16. show_chip
  17. show_output
  18. store_output
  19. softing_netdev_create
  20. softing_netdev_register
  21. softing_netdev_cleanup
  22. softing_pdev_remove
  23. softing_pdev_probe

   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->can_dlc;
  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->can_dlc);
  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);
 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.can_dlc = CAN_ERR_DLC;
 171                 msg.data[1] = CAN_ERR_CRTL_RX_OVERFLOW;
 172                 /*
 173                  * service to all busses, we don't know which it was applicable
 174                  * but only service busses 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.can_dlc = 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.can_dlc = get_can_dlc(*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);
 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.can_dlc;
 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.can_dlc;
 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 busses */
 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 inter-operability
 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                 ret = softing_startstop(ndev, 1);
 387         return ret;
 388 }
 389 
 390 static int softing_netdev_stop(struct net_device *ndev)
 391 {
 392         int ret;
 393 
 394         netif_stop_queue(ndev);
 395 
 396         /* softing cycle does close_candev() */
 397         ret = softing_startstop(ndev, 0);
 398         return ret;
 399 }
 400 
 401 static int softing_candev_set_mode(struct net_device *ndev, enum can_mode mode)
 402 {
 403         int ret;
 404 
 405         switch (mode) {
 406         case CAN_MODE_START:
 407                 /* softing_startstop does close_candev() */
 408                 ret = softing_startstop(ndev, 1);
 409                 return ret;
 410         case CAN_MODE_STOP:
 411         case CAN_MODE_SLEEP:
 412                 return -EOPNOTSUPP;
 413         }
 414         return 0;
 415 }
 416 
 417 /*
 418  * Softing device management helpers
 419  */
 420 int softing_enable_irq(struct softing *card, int enable)
 421 {
 422         int ret;
 423 
 424         if (!card->irq.nr) {
 425                 return 0;
 426         } else if (card->irq.requested && !enable) {
 427                 free_irq(card->irq.nr, card);
 428                 card->irq.requested = 0;
 429         } else if (!card->irq.requested && enable) {
 430                 ret = request_threaded_irq(card->irq.nr,
 431                                 (card->pdat->generation >= 2) ?
 432                                         softing_irq_v2 : softing_irq_v1,
 433                                 softing_irq_thread, IRQF_SHARED,
 434                                 dev_name(&card->pdev->dev), card);
 435                 if (ret) {
 436                         dev_alert(&card->pdev->dev,
 437                                         "request_threaded_irq(%u) failed\n",
 438                                         card->irq.nr);
 439                         return ret;
 440                 }
 441                 card->irq.requested = 1;
 442         }
 443         return 0;
 444 }
 445 
 446 static void softing_card_shutdown(struct softing *card)
 447 {
 448         int fw_up = 0;
 449 
 450         if (mutex_lock_interruptible(&card->fw.lock))
 451                 /* return -ERESTARTSYS */;
 452         fw_up = card->fw.up;
 453         card->fw.up = 0;
 454 
 455         if (card->irq.requested && card->irq.nr) {
 456                 free_irq(card->irq.nr, card);
 457                 card->irq.requested = 0;
 458         }
 459         if (fw_up) {
 460                 if (card->pdat->enable_irq)
 461                         card->pdat->enable_irq(card->pdev, 0);
 462                 softing_set_reset_dpram(card);
 463                 if (card->pdat->reset)
 464                         card->pdat->reset(card->pdev, 1);
 465         }
 466         mutex_unlock(&card->fw.lock);
 467 }
 468 
 469 static int softing_card_boot(struct softing *card)
 470 {
 471         int ret, j;
 472         static const uint8_t stream[] = {
 473                 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, };
 474         unsigned char back[sizeof(stream)];
 475 
 476         if (mutex_lock_interruptible(&card->fw.lock))
 477                 return -ERESTARTSYS;
 478         if (card->fw.up) {
 479                 mutex_unlock(&card->fw.lock);
 480                 return 0;
 481         }
 482         /* reset board */
 483         if (card->pdat->enable_irq)
 484                 card->pdat->enable_irq(card->pdev, 1);
 485         /* boot card */
 486         softing_set_reset_dpram(card);
 487         if (card->pdat->reset)
 488                 card->pdat->reset(card->pdev, 1);
 489         for (j = 0; (j + sizeof(stream)) < card->dpram_size;
 490                         j += sizeof(stream)) {
 491 
 492                 memcpy_toio(&card->dpram[j], stream, sizeof(stream));
 493                 /* flush IO cache */
 494                 mb();
 495                 memcpy_fromio(back, &card->dpram[j], sizeof(stream));
 496 
 497                 if (!memcmp(back, stream, sizeof(stream)))
 498                         continue;
 499                 /* memory is not equal */
 500                 dev_alert(&card->pdev->dev, "dpram failed at 0x%04x\n", j);
 501                 ret = -EIO;
 502                 goto failed;
 503         }
 504         wmb();
 505         /* load boot firmware */
 506         ret = softing_load_fw(card->pdat->boot.fw, card, card->dpram,
 507                                 card->dpram_size,
 508                                 card->pdat->boot.offs - card->pdat->boot.addr);
 509         if (ret < 0)
 510                 goto failed;
 511         /* load loader firmware */
 512         ret = softing_load_fw(card->pdat->load.fw, card, card->dpram,
 513                                 card->dpram_size,
 514                                 card->pdat->load.offs - card->pdat->load.addr);
 515         if (ret < 0)
 516                 goto failed;
 517 
 518         if (card->pdat->reset)
 519                 card->pdat->reset(card->pdev, 0);
 520         softing_clr_reset_dpram(card);
 521         ret = softing_bootloader_command(card, 0, "card boot");
 522         if (ret < 0)
 523                 goto failed;
 524         ret = softing_load_app_fw(card->pdat->app.fw, card);
 525         if (ret < 0)
 526                 goto failed;
 527 
 528         ret = softing_chip_poweron(card);
 529         if (ret < 0)
 530                 goto failed;
 531 
 532         card->fw.up = 1;
 533         mutex_unlock(&card->fw.lock);
 534         return 0;
 535 failed:
 536         card->fw.up = 0;
 537         if (card->pdat->enable_irq)
 538                 card->pdat->enable_irq(card->pdev, 0);
 539         softing_set_reset_dpram(card);
 540         if (card->pdat->reset)
 541                 card->pdat->reset(card->pdev, 1);
 542         mutex_unlock(&card->fw.lock);
 543         return ret;
 544 }
 545 
 546 /*
 547  * netdev sysfs
 548  */
 549 static ssize_t show_chip(struct device *dev, struct device_attribute *attr,
 550                 char *buf)
 551 {
 552         struct net_device *ndev = to_net_dev(dev);
 553         struct softing_priv *priv = netdev2softing(ndev);
 554 
 555         return sprintf(buf, "%i\n", priv->chip);
 556 }
 557 
 558 static ssize_t show_output(struct device *dev, struct device_attribute *attr,
 559                 char *buf)
 560 {
 561         struct net_device *ndev = to_net_dev(dev);
 562         struct softing_priv *priv = netdev2softing(ndev);
 563 
 564         return sprintf(buf, "0x%02x\n", priv->output);
 565 }
 566 
 567 static ssize_t store_output(struct device *dev, struct device_attribute *attr,
 568                 const char *buf, size_t count)
 569 {
 570         struct net_device *ndev = to_net_dev(dev);
 571         struct softing_priv *priv = netdev2softing(ndev);
 572         struct softing *card = priv->card;
 573         unsigned long val;
 574         int ret;
 575 
 576         ret = kstrtoul(buf, 0, &val);
 577         if (ret < 0)
 578                 return ret;
 579         val &= 0xFF;
 580 
 581         ret = mutex_lock_interruptible(&card->fw.lock);
 582         if (ret)
 583                 return -ERESTARTSYS;
 584         if (netif_running(ndev)) {
 585                 mutex_unlock(&card->fw.lock);
 586                 return -EBUSY;
 587         }
 588         priv->output = val;
 589         mutex_unlock(&card->fw.lock);
 590         return count;
 591 }
 592 
 593 static const DEVICE_ATTR(chip, 0444, show_chip, NULL);
 594 static const DEVICE_ATTR(output, 0644, show_output, store_output);
 595 
 596 static const struct attribute *const netdev_sysfs_attrs[] = {
 597         &dev_attr_chip.attr,
 598         &dev_attr_output.attr,
 599         NULL,
 600 };
 601 static const struct attribute_group netdev_sysfs_group = {
 602         .name = NULL,
 603         .attrs = (struct attribute **)netdev_sysfs_attrs,
 604 };
 605 
 606 static const struct net_device_ops softing_netdev_ops = {
 607         .ndo_open = softing_netdev_open,
 608         .ndo_stop = softing_netdev_stop,
 609         .ndo_start_xmit = softing_netdev_start_xmit,
 610         .ndo_change_mtu = can_change_mtu,
 611 };
 612 
 613 static const struct can_bittiming_const softing_btr_const = {
 614         .name = "softing",
 615         .tseg1_min = 1,
 616         .tseg1_max = 16,
 617         .tseg2_min = 1,
 618         .tseg2_max = 8,
 619         .sjw_max = 4, /* overruled */
 620         .brp_min = 1,
 621         .brp_max = 32, /* overruled */
 622         .brp_inc = 1,
 623 };
 624 
 625 
 626 static struct net_device *softing_netdev_create(struct softing *card,
 627                                                 uint16_t chip_id)
 628 {
 629         struct net_device *netdev;
 630         struct softing_priv *priv;
 631 
 632         netdev = alloc_candev(sizeof(*priv), TX_ECHO_SKB_MAX);
 633         if (!netdev) {
 634                 dev_alert(&card->pdev->dev, "alloc_candev failed\n");
 635                 return NULL;
 636         }
 637         priv = netdev_priv(netdev);
 638         priv->netdev = netdev;
 639         priv->card = card;
 640         memcpy(&priv->btr_const, &softing_btr_const, sizeof(priv->btr_const));
 641         priv->btr_const.brp_max = card->pdat->max_brp;
 642         priv->btr_const.sjw_max = card->pdat->max_sjw;
 643         priv->can.bittiming_const = &priv->btr_const;
 644         priv->can.clock.freq = 8000000;
 645         priv->chip = chip_id;
 646         priv->output = softing_default_output(netdev);
 647         SET_NETDEV_DEV(netdev, &card->pdev->dev);
 648 
 649         netdev->flags |= IFF_ECHO;
 650         netdev->netdev_ops = &softing_netdev_ops;
 651         priv->can.do_set_mode = softing_candev_set_mode;
 652         priv->can.ctrlmode_supported = CAN_CTRLMODE_3_SAMPLES;
 653 
 654         return netdev;
 655 }
 656 
 657 static int softing_netdev_register(struct net_device *netdev)
 658 {
 659         int ret;
 660 
 661         ret = register_candev(netdev);
 662         if (ret) {
 663                 dev_alert(&netdev->dev, "register failed\n");
 664                 return ret;
 665         }
 666         if (sysfs_create_group(&netdev->dev.kobj, &netdev_sysfs_group) < 0)
 667                 netdev_alert(netdev, "sysfs group failed\n");
 668 
 669         return 0;
 670 }
 671 
 672 static void softing_netdev_cleanup(struct net_device *netdev)
 673 {
 674         sysfs_remove_group(&netdev->dev.kobj, &netdev_sysfs_group);
 675         unregister_candev(netdev);
 676         free_candev(netdev);
 677 }
 678 
 679 /*
 680  * sysfs for Platform device
 681  */
 682 #define DEV_ATTR_RO(name, member) \
 683 static ssize_t show_##name(struct device *dev, \
 684                 struct device_attribute *attr, char *buf) \
 685 { \
 686         struct softing *card = dev_get_drvdata(dev); \
 687         return sprintf(buf, "%u\n", card->member); \
 688 } \
 689 static DEVICE_ATTR(name, 0444, show_##name, NULL)
 690 
 691 #define DEV_ATTR_RO_STR(name, member) \
 692 static ssize_t show_##name(struct device *dev, \
 693                 struct device_attribute *attr, char *buf) \
 694 { \
 695         struct softing *card = dev_get_drvdata(dev); \
 696         return sprintf(buf, "%s\n", card->member); \
 697 } \
 698 static DEVICE_ATTR(name, 0444, show_##name, NULL)
 699 
 700 DEV_ATTR_RO(serial, id.serial);
 701 DEV_ATTR_RO_STR(firmware, pdat->app.fw);
 702 DEV_ATTR_RO(firmware_version, id.fw_version);
 703 DEV_ATTR_RO_STR(hardware, pdat->name);
 704 DEV_ATTR_RO(hardware_version, id.hw_version);
 705 DEV_ATTR_RO(license, id.license);
 706 
 707 static struct attribute *softing_pdev_attrs[] = {
 708         &dev_attr_serial.attr,
 709         &dev_attr_firmware.attr,
 710         &dev_attr_firmware_version.attr,
 711         &dev_attr_hardware.attr,
 712         &dev_attr_hardware_version.attr,
 713         &dev_attr_license.attr,
 714         NULL,
 715 };
 716 
 717 static const struct attribute_group softing_pdev_group = {
 718         .name = NULL,
 719         .attrs = softing_pdev_attrs,
 720 };
 721 
 722 /*
 723  * platform driver
 724  */
 725 static int softing_pdev_remove(struct platform_device *pdev)
 726 {
 727         struct softing *card = platform_get_drvdata(pdev);
 728         int j;
 729 
 730         /* first, disable card*/
 731         softing_card_shutdown(card);
 732 
 733         for (j = 0; j < ARRAY_SIZE(card->net); ++j) {
 734                 if (!card->net[j])
 735                         continue;
 736                 softing_netdev_cleanup(card->net[j]);
 737                 card->net[j] = NULL;
 738         }
 739         sysfs_remove_group(&pdev->dev.kobj, &softing_pdev_group);
 740 
 741         iounmap(card->dpram);
 742         kfree(card);
 743         return 0;
 744 }
 745 
 746 static int softing_pdev_probe(struct platform_device *pdev)
 747 {
 748         const struct softing_platform_data *pdat = dev_get_platdata(&pdev->dev);
 749         struct softing *card;
 750         struct net_device *netdev;
 751         struct softing_priv *priv;
 752         struct resource *pres;
 753         int ret;
 754         int j;
 755 
 756         if (!pdat) {
 757                 dev_warn(&pdev->dev, "no platform data\n");
 758                 return -EINVAL;
 759         }
 760         if (pdat->nbus > ARRAY_SIZE(card->net)) {
 761                 dev_warn(&pdev->dev, "%u nets??\n", pdat->nbus);
 762                 return -EINVAL;
 763         }
 764 
 765         card = kzalloc(sizeof(*card), GFP_KERNEL);
 766         if (!card)
 767                 return -ENOMEM;
 768         card->pdat = pdat;
 769         card->pdev = pdev;
 770         platform_set_drvdata(pdev, card);
 771         mutex_init(&card->fw.lock);
 772         spin_lock_init(&card->spin);
 773 
 774         ret = -EINVAL;
 775         pres = platform_get_resource(pdev, IORESOURCE_MEM, 0);
 776         if (!pres)
 777                 goto platform_resource_failed;
 778         card->dpram_phys = pres->start;
 779         card->dpram_size = resource_size(pres);
 780         card->dpram = ioremap_nocache(card->dpram_phys, card->dpram_size);
 781         if (!card->dpram) {
 782                 dev_alert(&card->pdev->dev, "dpram ioremap failed\n");
 783                 goto ioremap_failed;
 784         }
 785 
 786         pres = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
 787         if (pres)
 788                 card->irq.nr = pres->start;
 789 
 790         /* reset card */
 791         ret = softing_card_boot(card);
 792         if (ret < 0) {
 793                 dev_alert(&pdev->dev, "failed to boot\n");
 794                 goto boot_failed;
 795         }
 796 
 797         /* only now, the chip's are known */
 798         card->id.freq = card->pdat->freq;
 799 
 800         ret = sysfs_create_group(&pdev->dev.kobj, &softing_pdev_group);
 801         if (ret < 0) {
 802                 dev_alert(&card->pdev->dev, "sysfs failed\n");
 803                 goto sysfs_failed;
 804         }
 805 
 806         for (j = 0; j < ARRAY_SIZE(card->net); ++j) {
 807                 card->net[j] = netdev =
 808                         softing_netdev_create(card, card->id.chip[j]);
 809                 if (!netdev) {
 810                         dev_alert(&pdev->dev, "failed to make can[%i]", j);
 811                         ret = -ENOMEM;
 812                         goto netdev_failed;
 813                 }
 814                 netdev->dev_id = j;
 815                 priv = netdev_priv(card->net[j]);
 816                 priv->index = j;
 817                 ret = softing_netdev_register(netdev);
 818                 if (ret) {
 819                         free_candev(netdev);
 820                         card->net[j] = NULL;
 821                         dev_alert(&card->pdev->dev,
 822                                         "failed to register can[%i]\n", j);
 823                         goto netdev_failed;
 824                 }
 825         }
 826         dev_info(&card->pdev->dev, "%s ready.\n", card->pdat->name);
 827         return 0;
 828 
 829 netdev_failed:
 830         for (j = 0; j < ARRAY_SIZE(card->net); ++j) {
 831                 if (!card->net[j])
 832                         continue;
 833                 softing_netdev_cleanup(card->net[j]);
 834         }
 835         sysfs_remove_group(&pdev->dev.kobj, &softing_pdev_group);
 836 sysfs_failed:
 837         softing_card_shutdown(card);
 838 boot_failed:
 839         iounmap(card->dpram);
 840 ioremap_failed:
 841 platform_resource_failed:
 842         kfree(card);
 843         return ret;
 844 }
 845 
 846 static struct platform_driver softing_driver = {
 847         .driver = {
 848                 .name = "softing",
 849         },
 850         .probe = softing_pdev_probe,
 851         .remove = softing_pdev_remove,
 852 };
 853 
 854 module_platform_driver(softing_driver);
 855 
 856 MODULE_ALIAS("platform:softing");
 857 MODULE_DESCRIPTION("Softing DPRAM CAN driver");
 858 MODULE_AUTHOR("Kurt Van Dijck <kurt.van.dijck@eia.be>");
 859 MODULE_LICENSE("GPL v2");

/* [<][>][^][v][top][bottom][index][help] */