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