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