linux/drivers/net/can/sja1000/peak_pcmcia.c
<<
>>
Prefs
   1/*
   2 * Copyright (C) 2010-2012 Stephane Grosjean <s.grosjean@peak-system.com>
   3 *
   4 * CAN driver for PEAK-System PCAN-PC Card
   5 * Derived from the PCAN project file driver/src/pcan_pccard.c
   6 * Copyright (C) 2006-2010 PEAK System-Technik GmbH
   7 *
   8 * This program is free software; you can redistribute it and/or modify
   9 * it under the terms of the version 2 of the GNU General Public License
  10 * as published by the Free Software Foundation
  11 *
  12 * This program is distributed in the hope that it will be useful,
  13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15 * GNU General Public License for more details.
  16 */
  17#include <linux/kernel.h>
  18#include <linux/module.h>
  19#include <linux/interrupt.h>
  20#include <linux/netdevice.h>
  21#include <linux/delay.h>
  22#include <linux/timer.h>
  23#include <linux/io.h>
  24#include <pcmcia/cistpl.h>
  25#include <pcmcia/ds.h>
  26#include <linux/can.h>
  27#include <linux/can/dev.h>
  28#include "sja1000.h"
  29
  30MODULE_AUTHOR("Stephane Grosjean <s.grosjean@peak-system.com>");
  31MODULE_DESCRIPTION("CAN driver for PEAK-System PCAN-PC Cards");
  32MODULE_LICENSE("GPL v2");
  33MODULE_SUPPORTED_DEVICE("PEAK PCAN-PC Card");
  34
  35/* PEAK-System PCMCIA driver name */
  36#define PCC_NAME                "peak_pcmcia"
  37
  38#define PCC_CHAN_MAX            2
  39
  40#define PCC_CAN_CLOCK           (16000000 / 2)
  41
  42#define PCC_MANF_ID             0x0377
  43#define PCC_CARD_ID             0x0001
  44
  45#define PCC_CHAN_SIZE           0x20
  46#define PCC_CHAN_OFF(c)         ((c) * PCC_CHAN_SIZE)
  47#define PCC_COMN_OFF            (PCC_CHAN_OFF(PCC_CHAN_MAX))
  48#define PCC_COMN_SIZE           0x40
  49
  50/* common area registers */
  51#define PCC_CCR                 0x00
  52#define PCC_CSR                 0x02
  53#define PCC_CPR                 0x04
  54#define PCC_SPI_DIR             0x06
  55#define PCC_SPI_DOR             0x08
  56#define PCC_SPI_ADR             0x0a
  57#define PCC_SPI_IR              0x0c
  58#define PCC_FW_MAJOR            0x10
  59#define PCC_FW_MINOR            0x12
  60
  61/* CCR bits */
  62#define PCC_CCR_CLK_16          0x00
  63#define PCC_CCR_CLK_10          0x01
  64#define PCC_CCR_CLK_21          0x02
  65#define PCC_CCR_CLK_8           0x03
  66#define PCC_CCR_CLK_MASK        PCC_CCR_CLK_8
  67
  68#define PCC_CCR_RST_CHAN(c)     (0x01 << ((c) + 2))
  69#define PCC_CCR_RST_ALL         (PCC_CCR_RST_CHAN(0) | PCC_CCR_RST_CHAN(1))
  70#define PCC_CCR_RST_MASK        PCC_CCR_RST_ALL
  71
  72/* led selection bits */
  73#define PCC_LED(c)              (1 << (c))
  74#define PCC_LED_ALL             (PCC_LED(0) | PCC_LED(1))
  75
  76/* led state value */
  77#define PCC_LED_ON              0x00
  78#define PCC_LED_FAST            0x01
  79#define PCC_LED_SLOW            0x02
  80#define PCC_LED_OFF             0x03
  81
  82#define PCC_CCR_LED_CHAN(s, c)  ((s) << (((c) + 2) << 1))
  83
  84#define PCC_CCR_LED_ON_CHAN(c)          PCC_CCR_LED_CHAN(PCC_LED_ON, c)
  85#define PCC_CCR_LED_FAST_CHAN(c)        PCC_CCR_LED_CHAN(PCC_LED_FAST, c)
  86#define PCC_CCR_LED_SLOW_CHAN(c)        PCC_CCR_LED_CHAN(PCC_LED_SLOW, c)
  87#define PCC_CCR_LED_OFF_CHAN(c)         PCC_CCR_LED_CHAN(PCC_LED_OFF, c)
  88#define PCC_CCR_LED_MASK_CHAN(c)        PCC_CCR_LED_OFF_CHAN(c)
  89#define PCC_CCR_LED_OFF_ALL             (PCC_CCR_LED_OFF_CHAN(0) | \
  90                                         PCC_CCR_LED_OFF_CHAN(1))
  91#define PCC_CCR_LED_MASK                PCC_CCR_LED_OFF_ALL
  92
  93#define PCC_CCR_INIT    (PCC_CCR_CLK_16 | PCC_CCR_RST_ALL | PCC_CCR_LED_OFF_ALL)
  94
  95/* CSR bits */
  96#define PCC_CSR_SPI_BUSY                0x04
  97
  98/* time waiting for SPI busy (prevent from infinite loop) */
  99#define PCC_SPI_MAX_BUSY_WAIT_MS        3
 100
 101/* max count of reading the SPI status register waiting for a change */
 102/* (prevent from infinite loop) */
 103#define PCC_WRITE_MAX_LOOP              1000
 104
 105/* max nb of int handled by that isr in one shot (prevent from infinite loop) */
 106#define PCC_ISR_MAX_LOOP                10
 107
 108/* EEPROM chip instruction set */
 109/* note: EEPROM Read/Write instructions include A8 bit */
 110#define PCC_EEP_WRITE(a)        (0x02 | (((a) & 0x100) >> 5))
 111#define PCC_EEP_READ(a)         (0x03 | (((a) & 0x100) >> 5))
 112#define PCC_EEP_WRDI            0x04    /* EEPROM Write Disable */
 113#define PCC_EEP_RDSR            0x05    /* EEPROM Read Status Register */
 114#define PCC_EEP_WREN            0x06    /* EEPROM Write Enable */
 115
 116/* EEPROM Status Register bits */
 117#define PCC_EEP_SR_WEN          0x02    /* EEPROM SR Write Enable bit */
 118#define PCC_EEP_SR_WIP          0x01    /* EEPROM SR Write In Progress bit */
 119
 120/*
 121 * The board configuration is probably following:
 122 * RX1 is connected to ground.
 123 * TX1 is not connected.
 124 * CLKO is not connected.
 125 * Setting the OCR register to 0xDA is a good idea.
 126 * This means normal output mode, push-pull and the correct polarity.
 127 */
 128#define PCC_OCR                 (OCR_TX0_PUSHPULL | OCR_TX1_PUSHPULL)
 129
 130/*
 131 * In the CDR register, you should set CBP to 1.
 132 * You will probably also want to set the clock divider value to 7
 133 * (meaning direct oscillator output) because the second SJA1000 chip
 134 * is driven by the first one CLKOUT output.
 135 */
 136#define PCC_CDR                 (CDR_CBP | CDR_CLKOUT_MASK)
 137
 138struct pcan_channel {
 139        struct net_device *netdev;
 140        unsigned long prev_rx_bytes;
 141        unsigned long prev_tx_bytes;
 142};
 143
 144/* PCAN-PC Card private structure */
 145struct pcan_pccard {
 146        struct pcmcia_device *pdev;
 147        int chan_count;
 148        struct pcan_channel channel[PCC_CHAN_MAX];
 149        u8 ccr;
 150        u8 fw_major;
 151        u8 fw_minor;
 152        void __iomem *ioport_addr;
 153        struct timer_list led_timer;
 154};
 155
 156static struct pcmcia_device_id pcan_table[] = {
 157        PCMCIA_DEVICE_MANF_CARD(PCC_MANF_ID, PCC_CARD_ID),
 158        PCMCIA_DEVICE_NULL,
 159};
 160
 161MODULE_DEVICE_TABLE(pcmcia, pcan_table);
 162
 163static void pcan_set_leds(struct pcan_pccard *card, u8 mask, u8 state);
 164
 165/*
 166 * start timer which controls leds state
 167 */
 168static void pcan_start_led_timer(struct pcan_pccard *card)
 169{
 170        if (!timer_pending(&card->led_timer))
 171                mod_timer(&card->led_timer, jiffies + HZ);
 172}
 173
 174/*
 175 * stop the timer which controls leds state
 176 */
 177static void pcan_stop_led_timer(struct pcan_pccard *card)
 178{
 179        del_timer_sync(&card->led_timer);
 180}
 181
 182/*
 183 * read a sja1000 register
 184 */
 185static u8 pcan_read_canreg(const struct sja1000_priv *priv, int port)
 186{
 187        return ioread8(priv->reg_base + port);
 188}
 189
 190/*
 191 * write a sja1000 register
 192 */
 193static void pcan_write_canreg(const struct sja1000_priv *priv, int port, u8 v)
 194{
 195        struct pcan_pccard *card = priv->priv;
 196        int c = (priv->reg_base - card->ioport_addr) / PCC_CHAN_SIZE;
 197
 198        /* sja1000 register changes control the leds state */
 199        if (port == REG_MOD)
 200                switch (v) {
 201                case MOD_RM:
 202                        /* Reset Mode: set led on */
 203                        pcan_set_leds(card, PCC_LED(c), PCC_LED_ON);
 204                        break;
 205                case 0x00:
 206                        /* Normal Mode: led slow blinking and start led timer */
 207                        pcan_set_leds(card, PCC_LED(c), PCC_LED_SLOW);
 208                        pcan_start_led_timer(card);
 209                        break;
 210                default:
 211                        break;
 212                }
 213
 214        iowrite8(v, priv->reg_base + port);
 215}
 216
 217/*
 218 * read a register from the common area
 219 */
 220static u8 pcan_read_reg(struct pcan_pccard *card, int port)
 221{
 222        return ioread8(card->ioport_addr + PCC_COMN_OFF + port);
 223}
 224
 225/*
 226 * write a register into the common area
 227 */
 228static void pcan_write_reg(struct pcan_pccard *card, int port, u8 v)
 229{
 230        /* cache ccr value */
 231        if (port == PCC_CCR) {
 232                if (card->ccr == v)
 233                        return;
 234                card->ccr = v;
 235        }
 236
 237        iowrite8(v, card->ioport_addr + PCC_COMN_OFF + port);
 238}
 239
 240/*
 241 * check whether the card is present by checking its fw version numbers
 242 * against values read at probing time.
 243 */
 244static inline int pcan_pccard_present(struct pcan_pccard *card)
 245{
 246        return ((pcan_read_reg(card, PCC_FW_MAJOR) == card->fw_major) &&
 247                (pcan_read_reg(card, PCC_FW_MINOR) == card->fw_minor));
 248}
 249
 250/*
 251 * wait for SPI engine while it is busy
 252 */
 253static int pcan_wait_spi_busy(struct pcan_pccard *card)
 254{
 255        unsigned long timeout = jiffies +
 256                                msecs_to_jiffies(PCC_SPI_MAX_BUSY_WAIT_MS) + 1;
 257
 258        /* be sure to read status at least once after sleeping */
 259        while (pcan_read_reg(card, PCC_CSR) & PCC_CSR_SPI_BUSY) {
 260                if (time_after(jiffies, timeout))
 261                        return -EBUSY;
 262                schedule();
 263        }
 264
 265        return 0;
 266}
 267
 268/*
 269 * write data in device eeprom
 270 */
 271static int pcan_write_eeprom(struct pcan_pccard *card, u16 addr, u8 v)
 272{
 273        u8 status;
 274        int err, i;
 275
 276        /* write instruction enabling write */
 277        pcan_write_reg(card, PCC_SPI_IR, PCC_EEP_WREN);
 278        err = pcan_wait_spi_busy(card);
 279        if (err)
 280                goto we_spi_err;
 281
 282        /* wait until write enabled */
 283        for (i = 0; i < PCC_WRITE_MAX_LOOP; i++) {
 284                /* write instruction reading the status register */
 285                pcan_write_reg(card, PCC_SPI_IR, PCC_EEP_RDSR);
 286                err = pcan_wait_spi_busy(card);
 287                if (err)
 288                        goto we_spi_err;
 289
 290                /* get status register value and check write enable bit */
 291                status = pcan_read_reg(card, PCC_SPI_DIR);
 292                if (status & PCC_EEP_SR_WEN)
 293                        break;
 294        }
 295
 296        if (i >= PCC_WRITE_MAX_LOOP) {
 297                dev_err(&card->pdev->dev,
 298                        "stop waiting to be allowed to write in eeprom\n");
 299                return -EIO;
 300        }
 301
 302        /* set address and data */
 303        pcan_write_reg(card, PCC_SPI_ADR, addr & 0xff);
 304        pcan_write_reg(card, PCC_SPI_DOR, v);
 305
 306        /*
 307         * write instruction with bit[3] set according to address value:
 308         * if addr refers to upper half of the memory array: bit[3] = 1
 309         */
 310        pcan_write_reg(card, PCC_SPI_IR, PCC_EEP_WRITE(addr));
 311        err = pcan_wait_spi_busy(card);
 312        if (err)
 313                goto we_spi_err;
 314
 315        /* wait while write in progress */
 316        for (i = 0; i < PCC_WRITE_MAX_LOOP; i++) {
 317                /* write instruction reading the status register */
 318                pcan_write_reg(card, PCC_SPI_IR, PCC_EEP_RDSR);
 319                err = pcan_wait_spi_busy(card);
 320                if (err)
 321                        goto we_spi_err;
 322
 323                /* get status register value and check write in progress bit */
 324                status = pcan_read_reg(card, PCC_SPI_DIR);
 325                if (!(status & PCC_EEP_SR_WIP))
 326                        break;
 327        }
 328
 329        if (i >= PCC_WRITE_MAX_LOOP) {
 330                dev_err(&card->pdev->dev,
 331                        "stop waiting for write in eeprom to complete\n");
 332                return -EIO;
 333        }
 334
 335        /* write instruction disabling write */
 336        pcan_write_reg(card, PCC_SPI_IR, PCC_EEP_WRDI);
 337        err = pcan_wait_spi_busy(card);
 338        if (err)
 339                goto we_spi_err;
 340
 341        return 0;
 342
 343we_spi_err:
 344        dev_err(&card->pdev->dev,
 345                "stop waiting (spi engine always busy) err %d\n", err);
 346
 347        return err;
 348}
 349
 350static void pcan_set_leds(struct pcan_pccard *card, u8 led_mask, u8 state)
 351{
 352        u8 ccr = card->ccr;
 353        int i;
 354
 355        for (i = 0; i < card->chan_count; i++)
 356                if (led_mask & PCC_LED(i)) {
 357                        /* clear corresponding led bits in ccr */
 358                        ccr &= ~PCC_CCR_LED_MASK_CHAN(i);
 359                        /* then set new bits */
 360                        ccr |= PCC_CCR_LED_CHAN(state, i);
 361                }
 362
 363        /* real write only if something has changed in ccr */
 364        pcan_write_reg(card, PCC_CCR, ccr);
 365}
 366
 367/*
 368 * enable/disable CAN connectors power
 369 */
 370static inline void pcan_set_can_power(struct pcan_pccard *card, int onoff)
 371{
 372        int err;
 373
 374        err = pcan_write_eeprom(card, 0, !!onoff);
 375        if (err)
 376                dev_err(&card->pdev->dev,
 377                        "failed setting power %s to can connectors (err %d)\n",
 378                        (onoff) ? "on" : "off", err);
 379}
 380
 381/*
 382 * set leds state according to channel activity
 383 */
 384static void pcan_led_timer(unsigned long arg)
 385{
 386        struct pcan_pccard *card = (struct pcan_pccard *)arg;
 387        struct net_device *netdev;
 388        int i, up_count = 0;
 389        u8 ccr;
 390
 391        ccr = card->ccr;
 392        for (i = 0; i < card->chan_count; i++) {
 393                /* default is: not configured */
 394                ccr &= ~PCC_CCR_LED_MASK_CHAN(i);
 395                ccr |= PCC_CCR_LED_ON_CHAN(i);
 396
 397                netdev = card->channel[i].netdev;
 398                if (!netdev || !(netdev->flags & IFF_UP))
 399                        continue;
 400
 401                up_count++;
 402
 403                /* no activity (but configured) */
 404                ccr &= ~PCC_CCR_LED_MASK_CHAN(i);
 405                ccr |= PCC_CCR_LED_SLOW_CHAN(i);
 406
 407                /* if bytes counters changed, set fast blinking led */
 408                if (netdev->stats.rx_bytes != card->channel[i].prev_rx_bytes) {
 409                        card->channel[i].prev_rx_bytes = netdev->stats.rx_bytes;
 410                        ccr &= ~PCC_CCR_LED_MASK_CHAN(i);
 411                        ccr |= PCC_CCR_LED_FAST_CHAN(i);
 412                }
 413                if (netdev->stats.tx_bytes != card->channel[i].prev_tx_bytes) {
 414                        card->channel[i].prev_tx_bytes = netdev->stats.tx_bytes;
 415                        ccr &= ~PCC_CCR_LED_MASK_CHAN(i);
 416                        ccr |= PCC_CCR_LED_FAST_CHAN(i);
 417                }
 418        }
 419
 420        /* write the new leds state */
 421        pcan_write_reg(card, PCC_CCR, ccr);
 422
 423        /* restart timer (except if no more configured channels) */
 424        if (up_count)
 425                mod_timer(&card->led_timer, jiffies + HZ);
 426}
 427
 428/*
 429 * interrupt service routine
 430 */
 431static irqreturn_t pcan_isr(int irq, void *dev_id)
 432{
 433        struct pcan_pccard *card = dev_id;
 434        int irq_handled;
 435
 436        /* prevent from infinite loop */
 437        for (irq_handled = 0; irq_handled < PCC_ISR_MAX_LOOP; irq_handled++) {
 438                /* handle shared interrupt and next loop */
 439                int nothing_to_handle = 1;
 440                int i;
 441
 442                /* check interrupt for each channel */
 443                for (i = 0; i < card->chan_count; i++) {
 444                        struct net_device *netdev;
 445
 446                        /*
 447                         * check whether the card is present before calling
 448                         * sja1000_interrupt() to speed up hotplug detection
 449                         */
 450                        if (!pcan_pccard_present(card)) {
 451                                /* card unplugged during isr */
 452                                return IRQ_NONE;
 453                        }
 454
 455                        /*
 456                         * should check whether all or SJA1000_MAX_IRQ
 457                         * interrupts have been handled: loop again to be sure.
 458                         */
 459                        netdev = card->channel[i].netdev;
 460                        if (netdev &&
 461                            sja1000_interrupt(irq, netdev) == IRQ_HANDLED)
 462                                nothing_to_handle = 0;
 463                }
 464
 465                if (nothing_to_handle)
 466                        break;
 467        }
 468
 469        return (irq_handled) ? IRQ_HANDLED : IRQ_NONE;
 470}
 471
 472/*
 473 * free all resources used by the channels and switch off leds and can power
 474 */
 475static void pcan_free_channels(struct pcan_pccard *card)
 476{
 477        int i;
 478        u8 led_mask = 0;
 479
 480        for (i = 0; i < card->chan_count; i++) {
 481                struct net_device *netdev;
 482                char name[IFNAMSIZ];
 483
 484                led_mask |= PCC_LED(i);
 485
 486                netdev = card->channel[i].netdev;
 487                if (!netdev)
 488                        continue;
 489
 490                strncpy(name, netdev->name, IFNAMSIZ);
 491
 492                unregister_sja1000dev(netdev);
 493
 494                free_sja1000dev(netdev);
 495
 496                dev_info(&card->pdev->dev, "%s removed\n", name);
 497        }
 498
 499        /* do it only if device not removed */
 500        if (pcan_pccard_present(card)) {
 501                pcan_set_leds(card, led_mask, PCC_LED_OFF);
 502                pcan_set_can_power(card, 0);
 503        }
 504}
 505
 506/*
 507 * check if a CAN controller is present at the specified location
 508 */
 509static inline int pcan_channel_present(struct sja1000_priv *priv)
 510{
 511        /* make sure SJA1000 is in reset mode */
 512        pcan_write_canreg(priv, REG_MOD, 1);
 513        pcan_write_canreg(priv, REG_CDR, CDR_PELICAN);
 514
 515        /* read reset-values */
 516        if (pcan_read_canreg(priv, REG_CDR) == CDR_PELICAN)
 517                return 1;
 518
 519        return 0;
 520}
 521
 522static int pcan_add_channels(struct pcan_pccard *card)
 523{
 524        struct pcmcia_device *pdev = card->pdev;
 525        int i, err = 0;
 526        u8 ccr = PCC_CCR_INIT;
 527
 528        /* init common registers (reset channels and leds off) */
 529        card->ccr = ~ccr;
 530        pcan_write_reg(card, PCC_CCR, ccr);
 531
 532        /* wait 2ms before unresetting channels */
 533        mdelay(2);
 534
 535        ccr &= ~PCC_CCR_RST_ALL;
 536        pcan_write_reg(card, PCC_CCR, ccr);
 537
 538        /* create one network device per channel detected */
 539        for (i = 0; i < ARRAY_SIZE(card->channel); i++) {
 540                struct net_device *netdev;
 541                struct sja1000_priv *priv;
 542
 543                netdev = alloc_sja1000dev(0);
 544                if (!netdev) {
 545                        err = -ENOMEM;
 546                        break;
 547                }
 548
 549                /* update linkages */
 550                priv = netdev_priv(netdev);
 551                priv->priv = card;
 552                SET_NETDEV_DEV(netdev, &pdev->dev);
 553
 554                priv->irq_flags = IRQF_SHARED;
 555                netdev->irq = pdev->irq;
 556                priv->reg_base = card->ioport_addr + PCC_CHAN_OFF(i);
 557
 558                /* check if channel is present */
 559                if (!pcan_channel_present(priv)) {
 560                        dev_err(&pdev->dev, "channel %d not present\n", i);
 561                        free_sja1000dev(netdev);
 562                        continue;
 563                }
 564
 565                priv->read_reg  = pcan_read_canreg;
 566                priv->write_reg = pcan_write_canreg;
 567                priv->can.clock.freq = PCC_CAN_CLOCK;
 568                priv->ocr = PCC_OCR;
 569                priv->cdr = PCC_CDR;
 570
 571                /* Neither a slave device distributes the clock */
 572                if (i > 0)
 573                        priv->cdr |= CDR_CLK_OFF;
 574
 575                priv->flags |= SJA1000_CUSTOM_IRQ_HANDLER;
 576
 577                /* register SJA1000 device */
 578                err = register_sja1000dev(netdev);
 579                if (err) {
 580                        free_sja1000dev(netdev);
 581                        continue;
 582                }
 583
 584                card->channel[i].netdev = netdev;
 585                card->chan_count++;
 586
 587                /* set corresponding led on in the new ccr */
 588                ccr &= ~PCC_CCR_LED_OFF_CHAN(i);
 589
 590                dev_info(&pdev->dev,
 591                        "%s on channel %d at 0x%p irq %d\n",
 592                        netdev->name, i, priv->reg_base, pdev->irq);
 593        }
 594
 595        /* write new ccr (change leds state) */
 596        pcan_write_reg(card, PCC_CCR, ccr);
 597
 598        return err;
 599}
 600
 601static int pcan_conf_check(struct pcmcia_device *pdev, void *priv_data)
 602{
 603        pdev->resource[0]->flags &= ~IO_DATA_PATH_WIDTH;
 604        pdev->resource[0]->flags |= IO_DATA_PATH_WIDTH_8; /* only */
 605        pdev->io_lines = 10;
 606
 607        /* This reserves IO space but doesn't actually enable it */
 608        return pcmcia_request_io(pdev);
 609}
 610
 611/*
 612 * free all resources used by the device
 613 */
 614static void pcan_free(struct pcmcia_device *pdev)
 615{
 616        struct pcan_pccard *card = pdev->priv;
 617
 618        if (!card)
 619                return;
 620
 621        free_irq(pdev->irq, card);
 622        pcan_stop_led_timer(card);
 623
 624        pcan_free_channels(card);
 625
 626        ioport_unmap(card->ioport_addr);
 627
 628        kfree(card);
 629        pdev->priv = NULL;
 630}
 631
 632/*
 633 * setup PCMCIA socket and probe for PEAK-System PC-CARD
 634 */
 635static int __devinit pcan_probe(struct pcmcia_device *pdev)
 636{
 637        struct pcan_pccard *card;
 638        int err;
 639
 640        pdev->config_flags |= CONF_ENABLE_IRQ | CONF_AUTO_SET_IO;
 641
 642        err = pcmcia_loop_config(pdev, pcan_conf_check, NULL);
 643        if (err) {
 644                dev_err(&pdev->dev, "pcmcia_loop_config() error %d\n", err);
 645                goto probe_err_1;
 646        }
 647
 648        if (!pdev->irq) {
 649                dev_err(&pdev->dev, "no irq assigned\n");
 650                err = -ENODEV;
 651                goto probe_err_1;
 652        }
 653
 654        err = pcmcia_enable_device(pdev);
 655        if (err) {
 656                dev_err(&pdev->dev, "pcmcia_enable_device failed err=%d\n",
 657                        err);
 658                goto probe_err_1;
 659        }
 660
 661        card = kzalloc(sizeof(struct pcan_pccard), GFP_KERNEL);
 662        if (!card) {
 663                dev_err(&pdev->dev, "couldn't allocate card memory\n");
 664                err = -ENOMEM;
 665                goto probe_err_2;
 666        }
 667
 668        card->pdev = pdev;
 669        pdev->priv = card;
 670
 671        /* sja1000 api uses iomem */
 672        card->ioport_addr = ioport_map(pdev->resource[0]->start,
 673                                        resource_size(pdev->resource[0]));
 674        if (!card->ioport_addr) {
 675                dev_err(&pdev->dev, "couldn't map io port into io memory\n");
 676                err = -ENOMEM;
 677                goto probe_err_3;
 678        }
 679        card->fw_major = pcan_read_reg(card, PCC_FW_MAJOR);
 680        card->fw_minor = pcan_read_reg(card, PCC_FW_MINOR);
 681
 682        /* display board name and firware version */
 683        dev_info(&pdev->dev, "PEAK-System pcmcia card %s fw %d.%d\n",
 684                pdev->prod_id[1] ? pdev->prod_id[1] : "PCAN-PC Card",
 685                card->fw_major, card->fw_minor);
 686
 687        /* detect available channels */
 688        pcan_add_channels(card);
 689        if (!card->chan_count)
 690                goto probe_err_4;
 691
 692        /* init the timer which controls the leds */
 693        init_timer(&card->led_timer);
 694        card->led_timer.function = pcan_led_timer;
 695        card->led_timer.data = (unsigned long)card;
 696
 697        /* request the given irq */
 698        err = request_irq(pdev->irq, &pcan_isr, IRQF_SHARED, PCC_NAME, card);
 699        if (err) {
 700                dev_err(&pdev->dev, "couldn't request irq%d\n", pdev->irq);
 701                goto probe_err_5;
 702        }
 703
 704        /* power on the connectors */
 705        pcan_set_can_power(card, 1);
 706
 707        return 0;
 708
 709probe_err_5:
 710        /* unregister can devices from network */
 711        pcan_free_channels(card);
 712
 713probe_err_4:
 714        ioport_unmap(card->ioport_addr);
 715
 716probe_err_3:
 717        kfree(card);
 718        pdev->priv = NULL;
 719
 720probe_err_2:
 721        pcmcia_disable_device(pdev);
 722
 723probe_err_1:
 724        return err;
 725}
 726
 727/*
 728 * release claimed resources
 729 */
 730static void pcan_remove(struct pcmcia_device *pdev)
 731{
 732        pcan_free(pdev);
 733        pcmcia_disable_device(pdev);
 734}
 735
 736static struct pcmcia_driver pcan_driver = {
 737        .name = PCC_NAME,
 738        .probe = pcan_probe,
 739        .remove = pcan_remove,
 740        .id_table = pcan_table,
 741};
 742
 743static int __init pcan_init(void)
 744{
 745        return pcmcia_register_driver(&pcan_driver);
 746}
 747module_init(pcan_init);
 748
 749static void __exit pcan_exit(void)
 750{
 751        pcmcia_unregister_driver(&pcan_driver);
 752}
 753module_exit(pcan_exit);
 754