ltpc.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278
  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 struct net_device *ltpc_timer_dev;
  620. static netdev_tx_t ltpc_xmit(struct sk_buff *skb, struct net_device *dev);
  621. static int read_30 ( struct net_device *dev)
  622. {
  623. lt_command c;
  624. c.getflags.command = LT_GETFLAGS;
  625. return do_read(dev, &c, sizeof(c.getflags),&c,0);
  626. }
  627. static int set_30 (struct net_device *dev,int x)
  628. {
  629. lt_command c;
  630. c.setflags.command = LT_SETFLAGS;
  631. c.setflags.flags = x;
  632. return do_write(dev, &c, sizeof(c.setflags),&c,0);
  633. }
  634. /* LLAP to DDP translation */
  635. static int sendup_buffer (struct net_device *dev)
  636. {
  637. /* on entry, command is in ltdmacbuf, data in ltdmabuf */
  638. /* called from idle, non-reentrant */
  639. int dnode, snode, llaptype, len;
  640. int sklen;
  641. struct sk_buff *skb;
  642. struct lt_rcvlap *ltc = (struct lt_rcvlap *) ltdmacbuf;
  643. if (ltc->command != LT_RCVLAP) {
  644. printk("unknown command 0x%02x from ltpc card\n",ltc->command);
  645. return -1;
  646. }
  647. dnode = ltc->dnode;
  648. snode = ltc->snode;
  649. llaptype = ltc->laptype;
  650. len = ltc->length;
  651. sklen = len;
  652. if (llaptype == 1)
  653. sklen += 8; /* correct for short ddp */
  654. if(sklen > 800) {
  655. printk(KERN_INFO "%s: nonsense length in ltpc command 0x14: 0x%08x\n",
  656. dev->name,sklen);
  657. return -1;
  658. }
  659. if ( (llaptype==0) || (llaptype>2) ) {
  660. printk(KERN_INFO "%s: unknown LLAP type: %d\n",dev->name,llaptype);
  661. return -1;
  662. }
  663. skb = dev_alloc_skb(3+sklen);
  664. if (skb == NULL)
  665. {
  666. printk("%s: dropping packet due to memory squeeze.\n",
  667. dev->name);
  668. return -1;
  669. }
  670. skb->dev = dev;
  671. if (sklen > len)
  672. skb_reserve(skb,8);
  673. skb_put(skb,len+3);
  674. skb->protocol = htons(ETH_P_LOCALTALK);
  675. /* add LLAP header */
  676. skb->data[0] = dnode;
  677. skb->data[1] = snode;
  678. skb->data[2] = llaptype;
  679. skb_reset_mac_header(skb); /* save pointer to llap header */
  680. skb_pull(skb,3);
  681. /* copy ddp(s,e)hdr + contents */
  682. skb_copy_to_linear_data(skb, ltdmabuf, len);
  683. skb_reset_transport_header(skb);
  684. dev->stats.rx_packets++;
  685. dev->stats.rx_bytes += skb->len;
  686. /* toss it onwards */
  687. netif_rx(skb);
  688. return 0;
  689. }
  690. /* the handler for the board interrupt */
  691. static irqreturn_t
  692. ltpc_interrupt(int irq, void *dev_id)
  693. {
  694. struct net_device *dev = dev_id;
  695. if (dev==NULL) {
  696. printk("ltpc_interrupt: unknown device.\n");
  697. return IRQ_NONE;
  698. }
  699. inb_p(dev->base_addr+6); /* disable further interrupts from board */
  700. idle(dev); /* handle whatever is coming in */
  701. /* idle re-enables interrupts from board */
  702. return IRQ_HANDLED;
  703. }
  704. /***
  705. *
  706. * The ioctls that the driver responds to are:
  707. *
  708. * SIOCSIFADDR -- do probe using the passed node hint.
  709. * SIOCGIFADDR -- return net, node.
  710. *
  711. * some of this stuff should be done elsewhere.
  712. *
  713. ***/
  714. static int ltpc_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
  715. {
  716. struct sockaddr_at *sa = (struct sockaddr_at *) &ifr->ifr_addr;
  717. /* we'll keep the localtalk node address in dev->pa_addr */
  718. struct ltpc_private *ltpc_priv = netdev_priv(dev);
  719. struct atalk_addr *aa = &ltpc_priv->my_addr;
  720. struct lt_init c;
  721. int ltflags;
  722. if(debug & DEBUG_VERBOSE) printk("ltpc_ioctl called\n");
  723. switch(cmd) {
  724. case SIOCSIFADDR:
  725. aa->s_net = sa->sat_addr.s_net;
  726. /* this does the probe and returns the node addr */
  727. c.command = LT_INIT;
  728. c.hint = sa->sat_addr.s_node;
  729. aa->s_node = do_read(dev,&c,sizeof(c),&c,0);
  730. /* get all llap frames raw */
  731. ltflags = read_30(dev);
  732. ltflags |= LT_FLAG_ALLLAP;
  733. set_30 (dev,ltflags);
  734. dev->broadcast[0] = 0xFF;
  735. dev->dev_addr[0] = aa->s_node;
  736. dev->addr_len=1;
  737. return 0;
  738. case SIOCGIFADDR:
  739. sa->sat_addr.s_net = aa->s_net;
  740. sa->sat_addr.s_node = aa->s_node;
  741. return 0;
  742. default:
  743. return -EINVAL;
  744. }
  745. }
  746. static void set_multicast_list(struct net_device *dev)
  747. {
  748. /* This needs to be present to keep netatalk happy. */
  749. /* Actually netatalk needs fixing! */
  750. }
  751. static int ltpc_poll_counter;
  752. static void ltpc_poll(struct timer_list *unused)
  753. {
  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. /* poll 20 times per second */
  763. idle(ltpc_timer_dev);
  764. ltpc_timer.expires = jiffies + HZ/20;
  765. add_timer(&ltpc_timer);
  766. }
  767. /* DDP to LLAP translation */
  768. static netdev_tx_t ltpc_xmit(struct sk_buff *skb, struct net_device *dev)
  769. {
  770. /* in kernel 1.3.xx, on entry skb->data points to ddp header,
  771. * and skb->len is the length of the ddp data + ddp header
  772. */
  773. int i;
  774. struct lt_sendlap cbuf;
  775. unsigned char *hdr;
  776. cbuf.command = LT_SENDLAP;
  777. cbuf.dnode = skb->data[0];
  778. cbuf.laptype = skb->data[2];
  779. skb_pull(skb,3); /* skip past LLAP header */
  780. cbuf.length = skb->len; /* this is host order */
  781. skb_reset_transport_header(skb);
  782. if(debug & DEBUG_UPPER) {
  783. printk("command ");
  784. for(i=0;i<6;i++)
  785. printk("%02x ",((unsigned char *)&cbuf)[i]);
  786. printk("\n");
  787. }
  788. hdr = skb_transport_header(skb);
  789. do_write(dev, &cbuf, sizeof(cbuf), hdr, skb->len);
  790. if(debug & DEBUG_UPPER) {
  791. printk("sent %d ddp bytes\n",skb->len);
  792. for (i = 0; i < skb->len; i++)
  793. printk("%02x ", hdr[i]);
  794. printk("\n");
  795. }
  796. dev->stats.tx_packets++;
  797. dev->stats.tx_bytes += skb->len;
  798. dev_kfree_skb(skb);
  799. return NETDEV_TX_OK;
  800. }
  801. /* initialization stuff */
  802. static int __init ltpc_probe_dma(int base, int dma)
  803. {
  804. int want = (dma == 3) ? 2 : (dma == 1) ? 1 : 3;
  805. unsigned long timeout;
  806. unsigned long f;
  807. if (want & 1) {
  808. if (request_dma(1,"ltpc")) {
  809. want &= ~1;
  810. } else {
  811. f=claim_dma_lock();
  812. disable_dma(1);
  813. clear_dma_ff(1);
  814. set_dma_mode(1,DMA_MODE_WRITE);
  815. set_dma_addr(1,virt_to_bus(ltdmabuf));
  816. set_dma_count(1,sizeof(struct lt_mem));
  817. enable_dma(1);
  818. release_dma_lock(f);
  819. }
  820. }
  821. if (want & 2) {
  822. if (request_dma(3,"ltpc")) {
  823. want &= ~2;
  824. } else {
  825. f=claim_dma_lock();
  826. disable_dma(3);
  827. clear_dma_ff(3);
  828. set_dma_mode(3,DMA_MODE_WRITE);
  829. set_dma_addr(3,virt_to_bus(ltdmabuf));
  830. set_dma_count(3,sizeof(struct lt_mem));
  831. enable_dma(3);
  832. release_dma_lock(f);
  833. }
  834. }
  835. /* set up request */
  836. /* FIXME -- do timings better! */
  837. ltdmabuf[0] = LT_READMEM;
  838. ltdmabuf[1] = 1; /* mailbox */
  839. ltdmabuf[2] = 0; ltdmabuf[3] = 0; /* address */
  840. ltdmabuf[4] = 0; ltdmabuf[5] = 1; /* read 0x0100 bytes */
  841. ltdmabuf[6] = 0; /* dunno if this is necessary */
  842. inb_p(io+1);
  843. inb_p(io+0);
  844. timeout = jiffies+100*HZ/100;
  845. while(time_before(jiffies, timeout)) {
  846. if ( 0xfa == inb_p(io+6) ) break;
  847. }
  848. inb_p(io+3);
  849. inb_p(io+2);
  850. while(time_before(jiffies, timeout)) {
  851. if ( 0xfb == inb_p(io+6) ) break;
  852. }
  853. /* release the other dma channel (if we opened both of them) */
  854. if ((want & 2) && (get_dma_residue(3)==sizeof(struct lt_mem))) {
  855. want &= ~2;
  856. free_dma(3);
  857. }
  858. if ((want & 1) && (get_dma_residue(1)==sizeof(struct lt_mem))) {
  859. want &= ~1;
  860. free_dma(1);
  861. }
  862. if (!want)
  863. return 0;
  864. return (want & 2) ? 3 : 1;
  865. }
  866. static const struct net_device_ops ltpc_netdev = {
  867. .ndo_start_xmit = ltpc_xmit,
  868. .ndo_do_ioctl = ltpc_ioctl,
  869. .ndo_set_rx_mode = set_multicast_list,
  870. };
  871. struct net_device * __init ltpc_probe(void)
  872. {
  873. struct net_device *dev;
  874. int err = -ENOMEM;
  875. int x=0,y=0;
  876. int autoirq;
  877. unsigned long f;
  878. unsigned long timeout;
  879. dev = alloc_ltalkdev(sizeof(struct ltpc_private));
  880. if (!dev)
  881. goto out;
  882. /* probe for the I/O port address */
  883. if (io != 0x240 && request_region(0x220,8,"ltpc")) {
  884. x = inb_p(0x220+6);
  885. if ( (x!=0xff) && (x>=0xf0) ) {
  886. io = 0x220;
  887. goto got_port;
  888. }
  889. release_region(0x220,8);
  890. }
  891. if (io != 0x220 && request_region(0x240,8,"ltpc")) {
  892. y = inb_p(0x240+6);
  893. if ( (y!=0xff) && (y>=0xf0) ){
  894. io = 0x240;
  895. goto got_port;
  896. }
  897. release_region(0x240,8);
  898. }
  899. /* give up in despair */
  900. printk(KERN_ERR "LocalTalk card not found; 220 = %02x, 240 = %02x.\n", x,y);
  901. err = -ENODEV;
  902. goto out1;
  903. got_port:
  904. /* probe for the IRQ line */
  905. if (irq < 2) {
  906. unsigned long irq_mask;
  907. irq_mask = probe_irq_on();
  908. /* reset the interrupt line */
  909. inb_p(io+7);
  910. inb_p(io+7);
  911. /* trigger an interrupt (I hope) */
  912. inb_p(io+6);
  913. mdelay(2);
  914. autoirq = probe_irq_off(irq_mask);
  915. if (autoirq == 0) {
  916. printk(KERN_ERR "ltpc: probe at %#x failed to detect IRQ line.\n", io);
  917. } else {
  918. irq = autoirq;
  919. }
  920. }
  921. /* allocate a DMA buffer */
  922. ltdmabuf = (unsigned char *) dma_mem_alloc(1000);
  923. if (!ltdmabuf) {
  924. printk(KERN_ERR "ltpc: mem alloc failed\n");
  925. err = -ENOMEM;
  926. goto out2;
  927. }
  928. ltdmacbuf = &ltdmabuf[800];
  929. if(debug & DEBUG_VERBOSE) {
  930. printk("ltdmabuf pointer %08lx\n",(unsigned long) ltdmabuf);
  931. }
  932. /* reset the card */
  933. inb_p(io+1);
  934. inb_p(io+3);
  935. msleep(20);
  936. inb_p(io+0);
  937. inb_p(io+2);
  938. inb_p(io+7); /* clear reset */
  939. inb_p(io+4);
  940. inb_p(io+5);
  941. inb_p(io+5); /* enable dma */
  942. inb_p(io+6); /* tri-state interrupt line */
  943. ssleep(1);
  944. /* now, figure out which dma channel we're using, unless it's
  945. already been specified */
  946. /* well, 0 is a legal DMA channel, but the LTPC card doesn't
  947. use it... */
  948. dma = ltpc_probe_dma(io, dma);
  949. if (!dma) { /* no dma channel */
  950. printk(KERN_ERR "No DMA channel found on ltpc card.\n");
  951. err = -ENODEV;
  952. goto out3;
  953. }
  954. /* print out friendly message */
  955. if(irq)
  956. printk(KERN_INFO "Apple/Farallon LocalTalk-PC card at %03x, IR%d, DMA%d.\n",io,irq,dma);
  957. else
  958. printk(KERN_INFO "Apple/Farallon LocalTalk-PC card at %03x, DMA%d. Using polled mode.\n",io,dma);
  959. dev->netdev_ops = &ltpc_netdev;
  960. dev->base_addr = io;
  961. dev->irq = irq;
  962. dev->dma = dma;
  963. /* the card will want to send a result at this point */
  964. /* (I think... leaving out this part makes the kernel crash,
  965. so I put it back in...) */
  966. f=claim_dma_lock();
  967. disable_dma(dma);
  968. clear_dma_ff(dma);
  969. set_dma_mode(dma,DMA_MODE_READ);
  970. set_dma_addr(dma,virt_to_bus(ltdmabuf));
  971. set_dma_count(dma,0x100);
  972. enable_dma(dma);
  973. release_dma_lock(f);
  974. (void) inb_p(io+3);
  975. (void) inb_p(io+2);
  976. timeout = jiffies+100*HZ/100;
  977. while(time_before(jiffies, timeout)) {
  978. if( 0xf9 == inb_p(io+6))
  979. break;
  980. schedule();
  981. }
  982. if(debug & DEBUG_VERBOSE) {
  983. printk("setting up timer and irq\n");
  984. }
  985. /* grab it and don't let go :-) */
  986. if (irq && request_irq( irq, ltpc_interrupt, 0, "ltpc", dev) >= 0)
  987. {
  988. (void) inb_p(io+7); /* enable interrupts from board */
  989. (void) inb_p(io+7); /* and reset irq line */
  990. } else {
  991. if( irq )
  992. printk(KERN_ERR "ltpc: IRQ already in use, using polled mode.\n");
  993. dev->irq = 0;
  994. /* polled mode -- 20 times per second */
  995. /* this is really, really slow... should it poll more often? */
  996. ltpc_timer_dev = dev;
  997. timer_setup(&ltpc_timer, ltpc_poll, 0);
  998. ltpc_timer.expires = jiffies + HZ/20;
  999. add_timer(&ltpc_timer);
  1000. }
  1001. err = register_netdev(dev);
  1002. if (err)
  1003. goto out4;
  1004. return NULL;
  1005. out4:
  1006. del_timer_sync(&ltpc_timer);
  1007. if (dev->irq)
  1008. free_irq(dev->irq, dev);
  1009. out3:
  1010. free_pages((unsigned long)ltdmabuf, get_order(1000));
  1011. out2:
  1012. release_region(io, 8);
  1013. out1:
  1014. free_netdev(dev);
  1015. out:
  1016. return ERR_PTR(err);
  1017. }
  1018. #ifndef MODULE
  1019. /* handles "ltpc=io,irq,dma" kernel command lines */
  1020. static int __init ltpc_setup(char *str)
  1021. {
  1022. int ints[5];
  1023. str = get_options(str, ARRAY_SIZE(ints), ints);
  1024. if (ints[0] == 0) {
  1025. if (str && !strncmp(str, "auto", 4)) {
  1026. /* do nothing :-) */
  1027. }
  1028. else {
  1029. /* usage message */
  1030. printk (KERN_ERR
  1031. "ltpc: usage: ltpc=auto|iobase[,irq[,dma]]\n");
  1032. return 0;
  1033. }
  1034. } else {
  1035. io = ints[1];
  1036. if (ints[0] > 1) {
  1037. irq = ints[2];
  1038. }
  1039. if (ints[0] > 2) {
  1040. dma = ints[3];
  1041. }
  1042. /* ignore any other parameters */
  1043. }
  1044. return 1;
  1045. }
  1046. __setup("ltpc=", ltpc_setup);
  1047. #endif /* MODULE */
  1048. static struct net_device *dev_ltpc;
  1049. #ifdef MODULE
  1050. MODULE_LICENSE("GPL");
  1051. module_param(debug, int, 0);
  1052. module_param_hw(io, int, ioport, 0);
  1053. module_param_hw(irq, int, irq, 0);
  1054. module_param_hw(dma, int, dma, 0);
  1055. static int __init ltpc_module_init(void)
  1056. {
  1057. if(io == 0)
  1058. printk(KERN_NOTICE
  1059. "ltpc: Autoprobing is not recommended for modules\n");
  1060. dev_ltpc = ltpc_probe();
  1061. return PTR_ERR_OR_ZERO(dev_ltpc);
  1062. }
  1063. module_init(ltpc_module_init);
  1064. #endif
  1065. static void __exit ltpc_cleanup(void)
  1066. {
  1067. if(debug & DEBUG_VERBOSE) printk("unregister_netdev\n");
  1068. unregister_netdev(dev_ltpc);
  1069. del_timer_sync(&ltpc_timer);
  1070. if(debug & DEBUG_VERBOSE) printk("freeing irq\n");
  1071. if (dev_ltpc->irq)
  1072. free_irq(dev_ltpc->irq, dev_ltpc);
  1073. if(debug & DEBUG_VERBOSE) printk("freeing dma\n");
  1074. if (dev_ltpc->dma)
  1075. free_dma(dev_ltpc->dma);
  1076. if(debug & DEBUG_VERBOSE) printk("freeing ioaddr\n");
  1077. if (dev_ltpc->base_addr)
  1078. release_region(dev_ltpc->base_addr,8);
  1079. free_netdev(dev_ltpc);
  1080. if(debug & DEBUG_VERBOSE) printk("free_pages\n");
  1081. free_pages( (unsigned long) ltdmabuf, get_order(1000));
  1082. if(debug & DEBUG_VERBOSE) printk("returning from cleanup_module\n");
  1083. }
  1084. module_exit(ltpc_cleanup);