1/*** ltpc.c -- a driver for the LocalTalk PC card. 2 * 3 * Copyright (c) 1995,1996 Bradford W. Johnson <johns393@maroon.tc.umn.edu> 4 * 5 * This software may be used and distributed according to the terms 6 * of the GNU General Public License, incorporated herein by reference. 7 * 8 * This is ALPHA code at best. It may not work for you. It may 9 * damage your equipment. It may damage your relations with other 10 * users of your network. Use it at your own risk! 11 * 12 * Based in part on: 13 * skeleton.c by Donald Becker 14 * dummy.c by Nick Holloway and Alan Cox 15 * loopback.c by Ross Biro, Fred van Kampen, Donald Becker 16 * the netatalk source code (UMICH) 17 * lots of work on the card... 18 * 19 * I do not have access to the (proprietary) SDK that goes with the card. 20 * If you do, I don't want to know about it, and you can probably write 21 * a better driver yourself anyway. This does mean that the pieces that 22 * talk to the card are guesswork on my part, so use at your own risk! 23 * 24 * This is my first try at writing Linux networking code, and is also 25 * guesswork. Again, use at your own risk! (Although on this part, I'd 26 * welcome suggestions) 27 * 28 * This is a loadable kernel module which seems to work at my site 29 * consisting of a 1.2.13 linux box running netatalk 1.3.3, and with 30 * the kernel support from 1.3.3b2 including patches routing.patch 31 * and ddp.disappears.from.chooser. In order to run it, you will need 32 * to patch ddp.c and aarp.c in the kernel, but only a little... 33 * 34 * I'm fairly confident that while this is arguably badly written, the 35 * problems that people experience will be "higher level", that is, with 36 * complications in the netatalk code. The driver itself doesn't do 37 * anything terribly complicated -- it pretends to be an ether device 38 * as far as netatalk is concerned, strips the DDP data out of the ether 39 * frame and builds a LLAP packet to send out the card. In the other 40 * direction, it receives LLAP frames from the card and builds a fake 41 * ether packet that it then tosses up to the networking code. You can 42 * argue (correctly) that this is an ugly way to do things, but it 43 * requires a minimal amount of fooling with the code in ddp.c and aarp.c. 44 * 45 * The card will do a lot more than is used here -- I *think* it has the 46 * layers up through ATP. Even if you knew how that part works (which I 47 * don't) it would be a big job to carve up the kernel ddp code to insert 48 * things at a higher level, and probably a bad idea... 49 * 50 * There are a number of other cards that do LocalTalk on the PC. If 51 * nobody finds any insurmountable (at the netatalk level) problems 52 * here, this driver should encourage people to put some work into the 53 * other cards (some of which I gather are still commercially available) 54 * and also to put hooks for LocalTalk into the official ddp code. 55 * 56 * I welcome comments and suggestions. This is my first try at Linux 57 * networking stuff, and there are probably lots of things that I did 58 * suboptimally. 59 * 60 ***/ 61 62/*** 63 * 64 * $Log: ltpc.c,v $ 65 * Revision 1.1.1.1 2007/08/03 18:52:46 rnuti 66 * Importing Linux MIPS Kernel 2.6.22 67 * 68 * Revision 1.1.2.1 2000/03/01 05:35:07 jgarzik 69 * at and tr cleanup 70 * 71 * Revision 1.8 1997/01/28 05:44:54 bradford 72 * Clean up for non-module a little. 73 * Hacked about a bit to clean things up - Alan Cox 74 * Probably broken it from the origina 1.8 75 * 76 77 * 1998/11/09: David Huggins-Daines <dhd@debian.org> 78 * Cleaned up the initialization code to use the standard autoirq methods, 79 and to probe for things in the standard order of i/o, irq, dma. This 80 removes the "reset the reset" hack, because I couldn't figure out an 81 easy way to get the card to trigger an interrupt after it. 82 * Added support for passing configuration parameters on the kernel command 83 line and through insmod 84 * Changed the device name from "ltalk0" to "lt0", both to conform with the 85 other localtalk driver, and to clear up the inconsistency between the 86 module and the non-module versions of the driver :-) 87 * Added a bunch of comments (I was going to make some enums for the state 88 codes and the register offsets, but I'm still not sure exactly what their 89 semantics are) 90 * Don't poll anymore in interrupt-driven mode 91 * It seems to work as a module now (as of 2.1.127), but I don't think 92 I'm responsible for that... 93 94 * 95 * Revision 1.7 1996/12/12 03:42:33 bradford 96 * DMA alloc cribbed from 3c505.c. 97 * 98 * Revision 1.6 1996/12/12 03:18:58 bradford 99 * Added virt_to_bus; works in 2.1.13. 100 * 101 * Revision 1.5 1996/12/12 03:13:22 root 102 * xmitQel initialization -- think through better though. 103 * 104 * Revision 1.4 1996/06/18 14:55:55 root 105 * Change names to ltpc. Tabs. Took a shot at dma alloc, 106 * although more needs to be done eventually. 107 * 108 * Revision 1.3 1996/05/22 14:59:39 root 109 * Change dev->open, dev->close to track dummy.c in 1.99.(around 7) 110 * 111 * Revision 1.2 1996/05/22 14:58:24 root 112 * Change tabs mostly. 113 * 114 * Revision 1.1 1996/04/23 04:45:09 root 115 * Initial revision 116 * 117 * Revision 0.16 1996/03/05 15:59:56 root 118 * Change ARPHRD_LOCALTLK definition to the "real" one. 119 * 120 * Revision 0.15 1996/03/05 06:28:30 root 121 * Changes for kernel 1.3.70. Still need a few patches to kernel, but 122 * it's getting closer. 123 * 124 * Revision 0.14 1996/02/25 17:38:32 root 125 * More cleanups. Removed query to card on get_stats. 126 * 127 * Revision 0.13 1996/02/21 16:27:40 root 128 * Refix debug_print_skb. Fix mac.raw gotcha that appeared in 1.3.65. 129 * Clean up receive code a little. 130 * 131 * Revision 0.12 1996/02/19 16:34:53 root 132 * Fix debug_print_skb. Kludge outgoing snet to 0 when using startup 133 * range. Change debug to mask: 1 for verbose, 2 for higher level stuff 134 * including packet printing, 4 for lower level (card i/o) stuff. 135 * 136 * Revision 0.11 1996/02/12 15:53:38 root 137 * Added router sends (requires new aarp.c patch) 138 * 139 * Revision 0.10 1996/02/11 00:19:35 root 140 * Change source LTALK_LOGGING debug switch to insmod ... debug=2. 141 * 142 * Revision 0.9 1996/02/10 23:59:35 root 143 * Fixed those fixes for 1.2 -- DANGER! The at.h that comes with netatalk 144 * has a *different* definition of struct sockaddr_at than the Linux kernel 145 * does. This is an "insidious and invidious" bug... 146 * (Actually the preceding comment is false -- it's the atalk.h in the 147 * ancient atalk-0.06 that's the problem) 148 * 149 * Revision 0.8 1996/02/10 19:09:00 root 150 * Merge 1.3 changes. Tested OK under 1.3.60. 151 * 152 * Revision 0.7 1996/02/10 17:56:56 root 153 * Added debug=1 parameter on insmod for debugging prints. Tried 154 * to fix timer unload on rmmod, but I don't think that's the problem. 155 * 156 * Revision 0.6 1995/12/31 19:01:09 root 157 * Clean up rmmod, irq comments per feedback from Corin Anderson (Thanks Corey!) 158 * Clean up initial probing -- sometimes the card wakes up latched in reset. 159 * 160 * Revision 0.5 1995/12/22 06:03:44 root 161 * Added comments in front and cleaned up a bit. 162 * This version sent out to people. 163 * 164 * Revision 0.4 1995/12/18 03:46:44 root 165 * Return shortDDP to longDDP fake to 0/0. Added command structs. 166 * 167 ***/ 168 169/* ltpc jumpers are: 170* 171* Interrupts -- set at most one. If none are set, the driver uses 172* polled mode. Because the card was developed in the XT era, the 173* original documentation refers to IRQ2. Since you'll be running 174* this on an AT (or later) class machine, that really means IRQ9. 175* 176* SW1 IRQ 4 177* SW2 IRQ 3 178* SW3 IRQ 9 (2 in original card documentation only applies to XT) 179* 180* 181* DMA -- choose DMA 1 or 3, and set both corresponding switches. 182* 183* SW4 DMA 3 184* SW5 DMA 1 185* SW6 DMA 3 186* SW7 DMA 1 187* 188* 189* I/O address -- choose one. 190* 191* SW8 220 / 240 192*/ 193 194/* To have some stuff logged, do 195* insmod ltpc.o debug=1 196* 197* For a whole bunch of stuff, use higher numbers. 198* 199* The default is 0, i.e. no messages except for the probe results. 200*/ 201 202/* insmod-tweakable variables */ 203static int debug; 204#define DEBUG_VERBOSE 1 205#define DEBUG_UPPER 2 206#define DEBUG_LOWER 4 207 208static int io; 209static int irq; 210static int dma; 211 212#include <linux/module.h> 213#include <linux/kernel.h> 214#include <linux/types.h> 215#include <linux/fcntl.h> 216#include <linux/interrupt.h> 217#include <linux/ptrace.h> 218#include <linux/ioport.h> 219#include <linux/spinlock.h> 220#include <linux/in.h> 221#include <linux/slab.h> 222#include <linux/string.h> 223#include <linux/errno.h> 224#include <linux/init.h> 225#include <linux/netdevice.h> 226#include <linux/etherdevice.h> 227#include <linux/skbuff.h> 228#include <linux/if_arp.h> 229#include <linux/if_ltalk.h> 230#include <linux/delay.h> 231#include <linux/timer.h> 232#include <linux/atalk.h> 233#include <linux/bitops.h> 234 235#include <asm/system.h> 236#include <asm/dma.h> 237#include <asm/io.h> 238 239/* our stuff */ 240#include "ltpc.h" 241 242static DEFINE_SPINLOCK(txqueue_lock); 243static DEFINE_SPINLOCK(mbox_lock); 244 245/* function prototypes */ 246static int do_read(struct net_device *dev, void *cbuf, int cbuflen, 247 void *dbuf, int dbuflen); 248static int sendup_buffer (struct net_device *dev); 249 250/* Dma Memory related stuff, cribbed directly from 3c505.c */ 251 252static unsigned long dma_mem_alloc(int size) 253{ 254 int order = get_order(size); 255 256 return __get_dma_pages(GFP_KERNEL, order); 257} 258 259/* DMA data buffer, DMA command buffer */ 260static unsigned char *ltdmabuf; 261static unsigned char *ltdmacbuf; 262 263/* private struct, holds our appletalk address */ 264 265struct ltpc_private 266{ 267 struct net_device_stats stats; 268 struct atalk_addr my_addr; 269}; 270 271/* transmit queue element struct */ 272 273struct xmitQel { 274 struct xmitQel *next; 275 /* command buffer */ 276 unsigned char *cbuf; 277 short cbuflen; 278 /* data buffer */ 279 unsigned char *dbuf; 280 short dbuflen; 281 unsigned char QWrite; /* read or write data */ 282 unsigned char mailbox; 283}; 284 285/* the transmit queue itself */ 286 287static struct xmitQel *xmQhd, *xmQtl; 288 289static void enQ(struct xmitQel *qel) 290{ 291 unsigned long flags; 292 qel->next = NULL; 293 294 spin_lock_irqsave(&txqueue_lock, flags); 295 if (xmQtl) { 296 xmQtl->next = qel; 297 } else { 298 xmQhd = qel; 299 } 300 xmQtl = qel; 301 spin_unlock_irqrestore(&txqueue_lock, flags); 302 303 if (debug & DEBUG_LOWER) 304 printk("enqueued a 0x%02x command\n",qel->cbuf[0]); 305} 306 307static struct xmitQel *deQ(void) 308{ 309 unsigned long flags; 310 int i; 311 struct xmitQel *qel=NULL; 312 313 spin_lock_irqsave(&txqueue_lock, flags); 314 if (xmQhd) { 315 qel = xmQhd; 316 xmQhd = qel->next; 317 if(!xmQhd) xmQtl = NULL; 318 } 319 spin_unlock_irqrestore(&txqueue_lock, flags); 320 321 if ((debug & DEBUG_LOWER) && qel) { 322 int n; 323 printk(KERN_DEBUG "ltpc: dequeued command "); 324 n = qel->cbuflen; 325 if (n>100) n=100; 326 for(i=0;i<n;i++) printk("%02x ",qel->cbuf[i]); 327 printk("\n"); 328 } 329 330 return qel; 331} 332 333/* and... the queue elements we'll be using */ 334static struct xmitQel qels[16]; 335 336/* and their corresponding mailboxes */ 337static unsigned char mailbox[16]; 338static unsigned char mboxinuse[16] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; 339 340static int wait_timeout(struct net_device *dev, int c) 341{ 342 /* returns true if it stayed c */ 343 /* this uses base+6, but it's ok */ 344 int i; 345 346 /* twenty second or so total */ 347 348 for(i=0;i<200000;i++) { 349 if ( c != inb_p(dev->base_addr+6) ) return 0; 350 udelay(100); 351 } 352 return 1; /* timed out */ 353} 354 355/* get the first free mailbox */ 356 357static int getmbox(void) 358{ 359 unsigned long flags; 360 int i; 361 362 spin_lock_irqsave(&mbox_lock, flags); 363 for(i=1;i<16;i++) if(!mboxinuse[i]) { 364 mboxinuse[i]=1; 365 spin_unlock_irqrestore(&mbox_lock, flags); 366 return i; 367 } 368 spin_unlock_irqrestore(&mbox_lock, flags); 369 return 0; 370} 371 372/* read a command from the card */ 373static void handlefc(struct net_device *dev) 374{ 375 /* called *only* from idle, non-reentrant */ 376 int dma = dev->dma; 377 int base = dev->base_addr; 378 unsigned long flags; 379 380 381 flags=claim_dma_lock(); 382 disable_dma(dma); 383 clear_dma_ff(dma); 384 set_dma_mode(dma,DMA_MODE_READ); 385 set_dma_addr(dma,virt_to_bus(ltdmacbuf)); 386 set_dma_count(dma,50); 387 enable_dma(dma); 388 release_dma_lock(flags); 389 390 inb_p(base+3); 391 inb_p(base+2); 392 393 if ( wait_timeout(dev,0xfc) ) printk("timed out in handlefc\n"); 394} 395 396/* read data from the card */ 397static void handlefd(struct net_device *dev) 398{ 399 int dma = dev->dma; 400 int base = dev->base_addr; 401 unsigned long flags; 402 403 flags=claim_dma_lock(); 404 disable_dma(dma); 405 clear_dma_ff(dma); 406 set_dma_mode(dma,DMA_MODE_READ); 407 set_dma_addr(dma,virt_to_bus(ltdmabuf)); 408 set_dma_count(dma,800); 409 enable_dma(dma); 410 release_dma_lock(flags); 411 412 inb_p(base+3); 413 inb_p(base+2); 414 415 if ( wait_timeout(dev,0xfd) ) printk("timed out in handlefd\n"); 416 sendup_buffer(dev); 417} 418 419static void handlewrite(struct net_device *dev) 420{ 421 /* called *only* from idle, non-reentrant */ 422 /* on entry, 0xfb and ltdmabuf holds data */ 423 int dma = dev->dma; 424 int base = dev->base_addr; 425 unsigned long flags; 426 427 flags=claim_dma_lock(); 428 disable_dma(dma); 429 clear_dma_ff(dma); 430 set_dma_mode(dma,DMA_MODE_WRITE); 431 set_dma_addr(dma,virt_to_bus(ltdmabuf)); 432 set_dma_count(dma,800); 433 enable_dma(dma); 434 release_dma_lock(flags); 435 436 inb_p(base+3); 437 inb_p(base+2); 438 439 if ( wait_timeout(dev,0xfb) ) { 440 flags=claim_dma_lock(); 441 printk("timed out in handlewrite, dma res %d\n", 442 get_dma_residue(dev->dma) ); 443 release_dma_lock(flags); 444 } 445} 446 447static void handleread(struct net_device *dev) 448{ 449 /* on entry, 0xfb */ 450 /* on exit, ltdmabuf holds data */ 451 int dma = dev->dma; 452 int base = dev->base_addr; 453 unsigned long flags; 454 455 456 flags=claim_dma_lock(); 457 disable_dma(dma); 458 clear_dma_ff(dma); 459 set_dma_mode(dma,DMA_MODE_READ); 460 set_dma_addr(dma,virt_to_bus(ltdmabuf)); 461 set_dma_count(dma,800); 462 enable_dma(dma); 463 release_dma_lock(flags); 464 465 inb_p(base+3); 466 inb_p(base+2); 467 if ( wait_timeout(dev,0xfb) ) printk("timed out in handleread\n"); 468} 469 470static void handlecommand(struct net_device *dev) 471{ 472 /* on entry, 0xfa and ltdmacbuf holds command */ 473 int dma = dev->dma; 474 int base = dev->base_addr; 475 unsigned long flags; 476 477 flags=claim_dma_lock(); 478 disable_dma(dma); 479 clear_dma_ff(dma); 480 set_dma_mode(dma,DMA_MODE_WRITE); 481 set_dma_addr(dma,virt_to_bus(ltdmacbuf)); 482 set_dma_count(dma,50); 483 enable_dma(dma); 484 release_dma_lock(flags); 485 inb_p(base+3); 486 inb_p(base+2); 487 if ( wait_timeout(dev,0xfa) ) printk("timed out in handlecommand\n"); 488} 489 490/* ready made command for getting the result from the card */ 491static unsigned char rescbuf[2] = {LT_GETRESULT,0}; 492static unsigned char resdbuf[2]; 493 494static int QInIdle; 495 496/* idle expects to be called with the IRQ line high -- either because of 497 * an interrupt, or because the line is tri-stated 498 */ 499 500static void idle(struct net_device *dev) 501{ 502 unsigned long flags; 503 int state; 504 struct xmitQel *q = NULL; 505 int oops; 506 int i; 507 int base = dev->base_addr; 508 509 spin_lock_irqsave(&txqueue_lock, flags); 510 if(QInIdle) { 511 spin_unlock_irqrestore(&txqueue_lock, flags); 512 return; 513 } 514 QInIdle = 1; 515 spin_unlock_irqrestore(&txqueue_lock, flags); 516 517 /* this tri-states the IRQ line */ 518 (void) inb_p(base+6); 519 520 oops = 100; 521 522loop: 523 if (0>oops--) { 524 printk("idle: looped too many times\n"); 525 goto done; 526 } 527 528 state = inb_p(base+6); 529 if (state != inb_p(base+6)) goto loop; 530 531 switch(state) { 532 case 0xfc: 533 /* incoming command */ 534 if (debug & DEBUG_LOWER) printk("idle: fc\n"); 535 handlefc(dev); 536 break; 537 case 0xfd: 538 /* incoming data */ 539 if(debug & DEBUG_LOWER) printk("idle: fd\n"); 540 handlefd(dev); 541 break; 542 case 0xf9: 543 /* result ready */ 544 if (debug & DEBUG_LOWER) printk("idle: f9\n"); 545 if(!mboxinuse[0]) { 546 mboxinuse[0] = 1; 547 qels[0].cbuf = rescbuf; 548 qels[0].cbuflen = 2; 549 qels[0].dbuf = resdbuf; 550 qels[0].dbuflen = 2; 551 qels[0].QWrite = 0; 552 qels[0].mailbox = 0; 553 enQ(&qels[0]); 554 } 555 inb_p(dev->base_addr+1); 556 inb_p(dev->base_addr+0); 557 if( wait_timeout(dev,0xf9) ) 558 printk("timed out idle f9\n"); 559 break; 560 case 0xf8: 561 /* ?? */ 562 if (xmQhd) { 563 inb_p(dev->base_addr+1); 564 inb_p(dev->base_addr+0); 565 if(wait_timeout(dev,0xf8) ) 566 printk("timed out idle f8\n"); 567 } else { 568 goto done; 569 } 570 break; 571 case 0xfa: 572 /* waiting for command */ 573 if(debug & DEBUG_LOWER) printk("idle: fa\n"); 574 if (xmQhd) { 575 q=deQ(); 576 memcpy(ltdmacbuf,q->cbuf,q->cbuflen); 577 ltdmacbuf[1] = q->mailbox; 578 if (debug>1) { 579 int n; 580 printk("ltpc: sent command "); 581 n = q->cbuflen; 582 if (n>100) n=100; 583 for(i=0;i<n;i++) 584 printk("%02x ",ltdmacbuf[i]); 585 printk("\n"); 586 } 587 handlecommand(dev); 588 if(0xfa==inb_p(base+6)) { 589 /* we timed out, so return */ 590 goto done; 591 } 592 } else { 593 /* we don't seem to have a command */ 594 if (!mboxinuse[0]) { 595 mboxinuse[0] = 1; 596 qels[0].cbuf = rescbuf; 597 qels[0].cbuflen = 2; 598 qels[0].dbuf = resdbuf; 599 qels[0].dbuflen = 2; 600 qels[0].QWrite = 0; 601 qels[0].mailbox = 0; 602 enQ(&qels[0]); 603 } else { 604 printk("trouble: response command already queued\n"); 605 goto done; 606 } 607 } 608 break; 609 case 0Xfb: 610 /* data transfer ready */ 611 if(debug & DEBUG_LOWER) printk("idle: fb\n"); 612 if(q->QWrite) { 613 memcpy(ltdmabuf,q->dbuf,q->dbuflen); 614 handlewrite(dev); 615 } else { 616 handleread(dev); 617 /* non-zero mailbox numbers are for 618 commmands, 0 is for GETRESULT 619 requests */ 620 if(q->mailbox) { 621 memcpy(q->dbuf,ltdmabuf,q->dbuflen); 622 } else { 623 /* this was a result */ 624 mailbox[ 0x0f & ltdmabuf[0] ] = ltdmabuf[1]; 625 mboxinuse[0]=0; 626 } 627 } 628 break; 629 } 630 goto loop; 631 632done: 633 QInIdle=0; 634 635 /* now set the interrupts back as appropriate */ 636 /* the first read takes it out of tri-state (but still high) */ 637 /* the second resets it */ 638 /* note that after this point, any read of base+6 will 639 trigger an interrupt */ 640 641 if (dev->irq) { 642 inb_p(base+7); 643 inb_p(base+7); 644 } 645 return; 646} 647 648 649static int do_write(struct net_device *dev, void *cbuf, int cbuflen, 650 void *dbuf, int dbuflen) 651{ 652 653 int i = getmbox(); 654 int ret; 655 656 if(i) { 657 qels[i].cbuf = (unsigned char *) cbuf; 658 qels[i].cbuflen = cbuflen; 659 qels[i].dbuf = (unsigned char *) dbuf; 660 qels[i].dbuflen = dbuflen; 661 qels[i].QWrite = 1; 662 qels[i].mailbox = i; /* this should be initted rather */ 663 enQ(&qels[i]); 664 idle(dev); 665 ret = mailbox[i]; 666 mboxinuse[i]=0; 667 return ret; 668 } 669 printk("ltpc: could not allocate mbox\n"); 670 return -1; 671} 672 673static int do_read(struct net_device *dev, void *cbuf, int cbuflen, 674 void *dbuf, int dbuflen) 675{ 676 677 int i = getmbox(); 678 int ret; 679 680 if(i) { 681 qels[i].cbuf = (unsigned char *) cbuf; 682 qels[i].cbuflen = cbuflen; 683 qels[i].dbuf = (unsigned char *) dbuf; 684 qels[i].dbuflen = dbuflen; 685 qels[i].QWrite = 0; 686 qels[i].mailbox = i; /* this should be initted rather */ 687 enQ(&qels[i]); 688 idle(dev); 689 ret = mailbox[i]; 690 mboxinuse[i]=0; 691 return ret; 692 } 693 printk("ltpc: could not allocate mbox\n"); 694 return -1; 695} 696 697/* end of idle handlers -- what should be seen is do_read, do_write */ 698 699static struct timer_list ltpc_timer; 700 701static int ltpc_xmit(struct sk_buff *skb, struct net_device *dev); 702static struct net_device_stats *ltpc_get_stats(struct net_device *dev); 703 704static int read_30 ( struct net_device *dev) 705{ 706 lt_command c; 707 c.getflags.command = LT_GETFLAGS; 708 return do_read(dev, &c, sizeof(c.getflags),&c,0); 709} 710 711static int set_30 (struct net_device *dev,int x) 712{ 713 lt_command c; 714 c.setflags.command = LT_SETFLAGS; 715 c.setflags.flags = x; 716 return do_write(dev, &c, sizeof(c.setflags),&c,0); 717} 718 719/* LLAP to DDP translation */ 720 721static int sendup_buffer (struct net_device *dev) 722{ 723 /* on entry, command is in ltdmacbuf, data in ltdmabuf */ 724 /* called from idle, non-reentrant */ 725 726 int dnode, snode, llaptype, len; 727 int sklen; 728 struct sk_buff *skb; 729 struct net_device_stats *stats = &((struct ltpc_private *)dev->priv)->stats; 730 struct lt_rcvlap *ltc = (struct lt_rcvlap *) ltdmacbuf; 731 732 if (ltc->command != LT_RCVLAP) { 733 printk("unknown command 0x%02x from ltpc card\n",ltc->command); 734 return(-1); 735 } 736 dnode = ltc->dnode; 737 snode = ltc->snode; 738 llaptype = ltc->laptype; 739 len = ltc->length; 740 741 sklen = len; 742 if (llaptype == 1) 743 sklen += 8; /* correct for short ddp */ 744 if(sklen > 800) { 745 printk(KERN_INFO "%s: nonsense length in ltpc command 0x14: 0x%08x\n", 746 dev->name,sklen); 747 return -1; 748 } 749 750 if ( (llaptype==0) || (llaptype>2) ) { 751 printk(KERN_INFO "%s: unknown LLAP type: %d\n",dev->name,llaptype); 752 return -1; 753 } 754 755 756 skb = dev_alloc_skb(3+sklen); 757 if (skb == NULL) 758 { 759 printk("%s: dropping packet due to memory squeeze.\n", 760 dev->name); 761 return -1; 762 } 763 skb->dev = dev; 764 765 if (sklen > len) 766 skb_reserve(skb,8); 767 skb_put(skb,len+3); 768 skb->protocol = htons(ETH_P_LOCALTALK); 769 /* add LLAP header */ 770 skb->data[0] = dnode; 771 skb->data[1] = snode; 772 skb->data[2] = llaptype; 773 skb_reset_mac_header(skb); /* save pointer to llap header */ 774 skb_pull(skb,3); 775 776 /* copy ddp(s,e)hdr + contents */ 777 skb_copy_to_linear_data(skb, ltdmabuf, len); 778 779 skb_reset_transport_header(skb); 780 781 stats->rx_packets++; 782 stats->rx_bytes+=skb->len; 783 784 /* toss it onwards */ 785 netif_rx(skb); 786 dev->last_rx = jiffies; 787 return 0; 788} 789 790/* the handler for the board interrupt */ 791 792static irqreturn_t 793ltpc_interrupt(int irq, void *dev_id) 794{ 795 struct net_device *dev = dev_id; 796 797 if (dev==NULL) { 798 printk("ltpc_interrupt: unknown device.\n"); 799 return IRQ_NONE; 800 } 801 802 inb_p(dev->base_addr+6); /* disable further interrupts from board */ 803 804 idle(dev); /* handle whatever is coming in */ 805 806 /* idle re-enables interrupts from board */ 807 808 return IRQ_HANDLED; 809} 810 811/*** 812 * 813 * The ioctls that the driver responds to are: 814 * 815 * SIOCSIFADDR -- do probe using the passed node hint. 816 * SIOCGIFADDR -- return net, node. 817 * 818 * some of this stuff should be done elsewhere. 819 * 820 ***/ 821 822static int ltpc_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd) 823{ 824 struct sockaddr_at *sa = (struct sockaddr_at *) &ifr->ifr_addr; 825 /* we'll keep the localtalk node address in dev->pa_addr */ 826 struct atalk_addr *aa = &((struct ltpc_private *)dev->priv)->my_addr; 827 struct lt_init c; 828 int ltflags; 829 830 if(debug & DEBUG_VERBOSE) printk("ltpc_ioctl called\n"); 831 832 switch(cmd) { 833 case SIOCSIFADDR: 834 835 aa->s_net = sa->sat_addr.s_net; 836 837 /* this does the probe and returns the node addr */ 838 c.command = LT_INIT; 839 c.hint = sa->sat_addr.s_node; 840 841 aa->s_node = do_read(dev,&c,sizeof(c),&c,0); 842 843 /* get all llap frames raw */ 844 ltflags = read_30(dev); 845 ltflags |= LT_FLAG_ALLLAP; 846 set_30 (dev,ltflags); 847 848 dev->broadcast[0] = 0xFF; 849 dev->dev_addr[0] = aa->s_node; 850 851 dev->addr_len=1; 852 853 return 0; 854 855 case SIOCGIFADDR: 856 857 sa->sat_addr.s_net = aa->s_net; 858 sa->sat_addr.s_node = aa->s_node; 859 860 return 0; 861 862 default: 863 return -EINVAL; 864 } 865} 866 867static void set_multicast_list(struct net_device *dev) 868{ 869 /* This needs to be present to keep netatalk happy. */ 870 /* Actually netatalk needs fixing! */ 871} 872 873static int ltpc_hard_header (struct sk_buff *skb, struct net_device *dev, 874 unsigned short type, void *daddr, void *saddr, unsigned len) 875{ 876 if(debug & DEBUG_VERBOSE) 877 printk("ltpc_hard_header called for device %s\n", 878 dev->name); 879 return 0; 880} 881 882static int ltpc_poll_counter; 883 884static void ltpc_poll(unsigned long l) 885{ 886 struct net_device *dev = (struct net_device *) l; 887 888 del_timer(<pc_timer); 889 890 if(debug & DEBUG_VERBOSE) { 891 if (!ltpc_poll_counter) { 892 ltpc_poll_counter = 50; 893 printk("ltpc poll is alive\n"); 894 } 895 ltpc_poll_counter--; 896 } 897 898 if (!dev) 899 return; /* we've been downed */ 900 901 /* poll 20 times per second */ 902 idle(dev); 903 ltpc_timer.expires = jiffies + HZ/20; 904 905 add_timer(<pc_timer); 906} 907 908/* DDP to LLAP translation */ 909 910static int ltpc_xmit(struct sk_buff *skb, struct net_device *dev) 911{ 912 /* in kernel 1.3.xx, on entry skb->data points to ddp header, 913 * and skb->len is the length of the ddp data + ddp header 914 */ 915 916 struct net_device_stats *stats = &((struct ltpc_private *)dev->priv)->stats; 917 918 int i; 919 struct lt_sendlap cbuf; 920 unsigned char *hdr; 921 922 cbuf.command = LT_SENDLAP; 923 cbuf.dnode = skb->data[0]; 924 cbuf.laptype = skb->data[2]; 925 skb_pull(skb,3); /* skip past LLAP header */ 926 cbuf.length = skb->len; /* this is host order */ 927 skb_reset_transport_header(skb); 928 929 if(debug & DEBUG_UPPER) { 930 printk("command "); 931 for(i=0;i<6;i++) 932 printk("%02x ",((unsigned char *)&cbuf)[i]); 933 printk("\n"); 934 } 935 936 hdr = skb_transport_header(skb); 937 do_write(dev, &cbuf, sizeof(cbuf), hdr, skb->len); 938 939 if(debug & DEBUG_UPPER) { 940 printk("sent %d ddp bytes\n",skb->len); 941 for (i = 0; i < skb->len; i++) 942 printk("%02x ", hdr[i]); 943 printk("\n"); 944 } 945 946 stats->tx_packets++; 947 stats->tx_bytes+=skb->len; 948 949 dev_kfree_skb(skb); 950 return 0; 951} 952 953static struct net_device_stats *ltpc_get_stats(struct net_device *dev) 954{ 955 struct net_device_stats *stats = &((struct ltpc_private *) dev->priv)->stats; 956 return stats; 957} 958 959/* initialization stuff */ 960 961static int __init ltpc_probe_dma(int base, int dma) 962{ 963 int want = (dma == 3) ? 2 : (dma == 1) ? 1 : 3; 964 unsigned long timeout; 965 unsigned long f; 966 967 if (want & 1) { 968 if (request_dma(1,"ltpc")) { 969 want &= ~1; 970 } else { 971 f=claim_dma_lock(); 972 disable_dma(1); 973 clear_dma_ff(1); 974 set_dma_mode(1,DMA_MODE_WRITE); 975 set_dma_addr(1,virt_to_bus(ltdmabuf)); 976 set_dma_count(1,sizeof(struct lt_mem)); 977 enable_dma(1); 978 release_dma_lock(f); 979 } 980 } 981 if (want & 2) { 982 if (request_dma(3,"ltpc")) { 983 want &= ~2; 984 } else { 985 f=claim_dma_lock(); 986 disable_dma(3); 987 clear_dma_ff(3); 988 set_dma_mode(3,DMA_MODE_WRITE); 989 set_dma_addr(3,virt_to_bus(ltdmabuf)); 990 set_dma_count(3,sizeof(struct lt_mem)); 991 enable_dma(3); 992 release_dma_lock(f); 993 } 994 } 995 /* set up request */ 996 997 998 ltdmabuf[0] = LT_READMEM; 999 ltdmabuf[1] = 1; /* mailbox */ 1000 ltdmabuf[2] = 0; ltdmabuf[3] = 0; /* address */ 1001 ltdmabuf[4] = 0; ltdmabuf[5] = 1; /* read 0x0100 bytes */ 1002 ltdmabuf[6] = 0; /* dunno if this is necessary */ 1003 1004 inb_p(io+1); 1005 inb_p(io+0); 1006 timeout = jiffies+100*HZ/100; 1007 while(time_before(jiffies, timeout)) { 1008 if ( 0xfa == inb_p(io+6) ) break; 1009 } 1010 1011 inb_p(io+3); 1012 inb_p(io+2); 1013 while(time_before(jiffies, timeout)) { 1014 if ( 0xfb == inb_p(io+6) ) break; 1015 } 1016 1017 /* release the other dma channel (if we opened both of them) */ 1018 1019 if ((want & 2) && (get_dma_residue(3)==sizeof(struct lt_mem))) { 1020 want &= ~2; 1021 free_dma(3); 1022 } 1023 1024 if ((want & 1) && (get_dma_residue(1)==sizeof(struct lt_mem))) { 1025 want &= ~1; 1026 free_dma(1); 1027 } 1028 1029 if (!want) 1030 return 0; 1031 1032 return (want & 2) ? 3 : 1; 1033} 1034 1035struct net_device * __init ltpc_probe(void) 1036{ 1037 struct net_device *dev; 1038 int err = -ENOMEM; 1039 int x=0,y=0; 1040 int autoirq; 1041 unsigned long f; 1042 unsigned long timeout; 1043 1044 dev = alloc_ltalkdev(sizeof(struct ltpc_private)); 1045 if (!dev) 1046 goto out; 1047 1048 SET_MODULE_OWNER(dev); 1049 1050 /* probe for the I/O port address */ 1051 1052 if (io != 0x240 && request_region(0x220,8,"ltpc")) { 1053 x = inb_p(0x220+6); 1054 if ( (x!=0xff) && (x>=0xf0) ) { 1055 io = 0x220; 1056 goto got_port; 1057 } 1058 release_region(0x220,8); 1059 } 1060 if (io != 0x220 && request_region(0x240,8,"ltpc")) { 1061 y = inb_p(0x240+6); 1062 if ( (y!=0xff) && (y>=0xf0) ){ 1063 io = 0x240; 1064 goto got_port; 1065 } 1066 release_region(0x240,8); 1067 } 1068 1069 /* give up in despair */ 1070 printk(KERN_ERR "LocalTalk card not found; 220 = %02x, 240 = %02x.\n", x,y); 1071 err = -ENODEV; 1072 goto out1; 1073 1074 got_port: 1075 /* probe for the IRQ line */ 1076 if (irq < 2) { 1077 unsigned long irq_mask; 1078 1079 irq_mask = probe_irq_on(); 1080 /* reset the interrupt line */ 1081 inb_p(io+7); 1082 inb_p(io+7); 1083 /* trigger an interrupt (I hope) */ 1084 inb_p(io+6); 1085 mdelay(2); 1086 autoirq = probe_irq_off(irq_mask); 1087 1088 if (autoirq == 0) { 1089 printk(KERN_ERR "ltpc: probe at %#x failed to detect IRQ line.\n", io); 1090 } else { 1091 irq = autoirq; 1092 } 1093 } 1094 1095 /* allocate a DMA buffer */ 1096 ltdmabuf = (unsigned char *) dma_mem_alloc(1000); 1097 if (!ltdmabuf) { 1098 printk(KERN_ERR "ltpc: mem alloc failed\n"); 1099 err = -ENOMEM; 1100 goto out2; 1101 } 1102 1103 ltdmacbuf = <dmabuf[800]; 1104 1105 if(debug & DEBUG_VERBOSE) { 1106 printk("ltdmabuf pointer %08lx\n",(unsigned long) ltdmabuf); 1107 } 1108 1109 /* reset the card */ 1110 1111 inb_p(io+1); 1112 inb_p(io+3); 1113 1114 msleep(20); 1115 1116 inb_p(io+0); 1117 inb_p(io+2); 1118 inb_p(io+7); /* clear reset */ 1119 inb_p(io+4); 1120 inb_p(io+5); 1121 inb_p(io+5); /* enable dma */ 1122 inb_p(io+6); /* tri-state interrupt line */ 1123 1124 ssleep(1); 1125 1126 /* now, figure out which dma channel we're using, unless it's 1127 already been specified */ 1128 /* well, 0 is a legal DMA channel, but the LTPC card doesn't 1129 use it... */ 1130 dma = ltpc_probe_dma(io, dma); 1131 if (!dma) { /* no dma channel */ 1132 printk(KERN_ERR "No DMA channel found on ltpc card.\n"); 1133 err = -ENODEV; 1134 goto out3; 1135 } 1136 1137 /* print out friendly message */ 1138 if(irq) 1139 printk(KERN_INFO "Apple/Farallon LocalTalk-PC card at %03x, IR%d, DMA%d.\n",io,irq,dma); 1140 else 1141 printk(KERN_INFO "Apple/Farallon LocalTalk-PC card at %03x, DMA%d. Using polled mode.\n",io,dma); 1142 1143 /* Fill in the fields of the device structure with ethernet-generic values. */ 1144 dev->hard_start_xmit = ltpc_xmit; 1145 dev->hard_header = ltpc_hard_header; 1146 dev->get_stats = ltpc_get_stats; 1147 1148 /* add the ltpc-specific things */ 1149 dev->do_ioctl = <pc_ioctl; 1150 1151 dev->set_multicast_list = &set_multicast_list; 1152 dev->mc_list = NULL; 1153 dev->base_addr = io; 1154 dev->irq = irq; 1155 dev->dma = dma; 1156 1157 /* the card will want to send a result at this point */ 1158 /* (I think... leaving out this part makes the kernel crash, 1159 so I put it back in...) */ 1160 1161 f=claim_dma_lock(); 1162 disable_dma(dma); 1163 clear_dma_ff(dma); 1164 set_dma_mode(dma,DMA_MODE_READ); 1165 set_dma_addr(dma,virt_to_bus(ltdmabuf)); 1166 set_dma_count(dma,0x100); 1167 enable_dma(dma); 1168 release_dma_lock(f); 1169 1170 (void) inb_p(io+3); 1171 (void) inb_p(io+2); 1172 timeout = jiffies+100*HZ/100; 1173 1174 while(time_before(jiffies, timeout)) { 1175 if( 0xf9 == inb_p(io+6)) 1176 break; 1177 schedule(); 1178 } 1179 1180 if(debug & DEBUG_VERBOSE) { 1181 printk("setting up timer and irq\n"); 1182 } 1183 1184 /* grab it and don't let go :-) */ 1185 if (irq && request_irq( irq, <pc_interrupt, 0, "ltpc", dev) >= 0) 1186 { 1187 (void) inb_p(io+7); /* enable interrupts from board */ 1188 (void) inb_p(io+7); /* and reset irq line */ 1189 } else { 1190 if( irq ) 1191 printk(KERN_ERR "ltpc: IRQ already in use, using polled mode.\n"); 1192 dev->irq = 0; 1193 /* polled mode -- 20 times per second */ 1194 /* this is really, really slow... should it poll more often? */ 1195 init_timer(<pc_timer); 1196 ltpc_timer.function=ltpc_poll; 1197 ltpc_timer.data = (unsigned long) dev; 1198 1199 ltpc_timer.expires = jiffies + HZ/20; 1200 add_timer(<pc_timer); 1201 } 1202 err = register_netdev(dev); 1203 if (err) 1204 goto out4; 1205 1206 return NULL; 1207out4: 1208 del_timer_sync(<pc_timer); 1209 if (dev->irq) 1210 free_irq(dev->irq, dev); 1211out3: 1212 free_pages((unsigned long)ltdmabuf, get_order(1000)); 1213out2: 1214 release_region(io, 8); 1215out1: 1216 free_netdev(dev); 1217out: 1218 return ERR_PTR(err); 1219} 1220 1221#ifndef MODULE 1222/* handles "ltpc=io,irq,dma" kernel command lines */ 1223static int __init ltpc_setup(char *str) 1224{ 1225 int ints[5]; 1226 1227 str = get_options(str, ARRAY_SIZE(ints), ints); 1228 1229 if (ints[0] == 0) { 1230 if (str && !strncmp(str, "auto", 4)) { 1231 /* do nothing :-) */ 1232 } 1233 else { 1234 /* usage message */ 1235 printk (KERN_ERR 1236 "ltpc: usage: ltpc=auto|iobase[,irq[,dma]]\n"); 1237 return 0; 1238 } 1239 } else { 1240 io = ints[1]; 1241 if (ints[0] > 1) { 1242 irq = ints[2]; 1243 } 1244 if (ints[0] > 2) { 1245 dma = ints[3]; 1246 } 1247 /* ignore any other paramters */ 1248 } 1249 return 1; 1250} 1251 1252__setup("ltpc=", ltpc_setup); 1253#endif /* MODULE */ 1254 1255static struct net_device *dev_ltpc; 1256 1257#ifdef MODULE 1258 1259MODULE_LICENSE("GPL"); 1260module_param(debug, int, 0); 1261module_param(io, int, 0); 1262module_param(irq, int, 0); 1263module_param(dma, int, 0); 1264 1265 1266int __init init_module(void) 1267{ 1268 if(io == 0) 1269 printk(KERN_NOTICE 1270 "ltpc: Autoprobing is not recommended for modules\n"); 1271 1272 dev_ltpc = ltpc_probe(); 1273 if (IS_ERR(dev_ltpc)) 1274 return PTR_ERR(dev_ltpc); 1275 return 0; 1276} 1277#endif 1278 1279static void __exit ltpc_cleanup(void) 1280{ 1281 1282 if(debug & DEBUG_VERBOSE) printk("unregister_netdev\n"); 1283 unregister_netdev(dev_ltpc); 1284 1285 ltpc_timer.data = 0; /* signal the poll routine that we're done */ 1286 1287 del_timer_sync(<pc_timer); 1288 1289 if(debug & DEBUG_VERBOSE) printk("freeing irq\n"); 1290 1291 if (dev_ltpc->irq) 1292 free_irq(dev_ltpc->irq, dev_ltpc); 1293 1294 if(debug & DEBUG_VERBOSE) printk("freeing dma\n"); 1295 1296 if (dev_ltpc->dma) 1297 free_dma(dev_ltpc->dma); 1298 1299 if(debug & DEBUG_VERBOSE) printk("freeing ioaddr\n"); 1300 1301 if (dev_ltpc->base_addr) 1302 release_region(dev_ltpc->base_addr,8); 1303 1304 free_netdev(dev_ltpc); 1305 1306 if(debug & DEBUG_VERBOSE) printk("free_pages\n"); 1307 1308 free_pages( (unsigned long) ltdmabuf, get_order(1000)); 1309 1310 if(debug & DEBUG_VERBOSE) printk("returning from cleanup_module\n"); 1311} 1312 1313module_exit(ltpc_cleanup); 1314