if_aue.c revision 1.5 1 /* $NetBSD: if_aue.c,v 1.5 2000/01/16 15:35:06 augustss Exp $ */
2 /*
3 * Copyright (c) 1997, 1998, 1999, 2000
4 * Bill Paul <wpaul (at) ee.columbia.edu>. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following 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 * 3. All advertising materials mentioning features or use of this software
15 * must display the following acknowledgement:
16 * This product includes software developed by Bill Paul.
17 * 4. Neither the name of the author nor the names of any co-contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
25 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
31 * THE POSSIBILITY OF SUCH DAMAGE.
32 *
33 * $FreeBSD: src/sys/dev/usb/if_aue.c,v 1.11 2000/01/14 01:36:14 wpaul Exp $
34 */
35
36 /*
37 * ADMtek AN986 Pegasus USB to ethernet driver. Datasheet is available
38 * from http://www.admtek.com.tw.
39 *
40 * Written by Bill Paul <wpaul (at) ee.columbia.edu>
41 * Electrical Engineering Department
42 * Columbia University, New York City
43 */
44
45 /*
46 * The Pegasus chip uses four USB "endpoints" to provide 10/100 ethernet
47 * support: the control endpoint for reading/writing registers, burst
48 * read endpoint for packet reception, burst write for packet transmission
49 * and one for "interrupts." The chip uses the same RX filter scheme
50 * as the other ADMtek ethernet parts: one perfect filter entry for the
51 * the station address and a 64-bit multicast hash table. The chip supports
52 * both MII and HomePNA attachments.
53 *
54 * Since the maximum data transfer speed of USB is supposed to be 12Mbps,
55 * you're never really going to get 100Mbps speeds from this device. I
56 * think the idea is to allow the device to connect to 10 or 100Mbps
57 * networks, not necessarily to provide 100Mbps performance. Also, since
58 * the controller uses an external PHY chip, it's possible that board
59 * designers might simply choose a 10Mbps PHY.
60 *
61 * Registers are accessed using usbd_do_request(). Packet transfers are
62 * done using usbd_transfer() and friends.
63 */
64
65 /*
66 * Ported to NetBSD and somewhat rewritten by Lennart Augustsson.
67 */
68
69 /*
70 * TODO:
71 * better error messages from rxstat
72 * split out if_auevar.h
73 * add thread to avoid register reads from interrupt context
74 * more error checks
75 * investigate short rx problem
76 */
77
78 #if defined(__NetBSD__) || defined(__OpenBSD__)
79 #include "opt_inet.h"
80 #include "opt_ns.h"
81 #include "bpfilter.h"
82 #include "rnd.h"
83 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
84
85 #include <sys/param.h>
86 #include <sys/systm.h>
87 #include <sys/sockio.h>
88 #include <sys/mbuf.h>
89 #include <sys/malloc.h>
90 #include <sys/kernel.h>
91 #include <sys/socket.h>
92
93 #if defined(__FreeBSD__)
94
95 #include <net/ethernet.h>
96 #include <machine/clock.h> /* for DELAY */
97 #include <sys/bus.h>
98 /* "controller miibus0" required. See GENERIC if you get errors here. */
99 #include "miibus_if.h"
100
101 #elif defined(__NetBSD__) || defined(__OpenBSD__)
102
103 #include <sys/device.h>
104
105 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
106
107 #include <net/if.h>
108 #include <net/if_arp.h>
109 #include <net/if_dl.h>
110 #include <net/if_media.h>
111
112 #if defined(__NetBSD__) || defined(__OpenBSD__)
113 #include <net/if_ether.h>
114
115 #define bpf_mtap(ifp, m) bpf_tap((ifp)->if_bpf, mtod((m), caddr_t), (m)->m_len)
116
117 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
118
119 #if defined(__FreeBSD__) || NBPFILTER > 0
120 #include <net/bpf.h>
121 #endif
122
123 #if defined(__NetBSD__) || defined(__OpenBSD__)
124 #ifdef INET
125 #include <netinet/in.h>
126 #include <netinet/if_inarp.h>
127 #endif
128
129 #ifdef NS
130 #include <netns/ns.h>
131 #include <netns/ns_if.h>
132 #endif
133 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
134
135 #include <dev/mii/mii.h>
136 #include <dev/mii/miivar.h>
137
138 #include <dev/usb/usb.h>
139 #include <dev/usb/usbdi.h>
140 #include <dev/usb/usbdi_util.h>
141 #include <dev/usb/usbdevs.h>
142
143 #ifdef __FreeBSD__
144 #include <dev/usb/usb_ethersubr.h>
145 #endif
146
147 #include <dev/usb/if_auereg.h>
148
149 #define AUE_DEBUG
150
151 #ifdef AUE_DEBUG
152 #define DPRINTF(x) if (auedebug) logprintf x
153 #define DPRINTFN(n,x) if (auedebug >= (n)) logprintf x
154 int auedebug = 1;
155 #else
156 #define DPRINTF(x)
157 #define DPRINTFN(n,x)
158 #endif
159
160 int aue_cutoff = AUE_CUTOFF;
161 #undef AUE_CUTOFF
162 #define AUE_CUTOFF aue_cutoff
163
164 /*
165 * Various supported device vendors/types and their names.
166 */
167 static struct aue_type aue_devs[] = {
168 { USB_VENDOR_BILLIONTON, USB_PRODUCT_BILLIONTON_USB100 },
169 { USB_VENDOR_MELCO, USB_PRODUCT_MELCO_LUATX },
170 { USB_VENDOR_LINKSYS, USB_PRODUCT_LINKSYS_USB100TX },
171 { USB_VENDOR_ADMTEK, USB_PRODUCT_ADMTEK_PEGASUS },
172 { USB_VENDOR_DLINK, USB_PRODUCT_DLINK_DSB650TX },
173 { USB_VENDOR_DLINK, USB_PRODUCT_DLINK_DSB650TX_PNA },
174 { USB_VENDOR_SMC, USB_PRODUCT_SMC_2202USB },
175 { 0, 0 }
176 };
177
178 USB_DECLARE_DRIVER(aue);
179
180 static int aue_tx_list_init __P((struct aue_softc *));
181 static int aue_rx_list_init __P((struct aue_softc *));
182 static int aue_newbuf __P((struct aue_softc *, struct aue_chain *,
183 struct mbuf *));
184 static int aue_send __P((struct aue_softc *, struct mbuf *, int));
185 static void aue_intr __P((usbd_xfer_handle,
186 usbd_private_handle, usbd_status));
187 static void aue_rxeof __P((usbd_xfer_handle,
188 usbd_private_handle, usbd_status));
189 static void aue_txeof __P((usbd_xfer_handle,
190 usbd_private_handle, usbd_status));
191 static void aue_tick __P((void *));
192 static void aue_start __P((struct ifnet *));
193 static int aue_ioctl __P((struct ifnet *, u_long, caddr_t));
194 static void aue_init __P((void *));
195 static void aue_stop __P((struct aue_softc *));
196 static void aue_watchdog __P((struct ifnet *));
197 #ifdef __FreeBSD__
198 static void aue_shutdown __P((device_ptr_t));
199 #endif
200 static int aue_ifmedia_upd __P((struct ifnet *));
201 static void aue_ifmedia_sts __P((struct ifnet *, struct ifmediareq *));
202
203 static int aue_eeprom_getword __P((struct aue_softc *, int));
204 static void aue_read_mac __P((struct aue_softc *, u_char *));
205 static int aue_miibus_readreg __P((device_ptr_t, int, int));
206 #if defined(__FreeBSD__)
207 static int aue_miibus_writereg __P((device_ptr_t, int, int, int));
208 #elif defined(__NetBSD__) || defined(__OpenBSD__)
209 static void aue_miibus_writereg __P((device_ptr_t, int, int, int));
210 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
211 static void aue_miibus_statchg __P((device_ptr_t));
212
213 static void aue_setmulti __P((struct aue_softc *));
214 static u_int32_t aue_crc __P((caddr_t));
215 static void aue_reset __P((struct aue_softc *));
216
217 static int csr_read_1 __P((struct aue_softc *, int));
218 static int csr_write_1 __P((struct aue_softc *, int, int));
219 static int csr_read_2 __P((struct aue_softc *, int));
220 static int csr_write_2 __P((struct aue_softc *, int, int));
221
222 #if defined(__FreeBSD__)
223 #if !defined(lint)
224 static const char rcsid[] =
225 "$FreeBSD: src/sys/dev/usb/if_aue.c,v 1.11 2000/01/14 01:36:14 wpaul Exp $";
226 #endif
227
228 static void aue_rxstart __P((struct ifnet *));
229
230 static struct usb_qdat aue_qdat;
231
232 static device_method_t aue_methods[] = {
233 /* Device interface */
234 DEVMETHOD(device_probe, aue_match),
235 DEVMETHOD(device_attach, aue_attach),
236 DEVMETHOD(device_detach, aue_detach),
237 DEVMETHOD(device_shutdown, aue_shutdown),
238
239 /* bus interface */
240 DEVMETHOD(bus_print_child, bus_generic_print_child),
241 DEVMETHOD(bus_driver_added, bus_generic_driver_added),
242
243 /* MII interface */
244 DEVMETHOD(miibus_readreg, aue_miibus_readreg),
245 DEVMETHOD(miibus_writereg, aue_miibus_writereg),
246 DEVMETHOD(miibus_statchg, aue_miibus_statchg),
247
248 { 0, 0 }
249 };
250
251 static driver_t aue_driver = {
252 "aue",
253 aue_methods,
254 sizeof(struct aue_softc)
255 };
256
257 static devclass_t aue_devclass;
258
259 DRIVER_MODULE(if_aue, uhub, aue_driver, aue_devclass, usbd_driver_load, 0);
260 DRIVER_MODULE(miibus, aue, miibus_driver, miibus_devclass, 0, 0);
261
262 #endif /* __FreeBSD__ */
263
264 #define AUE_DO_REQUEST(dev, req, data) usbd_do_request_flags(dev, req, data, USBD_NO_TSLEEP, NULL)
265
266 #define AUE_SETBIT(sc, reg, x) \
267 csr_write_1(sc, reg, csr_read_1(sc, reg) | (x))
268
269 #define AUE_CLRBIT(sc, reg, x) \
270 csr_write_1(sc, reg, csr_read_1(sc, reg) & ~(x))
271
272 static int
273 csr_read_1(sc, reg)
274 struct aue_softc *sc;
275 int reg;
276 {
277 usb_device_request_t req;
278 usbd_status err;
279 uByte val = 0;
280 int s;
281
282 req.bmRequestType = UT_READ_VENDOR_DEVICE;
283 req.bRequest = AUE_UR_READREG;
284 USETW(req.wValue, 0);
285 USETW(req.wIndex, reg);
286 USETW(req.wLength, 1);
287
288 s = splusb();
289 err = AUE_DO_REQUEST(sc->aue_udev, &req, &val);
290 splx(s);
291
292 if (err)
293 return (0);
294
295 return (val);
296 }
297
298 static int
299 csr_read_2(sc, reg)
300 struct aue_softc *sc;
301 int reg;
302 {
303 usb_device_request_t req;
304 usbd_status err;
305 uWord val;
306 int s;
307
308 req.bmRequestType = UT_READ_VENDOR_DEVICE;
309 req.bRequest = AUE_UR_READREG;
310 USETW(req.wValue, 0);
311 USETW(req.wIndex, reg);
312 USETW(req.wLength, 2);
313
314 s = splusb();
315 err = AUE_DO_REQUEST(sc->aue_udev, &req, &val);
316 splx(s);
317
318 if (err)
319 return (0);
320
321 return (UGETW(val));
322 }
323
324 static int
325 csr_write_1(sc, reg, aval)
326 struct aue_softc *sc;
327 int reg, aval;
328 {
329 usb_device_request_t req;
330 usbd_status err;
331 int s;
332 uByte val;
333
334 val = aval;
335 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
336 req.bRequest = AUE_UR_WRITEREG;
337 USETW(req.wValue, val);
338 USETW(req.wIndex, reg);
339 USETW(req.wLength, 1);
340
341 s = splusb();
342 err = AUE_DO_REQUEST(sc->aue_udev, &req, &val);
343 splx(s);
344
345 if (err)
346 return (-1);
347
348 return (0);
349 }
350
351 static int
352 csr_write_2(sc, reg, aval)
353 struct aue_softc *sc;
354 int reg, aval;
355 {
356 usb_device_request_t req;
357 usbd_status err;
358 int s;
359 uWord val;
360
361 USETW(val, aval);
362 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
363 req.bRequest = AUE_UR_WRITEREG;
364 USETW(req.wValue, aval);
365 USETW(req.wIndex, reg);
366 USETW(req.wLength, 2);
367
368 s = splusb();
369 err = AUE_DO_REQUEST(sc->aue_udev, &req, &val);
370 splx(s);
371
372 if (err)
373 return (-1);
374
375 return (0);
376 }
377
378 /*
379 * Read a word of data stored in the EEPROM at address 'addr.'
380 */
381 static int
382 aue_eeprom_getword(sc, addr)
383 struct aue_softc *sc;
384 int addr;
385 {
386 int i;
387
388 csr_write_1(sc, AUE_EE_REG, addr);
389 csr_write_1(sc, AUE_EE_CTL, AUE_EECTL_READ);
390
391 for (i = 0; i < AUE_TIMEOUT; i++) {
392 if (csr_read_1(sc, AUE_EE_CTL) & AUE_EECTL_DONE)
393 break;
394 }
395
396 if (i == AUE_TIMEOUT) {
397 printf("%s: EEPROM read timed out\n",
398 USBDEVNAME(sc->aue_dev));
399 }
400
401 return (csr_read_2(sc, AUE_EE_DATA));
402 }
403
404 /*
405 * Read the MAC from the EEPROM. It's at offset 0.
406 */
407 static void
408 aue_read_mac(sc, dest)
409 struct aue_softc *sc;
410 u_char *dest;
411 {
412 int i;
413 int off = 0;
414 int word;
415
416 DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __FUNCTION__));
417
418 for (i = 0; i < 3; i++) {
419 word = aue_eeprom_getword(sc, off + i);
420 dest[2 * i] = (u_char)word;
421 dest[2 * i + 1] = (u_char)(word >> 8);
422 }
423 }
424
425 static int
426 aue_miibus_readreg(dev, phy, reg)
427 device_ptr_t dev;
428 int phy, reg;
429 {
430 struct aue_softc *sc = USBGETSOFTC(dev);
431 int i;
432 u_int16_t val;
433
434 /*
435 * The Am79C901 HomePNA PHY actually contains
436 * two transceivers: a 1Mbps HomePNA PHY and a
437 * 10Mbps full/half duplex ethernet PHY with
438 * NWAY autoneg. However in the ADMtek adapter,
439 * only the 1Mbps PHY is actually connected to
440 * anything, so we ignore the 10Mbps one. It
441 * happens to be configured for MII address 3,
442 * so we filter that out.
443 */
444 if (sc->aue_vendor == USB_VENDOR_ADMTEK &&
445 sc->aue_product == USB_PRODUCT_ADMTEK_PEGASUS) {
446 if (phy != 1)
447 return (0);
448 }
449
450 csr_write_1(sc, AUE_PHY_ADDR, phy);
451 csr_write_1(sc, AUE_PHY_CTL, reg | AUE_PHYCTL_READ);
452
453 for (i = 0; i < AUE_TIMEOUT; i++) {
454 if (csr_read_1(sc, AUE_PHY_CTL) & AUE_PHYCTL_DONE)
455 break;
456 }
457
458 if (i == AUE_TIMEOUT) {
459 printf("%s: MII read timed out\n",
460 USBDEVNAME(sc->aue_dev));
461 }
462
463 val = csr_read_2(sc, AUE_PHY_DATA);
464
465 DPRINTFN(11,("%s: %s: phy=%d reg=%d => 0x%04x\n",
466 USBDEVNAME(sc->aue_dev), __FUNCTION__, phy, reg, val));
467
468 return (val);
469 }
470
471 #if defined(__FreeBSD__)
472 static int
473 #elif defined(__NetBSD__) || defined(__OpenBSD__)
474 static void
475 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
476 aue_miibus_writereg(dev, phy, reg, data)
477 device_ptr_t dev;
478 int phy, reg, data;
479 {
480 struct aue_softc *sc = USBGETSOFTC(dev);
481 int i;
482
483 if (sc->aue_vendor == USB_VENDOR_ADMTEK &&
484 sc->aue_product == USB_PRODUCT_ADMTEK_PEGASUS) {
485 if (phy == 3)
486 #if defined(__FreeBSD__)
487 return (0);
488 #elif defined(__NetBSD__) || defined(__OpenBSD__)
489 return;
490 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
491 }
492
493 DPRINTFN(11,("%s: %s: phy=%d reg=%d data=0x%04x\n",
494 USBDEVNAME(sc->aue_dev), __FUNCTION__, phy, reg, data));
495
496 csr_write_2(sc, AUE_PHY_DATA, data);
497 csr_write_1(sc, AUE_PHY_ADDR, phy);
498 csr_write_1(sc, AUE_PHY_CTL, reg | AUE_PHYCTL_WRITE);
499
500 for (i = 0; i < AUE_TIMEOUT; i++) {
501 if (csr_read_1(sc, AUE_PHY_CTL) & AUE_PHYCTL_DONE)
502 break;
503 }
504
505 if (i == AUE_TIMEOUT) {
506 printf("%s: MII read timed out\n",
507 USBDEVNAME(sc->aue_dev));
508 }
509
510 #if defined(__FreeBSD__)
511 return (0);
512 #endif
513 }
514
515 static void
516 aue_miibus_statchg(dev)
517 device_ptr_t dev;
518 {
519 struct aue_softc *sc = USBGETSOFTC(dev);
520 struct mii_data *mii = GET_MII(sc);
521 struct ifnet *ifp = GET_IFP(sc);
522
523 DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __FUNCTION__));
524
525 AUE_CLRBIT(sc, AUE_CTL0, AUE_CTL0_RX_ENB | AUE_CTL0_TX_ENB);
526
527 if (IFM_SUBTYPE(mii->mii_media_active) == IFM_100_TX) {
528 AUE_SETBIT(sc, AUE_CTL1, AUE_CTL1_SPEEDSEL);
529 ifp->if_baudrate = 100000000;
530 } else {
531 AUE_CLRBIT(sc, AUE_CTL1, AUE_CTL1_SPEEDSEL);
532 ifp->if_baudrate = 10000000;
533 }
534
535 if ((mii->mii_media_active & IFM_GMASK) == IFM_FDX)
536 AUE_SETBIT(sc, AUE_CTL1, AUE_CTL1_DUPLEX);
537 else
538 AUE_CLRBIT(sc, AUE_CTL1, AUE_CTL1_DUPLEX);
539
540 AUE_SETBIT(sc, AUE_CTL0, AUE_CTL0_RX_ENB | AUE_CTL0_TX_ENB);
541
542 /*
543 * Set the LED modes on the LinkSys adapter.
544 * This turns on the 'dual link LED' bin in the auxmode
545 * register of the Broadcom PHY.
546 */
547 if (sc->aue_vendor == USB_VENDOR_LINKSYS &&
548 sc->aue_product == USB_PRODUCT_LINKSYS_USB100TX) {
549 u_int16_t auxmode;
550 auxmode = aue_miibus_readreg(dev, 0, 0x1b);
551 aue_miibus_writereg(dev, 0, 0x1b, auxmode | 0x04);
552 }
553 }
554
555 #define AUE_POLY 0xEDB88320
556 #define AUE_BITS 6
557
558 static u_int32_t
559 aue_crc(addr)
560 caddr_t addr;
561 {
562 u_int32_t idx, bit, data, crc;
563
564 /* Compute CRC for the address value. */
565 crc = 0xFFFFFFFF; /* initial value */
566
567 for (idx = 0; idx < 6; idx++) {
568 for (data = *addr++, bit = 0; bit < 8; bit++, data >>= 1)
569 crc = (crc >> 1) ^ (((crc ^ data) & 1) ? AUE_POLY : 0);
570 }
571
572 return (crc & ((1 << AUE_BITS) - 1));
573 }
574
575 static void
576 aue_setmulti(sc)
577 struct aue_softc *sc;
578 {
579 struct ifnet *ifp;
580 #if defined(__FreeBSD__)
581 struct ifmultiaddr *ifma;
582 #elif defined(__NetBSD__) || defined(__OpenBSD__)
583 struct ether_multi *enm;
584 struct ether_multistep step;
585 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
586 u_int32_t h = 0, i;
587
588 DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __FUNCTION__));
589
590 ifp = GET_IFP(sc);
591
592 if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) {
593 AUE_SETBIT(sc, AUE_CTL0, AUE_CTL0_ALLMULTI);
594 return;
595 }
596
597 AUE_CLRBIT(sc, AUE_CTL0, AUE_CTL0_ALLMULTI);
598
599 /* first, zot all the existing hash bits */
600 for (i = 0; i < 8; i++)
601 csr_write_1(sc, AUE_MAR0 + i, 0);
602
603 /* now program new ones */
604 #if defined(__FreeBSD__)
605 for (ifma = ifp->if_multiaddrs.lh_first; ifma != NULL;
606 ifma = ifma->ifma_link.le_next) {
607 if (ifma->ifma_addr->sa_family != AF_LINK)
608 continue;
609 h = aue_crc(LLADDR((struct sockaddr_dl *)ifma->ifma_addr));
610 AUE_SETBIT(sc, AUE_MAR + (h >> 3), 1 << (h & 0xF));
611 }
612 #elif defined(__NetBSD__) || defined(__OpenBSD__)
613 ETHER_FIRST_MULTI(step, &sc->aue_ec, enm);
614 while (enm != NULL) {
615 #if 1
616 if (memcmp(enm->enm_addrlo,
617 enm->enm_addrhi, ETHER_ADDR_LEN) != 0) {
618 ifp->if_flags |= IFF_ALLMULTI;
619 AUE_SETBIT(sc, AUE_CTL0, AUE_CTL0_ALLMULTI);
620 return;
621 }
622 #endif
623 h = aue_crc(enm->enm_addrlo);
624 AUE_SETBIT(sc, AUE_MAR + (h >> 3), 1 << (h & 0xF));
625 ETHER_NEXT_MULTI(step, enm);
626 }
627 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
628 }
629
630 static void
631 aue_reset(sc)
632 struct aue_softc *sc;
633 {
634 int i;
635
636 DPRINTFN(2,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __FUNCTION__));
637
638 AUE_SETBIT(sc, AUE_CTL1, AUE_CTL1_RESETMAC);
639
640 for (i = 0; i < AUE_TIMEOUT; i++) {
641 if (!(csr_read_1(sc, AUE_CTL1) & AUE_CTL1_RESETMAC))
642 break;
643 }
644
645 if (i == AUE_TIMEOUT)
646 printf("%s: reset failed\n", USBDEVNAME(sc->aue_dev));
647
648 /*
649 * The PHY(s) attached to the Pegasus chip may be held
650 * in reset until we flip on the GPIO outputs. Make sure
651 * to set the GPIO pins high so that the PHY(s) will
652 * be enabled.
653 *
654 * Note: We force all of the GPIO pins low first, *then*
655 * enable the ones we want.
656 */
657 csr_write_1(sc, AUE_GPIO0, AUE_GPIO_OUT0|AUE_GPIO_SEL0);
658 csr_write_1(sc, AUE_GPIO0, AUE_GPIO_OUT0|AUE_GPIO_SEL0|AUE_GPIO_SEL1);
659
660 /* Grrr. LinkSys has to be different from everyone else. */
661 if (sc->aue_vendor == USB_VENDOR_LINKSYS &&
662 sc->aue_product == USB_PRODUCT_LINKSYS_USB100TX) {
663 csr_write_1(sc, AUE_GPIO0, AUE_GPIO_SEL0|AUE_GPIO_SEL1);
664 csr_write_1(sc, AUE_GPIO0, AUE_GPIO_SEL0|AUE_GPIO_SEL1|
665 AUE_GPIO_OUT0);
666 }
667
668 /* Wait a little while for the chip to get its brains in order. */
669 DELAY(10000); /* XXX */
670 }
671
672 /*
673 * Probe for a Pegasus chip.
674 */
675 USB_MATCH(aue)
676 {
677 USB_MATCH_START(aue, uaa);
678 struct aue_type *t;
679
680 if (uaa->iface != NULL)
681 return (UMATCH_NONE);
682
683 for (t = aue_devs; t->aue_vid != 0; t++)
684 if (uaa->vendor == t->aue_vid && uaa->product == t->aue_did)
685 return (UMATCH_VENDOR_PRODUCT);
686
687 return (UMATCH_NONE);
688 }
689
690 /*
691 * Attach the interface. Allocate softc structures, do ifmedia
692 * setup and ethernet/BPF attach.
693 */
694 USB_ATTACH(aue)
695 {
696 USB_ATTACH_START(aue, sc, uaa);
697 char devinfo[1024];
698 int s;
699 u_char eaddr[ETHER_ADDR_LEN];
700 struct ifnet *ifp;
701 struct mii_data *mii;
702 usbd_device_handle dev = uaa->device;
703 usbd_interface_handle iface;
704 usb_interface_descriptor_t *id;
705 usb_endpoint_descriptor_t *ed;
706 usbd_status err;
707 int i;
708
709 #ifdef __FreeBSD__
710 bzero(sc, sizeof(struct aue_softc));
711 #endif
712
713 DPRINTFN(5,(" : uan_attach: sc=%p", sc));
714
715 usbd_devinfo(dev, 0, devinfo);
716 USB_ATTACH_SETUP;
717 printf("%s: %s\n", USBDEVNAME(sc->aue_dev), devinfo);
718
719 err = usbd_set_config_no(dev, AUE_CONFIG_NO, 0);
720 if (err) {
721 printf("%s: setting config no failed\n",
722 USBDEVNAME(sc->aue_dev));
723 USB_ATTACH_ERROR_RETURN;
724 }
725
726 err = usbd_device2interface_handle(dev, AUE_IFACE_IDX, &iface);
727 if (err) {
728 printf("%s: getting interface handle failed\n",
729 USBDEVNAME(sc->aue_dev));
730 USB_ATTACH_ERROR_RETURN;
731 }
732
733 sc->aue_udev = dev;
734 sc->aue_iface = iface;
735 sc->aue_product = uaa->product;
736 sc->aue_vendor = uaa->vendor;
737
738 id = usbd_get_interface_descriptor(iface);
739
740 /* Find endpoints. */
741 for (i = 0; i < id->bNumEndpoints; i++) {
742 ed = usbd_interface2endpoint_descriptor(iface, i);
743 if (!ed) {
744 printf("%s: couldn't get endpoint descriptor %d\n",
745 USBDEVNAME(sc->aue_dev), i);
746 USB_ATTACH_ERROR_RETURN;
747 }
748 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
749 (ed->bmAttributes & UE_XFERTYPE) == UE_BULK) {
750 sc->aue_ed[AUE_ENDPT_RX] = ed->bEndpointAddress;
751 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
752 (ed->bmAttributes & UE_XFERTYPE) == UE_BULK) {
753 sc->aue_ed[AUE_ENDPT_TX] = ed->bEndpointAddress;
754 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
755 (ed->bmAttributes & UE_XFERTYPE) == UE_INTERRUPT) {
756 sc->aue_ed[AUE_ENDPT_INTR] = ed->bEndpointAddress;
757 }
758 }
759
760 if (sc->aue_ed[AUE_ENDPT_RX] == 0 || sc->aue_ed[AUE_ENDPT_TX] == 0 ||
761 sc->aue_ed[AUE_ENDPT_INTR] == 0) {
762 printf("%s: missing endpoint\n", USBDEVNAME(sc->aue_dev));
763 USB_ATTACH_ERROR_RETURN;
764 }
765
766
767 s = splimp();
768
769 /* Reset the adapter. */
770 aue_reset(sc);
771
772 /*
773 * Get station address from the EEPROM.
774 */
775 aue_read_mac(sc, eaddr);
776
777 /*
778 * A Pegasus chip was detected. Inform the world.
779 */
780 #if defined(__FreeBSD__)
781 printf("%s: Ethernet address: %6D\n", USBDEVNAME(sc->aue_dev),
782 eaddr, ":");
783
784 bcopy(eaddr, (char *)&sc->arpcom.ac_enaddr, ETHER_ADDR_LEN);
785
786 ifp = &sc->arpcom.ac_if;
787 ifp->if_softc = sc;
788 ifp->if_unit = sc->aue_unit;
789 ifp->if_name = "aue";
790 ifp->if_mtu = ETHERMTU;
791 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
792 ifp->if_ioctl = aue_ioctl;
793 ifp->if_output = ether_output;
794 ifp->if_start = aue_start;
795 ifp->if_watchdog = aue_watchdog;
796 ifp->if_init = aue_init;
797 ifp->if_baudrate = 10000000;
798 ifp->if_snd.ifq_maxlen = IFQ_MAXLEN;
799
800 /*
801 * Do MII setup.
802 * NOTE: Doing this causes child devices to be attached to us,
803 * which we would normally disconnect at in the detach routine
804 * using device_delete_child(). However the USB code is set up
805 * such that when this driver is removed, all childred devices
806 * are removed as well. In effect, the USB code ends up detaching
807 * all of our children for us, so we don't have to do is ourselves
808 * in aue_detach(). It's important to point this out since if
809 * we *do* try to detach the child devices ourselves, we will
810 * end up getting the children deleted twice, which will crash
811 * the system.
812 */
813 if (mii_phy_probe(self, &sc->aue_miibus,
814 aue_ifmedia_upd, aue_ifmedia_sts)) {
815 printf("%s: MII without any PHY!\n", USBDEVNAME(sc->aue_dev));
816 splx(s);
817 USB_ATTACH_ERROR_RETURN;
818 }
819
820 aue_qdat.ifp = ifp;
821 aue_qdat.if_rxstart = aue_rxstart;
822
823 /*
824 * Call MI attach routines.
825 */
826 if_attach(ifp);
827 ether_ifattach(ifp);
828 callout_handle_init(&sc->aue_stat_ch);
829 bpfattach(ifp, DLT_EN10MB, sizeof(struct ether_header));
830
831 usb_register_netisr();
832
833 #elif defined(__NetBSD__) || defined(__OpenBSD__)
834
835 printf("%s: Ethernet address %s\n", USBDEVNAME(sc->aue_dev),
836 ether_sprintf(eaddr));
837
838 /* Initialize interface info.*/
839 ifp = &sc->aue_ec.ec_if;
840 ifp->if_softc = sc;
841 ifp->if_mtu = ETHERMTU;
842 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
843 ifp->if_ioctl = aue_ioctl;
844 ifp->if_start = aue_start;
845 ifp->if_watchdog = aue_watchdog;
846 ifp->if_baudrate = 10000000;
847 strncpy(ifp->if_xname, USBDEVNAME(sc->aue_dev), IFNAMSIZ);
848
849 /* Initialize MII/media info. */
850 mii = &sc->aue_mii;
851 mii->mii_ifp = ifp;
852 mii->mii_readreg = aue_miibus_readreg;
853 mii->mii_writereg = aue_miibus_writereg;
854 mii->mii_statchg = aue_miibus_statchg;
855 ifmedia_init(&mii->mii_media, 0, aue_ifmedia_upd, aue_ifmedia_sts);
856 mii_phy_probe(self, mii, 0xffffffff, MII_PHY_ANY, MII_OFFSET_ANY);
857 if (LIST_FIRST(&mii->mii_phys) == NULL) {
858 ifmedia_add(&mii->mii_media, IFM_ETHER | IFM_NONE, 0, NULL);
859 ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_NONE);
860 } else
861 ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_AUTO);
862
863 /* Attach the interface. */
864 if_attach(ifp);
865 ether_ifattach(ifp, eaddr);
866
867 #if NBPFILTER > 0
868 bpfattach(&ifp->if_bpf, ifp, DLT_EN10MB,
869 sizeof(struct ether_header));
870 #endif
871 #if RND > 0
872 rnd_attach_source(&sc->rnd_source, USBDEVNAME(sc->aue_dev),
873 RND_TYPE_NET, 0);
874 #endif
875
876 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
877
878 splx(s);
879
880 USB_ATTACH_SUCCESS_RETURN;
881 }
882
883 USB_DETACH(aue)
884 {
885 USB_DETACH_START(aue, sc);
886 #if defined(__FreeBSD__)
887 struct ifnet *ifp;
888 int s;
889
890 DPRINTFN(2,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __FUNCTION__));
891
892 s = splusb();
893
894 ifp = &sc->arpcom.ac_if;
895
896 usb_untimeout(aue_tick, sc, sc->aue_stat_ch);
897 if_detach(ifp);
898
899 if (sc->aue_ep[AUE_ENDPT_TX] != NULL)
900 usbd_abort_pipe(sc->aue_ep[AUE_ENDPT_TX]);
901 if (sc->aue_ep[AUE_ENDPT_RX] != NULL)
902 usbd_abort_pipe(sc->aue_ep[AUE_ENDPT_RX]);
903 if (sc->aue_ep[AUE_ENDPT_INTR] != NULL)
904 usbd_abort_pipe(sc->aue_ep[AUE_ENDPT_INTR]);
905
906 splx(s);
907
908 return (0);
909 #elif defined(__NetBSD__) || defined(__OpenBSD__)
910 sc = sc; /* XXX use sc */
911 /* XXX deallocate */
912
913 #ifdef notyet
914 /*
915 * Our softc is about to go away, so drop our refernce
916 * to the ifnet.
917 */
918 if_delref(sc->aue_ec.ec_if);
919 return (0);
920 #else
921 return (EBUSY);
922 #endif
923
924 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
925 }
926
927 #if defined(__NetBSD__) || defined(__OpenBSD__)
928 int
929 aue_activate(self, act)
930 device_ptr_t self;
931 enum devact act;
932 {
933 struct aue_softc *sc = (struct aue_softc *)self;
934
935 DPRINTFN(2,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __FUNCTION__));
936
937 switch (act) {
938 case DVACT_ACTIVATE:
939 return (EOPNOTSUPP);
940 break;
941
942 case DVACT_DEACTIVATE:
943 #ifdef notyet
944 /* First, kill off the interface. */
945 if_detach(sc->aue_ec.ec_if);
946 #endif
947 sc->aue_dying = 1;
948 break;
949 }
950 return (0);
951 }
952 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
953
954 /*
955 * Initialize an RX descriptor and attach an MBUF cluster.
956 */
957 static int
958 aue_newbuf(sc, c, m)
959 struct aue_softc *sc;
960 struct aue_chain *c;
961 struct mbuf *m;
962 {
963 struct mbuf *m_new = NULL;
964
965 DPRINTFN(10,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev),__FUNCTION__));
966
967 if (m == NULL) {
968 MGETHDR(m_new, M_DONTWAIT, MT_DATA);
969 if (m_new == NULL) {
970 printf("%s: no memory for rx list "
971 "-- packet dropped!\n", USBDEVNAME(sc->aue_dev));
972 return (ENOBUFS);
973 }
974
975 MCLGET(m_new, M_DONTWAIT);
976 if (!(m_new->m_flags & M_EXT)) {
977 printf("%s: no memory for rx list "
978 "-- packet dropped!\n", USBDEVNAME(sc->aue_dev));
979 m_freem(m_new);
980 return (ENOBUFS);
981 }
982 m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
983 } else {
984 m_new = m;
985 m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
986 m_new->m_data = m_new->m_ext.ext_buf;
987 }
988
989 m_adj(m_new, ETHER_ALIGN);
990 c->aue_mbuf = m_new;
991
992 return (0);
993 }
994
995 static int
996 aue_rx_list_init(sc)
997 struct aue_softc *sc;
998 {
999 struct aue_cdata *cd;
1000 struct aue_chain *c;
1001 int i;
1002
1003 DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __FUNCTION__));
1004
1005 cd = &sc->aue_cdata;
1006 for (i = 0; i < AUE_RX_LIST_CNT; i++) {
1007 c = &cd->aue_rx_chain[i];
1008 c->aue_sc = sc;
1009 c->aue_idx = i;
1010 c->aue_accum = 0;
1011 if (aue_newbuf(sc, c, NULL) == ENOBUFS)
1012 return (ENOBUFS);
1013 if (c->aue_xfer == NULL) {
1014 c->aue_xfer = usbd_alloc_xfer(sc->aue_udev);
1015 if (c->aue_xfer == NULL)
1016 return (ENOBUFS);
1017 c->aue_buf = usbd_alloc_buffer(c->aue_xfer, AUE_BUFSZ);
1018 if (c->aue_buf == NULL)
1019 return (ENOBUFS); /* XXX free xfer */
1020 }
1021 }
1022
1023 return (0);
1024 }
1025
1026 static int
1027 aue_tx_list_init(sc)
1028 struct aue_softc *sc;
1029 {
1030 struct aue_cdata *cd;
1031 struct aue_chain *c;
1032 int i;
1033
1034 DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __FUNCTION__));
1035
1036 cd = &sc->aue_cdata;
1037 for (i = 0; i < AUE_TX_LIST_CNT; i++) {
1038 c = &cd->aue_tx_chain[i];
1039 c->aue_sc = sc;
1040 c->aue_idx = i;
1041 c->aue_mbuf = NULL;
1042 if (c->aue_xfer == NULL) {
1043 c->aue_xfer = usbd_alloc_xfer(sc->aue_udev);
1044 if (c->aue_xfer == NULL)
1045 return (ENOBUFS);
1046 c->aue_buf = usbd_alloc_buffer(c->aue_xfer, AUE_BUFSZ);
1047 if (c->aue_buf == NULL)
1048 return (ENOBUFS);
1049 }
1050 }
1051
1052 return (0);
1053 }
1054
1055 static void
1056 aue_intr(xfer, priv, status)
1057 usbd_xfer_handle xfer;
1058 usbd_private_handle priv;
1059 usbd_status status;
1060 {
1061 struct aue_softc *sc = priv;
1062 struct ifnet *ifp = GET_IFP(sc);
1063 struct aue_intrpkt *p = &sc->aue_cdata.aue_ibuf;
1064
1065 DPRINTFN(15,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev),__FUNCTION__));
1066
1067 if (!(ifp->if_flags & IFF_RUNNING))
1068 return;
1069
1070 if (status != USBD_NORMAL_COMPLETION) {
1071 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
1072 return;
1073 }
1074 printf("%s: usb error on intr: %s\n", USBDEVNAME(sc->aue_dev),
1075 usbd_errstr(status));
1076 if (status == USBD_STALLED)
1077 usbd_clear_endpoint_stall(sc->aue_ep[AUE_ENDPT_RX]);
1078 return;
1079 }
1080
1081 if (p->aue_txstat0)
1082 ifp->if_oerrors++;
1083
1084 if (p->aue_txstat0 & (AUE_TXSTAT0_LATECOLL | AUE_TXSTAT0_EXCESSCOLL))
1085 ifp->if_collisions++;
1086 }
1087
1088 #if defined(__FreeBSD__)
1089 static void
1090 aue_rxstart(ifp)
1091 struct ifnet *ifp;
1092 {
1093 struct aue_softc *sc;
1094 struct aue_chain *c;
1095
1096 sc = ifp->if_softc;
1097 c = &sc->aue_cdata.aue_rx_chain[sc->aue_cdata.aue_rx_prod];
1098
1099 if (aue_newbuf(sc, c, NULL) == ENOBUFS) {
1100 ifp->if_ierrors++;
1101 return;
1102 }
1103
1104 /* Setup new transfer. */
1105 usbd_setup_xfer(c->aue_xfer, sc->aue_ep[AUE_ENDPT_RX],
1106 c, mtod(c->aue_mbuf, char *), AUE_CUTOFF, USBD_SHORT_XFER_OK,
1107 USBD_NO_TIMEOUT, aue_rxeof);
1108 usbd_transfer(c->aue_xfer);
1109 }
1110 #endif
1111
1112 /*
1113 * A frame has been uploaded: pass the resulting mbuf chain up to
1114 * the higher level protocols.
1115 *
1116 * Grrr. Receiving transfers larger than about 1152 bytes sometimes
1117 * doesn't work. We get an incomplete frame. In order to avoid
1118 * this, we queue up RX transfers that are shorter than a full sized
1119 * frame. If the received frame is larger than our transfer size,
1120 * we snag the rest of the data using a second transfer. Does this
1121 * hurt performance? Yes. But after fighting with this stupid thing
1122 * for three days, I'm willing to settle. I'd rather have reliable
1123 * receive performance that fast but spotty performance.
1124 */
1125 static void
1126 aue_rxeof(xfer, priv, status)
1127 usbd_xfer_handle xfer;
1128 usbd_private_handle priv;
1129 usbd_status status;
1130 {
1131 struct aue_chain *c = priv;
1132 struct aue_softc *sc = c->aue_sc;
1133 struct ifnet *ifp = GET_IFP(sc);
1134 struct mbuf *m;
1135 u_int32_t total_len;
1136 struct aue_rxpkt r;
1137 #if defined(__NetBSD__) || defined(__OpenBSD__)
1138 int s;
1139 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
1140
1141 DPRINTFN(10,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev),__FUNCTION__));
1142
1143 if (!(ifp->if_flags & IFF_RUNNING))
1144 return;
1145
1146 if (status != USBD_NORMAL_COMPLETION) {
1147 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
1148 return;
1149 printf("%s: usb error on rx: %s\n", USBDEVNAME(sc->aue_dev),
1150 usbd_errstr(status));
1151 if (status == USBD_STALLED)
1152 usbd_clear_endpoint_stall(sc->aue_ep[AUE_ENDPT_RX]);
1153 goto done;
1154 }
1155
1156 usbd_get_xfer_status(xfer, NULL, NULL, &total_len, NULL);
1157
1158 /* XXX copy data to mbuf */
1159 memcpy(mtod(c->aue_mbuf, char*) + c->aue_accum, c->aue_buf, total_len);
1160
1161 /*
1162 * See if we've already accumulated some data from
1163 * a previous transfer.
1164 */
1165 if (c->aue_accum) {
1166 total_len += c->aue_accum;
1167 c->aue_accum = 0;
1168 }
1169
1170 if (total_len <= 4 + ETHER_CRC_LEN) {
1171 ifp->if_ierrors++;
1172 goto done;
1173 }
1174
1175 m = c->aue_mbuf;
1176 memcpy(&r, mtod(m, char *) + total_len - 4, sizeof(r));
1177
1178 /* Turn off all the non-error bits in the rx status word. */
1179 r.aue_rxstat &= AUE_RXSTAT_MASK;
1180
1181 /*
1182 * Check to see if this is just the first chunk of a
1183 * split transfer. We really need a more reliable way
1184 * to detect this.
1185 */
1186 if (UGETW(r.aue_pktlen) != total_len && total_len == AUE_CUTOFF) {
1187 c->aue_accum = AUE_CUTOFF;
1188 usbd_setup_xfer(xfer, sc->aue_ep[AUE_ENDPT_RX],
1189 c, c->aue_buf,
1190 AUE_CUTOFF, USBD_SHORT_XFER_OK,
1191 USBD_NO_TIMEOUT, aue_rxeof);
1192 DPRINTFN(5,("%s: %s: extra rx\n", USBDEVNAME(sc->aue_dev),
1193 __FUNCTION__));
1194 usbd_transfer(xfer);
1195 return;
1196 }
1197
1198 if (r.aue_rxstat) {
1199 ifp->if_ierrors++;
1200 goto done;
1201 }
1202
1203 /* No errors; receive the packet. */
1204 total_len -= ETHER_CRC_LEN + 4;
1205 m->m_pkthdr.len = m->m_len = total_len;
1206 ifp->if_ipackets++;
1207
1208 #if defined(__FreeBSD__)
1209 /* Put the packet on the special USB input queue. */
1210 usb_ether_input(m);
1211 return;
1212 #elif defined(__NetBSD__) || defined(__OpenBSD__)
1213 m->m_pkthdr.rcvif = ifp;
1214
1215 s = splimp();
1216
1217 /* XXX ugly */
1218 if (aue_newbuf(sc, c, NULL) == ENOBUFS) {
1219 ifp->if_ierrors++;
1220 goto done1;
1221 }
1222
1223 /*
1224 * Handle BPF listeners. Let the BPF user see the packet, but
1225 * don't pass it up to the ether_input() layer unless it's
1226 * a broadcast packet, multicast packet, matches our ethernet
1227 * address or the interface is in promiscuous mode.
1228 */
1229 if (ifp->if_bpf) {
1230 struct ether_header *eh = mtod(m, struct ether_header *);
1231 bpf_mtap(ifp, m);
1232 if ((ifp->if_flags & IFF_PROMISC) &&
1233 memcmp(eh->ether_dhost, LLADDR(ifp->if_sadl),
1234 ETHER_ADDR_LEN) &&
1235 !(eh->ether_dhost[0] & 1)) {
1236 m_freem(m);
1237 goto done1;
1238 }
1239 }
1240
1241 DPRINTFN(10,("%s: %s: deliver %d\n", USBDEVNAME(sc->aue_dev),
1242 __FUNCTION__, m->m_len));
1243 (*ifp->if_input)(ifp, m);
1244 done1:
1245 splx(s);
1246 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
1247
1248 done:
1249
1250 /* Setup new transfer. */
1251 usbd_setup_xfer(xfer, sc->aue_ep[AUE_ENDPT_RX],
1252 c, c->aue_buf, AUE_CUTOFF,
1253 USBD_SHORT_XFER_OK | USBD_NO_COPY,
1254 USBD_NO_TIMEOUT, aue_rxeof);
1255 usbd_transfer(xfer);
1256 DPRINTFN(10,("%s: %s: start rx\n", USBDEVNAME(sc->aue_dev),
1257 __FUNCTION__));
1258 }
1259
1260 /*
1261 * A frame was downloaded to the chip. It's safe for us to clean up
1262 * the list buffers.
1263 */
1264
1265 static void
1266 aue_txeof(xfer, priv, status)
1267 usbd_xfer_handle xfer;
1268 usbd_private_handle priv;
1269 usbd_status status;
1270 {
1271 struct aue_chain *c = priv;
1272 struct aue_softc *sc = c->aue_sc;
1273 struct ifnet *ifp = GET_IFP(sc);
1274 int s;
1275
1276 s = splimp();
1277
1278 DPRINTFN(10,("%s: %s: enter status=%d\n", USBDEVNAME(sc->aue_dev),
1279 __FUNCTION__, status));
1280
1281 if (status != USBD_NORMAL_COMPLETION) {
1282 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
1283 splx(s);
1284 return;
1285 }
1286 ifp->if_oerrors++;
1287 printf("%s: usb error on tx: %s\n", USBDEVNAME(sc->aue_dev),
1288 usbd_errstr(status));
1289 if (status == USBD_STALLED)
1290 usbd_clear_endpoint_stall(sc->aue_ep[AUE_ENDPT_TX]);
1291 splx(s);
1292 return;
1293 }
1294
1295 ifp->if_timer = 0;
1296 ifp->if_flags &= ~IFF_OACTIVE;
1297
1298 ifp->if_opackets++;
1299
1300 #if defined(__FreeBSD__)
1301 c->aue_mbuf->m_pkthdr.rcvif = ifp;
1302 usb_tx_done(c->aue_mbuf);
1303 c->aue_mbuf = NULL;
1304 #elif defined(__NetBSD__) || defined(__OpenBSD__)
1305 m_freem(c->aue_mbuf);
1306 c->aue_mbuf = NULL;
1307
1308 if (ifp->if_snd.ifq_head != NULL)
1309 aue_start(ifp);
1310 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
1311
1312 splx(s);
1313 }
1314
1315 static void
1316 aue_tick(xsc)
1317 void *xsc;
1318 {
1319 struct aue_softc *sc = xsc;
1320 struct ifnet *ifp;
1321 struct mii_data *mii;
1322 int s;
1323
1324 DPRINTFN(15,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev),__FUNCTION__));
1325
1326 if (sc == NULL)
1327 return;
1328
1329 ifp = GET_IFP(sc);
1330 mii = GET_MII(sc);
1331 if (mii == NULL)
1332 return;
1333
1334 s = splimp();
1335
1336 mii_tick(mii);
1337 if (!sc->aue_link) {
1338 mii_pollstat(mii);
1339 if (mii->mii_media_status & IFM_ACTIVE &&
1340 IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
1341 DPRINTFN(2,("%s: %s: got link\n",
1342 USBDEVNAME(sc->aue_dev),__FUNCTION__));
1343 sc->aue_link++;
1344 if (ifp->if_snd.ifq_head != NULL)
1345 aue_start(ifp);
1346 }
1347 }
1348
1349 usb_timeout(aue_tick, sc, hz, sc->aue_stat_ch);
1350
1351 splx(s);
1352 }
1353
1354 static int
1355 aue_send(sc, m, idx)
1356 struct aue_softc *sc;
1357 struct mbuf *m;
1358 int idx;
1359 {
1360 int total_len;
1361 struct aue_chain *c;
1362 usbd_status err;
1363
1364 DPRINTFN(10,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev),__FUNCTION__));
1365
1366 c = &sc->aue_cdata.aue_tx_chain[idx];
1367
1368 /*
1369 * Copy the mbuf data into a contiguous buffer, leaving two
1370 * bytes at the beginning to hold the frame length.
1371 */
1372 m_copydata(m, 0, m->m_pkthdr.len, c->aue_buf + 2);
1373 c->aue_mbuf = m;
1374
1375 /*
1376 * The ADMtek documentation says that the packet length is
1377 * supposed to be specified in the first two bytes of the
1378 * transfer, however it actually seems to ignore this info
1379 * and base the frame size on the bulk transfer length.
1380 */
1381 c->aue_buf[0] = (u_int8_t)m->m_pkthdr.len;
1382 c->aue_buf[1] = (u_int8_t)(m->m_pkthdr.len >> 3) & 0xE0;
1383 total_len = m->m_pkthdr.len + 2;
1384
1385 usbd_setup_xfer(c->aue_xfer, sc->aue_ep[AUE_ENDPT_TX],
1386 c, c->aue_buf, total_len, USBD_FORCE_SHORT_XFER | USBD_NO_COPY,
1387 AUE_TX_TIMEOUT, aue_txeof);
1388
1389 /* Transmit */
1390 err = usbd_transfer(c->aue_xfer);
1391 if (err != USBD_IN_PROGRESS) {
1392 aue_stop(sc);
1393 return (EIO);
1394 }
1395 DPRINTFN(5,("%s: %s: send %d bytes\n", USBDEVNAME(sc->aue_dev),
1396 __FUNCTION__, total_len));
1397
1398 sc->aue_cdata.aue_tx_cnt++;
1399
1400 return (0);
1401 }
1402
1403 static void
1404 aue_start(ifp)
1405 struct ifnet *ifp;
1406 {
1407 struct aue_softc *sc = ifp->if_softc;
1408 struct mbuf *m_head = NULL;
1409
1410 DPRINTFN(5,("%s: %s: enter, link=%d\n", USBDEVNAME(sc->aue_dev),
1411 __FUNCTION__, sc->aue_link));
1412
1413 if (!sc->aue_link)
1414 return;
1415
1416 if (ifp->if_flags & IFF_OACTIVE)
1417 return;
1418
1419 IF_DEQUEUE(&ifp->if_snd, m_head);
1420 if (m_head == NULL)
1421 return;
1422
1423 if (aue_send(sc, m_head, 0)) {
1424 IF_PREPEND(&ifp->if_snd, m_head);
1425 ifp->if_flags |= IFF_OACTIVE;
1426 return;
1427 }
1428
1429 /*
1430 * If there's a BPF listener, bounce a copy of this frame
1431 * to him.
1432 */
1433 if (ifp->if_bpf)
1434 bpf_mtap(ifp, m_head);
1435
1436 ifp->if_flags |= IFF_OACTIVE;
1437
1438 /*
1439 * Set a timeout in case the chip goes out to lunch.
1440 */
1441 ifp->if_timer = 5;
1442 }
1443
1444 static void
1445 aue_init(xsc)
1446 void *xsc;
1447 {
1448 struct aue_softc *sc = xsc;
1449 struct ifnet *ifp = GET_IFP(sc);
1450 struct mii_data *mii = GET_MII(sc);
1451 struct aue_chain *c;
1452 usbd_status err;
1453 int i, s;
1454 u_char *eaddr;
1455
1456 DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __FUNCTION__));
1457
1458 if (ifp->if_flags & IFF_RUNNING)
1459 return;
1460
1461 s = splimp();
1462
1463 /*
1464 * Cancel pending I/O and free all RX/TX buffers.
1465 */
1466 aue_reset(sc);
1467
1468 #if defined(__FreeBSD__)
1469 eaddr = sc->arpcom.ac_enaddr;
1470 #elif defined(__NetBSD__) || defined(__OpenBSD__)
1471 eaddr = LLADDR(ifp->if_sadl);
1472 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
1473 for (i = 0; i < ETHER_ADDR_LEN; i++)
1474 csr_write_1(sc, AUE_PAR0 + i, eaddr[i]);
1475
1476 /* If we want promiscuous mode, set the allframes bit. */
1477 if (ifp->if_flags & IFF_PROMISC)
1478 AUE_SETBIT(sc, AUE_CTL2, AUE_CTL2_RX_PROMISC);
1479 else
1480 AUE_CLRBIT(sc, AUE_CTL2, AUE_CTL2_RX_PROMISC);
1481
1482 /* Init TX ring. */
1483 if (aue_tx_list_init(sc) == ENOBUFS) {
1484 printf("%s: tx list init failed\n", USBDEVNAME(sc->aue_dev));
1485 splx(s);
1486 return;
1487 }
1488
1489 /* Init RX ring. */
1490 if (aue_rx_list_init(sc) == ENOBUFS) {
1491 printf("%s: rx list init failed\n", USBDEVNAME(sc->aue_dev));
1492 splx(s);
1493 return;
1494 }
1495
1496 /* Load the multicast filter. */
1497 aue_setmulti(sc);
1498
1499 /* Enable RX and TX */
1500 csr_write_1(sc, AUE_CTL0, AUE_CTL0_RXSTAT_APPEND|AUE_CTL0_RX_ENB);
1501 AUE_SETBIT(sc, AUE_CTL0, AUE_CTL0_TX_ENB);
1502 AUE_SETBIT(sc, AUE_CTL2, AUE_CTL2_EP3_CLR);
1503
1504 mii_mediachg(mii);
1505
1506 if (sc->aue_ep[AUE_ENDPT_RX] == NULL) {
1507 /* Open RX and TX pipes. */
1508 err = usbd_open_pipe(sc->aue_iface, sc->aue_ed[AUE_ENDPT_RX],
1509 USBD_EXCLUSIVE_USE, &sc->aue_ep[AUE_ENDPT_RX]);
1510 if (err) {
1511 printf("%s: open rx pipe failed: %s\n",
1512 USBDEVNAME(sc->aue_dev), usbd_errstr(err));
1513 splx(s);
1514 return;
1515 }
1516 usbd_open_pipe(sc->aue_iface, sc->aue_ed[AUE_ENDPT_TX],
1517 USBD_EXCLUSIVE_USE, &sc->aue_ep[AUE_ENDPT_TX]);
1518 if (err) {
1519 printf("%s: open tx pipe failed: %s\n",
1520 USBDEVNAME(sc->aue_dev), usbd_errstr(err));
1521 splx(s);
1522 return;
1523 }
1524 err = usbd_open_pipe_intr(sc->aue_iface, sc->aue_ed[AUE_ENDPT_INTR],
1525 USBD_EXCLUSIVE_USE, &sc->aue_ep[AUE_ENDPT_INTR], sc,
1526 &sc->aue_cdata.aue_ibuf, AUE_INTR_PKTLEN, aue_intr);
1527 if (err) {
1528 printf("%s: open intr pipe failed: %s\n",
1529 USBDEVNAME(sc->aue_dev), usbd_errstr(err));
1530 splx(s);
1531 return;
1532 }
1533
1534 /* Start up the receive pipe. */
1535 for (i = 0; i < AUE_RX_LIST_CNT; i++) {
1536 c = &sc->aue_cdata.aue_rx_chain[i];
1537 usbd_setup_xfer(c->aue_xfer, sc->aue_ep[AUE_ENDPT_RX],
1538 c, c->aue_buf, AUE_CUTOFF,
1539 USBD_SHORT_XFER_OK | USBD_NO_COPY, USBD_NO_TIMEOUT,
1540 aue_rxeof);
1541 usbd_transfer(c->aue_xfer);
1542 DPRINTFN(5,("%s: %s: start read\n", USBDEVNAME(sc->aue_dev),
1543 __FUNCTION__));
1544
1545 }
1546 }
1547
1548 ifp->if_flags |= IFF_RUNNING;
1549 ifp->if_flags &= ~IFF_OACTIVE;
1550
1551 splx(s);
1552
1553 usb_untimeout(aue_tick, sc, sc->aue_stat_ch);
1554 usb_timeout(aue_tick, sc, hz, sc->aue_stat_ch);
1555 }
1556
1557 /*
1558 * Set media options.
1559 */
1560 static int
1561 aue_ifmedia_upd(ifp)
1562 struct ifnet *ifp;
1563 {
1564 struct aue_softc *sc = ifp->if_softc;
1565 struct mii_data *mii = GET_MII(sc);
1566
1567 DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __FUNCTION__));
1568
1569 sc->aue_link = 0;
1570 if (mii->mii_instance) {
1571 struct mii_softc *miisc;
1572 for (miisc = LIST_FIRST(&mii->mii_phys); miisc != NULL;
1573 miisc = LIST_NEXT(miisc, mii_list))
1574 mii_phy_reset(miisc);
1575 }
1576 mii_mediachg(mii);
1577
1578 return (0);
1579 }
1580
1581 /*
1582 * Report current media status.
1583 */
1584 static void
1585 aue_ifmedia_sts(ifp, ifmr)
1586 struct ifnet *ifp;
1587 struct ifmediareq *ifmr;
1588 {
1589 struct aue_softc *sc = ifp->if_softc;
1590 struct mii_data *mii = GET_MII(sc);
1591
1592 DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __FUNCTION__));
1593
1594 mii_pollstat(mii);
1595 ifmr->ifm_active = mii->mii_media_active;
1596 ifmr->ifm_status = mii->mii_media_status;
1597 }
1598
1599 static int
1600 aue_ioctl(ifp, command, data)
1601 struct ifnet *ifp;
1602 u_long command;
1603 caddr_t data;
1604 {
1605 struct aue_softc *sc = ifp->if_softc;
1606 #if defined(__NetBSD__) || defined(__OpenBSD__)
1607 struct ifaddr *ifa = (struct ifaddr *)data;
1608 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
1609 struct ifreq *ifr = (struct ifreq *)data;
1610 struct mii_data *mii;
1611 int s, error = 0;
1612
1613 s = splimp();
1614
1615 switch(command) {
1616 #if defined(__FreeBSD__)
1617 case SIOCSIFADDR:
1618 case SIOCGIFADDR:
1619 case SIOCSIFMTU:
1620 error = ether_ioctl(ifp, command, data);
1621 break;
1622 #elif defined(__NetBSD__) || defined(__OpenBSD__)
1623 case SIOCSIFADDR:
1624 ifp->if_flags |= IFF_UP;
1625 aue_init(sc);
1626
1627 switch (ifa->ifa_addr->sa_family) {
1628 #ifdef INET
1629 case AF_INET:
1630 arp_ifinit(ifp, ifa);
1631 break;
1632 #endif /* INET */
1633 #ifdef NS
1634 case AF_NS:
1635 {
1636 struct ns_addr *ina = &IA_SNS(ifa)->sns_addr;
1637
1638 if (ns_nullhost(*ina))
1639 ina->x_hsot = *(union ns_host *)
1640 LLADDR(ifp->if_sadl);
1641 else
1642 memcpy(LLADDR(ifp->if_sadl),
1643 ina->x_host.c_host,
1644 ifp->if_addrlen);
1645 break;
1646 }
1647 #endif /* NS */
1648 }
1649 break;
1650
1651 case SIOCSIFMTU:
1652 if (ifr->ifr_mtu > ETHERMTU)
1653 error = EINVAL;
1654 else
1655 ifp->if_mtu = ifr->ifr_mtu;
1656 break;
1657
1658 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
1659 case SIOCSIFFLAGS:
1660 if (ifp->if_flags & IFF_UP) {
1661 if (ifp->if_flags & IFF_RUNNING &&
1662 ifp->if_flags & IFF_PROMISC &&
1663 !(sc->aue_if_flags & IFF_PROMISC)) {
1664 AUE_SETBIT(sc, AUE_CTL2, AUE_CTL2_RX_PROMISC);
1665 } else if (ifp->if_flags & IFF_RUNNING &&
1666 !(ifp->if_flags & IFF_PROMISC) &&
1667 sc->aue_if_flags & IFF_PROMISC) {
1668 AUE_CLRBIT(sc, AUE_CTL2, AUE_CTL2_RX_PROMISC);
1669 } else if (!(ifp->if_flags & IFF_RUNNING))
1670 aue_init(sc);
1671 } else {
1672 if (ifp->if_flags & IFF_RUNNING)
1673 aue_stop(sc);
1674 }
1675 sc->aue_if_flags = ifp->if_flags;
1676 error = 0;
1677 break;
1678 case SIOCADDMULTI:
1679 case SIOCDELMULTI:
1680 aue_setmulti(sc);
1681 error = 0;
1682 break;
1683 case SIOCGIFMEDIA:
1684 case SIOCSIFMEDIA:
1685 mii = GET_MII(sc);
1686 error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, command);
1687 break;
1688 default:
1689 error = EINVAL;
1690 break;
1691 }
1692
1693 splx(s);
1694
1695 return (error);
1696 }
1697
1698 static void
1699 aue_watchdog(ifp)
1700 struct ifnet *ifp;
1701 {
1702 struct aue_softc *sc = ifp->if_softc;
1703
1704 DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __FUNCTION__));
1705
1706 ifp->if_oerrors++;
1707 printf("%s: watchdog timeout\n", USBDEVNAME(sc->aue_dev));
1708
1709 /*
1710 * The polling business is a kludge to avoid allowing the
1711 * USB code to call tsleep() in usbd_delay_ms(), which will
1712 * kill us since the watchdog routine is invoked from
1713 * interrupt context.
1714 */
1715 usbd_set_polling(sc->aue_udev, 1);
1716 aue_stop(sc);
1717 aue_init(sc);
1718 usbd_set_polling(sc->aue_udev, 0);
1719
1720 if (ifp->if_snd.ifq_head != NULL)
1721 aue_start(ifp);
1722 }
1723
1724 /*
1725 * Stop the adapter and free any mbufs allocated to the
1726 * RX and TX lists.
1727 */
1728 static void
1729 aue_stop(sc)
1730 struct aue_softc *sc;
1731 {
1732 usbd_status err;
1733 struct ifnet *ifp;
1734 int i;
1735
1736 DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __FUNCTION__));
1737
1738 ifp = GET_IFP(sc);
1739 ifp->if_timer = 0;
1740
1741 csr_write_1(sc, AUE_CTL0, 0);
1742 csr_write_1(sc, AUE_CTL1, 0);
1743 aue_reset(sc);
1744 usb_untimeout(aue_tick, sc, sc->aue_stat_ch);
1745
1746 /* Stop transfers. */
1747 if (sc->aue_ep[AUE_ENDPT_RX] != NULL) {
1748 err = usbd_abort_pipe(sc->aue_ep[AUE_ENDPT_RX]);
1749 if (err) {
1750 printf("%s: abort rx pipe failed: %s\n",
1751 USBDEVNAME(sc->aue_dev), usbd_errstr(err));
1752 }
1753 err = usbd_close_pipe(sc->aue_ep[AUE_ENDPT_RX]);
1754 if (err) {
1755 printf("%s: close rx pipe failed: %s\n",
1756 USBDEVNAME(sc->aue_dev), usbd_errstr(err));
1757 }
1758 sc->aue_ep[AUE_ENDPT_RX] = NULL;
1759 }
1760
1761 if (sc->aue_ep[AUE_ENDPT_TX] != NULL) {
1762 err = usbd_abort_pipe(sc->aue_ep[AUE_ENDPT_TX]);
1763 if (err) {
1764 printf("%s: abort tx pipe failed: %s\n",
1765 USBDEVNAME(sc->aue_dev), usbd_errstr(err));
1766 }
1767 err = usbd_close_pipe(sc->aue_ep[AUE_ENDPT_TX]);
1768 if (err) {
1769 printf("%s: close tx pipe failed: %s\n",
1770 USBDEVNAME(sc->aue_dev), usbd_errstr(err));
1771 }
1772 sc->aue_ep[AUE_ENDPT_TX] = NULL;
1773 }
1774
1775 if (sc->aue_ep[AUE_ENDPT_INTR] != NULL) {
1776 err = usbd_abort_pipe(sc->aue_ep[AUE_ENDPT_INTR]);
1777 if (err) {
1778 printf("%s: abort intr pipe failed: %s\n",
1779 USBDEVNAME(sc->aue_dev), usbd_errstr(err));
1780 }
1781 err = usbd_close_pipe(sc->aue_ep[AUE_ENDPT_INTR]);
1782 if (err) {
1783 printf("%s: close intr pipe failed: %s\n",
1784 USBDEVNAME(sc->aue_dev), usbd_errstr(err));
1785 }
1786 sc->aue_ep[AUE_ENDPT_INTR] = NULL;
1787 }
1788
1789 /* Free RX resources. */
1790 for (i = 0; i < AUE_RX_LIST_CNT; i++) {
1791 if (sc->aue_cdata.aue_rx_chain[i].aue_mbuf != NULL) {
1792 m_freem(sc->aue_cdata.aue_rx_chain[i].aue_mbuf);
1793 sc->aue_cdata.aue_rx_chain[i].aue_mbuf = NULL;
1794 }
1795 if (sc->aue_cdata.aue_rx_chain[i].aue_xfer != NULL) {
1796 usbd_free_xfer(sc->aue_cdata.aue_rx_chain[i].aue_xfer);
1797 sc->aue_cdata.aue_rx_chain[i].aue_xfer = NULL;
1798 }
1799 }
1800
1801 /* Free TX resources. */
1802 for (i = 0; i < AUE_TX_LIST_CNT; i++) {
1803 if (sc->aue_cdata.aue_tx_chain[i].aue_mbuf != NULL) {
1804 m_freem(sc->aue_cdata.aue_tx_chain[i].aue_mbuf);
1805 sc->aue_cdata.aue_tx_chain[i].aue_mbuf = NULL;
1806 }
1807 if (sc->aue_cdata.aue_tx_chain[i].aue_xfer != NULL) {
1808 usbd_free_xfer(sc->aue_cdata.aue_tx_chain[i].aue_xfer);
1809 sc->aue_cdata.aue_tx_chain[i].aue_xfer = NULL;
1810 }
1811 }
1812
1813 sc->aue_link = 0;
1814
1815 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
1816 }
1817
1818 #ifdef __FreeBSD__
1819 /*
1820 * Stop all chip I/O so that the kernel's probe routines don't
1821 * get confused by errant DMAs when rebooting.
1822 */
1823 static void
1824 aue_shutdown(dev)
1825 device_ptr_t dev;
1826 {
1827 struct aue_softc *sc = USBGETSOFTC(dev);
1828
1829 DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __FUNCTION__));
1830
1831 aue_reset(sc);
1832 aue_stop(sc);
1833 }
1834 #endif
1835