Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /* sb1000.c: A General Instruments SB1000 driver for linux. */
0003 /*
0004     Written 1998 by Franco Venturi.
0005 
0006     Copyright 1998 by Franco Venturi.
0007     Copyright 1994,1995 by Donald Becker.
0008     Copyright 1993 United States Government as represented by the
0009     Director, National Security Agency.
0010 
0011     This driver is for the General Instruments SB1000 (internal SURFboard)
0012 
0013     The author may be reached as fventuri@mediaone.net
0014 
0015 
0016     Changes:
0017 
0018     981115 Steven Hirsch <shirsch@adelphia.net>
0019 
0020     Linus changed the timer interface.  Should work on all recent
0021     development kernels.
0022 
0023     980608 Steven Hirsch <shirsch@adelphia.net>
0024 
0025     Small changes to make it work with 2.1.x kernels. Hopefully,
0026     nothing major will change before official release of Linux 2.2.
0027 
0028     Merged with 2.2 - Alan Cox
0029 */
0030 
0031 static char version[] = "sb1000.c:v1.1.2 6/01/98 (fventuri@mediaone.net)\n";
0032 
0033 #include <linux/module.h>
0034 #include <linux/kernel.h>
0035 #include <linux/sched.h>
0036 #include <linux/string.h>
0037 #include <linux/interrupt.h>
0038 #include <linux/errno.h>
0039 #include <linux/if_cablemodem.h> /* for SIOGCM/SIOSCM stuff */
0040 #include <linux/in.h>
0041 #include <linux/ioport.h>
0042 #include <linux/netdevice.h>
0043 #include <linux/if_arp.h>
0044 #include <linux/skbuff.h>
0045 #include <linux/delay.h>    /* for udelay() */
0046 #include <linux/etherdevice.h>
0047 #include <linux/pnp.h>
0048 #include <linux/init.h>
0049 #include <linux/bitops.h>
0050 #include <linux/gfp.h>
0051 
0052 #include <asm/io.h>
0053 #include <asm/processor.h>
0054 #include <linux/uaccess.h>
0055 
0056 #ifdef SB1000_DEBUG
0057 static int sb1000_debug = SB1000_DEBUG;
0058 #else
0059 static const int sb1000_debug = 1;
0060 #endif
0061 
0062 static const int SB1000_IO_EXTENT = 8;
0063 /* SB1000 Maximum Receive Unit */
0064 static const int SB1000_MRU = 1500; /* octects */
0065 
0066 #define NPIDS 4
0067 struct sb1000_private {
0068     struct sk_buff *rx_skb[NPIDS];
0069     short rx_dlen[NPIDS];
0070     unsigned int rx_frames;
0071     short rx_error_count;
0072     short rx_error_dpc_count;
0073     unsigned char rx_session_id[NPIDS];
0074     unsigned char rx_frame_id[NPIDS];
0075     unsigned char rx_pkt_type[NPIDS];
0076 };
0077 
0078 /* prototypes for Linux interface */
0079 extern int sb1000_probe(struct net_device *dev);
0080 static int sb1000_open(struct net_device *dev);
0081 static int sb1000_siocdevprivate(struct net_device *dev, struct ifreq *ifr,
0082                  void __user *data, int cmd);
0083 static netdev_tx_t sb1000_start_xmit(struct sk_buff *skb,
0084                      struct net_device *dev);
0085 static irqreturn_t sb1000_interrupt(int irq, void *dev_id);
0086 static int sb1000_close(struct net_device *dev);
0087 
0088 
0089 /* SB1000 hardware routines to be used during open/configuration phases */
0090 static int card_wait_for_busy_clear(const int ioaddr[],
0091     const char* name);
0092 static int card_wait_for_ready(const int ioaddr[], const char* name,
0093     unsigned char in[]);
0094 static int card_send_command(const int ioaddr[], const char* name,
0095     const unsigned char out[], unsigned char in[]);
0096 
0097 /* SB1000 hardware routines to be used during frame rx interrupt */
0098 static int sb1000_wait_for_ready(const int ioaddr[], const char* name);
0099 static int sb1000_wait_for_ready_clear(const int ioaddr[],
0100     const char* name);
0101 static void sb1000_send_command(const int ioaddr[], const char* name,
0102     const unsigned char out[]);
0103 static void sb1000_read_status(const int ioaddr[], unsigned char in[]);
0104 static void sb1000_issue_read_command(const int ioaddr[],
0105     const char* name);
0106 
0107 /* SB1000 commands for open/configuration */
0108 static int sb1000_reset(const int ioaddr[], const char* name);
0109 static int sb1000_check_CRC(const int ioaddr[], const char* name);
0110 static inline int sb1000_start_get_set_command(const int ioaddr[],
0111     const char* name);
0112 static int sb1000_end_get_set_command(const int ioaddr[],
0113     const char* name);
0114 static int sb1000_activate(const int ioaddr[], const char* name);
0115 static int sb1000_get_firmware_version(const int ioaddr[],
0116     const char* name, unsigned char version[], int do_end);
0117 static int sb1000_get_frequency(const int ioaddr[], const char* name,
0118     int* frequency);
0119 static int sb1000_set_frequency(const int ioaddr[], const char* name,
0120     int frequency);
0121 static int sb1000_get_PIDs(const int ioaddr[], const char* name,
0122     short PID[]);
0123 static int sb1000_set_PIDs(const int ioaddr[], const char* name,
0124     const short PID[]);
0125 
0126 /* SB1000 commands for frame rx interrupt */
0127 static int sb1000_rx(struct net_device *dev);
0128 static void sb1000_error_dpc(struct net_device *dev);
0129 
0130 static const struct pnp_device_id sb1000_pnp_ids[] = {
0131     { "GIC1000", 0 },
0132     { "", 0 }
0133 };
0134 MODULE_DEVICE_TABLE(pnp, sb1000_pnp_ids);
0135 
0136 static const struct net_device_ops sb1000_netdev_ops = {
0137     .ndo_open       = sb1000_open,
0138     .ndo_start_xmit     = sb1000_start_xmit,
0139     .ndo_siocdevprivate = sb1000_siocdevprivate,
0140     .ndo_stop       = sb1000_close,
0141     .ndo_set_mac_address    = eth_mac_addr,
0142     .ndo_validate_addr  = eth_validate_addr,
0143 };
0144 
0145 static int
0146 sb1000_probe_one(struct pnp_dev *pdev, const struct pnp_device_id *id)
0147 {
0148     struct net_device *dev;
0149     unsigned short ioaddr[2], irq;
0150     unsigned int serial_number;
0151     int error = -ENODEV;
0152     u8 addr[ETH_ALEN];
0153 
0154     if (pnp_device_attach(pdev) < 0)
0155         return -ENODEV;
0156     if (pnp_activate_dev(pdev) < 0)
0157         goto out_detach;
0158 
0159     if (!pnp_port_valid(pdev, 0) || !pnp_port_valid(pdev, 1))
0160         goto out_disable;
0161     if (!pnp_irq_valid(pdev, 0))
0162         goto out_disable;
0163 
0164     serial_number = pdev->card->serial;
0165 
0166     ioaddr[0] = pnp_port_start(pdev, 0);
0167     ioaddr[1] = pnp_port_start(pdev, 0);
0168 
0169     irq = pnp_irq(pdev, 0);
0170 
0171     if (!request_region(ioaddr[0], 16, "sb1000"))
0172         goto out_disable;
0173     if (!request_region(ioaddr[1], 16, "sb1000"))
0174         goto out_release_region0;
0175 
0176     dev = alloc_etherdev(sizeof(struct sb1000_private));
0177     if (!dev) {
0178         error = -ENOMEM;
0179         goto out_release_regions;
0180     }
0181 
0182 
0183     dev->base_addr = ioaddr[0];
0184     /* mem_start holds the second I/O address */
0185     dev->mem_start = ioaddr[1];
0186     dev->irq = irq;
0187 
0188     if (sb1000_debug > 0)
0189         printk(KERN_NOTICE "%s: sb1000 at (%#3.3lx,%#3.3lx), "
0190             "S/N %#8.8x, IRQ %d.\n", dev->name, dev->base_addr,
0191             dev->mem_start, serial_number, dev->irq);
0192 
0193     /*
0194      * The SB1000 is an rx-only cable modem device.  The uplink is a modem
0195      * and we do not want to arp on it.
0196      */
0197     dev->flags = IFF_POINTOPOINT|IFF_NOARP;
0198 
0199     SET_NETDEV_DEV(dev, &pdev->dev);
0200 
0201     if (sb1000_debug > 0)
0202         printk(KERN_NOTICE "%s", version);
0203 
0204     dev->netdev_ops = &sb1000_netdev_ops;
0205 
0206     /* hardware address is 0:0:serial_number */
0207     addr[0] = 0;
0208     addr[1] = 0;
0209     addr[2] = serial_number >> 24 & 0xff;
0210     addr[3] = serial_number >> 16 & 0xff;
0211     addr[4] = serial_number >>  8 & 0xff;
0212     addr[5] = serial_number >>  0 & 0xff;
0213     eth_hw_addr_set(dev, addr);
0214 
0215     pnp_set_drvdata(pdev, dev);
0216 
0217     error = register_netdev(dev);
0218     if (error)
0219         goto out_free_netdev;
0220     return 0;
0221 
0222  out_free_netdev:
0223     free_netdev(dev);
0224  out_release_regions:
0225     release_region(ioaddr[1], 16);
0226  out_release_region0:
0227     release_region(ioaddr[0], 16);
0228  out_disable:
0229     pnp_disable_dev(pdev);
0230  out_detach:
0231     pnp_device_detach(pdev);
0232     return error;
0233 }
0234 
0235 static void
0236 sb1000_remove_one(struct pnp_dev *pdev)
0237 {
0238     struct net_device *dev = pnp_get_drvdata(pdev);
0239 
0240     unregister_netdev(dev);
0241     release_region(dev->base_addr, 16);
0242     release_region(dev->mem_start, 16);
0243     free_netdev(dev);
0244 }
0245 
0246 static struct pnp_driver sb1000_driver = {
0247     .name       = "sb1000",
0248     .id_table   = sb1000_pnp_ids,
0249     .probe      = sb1000_probe_one,
0250     .remove     = sb1000_remove_one,
0251 };
0252 
0253 
0254 /*
0255  * SB1000 hardware routines to be used during open/configuration phases
0256  */
0257 
0258 static const int TimeOutJiffies = (875 * HZ) / 100;
0259 
0260 /* Card Wait For Busy Clear (cannot be used during an interrupt) */
0261 static int
0262 card_wait_for_busy_clear(const int ioaddr[], const char* name)
0263 {
0264     unsigned char a;
0265     unsigned long timeout;
0266 
0267     a = inb(ioaddr[0] + 7);
0268     timeout = jiffies + TimeOutJiffies;
0269     while (a & 0x80 || a & 0x40) {
0270         /* a little sleep */
0271         yield();
0272 
0273         a = inb(ioaddr[0] + 7);
0274         if (time_after_eq(jiffies, timeout)) {
0275             printk(KERN_WARNING "%s: card_wait_for_busy_clear timeout\n",
0276                 name);
0277             return -ETIME;
0278         }
0279     }
0280 
0281     return 0;
0282 }
0283 
0284 /* Card Wait For Ready (cannot be used during an interrupt) */
0285 static int
0286 card_wait_for_ready(const int ioaddr[], const char* name, unsigned char in[])
0287 {
0288     unsigned char a;
0289     unsigned long timeout;
0290 
0291     a = inb(ioaddr[1] + 6);
0292     timeout = jiffies + TimeOutJiffies;
0293     while (a & 0x80 || !(a & 0x40)) {
0294         /* a little sleep */
0295         yield();
0296 
0297         a = inb(ioaddr[1] + 6);
0298         if (time_after_eq(jiffies, timeout)) {
0299             printk(KERN_WARNING "%s: card_wait_for_ready timeout\n",
0300                 name);
0301             return -ETIME;
0302         }
0303     }
0304 
0305     in[1] = inb(ioaddr[0] + 1);
0306     in[2] = inb(ioaddr[0] + 2);
0307     in[3] = inb(ioaddr[0] + 3);
0308     in[4] = inb(ioaddr[0] + 4);
0309     in[0] = inb(ioaddr[0] + 5);
0310     in[6] = inb(ioaddr[0] + 6);
0311     in[5] = inb(ioaddr[1] + 6);
0312     return 0;
0313 }
0314 
0315 /* Card Send Command (cannot be used during an interrupt) */
0316 static int
0317 card_send_command(const int ioaddr[], const char* name,
0318     const unsigned char out[], unsigned char in[])
0319 {
0320     int status;
0321 
0322     if ((status = card_wait_for_busy_clear(ioaddr, name)))
0323         return status;
0324     outb(0xa0, ioaddr[0] + 6);
0325     outb(out[2], ioaddr[0] + 1);
0326     outb(out[3], ioaddr[0] + 2);
0327     outb(out[4], ioaddr[0] + 3);
0328     outb(out[5], ioaddr[0] + 4);
0329     outb(out[1], ioaddr[0] + 5);
0330     outb(0xa0, ioaddr[0] + 6);
0331     outb(out[0], ioaddr[0] + 7);
0332     if (out[0] != 0x20 && out[0] != 0x30) {
0333         if ((status = card_wait_for_ready(ioaddr, name, in)))
0334             return status;
0335         inb(ioaddr[0] + 7);
0336         if (sb1000_debug > 3)
0337             printk(KERN_DEBUG "%s: card_send_command "
0338                 "out: %02x%02x%02x%02x%02x%02x  "
0339                 "in: %02x%02x%02x%02x%02x%02x%02x\n", name,
0340                 out[0], out[1], out[2], out[3], out[4], out[5],
0341                 in[0], in[1], in[2], in[3], in[4], in[5], in[6]);
0342     } else {
0343         if (sb1000_debug > 3)
0344             printk(KERN_DEBUG "%s: card_send_command "
0345                 "out: %02x%02x%02x%02x%02x%02x\n", name,
0346                 out[0], out[1], out[2], out[3], out[4], out[5]);
0347     }
0348 
0349     if (out[1] != 0x1b) {
0350         if (out[0] >= 0x80 && in[0] != (out[1] | 0x80))
0351             return -EIO;
0352     }
0353     return 0;
0354 }
0355 
0356 
0357 /*
0358  * SB1000 hardware routines to be used during frame rx interrupt
0359  */
0360 static const int Sb1000TimeOutJiffies = 7 * HZ;
0361 
0362 /* Card Wait For Ready (to be used during frame rx) */
0363 static int
0364 sb1000_wait_for_ready(const int ioaddr[], const char* name)
0365 {
0366     unsigned long timeout;
0367 
0368     timeout = jiffies + Sb1000TimeOutJiffies;
0369     while (inb(ioaddr[1] + 6) & 0x80) {
0370         if (time_after_eq(jiffies, timeout)) {
0371             printk(KERN_WARNING "%s: sb1000_wait_for_ready timeout\n",
0372                 name);
0373             return -ETIME;
0374         }
0375     }
0376     timeout = jiffies + Sb1000TimeOutJiffies;
0377     while (!(inb(ioaddr[1] + 6) & 0x40)) {
0378         if (time_after_eq(jiffies, timeout)) {
0379             printk(KERN_WARNING "%s: sb1000_wait_for_ready timeout\n",
0380                 name);
0381             return -ETIME;
0382         }
0383     }
0384     inb(ioaddr[0] + 7);
0385     return 0;
0386 }
0387 
0388 /* Card Wait For Ready Clear (to be used during frame rx) */
0389 static int
0390 sb1000_wait_for_ready_clear(const int ioaddr[], const char* name)
0391 {
0392     unsigned long timeout;
0393 
0394     timeout = jiffies + Sb1000TimeOutJiffies;
0395     while (inb(ioaddr[1] + 6) & 0x80) {
0396         if (time_after_eq(jiffies, timeout)) {
0397             printk(KERN_WARNING "%s: sb1000_wait_for_ready_clear timeout\n",
0398                 name);
0399             return -ETIME;
0400         }
0401     }
0402     timeout = jiffies + Sb1000TimeOutJiffies;
0403     while (inb(ioaddr[1] + 6) & 0x40) {
0404         if (time_after_eq(jiffies, timeout)) {
0405             printk(KERN_WARNING "%s: sb1000_wait_for_ready_clear timeout\n",
0406                 name);
0407             return -ETIME;
0408         }
0409     }
0410     return 0;
0411 }
0412 
0413 /* Card Send Command (to be used during frame rx) */
0414 static void
0415 sb1000_send_command(const int ioaddr[], const char* name,
0416     const unsigned char out[])
0417 {
0418     outb(out[2], ioaddr[0] + 1);
0419     outb(out[3], ioaddr[0] + 2);
0420     outb(out[4], ioaddr[0] + 3);
0421     outb(out[5], ioaddr[0] + 4);
0422     outb(out[1], ioaddr[0] + 5);
0423     outb(out[0], ioaddr[0] + 7);
0424     if (sb1000_debug > 3)
0425         printk(KERN_DEBUG "%s: sb1000_send_command out: %02x%02x%02x%02x"
0426             "%02x%02x\n", name, out[0], out[1], out[2], out[3], out[4], out[5]);
0427 }
0428 
0429 /* Card Read Status (to be used during frame rx) */
0430 static void
0431 sb1000_read_status(const int ioaddr[], unsigned char in[])
0432 {
0433     in[1] = inb(ioaddr[0] + 1);
0434     in[2] = inb(ioaddr[0] + 2);
0435     in[3] = inb(ioaddr[0] + 3);
0436     in[4] = inb(ioaddr[0] + 4);
0437     in[0] = inb(ioaddr[0] + 5);
0438 }
0439 
0440 /* Issue Read Command (to be used during frame rx) */
0441 static void
0442 sb1000_issue_read_command(const int ioaddr[], const char* name)
0443 {
0444     static const unsigned char Command0[6] = {0x20, 0x00, 0x00, 0x01, 0x00, 0x00};
0445 
0446     sb1000_wait_for_ready_clear(ioaddr, name);
0447     outb(0xa0, ioaddr[0] + 6);
0448     sb1000_send_command(ioaddr, name, Command0);
0449 }
0450 
0451 
0452 /*
0453  * SB1000 commands for open/configuration
0454  */
0455 /* reset SB1000 card */
0456 static int
0457 sb1000_reset(const int ioaddr[], const char* name)
0458 {
0459     static const unsigned char Command0[6] = {0x80, 0x16, 0x00, 0x00, 0x00, 0x00};
0460 
0461     unsigned char st[7];
0462     int port, status;
0463 
0464     port = ioaddr[1] + 6;
0465     outb(0x4, port);
0466     inb(port);
0467     udelay(1000);
0468     outb(0x0, port);
0469     inb(port);
0470     ssleep(1);
0471     outb(0x4, port);
0472     inb(port);
0473     udelay(1000);
0474     outb(0x0, port);
0475     inb(port);
0476     udelay(0);
0477 
0478     if ((status = card_send_command(ioaddr, name, Command0, st)))
0479         return status;
0480     if (st[3] != 0xf0)
0481         return -EIO;
0482     return 0;
0483 }
0484 
0485 /* check SB1000 firmware CRC */
0486 static int
0487 sb1000_check_CRC(const int ioaddr[], const char* name)
0488 {
0489     static const unsigned char Command0[6] = {0x80, 0x1f, 0x00, 0x00, 0x00, 0x00};
0490 
0491     unsigned char st[7];
0492     int status;
0493 
0494     /* check CRC */
0495     if ((status = card_send_command(ioaddr, name, Command0, st)))
0496         return status;
0497     if (st[1] != st[3] || st[2] != st[4])
0498         return -EIO;
0499     return 0;
0500 }
0501 
0502 static inline int
0503 sb1000_start_get_set_command(const int ioaddr[], const char* name)
0504 {
0505     static const unsigned char Command0[6] = {0x80, 0x1b, 0x00, 0x00, 0x00, 0x00};
0506 
0507     unsigned char st[7];
0508 
0509     return card_send_command(ioaddr, name, Command0, st);
0510 }
0511 
0512 static int
0513 sb1000_end_get_set_command(const int ioaddr[], const char* name)
0514 {
0515     static const unsigned char Command0[6] = {0x80, 0x1b, 0x02, 0x00, 0x00, 0x00};
0516     static const unsigned char Command1[6] = {0x20, 0x00, 0x00, 0x00, 0x00, 0x00};
0517 
0518     unsigned char st[7];
0519     int status;
0520 
0521     if ((status = card_send_command(ioaddr, name, Command0, st)))
0522         return status;
0523     return card_send_command(ioaddr, name, Command1, st);
0524 }
0525 
0526 static int
0527 sb1000_activate(const int ioaddr[], const char* name)
0528 {
0529     static const unsigned char Command0[6] = {0x80, 0x11, 0x00, 0x00, 0x00, 0x00};
0530     static const unsigned char Command1[6] = {0x80, 0x16, 0x00, 0x00, 0x00, 0x00};
0531 
0532     unsigned char st[7];
0533     int status;
0534 
0535     ssleep(1);
0536     status = card_send_command(ioaddr, name, Command0, st);
0537     if (status)
0538         return status;
0539     status = card_send_command(ioaddr, name, Command1, st);
0540     if (status)
0541         return status;
0542     if (st[3] != 0xf1) {
0543         status = sb1000_start_get_set_command(ioaddr, name);
0544         if (status)
0545             return status;
0546         return -EIO;
0547     }
0548     udelay(1000);
0549     return sb1000_start_get_set_command(ioaddr, name);
0550 }
0551 
0552 /* get SB1000 firmware version */
0553 static int
0554 sb1000_get_firmware_version(const int ioaddr[], const char* name,
0555     unsigned char version[], int do_end)
0556 {
0557     static const unsigned char Command0[6] = {0x80, 0x23, 0x00, 0x00, 0x00, 0x00};
0558 
0559     unsigned char st[7];
0560     int status;
0561 
0562     if ((status = sb1000_start_get_set_command(ioaddr, name)))
0563         return status;
0564     if ((status = card_send_command(ioaddr, name, Command0, st)))
0565         return status;
0566     if (st[0] != 0xa3)
0567         return -EIO;
0568     version[0] = st[1];
0569     version[1] = st[2];
0570     if (do_end)
0571         return sb1000_end_get_set_command(ioaddr, name);
0572     else
0573         return 0;
0574 }
0575 
0576 /* get SB1000 frequency */
0577 static int
0578 sb1000_get_frequency(const int ioaddr[], const char* name, int* frequency)
0579 {
0580     static const unsigned char Command0[6] = {0x80, 0x44, 0x00, 0x00, 0x00, 0x00};
0581 
0582     unsigned char st[7];
0583     int status;
0584 
0585     udelay(1000);
0586     if ((status = sb1000_start_get_set_command(ioaddr, name)))
0587         return status;
0588     if ((status = card_send_command(ioaddr, name, Command0, st)))
0589         return status;
0590     *frequency = ((st[1] << 8 | st[2]) << 8 | st[3]) << 8 | st[4];
0591     return sb1000_end_get_set_command(ioaddr, name);
0592 }
0593 
0594 /* set SB1000 frequency */
0595 static int
0596 sb1000_set_frequency(const int ioaddr[], const char* name, int frequency)
0597 {
0598     unsigned char st[7];
0599     int status;
0600     unsigned char Command0[6] = {0x80, 0x29, 0x00, 0x00, 0x00, 0x00};
0601 
0602     const int FrequencyLowerLimit = 57000;
0603     const int FrequencyUpperLimit = 804000;
0604 
0605     if (frequency < FrequencyLowerLimit || frequency > FrequencyUpperLimit) {
0606         printk(KERN_ERR "%s: frequency chosen (%d kHz) is not in the range "
0607             "[%d,%d] kHz\n", name, frequency, FrequencyLowerLimit,
0608             FrequencyUpperLimit);
0609         return -EINVAL;
0610     }
0611     udelay(1000);
0612     if ((status = sb1000_start_get_set_command(ioaddr, name)))
0613         return status;
0614     Command0[5] = frequency & 0xff;
0615     frequency >>= 8;
0616     Command0[4] = frequency & 0xff;
0617     frequency >>= 8;
0618     Command0[3] = frequency & 0xff;
0619     frequency >>= 8;
0620     Command0[2] = frequency & 0xff;
0621     return card_send_command(ioaddr, name, Command0, st);
0622 }
0623 
0624 /* get SB1000 PIDs */
0625 static int
0626 sb1000_get_PIDs(const int ioaddr[], const char* name, short PID[])
0627 {
0628     static const unsigned char Command0[6] = {0x80, 0x40, 0x00, 0x00, 0x00, 0x00};
0629     static const unsigned char Command1[6] = {0x80, 0x41, 0x00, 0x00, 0x00, 0x00};
0630     static const unsigned char Command2[6] = {0x80, 0x42, 0x00, 0x00, 0x00, 0x00};
0631     static const unsigned char Command3[6] = {0x80, 0x43, 0x00, 0x00, 0x00, 0x00};
0632 
0633     unsigned char st[7];
0634     int status;
0635 
0636     udelay(1000);
0637     if ((status = sb1000_start_get_set_command(ioaddr, name)))
0638         return status;
0639 
0640     if ((status = card_send_command(ioaddr, name, Command0, st)))
0641         return status;
0642     PID[0] = st[1] << 8 | st[2];
0643 
0644     if ((status = card_send_command(ioaddr, name, Command1, st)))
0645         return status;
0646     PID[1] = st[1] << 8 | st[2];
0647 
0648     if ((status = card_send_command(ioaddr, name, Command2, st)))
0649         return status;
0650     PID[2] = st[1] << 8 | st[2];
0651 
0652     if ((status = card_send_command(ioaddr, name, Command3, st)))
0653         return status;
0654     PID[3] = st[1] << 8 | st[2];
0655 
0656     return sb1000_end_get_set_command(ioaddr, name);
0657 }
0658 
0659 /* set SB1000 PIDs */
0660 static int
0661 sb1000_set_PIDs(const int ioaddr[], const char* name, const short PID[])
0662 {
0663     static const unsigned char Command4[6] = {0x80, 0x2e, 0x00, 0x00, 0x00, 0x00};
0664 
0665     unsigned char st[7];
0666     short p;
0667     int status;
0668     unsigned char Command0[6] = {0x80, 0x31, 0x00, 0x00, 0x00, 0x00};
0669     unsigned char Command1[6] = {0x80, 0x32, 0x00, 0x00, 0x00, 0x00};
0670     unsigned char Command2[6] = {0x80, 0x33, 0x00, 0x00, 0x00, 0x00};
0671     unsigned char Command3[6] = {0x80, 0x34, 0x00, 0x00, 0x00, 0x00};
0672 
0673     udelay(1000);
0674     if ((status = sb1000_start_get_set_command(ioaddr, name)))
0675         return status;
0676 
0677     p = PID[0];
0678     Command0[3] = p & 0xff;
0679     p >>= 8;
0680     Command0[2] = p & 0xff;
0681     if ((status = card_send_command(ioaddr, name, Command0, st)))
0682         return status;
0683 
0684     p = PID[1];
0685     Command1[3] = p & 0xff;
0686     p >>= 8;
0687     Command1[2] = p & 0xff;
0688     if ((status = card_send_command(ioaddr, name, Command1, st)))
0689         return status;
0690 
0691     p = PID[2];
0692     Command2[3] = p & 0xff;
0693     p >>= 8;
0694     Command2[2] = p & 0xff;
0695     if ((status = card_send_command(ioaddr, name, Command2, st)))
0696         return status;
0697 
0698     p = PID[3];
0699     Command3[3] = p & 0xff;
0700     p >>= 8;
0701     Command3[2] = p & 0xff;
0702     if ((status = card_send_command(ioaddr, name, Command3, st)))
0703         return status;
0704 
0705     if ((status = card_send_command(ioaddr, name, Command4, st)))
0706         return status;
0707     return sb1000_end_get_set_command(ioaddr, name);
0708 }
0709 
0710 
0711 static void
0712 sb1000_print_status_buffer(const char* name, unsigned char st[],
0713     unsigned char buffer[], int size)
0714 {
0715     int i, j, k;
0716 
0717     printk(KERN_DEBUG "%s: status: %02x %02x\n", name, st[0], st[1]);
0718     if (buffer[24] == 0x08 && buffer[25] == 0x00 && buffer[26] == 0x45) {
0719         printk(KERN_DEBUG "%s: length: %d protocol: %d from: %d.%d.%d.%d:%d "
0720             "to %d.%d.%d.%d:%d\n", name, buffer[28] << 8 | buffer[29],
0721             buffer[35], buffer[38], buffer[39], buffer[40], buffer[41],
0722             buffer[46] << 8 | buffer[47],
0723             buffer[42], buffer[43], buffer[44], buffer[45],
0724             buffer[48] << 8 | buffer[49]);
0725     } else {
0726         for (i = 0, k = 0; i < (size + 7) / 8; i++) {
0727             printk(KERN_DEBUG "%s: %s", name, i ? "       " : "buffer:");
0728             for (j = 0; j < 8 && k < size; j++, k++)
0729                 printk(" %02x", buffer[k]);
0730             printk("\n");
0731         }
0732     }
0733 }
0734 
0735 /*
0736  * SB1000 commands for frame rx interrupt
0737  */
0738 /* receive a single frame and assemble datagram
0739  * (this is the heart of the interrupt routine)
0740  */
0741 static int
0742 sb1000_rx(struct net_device *dev)
0743 {
0744 
0745 #define FRAMESIZE 184
0746     unsigned char st[2], buffer[FRAMESIZE], session_id, frame_id;
0747     short dlen;
0748     int ioaddr, ns;
0749     unsigned int skbsize;
0750     struct sk_buff *skb;
0751     struct sb1000_private *lp = netdev_priv(dev);
0752     struct net_device_stats *stats = &dev->stats;
0753 
0754     /* SB1000 frame constants */
0755     const int FrameSize = FRAMESIZE;
0756     const int NewDatagramHeaderSkip = 8;
0757     const int NewDatagramHeaderSize = NewDatagramHeaderSkip + 18;
0758     const int NewDatagramDataSize = FrameSize - NewDatagramHeaderSize;
0759     const int ContDatagramHeaderSkip = 7;
0760     const int ContDatagramHeaderSize = ContDatagramHeaderSkip + 1;
0761     const int ContDatagramDataSize = FrameSize - ContDatagramHeaderSize;
0762     const int TrailerSize = 4;
0763 
0764     ioaddr = dev->base_addr;
0765 
0766     insw(ioaddr, (unsigned short*) st, 1);
0767 #ifdef XXXDEBUG
0768 printk("cm0: received: %02x %02x\n", st[0], st[1]);
0769 #endif /* XXXDEBUG */
0770     lp->rx_frames++;
0771 
0772     /* decide if it is a good or bad frame */
0773     for (ns = 0; ns < NPIDS; ns++) {
0774         session_id = lp->rx_session_id[ns];
0775         frame_id = lp->rx_frame_id[ns];
0776         if (st[0] == session_id) {
0777             if (st[1] == frame_id || (!frame_id && (st[1] & 0xf0) == 0x30)) {
0778                 goto good_frame;
0779             } else if ((st[1] & 0xf0) == 0x30 && (st[0] & 0x40)) {
0780                 goto skipped_frame;
0781             } else {
0782                 goto bad_frame;
0783             }
0784         } else if (st[0] == (session_id | 0x40)) {
0785             if ((st[1] & 0xf0) == 0x30) {
0786                 goto skipped_frame;
0787             } else {
0788                 goto bad_frame;
0789             }
0790         }
0791     }
0792     goto bad_frame;
0793 
0794 skipped_frame:
0795     stats->rx_frame_errors++;
0796     skb = lp->rx_skb[ns];
0797     if (sb1000_debug > 1)
0798         printk(KERN_WARNING "%s: missing frame(s): got %02x %02x "
0799             "expecting %02x %02x\n", dev->name, st[0], st[1],
0800             skb ? session_id : session_id | 0x40, frame_id);
0801     if (skb) {
0802         dev_kfree_skb(skb);
0803         skb = NULL;
0804     }
0805 
0806 good_frame:
0807     lp->rx_frame_id[ns] = 0x30 | ((st[1] + 1) & 0x0f);
0808     /* new datagram */
0809     if (st[0] & 0x40) {
0810         /* get data length */
0811         insw(ioaddr, buffer, NewDatagramHeaderSize / 2);
0812 #ifdef XXXDEBUG
0813 printk("cm0: IP identification: %02x%02x  fragment offset: %02x%02x\n", buffer[30], buffer[31], buffer[32], buffer[33]);
0814 #endif /* XXXDEBUG */
0815         if (buffer[0] != NewDatagramHeaderSkip) {
0816             if (sb1000_debug > 1)
0817                 printk(KERN_WARNING "%s: new datagram header skip error: "
0818                     "got %02x expecting %02x\n", dev->name, buffer[0],
0819                     NewDatagramHeaderSkip);
0820             stats->rx_length_errors++;
0821             insw(ioaddr, buffer, NewDatagramDataSize / 2);
0822             goto bad_frame_next;
0823         }
0824         dlen = ((buffer[NewDatagramHeaderSkip + 3] & 0x0f) << 8 |
0825             buffer[NewDatagramHeaderSkip + 4]) - 17;
0826         if (dlen > SB1000_MRU) {
0827             if (sb1000_debug > 1)
0828                 printk(KERN_WARNING "%s: datagram length (%d) greater "
0829                     "than MRU (%d)\n", dev->name, dlen, SB1000_MRU);
0830             stats->rx_length_errors++;
0831             insw(ioaddr, buffer, NewDatagramDataSize / 2);
0832             goto bad_frame_next;
0833         }
0834         lp->rx_dlen[ns] = dlen;
0835         /* compute size to allocate for datagram */
0836         skbsize = dlen + FrameSize;
0837         if ((skb = alloc_skb(skbsize, GFP_ATOMIC)) == NULL) {
0838             if (sb1000_debug > 1)
0839                 printk(KERN_WARNING "%s: can't allocate %d bytes long "
0840                     "skbuff\n", dev->name, skbsize);
0841             stats->rx_dropped++;
0842             insw(ioaddr, buffer, NewDatagramDataSize / 2);
0843             goto dropped_frame;
0844         }
0845         skb->dev = dev;
0846         skb_reset_mac_header(skb);
0847         skb->protocol = (unsigned short) buffer[NewDatagramHeaderSkip + 16];
0848         insw(ioaddr, skb_put(skb, NewDatagramDataSize),
0849             NewDatagramDataSize / 2);
0850         lp->rx_skb[ns] = skb;
0851     } else {
0852         /* continuation of previous datagram */
0853         insw(ioaddr, buffer, ContDatagramHeaderSize / 2);
0854         if (buffer[0] != ContDatagramHeaderSkip) {
0855             if (sb1000_debug > 1)
0856                 printk(KERN_WARNING "%s: cont datagram header skip error: "
0857                     "got %02x expecting %02x\n", dev->name, buffer[0],
0858                     ContDatagramHeaderSkip);
0859             stats->rx_length_errors++;
0860             insw(ioaddr, buffer, ContDatagramDataSize / 2);
0861             goto bad_frame_next;
0862         }
0863         skb = lp->rx_skb[ns];
0864         insw(ioaddr, skb_put(skb, ContDatagramDataSize),
0865             ContDatagramDataSize / 2);
0866         dlen = lp->rx_dlen[ns];
0867     }
0868     if (skb->len < dlen + TrailerSize) {
0869         lp->rx_session_id[ns] &= ~0x40;
0870         return 0;
0871     }
0872 
0873     /* datagram completed: send to upper level */
0874     skb_trim(skb, dlen);
0875     __netif_rx(skb);
0876     stats->rx_bytes+=dlen;
0877     stats->rx_packets++;
0878     lp->rx_skb[ns] = NULL;
0879     lp->rx_session_id[ns] |= 0x40;
0880     return 0;
0881 
0882 bad_frame:
0883     insw(ioaddr, buffer, FrameSize / 2);
0884     if (sb1000_debug > 1)
0885         printk(KERN_WARNING "%s: frame error: got %02x %02x\n",
0886             dev->name, st[0], st[1]);
0887     stats->rx_frame_errors++;
0888 bad_frame_next:
0889     if (sb1000_debug > 2)
0890         sb1000_print_status_buffer(dev->name, st, buffer, FrameSize);
0891 dropped_frame:
0892     stats->rx_errors++;
0893     if (ns < NPIDS) {
0894         if ((skb = lp->rx_skb[ns])) {
0895             dev_kfree_skb(skb);
0896             lp->rx_skb[ns] = NULL;
0897         }
0898         lp->rx_session_id[ns] |= 0x40;
0899     }
0900     return -1;
0901 }
0902 
0903 static void
0904 sb1000_error_dpc(struct net_device *dev)
0905 {
0906     static const unsigned char Command0[6] = {0x80, 0x26, 0x00, 0x00, 0x00, 0x00};
0907 
0908     char *name;
0909     unsigned char st[5];
0910     int ioaddr[2];
0911     struct sb1000_private *lp = netdev_priv(dev);
0912     const int ErrorDpcCounterInitialize = 200;
0913 
0914     ioaddr[0] = dev->base_addr;
0915     /* mem_start holds the second I/O address */
0916     ioaddr[1] = dev->mem_start;
0917     name = dev->name;
0918 
0919     sb1000_wait_for_ready_clear(ioaddr, name);
0920     sb1000_send_command(ioaddr, name, Command0);
0921     sb1000_wait_for_ready(ioaddr, name);
0922     sb1000_read_status(ioaddr, st);
0923     if (st[1] & 0x10)
0924         lp->rx_error_dpc_count = ErrorDpcCounterInitialize;
0925 }
0926 
0927 
0928 /*
0929  * Linux interface functions
0930  */
0931 static int
0932 sb1000_open(struct net_device *dev)
0933 {
0934     char *name;
0935     int ioaddr[2], status;
0936     struct sb1000_private *lp = netdev_priv(dev);
0937     const unsigned short FirmwareVersion[] = {0x01, 0x01};
0938 
0939     ioaddr[0] = dev->base_addr;
0940     /* mem_start holds the second I/O address */
0941     ioaddr[1] = dev->mem_start;
0942     name = dev->name;
0943 
0944     /* initialize sb1000 */
0945     if ((status = sb1000_reset(ioaddr, name)))
0946         return status;
0947     ssleep(1);
0948     if ((status = sb1000_check_CRC(ioaddr, name)))
0949         return status;
0950 
0951     /* initialize private data before board can catch interrupts */
0952     lp->rx_skb[0] = NULL;
0953     lp->rx_skb[1] = NULL;
0954     lp->rx_skb[2] = NULL;
0955     lp->rx_skb[3] = NULL;
0956     lp->rx_dlen[0] = 0;
0957     lp->rx_dlen[1] = 0;
0958     lp->rx_dlen[2] = 0;
0959     lp->rx_dlen[3] = 0;
0960     lp->rx_frames = 0;
0961     lp->rx_error_count = 0;
0962     lp->rx_error_dpc_count = 0;
0963     lp->rx_session_id[0] = 0x50;
0964     lp->rx_session_id[1] = 0x48;
0965     lp->rx_session_id[2] = 0x44;
0966     lp->rx_session_id[3] = 0x42;
0967     lp->rx_frame_id[0] = 0;
0968     lp->rx_frame_id[1] = 0;
0969     lp->rx_frame_id[2] = 0;
0970     lp->rx_frame_id[3] = 0;
0971     if (request_irq(dev->irq, sb1000_interrupt, 0, "sb1000", dev)) {
0972         return -EAGAIN;
0973     }
0974 
0975     if (sb1000_debug > 2)
0976         printk(KERN_DEBUG "%s: Opening, IRQ %d\n", name, dev->irq);
0977 
0978     /* Activate board and check firmware version */
0979     udelay(1000);
0980     if ((status = sb1000_activate(ioaddr, name)))
0981         return status;
0982     udelay(0);
0983     if ((status = sb1000_get_firmware_version(ioaddr, name, version, 0)))
0984         return status;
0985     if (version[0] != FirmwareVersion[0] || version[1] != FirmwareVersion[1])
0986         printk(KERN_WARNING "%s: found firmware version %x.%02x "
0987             "(should be %x.%02x)\n", name, version[0], version[1],
0988             FirmwareVersion[0], FirmwareVersion[1]);
0989 
0990 
0991     netif_start_queue(dev);
0992     return 0;                   /* Always succeed */
0993 }
0994 
0995 static int sb1000_siocdevprivate(struct net_device *dev, struct ifreq *ifr,
0996                  void __user *data, int cmd)
0997 {
0998     char* name;
0999     unsigned char version[2];
1000     short PID[4];
1001     int ioaddr[2], status, frequency;
1002     unsigned int stats[5];
1003     struct sb1000_private *lp = netdev_priv(dev);
1004 
1005     if (!(dev && dev->flags & IFF_UP))
1006         return -ENODEV;
1007 
1008     ioaddr[0] = dev->base_addr;
1009     /* mem_start holds the second I/O address */
1010     ioaddr[1] = dev->mem_start;
1011     name = dev->name;
1012 
1013     switch (cmd) {
1014     case SIOCGCMSTATS:      /* get statistics */
1015         stats[0] = dev->stats.rx_bytes;
1016         stats[1] = lp->rx_frames;
1017         stats[2] = dev->stats.rx_packets;
1018         stats[3] = dev->stats.rx_errors;
1019         stats[4] = dev->stats.rx_dropped;
1020         if (copy_to_user(data, stats, sizeof(stats)))
1021             return -EFAULT;
1022         status = 0;
1023         break;
1024 
1025     case SIOCGCMFIRMWARE:       /* get firmware version */
1026         if ((status = sb1000_get_firmware_version(ioaddr, name, version, 1)))
1027             return status;
1028         if (copy_to_user(data, version, sizeof(version)))
1029             return -EFAULT;
1030         break;
1031 
1032     case SIOCGCMFREQUENCY:      /* get frequency */
1033         if ((status = sb1000_get_frequency(ioaddr, name, &frequency)))
1034             return status;
1035         if (put_user(frequency, (int __user *)data))
1036             return -EFAULT;
1037         break;
1038 
1039     case SIOCSCMFREQUENCY:      /* set frequency */
1040         if (!capable(CAP_NET_ADMIN))
1041             return -EPERM;
1042         if (get_user(frequency, (int __user *)data))
1043             return -EFAULT;
1044         if ((status = sb1000_set_frequency(ioaddr, name, frequency)))
1045             return status;
1046         break;
1047 
1048     case SIOCGCMPIDS:           /* get PIDs */
1049         if ((status = sb1000_get_PIDs(ioaddr, name, PID)))
1050             return status;
1051         if (copy_to_user(data, PID, sizeof(PID)))
1052             return -EFAULT;
1053         break;
1054 
1055     case SIOCSCMPIDS:           /* set PIDs */
1056         if (!capable(CAP_NET_ADMIN))
1057             return -EPERM;
1058         if (copy_from_user(PID, data, sizeof(PID)))
1059             return -EFAULT;
1060         if ((status = sb1000_set_PIDs(ioaddr, name, PID)))
1061             return status;
1062         /* set session_id, frame_id and pkt_type too */
1063         lp->rx_session_id[0] = 0x50 | (PID[0] & 0x0f);
1064         lp->rx_session_id[1] = 0x48;
1065         lp->rx_session_id[2] = 0x44;
1066         lp->rx_session_id[3] = 0x42;
1067         lp->rx_frame_id[0] = 0;
1068         lp->rx_frame_id[1] = 0;
1069         lp->rx_frame_id[2] = 0;
1070         lp->rx_frame_id[3] = 0;
1071         break;
1072 
1073     default:
1074         status = -EINVAL;
1075         break;
1076     }
1077     return status;
1078 }
1079 
1080 /* transmit function: do nothing since SB1000 can't send anything out */
1081 static netdev_tx_t
1082 sb1000_start_xmit(struct sk_buff *skb, struct net_device *dev)
1083 {
1084     printk(KERN_WARNING "%s: trying to transmit!!!\n", dev->name);
1085     /* sb1000 can't xmit datagrams */
1086     dev_kfree_skb(skb);
1087     return NETDEV_TX_OK;
1088 }
1089 
1090 /* SB1000 interrupt handler. */
1091 static irqreturn_t sb1000_interrupt(int irq, void *dev_id)
1092 {
1093     static const unsigned char Command0[6] = {0x80, 0x2c, 0x00, 0x00, 0x00, 0x00};
1094     static const unsigned char Command1[6] = {0x80, 0x2e, 0x00, 0x00, 0x00, 0x00};
1095 
1096     char *name;
1097     unsigned char st;
1098     int ioaddr[2];
1099     struct net_device *dev = dev_id;
1100     struct sb1000_private *lp = netdev_priv(dev);
1101 
1102     const int MaxRxErrorCount = 6;
1103 
1104     ioaddr[0] = dev->base_addr;
1105     /* mem_start holds the second I/O address */
1106     ioaddr[1] = dev->mem_start;
1107     name = dev->name;
1108 
1109     /* is it a good interrupt? */
1110     st = inb(ioaddr[1] + 6);
1111     if (!(st & 0x08 && st & 0x20)) {
1112         return IRQ_NONE;
1113     }
1114 
1115     if (sb1000_debug > 3)
1116         printk(KERN_DEBUG "%s: entering interrupt\n", dev->name);
1117 
1118     st = inb(ioaddr[0] + 7);
1119     if (sb1000_rx(dev))
1120         lp->rx_error_count++;
1121 #ifdef SB1000_DELAY
1122     udelay(SB1000_DELAY);
1123 #endif /* SB1000_DELAY */
1124     sb1000_issue_read_command(ioaddr, name);
1125     if (st & 0x01) {
1126         sb1000_error_dpc(dev);
1127         sb1000_issue_read_command(ioaddr, name);
1128     }
1129     if (lp->rx_error_dpc_count && !(--lp->rx_error_dpc_count)) {
1130         sb1000_wait_for_ready_clear(ioaddr, name);
1131         sb1000_send_command(ioaddr, name, Command0);
1132         sb1000_wait_for_ready(ioaddr, name);
1133         sb1000_issue_read_command(ioaddr, name);
1134     }
1135     if (lp->rx_error_count >= MaxRxErrorCount) {
1136         sb1000_wait_for_ready_clear(ioaddr, name);
1137         sb1000_send_command(ioaddr, name, Command1);
1138         sb1000_wait_for_ready(ioaddr, name);
1139         sb1000_issue_read_command(ioaddr, name);
1140         lp->rx_error_count = 0;
1141     }
1142 
1143     return IRQ_HANDLED;
1144 }
1145 
1146 static int sb1000_close(struct net_device *dev)
1147 {
1148     int i;
1149     int ioaddr[2];
1150     struct sb1000_private *lp = netdev_priv(dev);
1151 
1152     if (sb1000_debug > 2)
1153         printk(KERN_DEBUG "%s: Shutting down sb1000.\n", dev->name);
1154 
1155     netif_stop_queue(dev);
1156 
1157     ioaddr[0] = dev->base_addr;
1158     /* mem_start holds the second I/O address */
1159     ioaddr[1] = dev->mem_start;
1160 
1161     free_irq(dev->irq, dev);
1162     /* If we don't do this, we can't re-insmod it later. */
1163     release_region(ioaddr[1], SB1000_IO_EXTENT);
1164     release_region(ioaddr[0], SB1000_IO_EXTENT);
1165 
1166     /* free rx_skb's if needed */
1167     for (i=0; i<4; i++) {
1168         if (lp->rx_skb[i]) {
1169             dev_kfree_skb(lp->rx_skb[i]);
1170         }
1171     }
1172     return 0;
1173 }
1174 
1175 MODULE_AUTHOR("Franco Venturi <fventuri@mediaone.net>");
1176 MODULE_DESCRIPTION("General Instruments SB1000 driver");
1177 MODULE_LICENSE("GPL");
1178 
1179 module_pnp_driver(sb1000_driver);