if_aue.c revision 1.28 1 /* $NetBSD: if_aue.c,v 1.28 2000/03/06 21:02:03 thorpej 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 * proper cleanup on errors
77 */
78
79 #if defined(__NetBSD__) || defined(__OpenBSD__)
80 #include "opt_inet.h"
81 #include "opt_ns.h"
82 #include "bpfilter.h"
83 #include "rnd.h"
84 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
85
86 #include <sys/param.h>
87 #include <sys/systm.h>
88 #include <sys/sockio.h>
89 #include <sys/mbuf.h>
90 #include <sys/malloc.h>
91 #include <sys/kernel.h>
92 #include <sys/socket.h>
93
94 #if defined(__FreeBSD__)
95
96 #include <net/ethernet.h>
97 #include <machine/clock.h> /* for DELAY */
98 #include <sys/bus.h>
99 /* "controller miibus0" required. See GENERIC if you get errors here. */
100 #include "miibus_if.h"
101
102 #elif defined(__NetBSD__) || defined(__OpenBSD__)
103
104 #include <sys/device.h>
105 #if NRND > 0
106 #include <sys/rnd.h>
107 #endif
108
109 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
110
111 #include <net/if.h>
112 #include <net/if_arp.h>
113 #include <net/if_dl.h>
114 #include <net/if_media.h>
115
116 #if defined(__NetBSD__) || defined(__OpenBSD__)
117 #include <net/if_ether.h>
118 #define BPF_MTAP(ifp, m) bpf_mtap((ifp)->if_bpf, (m))
119 #else
120 #define BPF_MTAP(ifp, m) bpf_mtap((ifp), (m))
121 #endif
122
123 #if defined(__FreeBSD__) || NBPFILTER > 0
124 #include <net/bpf.h>
125 #endif
126
127 #if defined(__NetBSD__) || defined(__OpenBSD__)
128 #ifdef INET
129 #include <netinet/in.h>
130 #include <netinet/if_inarp.h>
131 #endif
132
133 #ifdef NS
134 #include <netns/ns.h>
135 #include <netns/ns_if.h>
136 #endif
137 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
138
139 #include <dev/mii/mii.h>
140 #include <dev/mii/miivar.h>
141
142 #include <dev/usb/usb.h>
143 #include <dev/usb/usbdi.h>
144 #include <dev/usb/usbdi_util.h>
145 #include <dev/usb/usbdevs.h>
146
147 #ifdef __FreeBSD__
148 #include <dev/usb/usb_ethersubr.h>
149 #endif
150
151 #include <dev/usb/if_auereg.h>
152
153 #ifdef AUE_DEBUG
154 #define DPRINTF(x) if (auedebug) logprintf x
155 #define DPRINTFN(n,x) if (auedebug >= (n)) logprintf x
156 int auedebug = 0;
157 #else
158 #define DPRINTF(x)
159 #define DPRINTFN(n,x)
160 #endif
161
162 /*
163 * Various supported device vendors/products.
164 */
165 static struct aue_type aue_devs[] = {
166 { USB_VENDOR_BILLIONTON, USB_PRODUCT_BILLIONTON_USB100 },
167 { USB_VENDOR_MELCO, USB_PRODUCT_MELCO_LUATX },
168 { USB_VENDOR_LINKSYS, USB_PRODUCT_LINKSYS_USB100TX },
169 { USB_VENDOR_ADMTEK, USB_PRODUCT_ADMTEK_PEGASUS },
170 { USB_VENDOR_DLINK, USB_PRODUCT_DLINK_DSB650TX },
171 { USB_VENDOR_DLINK, USB_PRODUCT_DLINK_DSB650TX_PNA },
172 { USB_VENDOR_SMC, USB_PRODUCT_SMC_2202USB },
173 { USB_VENDOR_COREGA, USB_PRODUCT_COREGA_FETHER_USB_TX },
174 { 0, 0 }
175 };
176
177 USB_DECLARE_DRIVER(aue);
178
179 static int aue_tx_list_init __P((struct aue_softc *));
180 static int aue_rx_list_init __P((struct aue_softc *));
181 static int aue_newbuf __P((struct aue_softc *, struct aue_chain *,
182 struct mbuf *));
183 static int aue_send __P((struct aue_softc *, struct mbuf *, int));
184 static void aue_intr __P((usbd_xfer_handle,
185 usbd_private_handle, usbd_status));
186 static void aue_rxeof __P((usbd_xfer_handle,
187 usbd_private_handle, usbd_status));
188 static void aue_txeof __P((usbd_xfer_handle,
189 usbd_private_handle, usbd_status));
190 static void aue_tick __P((void *));
191 static void aue_start __P((struct ifnet *));
192 static int aue_ioctl __P((struct ifnet *, u_long, caddr_t));
193 static void aue_init __P((void *));
194 static void aue_stop __P((struct aue_softc *));
195 static void aue_watchdog __P((struct ifnet *));
196 #ifdef __FreeBSD__
197 static void aue_shutdown __P((device_ptr_t));
198 #endif
199 static int aue_openpipes __P((struct aue_softc *));
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
522 DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __FUNCTION__));
523
524 AUE_CLRBIT(sc, AUE_CTL0, AUE_CTL0_RX_ENB | AUE_CTL0_TX_ENB);
525
526 if (IFM_SUBTYPE(mii->mii_media_active) == IFM_100_TX) {
527 AUE_SETBIT(sc, AUE_CTL1, AUE_CTL1_SPEEDSEL);
528 } else {
529 AUE_CLRBIT(sc, AUE_CTL1, AUE_CTL1_SPEEDSEL);
530 }
531
532 if ((mii->mii_media_active & IFM_GMASK) == IFM_FDX)
533 AUE_SETBIT(sc, AUE_CTL1, AUE_CTL1_DUPLEX);
534 else
535 AUE_CLRBIT(sc, AUE_CTL1, AUE_CTL1_DUPLEX);
536
537 AUE_SETBIT(sc, AUE_CTL0, AUE_CTL0_RX_ENB | AUE_CTL0_TX_ENB);
538
539 /*
540 * Set the LED modes on the LinkSys adapter.
541 * This turns on the 'dual link LED' bin in the auxmode
542 * register of the Broadcom PHY.
543 */
544 if ((sc->aue_vendor == USB_VENDOR_LINKSYS &&
545 sc->aue_product == USB_PRODUCT_LINKSYS_USB100TX) ||
546 (sc->aue_vendor == USB_VENDOR_DLINK &&
547 sc->aue_product == USB_PRODUCT_DLINK_DSB650TX)) {
548 u_int16_t auxmode;
549 auxmode = aue_miibus_readreg(dev, 0, 0x1b);
550 aue_miibus_writereg(dev, 0, 0x1b, auxmode | 0x04);
551 }
552 }
553
554 #define AUE_POLY 0xEDB88320
555 #define AUE_BITS 6
556
557 static u_int32_t
558 aue_crc(addr)
559 caddr_t addr;
560 {
561 u_int32_t idx, bit, data, crc;
562
563 /* Compute CRC for the address value. */
564 crc = 0xFFFFFFFF; /* initial value */
565
566 for (idx = 0; idx < 6; idx++) {
567 for (data = *addr++, bit = 0; bit < 8; bit++, data >>= 1)
568 crc = (crc >> 1) ^ (((crc ^ data) & 1) ? AUE_POLY : 0);
569 }
570
571 return (crc & ((1 << AUE_BITS) - 1));
572 }
573
574 static void
575 aue_setmulti(sc)
576 struct aue_softc *sc;
577 {
578 struct ifnet *ifp;
579 #if defined(__FreeBSD__)
580 struct ifmultiaddr *ifma;
581 #elif defined(__NetBSD__) || defined(__OpenBSD__)
582 struct ether_multi *enm;
583 struct ether_multistep step;
584 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
585 u_int32_t h = 0, i;
586
587 DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __FUNCTION__));
588
589 ifp = GET_IFP(sc);
590
591 if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) {
592 AUE_SETBIT(sc, AUE_CTL0, AUE_CTL0_ALLMULTI);
593 return;
594 }
595
596 AUE_CLRBIT(sc, AUE_CTL0, AUE_CTL0_ALLMULTI);
597
598 /* first, zot all the existing hash bits */
599 for (i = 0; i < 8; i++)
600 csr_write_1(sc, AUE_MAR0 + i, 0);
601
602 /* now program new ones */
603 #if defined(__FreeBSD__)
604 for (ifma = ifp->if_multiaddrs.lh_first; ifma != NULL;
605 ifma = ifma->ifma_link.le_next) {
606 if (ifma->ifma_addr->sa_family != AF_LINK)
607 continue;
608 h = aue_crc(LLADDR((struct sockaddr_dl *)ifma->ifma_addr));
609 AUE_SETBIT(sc, AUE_MAR + (h >> 3), 1 << (h & 0xF));
610 }
611 #elif defined(__NetBSD__) || defined(__OpenBSD__)
612 ETHER_FIRST_MULTI(step, &sc->aue_ec, enm);
613 while (enm != NULL) {
614 #if 1
615 if (memcmp(enm->enm_addrlo,
616 enm->enm_addrhi, ETHER_ADDR_LEN) != 0) {
617 ifp->if_flags |= IFF_ALLMULTI;
618 AUE_SETBIT(sc, AUE_CTL0, AUE_CTL0_ALLMULTI);
619 return;
620 }
621 #endif
622 h = aue_crc(enm->enm_addrlo);
623 AUE_SETBIT(sc, AUE_MAR + (h >> 3), 1 << (h & 0xF));
624 ETHER_NEXT_MULTI(step, enm);
625 }
626 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
627 }
628
629 static void
630 aue_reset(sc)
631 struct aue_softc *sc;
632 {
633 int i;
634
635 DPRINTFN(2,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __FUNCTION__));
636
637 AUE_SETBIT(sc, AUE_CTL1, AUE_CTL1_RESETMAC);
638
639 for (i = 0; i < AUE_TIMEOUT; i++) {
640 if (!(csr_read_1(sc, AUE_CTL1) & AUE_CTL1_RESETMAC))
641 break;
642 }
643
644 if (i == AUE_TIMEOUT)
645 printf("%s: reset failed\n", USBDEVNAME(sc->aue_dev));
646
647 /*
648 * The PHY(s) attached to the Pegasus chip may be held
649 * in reset until we flip on the GPIO outputs. Make sure
650 * to set the GPIO pins high so that the PHY(s) will
651 * be enabled.
652 *
653 * Note: We force all of the GPIO pins low first, *then*
654 * enable the ones we want.
655 */
656 csr_write_1(sc, AUE_GPIO0, AUE_GPIO_OUT0|AUE_GPIO_SEL0);
657 csr_write_1(sc, AUE_GPIO0, AUE_GPIO_OUT0|AUE_GPIO_SEL0|AUE_GPIO_SEL1);
658
659 /* Grrr. LinkSys has to be different from everyone else. */
660 if ((sc->aue_vendor == USB_VENDOR_LINKSYS &&
661 sc->aue_product == USB_PRODUCT_LINKSYS_USB100TX) ||
662 (sc->aue_vendor == USB_VENDOR_DLINK &&
663 sc->aue_product == USB_PRODUCT_DLINK_DSB650TX)) {
664 csr_write_1(sc, AUE_GPIO0, AUE_GPIO_SEL0|AUE_GPIO_SEL1);
665 csr_write_1(sc, AUE_GPIO0, AUE_GPIO_SEL0|AUE_GPIO_SEL1|
666 AUE_GPIO_OUT0);
667 }
668
669 /* Wait a little while for the chip to get its brains in order. */
670 DELAY(10000); /* XXX */
671 }
672
673 /*
674 * Probe for a Pegasus chip.
675 */
676 USB_MATCH(aue)
677 {
678 USB_MATCH_START(aue, uaa);
679 struct aue_type *t;
680
681 if (uaa->iface != NULL)
682 return (UMATCH_NONE);
683
684 for (t = aue_devs; t->aue_vid != 0; t++)
685 if (uaa->vendor == t->aue_vid && uaa->product == t->aue_did)
686 return (UMATCH_VENDOR_PRODUCT);
687
688 return (UMATCH_NONE);
689 }
690
691 /*
692 * Attach the interface. Allocate softc structures, do ifmedia
693 * setup and ethernet/BPF attach.
694 */
695 USB_ATTACH(aue)
696 {
697 USB_ATTACH_START(aue, sc, uaa);
698 char devinfo[1024];
699 int s;
700 u_char eaddr[ETHER_ADDR_LEN];
701 struct ifnet *ifp;
702 struct mii_data *mii;
703 usbd_device_handle dev = uaa->device;
704 usbd_interface_handle iface;
705 usbd_status err;
706 usb_interface_descriptor_t *id;
707 usb_endpoint_descriptor_t *ed;
708 int i;
709
710 #ifdef __FreeBSD__
711 bzero(sc, sizeof(struct aue_softc));
712 #endif
713
714 DPRINTFN(5,(" : aue_attach: sc=%p", sc));
715
716 usbd_devinfo(dev, 0, devinfo);
717 USB_ATTACH_SETUP;
718 printf("%s: %s\n", USBDEVNAME(sc->aue_dev), devinfo);
719
720 err = usbd_set_config_no(dev, AUE_CONFIG_NO, 0);
721 if (err) {
722 printf("%s: setting config no failed\n",
723 USBDEVNAME(sc->aue_dev));
724 USB_ATTACH_ERROR_RETURN;
725 }
726
727 err = usbd_device2interface_handle(dev, AUE_IFACE_IDX, &iface);
728 if (err) {
729 printf("%s: getting interface handle failed\n",
730 USBDEVNAME(sc->aue_dev));
731 USB_ATTACH_ERROR_RETURN;
732 }
733
734 sc->aue_udev = dev;
735 sc->aue_iface = iface;
736 sc->aue_product = uaa->product;
737 sc->aue_vendor = uaa->vendor;
738
739 id = usbd_get_interface_descriptor(iface);
740
741 /* Find endpoints. */
742 for (i = 0; i < id->bNumEndpoints; i++) {
743 ed = usbd_interface2endpoint_descriptor(iface, i);
744 if (ed == NULL) {
745 printf("%s: couldn't get endpoint descriptor %d\n",
746 USBDEVNAME(sc->aue_dev), i);
747 USB_ATTACH_ERROR_RETURN;
748 }
749 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
750 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
751 sc->aue_ed[AUE_ENDPT_RX] = ed->bEndpointAddress;
752 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
753 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
754 sc->aue_ed[AUE_ENDPT_TX] = ed->bEndpointAddress;
755 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
756 UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) {
757 sc->aue_ed[AUE_ENDPT_INTR] = ed->bEndpointAddress;
758 }
759 }
760
761 if (sc->aue_ed[AUE_ENDPT_RX] == 0 || sc->aue_ed[AUE_ENDPT_TX] == 0 ||
762 sc->aue_ed[AUE_ENDPT_INTR] == 0) {
763 printf("%s: missing endpoint\n", USBDEVNAME(sc->aue_dev));
764 USB_ATTACH_ERROR_RETURN;
765 }
766
767
768 s = splimp();
769
770 /* Reset the adapter. */
771 aue_reset(sc);
772
773 /*
774 * Get station address from the EEPROM.
775 */
776 aue_read_mac(sc, eaddr);
777
778 /*
779 * A Pegasus chip was detected. Inform the world.
780 */
781 #if defined(__FreeBSD__)
782 printf("%s: Ethernet address: %6D\n", USBDEVNAME(sc->aue_dev),
783 eaddr, ":");
784
785 bcopy(eaddr, (char *)&sc->arpcom.ac_enaddr, ETHER_ADDR_LEN);
786
787 ifp = &sc->arpcom.ac_if;
788 ifp->if_softc = sc;
789 ifp->if_unit = sc->aue_unit;
790 ifp->if_name = "aue";
791 ifp->if_mtu = ETHERMTU;
792 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
793 ifp->if_ioctl = aue_ioctl;
794 ifp->if_output = ether_output;
795 ifp->if_start = aue_start;
796 ifp->if_watchdog = aue_watchdog;
797 ifp->if_init = aue_init;
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 strncpy(ifp->if_xname, USBDEVNAME(sc->aue_dev), IFNAMSIZ);
847
848 /* Initialize MII/media info. */
849 mii = &sc->aue_mii;
850 mii->mii_ifp = ifp;
851 mii->mii_readreg = aue_miibus_readreg;
852 mii->mii_writereg = aue_miibus_writereg;
853 mii->mii_statchg = aue_miibus_statchg;
854 ifmedia_init(&mii->mii_media, 0, aue_ifmedia_upd, aue_ifmedia_sts);
855 mii_attach(self, mii, 0xffffffff, MII_PHY_ANY, MII_OFFSET_ANY, 0);
856 if (LIST_FIRST(&mii->mii_phys) == NULL) {
857 ifmedia_add(&mii->mii_media, IFM_ETHER | IFM_NONE, 0, NULL);
858 ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_NONE);
859 } else
860 ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_AUTO);
861
862 /* Attach the interface. */
863 if_attach(ifp);
864 ether_ifattach(ifp, eaddr);
865
866 #if NBPFILTER > 0
867 bpfattach(&ifp->if_bpf, ifp, DLT_EN10MB,
868 sizeof(struct ether_header));
869 #endif
870 #if NRND > 0
871 rnd_attach_source(&sc->rnd_source, USBDEVNAME(sc->aue_dev),
872 RND_TYPE_NET, 0);
873 #endif
874
875 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
876
877 sc->aue_attached = 1;
878 splx(s);
879
880 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->aue_udev,
881 USBDEV(sc->aue_dev));
882
883 USB_ATTACH_SUCCESS_RETURN;
884 }
885
886 USB_DETACH(aue)
887 {
888 USB_DETACH_START(aue, sc);
889 struct ifnet *ifp = GET_IFP(sc);
890 int s;
891
892 DPRINTFN(2,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __FUNCTION__));
893
894 s = splusb();
895
896 usb_untimeout(aue_tick, sc, sc->aue_stat_ch);
897
898 if (!sc->aue_attached) {
899 /* Detached before attached finished, so just bail out. */
900 splx(s);
901 return (0);
902 }
903
904 if (ifp->if_flags & IFF_RUNNING)
905 aue_stop(sc);
906
907 #if defined(__NetBSD__)
908 #if NRND > 0
909 rnd_detach_source(&sc->rnd_source);
910 #endif
911 mii_detach(&sc->aue_mii, MII_PHY_ANY, MII_OFFSET_ANY);
912 ifmedia_delete_instance(&sc->aue_mii.mii_media, IFM_INST_ANY);
913 #if NBPFILTER > 0
914 bpfdetach(ifp);
915 #endif
916 ether_ifdetach(ifp);
917 #endif /* __NetBSD__ */
918
919 if_detach(ifp);
920
921 #ifdef DIAGNOSTIC
922 if (sc->aue_ep[AUE_ENDPT_TX] != NULL ||
923 sc->aue_ep[AUE_ENDPT_RX] != NULL ||
924 sc->aue_ep[AUE_ENDPT_INTR] != NULL)
925 printf("%s: detach has active endpoints\n",
926 USBDEVNAME(sc->aue_dev));
927 #endif
928
929 sc->aue_attached = 0;
930 splx(s);
931
932 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->aue_udev,
933 USBDEV(sc->aue_dev));
934
935 return (0);
936 }
937
938 #if defined(__NetBSD__) || defined(__OpenBSD__)
939 int
940 aue_activate(self, act)
941 device_ptr_t self;
942 enum devact act;
943 {
944 struct aue_softc *sc = (struct aue_softc *)self;
945
946 DPRINTFN(2,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __FUNCTION__));
947
948 switch (act) {
949 case DVACT_ACTIVATE:
950 return (EOPNOTSUPP);
951 break;
952
953 case DVACT_DEACTIVATE:
954 if_deactivate(&sc->aue_ec.ec_if);
955 sc->aue_dying = 1;
956 break;
957 }
958 return (0);
959 }
960 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
961
962 /*
963 * Initialize an RX descriptor and attach an MBUF cluster.
964 */
965 static int
966 aue_newbuf(sc, c, m)
967 struct aue_softc *sc;
968 struct aue_chain *c;
969 struct mbuf *m;
970 {
971 struct mbuf *m_new = NULL;
972
973 DPRINTFN(10,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev),__FUNCTION__));
974
975 if (m == NULL) {
976 MGETHDR(m_new, M_DONTWAIT, MT_DATA);
977 if (m_new == NULL) {
978 printf("%s: no memory for rx list "
979 "-- packet dropped!\n", USBDEVNAME(sc->aue_dev));
980 return (ENOBUFS);
981 }
982
983 MCLGET(m_new, M_DONTWAIT);
984 if (!(m_new->m_flags & M_EXT)) {
985 printf("%s: no memory for rx list "
986 "-- packet dropped!\n", USBDEVNAME(sc->aue_dev));
987 m_freem(m_new);
988 return (ENOBUFS);
989 }
990 m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
991 } else {
992 m_new = m;
993 m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
994 m_new->m_data = m_new->m_ext.ext_buf;
995 }
996
997 m_adj(m_new, ETHER_ALIGN);
998 c->aue_mbuf = m_new;
999
1000 return (0);
1001 }
1002
1003 static int
1004 aue_rx_list_init(sc)
1005 struct aue_softc *sc;
1006 {
1007 struct aue_cdata *cd;
1008 struct aue_chain *c;
1009 int i;
1010
1011 DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __FUNCTION__));
1012
1013 cd = &sc->aue_cdata;
1014 for (i = 0; i < AUE_RX_LIST_CNT; i++) {
1015 c = &cd->aue_rx_chain[i];
1016 c->aue_sc = sc;
1017 c->aue_idx = i;
1018 if (aue_newbuf(sc, c, NULL) == ENOBUFS)
1019 return (ENOBUFS);
1020 if (c->aue_xfer == NULL) {
1021 c->aue_xfer = usbd_alloc_xfer(sc->aue_udev);
1022 if (c->aue_xfer == NULL)
1023 return (ENOBUFS);
1024 c->aue_buf = usbd_alloc_buffer(c->aue_xfer, AUE_BUFSZ);
1025 if (c->aue_buf == NULL)
1026 return (ENOBUFS); /* XXX free xfer */
1027 }
1028 }
1029
1030 return (0);
1031 }
1032
1033 static int
1034 aue_tx_list_init(sc)
1035 struct aue_softc *sc;
1036 {
1037 struct aue_cdata *cd;
1038 struct aue_chain *c;
1039 int i;
1040
1041 DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __FUNCTION__));
1042
1043 cd = &sc->aue_cdata;
1044 for (i = 0; i < AUE_TX_LIST_CNT; i++) {
1045 c = &cd->aue_tx_chain[i];
1046 c->aue_sc = sc;
1047 c->aue_idx = i;
1048 c->aue_mbuf = NULL;
1049 if (c->aue_xfer == NULL) {
1050 c->aue_xfer = usbd_alloc_xfer(sc->aue_udev);
1051 if (c->aue_xfer == NULL)
1052 return (ENOBUFS);
1053 c->aue_buf = usbd_alloc_buffer(c->aue_xfer, AUE_BUFSZ);
1054 if (c->aue_buf == NULL)
1055 return (ENOBUFS);
1056 }
1057 }
1058
1059 return (0);
1060 }
1061
1062 static void
1063 aue_intr(xfer, priv, status)
1064 usbd_xfer_handle xfer;
1065 usbd_private_handle priv;
1066 usbd_status status;
1067 {
1068 struct aue_softc *sc = priv;
1069 struct ifnet *ifp = GET_IFP(sc);
1070 struct aue_intrpkt *p = &sc->aue_cdata.aue_ibuf;
1071
1072 DPRINTFN(15,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev),__FUNCTION__));
1073
1074 if (sc->aue_dying)
1075 return;
1076
1077 if (!(ifp->if_flags & IFF_RUNNING))
1078 return;
1079
1080 if (status != USBD_NORMAL_COMPLETION) {
1081 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
1082 return;
1083 }
1084 printf("%s: usb error on intr: %s\n", USBDEVNAME(sc->aue_dev),
1085 usbd_errstr(status));
1086 if (status == USBD_STALLED)
1087 usbd_clear_endpoint_stall(sc->aue_ep[AUE_ENDPT_RX]);
1088 return;
1089 }
1090
1091 if (p->aue_txstat0)
1092 ifp->if_oerrors++;
1093
1094 if (p->aue_txstat0 & (AUE_TXSTAT0_LATECOLL | AUE_TXSTAT0_EXCESSCOLL))
1095 ifp->if_collisions++;
1096 }
1097
1098 #if defined(__FreeBSD__)
1099 static void
1100 aue_rxstart(ifp)
1101 struct ifnet *ifp;
1102 {
1103 struct aue_softc *sc;
1104 struct aue_chain *c;
1105
1106 sc = ifp->if_softc;
1107 c = &sc->aue_cdata.aue_rx_chain[sc->aue_cdata.aue_rx_prod];
1108
1109 if (aue_newbuf(sc, c, NULL) == ENOBUFS) {
1110 ifp->if_ierrors++;
1111 return;
1112 }
1113
1114 /* Setup new transfer. */
1115 usbd_setup_xfer(c->aue_xfer, sc->aue_ep[AUE_ENDPT_RX],
1116 c, mtod(c->aue_mbuf, char *), AUE_BUFSZ, USBD_SHORT_XFER_OK,
1117 USBD_NO_TIMEOUT, aue_rxeof);
1118 usbd_transfer(c->aue_xfer);
1119 }
1120 #endif
1121
1122 /*
1123 * A frame has been uploaded: pass the resulting mbuf chain up to
1124 * the higher level protocols.
1125 *
1126 * Grrr. Receiving transfers larger than about 1152 bytes sometimes
1127 * doesn't work. We get an incomplete frame. In order to avoid
1128 * this, we queue up RX transfers that are shorter than a full sized
1129 * frame. If the received frame is larger than our transfer size,
1130 * we snag the rest of the data using a second transfer. Does this
1131 * hurt performance? Yes. But after fighting with this stupid thing
1132 * for three days, I'm willing to settle. I'd rather have reliable
1133 * receive performance that fast but spotty performance.
1134 */
1135 static void
1136 aue_rxeof(xfer, priv, status)
1137 usbd_xfer_handle xfer;
1138 usbd_private_handle priv;
1139 usbd_status status;
1140 {
1141 struct aue_chain *c = priv;
1142 struct aue_softc *sc = c->aue_sc;
1143 struct ifnet *ifp = GET_IFP(sc);
1144 struct mbuf *m;
1145 u_int32_t total_len;
1146 struct aue_rxpkt r;
1147 #if defined(__NetBSD__) || defined(__OpenBSD__)
1148 int s;
1149 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
1150
1151 DPRINTFN(10,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev),__FUNCTION__));
1152
1153 if (sc->aue_dying)
1154 return;
1155
1156 if (!(ifp->if_flags & IFF_RUNNING))
1157 return;
1158
1159 if (status != USBD_NORMAL_COMPLETION) {
1160 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
1161 return;
1162 sc->aue_rx_errs++;
1163 if (usbd_ratecheck(&sc->aue_rx_notice)) {
1164 printf("%s: %u usb errors on rx: %s\n",
1165 USBDEVNAME(sc->aue_dev), sc->aue_rx_errs,
1166 usbd_errstr(status));
1167 sc->aue_rx_errs = 0;
1168 }
1169 if (status == USBD_STALLED)
1170 usbd_clear_endpoint_stall(sc->aue_ep[AUE_ENDPT_RX]);
1171 goto done;
1172 }
1173
1174 usbd_get_xfer_status(xfer, NULL, NULL, &total_len, NULL);
1175
1176 memcpy(mtod(c->aue_mbuf, char*), c->aue_buf, total_len);
1177
1178 if (total_len <= 4 + ETHER_CRC_LEN) {
1179 ifp->if_ierrors++;
1180 goto done;
1181 }
1182
1183 memcpy(&r, c->aue_buf + total_len - 4, sizeof(r));
1184
1185 /* Turn off all the non-error bits in the rx status word. */
1186 r.aue_rxstat &= AUE_RXSTAT_MASK;
1187 if (r.aue_rxstat) {
1188 ifp->if_ierrors++;
1189 goto done;
1190 }
1191
1192 /* No errors; receive the packet. */
1193 m = c->aue_mbuf;
1194 total_len -= ETHER_CRC_LEN + 4;
1195 m->m_pkthdr.len = m->m_len = total_len;
1196 ifp->if_ipackets++;
1197
1198 #if defined(__FreeBSD__)
1199 m->m_pkthdr.rcvif = (struct ifnet *)&kue_qdat;
1200 /* Put the packet on the special USB input queue. */
1201 usb_ether_input(m);
1202
1203 return;
1204
1205 #elif defined(__NetBSD__) || defined(__OpenBSD__)
1206 m->m_pkthdr.rcvif = ifp;
1207
1208 s = splimp();
1209
1210 /* XXX ugly */
1211 if (aue_newbuf(sc, c, NULL) == ENOBUFS) {
1212 ifp->if_ierrors++;
1213 goto done1;
1214 }
1215
1216 #if NBPFILTER > 0
1217 /*
1218 * Handle BPF listeners. Let the BPF user see the packet, but
1219 * don't pass it up to the ether_input() layer unless it's
1220 * a broadcast packet, multicast packet, matches our ethernet
1221 * address or the interface is in promiscuous mode.
1222 */
1223 if (ifp->if_bpf) {
1224 struct ether_header *eh = mtod(m, struct ether_header *);
1225 BPF_MTAP(ifp, m);
1226 if ((ifp->if_flags & IFF_PROMISC) &&
1227 memcmp(eh->ether_dhost, LLADDR(ifp->if_sadl),
1228 ETHER_ADDR_LEN) &&
1229 !(eh->ether_dhost[0] & 1)) {
1230 m_freem(m);
1231 goto done1;
1232 }
1233 }
1234 #endif
1235
1236 DPRINTFN(10,("%s: %s: deliver %d\n", USBDEVNAME(sc->aue_dev),
1237 __FUNCTION__, m->m_len));
1238 (*ifp->if_input)(ifp, m);
1239 done1:
1240 splx(s);
1241 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
1242
1243 done:
1244
1245 /* Setup new transfer. */
1246 usbd_setup_xfer(xfer, sc->aue_ep[AUE_ENDPT_RX],
1247 c, c->aue_buf, AUE_BUFSZ,
1248 USBD_SHORT_XFER_OK | USBD_NO_COPY,
1249 USBD_NO_TIMEOUT, aue_rxeof);
1250 usbd_transfer(xfer);
1251
1252 DPRINTFN(10,("%s: %s: start rx\n", USBDEVNAME(sc->aue_dev),
1253 __FUNCTION__));
1254 }
1255
1256 /*
1257 * A frame was downloaded to the chip. It's safe for us to clean up
1258 * the list buffers.
1259 */
1260
1261 static void
1262 aue_txeof(xfer, priv, status)
1263 usbd_xfer_handle xfer;
1264 usbd_private_handle priv;
1265 usbd_status status;
1266 {
1267 struct aue_chain *c = priv;
1268 struct aue_softc *sc = c->aue_sc;
1269 struct ifnet *ifp = GET_IFP(sc);
1270 int s;
1271
1272 if (sc->aue_dying)
1273 return;
1274
1275 s = splimp();
1276
1277 DPRINTFN(10,("%s: %s: enter status=%d\n", USBDEVNAME(sc->aue_dev),
1278 __FUNCTION__, status));
1279
1280 ifp->if_timer = 0;
1281 ifp->if_flags &= ~IFF_OACTIVE;
1282
1283 if (status != USBD_NORMAL_COMPLETION) {
1284 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
1285 splx(s);
1286 return;
1287 }
1288 ifp->if_oerrors++;
1289 printf("%s: usb error on tx: %s\n", USBDEVNAME(sc->aue_dev),
1290 usbd_errstr(status));
1291 if (status == USBD_STALLED)
1292 usbd_clear_endpoint_stall(sc->aue_ep[AUE_ENDPT_TX]);
1293 splx(s);
1294 return;
1295 }
1296
1297 ifp->if_opackets++;
1298
1299 #if defined(__FreeBSD__)
1300 c->aue_mbuf->m_pkthdr.rcvif = ifp;
1301 usb_tx_done(c->aue_mbuf);
1302 c->aue_mbuf = NULL;
1303 #elif defined(__NetBSD__) || defined(__OpenBSD__)
1304 m_freem(c->aue_mbuf);
1305 c->aue_mbuf = NULL;
1306
1307 if (ifp->if_snd.ifq_head != NULL)
1308 aue_start(ifp);
1309 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
1310
1311 splx(s);
1312 }
1313
1314 static void
1315 aue_tick(xsc)
1316 void *xsc;
1317 {
1318 struct aue_softc *sc = xsc;
1319 struct ifnet *ifp;
1320 struct mii_data *mii;
1321 int s;
1322
1323 DPRINTFN(15,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev),__FUNCTION__));
1324
1325 if (sc == NULL)
1326 return;
1327
1328 if (sc->aue_dying)
1329 return;
1330
1331 ifp = GET_IFP(sc);
1332 mii = GET_MII(sc);
1333 if (mii == NULL)
1334 return;
1335
1336 s = splimp();
1337
1338 mii_tick(mii);
1339 if (!sc->aue_link) {
1340 mii_pollstat(mii);
1341 if (mii->mii_media_status & IFM_ACTIVE &&
1342 IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
1343 DPRINTFN(2,("%s: %s: got link\n",
1344 USBDEVNAME(sc->aue_dev),__FUNCTION__));
1345 sc->aue_link++;
1346 if (ifp->if_snd.ifq_head != NULL)
1347 aue_start(ifp);
1348 }
1349 }
1350
1351 usb_timeout(aue_tick, sc, hz, sc->aue_stat_ch);
1352
1353 splx(s);
1354 }
1355
1356 static int
1357 aue_send(sc, m, idx)
1358 struct aue_softc *sc;
1359 struct mbuf *m;
1360 int idx;
1361 {
1362 int total_len;
1363 struct aue_chain *c;
1364 usbd_status err;
1365
1366 DPRINTFN(10,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev),__FUNCTION__));
1367
1368 c = &sc->aue_cdata.aue_tx_chain[idx];
1369
1370 /*
1371 * Copy the mbuf data into a contiguous buffer, leaving two
1372 * bytes at the beginning to hold the frame length.
1373 */
1374 m_copydata(m, 0, m->m_pkthdr.len, c->aue_buf + 2);
1375 c->aue_mbuf = m;
1376
1377 /*
1378 * The ADMtek documentation says that the packet length is
1379 * supposed to be specified in the first two bytes of the
1380 * transfer, however it actually seems to ignore this info
1381 * and base the frame size on the bulk transfer length.
1382 */
1383 c->aue_buf[0] = (u_int8_t)m->m_pkthdr.len;
1384 c->aue_buf[1] = (u_int8_t)(m->m_pkthdr.len >> 8);
1385 total_len = m->m_pkthdr.len + 2;
1386
1387 usbd_setup_xfer(c->aue_xfer, sc->aue_ep[AUE_ENDPT_TX],
1388 c, c->aue_buf, total_len, USBD_FORCE_SHORT_XFER | USBD_NO_COPY,
1389 AUE_TX_TIMEOUT, aue_txeof);
1390
1391 /* Transmit */
1392 err = usbd_transfer(c->aue_xfer);
1393 if (err != USBD_IN_PROGRESS) {
1394 aue_stop(sc);
1395 return (EIO);
1396 }
1397 DPRINTFN(5,("%s: %s: send %d bytes\n", USBDEVNAME(sc->aue_dev),
1398 __FUNCTION__, total_len));
1399
1400 sc->aue_cdata.aue_tx_cnt++;
1401
1402 return (0);
1403 }
1404
1405 static void
1406 aue_start(ifp)
1407 struct ifnet *ifp;
1408 {
1409 struct aue_softc *sc = ifp->if_softc;
1410 struct mbuf *m_head = NULL;
1411
1412 DPRINTFN(5,("%s: %s: enter, link=%d\n", USBDEVNAME(sc->aue_dev),
1413 __FUNCTION__, sc->aue_link));
1414
1415 if (sc->aue_dying)
1416 return;
1417
1418 if (!sc->aue_link)
1419 return;
1420
1421 if (ifp->if_flags & IFF_OACTIVE)
1422 return;
1423
1424 IF_DEQUEUE(&ifp->if_snd, m_head);
1425 if (m_head == NULL)
1426 return;
1427
1428 if (aue_send(sc, m_head, 0)) {
1429 IF_PREPEND(&ifp->if_snd, m_head);
1430 ifp->if_flags |= IFF_OACTIVE;
1431 return;
1432 }
1433
1434 #if NBPFILTER > 0
1435 /*
1436 * If there's a BPF listener, bounce a copy of this frame
1437 * to him.
1438 */
1439 if (ifp->if_bpf)
1440 BPF_MTAP(ifp, m_head);
1441 #endif
1442
1443 ifp->if_flags |= IFF_OACTIVE;
1444
1445 /*
1446 * Set a timeout in case the chip goes out to lunch.
1447 */
1448 ifp->if_timer = 5;
1449 }
1450
1451 static void
1452 aue_init(xsc)
1453 void *xsc;
1454 {
1455 struct aue_softc *sc = xsc;
1456 struct ifnet *ifp = GET_IFP(sc);
1457 struct mii_data *mii = GET_MII(sc);
1458 int i, s;
1459 u_char *eaddr;
1460
1461 DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __FUNCTION__));
1462
1463 if (sc->aue_dying)
1464 return;
1465
1466 if (ifp->if_flags & IFF_RUNNING)
1467 return;
1468
1469 s = splimp();
1470
1471 /*
1472 * Cancel pending I/O and free all RX/TX buffers.
1473 */
1474 aue_reset(sc);
1475
1476 #if defined(__FreeBSD__)
1477 eaddr = sc->arpcom.ac_enaddr;
1478 #elif defined(__NetBSD__) || defined(__OpenBSD__)
1479 eaddr = LLADDR(ifp->if_sadl);
1480 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
1481 for (i = 0; i < ETHER_ADDR_LEN; i++)
1482 csr_write_1(sc, AUE_PAR0 + i, eaddr[i]);
1483
1484 /* If we want promiscuous mode, set the allframes bit. */
1485 if (ifp->if_flags & IFF_PROMISC)
1486 AUE_SETBIT(sc, AUE_CTL2, AUE_CTL2_RX_PROMISC);
1487 else
1488 AUE_CLRBIT(sc, AUE_CTL2, AUE_CTL2_RX_PROMISC);
1489
1490 /* Init TX ring. */
1491 if (aue_tx_list_init(sc) == ENOBUFS) {
1492 printf("%s: tx list init failed\n", USBDEVNAME(sc->aue_dev));
1493 splx(s);
1494 return;
1495 }
1496
1497 /* Init RX ring. */
1498 if (aue_rx_list_init(sc) == ENOBUFS) {
1499 printf("%s: rx list init failed\n", USBDEVNAME(sc->aue_dev));
1500 splx(s);
1501 return;
1502 }
1503
1504 /* Load the multicast filter. */
1505 aue_setmulti(sc);
1506
1507 /* Enable RX and TX */
1508 csr_write_1(sc, AUE_CTL0, AUE_CTL0_RXSTAT_APPEND|AUE_CTL0_RX_ENB);
1509 AUE_SETBIT(sc, AUE_CTL0, AUE_CTL0_TX_ENB);
1510 AUE_SETBIT(sc, AUE_CTL2, AUE_CTL2_EP3_CLR);
1511
1512 mii_mediachg(mii);
1513
1514 if (sc->aue_ep[AUE_ENDPT_RX] == NULL) {
1515 if (aue_openpipes(sc)) {
1516 splx(s);
1517 return;
1518 }
1519 }
1520
1521 ifp->if_flags |= IFF_RUNNING;
1522 ifp->if_flags &= ~IFF_OACTIVE;
1523
1524 splx(s);
1525
1526 usb_untimeout(aue_tick, sc, sc->aue_stat_ch);
1527 usb_timeout(aue_tick, sc, hz, sc->aue_stat_ch);
1528 }
1529
1530 static int
1531 aue_openpipes(sc)
1532 struct aue_softc *sc;
1533 {
1534 struct aue_chain *c;
1535 usbd_status err;
1536 int i;
1537
1538 /* Open RX and TX pipes. */
1539 err = usbd_open_pipe(sc->aue_iface, sc->aue_ed[AUE_ENDPT_RX],
1540 USBD_EXCLUSIVE_USE, &sc->aue_ep[AUE_ENDPT_RX]);
1541 if (err) {
1542 printf("%s: open rx pipe failed: %s\n",
1543 USBDEVNAME(sc->aue_dev), usbd_errstr(err));
1544 return (EIO);
1545 }
1546 usbd_open_pipe(sc->aue_iface, sc->aue_ed[AUE_ENDPT_TX],
1547 USBD_EXCLUSIVE_USE, &sc->aue_ep[AUE_ENDPT_TX]);
1548 if (err) {
1549 printf("%s: open tx pipe failed: %s\n",
1550 USBDEVNAME(sc->aue_dev), usbd_errstr(err));
1551 return (EIO);
1552 }
1553 err = usbd_open_pipe_intr(sc->aue_iface, sc->aue_ed[AUE_ENDPT_INTR],
1554 USBD_EXCLUSIVE_USE, &sc->aue_ep[AUE_ENDPT_INTR], sc,
1555 &sc->aue_cdata.aue_ibuf, AUE_INTR_PKTLEN, aue_intr,
1556 AUE_INTR_INTERVAL);
1557 if (err) {
1558 printf("%s: open intr pipe failed: %s\n",
1559 USBDEVNAME(sc->aue_dev), usbd_errstr(err));
1560 return (EIO);
1561 }
1562
1563 /* Start up the receive pipe. */
1564 for (i = 0; i < AUE_RX_LIST_CNT; i++) {
1565 c = &sc->aue_cdata.aue_rx_chain[i];
1566 usbd_setup_xfer(c->aue_xfer, sc->aue_ep[AUE_ENDPT_RX],
1567 c, c->aue_buf, AUE_BUFSZ,
1568 USBD_SHORT_XFER_OK | USBD_NO_COPY, USBD_NO_TIMEOUT,
1569 aue_rxeof);
1570 (void)usbd_transfer(c->aue_xfer); /* XXX */
1571 DPRINTFN(5,("%s: %s: start read\n", USBDEVNAME(sc->aue_dev),
1572 __FUNCTION__));
1573
1574 }
1575 return (0);
1576 }
1577
1578 /*
1579 * Set media options.
1580 */
1581 static int
1582 aue_ifmedia_upd(ifp)
1583 struct ifnet *ifp;
1584 {
1585 struct aue_softc *sc = ifp->if_softc;
1586 struct mii_data *mii = GET_MII(sc);
1587
1588 DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __FUNCTION__));
1589
1590 if (sc->aue_dying)
1591 return (0);
1592
1593 sc->aue_link = 0;
1594 if (mii->mii_instance) {
1595 struct mii_softc *miisc;
1596 for (miisc = LIST_FIRST(&mii->mii_phys); miisc != NULL;
1597 miisc = LIST_NEXT(miisc, mii_list))
1598 mii_phy_reset(miisc);
1599 }
1600 mii_mediachg(mii);
1601
1602 return (0);
1603 }
1604
1605 /*
1606 * Report current media status.
1607 */
1608 static void
1609 aue_ifmedia_sts(ifp, ifmr)
1610 struct ifnet *ifp;
1611 struct ifmediareq *ifmr;
1612 {
1613 struct aue_softc *sc = ifp->if_softc;
1614 struct mii_data *mii = GET_MII(sc);
1615
1616 DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __FUNCTION__));
1617
1618 mii_pollstat(mii);
1619 ifmr->ifm_active = mii->mii_media_active;
1620 ifmr->ifm_status = mii->mii_media_status;
1621 }
1622
1623 static int
1624 aue_ioctl(ifp, command, data)
1625 struct ifnet *ifp;
1626 u_long command;
1627 caddr_t data;
1628 {
1629 struct aue_softc *sc = ifp->if_softc;
1630 #if defined(__NetBSD__) || defined(__OpenBSD__)
1631 struct ifaddr *ifa = (struct ifaddr *)data;
1632 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
1633 struct ifreq *ifr = (struct ifreq *)data;
1634 struct mii_data *mii;
1635 int s, error = 0;
1636
1637 if (sc->aue_dying)
1638 return (EIO);
1639
1640 s = splimp();
1641
1642 switch(command) {
1643 #if defined(__FreeBSD__)
1644 case SIOCSIFADDR:
1645 case SIOCGIFADDR:
1646 case SIOCSIFMTU:
1647 error = ether_ioctl(ifp, command, data);
1648 break;
1649 #elif defined(__NetBSD__) || defined(__OpenBSD__)
1650 case SIOCSIFADDR:
1651 ifp->if_flags |= IFF_UP;
1652 aue_init(sc);
1653
1654 switch (ifa->ifa_addr->sa_family) {
1655 #ifdef INET
1656 case AF_INET:
1657 arp_ifinit(ifp, ifa);
1658 break;
1659 #endif /* INET */
1660 #ifdef NS
1661 case AF_NS:
1662 {
1663 struct ns_addr *ina = &IA_SNS(ifa)->sns_addr;
1664
1665 if (ns_nullhost(*ina))
1666 ina->x_host = *(union ns_host *)
1667 LLADDR(ifp->if_sadl);
1668 else
1669 memcpy(LLADDR(ifp->if_sadl),
1670 ina->x_host.c_host,
1671 ifp->if_addrlen);
1672 break;
1673 }
1674 #endif /* NS */
1675 }
1676 break;
1677
1678 case SIOCSIFMTU:
1679 if (ifr->ifr_mtu > ETHERMTU)
1680 error = EINVAL;
1681 else
1682 ifp->if_mtu = ifr->ifr_mtu;
1683 break;
1684
1685 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
1686 case SIOCSIFFLAGS:
1687 if (ifp->if_flags & IFF_UP) {
1688 if (ifp->if_flags & IFF_RUNNING &&
1689 ifp->if_flags & IFF_PROMISC &&
1690 !(sc->aue_if_flags & IFF_PROMISC)) {
1691 AUE_SETBIT(sc, AUE_CTL2, AUE_CTL2_RX_PROMISC);
1692 } else if (ifp->if_flags & IFF_RUNNING &&
1693 !(ifp->if_flags & IFF_PROMISC) &&
1694 sc->aue_if_flags & IFF_PROMISC) {
1695 AUE_CLRBIT(sc, AUE_CTL2, AUE_CTL2_RX_PROMISC);
1696 } else if (!(ifp->if_flags & IFF_RUNNING))
1697 aue_init(sc);
1698 } else {
1699 if (ifp->if_flags & IFF_RUNNING)
1700 aue_stop(sc);
1701 }
1702 sc->aue_if_flags = ifp->if_flags;
1703 error = 0;
1704 break;
1705 case SIOCADDMULTI:
1706 case SIOCDELMULTI:
1707 aue_setmulti(sc);
1708 error = 0;
1709 break;
1710 case SIOCGIFMEDIA:
1711 case SIOCSIFMEDIA:
1712 mii = GET_MII(sc);
1713 error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, command);
1714 break;
1715 default:
1716 error = EINVAL;
1717 break;
1718 }
1719
1720 splx(s);
1721
1722 return (error);
1723 }
1724
1725 static void
1726 aue_watchdog(ifp)
1727 struct ifnet *ifp;
1728 {
1729 struct aue_softc *sc = ifp->if_softc;
1730
1731 DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __FUNCTION__));
1732
1733 ifp->if_oerrors++;
1734 printf("%s: watchdog timeout\n", USBDEVNAME(sc->aue_dev));
1735
1736 /*
1737 * The polling business is a kludge to avoid allowing the
1738 * USB code to call tsleep() in usbd_delay_ms(), which will
1739 * kill us since the watchdog routine is invoked from
1740 * interrupt context.
1741 */
1742 usbd_set_polling(sc->aue_udev, 1);
1743 aue_stop(sc);
1744 aue_init(sc);
1745 usbd_set_polling(sc->aue_udev, 0);
1746
1747 if (ifp->if_snd.ifq_head != NULL)
1748 aue_start(ifp);
1749 }
1750
1751 /*
1752 * Stop the adapter and free any mbufs allocated to the
1753 * RX and TX lists.
1754 */
1755 static void
1756 aue_stop(sc)
1757 struct aue_softc *sc;
1758 {
1759 usbd_status err;
1760 struct ifnet *ifp;
1761 int i;
1762
1763 DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __FUNCTION__));
1764
1765 ifp = GET_IFP(sc);
1766 ifp->if_timer = 0;
1767
1768 csr_write_1(sc, AUE_CTL0, 0);
1769 csr_write_1(sc, AUE_CTL1, 0);
1770 aue_reset(sc);
1771 usb_untimeout(aue_tick, sc, sc->aue_stat_ch);
1772
1773 /* Stop transfers. */
1774 if (sc->aue_ep[AUE_ENDPT_RX] != NULL) {
1775 err = usbd_abort_pipe(sc->aue_ep[AUE_ENDPT_RX]);
1776 if (err) {
1777 printf("%s: abort rx pipe failed: %s\n",
1778 USBDEVNAME(sc->aue_dev), usbd_errstr(err));
1779 }
1780 err = usbd_close_pipe(sc->aue_ep[AUE_ENDPT_RX]);
1781 if (err) {
1782 printf("%s: close rx pipe failed: %s\n",
1783 USBDEVNAME(sc->aue_dev), usbd_errstr(err));
1784 }
1785 sc->aue_ep[AUE_ENDPT_RX] = NULL;
1786 }
1787
1788 if (sc->aue_ep[AUE_ENDPT_TX] != NULL) {
1789 err = usbd_abort_pipe(sc->aue_ep[AUE_ENDPT_TX]);
1790 if (err) {
1791 printf("%s: abort tx pipe failed: %s\n",
1792 USBDEVNAME(sc->aue_dev), usbd_errstr(err));
1793 }
1794 err = usbd_close_pipe(sc->aue_ep[AUE_ENDPT_TX]);
1795 if (err) {
1796 printf("%s: close tx pipe failed: %s\n",
1797 USBDEVNAME(sc->aue_dev), usbd_errstr(err));
1798 }
1799 sc->aue_ep[AUE_ENDPT_TX] = NULL;
1800 }
1801
1802 if (sc->aue_ep[AUE_ENDPT_INTR] != NULL) {
1803 err = usbd_abort_pipe(sc->aue_ep[AUE_ENDPT_INTR]);
1804 if (err) {
1805 printf("%s: abort intr pipe failed: %s\n",
1806 USBDEVNAME(sc->aue_dev), usbd_errstr(err));
1807 }
1808 err = usbd_close_pipe(sc->aue_ep[AUE_ENDPT_INTR]);
1809 if (err) {
1810 printf("%s: close intr pipe failed: %s\n",
1811 USBDEVNAME(sc->aue_dev), usbd_errstr(err));
1812 }
1813 sc->aue_ep[AUE_ENDPT_INTR] = NULL;
1814 }
1815
1816 /* Free RX resources. */
1817 for (i = 0; i < AUE_RX_LIST_CNT; i++) {
1818 if (sc->aue_cdata.aue_rx_chain[i].aue_mbuf != NULL) {
1819 m_freem(sc->aue_cdata.aue_rx_chain[i].aue_mbuf);
1820 sc->aue_cdata.aue_rx_chain[i].aue_mbuf = NULL;
1821 }
1822 if (sc->aue_cdata.aue_rx_chain[i].aue_xfer != NULL) {
1823 usbd_free_xfer(sc->aue_cdata.aue_rx_chain[i].aue_xfer);
1824 sc->aue_cdata.aue_rx_chain[i].aue_xfer = NULL;
1825 }
1826 }
1827
1828 /* Free TX resources. */
1829 for (i = 0; i < AUE_TX_LIST_CNT; i++) {
1830 if (sc->aue_cdata.aue_tx_chain[i].aue_mbuf != NULL) {
1831 m_freem(sc->aue_cdata.aue_tx_chain[i].aue_mbuf);
1832 sc->aue_cdata.aue_tx_chain[i].aue_mbuf = NULL;
1833 }
1834 if (sc->aue_cdata.aue_tx_chain[i].aue_xfer != NULL) {
1835 usbd_free_xfer(sc->aue_cdata.aue_tx_chain[i].aue_xfer);
1836 sc->aue_cdata.aue_tx_chain[i].aue_xfer = NULL;
1837 }
1838 }
1839
1840 sc->aue_link = 0;
1841
1842 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
1843 }
1844
1845 #ifdef __FreeBSD__
1846 /*
1847 * Stop all chip I/O so that the kernel's probe routines don't
1848 * get confused by errant DMAs when rebooting.
1849 */
1850 static void
1851 aue_shutdown(dev)
1852 device_ptr_t dev;
1853 {
1854 struct aue_softc *sc = USBGETSOFTC(dev);
1855
1856 DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __FUNCTION__));
1857
1858 aue_reset(sc);
1859 aue_stop(sc);
1860 }
1861 #endif
1862