linux/drivers/net/can/sja1000/sja1000.c
<<
>>
Prefs
   1/*
   2 * sja1000.c -  Philips SJA1000 network device driver
   3 *
   4 * Copyright (c) 2003 Matthias Brukner, Trajet Gmbh, Rebenring 33,
   5 * 38106 Braunschweig, GERMANY
   6 *
   7 * Copyright (c) 2002-2007 Volkswagen Group Electronic Research
   8 * All rights reserved.
   9 *
  10 * Redistribution and use in source and binary forms, with or without
  11 * modification, are permitted provided that the following conditions
  12 * are met:
  13 * 1. Redistributions of source code must retain the above copyright
  14 *    notice, this list of conditions and the following disclaimer.
  15 * 2. Redistributions in binary form must reproduce the above copyright
  16 *    notice, this list of conditions and the following disclaimer in the
  17 *    documentation and/or other materials provided with the distribution.
  18 * 3. Neither the name of Volkswagen nor the names of its contributors
  19 *    may be used to endorse or promote products derived from this software
  20 *    without specific prior written permission.
  21 *
  22 * Alternatively, provided that this notice is retained in full, this
  23 * software may be distributed under the terms of the GNU General
  24 * Public License ("GPL") version 2, in which case the provisions of the
  25 * GPL apply INSTEAD OF those given above.
  26 *
  27 * The provided data structures and external interfaces from this code
  28 * are not restricted to be used by modules with a GPL compatible license.
  29 *
  30 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  31 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  32 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  33 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  34 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  35 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  36 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  37 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  38 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  39 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  40 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  41 * DAMAGE.
  42 *
  43 */
  44
  45#include <linux/module.h>
  46#include <linux/init.h>
  47#include <linux/kernel.h>
  48#include <linux/sched.h>
  49#include <linux/types.h>
  50#include <linux/fcntl.h>
  51#include <linux/interrupt.h>
  52#include <linux/ptrace.h>
  53#include <linux/string.h>
  54#include <linux/errno.h>
  55#include <linux/netdevice.h>
  56#include <linux/if_arp.h>
  57#include <linux/if_ether.h>
  58#include <linux/skbuff.h>
  59#include <linux/delay.h>
  60
  61#include <linux/can/dev.h>
  62#include <linux/can/error.h>
  63
  64#include "sja1000.h"
  65
  66#define DRV_NAME "sja1000"
  67
  68MODULE_AUTHOR("Oliver Hartkopp <oliver.hartkopp@volkswagen.de>");
  69MODULE_LICENSE("Dual BSD/GPL");
  70MODULE_DESCRIPTION(DRV_NAME "CAN netdevice driver");
  71
  72static const struct can_bittiming_const sja1000_bittiming_const = {
  73        .name = DRV_NAME,
  74        .tseg1_min = 1,
  75        .tseg1_max = 16,
  76        .tseg2_min = 1,
  77        .tseg2_max = 8,
  78        .sjw_max = 4,
  79        .brp_min = 1,
  80        .brp_max = 64,
  81        .brp_inc = 1,
  82};
  83
  84static void sja1000_write_cmdreg(struct sja1000_priv *priv, u8 val)
  85{
  86        unsigned long flags;
  87
  88        /*
  89         * The command register needs some locking and time to settle
  90         * the write_reg() operation - especially on SMP systems.
  91         */
  92        spin_lock_irqsave(&priv->cmdreg_lock, flags);
  93        priv->write_reg(priv, REG_CMR, val);
  94        priv->read_reg(priv, REG_SR);
  95        spin_unlock_irqrestore(&priv->cmdreg_lock, flags);
  96}
  97
  98static int sja1000_is_absent(struct sja1000_priv *priv)
  99{
 100        return (priv->read_reg(priv, REG_MOD) == 0xFF);
 101}
 102
 103static int sja1000_probe_chip(struct net_device *dev)
 104{
 105        struct sja1000_priv *priv = netdev_priv(dev);
 106
 107        if (priv->reg_base && sja1000_is_absent(priv)) {
 108                printk(KERN_INFO "%s: probing @0x%lX failed\n",
 109                       DRV_NAME, dev->base_addr);
 110                return 0;
 111        }
 112        return -1;
 113}
 114
 115static void set_reset_mode(struct net_device *dev)
 116{
 117        struct sja1000_priv *priv = netdev_priv(dev);
 118        unsigned char status = priv->read_reg(priv, REG_MOD);
 119        int i;
 120
 121        /* disable interrupts */
 122        priv->write_reg(priv, REG_IER, IRQ_OFF);
 123
 124        for (i = 0; i < 100; i++) {
 125                /* check reset bit */
 126                if (status & MOD_RM) {
 127                        priv->can.state = CAN_STATE_STOPPED;
 128                        return;
 129                }
 130
 131                priv->write_reg(priv, REG_MOD, MOD_RM); /* reset chip */
 132                udelay(10);
 133                status = priv->read_reg(priv, REG_MOD);
 134        }
 135
 136        netdev_err(dev, "setting SJA1000 into reset mode failed!\n");
 137}
 138
 139static void set_normal_mode(struct net_device *dev)
 140{
 141        struct sja1000_priv *priv = netdev_priv(dev);
 142        unsigned char status = priv->read_reg(priv, REG_MOD);
 143        int i;
 144
 145        for (i = 0; i < 100; i++) {
 146                /* check reset bit */
 147                if ((status & MOD_RM) == 0) {
 148                        priv->can.state = CAN_STATE_ERROR_ACTIVE;
 149                        /* enable interrupts */
 150                        if (priv->can.ctrlmode & CAN_CTRLMODE_BERR_REPORTING)
 151                                priv->write_reg(priv, REG_IER, IRQ_ALL);
 152                        else
 153                                priv->write_reg(priv, REG_IER,
 154                                                IRQ_ALL & ~IRQ_BEI);
 155                        return;
 156                }
 157
 158                /* set chip to normal mode */
 159                if (priv->can.ctrlmode & CAN_CTRLMODE_LISTENONLY)
 160                        priv->write_reg(priv, REG_MOD, MOD_LOM);
 161                else
 162                        priv->write_reg(priv, REG_MOD, 0x00);
 163
 164                udelay(10);
 165
 166                status = priv->read_reg(priv, REG_MOD);
 167        }
 168
 169        netdev_err(dev, "setting SJA1000 into normal mode failed!\n");
 170}
 171
 172static void sja1000_start(struct net_device *dev)
 173{
 174        struct sja1000_priv *priv = netdev_priv(dev);
 175
 176        /* leave reset mode */
 177        if (priv->can.state != CAN_STATE_STOPPED)
 178                set_reset_mode(dev);
 179
 180        /* Clear error counters and error code capture */
 181        priv->write_reg(priv, REG_TXERR, 0x0);
 182        priv->write_reg(priv, REG_RXERR, 0x0);
 183        priv->read_reg(priv, REG_ECC);
 184
 185        /* leave reset mode */
 186        set_normal_mode(dev);
 187}
 188
 189static int sja1000_set_mode(struct net_device *dev, enum can_mode mode)
 190{
 191        switch (mode) {
 192        case CAN_MODE_START:
 193                sja1000_start(dev);
 194                if (netif_queue_stopped(dev))
 195                        netif_wake_queue(dev);
 196                break;
 197
 198        default:
 199                return -EOPNOTSUPP;
 200        }
 201
 202        return 0;
 203}
 204
 205static int sja1000_set_bittiming(struct net_device *dev)
 206{
 207        struct sja1000_priv *priv = netdev_priv(dev);
 208        struct can_bittiming *bt = &priv->can.bittiming;
 209        u8 btr0, btr1;
 210
 211        btr0 = ((bt->brp - 1) & 0x3f) | (((bt->sjw - 1) & 0x3) << 6);
 212        btr1 = ((bt->prop_seg + bt->phase_seg1 - 1) & 0xf) |
 213                (((bt->phase_seg2 - 1) & 0x7) << 4);
 214        if (priv->can.ctrlmode & CAN_CTRLMODE_3_SAMPLES)
 215                btr1 |= 0x80;
 216
 217        netdev_info(dev, "setting BTR0=0x%02x BTR1=0x%02x\n", btr0, btr1);
 218
 219        priv->write_reg(priv, REG_BTR0, btr0);
 220        priv->write_reg(priv, REG_BTR1, btr1);
 221
 222        return 0;
 223}
 224
 225static int sja1000_get_berr_counter(const struct net_device *dev,
 226                                    struct can_berr_counter *bec)
 227{
 228        struct sja1000_priv *priv = netdev_priv(dev);
 229
 230        bec->txerr = priv->read_reg(priv, REG_TXERR);
 231        bec->rxerr = priv->read_reg(priv, REG_RXERR);
 232
 233        return 0;
 234}
 235
 236/*
 237 * initialize SJA1000 chip:
 238 *   - reset chip
 239 *   - set output mode
 240 *   - set baudrate
 241 *   - enable interrupts
 242 *   - start operating mode
 243 */
 244static void chipset_init(struct net_device *dev)
 245{
 246        struct sja1000_priv *priv = netdev_priv(dev);
 247
 248        /* set clock divider and output control register */
 249        priv->write_reg(priv, REG_CDR, priv->cdr | CDR_PELICAN);
 250
 251        /* set acceptance filter (accept all) */
 252        priv->write_reg(priv, REG_ACCC0, 0x00);
 253        priv->write_reg(priv, REG_ACCC1, 0x00);
 254        priv->write_reg(priv, REG_ACCC2, 0x00);
 255        priv->write_reg(priv, REG_ACCC3, 0x00);
 256
 257        priv->write_reg(priv, REG_ACCM0, 0xFF);
 258        priv->write_reg(priv, REG_ACCM1, 0xFF);
 259        priv->write_reg(priv, REG_ACCM2, 0xFF);
 260        priv->write_reg(priv, REG_ACCM3, 0xFF);
 261
 262        priv->write_reg(priv, REG_OCR, priv->ocr | OCR_MODE_NORMAL);
 263}
 264
 265/*
 266 * transmit a CAN message
 267 * message layout in the sk_buff should be like this:
 268 * xx xx xx xx   ff      ll   00 11 22 33 44 55 66 77
 269 * [  can-id ] [flags] [len] [can data (up to 8 bytes]
 270 */
 271static netdev_tx_t sja1000_start_xmit(struct sk_buff *skb,
 272                                            struct net_device *dev)
 273{
 274        struct sja1000_priv *priv = netdev_priv(dev);
 275        struct can_frame *cf = (struct can_frame *)skb->data;
 276        uint8_t fi;
 277        uint8_t dlc;
 278        canid_t id;
 279        uint8_t dreg;
 280        int i;
 281
 282        if (can_dropped_invalid_skb(dev, skb))
 283                return NETDEV_TX_OK;
 284
 285        netif_stop_queue(dev);
 286
 287        fi = dlc = cf->can_dlc;
 288        id = cf->can_id;
 289
 290        if (id & CAN_RTR_FLAG)
 291                fi |= FI_RTR;
 292
 293        if (id & CAN_EFF_FLAG) {
 294                fi |= FI_FF;
 295                dreg = EFF_BUF;
 296                priv->write_reg(priv, REG_FI, fi);
 297                priv->write_reg(priv, REG_ID1, (id & 0x1fe00000) >> (5 + 16));
 298                priv->write_reg(priv, REG_ID2, (id & 0x001fe000) >> (5 + 8));
 299                priv->write_reg(priv, REG_ID3, (id & 0x00001fe0) >> 5);
 300                priv->write_reg(priv, REG_ID4, (id & 0x0000001f) << 3);
 301        } else {
 302                dreg = SFF_BUF;
 303                priv->write_reg(priv, REG_FI, fi);
 304                priv->write_reg(priv, REG_ID1, (id & 0x000007f8) >> 3);
 305                priv->write_reg(priv, REG_ID2, (id & 0x00000007) << 5);
 306        }
 307
 308        for (i = 0; i < dlc; i++)
 309                priv->write_reg(priv, dreg++, cf->data[i]);
 310
 311        can_put_echo_skb(skb, dev, 0);
 312
 313        if (priv->can.ctrlmode & CAN_CTRLMODE_ONE_SHOT)
 314                sja1000_write_cmdreg(priv, CMD_TR | CMD_AT);
 315        else
 316                sja1000_write_cmdreg(priv, CMD_TR);
 317
 318        return NETDEV_TX_OK;
 319}
 320
 321static void sja1000_rx(struct net_device *dev)
 322{
 323        struct sja1000_priv *priv = netdev_priv(dev);
 324        struct net_device_stats *stats = &dev->stats;
 325        struct can_frame *cf;
 326        struct sk_buff *skb;
 327        uint8_t fi;
 328        uint8_t dreg;
 329        canid_t id;
 330        int i;
 331
 332        /* create zero'ed CAN frame buffer */
 333        skb = alloc_can_skb(dev, &cf);
 334        if (skb == NULL)
 335                return;
 336
 337        fi = priv->read_reg(priv, REG_FI);
 338
 339        if (fi & FI_FF) {
 340                /* extended frame format (EFF) */
 341                dreg = EFF_BUF;
 342                id = (priv->read_reg(priv, REG_ID1) << (5 + 16))
 343                    | (priv->read_reg(priv, REG_ID2) << (5 + 8))
 344                    | (priv->read_reg(priv, REG_ID3) << 5)
 345                    | (priv->read_reg(priv, REG_ID4) >> 3);
 346                id |= CAN_EFF_FLAG;
 347        } else {
 348                /* standard frame format (SFF) */
 349                dreg = SFF_BUF;
 350                id = (priv->read_reg(priv, REG_ID1) << 3)
 351                    | (priv->read_reg(priv, REG_ID2) >> 5);
 352        }
 353
 354        cf->can_dlc = get_can_dlc(fi & 0x0F);
 355        if (fi & FI_RTR) {
 356                id |= CAN_RTR_FLAG;
 357        } else {
 358                for (i = 0; i < cf->can_dlc; i++)
 359                        cf->data[i] = priv->read_reg(priv, dreg++);
 360        }
 361
 362        cf->can_id = id;
 363
 364        /* release receive buffer */
 365        sja1000_write_cmdreg(priv, CMD_RRB);
 366
 367        netif_rx(skb);
 368
 369        stats->rx_packets++;
 370        stats->rx_bytes += cf->can_dlc;
 371}
 372
 373static int sja1000_err(struct net_device *dev, uint8_t isrc, uint8_t status)
 374{
 375        struct sja1000_priv *priv = netdev_priv(dev);
 376        struct net_device_stats *stats = &dev->stats;
 377        struct can_frame *cf;
 378        struct sk_buff *skb;
 379        enum can_state state = priv->can.state;
 380        uint8_t ecc, alc;
 381
 382        skb = alloc_can_err_skb(dev, &cf);
 383        if (skb == NULL)
 384                return -ENOMEM;
 385
 386        if (isrc & IRQ_DOI) {
 387                /* data overrun interrupt */
 388                netdev_dbg(dev, "data overrun interrupt\n");
 389                cf->can_id |= CAN_ERR_CRTL;
 390                cf->data[1] = CAN_ERR_CRTL_RX_OVERFLOW;
 391                stats->rx_over_errors++;
 392                stats->rx_errors++;
 393                sja1000_write_cmdreg(priv, CMD_CDO);    /* clear bit */
 394        }
 395
 396        if (isrc & IRQ_EI) {
 397                /* error warning interrupt */
 398                netdev_dbg(dev, "error warning interrupt\n");
 399
 400                if (status & SR_BS) {
 401                        state = CAN_STATE_BUS_OFF;
 402                        cf->can_id |= CAN_ERR_BUSOFF;
 403                        can_bus_off(dev);
 404                } else if (status & SR_ES) {
 405                        state = CAN_STATE_ERROR_WARNING;
 406                } else
 407                        state = CAN_STATE_ERROR_ACTIVE;
 408        }
 409        if (isrc & IRQ_BEI) {
 410                /* bus error interrupt */
 411                priv->can.can_stats.bus_error++;
 412                stats->rx_errors++;
 413
 414                ecc = priv->read_reg(priv, REG_ECC);
 415
 416                cf->can_id |= CAN_ERR_PROT | CAN_ERR_BUSERROR;
 417
 418                switch (ecc & ECC_MASK) {
 419                case ECC_BIT:
 420                        cf->data[2] |= CAN_ERR_PROT_BIT;
 421                        break;
 422                case ECC_FORM:
 423                        cf->data[2] |= CAN_ERR_PROT_FORM;
 424                        break;
 425                case ECC_STUFF:
 426                        cf->data[2] |= CAN_ERR_PROT_STUFF;
 427                        break;
 428                default:
 429                        cf->data[2] |= CAN_ERR_PROT_UNSPEC;
 430                        cf->data[3] = ecc & ECC_SEG;
 431                        break;
 432                }
 433                /* Error occurred during transmission? */
 434                if ((ecc & ECC_DIR) == 0)
 435                        cf->data[2] |= CAN_ERR_PROT_TX;
 436        }
 437        if (isrc & IRQ_EPI) {
 438                /* error passive interrupt */
 439                netdev_dbg(dev, "error passive interrupt\n");
 440                if (status & SR_ES)
 441                        state = CAN_STATE_ERROR_PASSIVE;
 442                else
 443                        state = CAN_STATE_ERROR_ACTIVE;
 444        }
 445        if (isrc & IRQ_ALI) {
 446                /* arbitration lost interrupt */
 447                netdev_dbg(dev, "arbitration lost interrupt\n");
 448                alc = priv->read_reg(priv, REG_ALC);
 449                priv->can.can_stats.arbitration_lost++;
 450                stats->tx_errors++;
 451                cf->can_id |= CAN_ERR_LOSTARB;
 452                cf->data[0] = alc & 0x1f;
 453        }
 454
 455        if (state != priv->can.state && (state == CAN_STATE_ERROR_WARNING ||
 456                                         state == CAN_STATE_ERROR_PASSIVE)) {
 457                uint8_t rxerr = priv->read_reg(priv, REG_RXERR);
 458                uint8_t txerr = priv->read_reg(priv, REG_TXERR);
 459                cf->can_id |= CAN_ERR_CRTL;
 460                if (state == CAN_STATE_ERROR_WARNING) {
 461                        priv->can.can_stats.error_warning++;
 462                        cf->data[1] = (txerr > rxerr) ?
 463                                CAN_ERR_CRTL_TX_WARNING :
 464                                CAN_ERR_CRTL_RX_WARNING;
 465                } else {
 466                        priv->can.can_stats.error_passive++;
 467                        cf->data[1] = (txerr > rxerr) ?
 468                                CAN_ERR_CRTL_TX_PASSIVE :
 469                                CAN_ERR_CRTL_RX_PASSIVE;
 470                }
 471                cf->data[6] = txerr;
 472                cf->data[7] = rxerr;
 473        }
 474
 475        priv->can.state = state;
 476
 477        netif_rx(skb);
 478
 479        stats->rx_packets++;
 480        stats->rx_bytes += cf->can_dlc;
 481
 482        return 0;
 483}
 484
 485irqreturn_t sja1000_interrupt(int irq, void *dev_id)
 486{
 487        struct net_device *dev = (struct net_device *)dev_id;
 488        struct sja1000_priv *priv = netdev_priv(dev);
 489        struct net_device_stats *stats = &dev->stats;
 490        uint8_t isrc, status;
 491        int n = 0;
 492
 493        /* Shared interrupts and IRQ off? */
 494        if (priv->read_reg(priv, REG_IER) == IRQ_OFF)
 495                return IRQ_NONE;
 496
 497        if (priv->pre_irq)
 498                priv->pre_irq(priv);
 499
 500        while ((isrc = priv->read_reg(priv, REG_IR)) && (n < SJA1000_MAX_IRQ)) {
 501                n++;
 502                status = priv->read_reg(priv, REG_SR);
 503                /* check for absent controller due to hw unplug */
 504                if (status == 0xFF && sja1000_is_absent(priv))
 505                        return IRQ_NONE;
 506
 507                if (isrc & IRQ_WUI)
 508                        netdev_warn(dev, "wakeup interrupt\n");
 509
 510                if (isrc & IRQ_TI) {
 511                        /* transmission buffer released */
 512                        if (priv->can.ctrlmode & CAN_CTRLMODE_ONE_SHOT &&
 513                            !(status & SR_TCS)) {
 514                                stats->tx_errors++;
 515                                can_free_echo_skb(dev, 0);
 516                        } else {
 517                                /* transmission complete */
 518                                stats->tx_bytes +=
 519                                        priv->read_reg(priv, REG_FI) & 0xf;
 520                                stats->tx_packets++;
 521                                can_get_echo_skb(dev, 0);
 522                        }
 523                        netif_wake_queue(dev);
 524                }
 525                if (isrc & IRQ_RI) {
 526                        /* receive interrupt */
 527                        while (status & SR_RBS) {
 528                                sja1000_rx(dev);
 529                                status = priv->read_reg(priv, REG_SR);
 530                                /* check for absent controller */
 531                                if (status == 0xFF && sja1000_is_absent(priv))
 532                                        return IRQ_NONE;
 533                        }
 534                }
 535                if (isrc & (IRQ_DOI | IRQ_EI | IRQ_BEI | IRQ_EPI | IRQ_ALI)) {
 536                        /* error interrupt */
 537                        if (sja1000_err(dev, isrc, status))
 538                                break;
 539                }
 540        }
 541
 542        if (priv->post_irq)
 543                priv->post_irq(priv);
 544
 545        if (n >= SJA1000_MAX_IRQ)
 546                netdev_dbg(dev, "%d messages handled in ISR", n);
 547
 548        return (n) ? IRQ_HANDLED : IRQ_NONE;
 549}
 550EXPORT_SYMBOL_GPL(sja1000_interrupt);
 551
 552static int sja1000_open(struct net_device *dev)
 553{
 554        struct sja1000_priv *priv = netdev_priv(dev);
 555        int err;
 556
 557        /* set chip into reset mode */
 558        set_reset_mode(dev);
 559
 560        /* common open */
 561        err = open_candev(dev);
 562        if (err)
 563                return err;
 564
 565        /* register interrupt handler, if not done by the device driver */
 566        if (!(priv->flags & SJA1000_CUSTOM_IRQ_HANDLER)) {
 567                err = request_irq(dev->irq, sja1000_interrupt, priv->irq_flags,
 568                                  dev->name, (void *)dev);
 569                if (err) {
 570                        close_candev(dev);
 571                        return -EAGAIN;
 572                }
 573        }
 574
 575        /* init and start chi */
 576        sja1000_start(dev);
 577
 578        netif_start_queue(dev);
 579
 580        return 0;
 581}
 582
 583static int sja1000_close(struct net_device *dev)
 584{
 585        struct sja1000_priv *priv = netdev_priv(dev);
 586
 587        netif_stop_queue(dev);
 588        set_reset_mode(dev);
 589
 590        if (!(priv->flags & SJA1000_CUSTOM_IRQ_HANDLER))
 591                free_irq(dev->irq, (void *)dev);
 592
 593        close_candev(dev);
 594
 595        return 0;
 596}
 597
 598struct net_device *alloc_sja1000dev(int sizeof_priv)
 599{
 600        struct net_device *dev;
 601        struct sja1000_priv *priv;
 602
 603        dev = alloc_candev(sizeof(struct sja1000_priv) + sizeof_priv,
 604                SJA1000_ECHO_SKB_MAX);
 605        if (!dev)
 606                return NULL;
 607
 608        priv = netdev_priv(dev);
 609
 610        priv->dev = dev;
 611        priv->can.bittiming_const = &sja1000_bittiming_const;
 612        priv->can.do_set_bittiming = sja1000_set_bittiming;
 613        priv->can.do_set_mode = sja1000_set_mode;
 614        priv->can.do_get_berr_counter = sja1000_get_berr_counter;
 615        priv->can.ctrlmode_supported = CAN_CTRLMODE_3_SAMPLES |
 616                CAN_CTRLMODE_BERR_REPORTING | CAN_CTRLMODE_LISTENONLY |
 617                CAN_CTRLMODE_ONE_SHOT;
 618
 619        spin_lock_init(&priv->cmdreg_lock);
 620
 621        if (sizeof_priv)
 622                priv->priv = (void *)priv + sizeof(struct sja1000_priv);
 623
 624        return dev;
 625}
 626EXPORT_SYMBOL_GPL(alloc_sja1000dev);
 627
 628void free_sja1000dev(struct net_device *dev)
 629{
 630        free_candev(dev);
 631}
 632EXPORT_SYMBOL_GPL(free_sja1000dev);
 633
 634static const struct net_device_ops sja1000_netdev_ops = {
 635       .ndo_open               = sja1000_open,
 636       .ndo_stop               = sja1000_close,
 637       .ndo_start_xmit         = sja1000_start_xmit,
 638};
 639
 640int register_sja1000dev(struct net_device *dev)
 641{
 642        if (!sja1000_probe_chip(dev))
 643                return -ENODEV;
 644
 645        dev->flags |= IFF_ECHO; /* we support local echo */
 646        dev->netdev_ops = &sja1000_netdev_ops;
 647
 648        set_reset_mode(dev);
 649        chipset_init(dev);
 650
 651        return register_candev(dev);
 652}
 653EXPORT_SYMBOL_GPL(register_sja1000dev);
 654
 655void unregister_sja1000dev(struct net_device *dev)
 656{
 657        set_reset_mode(dev);
 658        unregister_candev(dev);
 659}
 660EXPORT_SYMBOL_GPL(unregister_sja1000dev);
 661
 662static __init int sja1000_init(void)
 663{
 664        printk(KERN_INFO "%s CAN netdevice driver\n", DRV_NAME);
 665
 666        return 0;
 667}
 668
 669module_init(sja1000_init);
 670
 671static __exit void sja1000_exit(void)
 672{
 673        printk(KERN_INFO "%s: driver removed\n", DRV_NAME);
 674}
 675
 676module_exit(sja1000_exit);
 677