Merge from vendor branch NTPD:
[dragonfly.git] / sys / dev / netif / rdp / if_rdp.c
1 /*
2  * Copyright 1998, Joerg Wunsch
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice unmodified, this list of conditions, and the following
10  *    disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  *
27  * $FreeBSD: src/sys/i386/isa/if_rdp.c,v 1.6.2.2 2000/07/17 21:24:32 archie Exp $
28  * $DragonFly: src/sys/dev/netif/rdp/if_rdp.c,v 1.14 2005/02/19 22:42:55 joerg Exp $
29  */
30
31 /*
32  * Device driver for RealTek RTL 8002 (`REDP') based pocket-ethernet
33  * adapters, hooked up to a printer port.  `rdp' is a shorthand for
34  * REDP since some tools like netstat work best if the interface name
35  * has no more than three letters.
36  *
37  * Driver configuration flags so far:
38  *   flags 0x1 -- assume 74S288 EEPROM (default 94C46)
39  *   flags 0x2 -- use `slow' mode (mode 3 of the packet driver, default 0)
40  *
41  * Maybe this driver will some day also work with the successor, RTL
42  * 8012 (`AREDP'), which is unfortunately not fully register-
43  * compatible with the 8002.  The 8012 offers support for faster
44  * transfer modi like bidirectional SPP and EPP, 64 K x 4 buffer
45  * memory as opposed to 16 K x 4 for the 8002, a multicast filter, and
46  * a builtin multiplexer that allows chaining a printer behind the
47  * ethernet adapter.
48  *
49  * About the only documentation i've been able to find about the RTL
50  * 8002 was the packet driver source code at ftp.realtek.com.tw, so
51  * this driver is somewhat based on the way the packet driver handles
52  * the chip.  The exact author of the packet driver is unknown, the
53  * only name that i could find in the source was someone called Chiu,
54  * supposedly an employee of RealTek.  So credits to them for that
55  * piece of code which has proven valuable to me.
56  *
57  * Later on, Leo kuo <leo@realtek.com.tw> has been very helpful to me
58  * by sending me a readable (PDF) file documenting the RTL 8012, which
59  * helped me to also understand the 8002, as well as by providing me
60  * with the source code of the 8012 packet driver that i haven't been
61  * able to find on the FTP site.  A big Thanks! goes here to RealTek
62  * for this kind of service.
63  */
64
65 #include "use_rdp.h"
66
67 #include <sys/param.h>
68 #include <sys/systm.h>
69 #include <sys/conf.h>
70 #include <sys/sockio.h>
71 #include <sys/malloc.h>
72 #include <sys/mbuf.h>
73 #include <sys/socket.h>
74 #include <sys/syslog.h>
75 #include <sys/linker_set.h>
76 #include <sys/module.h>
77 #include <sys/bus.h>
78
79 #include <net/ethernet.h>
80 #include <net/if.h>
81 #include <net/ifq_var.h>
82 #include <net/if_arp.h>
83 #include <net/if_dl.h>
84 #include <net/if_mib.h>
85
86 #ifdef INET
87 #include <netinet/in.h>
88 #include <netinet/if_ether.h>
89 #endif
90
91 #ifdef NS
92 #include <netns/ns.h>
93 #include <netns/ns_if.h>
94 #endif
95
96 #include <net/bpf.h>
97
98 #include <machine/clock.h>
99 #include <machine/md_var.h>
100
101 #include <bus/isa/isavar.h>
102 #include <bus/isa/i386/isa_device.h>
103 #include <i386/isa/icu.h>
104 #include "if_rdpreg.h"
105 #include <i386/isa/intr_machdep.h>
106
107 #define IOCTL_CMD_T u_long
108
109 /*
110  * Debug levels (ORed together):
111  *  != 0 - general (bad packets etc.)
112  *  2 - debug EEPROM IO
113  *  4 - debug interrupt status
114  */
115 #undef DEBUG
116 #define DEBUG 0
117
118 /*
119  * rdp_softc: per interface info and status
120  */
121 struct rdp_softc {
122         struct arpcom arpcom;   /*
123                                  * Ethernet common, always goes first so
124                                  * a rdp_softc * can be cast into an
125                                  * arpcom * or into an ifnet *.
126                                  */
127
128         /*
129          * local stuff, somewhat sorted by memory alignment class
130          */
131         u_short baseaddr;       /* IO port address */
132         u_short txsize;         /* tx size for next (buffered) packet,
133                                  * there's only one additional packet
134                                  * we can buffer, thus a single variable
135                                  * ought to be enough */
136         int txbusy;             /* tx is transmitting */
137         int txbuffered;         /* # of packets in tx buffer */
138         int slow;               /* use lpt_control to send data */
139         u_char irqenbit;        /* mirror of current Ctrl_IRQEN */
140         /*
141          * type of parameter EEPROM; device flags 0x1 selects 74S288
142          */
143         enum {
144                 EEPROM_93C46, EEPROM_74S288 /* or 82S123 */
145         } eeprom;
146 };
147
148 DECLARE_DUMMY_MODULE(if_rdp);
149
150 static struct rdp_softc rdp_softc[NRDP];
151
152 /*
153  * Since there's no fixed location in the EEPROM about where to find
154  * the ethernet hardware address, we drop a table of valid OUIs here,
155  * and search through the EEPROM until we find a possible valid
156  * Ethernet address.  Only the first 16 bits of all possible OUIs are
157  * recorded in the table (as obtained from
158  * http://standards.ieee.org/regauth/oui/oui.txt).
159  */
160
161 static u_short allowed_ouis[] = {
162         0x0000, 0x0001, 0x0002, 0x0004, 0x0005, 0x0006, 0x0007,
163         0x0008, 0x0010, 0x001C, 0x0020, 0x0040, 0x0050, 0x0060,
164         0x0070, 0x0080, 0x0090, 0x009D, 0x00A0, 0x00AA, 0x00BB,
165         0x00C0, 0x00CF, 0x00DD, 0x00E0, 0x00E6, 0x0207, 0x021C,
166         0x0260, 0x0270, 0x029D, 0x02AA, 0x02BB, 0x02C0, 0x02CF,
167         0x02E6, 0x040A, 0x04E0, 0x0800, 0x08BB, 0x1000, 0x1100,
168         0x8000, 0xAA00
169 };
170
171 /*
172  * ISA bus support.
173  */
174 static int rdp_probe            (struct isa_device *);
175 static int rdp_attach           (struct isa_device *);
176
177 /*
178  * Required entry points.
179  */
180 static void rdp_init(void *);
181 static int rdp_ioctl(struct ifnet *, IOCTL_CMD_T, caddr_t, struct ucred *);
182 static void rdp_start(struct ifnet *);
183 static void rdp_reset(struct ifnet *);
184 static void rdp_watchdog(struct ifnet *);
185 static void rdpintr(int);
186
187 /*
188  * REDP private functions.
189  */
190
191 static void rdp_stop(struct rdp_softc *);
192 static void rdp_rint(struct rdp_softc *);
193 static void rdp_get_packet(struct rdp_softc *, unsigned);
194 static u_short rdp_write_mbufs(struct rdp_softc *, struct mbuf *);
195 static int rdp_gethwaddr_93c46(struct rdp_softc *, u_char *);
196 static void rdp_gethwaddr_74s288(struct rdp_softc *, u_char *);
197 static void rdp_93c46_cmd(struct rdp_softc *, u_short, unsigned);
198 static u_short rdp_93c46_read(struct rdp_softc *);
199
200 struct isa_driver rdpdriver = {
201         rdp_probe,
202         rdp_attach,
203         "rdp",
204         1                       /* we wanna get a chance before lptN */
205 };
206
207 /*
208  * REDP-specific functions.
209  *
210  * They are inlined, thus go first in this file.  Together with gcc's
211  * usual optimization, these functions probably come close to the
212  * packet driver's hand-optimized code. ;-)
213  *
214  * Comments are partially obtained from the packet driver as well.
215  * Some of the function names contain register names which don't make
216  * much sense for us, but i've kept them for easier reference in
217  * comparision to the packet driver.
218  *
219  * Some of the functions are currently not used by the driver; it's
220  * not quite clear whether we ever need them at all.  They are
221  * supposedly even slower than what is currently implemented as `slow'
222  * mode.  Right now, `fast' (default) mode is what the packet driver
223  * calls mode 0, slow mode is mode 3 (writing through lpt_control,
224  * reading twice).
225  *
226  * We should autoprobe the modi, as opposed to making them dependent
227  * on a kernel configuration flag.
228  */
229
230 /*
231  * read a nibble from rreg; end-of-data cmd is not issued;
232  * used for general register read.
233  *
234  * Unlike the packet driver's version, i'm shifting the result
235  * by 3 here (as opposed to within the caller's code) for clarity.
236  *  -- Joerg
237  */
238 static __inline u_char
239 RdNib(struct rdp_softc *sc, u_char rreg)
240 {
241
242         outb(sc->baseaddr + lpt_data, EOC + rreg);
243         outb(sc->baseaddr + lpt_data, RdAddr + rreg); /* write addr */
244         (void)inb(sc->baseaddr + lpt_status);
245         return (inb(sc->baseaddr + lpt_status) >> 3) & 0x0f;
246 }
247
248 #if 0
249 /*
250  * read a byte from MAR register through lpt_data; the low nibble is
251  * read prior to the high one; end-of-read command is not issued; used
252  * for remote DMA in mode 4 + 5
253  */
254 static __inline u_char
255 RdByte(struct rdp_softc *sc)
256 {
257         u_char hinib, lonib;
258
259         outb(sc->baseaddr + lpt_data, RdAddr + MAR); /* cmd for low nibble */
260         lonib = (inb(sc->baseaddr + lpt_status) >> 3) & 0x0f;
261         outb(sc->baseaddr + lpt_data, RdAddr + MAR + HNib);
262         hinib = (inb(sc->baseaddr + lpt_status) << 1) & 0xf0;
263         return hinib + lonib;
264 }
265
266
267 /*
268  * read a byte from MAR register through lpt_data; the low nibble is
269  * read prior to the high one; end-of-read command is not issued; used
270  * for remote DMA in mode 6 + 7
271  */
272 static __inline u_char
273 RdByte1(struct rdp_softc *sc)
274 {
275         u_char hinib, lonib;
276
277         outb(sc->baseaddr + lpt_data, RdAddr + MAR); /* cmd for low nibble */
278         (void)inb(sc->baseaddr + lpt_status);
279         lonib = (inb(sc->baseaddr + lpt_status) >> 3) & 0x0f;
280         outb(sc->baseaddr + lpt_data, RdAddr + MAR + HNib);
281         (void)inb(sc->baseaddr + lpt_status);
282         hinib = (inb(sc->baseaddr + lpt_status) << 1) & 0xf0;
283         return hinib + lonib;
284 }
285 #endif
286
287
288 /*
289  * read a byte from MAR register through lpt_control; the low nibble is
290  * read prior to the high one; end-of-read command is not issued; used
291  * for remote DMA in mode 0 + 1
292  */
293 static __inline u_char
294 RdByteA1(struct rdp_softc *sc)
295 {
296         u_char hinib, lonib;
297
298         outb(sc->baseaddr + lpt_control, Ctrl_LNibRead);
299         lonib = (inb(sc->baseaddr + lpt_status) >> 3) & 0x0f;
300         outb(sc->baseaddr + lpt_control, Ctrl_HNibRead);
301         hinib = (inb(sc->baseaddr + lpt_status) << 1) & 0xf0;
302         return hinib + lonib;
303 }
304
305
306 /*
307  * read a byte from MAR register through lpt_control; the low nibble is
308  * read prior to the high one; end-of-read command is not issued; used
309  * for remote DMA in mode 2 + 3
310  */
311 static __inline u_char
312 RdByteA2(struct rdp_softc *sc)
313 {
314         u_char hinib, lonib;
315
316         outb(sc->baseaddr + lpt_control, Ctrl_LNibRead);
317         (void)inb(sc->baseaddr + lpt_status);
318         lonib = (inb(sc->baseaddr + lpt_status) >> 3) & 0x0f;
319         outb(sc->baseaddr + lpt_control, Ctrl_HNibRead);
320         (void)inb(sc->baseaddr + lpt_status);
321         hinib = (inb(sc->baseaddr + lpt_status) << 1) & 0xf0;
322         return hinib + lonib;
323 }
324
325 /*
326  * End-of-read cmd
327  */
328 static __inline void
329 RdEnd(struct rdp_softc *sc, u_char rreg)
330 {
331
332         outb(sc->baseaddr + lpt_data, EOC + rreg);
333 }
334
335 /*
336  * Write a nibble to a register; end-of-write is issued.
337  * Used for general register write.
338  */
339 static __inline void
340 WrNib(struct rdp_softc *sc, u_char wreg, u_char wdata)
341 {
342
343         /* prepare and write address */
344         outb(sc->baseaddr + lpt_data, EOC + wreg);
345         outb(sc->baseaddr + lpt_data, WrAddr + wreg);
346         outb(sc->baseaddr + lpt_data, WrAddr + wreg);
347         /* prepare and write data */
348         outb(sc->baseaddr + lpt_data, WrAddr + wdata);
349         outb(sc->baseaddr + lpt_data, wdata);
350         outb(sc->baseaddr + lpt_data, wdata);
351         /* end-of-write */
352         outb(sc->baseaddr + lpt_data, EOC + wdata);
353 }
354
355 /*
356  * Write a byte to a register; end-of-write is issued.
357  * Used for general register write.
358  */
359 static __inline void
360 WrByte(struct rdp_softc *sc, u_char wreg, u_char wdata)
361 {
362
363         /* prepare and write address */
364         outb(sc->baseaddr + lpt_data, EOC + wreg);
365         outb(sc->baseaddr + lpt_data, WrAddr + wreg);
366         outb(sc->baseaddr + lpt_data, WrAddr + wreg);
367         /* prepare and write low nibble */
368         outb(sc->baseaddr + lpt_data, WrAddr + (wdata & 0x0F));
369         outb(sc->baseaddr + lpt_data, (wdata & 0x0F));
370         outb(sc->baseaddr + lpt_data, (wdata & 0x0F));
371         /* prepare and write high nibble */
372         wdata >>= 4;
373         outb(sc->baseaddr + lpt_data, wdata);
374         outb(sc->baseaddr + lpt_data, wdata + HNib);
375         outb(sc->baseaddr + lpt_data, wdata + HNib);
376         /* end-of-write */
377         outb(sc->baseaddr + lpt_data, EOC + wdata + HNib);
378 }
379
380 /*
381  * Write the byte to DRAM via lpt_data;
382  * used for remote DMA write in mode 0 / 2 / 4
383  */
384 static __inline void
385 WrByteALToDRAM(struct rdp_softc *sc, u_char val)
386 {
387
388         outb(sc->baseaddr + lpt_data, val & 0x0F);
389         outb(sc->baseaddr + lpt_data, MkHi(val));
390 }
391
392 /*
393  * Write the byte to DRAM via lpt_control;
394  * used for remote DMA write in mode 1 / 3 / 5
395  */
396 static __inline void
397 WrByteALToDRAMA(struct rdp_softc *sc, u_char val)
398 {
399
400         outb(sc->baseaddr + lpt_data, val & 0x0F);
401         outb(sc->baseaddr + lpt_control, Ctrl_LNibRead | sc->irqenbit);
402         outb(sc->baseaddr + lpt_data, val >> 4);
403         outb(sc->baseaddr + lpt_control, Ctrl_HNibRead | sc->irqenbit);
404 }
405
406 #if 0 /* they could be used for the RAM test */
407 /*
408  * Write the u_short to DRAM via lpt_data;
409  * used for remote DMA write in mode 0 / 2 / 4
410  */
411 static __inline void
412 WrWordbxToDRAM(struct rdp_softc *sc, u_short val)
413 {
414
415         outb(sc->baseaddr + lpt_data, val & 0x0F);
416         val >>= 4;
417         outb(sc->baseaddr + lpt_data, (val & 0x0F) + HNib);
418         val >>= 4;
419         outb(sc->baseaddr + lpt_data, val & 0x0F);
420         val >>= 4;
421         outb(sc->baseaddr + lpt_data, val + HNib);
422 }
423
424
425 /*
426  * Write the u_short to DRAM via lpt_control;
427  * used for remote DMA write in mode 1 / 3 / 5
428  */
429 static __inline void
430 WrWordbxToDRAMA(struct rdp_softc *sc, u_short val)
431 {
432
433         outb(sc->baseaddr + lpt_data, val & 0x0F);
434         outb(sc->baseaddr + lpt_control, Ctrl_LNibRead | sc->irqenbit);
435         val >>= 4;
436         outb(sc->baseaddr + lpt_data, (val & 0x0F) + HNib);
437         outb(sc->baseaddr + lpt_control, Ctrl_HNibRead | sc->irqenbit);
438         val >>= 4;
439         outb(sc->baseaddr + lpt_data, val & 0x0F);
440         outb(sc->baseaddr + lpt_control, Ctrl_LNibRead | sc->irqenbit);
441         val >>= 4;
442         outb(sc->baseaddr + lpt_data, val + HNib);
443         outb(sc->baseaddr + lpt_control, Ctrl_HNibRead | sc->irqenbit);
444 }
445 #endif
446
447
448 /*
449  * Determine if the device is present
450  *
451  *   on entry:
452  *      a pointer to an isa_device struct
453  *   on exit:
454  *      0 if device not found
455  *      or # of i/o addresses used (if found)
456  */
457 static int
458 rdp_probe(struct isa_device *isa_dev)
459 {
460         int unit = isa_dev->id_unit;
461         struct rdp_softc *sc = &rdp_softc[unit];
462         u_char b1, b2;
463         intrmask_t irqmap[3];
464         u_char sval[3];
465
466         if (unit < 0 || unit >= NRDP)
467                 return 0;
468
469         sc->baseaddr = isa_dev->id_iobase;
470         if (isa_dev->id_flags & 1)
471                 sc->eeprom = EEPROM_74S288;
472         /* else defaults to 93C46 */
473         if (isa_dev->id_flags & 2)
474                 sc->slow = 1;
475
476         /* let R/WB = A/DB = CSB = high to be ready for next r/w cycle */
477         outb(sc->baseaddr + lpt_data, 0xFF);
478         /* DIR = 0 for write mode, IRQEN=0, SLCT=INIT=AUTOFEED=STB=high */
479         outb(sc->baseaddr + lpt_control, Ctrl_SelData);
480         /* software reset */
481         WrNib(sc, CMR1 + HNib, MkHi(CMR1_RST));
482         DELAY(2000);
483         /* is EPLC alive? */
484         b1 = RdNib(sc, CMR1);
485         RdEnd(sc, CMR1);
486         b2 = RdNib(sc, CMR2) & 0x0f;
487         b2 |= RdNib(sc, CMR2 + HNib) << 4;
488         RdEnd(sc, CMR2 + HNib);
489         /*
490          * After the reset, we expect CMR1 & 7 to be 1 (rx buffer empty),
491          * and CMR2 & 0xf7 to be 0x20 (receive mode set to physical and
492          * broadcasts).
493          */
494         if (bootverbose)
495                 printf("rdp%d: CMR1 = %#x, CMR2 = %#x\n", unit, b1, b2);
496
497         if ((b1 & (CMR1_BUFE | CMR1_IRQ | CMR1_TRA)) != CMR1_BUFE
498             || (b2 & ~CMR2_IRQINV) != CMR2_AM_PB)
499                 return 0;
500
501         /*
502          * We have found something that could be a RTL 80[01]2, now
503          * see whether we can generate an interrupt.
504          */
505         cpu_disable_intr();
506
507         /*
508          * Test whether our configured IRQ is working.
509          *
510          * Set to no acception mode + IRQout, then enable RxE + TxE,
511          * then cause RBER (by advancing the read pointer although
512          * the read buffer is empty) to generate an interrupt.
513          */
514         WrByte(sc, CMR2, CMR2_IRQOUT);
515         WrNib(sc, CMR1 + HNib, MkHi(CMR1_TE | CMR1_RE));
516         WrNib(sc, CMR1, CMR1_RDPAC);
517         DELAY(1000);
518
519         irqmap[0] = isa_irq_pending();
520         sval[0] = inb(sc->baseaddr + lpt_status);
521
522         /* allow IRQs to pass the parallel interface */
523         outb(sc->baseaddr + lpt_control, Ctrl_IRQEN + Ctrl_SelData);
524         DELAY(1000);
525         /* generate interrupt */
526         WrNib(sc, IMR + HNib, MkHi(ISR_RBER));
527         DELAY(1000);
528
529         irqmap[1] = isa_irq_pending();
530         sval[1] = inb(sc->baseaddr + lpt_status);
531
532         /* de-assert and disable IRQ */
533         WrNib(sc, IMR + HNib, MkHi(0));
534         (void)inb(sc->baseaddr + lpt_status); /* might be necessary to
535                                                  clear IRQ */
536         DELAY(1000);
537         irqmap[2] = isa_irq_pending();
538         sval[2] = inb(sc->baseaddr + lpt_status);
539
540         WrNib(sc, CMR1 + HNib, MkHi(0));
541         outb(sc->baseaddr + lpt_control, Ctrl_SelData);
542         WrNib(sc, CMR2, CMR2_IRQINV);
543
544         cpu_enable_intr();
545
546         if (bootverbose)
547                 printf("rdp%d: irq maps / lpt status "
548                        "%#x/%#x - %#x/%#x - %#x/%#x (id_irq %#x)\n",
549                        unit, irqmap[0], sval[0], irqmap[1], sval[1],
550                        irqmap[2], sval[2], isa_dev->id_irq);
551
552         if ((irqmap[1] & isa_dev->id_irq) == 0) {
553                 printf("rdp%d: configured IRQ (%d) cannot be asserted "
554                        "by device",
555                        unit, ffs(isa_dev->id_irq) - 1);
556                 if (irqmap[1])
557                         printf(" (probable IRQ: %d)", ffs(irqmap[1]) - 1);
558                 printf("\n");
559                 return 0;
560         }
561
562         /*
563          * XXX should do RAMtest here
564          */
565
566         switch (sc->eeprom) {
567         case EEPROM_93C46:
568                 if (rdp_gethwaddr_93c46(sc, sc->arpcom.ac_enaddr) == 0) {
569                         printf("rdp%d: failed to find a valid hardware "
570                                "address in EEPROM\n",
571                                unit);
572                         return 0;
573                 }
574                 break;
575
576         case EEPROM_74S288:
577                 rdp_gethwaddr_74s288(sc, sc->arpcom.ac_enaddr);
578                 break;
579         }
580
581         return lpt_control + 1;
582 }
583
584 /*
585  * Install interface into kernel networking data structures
586  */
587 static int
588 rdp_attach(struct isa_device *isa_dev)
589 {
590         int unit = isa_dev->id_unit;
591         struct rdp_softc *sc = &rdp_softc[unit];
592         struct ifnet *ifp = &sc->arpcom.ac_if;
593
594         isa_dev->id_ointr = rdpintr;
595
596         /*
597          * Reset interface
598          */
599         rdp_stop(sc);
600
601         /*
602          * Initialize ifnet structure
603          */
604         ifp->if_softc = sc;
605         if_initname(ifp, "rdp", unit);
606         ifp->if_start = rdp_start;
607         ifp->if_ioctl = rdp_ioctl;
608         ifp->if_watchdog = rdp_watchdog;
609         ifp->if_init = rdp_init;
610         ifq_set_maxlen(&ifp->if_snd, IFQ_MAXLEN);
611         ifq_set_ready(&ifp->if_snd);
612         ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX;
613
614         /*
615          * Attach the interface
616          */
617         ether_ifattach(ifp, sc->arpcom.ac_enaddr);
618
619         /*
620          * Print additional info when attached
621          */
622         printf("%s: RealTek RTL%s pocket ethernet, EEPROM %s, %s mode\n",
623                ifp->if_xname,
624                "8002",          /* hook for 8012 */
625                sc->eeprom == EEPROM_93C46? "93C46": "74S288",
626                sc->slow? "slow": "fast");
627
628         return 1;
629 }
630
631 /*
632  * Reset interface.
633  */
634 static void
635 rdp_reset(struct ifnet *ifp)
636 {
637         struct rdp_softc *sc = ifp->if_softc;
638         int s;
639
640         s = splimp();
641
642         /*
643          * Stop interface and re-initialize.
644          */
645         rdp_stop(sc);
646         rdp_init(sc);
647
648         (void) splx(s);
649 }
650
651 /*
652  * Take interface offline.
653  */
654 static void
655 rdp_stop(struct rdp_softc *sc)
656 {
657
658         sc->txbusy = sc->txbusy = 0;
659
660         /* disable printer interface interrupts */
661         sc->irqenbit = 0;
662         outb(sc->baseaddr + lpt_control, Ctrl_SelData);
663         outb(sc->baseaddr + lpt_data, 0xff);
664
665         /* reset the RTL 8002 */
666         WrNib(sc, CMR1 + HNib, MkHi(CMR1_RST));
667         DELAY(100);
668 }
669
670 /*
671  * Device timeout/watchdog routine. Entered if the device neglects to
672  * generate an interrupt after a transmit has been started on it.
673  */
674 static void
675 rdp_watchdog(struct ifnet *ifp)
676 {
677
678         log(LOG_ERR, "%s: device timeout\n", ifp->if_xname);
679         ifp->if_oerrors++;
680
681         rdp_reset(ifp);
682 }
683
684 /*
685  * Initialize device.
686  */
687 static void
688 rdp_init(void *xsc)
689 {
690         struct rdp_softc *sc = xsc;
691         struct ifnet *ifp = &sc->arpcom.ac_if;
692         int i, s;
693         u_char reg;
694
695         /* address not known */
696         if (TAILQ_EMPTY(&ifp->if_addrhead))
697                 return;
698
699         s = splimp();
700
701         ifp->if_timer = 0;
702
703         /* program ethernet ID into the chip */
704         for (i = 0, reg = IDR0; i < 6; i++, reg++)
705                 WrByte(sc, reg, sc->arpcom.ac_enaddr[i]);
706
707         /* set accept mode */
708         WrNib(sc, CMR2 + HNib,
709               MkHi((ifp->if_flags & IFF_PROMISC)? CMR2_AM_ALL: CMR2_AM_PB));
710
711         /* enable tx and rx */
712         WrNib(sc, CMR1 + HNib, MkHi(CMR1_TE | CMR1_RE));
713
714         /* allow interrupts to happen */
715         WrNib(sc, CMR2, CMR2_IRQOUT | CMR2_IRQINV);
716         WrNib(sc, IMR, ISR_TOK | ISR_TER | ISR_ROK | ISR_RER);
717         WrNib(sc, IMR + HNib, MkHi(ISR_RBER));
718
719         /* allow IRQs to pass the parallel interface */
720         sc->irqenbit = Ctrl_IRQEN;
721         outb(sc->baseaddr + lpt_control, sc->irqenbit + Ctrl_SelData);
722
723         /* clear all flags */
724         sc->txbusy = sc->txbuffered = 0;
725
726         /*
727          * Set 'running' flag, and clear output active flag.
728          */
729         ifp->if_flags |= IFF_RUNNING;
730         ifp->if_flags &= ~IFF_OACTIVE;
731
732         /*
733          * ...and attempt to start output
734          */
735         rdp_start(ifp);
736
737         (void) splx(s);
738 }
739
740 /*
741  * Start output on interface.
742  * We make two assumptions here:
743  *  1) that the current priority is set to splimp _before_ this code
744  *     is called *and* is returned to the appropriate priority after
745  *     return
746  *  2) that the IFF_OACTIVE flag is checked before this code is called
747  *     (i.e. that the output part of the interface is idle)
748  */
749 static void
750 rdp_start(struct ifnet *ifp)
751 {
752         struct rdp_softc *sc = ifp->if_softc;
753         struct mbuf *m;
754         int len;
755
756 outloop:
757
758         /*
759          * See if there is room to put another packet in the buffer.
760          */
761         if (sc->txbuffered) {
762                 /*
763                  * No room. Indicate this to the outside world and exit.
764                  */
765                 ifp->if_flags |= IFF_OACTIVE;
766                 return;
767         }
768         m = ifq_dequeue(&ifp->if_snd);
769         if (m == NULL) {
770                 /*
771                  * We are using the !OACTIVE flag to indicate to the outside
772                  * world that we can accept an additional packet rather than
773                  * that the transmitter is _actually_ active. Indeed, the
774                  * transmitter may be active, but if we haven't filled all the
775                  * buffers with data then we still want to accept more.
776                  */
777                 ifp->if_flags &= ~IFF_OACTIVE;
778                 return;
779         }
780
781         /*
782          * Copy the mbuf chain into the transmit buffer
783          */
784
785         len = rdp_write_mbufs(sc, m);
786         if (len == 0)
787                 goto outloop;
788
789         /* ensure minimal valid ethernet length */
790         len = max(len, (ETHER_MIN_LEN-ETHER_CRC_LEN));
791
792         /*
793          * Actually start the transceiver.  Set a timeout in case the
794          * Tx interrupt never arrives.
795          */
796         if (!sc->txbusy) {
797                 WrNib(sc, TBCR1, len >> 8);
798                 WrByte(sc, TBCR0, len & 0xff);
799                 WrNib(sc, CMR1, CMR1_TRA);
800                 sc->txbusy = 1;
801                 ifp->if_timer = 2;
802         } else {
803                 sc->txbuffered = 1;
804                 sc->txsize = len;
805         }
806
807         BPF_MTAP(ifp, m);
808
809         m_freem(m);
810
811         /*
812          * Loop back to the top to possibly buffer more packets
813          */
814         goto outloop;
815 }
816
817 /*
818  * Process an ioctl request.
819  */
820 static int
821 rdp_ioctl(struct ifnet *ifp, IOCTL_CMD_T command, caddr_t data,
822           struct ucred *ur)
823 {
824         struct rdp_softc *sc = ifp->if_softc;
825         int s, error = 0;
826
827         s = splimp();
828
829         switch (command) {
830
831         case SIOCSIFADDR:
832         case SIOCGIFADDR:
833         case SIOCSIFMTU:
834                 error = ether_ioctl(ifp, command, data);
835                 break;
836
837         case SIOCSIFFLAGS:
838                 /*
839                  * If the interface is marked up and stopped, then start it.
840                  * If it is marked down and running, then stop it.
841                  */
842                 if (ifp->if_flags & IFF_UP) {
843                         if ((ifp->if_flags & IFF_RUNNING) == 0)
844                                 rdp_init(sc);
845                 } else {
846                         if (ifp->if_flags & IFF_RUNNING) {
847                                 rdp_stop(sc);
848                                 ifp->if_flags &= ~IFF_RUNNING;
849                         }
850                 }
851
852                 /*
853                  * Promiscuous flag may have changed, propagage this
854                  * to the NIC.
855                  */
856                 if (ifp->if_flags & IFF_UP)
857                         WrNib(sc, CMR2 + HNib,
858                               MkHi((ifp->if_flags & IFF_PROMISC)?
859                                    CMR2_AM_ALL: CMR2_AM_PB));
860
861                 break;
862
863         case SIOCADDMULTI:
864         case SIOCDELMULTI:
865                 /*
866                  * Multicast list has changed; we don't support it.
867                  */
868                 error = ENOTTY;
869                 break;
870
871         default:
872                 error = EINVAL;
873         }
874         (void) splx(s);
875         return (error);
876 }
877
878 /*
879  * External interrupt service routine.
880  */
881 void 
882 rdpintr(int unit)
883 {
884         struct rdp_softc *sc = rdp_softc + unit;
885         struct ifnet *ifp = (struct ifnet *)sc;
886         u_char isr, tsr, rsr, colls;
887
888         /* disable interrupts, so SD3 can be routed to the pin */
889         sc->irqenbit = 0;
890         outb(sc->baseaddr + lpt_control, Ctrl_SelData);
891         WrNib(sc, CMR2, CMR2_IRQINV);
892         /*
893          * loop until there are no more new interrupts
894          */
895         for (;;) {
896                 isr = RdNib(sc, ISR);
897                 isr |= RdNib(sc, ISR + HNib) << 4;
898                 RdEnd(sc, ISR + HNib);
899
900                 if (isr == 0)
901                         break;
902 #if DEBUG & 4
903                 printf("rdp%d: ISR = %#x\n", unit, isr);
904 #endif
905
906                 /*
907                  * Clear the pending interrupt bits.
908                  */
909                 WrNib(sc, ISR, isr & 0x0f);
910                 if (isr & 0xf0)
911                         WrNib(sc, ISR + HNib, MkHi(isr));
912
913                 /*
914                  * Handle transmitter interrupts.
915                  */
916                 if (isr & (ISR_TOK | ISR_TER)) {
917                         tsr = RdNib(sc, TSR);
918                         RdEnd(sc, TSR);
919 #if DEBUG & 4
920                         if (isr & ISR_TER)
921                                 printf("rdp%d: tsr %#x\n", unit, tsr);
922 #endif
923                         if (tsr & TSR_TABT)
924                                 ifp->if_oerrors++;
925                         else
926                                 /*
927                                  * Update total number of successfully
928                                  * transmitted packets.
929                                  */
930                                 ifp->if_opackets++;
931
932                         if (tsr & TSR_COL) {
933                                 colls = RdNib(sc, COLR);
934                                 RdEnd(sc, COLR);
935                                 ifp->if_collisions += colls;
936                         }
937
938                         /*
939                          * reset tx busy and output active flags
940                          */
941                         sc->txbusy = 0;
942                         ifp->if_flags &= ~IFF_OACTIVE;
943
944                         /*
945                          * If we had already queued up another packet,
946                          * start sending it now.
947                          */
948                         if (sc->txbuffered) {
949                                 WrNib(sc, TBCR1, sc->txsize >> 8);
950                                 WrByte(sc, TBCR0, sc->txsize & 0xff);
951                                 WrNib(sc, CMR1, CMR1_TRA);
952                                 sc->txbusy = 1;
953                                 sc->txbuffered = 0;
954                                 ifp->if_timer = 2;
955                         } else {
956                                 /*
957                                  * clear watchdog timer
958                                  */
959                                 ifp->if_timer = 0;
960                         }
961                         
962                 }
963
964                 /*
965                  * Handle receiver interrupts
966                  */
967                 if (isr & (ISR_ROK | ISR_RER | ISR_RBER)) {
968                         rsr = RdNib(sc, RSR);
969                         rsr |= RdNib(sc, RSR + HNib) << 4;
970                         RdEnd(sc, RSR + HNib);
971 #if DEBUG & 4
972                         if (isr & (ISR_RER | ISR_RBER))
973                                 printf("rdp%d: rsr %#x\n", unit, rsr);
974 #endif
975
976                         if (rsr & (RSR_PUN | RSR_POV)) {
977                                 printf("rdp%d: rsr %#x, resetting\n",
978                                        unit, rsr);
979                                 rdp_reset(ifp);
980                                 break;
981                         }
982
983                         if (rsr & RSR_BUFO)
984                                 /*
985                                  * CRC and FA errors are recorded in
986                                  * rdp_rint() on a per-packet basis
987                                  */
988                                 ifp->if_ierrors++;
989                         if (isr & (ISR_ROK | ISR_RER))
990                                 rdp_rint(sc);
991                 }
992
993                 /*
994                  * If it looks like the transmitter can take more data,
995                  * attempt to start output on the interface. This is done
996                  * after handling the receiver to give the receiver priority.
997                  */
998                 if ((ifp->if_flags & IFF_OACTIVE) == 0)
999                         rdp_start(ifp);
1000
1001         }
1002         /* re-enable interrupts */
1003         WrNib(sc, CMR2, CMR2_IRQOUT | CMR2_IRQINV);
1004         sc->irqenbit = Ctrl_IRQEN;
1005         outb(sc->baseaddr + lpt_control, Ctrl_SelData + sc->irqenbit);
1006 }
1007
1008 /*
1009  * Ethernet interface receiver interrupt.
1010  */
1011 static void
1012 rdp_rint(struct rdp_softc *sc)
1013 {
1014         struct ifnet *ifp = &sc->arpcom.ac_if;
1015         struct rdphdr rh;
1016         u_short len;
1017         size_t i;
1018         u_char *packet_ptr, b, status;
1019         int excessive_bad_pkts = 0;
1020
1021         /*
1022          * Fetch the packets from the NIC's buffer.
1023          */
1024         for (;;) {
1025                 b = RdNib(sc, CMR1);
1026                 RdEnd(sc, CMR1);
1027
1028                 if (b & CMR1_BUFE)
1029                         /* no more packets */
1030                         break;
1031
1032                 /* first, obtain the buffer header */
1033                 
1034                 outb(sc->baseaddr + lpt_data, MAR + EOC); /* prepare addr */
1035                 outb(sc->baseaddr + lpt_control, Ctrl_LNibRead);
1036                 outb(sc->baseaddr + lpt_data, MAR + RdAddr + HNib);
1037
1038                 packet_ptr = (u_char *)&rh;
1039                 if (sc->slow)
1040                         for (i = 0; i < sizeof rh; i++, packet_ptr++)
1041                                 *packet_ptr = RdByteA2(sc);
1042                 else
1043                         for (i = 0; i < sizeof rh; i++, packet_ptr++)
1044                                 *packet_ptr = RdByteA1(sc);
1045
1046                 RdEnd(sc, MAR + HNib);
1047                 outb(sc->baseaddr + lpt_control, Ctrl_SelData);
1048
1049                 len = rh.pktlen - ETHER_CRC_LEN;
1050                 status = rh.status;
1051
1052                 if ((status & (RSR_ROK | RSR_CRC | RSR_FA)) != RSR_ROK ||
1053                     len > (ETHER_MAX_LEN - ETHER_CRC_LEN) ||
1054                     len < (ETHER_MIN_LEN - ETHER_CRC_LEN) ||
1055                     len > MCLBYTES) {
1056 #if DEBUG
1057                         printf("%s: bad packet in buffer, "
1058                                "len %d, status %#x\n",
1059                                ifp->if_xname, (int)len, (int)status);
1060 #endif
1061                         ifp->if_ierrors++;
1062                         /* rx jump packet */
1063                         WrNib(sc, CMR1, CMR1_RDPAC);
1064                         if (++excessive_bad_pkts > 5) {
1065                                 /*
1066                                  * the chip seems to be stuck, we are
1067                                  * probably seeing the same bad packet
1068                                  * over and over again
1069                                  */
1070 #if DEBUG
1071                                 printf("%s: resetting due to an "
1072                                        "excessive number of bad packets\n",
1073                                        ifp->if_xname);
1074 #endif
1075                                 rdp_reset(ifp);
1076                                 return;
1077                         }
1078                         continue;
1079                 }
1080
1081                 /*
1082                  * Go get packet.
1083                  */
1084                 excessive_bad_pkts = 0;
1085                 rdp_get_packet(sc, len);
1086                 ifp->if_ipackets++;
1087         }
1088 }
1089
1090 /*
1091  * Retreive packet from NIC memory and send to the next level up via
1092  * ether_input().
1093  */
1094 static void
1095 rdp_get_packet(struct rdp_softc *sc, unsigned len)
1096 {
1097         struct ifnet *ifp = &sc->arpcom.ac_if;
1098         struct mbuf *m;
1099         u_char *packet_ptr;
1100         size_t s;
1101
1102         /* Allocate a header mbuf */
1103         MGETHDR(m, MB_DONTWAIT, MT_DATA);
1104         if (m == NULL)
1105                 return;
1106         m->m_pkthdr.rcvif = ifp;
1107         m->m_pkthdr.len = m->m_len = len;
1108
1109         /*
1110          * We always put the received packet in a single buffer -
1111          * either with just an mbuf header or in a cluster attached
1112          * to the header. The +2 is to compensate for the alignment
1113          * fixup below.
1114          */
1115         if ((len + 2) > MHLEN) {
1116                 /* Attach an mbuf cluster */
1117                 MCLGET(m, MB_DONTWAIT);
1118
1119                 /* Insist on getting a cluster */
1120                 if ((m->m_flags & M_EXT) == 0) {
1121                         m_freem(m);
1122                         return;
1123                 }
1124         }
1125
1126         /*
1127          * The +2 is to longword align the start of the real packet.
1128          * This is important for NFS.
1129          */
1130         m->m_data += 2;
1131
1132         /*
1133          * Get packet, including link layer address, from interface.
1134          */
1135         outb(sc->baseaddr + lpt_control, Ctrl_LNibRead);
1136         outb(sc->baseaddr + lpt_data, RdAddr + MAR);
1137
1138         packet_ptr = mtod(m, u_char *);
1139         if (sc->slow)
1140                 for (s = 0; s < len; s++, packet_ptr++)
1141                         *packet_ptr = RdByteA2(sc);
1142         else
1143                 for (s = 0; s < len; s++, packet_ptr++)
1144                         *packet_ptr = RdByteA1(sc);
1145
1146         RdEnd(sc, MAR + HNib);
1147         outb(sc->baseaddr + lpt_control, Ctrl_SelData);
1148         WrNib(sc, CMR1, CMR1_RDPAC);
1149
1150         (*ifp->if_input)(ifp, m);
1151 }
1152
1153 /*
1154  * Write an mbuf chain to the NIC's tx buffer.
1155  */
1156 static u_short
1157 rdp_write_mbufs(struct rdp_softc *sc, struct mbuf *m)
1158 {
1159         u_short total_len;
1160         struct mbuf *mp;
1161         u_char *dp, b;
1162         int i;
1163
1164         /* First, count up the total number of bytes to copy */
1165         for (total_len = 0, mp = m; mp; mp = mp->m_next)
1166                 total_len += mp->m_len;
1167
1168         if (total_len == 0)
1169                 return 0;
1170
1171         outb(sc->baseaddr + lpt_data, MAR | EOC);
1172
1173         /*
1174          * Transfer the mbuf chain to the NIC memory.
1175          */
1176         if (sc->slow) {
1177                 /* writing the first byte is complicated */
1178                 outb(sc->baseaddr + lpt_control,
1179                      Ctrl_LNibRead | sc->irqenbit);
1180                 outb(sc->baseaddr + lpt_data, MAR | WrAddr);
1181                 b = *(u_char *)m->m_data;
1182                 outb(sc->baseaddr + lpt_data, (b & 0x0f) | 0x40);
1183                 outb(sc->baseaddr + lpt_data, b & 0x0f);
1184                 outb(sc->baseaddr + lpt_data, b >> 4);
1185                 outb(sc->baseaddr + lpt_control,
1186                      Ctrl_HNibRead | sc->irqenbit);
1187                 /* advance the mbuf pointer */
1188                 mp = m;
1189                 m->m_len--;
1190                 m->m_data++;
1191                 /* write the remaining bytes */
1192                 while (m) {
1193                         for (i = 0, dp = (u_char *)m->m_data;
1194                              i < m->m_len;
1195                              i++, dp++)
1196                                 WrByteALToDRAMA(sc, *dp);
1197                         m = m->m_next;
1198                 }
1199                 /*
1200                  * restore old mbuf in case we have to hand it off to
1201                  * BPF again
1202                  */
1203                 m = mp;
1204                 m->m_len++;
1205                 m->m_data--;
1206
1207                 /* the RTL 8002 requires an even byte-count remote DMA */
1208                 if (total_len & 1)
1209                         WrByteALToDRAMA(sc, 0);
1210         } else {
1211                 outb(sc->baseaddr + lpt_data, MAR | WrAddr);
1212                 while (m) {
1213                         for (i = 0, dp = (u_char *)m->m_data;
1214                              i < m->m_len;
1215                              i++, dp++)
1216                                 WrByteALToDRAM(sc, *dp);
1217                         m = m->m_next;
1218                 }
1219
1220                 /* the RTL 8002 requires an even byte-count remote DMA */
1221                 if (total_len & 1)
1222                         WrByteALToDRAM(sc, 0);
1223         }
1224
1225         outb(sc->baseaddr + lpt_data, 0xff);
1226         outb(sc->baseaddr + lpt_control,
1227              Ctrl_HNibRead | Ctrl_SelData | sc->irqenbit);
1228
1229         return total_len;
1230 }
1231
1232 /*
1233  * Read the designated ethernet hardware address out of a 93C46
1234  * (serial) EEPROM.
1235  * Note that the 93C46 uses 16-bit words in big-endian notation.
1236  */
1237 static int
1238 rdp_gethwaddr_93c46(struct rdp_softc *sc, u_char *etheraddr)
1239 {
1240         int i, magic;
1241         size_t j = 0;
1242         u_short w;
1243
1244         WrNib(sc, CMR2, CMR2_PAGE | CMR2_IRQINV); /* select page 1 */
1245
1246         /*
1247          * The original RealTek packet driver had the ethernet address
1248          * starting at EEPROM address 0.  Other vendors seem to have
1249          * gone `creative' here -- while they didn't do anything else
1250          * than changing a few strings in the entire driver, compared
1251          * to the RealTek version, they also moved out the ethernet
1252          * address to a different location in the EEPROM, so the
1253          * original RealTek driver won't work correctly with them, and
1254          * vice versa.  Sounds pretty cool, eh?  $@%&!
1255          *
1256          * Anyway, we walk through the EEPROM, until we find some
1257          * allowable value based upon our table of IEEE OUI assignments.
1258          */
1259         for (i = magic = 0; magic < 3 && i < 32; i++) {
1260                 /* read cmd (+ 6 bit address) */
1261                 rdp_93c46_cmd(sc, 0x180 + i, 10);
1262                 w = rdp_93c46_read(sc);
1263                 switch (magic) {
1264                 case 0:
1265                         for (j = 0;
1266                              j < sizeof allowed_ouis / sizeof(u_short);
1267                              j++)
1268                                 if (w == allowed_ouis[j]) {
1269                                         etheraddr[0] = (w >> 8) & 0xff;
1270                                         etheraddr[1] = w & 0xff;
1271                                         magic++;
1272                                         break;
1273                                 }
1274                         break;
1275
1276                 case 1:
1277                         /*
1278                          * If the first two bytes have been 00:00, we
1279                          * discard the match iff the next two bytes
1280                          * are also 00:00, so we won't get fooled by
1281                          * an EEPROM that has been filled with zeros.
1282                          * This in theory would disallow 64 K of legal
1283                          * addresses assigned to Xerox, but it's
1284                          * almost certain that those addresses haven't
1285                          * been used for RTL80[01]2 chips anyway.
1286                          */
1287                         if ((etheraddr[0] | etheraddr[1]) == 0 && w == 0) {
1288                                 magic--;
1289                                 break;
1290                         }
1291
1292                         etheraddr[2] = (w >> 8) & 0xff;
1293                         etheraddr[3] = w & 0xff;
1294                         magic++;
1295                         break;
1296
1297                 case 2:
1298                         etheraddr[4] = (w >> 8) & 0xff;
1299                         etheraddr[5] = w & 0xff;
1300                         magic++;
1301                         break;
1302                 }
1303         }
1304
1305         WrNib(sc, CMR2, CMR2_IRQINV);   /* back to page 0 */
1306
1307         return magic == 3;
1308 }
1309
1310 /*
1311  * Read the designated ethernet hardware address out of a 74S288
1312  * EEPROM.
1313  *
1314  * This is untested, since i haven't seen any adapter actually using
1315  * a 74S288.  In the RTL 8012, only the serial EEPROM (94C46) is
1316  * supported anymore.
1317  */
1318 static void
1319 rdp_gethwaddr_74s288(struct rdp_softc *sc, u_char *etheraddr)
1320 {
1321         int i;
1322         u_char b;
1323
1324         WrNib(sc, CMR2, CMR2_PAGE | CMR2_IRQINV); /* select page 1 */
1325
1326         for (i = 0; i < 6; i++) {
1327                 WrNib(sc, PCMR, i & 0x0f); /* lower 4 bit of addr */
1328                 WrNib(sc, PCMR + HNib, HNib + 4); /* upper 2 bit addr + /CS */
1329                 WrNib(sc, PCMR + HNib, HNib); /* latch data now */
1330                 b = RdNib(sc, PDR) & 0x0f;
1331                 b |= (RdNib(sc, PDR + HNib) & 0x0f) << 4;
1332                 etheraddr[i] = b;
1333         }
1334
1335         RdEnd(sc, PDR + HNib);
1336         WrNib(sc, CMR2, CMR2_IRQINV);   /* reselect page 0 */
1337 }
1338
1339 /*
1340  * Send nbits of data (starting with MSB) out to the 93c46 as a
1341  * command.  Assumes register page 1 has already been selected.
1342  */
1343 static void
1344 rdp_93c46_cmd(struct rdp_softc *sc, u_short data, unsigned nbits)
1345 {
1346         u_short mask = 1 << (nbits - 1);
1347         unsigned i;
1348         u_char b;
1349
1350 #if DEBUG & 2
1351         printf("rdp_93c46_cmd(): ");
1352 #endif
1353         for (i = 0; i < nbits; i++, mask >>= 1) {
1354                 b = HNib + PCMR_SK + PCMR_CS;
1355                 if (data & mask)
1356                         b += PCMR_DO;
1357 #if DEBUG & 2
1358                 printf("%d", b & 1);
1359 #endif
1360                 WrNib(sc, PCMR + HNib, b);
1361                 DELAY(1);
1362                 WrNib(sc, PCMR + HNib, b & ~PCMR_SK);
1363                 DELAY(1);
1364         }
1365 #if DEBUG & 2
1366         printf("\n");
1367 #endif
1368 }
1369
1370 /*
1371  * Read one word of data from the 93c46.  Actually, we have to read
1372  * 17 bits, and discard the very first bit.  Assumes register page 1
1373  * to be selected as well.
1374  */
1375 static u_short
1376 rdp_93c46_read(struct rdp_softc *sc)
1377 {
1378         u_short data = 0;
1379         u_char b;
1380         int i;
1381
1382 #if DEBUG & 2
1383         printf("rdp_93c46_read(): ");
1384 #endif
1385         for (i = 0; i < 17; i++) {
1386                 WrNib(sc, PCMR + HNib, PCMR_SK + PCMR_CS + HNib);
1387                 DELAY(1);
1388                 WrNib(sc, PCMR + HNib, PCMR_CS + HNib);
1389                 DELAY(1);
1390                 b = RdNib(sc, PDR);
1391                 data <<= 1;
1392                 if (b & 1)
1393                         data |= 1;
1394 #if DEBUG & 2
1395                 printf("%d", b & 1);
1396 #endif
1397                 RdEnd(sc, PDR);
1398                 DELAY(1);
1399         }
1400
1401 #if DEBUG & 2
1402         printf("\n");
1403 #endif
1404         /* end of cycle */
1405         WrNib(sc, PCMR + HNib, PCMR_SK + HNib);
1406         DELAY(1);
1407
1408         return data;
1409 }