if_aue.c revision 194228
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: head/sys/dev/usb/net/if_aue.c 194228 2009-06-15 01:02:43Z thompsa $");
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 uether_do_request(). Packet
68 * transfers are done using usbd_transfer() and friends.
69 */
70
71#include "usbdevs.h"
72#include <dev/usb/usb.h>
73#include <dev/usb/usb_mfunc.h>
74#include <dev/usb/usb_error.h>
75
76#define	USB_DEBUG_VAR aue_debug
77
78#include <dev/usb/usb_core.h>
79#include <dev/usb/usb_lookup.h>
80#include <dev/usb/usb_process.h>
81#include <dev/usb/usb_debug.h>
82#include <dev/usb/usb_request.h>
83#include <dev/usb/usb_busdma.h>
84#include <dev/usb/usb_util.h>
85
86#include <dev/usb/net/usb_ethernet.h>
87#include <dev/usb/net/if_auereg.h>
88
89#if USB_DEBUG
90static int aue_debug = 0;
91
92SYSCTL_NODE(_hw_usb, OID_AUTO, aue, CTLFLAG_RW, 0, "USB aue");
93SYSCTL_INT(_hw_usb_aue, OID_AUTO, debug, CTLFLAG_RW, &aue_debug, 0,
94    "Debug level");
95#endif
96
97/*
98 * Various supported device vendors/products.
99 */
100static const struct usb_device_id aue_devs[] = {
101    {USB_VPI(USB_VENDOR_3COM, USB_PRODUCT_3COM_3C460B, AUE_FLAG_PII)},
102    {USB_VPI(USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_DSB650TX_PNA, 0)},
103    {USB_VPI(USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_UFE1000, AUE_FLAG_LSYS)},
104    {USB_VPI(USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_XX10, 0)},
105    {USB_VPI(USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_XX1, AUE_FLAG_PNA | AUE_FLAG_PII)},
106    {USB_VPI(USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_XX2, AUE_FLAG_PII)},
107    {USB_VPI(USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_XX4, AUE_FLAG_PNA)},
108    {USB_VPI(USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_XX5, AUE_FLAG_PNA)},
109    {USB_VPI(USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_XX6, AUE_FLAG_PII)},
110    {USB_VPI(USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_XX7, AUE_FLAG_PII)},
111    {USB_VPI(USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_XX8, AUE_FLAG_PII)},
112    {USB_VPI(USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_XX9, AUE_FLAG_PNA)},
113    {USB_VPI(USB_VENDOR_ACCTON, USB_PRODUCT_ACCTON_SS1001, AUE_FLAG_PII)},
114    {USB_VPI(USB_VENDOR_ACCTON, USB_PRODUCT_ACCTON_USB320_EC, 0)},
115    {USB_VPI(USB_VENDOR_ADMTEK, USB_PRODUCT_ADMTEK_PEGASUSII_2, AUE_FLAG_PII)},
116    {USB_VPI(USB_VENDOR_ADMTEK, USB_PRODUCT_ADMTEK_PEGASUSII_3, AUE_FLAG_PII)},
117    {USB_VPI(USB_VENDOR_ADMTEK, USB_PRODUCT_ADMTEK_PEGASUSII_4, AUE_FLAG_PII)},
118    {USB_VPI(USB_VENDOR_ADMTEK, USB_PRODUCT_ADMTEK_PEGASUSII, AUE_FLAG_PII)},
119    {USB_VPI(USB_VENDOR_ADMTEK, USB_PRODUCT_ADMTEK_PEGASUS, AUE_FLAG_PNA | AUE_FLAG_DUAL_PHY)},
120    {USB_VPI(USB_VENDOR_AEI, USB_PRODUCT_AEI_FASTETHERNET, AUE_FLAG_PII)},
121    {USB_VPI(USB_VENDOR_ALLIEDTELESYN, USB_PRODUCT_ALLIEDTELESYN_ATUSB100, AUE_FLAG_PII)},
122    {USB_VPI(USB_VENDOR_ATEN, USB_PRODUCT_ATEN_UC110T, AUE_FLAG_PII)},
123    {USB_VPI(USB_VENDOR_BELKIN, USB_PRODUCT_BELKIN_USB2LAN, AUE_FLAG_PII)},
124    {USB_VPI(USB_VENDOR_BILLIONTON, USB_PRODUCT_BILLIONTON_USB100, 0)},
125    {USB_VPI(USB_VENDOR_BILLIONTON, USB_PRODUCT_BILLIONTON_USBE100, AUE_FLAG_PII)},
126    {USB_VPI(USB_VENDOR_BILLIONTON, USB_PRODUCT_BILLIONTON_USBEL100, 0)},
127    {USB_VPI(USB_VENDOR_BILLIONTON, USB_PRODUCT_BILLIONTON_USBLP100, AUE_FLAG_PNA)},
128    {USB_VPI(USB_VENDOR_COREGA, USB_PRODUCT_COREGA_FETHER_USB_TXS, AUE_FLAG_PII)},
129    {USB_VPI(USB_VENDOR_COREGA, USB_PRODUCT_COREGA_FETHER_USB_TX, 0)},
130    {USB_VPI(USB_VENDOR_DLINK, USB_PRODUCT_DLINK_DSB650TX1, AUE_FLAG_LSYS)},
131    {USB_VPI(USB_VENDOR_DLINK, USB_PRODUCT_DLINK_DSB650TX2, AUE_FLAG_LSYS | AUE_FLAG_PII)},
132    {USB_VPI(USB_VENDOR_DLINK, USB_PRODUCT_DLINK_DSB650TX3, AUE_FLAG_LSYS | AUE_FLAG_PII)},
133    {USB_VPI(USB_VENDOR_DLINK, USB_PRODUCT_DLINK_DSB650TX4, AUE_FLAG_LSYS | AUE_FLAG_PII)},
134    {USB_VPI(USB_VENDOR_DLINK, USB_PRODUCT_DLINK_DSB650TX_PNA, AUE_FLAG_PNA)},
135    {USB_VPI(USB_VENDOR_DLINK, USB_PRODUCT_DLINK_DSB650TX, AUE_FLAG_LSYS)},
136    {USB_VPI(USB_VENDOR_DLINK, USB_PRODUCT_DLINK_DSB650, AUE_FLAG_LSYS)},
137    {USB_VPI(USB_VENDOR_ELCON, USB_PRODUCT_ELCON_PLAN, AUE_FLAG_PNA | AUE_FLAG_PII)},
138    {USB_VPI(USB_VENDOR_ELECOM, USB_PRODUCT_ELECOM_LDUSB20, AUE_FLAG_PII)},
139    {USB_VPI(USB_VENDOR_ELECOM, USB_PRODUCT_ELECOM_LDUSBLTX, AUE_FLAG_PII)},
140    {USB_VPI(USB_VENDOR_ELECOM, USB_PRODUCT_ELECOM_LDUSBTX0, 0)},
141    {USB_VPI(USB_VENDOR_ELECOM, USB_PRODUCT_ELECOM_LDUSBTX1, AUE_FLAG_LSYS)},
142    {USB_VPI(USB_VENDOR_ELECOM, USB_PRODUCT_ELECOM_LDUSBTX2, 0)},
143    {USB_VPI(USB_VENDOR_ELECOM, USB_PRODUCT_ELECOM_LDUSBTX3, AUE_FLAG_LSYS)},
144    {USB_VPI(USB_VENDOR_ELSA, USB_PRODUCT_ELSA_USB2ETHERNET, 0)},
145    {USB_VPI(USB_VENDOR_GIGABYTE, USB_PRODUCT_GIGABYTE_GNBR402W, 0)},
146    {USB_VPI(USB_VENDOR_HAWKING, USB_PRODUCT_HAWKING_UF100, AUE_FLAG_PII)},
147    {USB_VPI(USB_VENDOR_HP, USB_PRODUCT_HP_HN210E, AUE_FLAG_PII)},
148    {USB_VPI(USB_VENDOR_IODATA, USB_PRODUCT_IODATA_USBETTXS, AUE_FLAG_PII)},
149    {USB_VPI(USB_VENDOR_IODATA, USB_PRODUCT_IODATA_USBETTX, 0)},
150    {USB_VPI(USB_VENDOR_KINGSTON, USB_PRODUCT_KINGSTON_KNU101TX, 0)},
151    {USB_VPI(USB_VENDOR_LINKSYS, USB_PRODUCT_LINKSYS_USB100H1, AUE_FLAG_LSYS | AUE_FLAG_PNA)},
152    {USB_VPI(USB_VENDOR_LINKSYS, USB_PRODUCT_LINKSYS_USB100TX, AUE_FLAG_LSYS)},
153    {USB_VPI(USB_VENDOR_LINKSYS, USB_PRODUCT_LINKSYS_USB10TA, AUE_FLAG_LSYS)},
154    {USB_VPI(USB_VENDOR_LINKSYS, USB_PRODUCT_LINKSYS_USB10TX1, AUE_FLAG_LSYS | AUE_FLAG_PII)},
155    {USB_VPI(USB_VENDOR_LINKSYS, USB_PRODUCT_LINKSYS_USB10TX2, AUE_FLAG_LSYS | AUE_FLAG_PII)},
156    {USB_VPI(USB_VENDOR_LINKSYS, USB_PRODUCT_LINKSYS_USB10T, AUE_FLAG_LSYS)},
157    {USB_VPI(USB_VENDOR_MELCO, USB_PRODUCT_MELCO_LUA2TX5, AUE_FLAG_PII)},
158    {USB_VPI(USB_VENDOR_MELCO, USB_PRODUCT_MELCO_LUATX1, 0)},
159    {USB_VPI(USB_VENDOR_MELCO, USB_PRODUCT_MELCO_LUATX5, 0)},
160    {USB_VPI(USB_VENDOR_MICROSOFT, USB_PRODUCT_MICROSOFT_MN110, AUE_FLAG_PII)},
161    {USB_VPI(USB_VENDOR_NETGEAR, USB_PRODUCT_NETGEAR_FA101, AUE_FLAG_PII)},
162    {USB_VPI(USB_VENDOR_SIEMENS, USB_PRODUCT_SIEMENS_SPEEDSTREAM, AUE_FLAG_PII)},
163    {USB_VPI(USB_VENDOR_SIIG2, USB_PRODUCT_SIIG2_USBTOETHER, AUE_FLAG_PII)},
164    {USB_VPI(USB_VENDOR_SMARTBRIDGES, USB_PRODUCT_SMARTBRIDGES_SMARTNIC, AUE_FLAG_PII)},
165    {USB_VPI(USB_VENDOR_SMC, USB_PRODUCT_SMC_2202USB, 0)},
166    {USB_VPI(USB_VENDOR_SMC, USB_PRODUCT_SMC_2206USB, AUE_FLAG_PII)},
167    {USB_VPI(USB_VENDOR_SOHOWARE, USB_PRODUCT_SOHOWARE_NUB100, 0)},
168    {USB_VPI(USB_VENDOR_SOHOWARE, USB_PRODUCT_SOHOWARE_NUB110, AUE_FLAG_PII)},
169};
170
171/* prototypes */
172
173static device_probe_t aue_probe;
174static device_attach_t aue_attach;
175static device_detach_t aue_detach;
176static miibus_readreg_t aue_miibus_readreg;
177static miibus_writereg_t aue_miibus_writereg;
178static miibus_statchg_t aue_miibus_statchg;
179
180static usb_callback_t aue_intr_callback;
181static usb_callback_t aue_bulk_read_callback;
182static usb_callback_t aue_bulk_write_callback;
183
184static uether_fn_t aue_attach_post;
185static uether_fn_t aue_init;
186static uether_fn_t aue_stop;
187static uether_fn_t aue_start;
188static uether_fn_t aue_tick;
189static uether_fn_t aue_setmulti;
190static uether_fn_t aue_setpromisc;
191
192static uint8_t	aue_csr_read_1(struct aue_softc *, uint16_t);
193static uint16_t	aue_csr_read_2(struct aue_softc *, uint16_t);
194static void	aue_csr_write_1(struct aue_softc *, uint16_t, uint8_t);
195static void	aue_csr_write_2(struct aue_softc *, uint16_t, uint16_t);
196static void	aue_eeprom_getword(struct aue_softc *, int, uint16_t *);
197static void	aue_read_eeprom(struct aue_softc *, uint8_t *, uint16_t,
198		    uint16_t);
199static void	aue_reset(struct aue_softc *);
200static void	aue_reset_pegasus_II(struct aue_softc *);
201
202static int	aue_ifmedia_upd(struct ifnet *);
203static void	aue_ifmedia_sts(struct ifnet *, struct ifmediareq *);
204
205static const struct usb_config aue_config[AUE_N_TRANSFER] = {
206
207	[AUE_BULK_DT_WR] = {
208		.type = UE_BULK,
209		.endpoint = UE_ADDR_ANY,
210		.direction = UE_DIR_OUT,
211		.bufsize = (MCLBYTES + 2),
212		.flags = {.pipe_bof = 1,.force_short_xfer = 1,},
213		.callback = aue_bulk_write_callback,
214		.timeout = 10000,	/* 10 seconds */
215	},
216
217	[AUE_BULK_DT_RD] = {
218		.type = UE_BULK,
219		.endpoint = UE_ADDR_ANY,
220		.direction = UE_DIR_IN,
221		.bufsize = (MCLBYTES + 4 + ETHER_CRC_LEN),
222		.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
223		.callback = aue_bulk_read_callback,
224	},
225
226	[AUE_INTR_DT_RD] = {
227		.type = UE_INTERRUPT,
228		.endpoint = UE_ADDR_ANY,
229		.direction = UE_DIR_IN,
230		.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
231		.bufsize = 0,	/* use wMaxPacketSize */
232		.callback = aue_intr_callback,
233	},
234};
235
236static device_method_t aue_methods[] = {
237	/* Device interface */
238	DEVMETHOD(device_probe, aue_probe),
239	DEVMETHOD(device_attach, aue_attach),
240	DEVMETHOD(device_detach, aue_detach),
241
242	/* bus interface */
243	DEVMETHOD(bus_print_child, bus_generic_print_child),
244	DEVMETHOD(bus_driver_added, bus_generic_driver_added),
245
246	/* MII interface */
247	DEVMETHOD(miibus_readreg, aue_miibus_readreg),
248	DEVMETHOD(miibus_writereg, aue_miibus_writereg),
249	DEVMETHOD(miibus_statchg, aue_miibus_statchg),
250
251	{0, 0}
252};
253
254static driver_t aue_driver = {
255	.name = "aue",
256	.methods = aue_methods,
257	.size = sizeof(struct aue_softc)
258};
259
260static devclass_t aue_devclass;
261
262DRIVER_MODULE(aue, uhub, aue_driver, aue_devclass, NULL, 0);
263DRIVER_MODULE(miibus, aue, miibus_driver, miibus_devclass, 0, 0);
264MODULE_DEPEND(aue, uether, 1, 1, 1);
265MODULE_DEPEND(aue, usb, 1, 1, 1);
266MODULE_DEPEND(aue, ether, 1, 1, 1);
267MODULE_DEPEND(aue, miibus, 1, 1, 1);
268
269static const struct usb_ether_methods aue_ue_methods = {
270	.ue_attach_post = aue_attach_post,
271	.ue_start = aue_start,
272	.ue_init = aue_init,
273	.ue_stop = aue_stop,
274	.ue_tick = aue_tick,
275	.ue_setmulti = aue_setmulti,
276	.ue_setpromisc = aue_setpromisc,
277	.ue_mii_upd = aue_ifmedia_upd,
278	.ue_mii_sts = aue_ifmedia_sts,
279};
280
281#define	AUE_SETBIT(sc, reg, x) \
282	aue_csr_write_1(sc, reg, aue_csr_read_1(sc, reg) | (x))
283
284#define	AUE_CLRBIT(sc, reg, x) \
285	aue_csr_write_1(sc, reg, aue_csr_read_1(sc, reg) & ~(x))
286
287static uint8_t
288aue_csr_read_1(struct aue_softc *sc, uint16_t reg)
289{
290	struct usb_device_request req;
291	usb_error_t err;
292	uint8_t val;
293
294	req.bmRequestType = UT_READ_VENDOR_DEVICE;
295	req.bRequest = AUE_UR_READREG;
296	USETW(req.wValue, 0);
297	USETW(req.wIndex, reg);
298	USETW(req.wLength, 1);
299
300	err = uether_do_request(&sc->sc_ue, &req, &val, 1000);
301	if (err)
302		return (0);
303	return (val);
304}
305
306static uint16_t
307aue_csr_read_2(struct aue_softc *sc, uint16_t reg)
308{
309	struct usb_device_request req;
310	usb_error_t err;
311	uint16_t val;
312
313	req.bmRequestType = UT_READ_VENDOR_DEVICE;
314	req.bRequest = AUE_UR_READREG;
315	USETW(req.wValue, 0);
316	USETW(req.wIndex, reg);
317	USETW(req.wLength, 2);
318
319	err = uether_do_request(&sc->sc_ue, &req, &val, 1000);
320	if (err)
321		return (0);
322	return (le16toh(val));
323}
324
325static void
326aue_csr_write_1(struct aue_softc *sc, uint16_t reg, uint8_t val)
327{
328	struct usb_device_request req;
329
330	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
331	req.bRequest = AUE_UR_WRITEREG;
332	req.wValue[0] = val;
333	req.wValue[1] = 0;
334	USETW(req.wIndex, reg);
335	USETW(req.wLength, 1);
336
337	if (uether_do_request(&sc->sc_ue, &req, &val, 1000)) {
338		/* error ignored */
339	}
340}
341
342static void
343aue_csr_write_2(struct aue_softc *sc, uint16_t reg, uint16_t val)
344{
345	struct usb_device_request req;
346
347	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
348	req.bRequest = AUE_UR_WRITEREG;
349	USETW(req.wValue, val);
350	USETW(req.wIndex, reg);
351	USETW(req.wLength, 2);
352
353	val = htole16(val);
354
355	if (uether_do_request(&sc->sc_ue, &req, &val, 1000)) {
356		/* error ignored */
357	}
358}
359
360/*
361 * Read a word of data stored in the EEPROM at address 'addr.'
362 */
363static void
364aue_eeprom_getword(struct aue_softc *sc, int addr, uint16_t *dest)
365{
366	int i;
367	uint16_t word = 0;
368
369	aue_csr_write_1(sc, AUE_EE_REG, addr);
370	aue_csr_write_1(sc, AUE_EE_CTL, AUE_EECTL_READ);
371
372	for (i = 0; i != AUE_TIMEOUT; i++) {
373		if (aue_csr_read_1(sc, AUE_EE_CTL) & AUE_EECTL_DONE)
374			break;
375		if (uether_pause(&sc->sc_ue, hz / 100))
376			break;
377	}
378
379	if (i == AUE_TIMEOUT)
380		device_printf(sc->sc_ue.ue_dev, "EEPROM read timed out\n");
381
382	word = aue_csr_read_2(sc, AUE_EE_DATA);
383	*dest = word;
384}
385
386/*
387 * Read a sequence of words from the EEPROM.
388 */
389static void
390aue_read_eeprom(struct aue_softc *sc, uint8_t *dest,
391    uint16_t off, uint16_t len)
392{
393	uint16_t *ptr = (uint16_t *)dest;
394	int i;
395
396	for (i = 0; i != len; i++, ptr++)
397		aue_eeprom_getword(sc, off + i, ptr);
398}
399
400static int
401aue_miibus_readreg(device_t dev, int phy, int reg)
402{
403	struct aue_softc *sc = device_get_softc(dev);
404	int i, locked;
405	uint16_t val = 0;
406
407	locked = mtx_owned(&sc->sc_mtx);
408	if (!locked)
409		AUE_LOCK(sc);
410
411	/*
412	 * The Am79C901 HomePNA PHY actually contains two transceivers: a 1Mbps
413	 * HomePNA PHY and a 10Mbps full/half duplex ethernet PHY with NWAY
414	 * autoneg. However in the ADMtek adapter, only the 1Mbps PHY is
415	 * actually connected to anything, so we ignore the 10Mbps one. It
416	 * happens to be configured for MII address 3, so we filter that out.
417	 */
418	if (sc->sc_flags & AUE_FLAG_DUAL_PHY) {
419		if (phy == 3)
420			goto done;
421#if 0
422		if (phy != 1)
423			goto done;
424#endif
425	}
426	aue_csr_write_1(sc, AUE_PHY_ADDR, phy);
427	aue_csr_write_1(sc, AUE_PHY_CTL, reg | AUE_PHYCTL_READ);
428
429	for (i = 0; i != AUE_TIMEOUT; i++) {
430		if (aue_csr_read_1(sc, AUE_PHY_CTL) & AUE_PHYCTL_DONE)
431			break;
432		if (uether_pause(&sc->sc_ue, hz / 100))
433			break;
434	}
435
436	if (i == AUE_TIMEOUT)
437		device_printf(sc->sc_ue.ue_dev, "MII read timed out\n");
438
439	val = aue_csr_read_2(sc, AUE_PHY_DATA);
440
441done:
442	if (!locked)
443		AUE_UNLOCK(sc);
444	return (val);
445}
446
447static int
448aue_miibus_writereg(device_t dev, int phy, int reg, int data)
449{
450	struct aue_softc *sc = device_get_softc(dev);
451	int i;
452	int locked;
453
454	if (phy == 3)
455		return (0);
456
457	locked = mtx_owned(&sc->sc_mtx);
458	if (!locked)
459		AUE_LOCK(sc);
460
461	aue_csr_write_2(sc, AUE_PHY_DATA, data);
462	aue_csr_write_1(sc, AUE_PHY_ADDR, phy);
463	aue_csr_write_1(sc, AUE_PHY_CTL, reg | AUE_PHYCTL_WRITE);
464
465	for (i = 0; i != AUE_TIMEOUT; i++) {
466		if (aue_csr_read_1(sc, AUE_PHY_CTL) & AUE_PHYCTL_DONE)
467			break;
468		if (uether_pause(&sc->sc_ue, hz / 100))
469			break;
470	}
471
472	if (i == AUE_TIMEOUT)
473		device_printf(sc->sc_ue.ue_dev, "MII read timed out\n");
474
475	if (!locked)
476		AUE_UNLOCK(sc);
477	return (0);
478}
479
480static void
481aue_miibus_statchg(device_t dev)
482{
483	struct aue_softc *sc = device_get_softc(dev);
484	struct mii_data *mii = GET_MII(sc);
485	int locked;
486
487	locked = mtx_owned(&sc->sc_mtx);
488	if (!locked)
489		AUE_LOCK(sc);
490
491	AUE_CLRBIT(sc, AUE_CTL0, AUE_CTL0_RX_ENB | AUE_CTL0_TX_ENB);
492	if (IFM_SUBTYPE(mii->mii_media_active) == IFM_100_TX)
493		AUE_SETBIT(sc, AUE_CTL1, AUE_CTL1_SPEEDSEL);
494	else
495		AUE_CLRBIT(sc, AUE_CTL1, AUE_CTL1_SPEEDSEL);
496
497	if ((mii->mii_media_active & IFM_GMASK) == IFM_FDX)
498		AUE_SETBIT(sc, AUE_CTL1, AUE_CTL1_DUPLEX);
499	else
500		AUE_CLRBIT(sc, AUE_CTL1, AUE_CTL1_DUPLEX);
501
502	AUE_SETBIT(sc, AUE_CTL0, AUE_CTL0_RX_ENB | AUE_CTL0_TX_ENB);
503
504	/*
505	 * Set the LED modes on the LinkSys adapter.
506	 * This turns on the 'dual link LED' bin in the auxmode
507	 * register of the Broadcom PHY.
508	 */
509	if (sc->sc_flags & AUE_FLAG_LSYS) {
510		uint16_t auxmode;
511
512		auxmode = aue_miibus_readreg(dev, 0, 0x1b);
513		aue_miibus_writereg(dev, 0, 0x1b, auxmode | 0x04);
514	}
515	if (!locked)
516		AUE_UNLOCK(sc);
517}
518
519#define	AUE_BITS	6
520static void
521aue_setmulti(struct usb_ether *ue)
522{
523	struct aue_softc *sc = uether_getsc(ue);
524	struct ifnet *ifp = uether_getifp(ue);
525	struct ifmultiaddr *ifma;
526	uint32_t h = 0;
527	uint32_t i;
528	uint8_t hashtbl[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
529
530	AUE_LOCK_ASSERT(sc, MA_OWNED);
531
532	if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) {
533		AUE_SETBIT(sc, AUE_CTL0, AUE_CTL0_ALLMULTI);
534		return;
535	}
536
537	AUE_CLRBIT(sc, AUE_CTL0, AUE_CTL0_ALLMULTI);
538
539	/* now program new ones */
540	IF_ADDR_LOCK(ifp);
541	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
542		if (ifma->ifma_addr->sa_family != AF_LINK)
543			continue;
544		h = ether_crc32_le(LLADDR((struct sockaddr_dl *)
545		    ifma->ifma_addr), ETHER_ADDR_LEN) & ((1 << AUE_BITS) - 1);
546		hashtbl[(h >> 3)] |=  1 << (h & 0x7);
547	}
548	IF_ADDR_UNLOCK(ifp);
549
550	/* write the hashtable */
551	for (i = 0; i != 8; i++)
552		aue_csr_write_1(sc, AUE_MAR0 + i, hashtbl[i]);
553}
554
555static void
556aue_reset_pegasus_II(struct aue_softc *sc)
557{
558	/* Magic constants taken from Linux driver. */
559	aue_csr_write_1(sc, AUE_REG_1D, 0);
560	aue_csr_write_1(sc, AUE_REG_7B, 2);
561#if 0
562	if ((sc->sc_flags & HAS_HOME_PNA) && mii_mode)
563		aue_csr_write_1(sc, AUE_REG_81, 6);
564	else
565#endif
566		aue_csr_write_1(sc, AUE_REG_81, 2);
567}
568
569static void
570aue_reset(struct aue_softc *sc)
571{
572	int i;
573
574	AUE_SETBIT(sc, AUE_CTL1, AUE_CTL1_RESETMAC);
575
576	for (i = 0; i != AUE_TIMEOUT; i++) {
577		if (!(aue_csr_read_1(sc, AUE_CTL1) & AUE_CTL1_RESETMAC))
578			break;
579		if (uether_pause(&sc->sc_ue, hz / 100))
580			break;
581	}
582
583	if (i == AUE_TIMEOUT)
584		device_printf(sc->sc_ue.ue_dev, "reset failed\n");
585
586	/*
587	 * The PHY(s) attached to the Pegasus chip may be held
588	 * in reset until we flip on the GPIO outputs. Make sure
589	 * to set the GPIO pins high so that the PHY(s) will
590	 * be enabled.
591	 *
592	 * Note: We force all of the GPIO pins low first, *then*
593	 * enable the ones we want.
594	 */
595	aue_csr_write_1(sc, AUE_GPIO0, AUE_GPIO_OUT0|AUE_GPIO_SEL0);
596	aue_csr_write_1(sc, AUE_GPIO0, AUE_GPIO_OUT0|AUE_GPIO_SEL0|AUE_GPIO_SEL1);
597
598	if (sc->sc_flags & AUE_FLAG_LSYS) {
599		/* Grrr. LinkSys has to be different from everyone else. */
600		aue_csr_write_1(sc, AUE_GPIO0, AUE_GPIO_SEL0|AUE_GPIO_SEL1);
601		aue_csr_write_1(sc, AUE_GPIO0,
602		    AUE_GPIO_SEL0|AUE_GPIO_SEL1|AUE_GPIO_OUT0);
603	}
604	if (sc->sc_flags & AUE_FLAG_PII)
605		aue_reset_pegasus_II(sc);
606
607	/* Wait a little while for the chip to get its brains in order: */
608	uether_pause(&sc->sc_ue, hz / 100);
609}
610
611static void
612aue_attach_post(struct usb_ether *ue)
613{
614	struct aue_softc *sc = uether_getsc(ue);
615
616	/* reset the adapter */
617	aue_reset(sc);
618
619	/* get station address from the EEPROM */
620	aue_read_eeprom(sc, ue->ue_eaddr, 0, 3);
621}
622
623/*
624 * Probe for a Pegasus chip.
625 */
626static int
627aue_probe(device_t dev)
628{
629	struct usb_attach_arg *uaa = device_get_ivars(dev);
630
631	if (uaa->usb_mode != USB_MODE_HOST)
632		return (ENXIO);
633	if (uaa->info.bConfigIndex != AUE_CONFIG_INDEX)
634		return (ENXIO);
635	if (uaa->info.bIfaceIndex != AUE_IFACE_IDX)
636		return (ENXIO);
637	/*
638	 * Belkin USB Bluetooth dongles of the F8T012xx1 model series conflict
639	 * with older Belkin USB2LAN adapters.  Skip if_aue if we detect one of
640	 * the devices that look like Bluetooth adapters.
641	 */
642	if (uaa->info.idVendor == USB_VENDOR_BELKIN &&
643	    uaa->info.idProduct == USB_PRODUCT_BELKIN_F8T012 &&
644	    uaa->info.bcdDevice == 0x0413)
645		return (ENXIO);
646
647	return (usbd_lookup_id_by_uaa(aue_devs, sizeof(aue_devs), uaa));
648}
649
650/*
651 * Attach the interface. Allocate softc structures, do ifmedia
652 * setup and ethernet/BPF attach.
653 */
654static int
655aue_attach(device_t dev)
656{
657	struct usb_attach_arg *uaa = device_get_ivars(dev);
658	struct aue_softc *sc = device_get_softc(dev);
659	struct usb_ether *ue = &sc->sc_ue;
660	uint8_t iface_index;
661	int error;
662
663	sc->sc_flags = USB_GET_DRIVER_INFO(uaa);
664
665	if (uaa->info.bcdDevice >= 0x0201) {
666		/* XXX currently undocumented */
667		sc->sc_flags |= AUE_FLAG_VER_2;
668	}
669
670	device_set_usb_desc(dev);
671	mtx_init(&sc->sc_mtx, device_get_nameunit(dev), NULL, MTX_DEF);
672
673	iface_index = AUE_IFACE_IDX;
674	error = usbd_transfer_setup(uaa->device, &iface_index,
675	    sc->sc_xfer, aue_config, AUE_N_TRANSFER,
676	    sc, &sc->sc_mtx);
677	if (error) {
678		device_printf(dev, "allocating USB transfers failed!\n");
679		goto detach;
680	}
681
682	ue->ue_sc = sc;
683	ue->ue_dev = dev;
684	ue->ue_udev = uaa->device;
685	ue->ue_mtx = &sc->sc_mtx;
686	ue->ue_methods = &aue_ue_methods;
687
688	error = uether_ifattach(ue);
689	if (error) {
690		device_printf(dev, "could not attach interface\n");
691		goto detach;
692	}
693	return (0);			/* success */
694
695detach:
696	aue_detach(dev);
697	return (ENXIO);			/* failure */
698}
699
700static int
701aue_detach(device_t dev)
702{
703	struct aue_softc *sc = device_get_softc(dev);
704	struct usb_ether *ue = &sc->sc_ue;
705
706	usbd_transfer_unsetup(sc->sc_xfer, AUE_N_TRANSFER);
707	uether_ifdetach(ue);
708	mtx_destroy(&sc->sc_mtx);
709
710	return (0);
711}
712
713static void
714aue_intr_callback(struct usb_xfer *xfer)
715{
716	struct aue_softc *sc = xfer->priv_sc;
717	struct ifnet *ifp = uether_getifp(&sc->sc_ue);
718	struct aue_intrpkt pkt;
719
720	switch (USB_GET_STATE(xfer)) {
721	case USB_ST_TRANSFERRED:
722
723		if ((ifp->if_drv_flags & IFF_DRV_RUNNING) &&
724		    xfer->actlen >= sizeof(pkt)) {
725
726			usbd_copy_out(xfer->frbuffers, 0, &pkt, sizeof(pkt));
727
728			if (pkt.aue_txstat0)
729				ifp->if_oerrors++;
730			if (pkt.aue_txstat0 & (AUE_TXSTAT0_LATECOLL &
731			    AUE_TXSTAT0_EXCESSCOLL))
732				ifp->if_collisions++;
733		}
734		/* FALLTHROUGH */
735	case USB_ST_SETUP:
736tr_setup:
737		xfer->frlengths[0] = xfer->max_data_length;
738		usbd_transfer_submit(xfer);
739		return;
740
741	default:			/* Error */
742		if (xfer->error != USB_ERR_CANCELLED) {
743			/* try to clear stall first */
744			xfer->flags.stall_pipe = 1;
745			goto tr_setup;
746		}
747		return;
748	}
749}
750
751static void
752aue_bulk_read_callback(struct usb_xfer *xfer)
753{
754	struct aue_softc *sc = xfer->priv_sc;
755	struct usb_ether *ue = &sc->sc_ue;
756	struct ifnet *ifp = uether_getifp(ue);
757	struct aue_rxpkt stat;
758
759	switch (USB_GET_STATE(xfer)) {
760	case USB_ST_TRANSFERRED:
761		DPRINTFN(11, "received %d bytes\n", xfer->actlen);
762
763		if (sc->sc_flags & AUE_FLAG_VER_2) {
764
765			if (xfer->actlen == 0) {
766				ifp->if_ierrors++;
767				goto tr_setup;
768			}
769		} else {
770
771			if (xfer->actlen <= (sizeof(stat) + ETHER_CRC_LEN)) {
772				ifp->if_ierrors++;
773				goto tr_setup;
774			}
775			usbd_copy_out(xfer->frbuffers,
776			    xfer->actlen - sizeof(stat), &stat, sizeof(stat));
777
778			/*
779			 * turn off all the non-error bits in the rx status
780			 * word:
781			 */
782			stat.aue_rxstat &= AUE_RXSTAT_MASK;
783			if (stat.aue_rxstat) {
784				ifp->if_ierrors++;
785				goto tr_setup;
786			}
787			/* No errors; receive the packet. */
788			xfer->actlen -= (sizeof(stat) + ETHER_CRC_LEN);
789		}
790		uether_rxbuf(ue, xfer->frbuffers, 0, xfer->actlen);
791
792		/* FALLTHROUGH */
793	case USB_ST_SETUP:
794tr_setup:
795		xfer->frlengths[0] = xfer->max_data_length;
796		usbd_transfer_submit(xfer);
797		uether_rxflush(ue);
798		return;
799
800	default:			/* Error */
801		DPRINTF("bulk read error, %s\n",
802		    usbd_errstr(xfer->error));
803
804		if (xfer->error != USB_ERR_CANCELLED) {
805			/* try to clear stall first */
806			xfer->flags.stall_pipe = 1;
807			goto tr_setup;
808		}
809		return;
810	}
811}
812
813static void
814aue_bulk_write_callback(struct usb_xfer *xfer)
815{
816	struct aue_softc *sc = xfer->priv_sc;
817	struct ifnet *ifp = uether_getifp(&sc->sc_ue);
818	struct mbuf *m;
819	uint8_t buf[2];
820
821	switch (USB_GET_STATE(xfer)) {
822	case USB_ST_TRANSFERRED:
823		DPRINTFN(11, "transfer of %d bytes complete\n", xfer->actlen);
824		ifp->if_opackets++;
825
826		/* FALLTHROUGH */
827	case USB_ST_SETUP:
828tr_setup:
829		if ((sc->sc_flags & AUE_FLAG_LINK) == 0) {
830			/*
831			 * don't send anything if there is no link !
832			 */
833			return;
834		}
835		IFQ_DRV_DEQUEUE(&ifp->if_snd, m);
836
837		if (m == NULL)
838			return;
839		if (m->m_pkthdr.len > MCLBYTES)
840			m->m_pkthdr.len = MCLBYTES;
841		if (sc->sc_flags & AUE_FLAG_VER_2) {
842
843			xfer->frlengths[0] = m->m_pkthdr.len;
844
845			usbd_m_copy_in(xfer->frbuffers, 0,
846			    m, 0, m->m_pkthdr.len);
847
848		} else {
849
850			xfer->frlengths[0] = (m->m_pkthdr.len + 2);
851
852			/*
853		         * The ADMtek documentation says that the
854		         * packet length is supposed to be specified
855		         * in the first two bytes of the transfer,
856		         * however it actually seems to ignore this
857		         * info and base the frame size on the bulk
858		         * transfer length.
859		         */
860			buf[0] = (uint8_t)(m->m_pkthdr.len);
861			buf[1] = (uint8_t)(m->m_pkthdr.len >> 8);
862
863			usbd_copy_in(xfer->frbuffers, 0, buf, 2);
864
865			usbd_m_copy_in(xfer->frbuffers, 2,
866			    m, 0, m->m_pkthdr.len);
867		}
868
869		/*
870		 * if there's a BPF listener, bounce a copy
871		 * of this frame to him:
872		 */
873		BPF_MTAP(ifp, m);
874
875		m_freem(m);
876
877		usbd_transfer_submit(xfer);
878		return;
879
880	default:			/* Error */
881		DPRINTFN(11, "transfer error, %s\n",
882		    usbd_errstr(xfer->error));
883
884		ifp->if_oerrors++;
885
886		if (xfer->error != USB_ERR_CANCELLED) {
887			/* try to clear stall first */
888			xfer->flags.stall_pipe = 1;
889			goto tr_setup;
890		}
891		return;
892	}
893}
894
895static void
896aue_tick(struct usb_ether *ue)
897{
898	struct aue_softc *sc = uether_getsc(ue);
899	struct mii_data *mii = GET_MII(sc);
900
901	AUE_LOCK_ASSERT(sc, MA_OWNED);
902
903	mii_tick(mii);
904	if ((sc->sc_flags & AUE_FLAG_LINK) == 0
905	    && mii->mii_media_status & IFM_ACTIVE &&
906	    IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
907		sc->sc_flags |= AUE_FLAG_LINK;
908		aue_start(ue);
909	}
910}
911
912static void
913aue_start(struct usb_ether *ue)
914{
915	struct aue_softc *sc = uether_getsc(ue);
916
917	/*
918	 * start the USB transfers, if not already started:
919	 */
920	usbd_transfer_start(sc->sc_xfer[AUE_INTR_DT_RD]);
921	usbd_transfer_start(sc->sc_xfer[AUE_BULK_DT_RD]);
922	usbd_transfer_start(sc->sc_xfer[AUE_BULK_DT_WR]);
923}
924
925static void
926aue_init(struct usb_ether *ue)
927{
928	struct aue_softc *sc = uether_getsc(ue);
929	struct ifnet *ifp = uether_getifp(ue);
930	int i;
931
932	AUE_LOCK_ASSERT(sc, MA_OWNED);
933
934	/*
935	 * Cancel pending I/O
936	 */
937	aue_reset(sc);
938
939	/* Set MAC address */
940	for (i = 0; i != ETHER_ADDR_LEN; i++)
941		aue_csr_write_1(sc, AUE_PAR0 + i, IF_LLADDR(ifp)[i]);
942
943	/* update promiscuous setting */
944	aue_setpromisc(ue);
945
946	/* Load the multicast filter. */
947	aue_setmulti(ue);
948
949	/* Enable RX and TX */
950	aue_csr_write_1(sc, AUE_CTL0, AUE_CTL0_RXSTAT_APPEND | AUE_CTL0_RX_ENB);
951	AUE_SETBIT(sc, AUE_CTL0, AUE_CTL0_TX_ENB);
952	AUE_SETBIT(sc, AUE_CTL2, AUE_CTL2_EP3_CLR);
953
954	usbd_transfer_set_stall(sc->sc_xfer[AUE_BULK_DT_WR]);
955
956	ifp->if_drv_flags |= IFF_DRV_RUNNING;
957	aue_start(ue);
958}
959
960static void
961aue_setpromisc(struct usb_ether *ue)
962{
963	struct aue_softc *sc = uether_getsc(ue);
964	struct ifnet *ifp = uether_getifp(ue);
965
966	AUE_LOCK_ASSERT(sc, MA_OWNED);
967
968	/* if we want promiscuous mode, set the allframes bit: */
969	if (ifp->if_flags & IFF_PROMISC)
970		AUE_SETBIT(sc, AUE_CTL2, AUE_CTL2_RX_PROMISC);
971	else
972		AUE_CLRBIT(sc, AUE_CTL2, AUE_CTL2_RX_PROMISC);
973}
974
975/*
976 * Set media options.
977 */
978static int
979aue_ifmedia_upd(struct ifnet *ifp)
980{
981	struct aue_softc *sc = ifp->if_softc;
982	struct mii_data *mii = GET_MII(sc);
983
984	AUE_LOCK_ASSERT(sc, MA_OWNED);
985
986        sc->sc_flags &= ~AUE_FLAG_LINK;
987	if (mii->mii_instance) {
988		struct mii_softc *miisc;
989
990		LIST_FOREACH(miisc, &mii->mii_phys, mii_list)
991			mii_phy_reset(miisc);
992	}
993	mii_mediachg(mii);
994	return (0);
995}
996
997/*
998 * Report current media status.
999 */
1000static void
1001aue_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
1002{
1003	struct aue_softc *sc = ifp->if_softc;
1004	struct mii_data *mii = GET_MII(sc);
1005
1006	AUE_LOCK(sc);
1007	mii_pollstat(mii);
1008	AUE_UNLOCK(sc);
1009	ifmr->ifm_active = mii->mii_media_active;
1010	ifmr->ifm_status = mii->mii_media_status;
1011}
1012
1013/*
1014 * Stop the adapter and free any mbufs allocated to the
1015 * RX and TX lists.
1016 */
1017static void
1018aue_stop(struct usb_ether *ue)
1019{
1020	struct aue_softc *sc = uether_getsc(ue);
1021	struct ifnet *ifp = uether_getifp(ue);
1022
1023	AUE_LOCK_ASSERT(sc, MA_OWNED);
1024
1025	ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1026	sc->sc_flags &= ~AUE_FLAG_LINK;
1027
1028	/*
1029	 * stop all the transfers, if not already stopped:
1030	 */
1031	usbd_transfer_stop(sc->sc_xfer[AUE_BULK_DT_WR]);
1032	usbd_transfer_stop(sc->sc_xfer[AUE_BULK_DT_RD]);
1033	usbd_transfer_stop(sc->sc_xfer[AUE_INTR_DT_RD]);
1034
1035	aue_csr_write_1(sc, AUE_CTL0, 0);
1036	aue_csr_write_1(sc, AUE_CTL1, 0);
1037	aue_reset(sc);
1038}
1039