ltpc.c 31 KB

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