[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ]

FreeBSD/Linux Kernel Cross Reference
sys/dev/usb/if_aue.c

Version: -  FREEBSD  -  FREEBSD7  -  FREEBSD70  -  FREEBSD6  -  FREEBSD64  -  FREEBSD63  -  FREEBSD62  -  FREEBSD61  -  FREEBSD60  -  FREEBSD5  -  FREEBSD55  -  FREEBSD54  -  FREEBSD53  -  FREEBSD52  -  FREEBSD51  -  FREEBSD50  -  FREEBSD4  -  FREEBSD3  -  FREEBSD22  -  linux-2.6  -  linux-2.4.22  -  MK83  -  MK84  -  PLAN9  -  DFBSD  -  NETBSD  -  NETBSD5  -  NETBSD4  -  NETBSD3  -  NETBSD20  -  OPENBSD  -  xnu-517  -  xnu-792  -  xnu-792.6.70  -  xnu-1228  -  OPENSOLARIS  -  minix-3-1-1  -  TRUSTEDBSD-SEBSD  -  FREEBSD-LIBC  -  FREEBSD7-LIBC  -  FREEBSD6-LIBC  -  GLIBC27 
SearchContext: -  none  -  excerpts  -  bigexcerpts 

  1 /*-
  2  * Copyright (c) 1997, 1998, 1999, 2000
  3  *      Bill Paul <wpaul@ee.columbia.edu>.  All rights reserved.
  4  *
  5  * Copyright (c) 2006
  6  *      Alfred Perlstein <alfred@freebsd.org>. All rights reserved.
  7  *
  8  * Redistribution and use in source and binary forms, with or without
  9  * modification, are permitted provided that the following conditions
 10  * are met:
 11  * 1. Redistributions of source code must retain the above copyright
 12  *    notice, this list of conditions and the following disclaimer.
 13  * 2. Redistributions in binary form must reproduce the above copyright
 14  *    notice, this list of conditions and the following disclaimer in the
 15  *    documentation and/or other materials provided with the distribution.
 16  * 3. All advertising materials mentioning features or use of this software
 17  *    must display the following acknowledgement:
 18  *      This product includes software developed by Bill Paul.
 19  * 4. Neither the name of the author nor the names of any co-contributors
 20  *    may be used to endorse or promote products derived from this software
 21  *    without specific prior written permission.
 22  *
 23  * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
 24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 26  * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
 27  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 28  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 29  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 30  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 31  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 32  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
 33  * THE POSSIBILITY OF SUCH DAMAGE.
 34  */
 35 
 36 #include <sys/cdefs.h>
 37 __FBSDID("$FreeBSD: src/sys/dev/usb/if_aue.c,v 1.119 2008/11/12 13:58:59 keramida Exp $");
 38 
 39 /*
 40  * ADMtek AN986 Pegasus and AN8511 Pegasus II USB to ethernet driver.
 41  * Datasheet is available from http://www.admtek.com.tw.
 42  *
 43  * Written by Bill Paul <wpaul@ee.columbia.edu>
 44  * Electrical Engineering Department
 45  * Columbia University, New York City
 46  *
 47  * SMP locking by Alfred Perlstein <alfred@freebsd.org>.
 48  * RED Inc.
 49  */
 50 
 51 /*
 52  * The Pegasus chip uses four USB "endpoints" to provide 10/100 ethernet
 53  * support: the control endpoint for reading/writing registers, burst
 54  * read endpoint for packet reception, burst write for packet transmission
 55  * and one for "interrupts." The chip uses the same RX filter scheme
 56  * as the other ADMtek ethernet parts: one perfect filter entry for the
 57  * the station address and a 64-bit multicast hash table. The chip supports
 58  * both MII and HomePNA attachments.
 59  *
 60  * Since the maximum data transfer speed of USB is supposed to be 12Mbps,
 61  * you're never really going to get 100Mbps speeds from this device. I
 62  * think the idea is to allow the device to connect to 10 or 100Mbps
 63  * networks, not necessarily to provide 100Mbps performance. Also, since
 64  * the controller uses an external PHY chip, it's possible that board
 65  * designers might simply choose a 10Mbps PHY.
 66  *
 67  * Registers are accessed using usbd_do_request(). Packet transfers are
 68  * done using usbd_transfer() and friends.
 69  */
 70 
 71 #include <sys/param.h>
 72 #include <sys/systm.h>
 73 #include <sys/sockio.h>
 74 #include <sys/mbuf.h>
 75 #include <sys/malloc.h>
 76 #include <sys/kernel.h>
 77 #include <sys/kdb.h>
 78 #include <sys/lock.h>
 79 #include <sys/module.h>
 80 #include <sys/socket.h>
 81 #include <sys/sx.h>
 82 #include <sys/taskqueue.h>
 83 
 84 #include <net/if.h>
 85 #include <net/if_arp.h>
 86 #include <net/ethernet.h>
 87 #include <net/if_dl.h>
 88 #include <net/if_media.h>
 89 #include <net/if_types.h>
 90 
 91 #include <net/bpf.h>
 92 
 93 #include <sys/bus.h>
 94 #include <machine/bus.h>
 95 
 96 #include <dev/usb/usb.h>
 97 #include <dev/usb/usbdi.h>
 98 #include <dev/usb/usbdi_util.h>
 99 #include <dev/usb/usbdivar.h>
100 #include "usbdevs.h"
101 #include <dev/usb/usb_ethersubr.h>
102 
103 #include <dev/mii/mii.h>
104 #include <dev/mii/miivar.h>
105 
106 #include <dev/usb/if_auereg.h>
107 
108 MODULE_DEPEND(aue, usb, 1, 1, 1);
109 MODULE_DEPEND(aue, ether, 1, 1, 1);
110 MODULE_DEPEND(aue, miibus, 1, 1, 1);
111 
112 /* "device miibus" required.  See GENERIC if you get errors here. */
113 #include "miibus_if.h"
114 
115 /*
116  * Various supported device vendors/products.
117  */
118 struct aue_type {
119         struct usb_devno        aue_dev;
120         u_int16_t               aue_flags;
121 #define LSYS    0x0001          /* use Linksys reset */
122 #define PNA     0x0002          /* has Home PNA */
123 #define PII     0x0004          /* Pegasus II chip */
124 };
125 
126 static const struct aue_type aue_devs[] = {
127  {{ USB_VENDOR_3COM,            USB_PRODUCT_3COM_3C460B},         PII },
128  {{ USB_VENDOR_ABOCOM,          USB_PRODUCT_ABOCOM_XX1},          PNA|PII },
129  {{ USB_VENDOR_ABOCOM,          USB_PRODUCT_ABOCOM_XX2},          PII },
130  {{ USB_VENDOR_ABOCOM,          USB_PRODUCT_ABOCOM_UFE1000},      LSYS },
131  {{ USB_VENDOR_ABOCOM,          USB_PRODUCT_ABOCOM_XX4},          PNA },
132  {{ USB_VENDOR_ABOCOM,          USB_PRODUCT_ABOCOM_XX5},          PNA },
133  {{ USB_VENDOR_ABOCOM,          USB_PRODUCT_ABOCOM_XX6},          PII },
134  {{ USB_VENDOR_ABOCOM,          USB_PRODUCT_ABOCOM_XX7},          PII },
135  {{ USB_VENDOR_ABOCOM,          USB_PRODUCT_ABOCOM_XX8},          PII },
136  {{ USB_VENDOR_ABOCOM,          USB_PRODUCT_ABOCOM_XX9},          PNA },
137  {{ USB_VENDOR_ABOCOM,          USB_PRODUCT_ABOCOM_XX10},         0 },
138  {{ USB_VENDOR_ABOCOM,          USB_PRODUCT_ABOCOM_DSB650TX_PNA}, 0 },
139  {{ USB_VENDOR_ACCTON,          USB_PRODUCT_ACCTON_USB320_EC},    0 },
140  {{ USB_VENDOR_ACCTON,          USB_PRODUCT_ACCTON_SS1001},       PII },
141  {{ USB_VENDOR_ADMTEK,          USB_PRODUCT_ADMTEK_PEGASUS},      PNA },
142  {{ USB_VENDOR_ADMTEK,          USB_PRODUCT_ADMTEK_PEGASUSII},    PII },
143  {{ USB_VENDOR_ADMTEK,          USB_PRODUCT_ADMTEK_PEGASUSII_2},  PII },
144  {{ USB_VENDOR_ADMTEK,          USB_PRODUCT_ADMTEK_PEGASUSII_3},  PII },
145  {{ USB_VENDOR_ADMTEK,          USB_PRODUCT_ADMTEK_PEGASUSII_4},  PII },
146  {{ USB_VENDOR_AEI,             USB_PRODUCT_AEI_FASTETHERNET},    PII },
147  {{ USB_VENDOR_ALLIEDTELESYN,   USB_PRODUCT_ALLIEDTELESYN_ATUSB100}, PII },
148  {{ USB_VENDOR_ATEN,            USB_PRODUCT_ATEN_UC110T},         PII },
149  {{ USB_VENDOR_BELKIN,          USB_PRODUCT_BELKIN_USB2LAN},      PII },
150  {{ USB_VENDOR_BILLIONTON,      USB_PRODUCT_BILLIONTON_USB100},   0 },
151  {{ USB_VENDOR_BILLIONTON,      USB_PRODUCT_BILLIONTON_USBLP100}, PNA },
152  {{ USB_VENDOR_BILLIONTON,      USB_PRODUCT_BILLIONTON_USBEL100}, 0 },
153  {{ USB_VENDOR_BILLIONTON,      USB_PRODUCT_BILLIONTON_USBE100},  PII },
154  {{ USB_VENDOR_COREGA,          USB_PRODUCT_COREGA_FETHER_USB_TX}, 0 },
155  {{ USB_VENDOR_COREGA,          USB_PRODUCT_COREGA_FETHER_USB_TXS},PII },
156  {{ USB_VENDOR_DLINK,           USB_PRODUCT_DLINK_DSB650TX4},     LSYS|PII },
157  {{ USB_VENDOR_DLINK,           USB_PRODUCT_DLINK_DSB650TX1},     LSYS },
158  {{ USB_VENDOR_DLINK,           USB_PRODUCT_DLINK_DSB650TX},      LSYS },
159  {{ USB_VENDOR_DLINK,           USB_PRODUCT_DLINK_DSB650TX_PNA},  PNA },
160  {{ USB_VENDOR_DLINK,           USB_PRODUCT_DLINK_DSB650TX3},     LSYS|PII },
161  {{ USB_VENDOR_DLINK,           USB_PRODUCT_DLINK_DSB650TX2},     LSYS|PII },
162  {{ USB_VENDOR_DLINK,           USB_PRODUCT_DLINK_DSB650},        LSYS },
163  {{ USB_VENDOR_ELCON,           USB_PRODUCT_ELCON_PLAN},          PNA|PII },
164  {{ USB_VENDOR_ELECOM,          USB_PRODUCT_ELECOM_LDUSB20},      PII },
165  {{ USB_VENDOR_ELECOM,          USB_PRODUCT_ELECOM_LDUSBTX0},     0 },
166  {{ USB_VENDOR_ELECOM,          USB_PRODUCT_ELECOM_LDUSBTX1},     LSYS },
167  {{ USB_VENDOR_ELECOM,          USB_PRODUCT_ELECOM_LDUSBTX2},     0 },
168  {{ USB_VENDOR_ELECOM,          USB_PRODUCT_ELECOM_LDUSBTX3},     LSYS },
169  {{ USB_VENDOR_ELECOM,          USB_PRODUCT_ELECOM_LDUSBLTX},     PII },
170  {{ USB_VENDOR_ELSA,            USB_PRODUCT_ELSA_USB2ETHERNET},   0 },
171  {{ USB_VENDOR_GIGABYTE,        USB_PRODUCT_GIGABYTE_GNBR402W},   0 },
172  {{ USB_VENDOR_HAWKING,         USB_PRODUCT_HAWKING_UF100},       PII },
173  {{ USB_VENDOR_HP,              USB_PRODUCT_HP_HN210E},           PII },
174  {{ USB_VENDOR_IODATA,          USB_PRODUCT_IODATA_USBETTX},      0 },
175  {{ USB_VENDOR_IODATA,          USB_PRODUCT_IODATA_USBETTXS},     PII },
176  {{ USB_VENDOR_KINGSTON,        USB_PRODUCT_KINGSTON_KNU101TX},   0 },
177  {{ USB_VENDOR_LINKSYS,         USB_PRODUCT_LINKSYS_USB10TX1},    LSYS|PII },
178  {{ USB_VENDOR_LINKSYS,         USB_PRODUCT_LINKSYS_USB10T},      LSYS },
179  {{ USB_VENDOR_LINKSYS,         USB_PRODUCT_LINKSYS_USB100TX},    LSYS },
180  {{ USB_VENDOR_LINKSYS,         USB_PRODUCT_LINKSYS_USB100H1},    LSYS|PNA },
181  {{ USB_VENDOR_LINKSYS,         USB_PRODUCT_LINKSYS_USB10TA},     LSYS },
182  {{ USB_VENDOR_LINKSYS,         USB_PRODUCT_LINKSYS_USB10TX2},    LSYS|PII },
183  {{ USB_VENDOR_MELCO,           USB_PRODUCT_MELCO_LUATX1},        0 },
184  {{ USB_VENDOR_MELCO,           USB_PRODUCT_MELCO_LUATX5},        0 },
185  {{ USB_VENDOR_MELCO,           USB_PRODUCT_MELCO_LUA2TX5},       PII },
186  {{ USB_VENDOR_MICROSOFT,       USB_PRODUCT_MICROSOFT_MN110},     PII },
187  {{ USB_VENDOR_NETGEAR,         USB_PRODUCT_NETGEAR_FA101},       PII },
188  {{ USB_VENDOR_SIEMENS,         USB_PRODUCT_SIEMENS_SPEEDSTREAM}, PII },
189  {{ USB_VENDOR_SIIG2,           USB_PRODUCT_SIIG2_USBTOETHER},    PII },
190  {{ USB_VENDOR_SMARTBRIDGES,    USB_PRODUCT_SMARTBRIDGES_SMARTNIC},PII },
191  {{ USB_VENDOR_SMC,             USB_PRODUCT_SMC_2202USB},         0 },
192  {{ USB_VENDOR_SMC,             USB_PRODUCT_SMC_2206USB},         PII },
193  {{ USB_VENDOR_SOHOWARE,        USB_PRODUCT_SOHOWARE_NUB100},     0 },
194  {{ USB_VENDOR_SOHOWARE,        USB_PRODUCT_SOHOWARE_NUB110},     PII },
195 };
196 #define aue_lookup(v, p) ((const struct aue_type *)usb_lookup(aue_devs, v, p))
197 
198 static device_probe_t aue_match;
199 static device_attach_t aue_attach;
200 static device_detach_t aue_detach;
201 static device_shutdown_t aue_shutdown;
202 static miibus_readreg_t aue_miibus_readreg;
203 static miibus_writereg_t aue_miibus_writereg;
204 static miibus_statchg_t aue_miibus_statchg;
205 
206 static void aue_reset_pegasus_II(struct aue_softc *sc);
207 static int aue_encap(struct aue_softc *, struct mbuf *, int);
208 #ifdef AUE_INTR_PIPE
209 static void aue_intr(usbd_xfer_handle, usbd_private_handle, usbd_status);
210 #endif
211 static void aue_rxeof(usbd_xfer_handle, usbd_private_handle, usbd_status);
212 static void aue_rxeof_thread(struct aue_softc *sc);
213 static void aue_txeof(usbd_xfer_handle, usbd_private_handle, usbd_status);
214 static void aue_txeof_thread(struct aue_softc *);
215 static void aue_task_sched(struct aue_softc *, int);
216 static void aue_task(void *xsc, int pending);
217 static void aue_tick(void *);
218 static void aue_rxstart(struct ifnet *);
219 static void aue_rxstart_thread(struct aue_softc *);
220 static void aue_start(struct ifnet *);
221 static void aue_start_thread(struct aue_softc *);
222 static int aue_ioctl(struct ifnet *, u_long, caddr_t);
223 static void aue_init(void *);
224 static void aue_init_body(struct aue_softc *);
225 static void aue_stop(struct aue_softc *);
226 static void aue_watchdog(struct aue_softc *);
227 static int aue_ifmedia_upd(struct ifnet *);
228 static void aue_ifmedia_sts(struct ifnet *, struct ifmediareq *);
229 
230 static void aue_eeprom_getword(struct aue_softc *, int, u_int16_t *);
231 static void aue_read_eeprom(struct aue_softc *, caddr_t, int, int, int);
232 
233 static void aue_setmulti(struct aue_softc *);
234 static void aue_reset(struct aue_softc *);
235 
236 static int aue_csr_read_1(struct aue_softc *, int);
237 static int aue_csr_write_1(struct aue_softc *, int, int);
238 static int aue_csr_read_2(struct aue_softc *, int);
239 static int aue_csr_write_2(struct aue_softc *, int, int);
240 
241 static device_method_t aue_methods[] = {
242         /* Device interface */
243         DEVMETHOD(device_probe,         aue_match),
244         DEVMETHOD(device_attach,        aue_attach),
245         DEVMETHOD(device_detach,        aue_detach),
246         DEVMETHOD(device_shutdown,      aue_shutdown),
247 
248         /* bus interface */
249         DEVMETHOD(bus_print_child,      bus_generic_print_child),
250         DEVMETHOD(bus_driver_added,     bus_generic_driver_added),
251 
252         /* MII interface */
253         DEVMETHOD(miibus_readreg,       aue_miibus_readreg),
254         DEVMETHOD(miibus_writereg,      aue_miibus_writereg),
255         DEVMETHOD(miibus_statchg,       aue_miibus_statchg),
256 
257         { 0, 0 }
258 };
259 
260 static driver_t aue_driver = {
261         "aue",
262         aue_methods,
263         sizeof(struct aue_softc)
264 };
265 
266 static devclass_t aue_devclass;
267 
268 DRIVER_MODULE(aue, uhub, aue_driver, aue_devclass, usbd_driver_load, 0);
269 DRIVER_MODULE(miibus, aue, miibus_driver, miibus_devclass, 0, 0);
270 
271 #define AUE_SETBIT(sc, reg, x)                          \
272         aue_csr_write_1(sc, reg, aue_csr_read_1(sc, reg) | (x))
273 
274 #define AUE_CLRBIT(sc, reg, x)                          \
275         aue_csr_write_1(sc, reg, aue_csr_read_1(sc, reg) & ~(x))
276 
277 static int
278 aue_csr_read_1(struct aue_softc *sc, int reg)
279 {
280         usb_device_request_t    req;
281         usbd_status             err;
282         u_int8_t                val = 0;
283 
284         AUE_SXASSERTLOCKED(sc);
285 
286         req.bmRequestType = UT_READ_VENDOR_DEVICE;
287         req.bRequest = AUE_UR_READREG;
288         USETW(req.wValue, 0);
289         USETW(req.wIndex, reg);
290         USETW(req.wLength, 1);
291 
292         err = usbd_do_request(sc->aue_udev, &req, &val);
293 
294         if (err) {
295                 return (0);
296         }
297 
298         return (val);
299 }
300 
301 static int
302 aue_csr_read_2(struct aue_softc *sc, int reg)
303 {
304         usb_device_request_t    req;
305         usbd_status             err;
306         u_int16_t               val = 0;
307 
308         AUE_SXASSERTLOCKED(sc);
309 
310         req.bmRequestType = UT_READ_VENDOR_DEVICE;
311         req.bRequest = AUE_UR_READREG;
312         USETW(req.wValue, 0);
313         USETW(req.wIndex, reg);
314         USETW(req.wLength, 2);
315 
316         err = usbd_do_request(sc->aue_udev, &req, &val);
317 
318         if (err) {
319                 return (0);
320         }
321 
322         return (val);
323 }
324 
325 static int
326 aue_csr_write_1(struct aue_softc *sc, int reg, int val)
327 {
328         usb_device_request_t    req;
329         usbd_status             err;
330 
331         AUE_SXASSERTLOCKED(sc);
332 
333         req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
334         req.bRequest = AUE_UR_WRITEREG;
335         USETW(req.wValue, val);
336         USETW(req.wIndex, reg);
337         USETW(req.wLength, 1);
338 
339         err = usbd_do_request(sc->aue_udev, &req, &val);
340 
341         if (err) {
342                 return (-1);
343         }
344 
345         return (0);
346 }
347 
348 static int
349 aue_csr_write_2(struct aue_softc *sc, int reg, int val)
350 {
351         usb_device_request_t    req;
352         usbd_status             err;
353 
354         AUE_SXASSERTLOCKED(sc);
355 
356         req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
357         req.bRequest = AUE_UR_WRITEREG;
358         USETW(req.wValue, val);
359         USETW(req.wIndex, reg);
360         USETW(req.wLength, 2);
361 
362         err = usbd_do_request(sc->aue_udev, &req, &val);
363 
364         if (err) {
365                 return (-1);
366         }
367 
368         return (0);
369 }
370 
371 /*
372  * Read a word of data stored in the EEPROM at address 'addr.'
373  */
374 static void
375 aue_eeprom_getword(struct aue_softc *sc, int addr, u_int16_t *dest)
376 {
377         int             i;
378         u_int16_t       word = 0;
379 
380         aue_csr_write_1(sc, AUE_EE_REG, addr);
381         aue_csr_write_1(sc, AUE_EE_CTL, AUE_EECTL_READ);
382 
383         for (i = 0; i < AUE_TIMEOUT; i++) {
384                 if (aue_csr_read_1(sc, AUE_EE_CTL) & AUE_EECTL_DONE)
385                         break;
386         }
387 
388         if (i == AUE_TIMEOUT) {
389                 printf("aue%d: EEPROM read timed out\n",
390                     sc->aue_unit);
391         }
392 
393         word = aue_csr_read_2(sc, AUE_EE_DATA);
394         *dest = word;
395 
396         return;
397 }
398 
399 /*
400  * Read a sequence of words from the EEPROM.
401  */
402 static void
403 aue_read_eeprom(struct aue_softc *sc, caddr_t dest, int off, int cnt, int swap)
404 {
405         int                     i;
406         u_int16_t               word = 0, *ptr;
407 
408         for (i = 0; i < cnt; i++) {
409                 aue_eeprom_getword(sc, off + i, &word);
410                 ptr = (u_int16_t *)(dest + (i * 2));
411                 if (swap)
412                         *ptr = ntohs(word);
413                 else
414                         *ptr = word;
415         }
416 
417         return;
418 }
419 
420 static int
421 aue_miibus_readreg(device_t dev, int phy, int reg)
422 {
423         struct aue_softc        *sc = device_get_softc(dev);
424         int                     i;
425         u_int16_t               val = 0;
426 
427         /*
428          * The Am79C901 HomePNA PHY actually contains
429          * two transceivers: a 1Mbps HomePNA PHY and a
430          * 10Mbps full/half duplex ethernet PHY with
431          * NWAY autoneg. However in the ADMtek adapter,
432          * only the 1Mbps PHY is actually connected to
433          * anything, so we ignore the 10Mbps one. It
434          * happens to be configured for MII address 3,
435          * so we filter that out.
436          */
437         if (sc->aue_vendor == USB_VENDOR_ADMTEK &&
438             sc->aue_product == USB_PRODUCT_ADMTEK_PEGASUS) {
439                 if (phy == 3)
440                         return (0);
441 #ifdef notdef
442                 if (phy != 1)
443                         return (0);
444 #endif
445         }
446 
447         aue_csr_write_1(sc, AUE_PHY_ADDR, phy);
448         aue_csr_write_1(sc, AUE_PHY_CTL, reg | AUE_PHYCTL_READ);
449 
450         for (i = 0; i < AUE_TIMEOUT; i++) {
451                 if (aue_csr_read_1(sc, AUE_PHY_CTL) & AUE_PHYCTL_DONE)
452                         break;
453         }
454 
455         if (i == AUE_TIMEOUT) {
456                 printf("aue%d: MII read timed out\n", sc->aue_unit);
457         }
458 
459         val = aue_csr_read_2(sc, AUE_PHY_DATA);
460 
461         return (val);
462 }
463 
464 static int
465 aue_miibus_writereg(device_t dev, int phy, int reg, int data)
466 {
467         struct aue_softc        *sc = device_get_softc(dev);
468         int                     i;
469 
470         if (phy == 3)
471                 return (0);
472 
473         aue_csr_write_2(sc, AUE_PHY_DATA, data);
474         aue_csr_write_1(sc, AUE_PHY_ADDR, phy);
475         aue_csr_write_1(sc, AUE_PHY_CTL, reg | AUE_PHYCTL_WRITE);
476 
477         for (i = 0; i < AUE_TIMEOUT; i++) {
478                 if (aue_csr_read_1(sc, AUE_PHY_CTL) & AUE_PHYCTL_DONE)
479                         break;
480         }
481 
482         if (i == AUE_TIMEOUT) {
483                 printf("aue%d: MII read timed out\n",
484                     sc->aue_unit);
485         }
486 
487         return(0);
488 }
489 
490 static void
491 aue_miibus_statchg(device_t dev)
492 {
493         struct aue_softc        *sc = device_get_softc(dev);
494         struct mii_data         *mii = GET_MII(sc);
495 
496         AUE_CLRBIT(sc, AUE_CTL0, AUE_CTL0_RX_ENB | AUE_CTL0_TX_ENB);
497         if (IFM_SUBTYPE(mii->mii_media_active) == IFM_100_TX) {
498                 AUE_SETBIT(sc, AUE_CTL1, AUE_CTL1_SPEEDSEL);
499         } else {
500                 AUE_CLRBIT(sc, AUE_CTL1, AUE_CTL1_SPEEDSEL);
501         }
502 
503         if ((mii->mii_media_active & IFM_GMASK) == IFM_FDX)
504                 AUE_SETBIT(sc, AUE_CTL1, AUE_CTL1_DUPLEX);
505         else
506                 AUE_CLRBIT(sc, AUE_CTL1, AUE_CTL1_DUPLEX);
507 
508         AUE_SETBIT(sc, AUE_CTL0, AUE_CTL0_RX_ENB | AUE_CTL0_TX_ENB);
509 
510         /*
511          * Set the LED modes on the LinkSys adapter.
512          * This turns on the 'dual link LED' bin in the auxmode
513          * register of the Broadcom PHY.
514          */
515         if (sc->aue_flags & LSYS) {
516                 u_int16_t auxmode;
517                 auxmode = aue_miibus_readreg(dev, 0, 0x1b);
518                 aue_miibus_writereg(dev, 0, 0x1b, auxmode | 0x04);
519         }
520 
521         return;
522 }
523 
524 #define AUE_BITS        6
525 
526 static void
527 aue_setmulti(struct aue_softc *sc)
528 {
529         struct ifnet            *ifp;
530         struct ifmultiaddr      *ifma;
531         u_int32_t               h = 0, i;
532         u_int8_t                hashtbl[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
533 
534         AUE_SXASSERTLOCKED(sc);
535         ifp = sc->aue_ifp;
536 
537         if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) {
538                 AUE_SETBIT(sc, AUE_CTL0, AUE_CTL0_ALLMULTI);
539                 return;
540         }
541 
542         AUE_CLRBIT(sc, AUE_CTL0, AUE_CTL0_ALLMULTI);
543 
544         /* now program new ones */
545         IF_ADDR_LOCK(ifp);
546         TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
547         {
548                 if (ifma->ifma_addr->sa_family != AF_LINK)
549                         continue;
550                 h = ether_crc32_le(LLADDR((struct sockaddr_dl *)
551                     ifma->ifma_addr), ETHER_ADDR_LEN) & ((1 << AUE_BITS) - 1);
552                 hashtbl[(h >> 3)] |=  1 << (h & 0x7);
553         }
554         IF_ADDR_UNLOCK(ifp);
555 
556         /* write the hashtable */
557         for (i = 0; i < 8; i++)
558                 aue_csr_write_1(sc, AUE_MAR0 + i, hashtbl[i]);
559 
560         return;
561 }
562 
563 static void
564 aue_reset_pegasus_II(struct aue_softc *sc)
565 {
566         /* Magic constants taken from Linux driver. */
567         aue_csr_write_1(sc, AUE_REG_1D, 0);
568         aue_csr_write_1(sc, AUE_REG_7B, 2);
569 #if 0
570         if ((sc->aue_flags & HAS_HOME_PNA) && mii_mode)
571                 aue_csr_write_1(sc, AUE_REG_81, 6);
572         else
573 #endif
574                 aue_csr_write_1(sc, AUE_REG_81, 2);
575 }
576 
577 static void
578 aue_reset(struct aue_softc *sc)
579 {
580         int             i;
581 
582         AUE_SXASSERTLOCKED(sc);
583         AUE_SETBIT(sc, AUE_CTL1, AUE_CTL1_RESETMAC);
584 
585         for (i = 0; i < AUE_TIMEOUT; i++) {
586                 if (!(aue_csr_read_1(sc, AUE_CTL1) & AUE_CTL1_RESETMAC))
587                         break;
588         }
589 
590         if (i == AUE_TIMEOUT)
591                 printf("aue%d: reset failed\n", sc->aue_unit);
592 
593         /*
594          * The PHY(s) attached to the Pegasus chip may be held
595          * in reset until we flip on the GPIO outputs. Make sure
596          * to set the GPIO pins high so that the PHY(s) will
597          * be enabled.
598          *
599          * Note: We force all of the GPIO pins low first, *then*
600          * enable the ones we want.
601          */
602         aue_csr_write_1(sc, AUE_GPIO0, AUE_GPIO_OUT0|AUE_GPIO_SEL0);
603         aue_csr_write_1(sc, AUE_GPIO0, AUE_GPIO_OUT0|AUE_GPIO_SEL0|AUE_GPIO_SEL1);
604 
605         if (sc->aue_flags & LSYS) {
606                 /* Grrr. LinkSys has to be different from everyone else. */
607                 aue_csr_write_1(sc, AUE_GPIO0,
608                     AUE_GPIO_SEL0 | AUE_GPIO_SEL1);
609                 aue_csr_write_1(sc, AUE_GPIO0,
610                     AUE_GPIO_SEL0 | AUE_GPIO_SEL1 | AUE_GPIO_OUT0);
611         }
612 
613         if (sc->aue_flags & PII)
614                 aue_reset_pegasus_II(sc);
615 
616         /* Wait a little while for the chip to get its brains in order. */
617         DELAY(10000);
618 
619         return;
620 }
621 
622 /*
623  * Probe for a Pegasus chip.
624  */
625 static int
626 aue_match(device_t self)
627 {
628         struct usb_attach_arg *uaa = device_get_ivars(self);
629 
630         if (uaa->iface != NULL)
631                 return (UMATCH_NONE);
632 
633         /*
634          * Belkin USB Bluetooth dongles of the F8T012xx1 model series conflict
635          * with older Belkin USB2LAN adapters.  Skip if_aue if we detect one of
636          * the devices that look like Bluetooth adapters.
637          */
638         if (uaa->vendor == USB_VENDOR_BELKIN &&
639             uaa->product == USB_PRODUCT_BELKIN_F8T012 && uaa->release == 0x0413)
640                 return (UMATCH_NONE);
641 
642         return (aue_lookup(uaa->vendor, uaa->product) != NULL ?
643                 UMATCH_VENDOR_PRODUCT : UMATCH_NONE);
644 }
645 
646 /*
647  * Attach the interface. Allocate softc structures, do ifmedia
648  * setup and ethernet/BPF attach.
649  */
650 static int
651 aue_attach(device_t self)
652 {
653         struct aue_softc *sc = device_get_softc(self);
654         struct usb_attach_arg *uaa = device_get_ivars(self);
655         u_char                  eaddr[ETHER_ADDR_LEN];
656         struct ifnet            *ifp;
657         usbd_interface_handle   iface;
658         usbd_status             err;
659         usb_interface_descriptor_t      *id;
660         usb_endpoint_descriptor_t       *ed;
661         int                     i;
662 
663         sc->aue_dev = self;
664         sc->aue_udev = uaa->device;
665         sc->aue_unit = device_get_unit(self);
666 
667         if (usbd_set_config_no(sc->aue_udev, AUE_CONFIG_NO, 0)) {
668                 device_printf(self, "getting interface handle failed\n");
669                 return ENXIO;
670         }
671 
672         err = usbd_device2interface_handle(uaa->device, AUE_IFACE_IDX, &iface);
673         if (err) {
674                 device_printf(self, "getting interface handle failed\n");
675                 return ENXIO;
676         }
677 
678         sc->aue_iface = iface;
679         sc->aue_flags = aue_lookup(uaa->vendor, uaa->product)->aue_flags;
680 
681         sc->aue_product = uaa->product;
682         sc->aue_vendor = uaa->vendor;
683 
684         id = usbd_get_interface_descriptor(sc->aue_iface);
685 
686         /* Find endpoints. */
687         for (i = 0; i < id->bNumEndpoints; i++) {
688                 ed = usbd_interface2endpoint_descriptor(iface, i);
689                 if (ed == NULL) {
690                         device_printf(self, "couldn't get ep %d\n", i);
691                         return ENXIO;
692                 }
693                 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
694                     UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
695                         sc->aue_ed[AUE_ENDPT_RX] = ed->bEndpointAddress;
696                 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
697                            UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
698                         sc->aue_ed[AUE_ENDPT_TX] = ed->bEndpointAddress;
699                 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
700                            UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) {
701                         sc->aue_ed[AUE_ENDPT_INTR] = ed->bEndpointAddress;
702                 }
703         }
704 
705         mtx_init(&sc->aue_mtx, device_get_nameunit(self), MTX_NETWORK_LOCK,
706             MTX_DEF | MTX_RECURSE);
707         sx_init(&sc->aue_sx, device_get_nameunit(self));
708         TASK_INIT(&sc->aue_task, 0, aue_task, sc);
709         usb_ether_task_init(self, 0, &sc->aue_taskqueue);
710         AUE_SXLOCK(sc);
711 
712         /* Reset the adapter. */
713         aue_reset(sc);
714 
715         /*
716          * Get station address from the EEPROM.
717          */
718         aue_read_eeprom(sc, (caddr_t)&eaddr, 0, 3, 0);
719 
720         ifp = sc->aue_ifp = if_alloc(IFT_ETHER);
721         if (ifp == NULL) {
722                 device_printf(self, "can not if_alloc()\n");
723                 AUE_SXUNLOCK(sc);
724                 mtx_destroy(&sc->aue_mtx);
725                 sx_destroy(&sc->aue_sx);
726                 usb_ether_task_destroy(&sc->aue_taskqueue);
727                 return ENXIO;
728         }
729         ifp->if_softc = sc;
730         if_initname(ifp, "aue", sc->aue_unit);
731         ifp->if_mtu = ETHERMTU;
732         ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
733         ifp->if_ioctl = aue_ioctl;
734         ifp->if_start = aue_start;
735         ifp->if_init = aue_init;
736         IFQ_SET_MAXLEN(&ifp->if_snd, IFQ_MAXLEN);
737         ifp->if_snd.ifq_drv_maxlen = IFQ_MAXLEN;
738         IFQ_SET_READY(&ifp->if_snd);
739 
740         /*
741          * Do MII setup.
742          * NOTE: Doing this causes child devices to be attached to us,
743          * which we would normally disconnect at in the detach routine
744          * using device_delete_child(). However the USB code is set up
745          * such that when this driver is removed, all children devices
746          * are removed as well. In effect, the USB code ends up detaching
747          * all of our children for us, so we don't have to do is ourselves
748          * in aue_detach(). It's important to point this out since if
749          * we *do* try to detach the child devices ourselves, we will
750          * end up getting the children deleted twice, which will crash
751          * the system.
752          */
753         if (mii_phy_probe(self, &sc->aue_miibus,
754             aue_ifmedia_upd, aue_ifmedia_sts)) {
755                 device_printf(self, "MII without any PHY!\n");
756                 if_free(ifp);
757                 AUE_SXUNLOCK(sc);
758                 mtx_destroy(&sc->aue_mtx);
759                 sx_destroy(&sc->aue_sx);
760                 usb_ether_task_destroy(&sc->aue_taskqueue);
761                 return ENXIO;
762         }
763 
764         sc->aue_qdat.ifp = ifp;
765         sc->aue_qdat.if_rxstart = aue_rxstart;
766 
767         /*
768          * Call MI attach routine.
769          */
770         ether_ifattach(ifp, eaddr);
771         usb_register_netisr();
772         sc->aue_dying = 0;
773         sc->aue_link = 1;
774 
775         AUE_SXUNLOCK(sc);
776         return 0;
777 }
778 
779 static int
780 aue_detach(device_t dev)
781 {
782         struct aue_softc        *sc;
783         struct ifnet            *ifp;
784 
785         sc = device_get_softc(dev);
786         AUE_SXLOCK(sc);
787         ifp = sc->aue_ifp;
788         ether_ifdetach(ifp);
789         sc->aue_dying = 1;
790         AUE_SXUNLOCK(sc);
791         callout_drain(&sc->aue_tick_callout);
792         usb_ether_task_drain(&sc->aue_taskqueue, &sc->aue_task);
793         usb_ether_task_destroy(&sc->aue_taskqueue);
794         if_free(ifp);
795 
796         if (sc->aue_ep[AUE_ENDPT_TX] != NULL)
797                 usbd_abort_pipe(sc->aue_ep[AUE_ENDPT_TX]);
798         if (sc->aue_ep[AUE_ENDPT_RX] != NULL)
799                 usbd_abort_pipe(sc->aue_ep[AUE_ENDPT_RX]);
800 #ifdef AUE_INTR_PIPE
801         if (sc->aue_ep[AUE_ENDPT_INTR] != NULL)
802                 usbd_abort_pipe(sc->aue_ep[AUE_ENDPT_INTR]);
803 #endif
804 
805         mtx_destroy(&sc->aue_mtx);
806         sx_destroy(&sc->aue_sx);
807 
808         return (0);
809 }
810 
811 static void
812 aue_rxstart(struct ifnet *ifp)
813 {
814         struct aue_softc        *sc = ifp->if_softc;
815         aue_task_sched(sc, AUE_TASK_RXSTART);
816 }
817 
818 static void
819 aue_rxstart_thread(struct aue_softc *sc)
820 {
821         struct ue_chain *c;
822         struct ifnet *ifp;
823 
824         ifp = sc->aue_ifp;
825 
826         sc = ifp->if_softc;
827         AUE_SXASSERTLOCKED(sc);
828         c = &sc->aue_cdata.ue_rx_chain[sc->aue_cdata.ue_rx_prod];
829 
830         c->ue_mbuf = usb_ether_newbuf();
831         if (c->ue_mbuf == NULL) {
832                 device_printf(sc->aue_dev, "no memory for rx list -- packet "
833                     "dropped!\n");
834                 ifp->if_ierrors++;
835                 AUE_UNLOCK(sc);
836                 return;
837         }
838 
839         /* Setup new transfer. */
840         usbd_setup_xfer(c->ue_xfer, sc->aue_ep[AUE_ENDPT_RX],
841             c, mtod(c->ue_mbuf, char *), UE_BUFSZ, USBD_SHORT_XFER_OK,
842             USBD_NO_TIMEOUT, aue_rxeof);
843         usbd_transfer(c->ue_xfer);
844 
845         return;
846 }
847 
848 /*
849  * A frame has been uploaded: pass the resulting mbuf chain up to
850  * the higher level protocols.
851  */
852 static void
853 aue_rxeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
854 {
855         struct ue_chain *c = priv;
856         c->ue_status = status;
857         aue_task_sched(c->ue_sc, AUE_TASK_RXEOF);
858 }
859 
860 static void
861 aue_rxeof_thread(struct aue_softc *sc)
862 {
863         struct ue_chain *c = &(sc->aue_cdata.ue_rx_chain[0]);
864         struct mbuf             *m;
865         struct ifnet            *ifp;
866         int                     total_len = 0;
867         struct aue_rxpkt        r;
868         usbd_status             status = c->ue_status;
869 
870 
871         AUE_SXASSERTLOCKED(sc);
872         ifp = sc->aue_ifp;
873 
874         if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
875                 return;
876         }
877 
878         if (status != USBD_NORMAL_COMPLETION) {
879                 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
880                         return;
881                 }
882                 if (usbd_ratecheck(&sc->aue_rx_notice))
883                         device_printf(sc->aue_dev, "usb error on rx: %s\n",
884                             usbd_errstr(status));
885                 if (status == USBD_STALLED)
886                         usbd_clear_endpoint_stall(sc->aue_ep[AUE_ENDPT_RX]);
887                 goto done;
888         }
889 
890         usbd_get_xfer_status(c->ue_xfer, NULL, NULL, &total_len, NULL);
891 
892         if (total_len <= 4 + ETHER_CRC_LEN) {
893                 ifp->if_ierrors++;
894                 goto done;
895         }
896 
897         m = c->ue_mbuf;
898         bcopy(mtod(m, char *) + total_len - 4, (char *)&r, sizeof(r));
899 
900         /* Turn off all the non-error bits in the rx status word. */
901         r.aue_rxstat &= AUE_RXSTAT_MASK;
902 
903         if (r.aue_rxstat) {
904                 ifp->if_ierrors++;
905                 goto done;
906         }
907 
908         /* No errors; receive the packet. */
909         total_len -= (4 + ETHER_CRC_LEN);
910 
911         ifp->if_ipackets++;
912         m->m_pkthdr.rcvif = (void *)&sc->aue_qdat;
913         m->m_pkthdr.len = m->m_len = total_len;
914 
915         /* Put the packet on the special USB input queue. */
916         usb_ether_input(m);
917         return;
918 done:
919 
920         /* Setup new transfer. */
921         usbd_setup_xfer(c->ue_xfer, sc->aue_ep[AUE_ENDPT_RX],