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