]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/usb/net/if_aue.c
Update tcsh to 6.21.00.
[FreeBSD/FreeBSD.git] / sys / dev / usb / net / if_aue.c
1 /*-
2  * SPDX-License-Identifier: BSD-4-Clause
3  *
4  * Copyright (c) 1997, 1998, 1999, 2000
5  *      Bill Paul <wpaul@ee.columbia.edu>.  All rights reserved.
6  *
7  * Copyright (c) 2006
8  *      Alfred Perlstein <alfred@FreeBSD.org>. All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *      This product includes software developed by Bill Paul.
21  * 4. Neither the name of the author nor the names of any co-contributors
22  *    may be used to endorse or promote products derived from this software
23  *    without specific prior written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28  * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
29  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
35  * THE POSSIBILITY OF SUCH DAMAGE.
36  */
37
38 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD$");
40
41 /*
42  * ADMtek AN986 Pegasus and AN8511 Pegasus II USB to ethernet driver.
43  * Datasheet is available from http://www.admtek.com.tw.
44  *
45  * Written by Bill Paul <wpaul@ee.columbia.edu>
46  * Electrical Engineering Department
47  * Columbia University, New York City
48  *
49  * SMP locking by Alfred Perlstein <alfred@FreeBSD.org>.
50  * RED Inc.
51  */
52
53 /*
54  * The Pegasus chip uses four USB "endpoints" to provide 10/100 ethernet
55  * support: the control endpoint for reading/writing registers, burst
56  * read endpoint for packet reception, burst write for packet transmission
57  * and one for "interrupts." The chip uses the same RX filter scheme
58  * as the other ADMtek ethernet parts: one perfect filter entry for the
59  * the station address and a 64-bit multicast hash table. The chip supports
60  * both MII and HomePNA attachments.
61  *
62  * Since the maximum data transfer speed of USB is supposed to be 12Mbps,
63  * you're never really going to get 100Mbps speeds from this device. I
64  * think the idea is to allow the device to connect to 10 or 100Mbps
65  * networks, not necessarily to provide 100Mbps performance. Also, since
66  * the controller uses an external PHY chip, it's possible that board
67  * designers might simply choose a 10Mbps PHY.
68  *
69  * Registers are accessed using uether_do_request(). Packet
70  * transfers are done using usbd_transfer() and friends.
71  */
72
73 #include <sys/stdint.h>
74 #include <sys/stddef.h>
75 #include <sys/param.h>
76 #include <sys/queue.h>
77 #include <sys/types.h>
78 #include <sys/systm.h>
79 #include <sys/socket.h>
80 #include <sys/kernel.h>
81 #include <sys/bus.h>
82 #include <sys/module.h>
83 #include <sys/lock.h>
84 #include <sys/mutex.h>
85 #include <sys/condvar.h>
86 #include <sys/sysctl.h>
87 #include <sys/sx.h>
88 #include <sys/unistd.h>
89 #include <sys/callout.h>
90 #include <sys/malloc.h>
91 #include <sys/priv.h>
92
93 #include <net/if.h>
94 #include <net/if_var.h>
95 #include <net/if_media.h>
96
97 #include <dev/mii/mii.h>
98 #include <dev/mii/miivar.h>
99
100 #include <dev/usb/usb.h>
101 #include <dev/usb/usbdi.h>
102 #include <dev/usb/usbdi_util.h>
103 #include "usbdevs.h"
104
105 #define USB_DEBUG_VAR aue_debug
106 #include <dev/usb/usb_debug.h>
107 #include <dev/usb/usb_process.h>
108
109 #include <dev/usb/net/usb_ethernet.h>
110 #include <dev/usb/net/if_auereg.h>
111
112 #include "miibus_if.h"
113
114 #ifdef USB_DEBUG
115 static int aue_debug = 0;
116
117 static SYSCTL_NODE(_hw_usb, OID_AUTO, aue, CTLFLAG_RW, 0, "USB aue");
118 SYSCTL_INT(_hw_usb_aue, OID_AUTO, debug, CTLFLAG_RWTUN, &aue_debug, 0,
119     "Debug level");
120 #endif
121
122 /*
123  * Various supported device vendors/products.
124  */
125 static const STRUCT_USB_HOST_ID aue_devs[] = {
126 #define AUE_DEV(v,p,i) { USB_VPI(USB_VENDOR_##v, USB_PRODUCT_##v##_##p, i) }
127     AUE_DEV(3COM, 3C460B, AUE_FLAG_PII),
128     AUE_DEV(ABOCOM, DSB650TX_PNA, 0),
129     AUE_DEV(ABOCOM, UFE1000, AUE_FLAG_LSYS),
130     AUE_DEV(ABOCOM, XX10, 0),
131     AUE_DEV(ABOCOM, XX1, AUE_FLAG_PNA | AUE_FLAG_PII),
132     AUE_DEV(ABOCOM, XX2, AUE_FLAG_PII),
133     AUE_DEV(ABOCOM, XX4, AUE_FLAG_PNA),
134     AUE_DEV(ABOCOM, XX5, AUE_FLAG_PNA),
135     AUE_DEV(ABOCOM, XX6, AUE_FLAG_PII),
136     AUE_DEV(ABOCOM, XX7, AUE_FLAG_PII),
137     AUE_DEV(ABOCOM, XX8, AUE_FLAG_PII),
138     AUE_DEV(ABOCOM, XX9, AUE_FLAG_PNA),
139     AUE_DEV(ACCTON, SS1001, AUE_FLAG_PII),
140     AUE_DEV(ACCTON, USB320_EC, 0),
141     AUE_DEV(ADMTEK, PEGASUSII_2, AUE_FLAG_PII),
142     AUE_DEV(ADMTEK, PEGASUSII_3, AUE_FLAG_PII),
143     AUE_DEV(ADMTEK, PEGASUSII_4, AUE_FLAG_PII),
144     AUE_DEV(ADMTEK, PEGASUSII, AUE_FLAG_PII),
145     AUE_DEV(ADMTEK, PEGASUS, AUE_FLAG_PNA | AUE_FLAG_DUAL_PHY),
146     AUE_DEV(AEI, FASTETHERNET, AUE_FLAG_PII),
147     AUE_DEV(ALLIEDTELESYN, ATUSB100, AUE_FLAG_PII),
148     AUE_DEV(ATEN, UC110T, AUE_FLAG_PII),
149     AUE_DEV(BELKIN, USB2LAN, AUE_FLAG_PII),
150     AUE_DEV(BILLIONTON, USB100, 0),
151     AUE_DEV(BILLIONTON, USBE100, AUE_FLAG_PII),
152     AUE_DEV(BILLIONTON, USBEL100, 0),
153     AUE_DEV(BILLIONTON, USBLP100, AUE_FLAG_PNA),
154     AUE_DEV(COREGA, FETHER_USB_TXS, AUE_FLAG_PII),
155     AUE_DEV(COREGA, FETHER_USB_TX, 0),
156     AUE_DEV(DLINK, DSB650TX1, AUE_FLAG_LSYS),
157     AUE_DEV(DLINK, DSB650TX2, AUE_FLAG_LSYS | AUE_FLAG_PII),
158     AUE_DEV(DLINK, DSB650TX3, AUE_FLAG_LSYS | AUE_FLAG_PII),
159     AUE_DEV(DLINK, DSB650TX4, AUE_FLAG_LSYS | AUE_FLAG_PII),
160     AUE_DEV(DLINK, DSB650TX_PNA, AUE_FLAG_PNA),
161     AUE_DEV(DLINK, DSB650TX, AUE_FLAG_LSYS),
162     AUE_DEV(DLINK, DSB650, AUE_FLAG_LSYS),
163     AUE_DEV(ELCON, PLAN, AUE_FLAG_PNA | AUE_FLAG_PII),
164     AUE_DEV(ELECOM, LDUSB20, AUE_FLAG_PII),
165     AUE_DEV(ELECOM, LDUSBLTX, AUE_FLAG_PII),
166     AUE_DEV(ELECOM, LDUSBTX0, 0),
167     AUE_DEV(ELECOM, LDUSBTX1, AUE_FLAG_LSYS),
168     AUE_DEV(ELECOM, LDUSBTX2, 0),
169     AUE_DEV(ELECOM, LDUSBTX3, AUE_FLAG_LSYS),
170     AUE_DEV(ELSA, USB2ETHERNET, 0),
171     AUE_DEV(GIGABYTE, GNBR402W, 0),
172     AUE_DEV(HAWKING, UF100, AUE_FLAG_PII),
173     AUE_DEV(HP, HN210E, AUE_FLAG_PII),
174     AUE_DEV(IODATA, USBETTXS, AUE_FLAG_PII),
175     AUE_DEV(IODATA, USBETTX, 0),
176     AUE_DEV(KINGSTON, KNU101TX, 0),
177     AUE_DEV(LINKSYS, USB100H1, AUE_FLAG_LSYS | AUE_FLAG_PNA),
178     AUE_DEV(LINKSYS, USB100TX, AUE_FLAG_LSYS),
179     AUE_DEV(LINKSYS, USB10TA, AUE_FLAG_LSYS),
180     AUE_DEV(LINKSYS, USB10TX1, AUE_FLAG_LSYS | AUE_FLAG_PII),
181     AUE_DEV(LINKSYS, USB10TX2, AUE_FLAG_LSYS | AUE_FLAG_PII),
182     AUE_DEV(LINKSYS, USB10T, AUE_FLAG_LSYS),
183     AUE_DEV(MELCO, LUA2TX5, AUE_FLAG_PII),
184     AUE_DEV(MELCO, LUATX1, 0),
185     AUE_DEV(MELCO, LUATX5, 0),
186     AUE_DEV(MICROSOFT, MN110, AUE_FLAG_PII),
187     AUE_DEV(NETGEAR, FA101, AUE_FLAG_PII),
188     AUE_DEV(SIEMENS, SPEEDSTREAM, AUE_FLAG_PII),
189     AUE_DEV(SIIG2, USBTOETHER, AUE_FLAG_PII),
190     AUE_DEV(SMARTBRIDGES, SMARTNIC, AUE_FLAG_PII),
191     AUE_DEV(SMC, 2202USB, 0),
192     AUE_DEV(SMC, 2206USB, AUE_FLAG_PII),
193     AUE_DEV(SOHOWARE, NUB100, 0),
194     AUE_DEV(SOHOWARE, NUB110, AUE_FLAG_PII),
195 #undef AUE_DEV
196 };
197
198 /* prototypes */
199
200 static device_probe_t aue_probe;
201 static device_attach_t aue_attach;
202 static device_detach_t aue_detach;
203 static miibus_readreg_t aue_miibus_readreg;
204 static miibus_writereg_t aue_miibus_writereg;
205 static miibus_statchg_t aue_miibus_statchg;
206
207 static usb_callback_t aue_intr_callback;
208 static usb_callback_t aue_bulk_read_callback;
209 static usb_callback_t aue_bulk_write_callback;
210
211 static uether_fn_t aue_attach_post;
212 static uether_fn_t aue_init;
213 static uether_fn_t aue_stop;
214 static uether_fn_t aue_start;
215 static uether_fn_t aue_tick;
216 static uether_fn_t aue_setmulti;
217 static uether_fn_t aue_setpromisc;
218
219 static uint8_t  aue_csr_read_1(struct aue_softc *, uint16_t);
220 static uint16_t aue_csr_read_2(struct aue_softc *, uint16_t);
221 static void     aue_csr_write_1(struct aue_softc *, uint16_t, uint8_t);
222 static void     aue_csr_write_2(struct aue_softc *, uint16_t, uint16_t);
223 static uint16_t aue_eeprom_getword(struct aue_softc *, int);
224 static void     aue_reset(struct aue_softc *);
225 static void     aue_reset_pegasus_II(struct aue_softc *);
226
227 static int      aue_ifmedia_upd(struct ifnet *);
228 static void     aue_ifmedia_sts(struct ifnet *, struct ifmediareq *);
229
230 static const struct usb_config aue_config[AUE_N_TRANSFER] = {
231
232         [AUE_BULK_DT_WR] = {
233                 .type = UE_BULK,
234                 .endpoint = UE_ADDR_ANY,
235                 .direction = UE_DIR_OUT,
236                 .bufsize = (MCLBYTES + 2),
237                 .flags = {.pipe_bof = 1,.force_short_xfer = 1,},
238                 .callback = aue_bulk_write_callback,
239                 .timeout = 10000,       /* 10 seconds */
240         },
241
242         [AUE_BULK_DT_RD] = {
243                 .type = UE_BULK,
244                 .endpoint = UE_ADDR_ANY,
245                 .direction = UE_DIR_IN,
246                 .bufsize = (MCLBYTES + 4 + ETHER_CRC_LEN),
247                 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
248                 .callback = aue_bulk_read_callback,
249         },
250
251         [AUE_INTR_DT_RD] = {
252                 .type = UE_INTERRUPT,
253                 .endpoint = UE_ADDR_ANY,
254                 .direction = UE_DIR_IN,
255                 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
256                 .bufsize = 0,   /* use wMaxPacketSize */
257                 .callback = aue_intr_callback,
258         },
259 };
260
261 static device_method_t aue_methods[] = {
262         /* Device interface */
263         DEVMETHOD(device_probe, aue_probe),
264         DEVMETHOD(device_attach, aue_attach),
265         DEVMETHOD(device_detach, aue_detach),
266
267         /* MII interface */
268         DEVMETHOD(miibus_readreg, aue_miibus_readreg),
269         DEVMETHOD(miibus_writereg, aue_miibus_writereg),
270         DEVMETHOD(miibus_statchg, aue_miibus_statchg),
271
272         DEVMETHOD_END
273 };
274
275 static driver_t aue_driver = {
276         .name = "aue",
277         .methods = aue_methods,
278         .size = sizeof(struct aue_softc)
279 };
280
281 static devclass_t aue_devclass;
282
283 DRIVER_MODULE(aue, uhub, aue_driver, aue_devclass, NULL, 0);
284 DRIVER_MODULE(miibus, aue, miibus_driver, miibus_devclass, 0, 0);
285 MODULE_DEPEND(aue, uether, 1, 1, 1);
286 MODULE_DEPEND(aue, usb, 1, 1, 1);
287 MODULE_DEPEND(aue, ether, 1, 1, 1);
288 MODULE_DEPEND(aue, miibus, 1, 1, 1);
289 MODULE_VERSION(aue, 1);
290 USB_PNP_HOST_INFO(aue_devs);
291
292 static const struct usb_ether_methods aue_ue_methods = {
293         .ue_attach_post = aue_attach_post,
294         .ue_start = aue_start,
295         .ue_init = aue_init,
296         .ue_stop = aue_stop,
297         .ue_tick = aue_tick,
298         .ue_setmulti = aue_setmulti,
299         .ue_setpromisc = aue_setpromisc,
300         .ue_mii_upd = aue_ifmedia_upd,
301         .ue_mii_sts = aue_ifmedia_sts,
302 };
303
304 #define AUE_SETBIT(sc, reg, x) \
305         aue_csr_write_1(sc, reg, aue_csr_read_1(sc, reg) | (x))
306
307 #define AUE_CLRBIT(sc, reg, x) \
308         aue_csr_write_1(sc, reg, aue_csr_read_1(sc, reg) & ~(x))
309
310 static uint8_t
311 aue_csr_read_1(struct aue_softc *sc, uint16_t reg)
312 {
313         struct usb_device_request req;
314         usb_error_t err;
315         uint8_t val;
316
317         req.bmRequestType = UT_READ_VENDOR_DEVICE;
318         req.bRequest = AUE_UR_READREG;
319         USETW(req.wValue, 0);
320         USETW(req.wIndex, reg);
321         USETW(req.wLength, 1);
322
323         err = uether_do_request(&sc->sc_ue, &req, &val, 1000);
324         if (err)
325                 return (0);
326         return (val);
327 }
328
329 static uint16_t
330 aue_csr_read_2(struct aue_softc *sc, uint16_t reg)
331 {
332         struct usb_device_request req;
333         usb_error_t err;
334         uint16_t val;
335
336         req.bmRequestType = UT_READ_VENDOR_DEVICE;
337         req.bRequest = AUE_UR_READREG;
338         USETW(req.wValue, 0);
339         USETW(req.wIndex, reg);
340         USETW(req.wLength, 2);
341
342         err = uether_do_request(&sc->sc_ue, &req, &val, 1000);
343         if (err)
344                 return (0);
345         return (le16toh(val));
346 }
347
348 static void
349 aue_csr_write_1(struct aue_softc *sc, uint16_t reg, uint8_t val)
350 {
351         struct usb_device_request req;
352
353         req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
354         req.bRequest = AUE_UR_WRITEREG;
355         req.wValue[0] = val;
356         req.wValue[1] = 0;
357         USETW(req.wIndex, reg);
358         USETW(req.wLength, 1);
359
360         if (uether_do_request(&sc->sc_ue, &req, &val, 1000)) {
361                 /* error ignored */
362         }
363 }
364
365 static void
366 aue_csr_write_2(struct aue_softc *sc, uint16_t reg, uint16_t val)
367 {
368         struct usb_device_request req;
369
370         req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
371         req.bRequest = AUE_UR_WRITEREG;
372         USETW(req.wValue, val);
373         USETW(req.wIndex, reg);
374         USETW(req.wLength, 2);
375
376         val = htole16(val);
377
378         if (uether_do_request(&sc->sc_ue, &req, &val, 1000)) {
379                 /* error ignored */
380         }
381 }
382
383 /*
384  * Read a word of data stored in the EEPROM at address 'addr.'
385  */
386 static uint16_t
387 aue_eeprom_getword(struct aue_softc *sc, int addr)
388 {
389         int i;
390
391         aue_csr_write_1(sc, AUE_EE_REG, addr);
392         aue_csr_write_1(sc, AUE_EE_CTL, AUE_EECTL_READ);
393
394         for (i = 0; i != AUE_TIMEOUT; i++) {
395                 if (aue_csr_read_1(sc, AUE_EE_CTL) & AUE_EECTL_DONE)
396                         break;
397                 if (uether_pause(&sc->sc_ue, hz / 100))
398                         break;
399         }
400
401         if (i == AUE_TIMEOUT)
402                 device_printf(sc->sc_ue.ue_dev, "EEPROM read timed out\n");
403
404         return (aue_csr_read_2(sc, AUE_EE_DATA));
405 }
406
407 /*
408  * Read station address(offset 0) from the EEPROM.
409  */
410 static void
411 aue_read_mac(struct aue_softc *sc, uint8_t *eaddr)
412 {
413         int i, offset;
414         uint16_t word;
415
416         for (i = 0, offset = 0; i < ETHER_ADDR_LEN / 2; i++) {
417                 word = aue_eeprom_getword(sc, offset + i);
418                 eaddr[i * 2] = (uint8_t)word;
419                 eaddr[i * 2 + 1] = (uint8_t)(word >> 8);
420         }
421 }
422
423 static int
424 aue_miibus_readreg(device_t dev, int phy, int reg)
425 {
426         struct aue_softc *sc = device_get_softc(dev);
427         int i, locked;
428         uint16_t val = 0;
429
430         locked = mtx_owned(&sc->sc_mtx);
431         if (!locked)
432                 AUE_LOCK(sc);
433
434         /*
435          * The Am79C901 HomePNA PHY actually contains two transceivers: a 1Mbps
436          * HomePNA PHY and a 10Mbps full/half duplex ethernet PHY with NWAY
437          * autoneg. However in the ADMtek adapter, only the 1Mbps PHY is
438          * actually connected to anything, so we ignore the 10Mbps one. It
439          * happens to be configured for MII address 3, so we filter that out.
440          */
441         if (sc->sc_flags & AUE_FLAG_DUAL_PHY) {
442                 if (phy == 3)
443                         goto done;
444 #if 0
445                 if (phy != 1)
446                         goto done;
447 #endif
448         }
449         aue_csr_write_1(sc, AUE_PHY_ADDR, phy);
450         aue_csr_write_1(sc, AUE_PHY_CTL, reg | AUE_PHYCTL_READ);
451
452         for (i = 0; i != AUE_TIMEOUT; i++) {
453                 if (aue_csr_read_1(sc, AUE_PHY_CTL) & AUE_PHYCTL_DONE)
454                         break;
455                 if (uether_pause(&sc->sc_ue, hz / 100))
456                         break;
457         }
458
459         if (i == AUE_TIMEOUT)
460                 device_printf(sc->sc_ue.ue_dev, "MII read timed out\n");
461
462         val = aue_csr_read_2(sc, AUE_PHY_DATA);
463
464 done:
465         if (!locked)
466                 AUE_UNLOCK(sc);
467         return (val);
468 }
469
470 static int
471 aue_miibus_writereg(device_t dev, int phy, int reg, int data)
472 {
473         struct aue_softc *sc = device_get_softc(dev);
474         int i;
475         int locked;
476
477         if (phy == 3)
478                 return (0);
479
480         locked = mtx_owned(&sc->sc_mtx);
481         if (!locked)
482                 AUE_LOCK(sc);
483
484         aue_csr_write_2(sc, AUE_PHY_DATA, data);
485         aue_csr_write_1(sc, AUE_PHY_ADDR, phy);
486         aue_csr_write_1(sc, AUE_PHY_CTL, reg | AUE_PHYCTL_WRITE);
487
488         for (i = 0; i != AUE_TIMEOUT; i++) {
489                 if (aue_csr_read_1(sc, AUE_PHY_CTL) & AUE_PHYCTL_DONE)
490                         break;
491                 if (uether_pause(&sc->sc_ue, hz / 100))
492                         break;
493         }
494
495         if (i == AUE_TIMEOUT)
496                 device_printf(sc->sc_ue.ue_dev, "MII write timed out\n");
497
498         if (!locked)
499                 AUE_UNLOCK(sc);
500         return (0);
501 }
502
503 static void
504 aue_miibus_statchg(device_t dev)
505 {
506         struct aue_softc *sc = device_get_softc(dev);
507         struct mii_data *mii = GET_MII(sc);
508         int locked;
509
510         locked = mtx_owned(&sc->sc_mtx);
511         if (!locked)
512                 AUE_LOCK(sc);
513
514         AUE_CLRBIT(sc, AUE_CTL0, AUE_CTL0_RX_ENB | AUE_CTL0_TX_ENB);
515         if (IFM_SUBTYPE(mii->mii_media_active) == IFM_100_TX)
516                 AUE_SETBIT(sc, AUE_CTL1, AUE_CTL1_SPEEDSEL);
517         else
518                 AUE_CLRBIT(sc, AUE_CTL1, AUE_CTL1_SPEEDSEL);
519
520         if ((mii->mii_media_active & IFM_GMASK) == IFM_FDX)
521                 AUE_SETBIT(sc, AUE_CTL1, AUE_CTL1_DUPLEX);
522         else
523                 AUE_CLRBIT(sc, AUE_CTL1, AUE_CTL1_DUPLEX);
524
525         AUE_SETBIT(sc, AUE_CTL0, AUE_CTL0_RX_ENB | AUE_CTL0_TX_ENB);
526
527         /*
528          * Set the LED modes on the LinkSys adapter.
529          * This turns on the 'dual link LED' bin in the auxmode
530          * register of the Broadcom PHY.
531          */
532         if (sc->sc_flags & AUE_FLAG_LSYS) {
533                 uint16_t auxmode;
534
535                 auxmode = aue_miibus_readreg(dev, 0, 0x1b);
536                 aue_miibus_writereg(dev, 0, 0x1b, auxmode | 0x04);
537         }
538         if (!locked)
539                 AUE_UNLOCK(sc);
540 }
541
542 #define AUE_BITS        6
543 static void
544 aue_setmulti(struct usb_ether *ue)
545 {
546         struct aue_softc *sc = uether_getsc(ue);
547         struct ifnet *ifp = uether_getifp(ue);
548         struct ifmultiaddr *ifma;
549         uint32_t h = 0;
550         uint32_t i;
551         uint8_t hashtbl[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
552
553         AUE_LOCK_ASSERT(sc, MA_OWNED);
554
555         if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) {
556                 AUE_SETBIT(sc, AUE_CTL0, AUE_CTL0_ALLMULTI);
557                 return;
558         }
559
560         AUE_CLRBIT(sc, AUE_CTL0, AUE_CTL0_ALLMULTI);
561
562         /* now program new ones */
563         if_maddr_rlock(ifp);
564         CK_STAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
565                 if (ifma->ifma_addr->sa_family != AF_LINK)
566                         continue;
567                 h = ether_crc32_le(LLADDR((struct sockaddr_dl *)
568                     ifma->ifma_addr), ETHER_ADDR_LEN) & ((1 << AUE_BITS) - 1);
569                 hashtbl[(h >> 3)] |=  1 << (h & 0x7);
570         }
571         if_maddr_runlock(ifp);
572
573         /* write the hashtable */
574         for (i = 0; i != 8; i++)
575                 aue_csr_write_1(sc, AUE_MAR0 + i, hashtbl[i]);
576 }
577
578 static void
579 aue_reset_pegasus_II(struct aue_softc *sc)
580 {
581         /* Magic constants taken from Linux driver. */
582         aue_csr_write_1(sc, AUE_REG_1D, 0);
583         aue_csr_write_1(sc, AUE_REG_7B, 2);
584 #if 0
585         if ((sc->sc_flags & HAS_HOME_PNA) && mii_mode)
586                 aue_csr_write_1(sc, AUE_REG_81, 6);
587         else
588 #endif
589                 aue_csr_write_1(sc, AUE_REG_81, 2);
590 }
591
592 static void
593 aue_reset(struct aue_softc *sc)
594 {
595         int i;
596
597         AUE_SETBIT(sc, AUE_CTL1, AUE_CTL1_RESETMAC);
598
599         for (i = 0; i != AUE_TIMEOUT; i++) {
600                 if (!(aue_csr_read_1(sc, AUE_CTL1) & AUE_CTL1_RESETMAC))
601                         break;
602                 if (uether_pause(&sc->sc_ue, hz / 100))
603                         break;
604         }
605
606         if (i == AUE_TIMEOUT)
607                 device_printf(sc->sc_ue.ue_dev, "reset failed\n");
608
609         /*
610          * The PHY(s) attached to the Pegasus chip may be held
611          * in reset until we flip on the GPIO outputs. Make sure
612          * to set the GPIO pins high so that the PHY(s) will
613          * be enabled.
614          *
615          * NOTE: We used to force all of the GPIO pins low first and then
616          * enable the ones we want. This has been changed to better
617          * match the ADMtek's reference design to avoid setting the
618          * power-down configuration line of the PHY at the same time
619          * it is reset.
620          */
621         aue_csr_write_1(sc, AUE_GPIO0, AUE_GPIO_SEL0|AUE_GPIO_SEL1);
622         aue_csr_write_1(sc, AUE_GPIO0, AUE_GPIO_SEL0|AUE_GPIO_SEL1|AUE_GPIO_OUT0);
623
624         if (sc->sc_flags & AUE_FLAG_LSYS) {
625                 /* Grrr. LinkSys has to be different from everyone else. */
626                 aue_csr_write_1(sc, AUE_GPIO0, AUE_GPIO_SEL0|AUE_GPIO_SEL1);
627                 aue_csr_write_1(sc, AUE_GPIO0,
628                     AUE_GPIO_SEL0|AUE_GPIO_SEL1|AUE_GPIO_OUT0);
629         }
630         if (sc->sc_flags & AUE_FLAG_PII)
631                 aue_reset_pegasus_II(sc);
632
633         /* Wait a little while for the chip to get its brains in order: */
634         uether_pause(&sc->sc_ue, hz / 100);
635 }
636
637 static void
638 aue_attach_post(struct usb_ether *ue)
639 {
640         struct aue_softc *sc = uether_getsc(ue);
641
642         /* reset the adapter */
643         aue_reset(sc);
644
645         /* get station address from the EEPROM */
646         aue_read_mac(sc, ue->ue_eaddr);
647 }
648
649 /*
650  * Probe for a Pegasus chip.
651  */
652 static int
653 aue_probe(device_t dev)
654 {
655         struct usb_attach_arg *uaa = device_get_ivars(dev);
656
657         if (uaa->usb_mode != USB_MODE_HOST)
658                 return (ENXIO);
659         if (uaa->info.bConfigIndex != AUE_CONFIG_INDEX)
660                 return (ENXIO);
661         if (uaa->info.bIfaceIndex != AUE_IFACE_IDX)
662                 return (ENXIO);
663         /*
664          * Belkin USB Bluetooth dongles of the F8T012xx1 model series conflict
665          * with older Belkin USB2LAN adapters.  Skip if_aue if we detect one of
666          * the devices that look like Bluetooth adapters.
667          */
668         if (uaa->info.idVendor == USB_VENDOR_BELKIN &&
669             uaa->info.idProduct == USB_PRODUCT_BELKIN_F8T012 &&
670             uaa->info.bcdDevice == 0x0413)
671                 return (ENXIO);
672
673         return (usbd_lookup_id_by_uaa(aue_devs, sizeof(aue_devs), uaa));
674 }
675
676 /*
677  * Attach the interface. Allocate softc structures, do ifmedia
678  * setup and ethernet/BPF attach.
679  */
680 static int
681 aue_attach(device_t dev)
682 {
683         struct usb_attach_arg *uaa = device_get_ivars(dev);
684         struct aue_softc *sc = device_get_softc(dev);
685         struct usb_ether *ue = &sc->sc_ue;
686         uint8_t iface_index;
687         int error;
688
689         sc->sc_flags = USB_GET_DRIVER_INFO(uaa);
690
691         if (uaa->info.bcdDevice >= 0x0201) {
692                 /* XXX currently undocumented */
693                 sc->sc_flags |= AUE_FLAG_VER_2;
694         }
695
696         device_set_usb_desc(dev);
697         mtx_init(&sc->sc_mtx, device_get_nameunit(dev), NULL, MTX_DEF);
698
699         iface_index = AUE_IFACE_IDX;
700         error = usbd_transfer_setup(uaa->device, &iface_index,
701             sc->sc_xfer, aue_config, AUE_N_TRANSFER,
702             sc, &sc->sc_mtx);
703         if (error) {
704                 device_printf(dev, "allocating USB transfers failed\n");
705                 goto detach;
706         }
707
708         ue->ue_sc = sc;
709         ue->ue_dev = dev;
710         ue->ue_udev = uaa->device;
711         ue->ue_mtx = &sc->sc_mtx;
712         ue->ue_methods = &aue_ue_methods;
713
714         error = uether_ifattach(ue);
715         if (error) {
716                 device_printf(dev, "could not attach interface\n");
717                 goto detach;
718         }
719         return (0);                     /* success */
720
721 detach:
722         aue_detach(dev);
723         return (ENXIO);                 /* failure */
724 }
725
726 static int
727 aue_detach(device_t dev)
728 {
729         struct aue_softc *sc = device_get_softc(dev);
730         struct usb_ether *ue = &sc->sc_ue;
731
732         usbd_transfer_unsetup(sc->sc_xfer, AUE_N_TRANSFER);
733         uether_ifdetach(ue);
734         mtx_destroy(&sc->sc_mtx);
735
736         return (0);
737 }
738
739 static void
740 aue_intr_callback(struct usb_xfer *xfer, usb_error_t error)
741 {
742         struct aue_softc *sc = usbd_xfer_softc(xfer);
743         struct ifnet *ifp = uether_getifp(&sc->sc_ue);
744         struct aue_intrpkt pkt;
745         struct usb_page_cache *pc;
746         int actlen;
747
748         usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
749
750         switch (USB_GET_STATE(xfer)) {
751         case USB_ST_TRANSFERRED:
752
753                 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) &&
754                     actlen >= (int)sizeof(pkt)) {
755
756                         pc = usbd_xfer_get_frame(xfer, 0);
757                         usbd_copy_out(pc, 0, &pkt, sizeof(pkt));
758
759                         if (pkt.aue_txstat0)
760                                 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
761                         if (pkt.aue_txstat0 & (AUE_TXSTAT0_LATECOLL |
762                             AUE_TXSTAT0_EXCESSCOLL))
763                                 if_inc_counter(ifp, IFCOUNTER_COLLISIONS, 1);
764                 }
765                 /* FALLTHROUGH */
766         case USB_ST_SETUP:
767 tr_setup:
768                 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
769                 usbd_transfer_submit(xfer);
770                 return;
771
772         default:                        /* Error */
773                 if (error != USB_ERR_CANCELLED) {
774                         /* try to clear stall first */
775                         usbd_xfer_set_stall(xfer);
776                         goto tr_setup;
777                 }
778                 return;
779         }
780 }
781
782 static void
783 aue_bulk_read_callback(struct usb_xfer *xfer, usb_error_t error)
784 {
785         struct aue_softc *sc = usbd_xfer_softc(xfer);
786         struct usb_ether *ue = &sc->sc_ue;
787         struct ifnet *ifp = uether_getifp(ue);
788         struct aue_rxpkt stat;
789         struct usb_page_cache *pc;
790         int actlen;
791
792         usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
793         pc = usbd_xfer_get_frame(xfer, 0);
794
795         switch (USB_GET_STATE(xfer)) {
796         case USB_ST_TRANSFERRED:
797                 DPRINTFN(11, "received %d bytes\n", actlen);
798
799                 if (sc->sc_flags & AUE_FLAG_VER_2) {
800
801                         if (actlen == 0) {
802                                 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
803                                 goto tr_setup;
804                         }
805                 } else {
806
807                         if (actlen <= (int)(sizeof(stat) + ETHER_CRC_LEN)) {
808                                 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
809                                 goto tr_setup;
810                         }
811                         usbd_copy_out(pc, actlen - sizeof(stat), &stat,
812                             sizeof(stat));
813
814                         /*
815                          * turn off all the non-error bits in the rx status
816                          * word:
817                          */
818                         stat.aue_rxstat &= AUE_RXSTAT_MASK;
819                         if (stat.aue_rxstat) {
820                                 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
821                                 goto tr_setup;
822                         }
823                         /* No errors; receive the packet. */
824                         actlen -= (sizeof(stat) + ETHER_CRC_LEN);
825                 }
826                 uether_rxbuf(ue, pc, 0, actlen);
827
828                 /* FALLTHROUGH */
829         case USB_ST_SETUP:
830 tr_setup:
831                 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
832                 usbd_transfer_submit(xfer);
833                 uether_rxflush(ue);
834                 return;
835
836         default:                        /* Error */
837                 DPRINTF("bulk read error, %s\n",
838                     usbd_errstr(error));
839
840                 if (error != USB_ERR_CANCELLED) {
841                         /* try to clear stall first */
842                         usbd_xfer_set_stall(xfer);
843                         goto tr_setup;
844                 }
845                 return;
846         }
847 }
848
849 static void
850 aue_bulk_write_callback(struct usb_xfer *xfer, usb_error_t error)
851 {
852         struct aue_softc *sc = usbd_xfer_softc(xfer);
853         struct ifnet *ifp = uether_getifp(&sc->sc_ue);
854         struct usb_page_cache *pc;
855         struct mbuf *m;
856         uint8_t buf[2];
857         int actlen;
858
859         usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
860         pc = usbd_xfer_get_frame(xfer, 0);
861
862         switch (USB_GET_STATE(xfer)) {
863         case USB_ST_TRANSFERRED:
864                 DPRINTFN(11, "transfer of %d bytes complete\n", actlen);
865                 if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
866
867                 /* FALLTHROUGH */
868         case USB_ST_SETUP:
869 tr_setup:
870                 if ((sc->sc_flags & AUE_FLAG_LINK) == 0) {
871                         /*
872                          * don't send anything if there is no link !
873                          */
874                         return;
875                 }
876                 IFQ_DRV_DEQUEUE(&ifp->if_snd, m);
877
878                 if (m == NULL)
879                         return;
880                 if (m->m_pkthdr.len > MCLBYTES)
881                         m->m_pkthdr.len = MCLBYTES;
882                 if (sc->sc_flags & AUE_FLAG_VER_2) {
883
884                         usbd_xfer_set_frame_len(xfer, 0, m->m_pkthdr.len);
885
886                         usbd_m_copy_in(pc, 0, m, 0, m->m_pkthdr.len);
887
888                 } else {
889
890                         usbd_xfer_set_frame_len(xfer, 0, (m->m_pkthdr.len + 2));
891
892                         /*
893                          * The ADMtek documentation says that the
894                          * packet length is supposed to be specified
895                          * in the first two bytes of the transfer,
896                          * however it actually seems to ignore this
897                          * info and base the frame size on the bulk
898                          * transfer length.
899                          */
900                         buf[0] = (uint8_t)(m->m_pkthdr.len);
901                         buf[1] = (uint8_t)(m->m_pkthdr.len >> 8);
902
903                         usbd_copy_in(pc, 0, buf, 2);
904                         usbd_m_copy_in(pc, 2, m, 0, m->m_pkthdr.len);
905                 }
906
907                 /*
908                  * if there's a BPF listener, bounce a copy
909                  * of this frame to him:
910                  */
911                 BPF_MTAP(ifp, m);
912
913                 m_freem(m);
914
915                 usbd_transfer_submit(xfer);
916                 return;
917
918         default:                        /* Error */
919                 DPRINTFN(11, "transfer error, %s\n",
920                     usbd_errstr(error));
921
922                 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
923
924                 if (error != USB_ERR_CANCELLED) {
925                         /* try to clear stall first */
926                         usbd_xfer_set_stall(xfer);
927                         goto tr_setup;
928                 }
929                 return;
930         }
931 }
932
933 static void
934 aue_tick(struct usb_ether *ue)
935 {
936         struct aue_softc *sc = uether_getsc(ue);
937         struct mii_data *mii = GET_MII(sc);
938
939         AUE_LOCK_ASSERT(sc, MA_OWNED);
940
941         mii_tick(mii);
942         if ((sc->sc_flags & AUE_FLAG_LINK) == 0
943             && mii->mii_media_status & IFM_ACTIVE &&
944             IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
945                 sc->sc_flags |= AUE_FLAG_LINK;
946                 aue_start(ue);
947         }
948 }
949
950 static void
951 aue_start(struct usb_ether *ue)
952 {
953         struct aue_softc *sc = uether_getsc(ue);
954
955         /*
956          * start the USB transfers, if not already started:
957          */
958         usbd_transfer_start(sc->sc_xfer[AUE_INTR_DT_RD]);
959         usbd_transfer_start(sc->sc_xfer[AUE_BULK_DT_RD]);
960         usbd_transfer_start(sc->sc_xfer[AUE_BULK_DT_WR]);
961 }
962
963 static void
964 aue_init(struct usb_ether *ue)
965 {
966         struct aue_softc *sc = uether_getsc(ue);
967         struct ifnet *ifp = uether_getifp(ue);
968         int i;
969
970         AUE_LOCK_ASSERT(sc, MA_OWNED);
971
972         /*
973          * Cancel pending I/O
974          */
975         aue_reset(sc);
976
977         /* Set MAC address */
978         for (i = 0; i != ETHER_ADDR_LEN; i++)
979                 aue_csr_write_1(sc, AUE_PAR0 + i, IF_LLADDR(ifp)[i]);
980
981         /* update promiscuous setting */
982         aue_setpromisc(ue);
983
984         /* Load the multicast filter. */
985         aue_setmulti(ue);
986
987         /* Enable RX and TX */
988         aue_csr_write_1(sc, AUE_CTL0, AUE_CTL0_RXSTAT_APPEND | AUE_CTL0_RX_ENB);
989         AUE_SETBIT(sc, AUE_CTL0, AUE_CTL0_TX_ENB);
990         AUE_SETBIT(sc, AUE_CTL2, AUE_CTL2_EP3_CLR);
991
992         usbd_xfer_set_stall(sc->sc_xfer[AUE_BULK_DT_WR]);
993
994         ifp->if_drv_flags |= IFF_DRV_RUNNING;
995         aue_start(ue);
996 }
997
998 static void
999 aue_setpromisc(struct usb_ether *ue)
1000 {
1001         struct aue_softc *sc = uether_getsc(ue);
1002         struct ifnet *ifp = uether_getifp(ue);
1003
1004         AUE_LOCK_ASSERT(sc, MA_OWNED);
1005
1006         /* if we want promiscuous mode, set the allframes bit: */
1007         if (ifp->if_flags & IFF_PROMISC)
1008                 AUE_SETBIT(sc, AUE_CTL2, AUE_CTL2_RX_PROMISC);
1009         else
1010                 AUE_CLRBIT(sc, AUE_CTL2, AUE_CTL2_RX_PROMISC);
1011 }
1012
1013 /*
1014  * Set media options.
1015  */
1016 static int
1017 aue_ifmedia_upd(struct ifnet *ifp)
1018 {
1019         struct aue_softc *sc = ifp->if_softc;
1020         struct mii_data *mii = GET_MII(sc);
1021         struct mii_softc *miisc;
1022         int error;
1023
1024         AUE_LOCK_ASSERT(sc, MA_OWNED);
1025
1026         sc->sc_flags &= ~AUE_FLAG_LINK;
1027         LIST_FOREACH(miisc, &mii->mii_phys, mii_list)
1028                 PHY_RESET(miisc);
1029         error = mii_mediachg(mii);
1030         return (error);
1031 }
1032
1033 /*
1034  * Report current media status.
1035  */
1036 static void
1037 aue_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
1038 {
1039         struct aue_softc *sc = ifp->if_softc;
1040         struct mii_data *mii = GET_MII(sc);
1041
1042         AUE_LOCK(sc);
1043         mii_pollstat(mii);
1044         ifmr->ifm_active = mii->mii_media_active;
1045         ifmr->ifm_status = mii->mii_media_status;
1046         AUE_UNLOCK(sc);
1047 }
1048
1049 /*
1050  * Stop the adapter and free any mbufs allocated to the
1051  * RX and TX lists.
1052  */
1053 static void
1054 aue_stop(struct usb_ether *ue)
1055 {
1056         struct aue_softc *sc = uether_getsc(ue);
1057         struct ifnet *ifp = uether_getifp(ue);
1058
1059         AUE_LOCK_ASSERT(sc, MA_OWNED);
1060
1061         ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1062         sc->sc_flags &= ~AUE_FLAG_LINK;
1063
1064         /*
1065          * stop all the transfers, if not already stopped:
1066          */
1067         usbd_transfer_stop(sc->sc_xfer[AUE_BULK_DT_WR]);
1068         usbd_transfer_stop(sc->sc_xfer[AUE_BULK_DT_RD]);
1069         usbd_transfer_stop(sc->sc_xfer[AUE_INTR_DT_RD]);
1070
1071         aue_csr_write_1(sc, AUE_CTL0, 0);
1072         aue_csr_write_1(sc, AUE_CTL1, 0);
1073         aue_reset(sc);
1074 }