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