linux/drivers/net/sb1000.c
<<
>>
Prefs
   1/* sb1000.c: A General Instruments SB1000 driver for linux. */
   2/*
   3        Written 1998 by Franco Venturi.
   4
   5        Copyright 1998 by Franco Venturi.
   6        Copyright 1994,1995 by Donald Becker.
   7        Copyright 1993 United States Government as represented by the
   8        Director, National Security Agency.
   9
  10        This driver is for the General Instruments SB1000 (internal SURFboard)
  11
  12        The author may be reached as fventuri@mediaone.net
  13
  14        This program is free software; you can redistribute it
  15        and/or  modify it under  the terms of  the GNU General
  16        Public  License as  published  by  the  Free  Software
  17        Foundation;  either  version 2 of the License, or  (at
  18        your option) any later version.
  19
  20        Changes:
  21
  22        981115 Steven Hirsch <shirsch@adelphia.net>
  23
  24        Linus changed the timer interface.  Should work on all recent
  25        development kernels.
  26
  27        980608 Steven Hirsch <shirsch@adelphia.net>
  28
  29        Small changes to make it work with 2.1.x kernels. Hopefully,
  30        nothing major will change before official release of Linux 2.2.
  31
  32        Merged with 2.2 - Alan Cox
  33*/
  34
  35static char version[] = "sb1000.c:v1.1.2 6/01/98 (fventuri@mediaone.net)\n";
  36
  37#include <linux/module.h>
  38#include <linux/kernel.h>
  39#include <linux/sched.h>
  40#include <linux/string.h>
  41#include <linux/interrupt.h>
  42#include <linux/errno.h>
  43#include <linux/if_cablemodem.h> /* for SIOGCM/SIOSCM stuff */
  44#include <linux/in.h>
  45#include <linux/slab.h>
  46#include <linux/ioport.h>
  47#include <linux/netdevice.h>
  48#include <linux/if_arp.h>
  49#include <linux/skbuff.h>
  50#include <linux/delay.h>        /* for udelay() */
  51#include <linux/etherdevice.h>
  52#include <linux/pnp.h>
  53#include <linux/init.h>
  54#include <linux/bitops.h>
  55
  56#include <asm/io.h>
  57#include <asm/processor.h>
  58#include <asm/uaccess.h>
  59
  60#ifdef SB1000_DEBUG
  61static int sb1000_debug = SB1000_DEBUG;
  62#else
  63static const int sb1000_debug = 1;
  64#endif
  65
  66static const int SB1000_IO_EXTENT = 8;
  67/* SB1000 Maximum Receive Unit */
  68static const int SB1000_MRU = 1500; /* octects */
  69
  70#define NPIDS 4
  71struct sb1000_private {
  72        struct sk_buff *rx_skb[NPIDS];
  73        short rx_dlen[NPIDS];
  74        unsigned int rx_frames;
  75        short rx_error_count;
  76        short rx_error_dpc_count;
  77        unsigned char rx_session_id[NPIDS];
  78        unsigned char rx_frame_id[NPIDS];
  79        unsigned char rx_pkt_type[NPIDS];
  80};
  81
  82/* prototypes for Linux interface */
  83extern int sb1000_probe(struct net_device *dev);
  84static int sb1000_open(struct net_device *dev);
  85static int sb1000_dev_ioctl (struct net_device *dev, struct ifreq *ifr, int cmd);
  86static netdev_tx_t sb1000_start_xmit(struct sk_buff *skb,
  87                                     struct net_device *dev);
  88static irqreturn_t sb1000_interrupt(int irq, void *dev_id);
  89static int sb1000_close(struct net_device *dev);
  90
  91
  92/* SB1000 hardware routines to be used during open/configuration phases */
  93static int card_wait_for_busy_clear(const int ioaddr[],
  94        const char* name);
  95static int card_wait_for_ready(const int ioaddr[], const char* name,
  96        unsigned char in[]);
  97static int card_send_command(const int ioaddr[], const char* name,
  98        const unsigned char out[], unsigned char in[]);
  99
 100/* SB1000 hardware routines to be used during frame rx interrupt */
 101static int sb1000_wait_for_ready(const int ioaddr[], const char* name);
 102static int sb1000_wait_for_ready_clear(const int ioaddr[],
 103        const char* name);
 104static void sb1000_send_command(const int ioaddr[], const char* name,
 105        const unsigned char out[]);
 106static void sb1000_read_status(const int ioaddr[], unsigned char in[]);
 107static void sb1000_issue_read_command(const int ioaddr[],
 108        const char* name);
 109
 110/* SB1000 commands for open/configuration */
 111static int sb1000_reset(const int ioaddr[], const char* name);
 112static int sb1000_check_CRC(const int ioaddr[], const char* name);
 113static inline int sb1000_start_get_set_command(const int ioaddr[],
 114        const char* name);
 115static int sb1000_end_get_set_command(const int ioaddr[],
 116        const char* name);
 117static int sb1000_activate(const int ioaddr[], const char* name);
 118static int sb1000_get_firmware_version(const int ioaddr[],
 119        const char* name, unsigned char version[], int do_end);
 120static int sb1000_get_frequency(const int ioaddr[], const char* name,
 121        int* frequency);
 122static int sb1000_set_frequency(const int ioaddr[], const char* name,
 123        int frequency);
 124static int sb1000_get_PIDs(const int ioaddr[], const char* name,
 125        short PID[]);
 126static int sb1000_set_PIDs(const int ioaddr[], const char* name,
 127        const short PID[]);
 128
 129/* SB1000 commands for frame rx interrupt */
 130static int sb1000_rx(struct net_device *dev);
 131static void sb1000_error_dpc(struct net_device *dev);
 132
 133static const struct pnp_device_id sb1000_pnp_ids[] = {
 134        { "GIC1000", 0 },
 135        { "", 0 }
 136};
 137MODULE_DEVICE_TABLE(pnp, sb1000_pnp_ids);
 138
 139static const struct net_device_ops sb1000_netdev_ops = {
 140        .ndo_open               = sb1000_open,
 141        .ndo_start_xmit         = sb1000_start_xmit,
 142        .ndo_do_ioctl           = sb1000_dev_ioctl,
 143        .ndo_stop               = sb1000_close,
 144        .ndo_change_mtu         = eth_change_mtu,
 145        .ndo_set_mac_address    = eth_mac_addr,
 146        .ndo_validate_addr      = eth_validate_addr,
 147};
 148
 149static int
 150sb1000_probe_one(struct pnp_dev *pdev, const struct pnp_device_id *id)
 151{
 152        struct net_device *dev;
 153        unsigned short ioaddr[2], irq;
 154        unsigned int serial_number;
 155        int error = -ENODEV;
 156
 157        if (pnp_device_attach(pdev) < 0)
 158                return -ENODEV;
 159        if (pnp_activate_dev(pdev) < 0)
 160                goto out_detach;
 161
 162        if (!pnp_port_valid(pdev, 0) || !pnp_port_valid(pdev, 1))
 163                goto out_disable;
 164        if (!pnp_irq_valid(pdev, 0))
 165                goto out_disable;
 166
 167        serial_number = pdev->card->serial;
 168
 169        ioaddr[0] = pnp_port_start(pdev, 0);
 170        ioaddr[1] = pnp_port_start(pdev, 0);
 171
 172        irq = pnp_irq(pdev, 0);
 173
 174        if (!request_region(ioaddr[0], 16, "sb1000"))
 175                goto out_disable;
 176        if (!request_region(ioaddr[1], 16, "sb1000"))
 177                goto out_release_region0;
 178
 179        dev = alloc_etherdev(sizeof(struct sb1000_private));
 180        if (!dev) {
 181                error = -ENOMEM;
 182                goto out_release_regions;
 183        }
 184
 185
 186        dev->base_addr = ioaddr[0];
 187        /* mem_start holds the second I/O address */
 188        dev->mem_start = ioaddr[1];
 189        dev->irq = irq;
 190
 191        if (sb1000_debug > 0)
 192                printk(KERN_NOTICE "%s: sb1000 at (%#3.3lx,%#3.3lx), "
 193                        "S/N %#8.8x, IRQ %d.\n", dev->name, dev->base_addr,
 194                        dev->mem_start, serial_number, dev->irq);
 195
 196        /*
 197         * The SB1000 is an rx-only cable modem device.  The uplink is a modem
 198         * and we do not want to arp on it.
 199         */
 200        dev->flags = IFF_POINTOPOINT|IFF_NOARP;
 201
 202        SET_NETDEV_DEV(dev, &pdev->dev);
 203
 204        if (sb1000_debug > 0)
 205                printk(KERN_NOTICE "%s", version);
 206
 207        dev->netdev_ops = &sb1000_netdev_ops;
 208
 209        /* hardware address is 0:0:serial_number */
 210        dev->dev_addr[2]        = serial_number >> 24 & 0xff;
 211        dev->dev_addr[3]        = serial_number >> 16 & 0xff;
 212        dev->dev_addr[4]        = serial_number >>  8 & 0xff;
 213        dev->dev_addr[5]        = serial_number >>  0 & 0xff;
 214
 215        pnp_set_drvdata(pdev, dev);
 216
 217        error = register_netdev(dev);
 218        if (error)
 219                goto out_free_netdev;
 220        return 0;
 221
 222 out_free_netdev:
 223        free_netdev(dev);
 224 out_release_regions:
 225        release_region(ioaddr[1], 16);
 226 out_release_region0:
 227        release_region(ioaddr[0], 16);
 228 out_disable:
 229        pnp_disable_dev(pdev);
 230 out_detach:
 231        pnp_device_detach(pdev);
 232        return error;
 233}
 234
 235static void
 236sb1000_remove_one(struct pnp_dev *pdev)
 237{
 238        struct net_device *dev = pnp_get_drvdata(pdev);
 239
 240        unregister_netdev(dev);
 241        release_region(dev->base_addr, 16);
 242        release_region(dev->mem_start, 16);
 243        free_netdev(dev);
 244}
 245
 246static struct pnp_driver sb1000_driver = {
 247        .name           = "sb1000",
 248        .id_table       = sb1000_pnp_ids,
 249        .probe          = sb1000_probe_one,
 250        .remove         = sb1000_remove_one,
 251};
 252
 253
 254/*
 255 * SB1000 hardware routines to be used during open/configuration phases
 256 */
 257
 258static const int TimeOutJiffies = (875 * HZ) / 100;
 259
 260/* Card Wait For Busy Clear (cannot be used during an interrupt) */
 261static int
 262card_wait_for_busy_clear(const int ioaddr[], const char* name)
 263{
 264        unsigned char a;
 265        unsigned long timeout;
 266
 267        a = inb(ioaddr[0] + 7);
 268        timeout = jiffies + TimeOutJiffies;
 269        while (a & 0x80 || a & 0x40) {
 270                /* a little sleep */
 271                yield();
 272
 273                a = inb(ioaddr[0] + 7);
 274                if (time_after_eq(jiffies, timeout)) {
 275                        printk(KERN_WARNING "%s: card_wait_for_busy_clear timeout\n",
 276                                name);
 277                        return -ETIME;
 278                }
 279        }
 280
 281        return 0;
 282}
 283
 284/* Card Wait For Ready (cannot be used during an interrupt) */
 285static int
 286card_wait_for_ready(const int ioaddr[], const char* name, unsigned char in[])
 287{
 288        unsigned char a;
 289        unsigned long timeout;
 290
 291        a = inb(ioaddr[1] + 6);
 292        timeout = jiffies + TimeOutJiffies;
 293        while (a & 0x80 || !(a & 0x40)) {
 294                /* a little sleep */
 295                yield();
 296
 297                a = inb(ioaddr[1] + 6);
 298                if (time_after_eq(jiffies, timeout)) {
 299                        printk(KERN_WARNING "%s: card_wait_for_ready timeout\n",
 300                                name);
 301                        return -ETIME;
 302                }
 303        }
 304
 305        in[1] = inb(ioaddr[0] + 1);
 306        in[2] = inb(ioaddr[0] + 2);
 307        in[3] = inb(ioaddr[0] + 3);
 308        in[4] = inb(ioaddr[0] + 4);
 309        in[0] = inb(ioaddr[0] + 5);
 310        in[6] = inb(ioaddr[0] + 6);
 311        in[5] = inb(ioaddr[1] + 6);
 312        return 0;
 313}
 314
 315/* Card Send Command (cannot be used during an interrupt) */
 316static int
 317card_send_command(const int ioaddr[], const char* name,
 318        const unsigned char out[], unsigned char in[])
 319{
 320        int status, x;
 321
 322        if ((status = card_wait_for_busy_clear(ioaddr, name)))
 323                return status;
 324        outb(0xa0, ioaddr[0] + 6);
 325        outb(out[2], ioaddr[0] + 1);
 326        outb(out[3], ioaddr[0] + 2);
 327        outb(out[4], ioaddr[0] + 3);
 328        outb(out[5], ioaddr[0] + 4);
 329        outb(out[1], ioaddr[0] + 5);
 330        outb(0xa0, ioaddr[0] + 6);
 331        outb(out[0], ioaddr[0] + 7);
 332        if (out[0] != 0x20 && out[0] != 0x30) {
 333                if ((status = card_wait_for_ready(ioaddr, name, in)))
 334                        return status;
 335                inb(ioaddr[0] + 7);
 336                if (sb1000_debug > 3)
 337                        printk(KERN_DEBUG "%s: card_send_command "
 338                                "out: %02x%02x%02x%02x%02x%02x  "
 339                                "in: %02x%02x%02x%02x%02x%02x%02x\n", name,
 340                                out[0], out[1], out[2], out[3], out[4], out[5],
 341                                in[0], in[1], in[2], in[3], in[4], in[5], in[6]);
 342        } else {
 343                if (sb1000_debug > 3)
 344                        printk(KERN_DEBUG "%s: card_send_command "
 345                                "out: %02x%02x%02x%02x%02x%02x\n", name,
 346                                out[0], out[1], out[2], out[3], out[4], out[5]);
 347        }
 348
 349        if (out[1] == 0x1b) {
 350                x = (out[2] == 0x02);
 351        } else {
 352                if (out[0] >= 0x80 && in[0] != (out[1] | 0x80))
 353                        return -EIO;
 354        }
 355        return 0;
 356}
 357
 358
 359/*
 360 * SB1000 hardware routines to be used during frame rx interrupt
 361 */
 362static const int Sb1000TimeOutJiffies = 7 * HZ;
 363
 364/* Card Wait For Ready (to be used during frame rx) */
 365static int
 366sb1000_wait_for_ready(const int ioaddr[], const char* name)
 367{
 368        unsigned long timeout;
 369
 370        timeout = jiffies + Sb1000TimeOutJiffies;
 371        while (inb(ioaddr[1] + 6) & 0x80) {
 372                if (time_after_eq(jiffies, timeout)) {
 373                        printk(KERN_WARNING "%s: sb1000_wait_for_ready timeout\n",
 374                                name);
 375                        return -ETIME;
 376                }
 377        }
 378        timeout = jiffies + Sb1000TimeOutJiffies;
 379        while (!(inb(ioaddr[1] + 6) & 0x40)) {
 380                if (time_after_eq(jiffies, timeout)) {
 381                        printk(KERN_WARNING "%s: sb1000_wait_for_ready timeout\n",
 382                                name);
 383                        return -ETIME;
 384                }
 385        }
 386        inb(ioaddr[0] + 7);
 387        return 0;
 388}
 389
 390/* Card Wait For Ready Clear (to be used during frame rx) */
 391static int
 392sb1000_wait_for_ready_clear(const int ioaddr[], const char* name)
 393{
 394        unsigned long timeout;
 395
 396        timeout = jiffies + Sb1000TimeOutJiffies;
 397        while (inb(ioaddr[1] + 6) & 0x80) {
 398                if (time_after_eq(jiffies, timeout)) {
 399                        printk(KERN_WARNING "%s: sb1000_wait_for_ready_clear timeout\n",
 400                                name);
 401                        return -ETIME;
 402                }
 403        }
 404        timeout = jiffies + Sb1000TimeOutJiffies;
 405        while (inb(ioaddr[1] + 6) & 0x40) {
 406                if (time_after_eq(jiffies, timeout)) {
 407                        printk(KERN_WARNING "%s: sb1000_wait_for_ready_clear timeout\n",
 408                                name);
 409                        return -ETIME;
 410                }
 411        }
 412        return 0;
 413}
 414
 415/* Card Send Command (to be used during frame rx) */
 416static void
 417sb1000_send_command(const int ioaddr[], const char* name,
 418        const unsigned char out[])
 419{
 420        outb(out[2], ioaddr[0] + 1);
 421        outb(out[3], ioaddr[0] + 2);
 422        outb(out[4], ioaddr[0] + 3);
 423        outb(out[5], ioaddr[0] + 4);
 424        outb(out[1], ioaddr[0] + 5);
 425        outb(out[0], ioaddr[0] + 7);
 426        if (sb1000_debug > 3)
 427                printk(KERN_DEBUG "%s: sb1000_send_command out: %02x%02x%02x%02x"
 428                        "%02x%02x\n", name, out[0], out[1], out[2], out[3], out[4], out[5]);
 429        return;
 430}
 431
 432/* Card Read Status (to be used during frame rx) */
 433static void
 434sb1000_read_status(const int ioaddr[], unsigned char in[])
 435{
 436        in[1] = inb(ioaddr[0] + 1);
 437        in[2] = inb(ioaddr[0] + 2);
 438        in[3] = inb(ioaddr[0] + 3);
 439        in[4] = inb(ioaddr[0] + 4);
 440        in[0] = inb(ioaddr[0] + 5);
 441        return;
 442}
 443
 444/* Issue Read Command (to be used during frame rx) */
 445static void
 446sb1000_issue_read_command(const int ioaddr[], const char* name)
 447{
 448        static const unsigned char Command0[6] = {0x20, 0x00, 0x00, 0x01, 0x00, 0x00};
 449
 450        sb1000_wait_for_ready_clear(ioaddr, name);
 451        outb(0xa0, ioaddr[0] + 6);
 452        sb1000_send_command(ioaddr, name, Command0);
 453        return;
 454}
 455
 456
 457/*
 458 * SB1000 commands for open/configuration
 459 */
 460/* reset SB1000 card */
 461static int
 462sb1000_reset(const int ioaddr[], const char* name)
 463{
 464        static const unsigned char Command0[6] = {0x80, 0x16, 0x00, 0x00, 0x00, 0x00};
 465
 466        unsigned char st[7];
 467        int port, status;
 468
 469        port = ioaddr[1] + 6;
 470        outb(0x4, port);
 471        inb(port);
 472        udelay(1000);
 473        outb(0x0, port);
 474        inb(port);
 475        ssleep(1);
 476        outb(0x4, port);
 477        inb(port);
 478        udelay(1000);
 479        outb(0x0, port);
 480        inb(port);
 481        udelay(0);
 482
 483        if ((status = card_send_command(ioaddr, name, Command0, st)))
 484                return status;
 485        if (st[3] != 0xf0)
 486                return -EIO;
 487        return 0;
 488}
 489
 490/* check SB1000 firmware CRC */
 491static int
 492sb1000_check_CRC(const int ioaddr[], const char* name)
 493{
 494        static const unsigned char Command0[6] = {0x80, 0x1f, 0x00, 0x00, 0x00, 0x00};
 495
 496        unsigned char st[7];
 497        int crc, status;
 498
 499        /* check CRC */
 500        if ((status = card_send_command(ioaddr, name, Command0, st)))
 501                return status;
 502        if (st[1] != st[3] || st[2] != st[4])
 503                return -EIO;
 504        crc = st[1] << 8 | st[2];
 505        return 0;
 506}
 507
 508static inline int
 509sb1000_start_get_set_command(const int ioaddr[], const char* name)
 510{
 511        static const unsigned char Command0[6] = {0x80, 0x1b, 0x00, 0x00, 0x00, 0x00};
 512
 513        unsigned char st[7];
 514
 515        return card_send_command(ioaddr, name, Command0, st);
 516}
 517
 518static int
 519sb1000_end_get_set_command(const int ioaddr[], const char* name)
 520{
 521        static const unsigned char Command0[6] = {0x80, 0x1b, 0x02, 0x00, 0x00, 0x00};
 522        static const unsigned char Command1[6] = {0x20, 0x00, 0x00, 0x00, 0x00, 0x00};
 523
 524        unsigned char st[7];
 525        int status;
 526
 527        if ((status = card_send_command(ioaddr, name, Command0, st)))
 528                return status;
 529        return card_send_command(ioaddr, name, Command1, st);
 530}
 531
 532static int
 533sb1000_activate(const int ioaddr[], const char* name)
 534{
 535        static const unsigned char Command0[6] = {0x80, 0x11, 0x00, 0x00, 0x00, 0x00};
 536        static const unsigned char Command1[6] = {0x80, 0x16, 0x00, 0x00, 0x00, 0x00};
 537
 538        unsigned char st[7];
 539        int status;
 540
 541        ssleep(1);
 542        if ((status = card_send_command(ioaddr, name, Command0, st)))
 543                return status;
 544        if ((status = card_send_command(ioaddr, name, Command1, st)))
 545                return status;
 546        if (st[3] != 0xf1) {
 547        if ((status = sb1000_start_get_set_command(ioaddr, name)))
 548                        return status;
 549                return -EIO;
 550        }
 551        udelay(1000);
 552    return sb1000_start_get_set_command(ioaddr, name);
 553}
 554
 555/* get SB1000 firmware version */
 556static int
 557sb1000_get_firmware_version(const int ioaddr[], const char* name,
 558        unsigned char version[], int do_end)
 559{
 560        static const unsigned char Command0[6] = {0x80, 0x23, 0x00, 0x00, 0x00, 0x00};
 561
 562        unsigned char st[7];
 563        int status;
 564
 565        if ((status = sb1000_start_get_set_command(ioaddr, name)))
 566                return status;
 567        if ((status = card_send_command(ioaddr, name, Command0, st)))
 568                return status;
 569        if (st[0] != 0xa3)
 570                return -EIO;
 571        version[0] = st[1];
 572        version[1] = st[2];
 573        if (do_end)
 574                return sb1000_end_get_set_command(ioaddr, name);
 575        else
 576                return 0;
 577}
 578
 579/* get SB1000 frequency */
 580static int
 581sb1000_get_frequency(const int ioaddr[], const char* name, int* frequency)
 582{
 583        static const unsigned char Command0[6] = {0x80, 0x44, 0x00, 0x00, 0x00, 0x00};
 584
 585        unsigned char st[7];
 586        int status;
 587
 588        udelay(1000);
 589        if ((status = sb1000_start_get_set_command(ioaddr, name)))
 590                return status;
 591        if ((status = card_send_command(ioaddr, name, Command0, st)))
 592                return status;
 593        *frequency = ((st[1] << 8 | st[2]) << 8 | st[3]) << 8 | st[4];
 594        return sb1000_end_get_set_command(ioaddr, name);
 595}
 596
 597/* set SB1000 frequency */
 598static int
 599sb1000_set_frequency(const int ioaddr[], const char* name, int frequency)
 600{
 601        unsigned char st[7];
 602        int status;
 603        unsigned char Command0[6] = {0x80, 0x29, 0x00, 0x00, 0x00, 0x00};
 604
 605        const int FrequencyLowerLimit = 57000;
 606        const int FrequencyUpperLimit = 804000;
 607
 608        if (frequency < FrequencyLowerLimit || frequency > FrequencyUpperLimit) {
 609                printk(KERN_ERR "%s: frequency chosen (%d kHz) is not in the range "
 610                        "[%d,%d] kHz\n", name, frequency, FrequencyLowerLimit,
 611                        FrequencyUpperLimit);
 612                return -EINVAL;
 613        }
 614        udelay(1000);
 615        if ((status = sb1000_start_get_set_command(ioaddr, name)))
 616                return status;
 617        Command0[5] = frequency & 0xff;
 618        frequency >>= 8;
 619        Command0[4] = frequency & 0xff;
 620        frequency >>= 8;
 621        Command0[3] = frequency & 0xff;
 622        frequency >>= 8;
 623        Command0[2] = frequency & 0xff;
 624        return card_send_command(ioaddr, name, Command0, st);
 625}
 626
 627/* get SB1000 PIDs */
 628static int
 629sb1000_get_PIDs(const int ioaddr[], const char* name, short PID[])
 630{
 631        static const unsigned char Command0[6] = {0x80, 0x40, 0x00, 0x00, 0x00, 0x00};
 632        static const unsigned char Command1[6] = {0x80, 0x41, 0x00, 0x00, 0x00, 0x00};
 633        static const unsigned char Command2[6] = {0x80, 0x42, 0x00, 0x00, 0x00, 0x00};
 634        static const unsigned char Command3[6] = {0x80, 0x43, 0x00, 0x00, 0x00, 0x00};
 635
 636        unsigned char st[7];
 637        int status;
 638
 639        udelay(1000);
 640        if ((status = sb1000_start_get_set_command(ioaddr, name)))
 641                return status;
 642
 643        if ((status = card_send_command(ioaddr, name, Command0, st)))
 644                return status;
 645        PID[0] = st[1] << 8 | st[2];
 646
 647        if ((status = card_send_command(ioaddr, name, Command1, st)))
 648                return status;
 649        PID[1] = st[1] << 8 | st[2];
 650
 651        if ((status = card_send_command(ioaddr, name, Command2, st)))
 652                return status;
 653        PID[2] = st[1] << 8 | st[2];
 654
 655        if ((status = card_send_command(ioaddr, name, Command3, st)))
 656                return status;
 657        PID[3] = st[1] << 8 | st[2];
 658
 659        return sb1000_end_get_set_command(ioaddr, name);
 660}
 661
 662/* set SB1000 PIDs */
 663static int
 664sb1000_set_PIDs(const int ioaddr[], const char* name, const short PID[])
 665{
 666        static const unsigned char Command4[6] = {0x80, 0x2e, 0x00, 0x00, 0x00, 0x00};
 667
 668        unsigned char st[7];
 669        short p;
 670        int status;
 671        unsigned char Command0[6] = {0x80, 0x31, 0x00, 0x00, 0x00, 0x00};
 672        unsigned char Command1[6] = {0x80, 0x32, 0x00, 0x00, 0x00, 0x00};
 673        unsigned char Command2[6] = {0x80, 0x33, 0x00, 0x00, 0x00, 0x00};
 674        unsigned char Command3[6] = {0x80, 0x34, 0x00, 0x00, 0x00, 0x00};
 675
 676        udelay(1000);
 677        if ((status = sb1000_start_get_set_command(ioaddr, name)))
 678                return status;
 679
 680        p = PID[0];
 681        Command0[3] = p & 0xff;
 682        p >>= 8;
 683        Command0[2] = p & 0xff;
 684        if ((status = card_send_command(ioaddr, name, Command0, st)))
 685                return status;
 686
 687        p = PID[1];
 688        Command1[3] = p & 0xff;
 689        p >>= 8;
 690        Command1[2] = p & 0xff;
 691        if ((status = card_send_command(ioaddr, name, Command1, st)))
 692                return status;
 693
 694        p = PID[2];
 695        Command2[3] = p & 0xff;
 696        p >>= 8;
 697        Command2[2] = p & 0xff;
 698        if ((status = card_send_command(ioaddr, name, Command2, st)))
 699                return status;
 700
 701        p = PID[3];
 702        Command3[3] = p & 0xff;
 703        p >>= 8;
 704        Command3[2] = p & 0xff;
 705        if ((status = card_send_command(ioaddr, name, Command3, st)))
 706                return status;
 707
 708        if ((status = card_send_command(ioaddr, name, Command4, st)))
 709                return status;
 710        return sb1000_end_get_set_command(ioaddr, name);
 711}
 712
 713
 714static void
 715sb1000_print_status_buffer(const char* name, unsigned char st[],
 716        unsigned char buffer[], int size)
 717{
 718        int i, j, k;
 719
 720        printk(KERN_DEBUG "%s: status: %02x %02x\n", name, st[0], st[1]);
 721        if (buffer[24] == 0x08 && buffer[25] == 0x00 && buffer[26] == 0x45) {
 722                printk(KERN_DEBUG "%s: length: %d protocol: %d from: %d.%d.%d.%d:%d "
 723                        "to %d.%d.%d.%d:%d\n", name, buffer[28] << 8 | buffer[29],
 724                        buffer[35], buffer[38], buffer[39], buffer[40], buffer[41],
 725            buffer[46] << 8 | buffer[47],
 726                        buffer[42], buffer[43], buffer[44], buffer[45],
 727            buffer[48] << 8 | buffer[49]);
 728        } else {
 729                for (i = 0, k = 0; i < (size + 7) / 8; i++) {
 730                        printk(KERN_DEBUG "%s: %s", name, i ? "       " : "buffer:");
 731                        for (j = 0; j < 8 && k < size; j++, k++)
 732                                printk(" %02x", buffer[k]);
 733                        printk("\n");
 734                }
 735        }
 736        return;
 737}
 738
 739/*
 740 * SB1000 commands for frame rx interrupt
 741 */
 742/* receive a single frame and assemble datagram
 743 * (this is the heart of the interrupt routine)
 744 */
 745static int
 746sb1000_rx(struct net_device *dev)
 747{
 748
 749#define FRAMESIZE 184
 750        unsigned char st[2], buffer[FRAMESIZE], session_id, frame_id;
 751        short dlen;
 752        int ioaddr, ns;
 753        unsigned int skbsize;
 754        struct sk_buff *skb;
 755        struct sb1000_private *lp = netdev_priv(dev);
 756        struct net_device_stats *stats = &dev->stats;
 757
 758        /* SB1000 frame constants */
 759        const int FrameSize = FRAMESIZE;
 760        const int NewDatagramHeaderSkip = 8;
 761        const int NewDatagramHeaderSize = NewDatagramHeaderSkip + 18;
 762        const int NewDatagramDataSize = FrameSize - NewDatagramHeaderSize;
 763        const int ContDatagramHeaderSkip = 7;
 764        const int ContDatagramHeaderSize = ContDatagramHeaderSkip + 1;
 765        const int ContDatagramDataSize = FrameSize - ContDatagramHeaderSize;
 766        const int TrailerSize = 4;
 767
 768        ioaddr = dev->base_addr;
 769
 770        insw(ioaddr, (unsigned short*) st, 1);
 771#ifdef XXXDEBUG
 772printk("cm0: received: %02x %02x\n", st[0], st[1]);
 773#endif /* XXXDEBUG */
 774        lp->rx_frames++;
 775
 776        /* decide if it is a good or bad frame */
 777        for (ns = 0; ns < NPIDS; ns++) {
 778                session_id = lp->rx_session_id[ns];
 779                frame_id = lp->rx_frame_id[ns];
 780                if (st[0] == session_id) {
 781                        if (st[1] == frame_id || (!frame_id && (st[1] & 0xf0) == 0x30)) {
 782                                goto good_frame;
 783                        } else if ((st[1] & 0xf0) == 0x30 && (st[0] & 0x40)) {
 784                                goto skipped_frame;
 785                        } else {
 786                                goto bad_frame;
 787                        }
 788                } else if (st[0] == (session_id | 0x40)) {
 789                        if ((st[1] & 0xf0) == 0x30) {
 790                                goto skipped_frame;
 791                        } else {
 792                                goto bad_frame;
 793                        }
 794                }
 795        }
 796        goto bad_frame;
 797
 798skipped_frame:
 799        stats->rx_frame_errors++;
 800        skb = lp->rx_skb[ns];
 801        if (sb1000_debug > 1)
 802                printk(KERN_WARNING "%s: missing frame(s): got %02x %02x "
 803                        "expecting %02x %02x\n", dev->name, st[0], st[1],
 804                        skb ? session_id : session_id | 0x40, frame_id);
 805        if (skb) {
 806                dev_kfree_skb(skb);
 807                skb = NULL;
 808        }
 809
 810good_frame:
 811        lp->rx_frame_id[ns] = 0x30 | ((st[1] + 1) & 0x0f);
 812        /* new datagram */
 813        if (st[0] & 0x40) {
 814                /* get data length */
 815                insw(ioaddr, buffer, NewDatagramHeaderSize / 2);
 816#ifdef XXXDEBUG
 817printk("cm0: IP identification: %02x%02x  fragment offset: %02x%02x\n", buffer[30], buffer[31], buffer[32], buffer[33]);
 818#endif /* XXXDEBUG */
 819                if (buffer[0] != NewDatagramHeaderSkip) {
 820                        if (sb1000_debug > 1)
 821                                printk(KERN_WARNING "%s: new datagram header skip error: "
 822                                        "got %02x expecting %02x\n", dev->name, buffer[0],
 823                                        NewDatagramHeaderSkip);
 824                        stats->rx_length_errors++;
 825                        insw(ioaddr, buffer, NewDatagramDataSize / 2);
 826                        goto bad_frame_next;
 827                }
 828                dlen = ((buffer[NewDatagramHeaderSkip + 3] & 0x0f) << 8 |
 829                        buffer[NewDatagramHeaderSkip + 4]) - 17;
 830                if (dlen > SB1000_MRU) {
 831                        if (sb1000_debug > 1)
 832                                printk(KERN_WARNING "%s: datagram length (%d) greater "
 833                                        "than MRU (%d)\n", dev->name, dlen, SB1000_MRU);
 834                        stats->rx_length_errors++;
 835                        insw(ioaddr, buffer, NewDatagramDataSize / 2);
 836                        goto bad_frame_next;
 837                }
 838                lp->rx_dlen[ns] = dlen;
 839                /* compute size to allocate for datagram */
 840                skbsize = dlen + FrameSize;
 841                if ((skb = alloc_skb(skbsize, GFP_ATOMIC)) == NULL) {
 842                        if (sb1000_debug > 1)
 843                                printk(KERN_WARNING "%s: can't allocate %d bytes long "
 844                                        "skbuff\n", dev->name, skbsize);
 845                        stats->rx_dropped++;
 846                        insw(ioaddr, buffer, NewDatagramDataSize / 2);
 847                        goto dropped_frame;
 848                }
 849                skb->dev = dev;
 850                skb_reset_mac_header(skb);
 851                skb->protocol = (unsigned short) buffer[NewDatagramHeaderSkip + 16];
 852                insw(ioaddr, skb_put(skb, NewDatagramDataSize),
 853                        NewDatagramDataSize / 2);
 854                lp->rx_skb[ns] = skb;
 855        } else {
 856                /* continuation of previous datagram */
 857                insw(ioaddr, buffer, ContDatagramHeaderSize / 2);
 858                if (buffer[0] != ContDatagramHeaderSkip) {
 859                        if (sb1000_debug > 1)
 860                                printk(KERN_WARNING "%s: cont datagram header skip error: "
 861                                        "got %02x expecting %02x\n", dev->name, buffer[0],
 862                                        ContDatagramHeaderSkip);
 863                        stats->rx_length_errors++;
 864                        insw(ioaddr, buffer, ContDatagramDataSize / 2);
 865                        goto bad_frame_next;
 866                }
 867                skb = lp->rx_skb[ns];
 868                insw(ioaddr, skb_put(skb, ContDatagramDataSize),
 869                        ContDatagramDataSize / 2);
 870                dlen = lp->rx_dlen[ns];
 871        }
 872        if (skb->len < dlen + TrailerSize) {
 873                lp->rx_session_id[ns] &= ~0x40;
 874                return 0;
 875        }
 876
 877        /* datagram completed: send to upper level */
 878        skb_trim(skb, dlen);
 879        netif_rx(skb);
 880        stats->rx_bytes+=dlen;
 881        stats->rx_packets++;
 882        lp->rx_skb[ns] = NULL;
 883        lp->rx_session_id[ns] |= 0x40;
 884        return 0;
 885
 886bad_frame:
 887        insw(ioaddr, buffer, FrameSize / 2);
 888        if (sb1000_debug > 1)
 889                printk(KERN_WARNING "%s: frame error: got %02x %02x\n",
 890                        dev->name, st[0], st[1]);
 891        stats->rx_frame_errors++;
 892bad_frame_next:
 893        if (sb1000_debug > 2)
 894                sb1000_print_status_buffer(dev->name, st, buffer, FrameSize);
 895dropped_frame:
 896        stats->rx_errors++;
 897        if (ns < NPIDS) {
 898                if ((skb = lp->rx_skb[ns])) {
 899                        dev_kfree_skb(skb);
 900                        lp->rx_skb[ns] = NULL;
 901                }
 902                lp->rx_session_id[ns] |= 0x40;
 903        }
 904        return -1;
 905}
 906
 907static void
 908sb1000_error_dpc(struct net_device *dev)
 909{
 910        static const unsigned char Command0[6] = {0x80, 0x26, 0x00, 0x00, 0x00, 0x00};
 911
 912        char *name;
 913        unsigned char st[5];
 914        int ioaddr[2];
 915        struct sb1000_private *lp = netdev_priv(dev);
 916        const int ErrorDpcCounterInitialize = 200;
 917
 918        ioaddr[0] = dev->base_addr;
 919        /* mem_start holds the second I/O address */
 920        ioaddr[1] = dev->mem_start;
 921        name = dev->name;
 922
 923        sb1000_wait_for_ready_clear(ioaddr, name);
 924        sb1000_send_command(ioaddr, name, Command0);
 925        sb1000_wait_for_ready(ioaddr, name);
 926        sb1000_read_status(ioaddr, st);
 927        if (st[1] & 0x10)
 928                lp->rx_error_dpc_count = ErrorDpcCounterInitialize;
 929        return;
 930}
 931
 932
 933/*
 934 * Linux interface functions
 935 */
 936static int
 937sb1000_open(struct net_device *dev)
 938{
 939        char *name;
 940        int ioaddr[2], status;
 941        struct sb1000_private *lp = netdev_priv(dev);
 942        const unsigned short FirmwareVersion[] = {0x01, 0x01};
 943
 944        ioaddr[0] = dev->base_addr;
 945        /* mem_start holds the second I/O address */
 946        ioaddr[1] = dev->mem_start;
 947        name = dev->name;
 948
 949        /* initialize sb1000 */
 950        if ((status = sb1000_reset(ioaddr, name)))
 951                return status;
 952        ssleep(1);
 953        if ((status = sb1000_check_CRC(ioaddr, name)))
 954                return status;
 955
 956        /* initialize private data before board can catch interrupts */
 957        lp->rx_skb[0] = NULL;
 958        lp->rx_skb[1] = NULL;
 959        lp->rx_skb[2] = NULL;
 960        lp->rx_skb[3] = NULL;
 961        lp->rx_dlen[0] = 0;
 962        lp->rx_dlen[1] = 0;
 963        lp->rx_dlen[2] = 0;
 964        lp->rx_dlen[3] = 0;
 965        lp->rx_frames = 0;
 966        lp->rx_error_count = 0;
 967        lp->rx_error_dpc_count = 0;
 968        lp->rx_session_id[0] = 0x50;
 969        lp->rx_session_id[0] = 0x48;
 970        lp->rx_session_id[0] = 0x44;
 971        lp->rx_session_id[0] = 0x42;
 972        lp->rx_frame_id[0] = 0;
 973        lp->rx_frame_id[1] = 0;
 974        lp->rx_frame_id[2] = 0;
 975        lp->rx_frame_id[3] = 0;
 976        if (request_irq(dev->irq, &sb1000_interrupt, 0, "sb1000", dev)) {
 977                return -EAGAIN;
 978        }
 979
 980        if (sb1000_debug > 2)
 981                printk(KERN_DEBUG "%s: Opening, IRQ %d\n", name, dev->irq);
 982
 983        /* Activate board and check firmware version */
 984        udelay(1000);
 985        if ((status = sb1000_activate(ioaddr, name)))
 986                return status;
 987        udelay(0);
 988        if ((status = sb1000_get_firmware_version(ioaddr, name, version, 0)))
 989                return status;
 990        if (version[0] != FirmwareVersion[0] || version[1] != FirmwareVersion[1])
 991                printk(KERN_WARNING "%s: found firmware version %x.%02x "
 992                        "(should be %x.%02x)\n", name, version[0], version[1],
 993                        FirmwareVersion[0], FirmwareVersion[1]);
 994
 995
 996        netif_start_queue(dev);
 997        return 0;                                       /* Always succeed */
 998}
 999
1000static int sb1000_dev_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
1001{
1002        char* name;
1003        unsigned char version[2];
1004        short PID[4];
1005        int ioaddr[2], status, frequency;
1006        unsigned int stats[5];
1007        struct sb1000_private *lp = netdev_priv(dev);
1008
1009        if (!(dev && dev->flags & IFF_UP))
1010                return -ENODEV;
1011
1012        ioaddr[0] = dev->base_addr;
1013        /* mem_start holds the second I/O address */
1014        ioaddr[1] = dev->mem_start;
1015        name = dev->name;
1016
1017        switch (cmd) {
1018        case SIOCGCMSTATS:              /* get statistics */
1019                stats[0] = dev->stats.rx_bytes;
1020                stats[1] = lp->rx_frames;
1021                stats[2] = dev->stats.rx_packets;
1022                stats[3] = dev->stats.rx_errors;
1023                stats[4] = dev->stats.rx_dropped;
1024                if(copy_to_user(ifr->ifr_data, stats, sizeof(stats)))
1025                        return -EFAULT;
1026                status = 0;
1027                break;
1028
1029        case SIOCGCMFIRMWARE:           /* get firmware version */
1030                if ((status = sb1000_get_firmware_version(ioaddr, name, version, 1)))
1031                        return status;
1032                if(copy_to_user(ifr->ifr_data, version, sizeof(version)))
1033                        return -EFAULT;
1034                break;
1035
1036        case SIOCGCMFREQUENCY:          /* get frequency */
1037                if ((status = sb1000_get_frequency(ioaddr, name, &frequency)))
1038                        return status;
1039                if(put_user(frequency, (int __user *) ifr->ifr_data))
1040                        return -EFAULT;
1041                break;
1042
1043        case SIOCSCMFREQUENCY:          /* set frequency */
1044                if (!capable(CAP_NET_ADMIN))
1045                        return -EPERM;
1046                if(get_user(frequency, (int __user *) ifr->ifr_data))
1047                        return -EFAULT;
1048                if ((status = sb1000_set_frequency(ioaddr, name, frequency)))
1049                        return status;
1050                break;
1051
1052        case SIOCGCMPIDS:                       /* get PIDs */
1053                if ((status = sb1000_get_PIDs(ioaddr, name, PID)))
1054                        return status;
1055                if(copy_to_user(ifr->ifr_data, PID, sizeof(PID)))
1056                        return -EFAULT;
1057                break;
1058
1059        case SIOCSCMPIDS:                       /* set PIDs */
1060                if (!capable(CAP_NET_ADMIN))
1061                        return -EPERM;
1062                if(copy_from_user(PID, ifr->ifr_data, sizeof(PID)))
1063                        return -EFAULT;
1064                if ((status = sb1000_set_PIDs(ioaddr, name, PID)))
1065                        return status;
1066                /* set session_id, frame_id and pkt_type too */
1067                lp->rx_session_id[0] = 0x50 | (PID[0] & 0x0f);
1068                lp->rx_session_id[1] = 0x48;
1069                lp->rx_session_id[2] = 0x44;
1070                lp->rx_session_id[3] = 0x42;
1071                lp->rx_frame_id[0] = 0;
1072                lp->rx_frame_id[1] = 0;
1073                lp->rx_frame_id[2] = 0;
1074                lp->rx_frame_id[3] = 0;
1075                break;
1076
1077        default:
1078                status = -EINVAL;
1079                break;
1080        }
1081        return status;
1082}
1083
1084/* transmit function: do nothing since SB1000 can't send anything out */
1085static netdev_tx_t
1086sb1000_start_xmit(struct sk_buff *skb, struct net_device *dev)
1087{
1088        printk(KERN_WARNING "%s: trying to transmit!!!\n", dev->name);
1089        /* sb1000 can't xmit datagrams */
1090        dev_kfree_skb(skb);
1091        return NETDEV_TX_OK;
1092}
1093
1094/* SB1000 interrupt handler. */
1095static irqreturn_t sb1000_interrupt(int irq, void *dev_id)
1096{
1097        static const unsigned char Command0[6] = {0x80, 0x2c, 0x00, 0x00, 0x00, 0x00};
1098        static const unsigned char Command1[6] = {0x80, 0x2e, 0x00, 0x00, 0x00, 0x00};
1099
1100        char *name;
1101        unsigned char st;
1102        int ioaddr[2];
1103        struct net_device *dev = dev_id;
1104        struct sb1000_private *lp = netdev_priv(dev);
1105
1106        const int MaxRxErrorCount = 6;
1107
1108        ioaddr[0] = dev->base_addr;
1109        /* mem_start holds the second I/O address */
1110        ioaddr[1] = dev->mem_start;
1111        name = dev->name;
1112
1113        /* is it a good interrupt? */
1114        st = inb(ioaddr[1] + 6);
1115        if (!(st & 0x08 && st & 0x20)) {
1116                return IRQ_NONE;
1117        }
1118
1119        if (sb1000_debug > 3)
1120                printk(KERN_DEBUG "%s: entering interrupt\n", dev->name);
1121
1122        st = inb(ioaddr[0] + 7);
1123        if (sb1000_rx(dev))
1124                lp->rx_error_count++;
1125#ifdef SB1000_DELAY
1126        udelay(SB1000_DELAY);
1127#endif /* SB1000_DELAY */
1128        sb1000_issue_read_command(ioaddr, name);
1129        if (st & 0x01) {
1130                sb1000_error_dpc(dev);
1131                sb1000_issue_read_command(ioaddr, name);
1132        }
1133        if (lp->rx_error_dpc_count && !(--lp->rx_error_dpc_count)) {
1134                sb1000_wait_for_ready_clear(ioaddr, name);
1135                sb1000_send_command(ioaddr, name, Command0);
1136                sb1000_wait_for_ready(ioaddr, name);
1137                sb1000_issue_read_command(ioaddr, name);
1138        }
1139        if (lp->rx_error_count >= MaxRxErrorCount) {
1140                sb1000_wait_for_ready_clear(ioaddr, name);
1141                sb1000_send_command(ioaddr, name, Command1);
1142                sb1000_wait_for_ready(ioaddr, name);
1143                sb1000_issue_read_command(ioaddr, name);
1144                lp->rx_error_count = 0;
1145        }
1146
1147        return IRQ_HANDLED;
1148}
1149
1150static int sb1000_close(struct net_device *dev)
1151{
1152        int i;
1153        int ioaddr[2];
1154        struct sb1000_private *lp = netdev_priv(dev);
1155
1156        if (sb1000_debug > 2)
1157                printk(KERN_DEBUG "%s: Shutting down sb1000.\n", dev->name);
1158
1159        netif_stop_queue(dev);
1160
1161        ioaddr[0] = dev->base_addr;
1162        /* mem_start holds the second I/O address */
1163        ioaddr[1] = dev->mem_start;
1164
1165        free_irq(dev->irq, dev);
1166        /* If we don't do this, we can't re-insmod it later. */
1167        release_region(ioaddr[1], SB1000_IO_EXTENT);
1168        release_region(ioaddr[0], SB1000_IO_EXTENT);
1169
1170        /* free rx_skb's if needed */
1171        for (i=0; i<4; i++) {
1172                if (lp->rx_skb[i]) {
1173                        dev_kfree_skb(lp->rx_skb[i]);
1174                }
1175        }
1176        return 0;
1177}
1178
1179MODULE_AUTHOR("Franco Venturi <fventuri@mediaone.net>");
1180MODULE_DESCRIPTION("General Instruments SB1000 driver");
1181MODULE_LICENSE("GPL");
1182
1183static int __init
1184sb1000_init(void)
1185{
1186        return pnp_register_driver(&sb1000_driver);
1187}
1188
1189static void __exit
1190sb1000_exit(void)
1191{
1192        pnp_unregister_driver(&sb1000_driver);
1193}
1194
1195module_init(sb1000_init);
1196module_exit(sb1000_exit);
1197