if_aue.c revision 187192
1/*-
2 * Copyright (c) 1997, 1998, 1999, 2000
3 *	Bill Paul <wpaul@ee.columbia.edu>.  All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 *    must display the following acknowledgement:
15 *	This product includes software developed by Bill Paul.
16 * 4. Neither the name of the author nor the names of any co-contributors
17 *    may be used to endorse or promote products derived from this software
18 *    without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
30 * THE POSSIBILITY OF SUCH DAMAGE.
31 */
32
33#include <sys/cdefs.h>
34__FBSDID("$FreeBSD: head/sys/dev/usb2/ethernet/if_aue2.c 187192 2009-01-13 21:18:14Z thompsa $");
35
36/*
37 * ADMtek AN986 Pegasus and AN8511 Pegasus II USB to ethernet driver.
38 * Datasheet is available from http://www.admtek.com.tw.
39 *
40 * Written by Bill Paul <wpaul@ee.columbia.edu>
41 * Electrical Engineering Department
42 * Columbia University, New York City
43 */
44
45/*
46 * The Pegasus chip uses four USB "endpoints" to provide 10/100 ethernet
47 * support: the control endpoint for reading/writing registers, burst
48 * read endpoint for packet reception, burst write for packet transmission
49 * and one for "interrupts." The chip uses the same RX filter scheme
50 * as the other ADMtek ethernet parts: one perfect filter entry for the
51 * the station address and a 64-bit multicast hash table. The chip supports
52 * both MII and HomePNA attachments.
53 *
54 * Since the maximum data transfer speed of USB is supposed to be 12Mbps,
55 * you're never really going to get 100Mbps speeds from this device. I
56 * think the idea is to allow the device to connect to 10 or 100Mbps
57 * networks, not necessarily to provide 100Mbps performance. Also, since
58 * the controller uses an external PHY chip, it's possible that board
59 * designers might simply choose a 10Mbps PHY.
60 *
61 * Registers are accessed using usb2_do_request(). Packet transfers are
62 * done using usb2_transfer() and friends.
63 */
64
65/*
66 * NOTE: all function names beginning like "aue_cfg_" can only
67 * be called from within the config thread function !
68 */
69
70#include <dev/usb2/include/usb2_devid.h>
71#include <dev/usb2/include/usb2_standard.h>
72#include <dev/usb2/include/usb2_mfunc.h>
73#include <dev/usb2/include/usb2_error.h>
74
75#define	usb2_config_td_cc usb2_ether_cc
76#define	usb2_config_td_softc aue_softc
77
78#define	USB_DEBUG_VAR aue_debug
79
80#include <dev/usb2/core/usb2_core.h>
81#include <dev/usb2/core/usb2_lookup.h>
82#include <dev/usb2/core/usb2_process.h>
83#include <dev/usb2/core/usb2_config_td.h>
84#include <dev/usb2/core/usb2_debug.h>
85#include <dev/usb2/core/usb2_request.h>
86#include <dev/usb2/core/usb2_busdma.h>
87#include <dev/usb2/core/usb2_util.h>
88
89#include <dev/usb2/ethernet/usb2_ethernet.h>
90#include <dev/usb2/ethernet/if_auereg.h>
91
92MODULE_DEPEND(aue, usb2_ethernet, 1, 1, 1);
93MODULE_DEPEND(aue, usb2_core, 1, 1, 1);
94MODULE_DEPEND(aue, ether, 1, 1, 1);
95MODULE_DEPEND(aue, miibus, 1, 1, 1);
96
97#if USB_DEBUG
98static int aue_debug = 0;
99
100SYSCTL_NODE(_hw_usb2, OID_AUTO, aue, CTLFLAG_RW, 0, "USB aue");
101SYSCTL_INT(_hw_usb2_aue, OID_AUTO, debug, CTLFLAG_RW, &aue_debug, 0,
102    "Debug level");
103#endif
104
105/*
106 * Various supported device vendors/products.
107 */
108static const struct usb2_device_id aue_devs[] = {
109	{USB_VPI(USB_VENDOR_3COM, USB_PRODUCT_3COM_3C460B, AUE_FLAG_PII)},
110	{USB_VPI(USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_DSB650TX_PNA, 0)},
111	{USB_VPI(USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_UFE1000, AUE_FLAG_LSYS)},
112	{USB_VPI(USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_XX10, 0)},
113	{USB_VPI(USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_XX1, AUE_FLAG_PNA | AUE_FLAG_PII)},
114	{USB_VPI(USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_XX2, AUE_FLAG_PII)},
115	{USB_VPI(USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_XX4, AUE_FLAG_PNA)},
116	{USB_VPI(USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_XX5, AUE_FLAG_PNA)},
117	{USB_VPI(USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_XX6, AUE_FLAG_PII)},
118	{USB_VPI(USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_XX7, AUE_FLAG_PII)},
119	{USB_VPI(USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_XX8, AUE_FLAG_PII)},
120	{USB_VPI(USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_XX9, AUE_FLAG_PNA)},
121	{USB_VPI(USB_VENDOR_ACCTON, USB_PRODUCT_ACCTON_SS1001, AUE_FLAG_PII)},
122	{USB_VPI(USB_VENDOR_ACCTON, USB_PRODUCT_ACCTON_USB320_EC, 0)},
123	{USB_VPI(USB_VENDOR_ADMTEK, USB_PRODUCT_ADMTEK_PEGASUSII_2, AUE_FLAG_PII)},
124	{USB_VPI(USB_VENDOR_ADMTEK, USB_PRODUCT_ADMTEK_PEGASUSII_3, AUE_FLAG_PII)},
125	{USB_VPI(USB_VENDOR_ADMTEK, USB_PRODUCT_ADMTEK_PEGASUSII_4, AUE_FLAG_PII)},
126	{USB_VPI(USB_VENDOR_ADMTEK, USB_PRODUCT_ADMTEK_PEGASUSII, AUE_FLAG_PII)},
127	{USB_VPI(USB_VENDOR_ADMTEK, USB_PRODUCT_ADMTEK_PEGASUS, AUE_FLAG_PNA | AUE_FLAG_DUAL_PHY)},
128	{USB_VPI(USB_VENDOR_AEI, USB_PRODUCT_AEI_FASTETHERNET, AUE_FLAG_PII)},
129	{USB_VPI(USB_VENDOR_ALLIEDTELESYN, USB_PRODUCT_ALLIEDTELESYN_ATUSB100, AUE_FLAG_PII)},
130	{USB_VPI(USB_VENDOR_ATEN, USB_PRODUCT_ATEN_UC110T, AUE_FLAG_PII)},
131	{USB_VPI(USB_VENDOR_BELKIN, USB_PRODUCT_BELKIN_USB2LAN, AUE_FLAG_PII)},
132	{USB_VPI(USB_VENDOR_BILLIONTON, USB_PRODUCT_BILLIONTON_USB100, 0)},
133	{USB_VPI(USB_VENDOR_BILLIONTON, USB_PRODUCT_BILLIONTON_USBE100, AUE_FLAG_PII)},
134	{USB_VPI(USB_VENDOR_BILLIONTON, USB_PRODUCT_BILLIONTON_USBEL100, 0)},
135	{USB_VPI(USB_VENDOR_BILLIONTON, USB_PRODUCT_BILLIONTON_USBLP100, AUE_FLAG_PNA)},
136	{USB_VPI(USB_VENDOR_COREGA, USB_PRODUCT_COREGA_FETHER_USB_TXS, AUE_FLAG_PII)},
137	{USB_VPI(USB_VENDOR_COREGA, USB_PRODUCT_COREGA_FETHER_USB_TX, 0)},
138	{USB_VPI(USB_VENDOR_DLINK, USB_PRODUCT_DLINK_DSB650TX1, AUE_FLAG_LSYS)},
139	{USB_VPI(USB_VENDOR_DLINK, USB_PRODUCT_DLINK_DSB650TX2, AUE_FLAG_LSYS | AUE_FLAG_PII)},
140	{USB_VPI(USB_VENDOR_DLINK, USB_PRODUCT_DLINK_DSB650TX3, AUE_FLAG_LSYS | AUE_FLAG_PII)},
141	{USB_VPI(USB_VENDOR_DLINK, USB_PRODUCT_DLINK_DSB650TX4, AUE_FLAG_LSYS | AUE_FLAG_PII)},
142	{USB_VPI(USB_VENDOR_DLINK, USB_PRODUCT_DLINK_DSB650TX_PNA, AUE_FLAG_PNA)},
143	{USB_VPI(USB_VENDOR_DLINK, USB_PRODUCT_DLINK_DSB650TX, AUE_FLAG_LSYS)},
144	{USB_VPI(USB_VENDOR_DLINK, USB_PRODUCT_DLINK_DSB650, AUE_FLAG_LSYS)},
145	{USB_VPI(USB_VENDOR_ELCON, USB_PRODUCT_ELCON_PLAN, AUE_FLAG_PNA | AUE_FLAG_PII)},
146	{USB_VPI(USB_VENDOR_ELECOM, USB_PRODUCT_ELECOM_LDUSB20, AUE_FLAG_PII)},
147	{USB_VPI(USB_VENDOR_ELECOM, USB_PRODUCT_ELECOM_LDUSBLTX, AUE_FLAG_PII)},
148	{USB_VPI(USB_VENDOR_ELECOM, USB_PRODUCT_ELECOM_LDUSBTX0, 0)},
149	{USB_VPI(USB_VENDOR_ELECOM, USB_PRODUCT_ELECOM_LDUSBTX1, AUE_FLAG_LSYS)},
150	{USB_VPI(USB_VENDOR_ELECOM, USB_PRODUCT_ELECOM_LDUSBTX2, 0)},
151	{USB_VPI(USB_VENDOR_ELECOM, USB_PRODUCT_ELECOM_LDUSBTX3, AUE_FLAG_LSYS)},
152	{USB_VPI(USB_VENDOR_ELSA, USB_PRODUCT_ELSA_USB2ETHERNET, 0)},
153	{USB_VPI(USB_VENDOR_GIGABYTE, USB_PRODUCT_GIGABYTE_GNBR402W, 0)},
154	{USB_VPI(USB_VENDOR_HAWKING, USB_PRODUCT_HAWKING_UF100, AUE_FLAG_PII)},
155	{USB_VPI(USB_VENDOR_HP, USB_PRODUCT_HP_HN210E, AUE_FLAG_PII)},
156	{USB_VPI(USB_VENDOR_IODATA, USB_PRODUCT_IODATA_USBETTXS, AUE_FLAG_PII)},
157	{USB_VPI(USB_VENDOR_IODATA, USB_PRODUCT_IODATA_USBETTX, 0)},
158	{USB_VPI(USB_VENDOR_KINGSTON, USB_PRODUCT_KINGSTON_KNU101TX, 0)},
159	{USB_VPI(USB_VENDOR_LINKSYS, USB_PRODUCT_LINKSYS_USB100H1, AUE_FLAG_LSYS | AUE_FLAG_PNA)},
160	{USB_VPI(USB_VENDOR_LINKSYS, USB_PRODUCT_LINKSYS_USB100TX, AUE_FLAG_LSYS)},
161	{USB_VPI(USB_VENDOR_LINKSYS, USB_PRODUCT_LINKSYS_USB10TA, AUE_FLAG_LSYS)},
162	{USB_VPI(USB_VENDOR_LINKSYS, USB_PRODUCT_LINKSYS_USB10TX1, AUE_FLAG_LSYS | AUE_FLAG_PII)},
163	{USB_VPI(USB_VENDOR_LINKSYS, USB_PRODUCT_LINKSYS_USB10TX2, AUE_FLAG_LSYS | AUE_FLAG_PII)},
164	{USB_VPI(USB_VENDOR_LINKSYS, USB_PRODUCT_LINKSYS_USB10T, AUE_FLAG_LSYS)},
165	{USB_VPI(USB_VENDOR_MELCO, USB_PRODUCT_MELCO_LUA2TX5, AUE_FLAG_PII)},
166	{USB_VPI(USB_VENDOR_MELCO, USB_PRODUCT_MELCO_LUATX1, 0)},
167	{USB_VPI(USB_VENDOR_MELCO, USB_PRODUCT_MELCO_LUATX5, 0)},
168	{USB_VPI(USB_VENDOR_MICROSOFT, USB_PRODUCT_MICROSOFT_MN110, AUE_FLAG_PII)},
169	{USB_VPI(USB_VENDOR_NETGEAR, USB_PRODUCT_NETGEAR_FA101, AUE_FLAG_PII)},
170	{USB_VPI(USB_VENDOR_SIEMENS, USB_PRODUCT_SIEMENS_SPEEDSTREAM, AUE_FLAG_PII)},
171	{USB_VPI(USB_VENDOR_SIIG2, USB_PRODUCT_SIIG2_USBTOETHER, AUE_FLAG_PII)},
172	{USB_VPI(USB_VENDOR_SMARTBRIDGES, USB_PRODUCT_SMARTBRIDGES_SMARTNIC, AUE_FLAG_PII)},
173	{USB_VPI(USB_VENDOR_SMC, USB_PRODUCT_SMC_2202USB, 0)},
174	{USB_VPI(USB_VENDOR_SMC, USB_PRODUCT_SMC_2206USB, AUE_FLAG_PII)},
175	{USB_VPI(USB_VENDOR_SOHOWARE, USB_PRODUCT_SOHOWARE_NUB100, 0)},
176	{USB_VPI(USB_VENDOR_SOHOWARE, USB_PRODUCT_SOHOWARE_NUB110, AUE_FLAG_PII)},
177};
178
179/* prototypes */
180
181static device_probe_t aue_probe;
182static device_attach_t aue_attach;
183static device_detach_t aue_detach;
184static device_shutdown_t aue_shutdown;
185
186static usb2_callback_t aue_intr_clear_stall_callback;
187static usb2_callback_t aue_intr_callback;
188static usb2_callback_t aue_bulk_read_clear_stall_callback;
189static usb2_callback_t aue_bulk_read_callback;
190static usb2_callback_t aue_bulk_write_clear_stall_callback;
191static usb2_callback_t aue_bulk_write_callback;
192
193static void	aue_cfg_do_request(struct aue_softc *,
194		    struct usb2_device_request *, void *);
195static uint8_t	aue_cfg_csr_read_1(struct aue_softc *, uint16_t);
196static uint16_t	aue_cfg_csr_read_2(struct aue_softc *, uint16_t);
197static void	aue_cfg_csr_write_1(struct aue_softc *, uint16_t, uint8_t);
198static void	aue_cfg_csr_write_2(struct aue_softc *, uint16_t, uint16_t);
199static void	aue_cfg_eeprom_getword(struct aue_softc *, uint8_t, uint8_t *);
200static void	aue_cfg_read_eeprom(struct aue_softc *, uint8_t *, uint16_t,
201		    uint16_t);
202
203static miibus_readreg_t aue_cfg_miibus_readreg;
204static miibus_writereg_t aue_cfg_miibus_writereg;
205static miibus_statchg_t aue_cfg_miibus_statchg;
206
207static usb2_config_td_command_t aue_cfg_setmulti;
208static usb2_config_td_command_t aue_cfg_first_time_setup;
209static usb2_config_td_command_t aue_config_copy;
210static usb2_config_td_command_t aue_cfg_tick;
211static usb2_config_td_command_t aue_cfg_pre_init;
212static usb2_config_td_command_t aue_cfg_init;
213static usb2_config_td_command_t aue_cfg_promisc_upd;
214static usb2_config_td_command_t aue_cfg_ifmedia_upd;
215static usb2_config_td_command_t aue_cfg_pre_stop;
216static usb2_config_td_command_t aue_cfg_stop;
217
218static void	aue_cfg_reset_pegasus_II(struct aue_softc *);
219static void	aue_cfg_reset(struct aue_softc *);
220static void	aue_start_cb(struct ifnet *);
221static void	aue_init_cb(void *);
222static void	aue_start_transfers(struct aue_softc *);
223static int	aue_ifmedia_upd_cb(struct ifnet *);
224static void	aue_ifmedia_sts_cb(struct ifnet *, struct ifmediareq *);
225static int	aue_ioctl_cb(struct ifnet *, u_long, caddr_t);
226static void	aue_watchdog(void *);
227
228static const struct usb2_config aue_config[AUE_ENDPT_MAX] = {
229
230	[0] = {
231		.type = UE_BULK,
232		.endpoint = UE_ADDR_ANY,
233		.direction = UE_DIR_OUT,
234		.mh.bufsize = (MCLBYTES + 2),
235		.mh.flags = {.pipe_bof = 1,.force_short_xfer = 1,},
236		.mh.callback = &aue_bulk_write_callback,
237		.mh.timeout = 10000,	/* 10 seconds */
238	},
239
240	[1] = {
241		.type = UE_BULK,
242		.endpoint = UE_ADDR_ANY,
243		.direction = UE_DIR_IN,
244		.mh.bufsize = (MCLBYTES + 4 + ETHER_CRC_LEN),
245		.mh.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
246		.mh.callback = &aue_bulk_read_callback,
247	},
248
249	[2] = {
250		.type = UE_CONTROL,
251		.endpoint = 0x00,	/* Control pipe */
252		.direction = UE_DIR_ANY,
253		.mh.bufsize = sizeof(struct usb2_device_request),
254		.mh.flags = {},
255		.mh.callback = &aue_bulk_write_clear_stall_callback,
256		.mh.timeout = 1000,	/* 1 second */
257		.mh.interval = 50,	/* 50ms */
258	},
259
260	[3] = {
261		.type = UE_CONTROL,
262		.endpoint = 0x00,	/* Control pipe */
263		.direction = UE_DIR_ANY,
264		.mh.bufsize = sizeof(struct usb2_device_request),
265		.mh.flags = {},
266		.mh.callback = &aue_bulk_read_clear_stall_callback,
267		.mh.timeout = 1000,	/* 1 second */
268		.mh.interval = 50,	/* 50ms */
269	},
270
271	[4] = {
272		.type = UE_INTERRUPT,
273		.endpoint = UE_ADDR_ANY,
274		.direction = UE_DIR_IN,
275		.mh.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
276		.mh.bufsize = 0,	/* use wMaxPacketSize */
277		.mh.callback = &aue_intr_callback,
278	},
279
280	[5] = {
281		.type = UE_CONTROL,
282		.endpoint = 0x00,	/* Control pipe */
283		.direction = UE_DIR_ANY,
284		.mh.bufsize = sizeof(struct usb2_device_request),
285		.mh.flags = {},
286		.mh.callback = &aue_intr_clear_stall_callback,
287		.mh.timeout = 1000,	/* 1 second */
288		.mh.interval = 50,	/* 50ms */
289	},
290};
291
292static device_method_t aue_methods[] = {
293	/* Device interface */
294	DEVMETHOD(device_probe, aue_probe),
295	DEVMETHOD(device_attach, aue_attach),
296	DEVMETHOD(device_detach, aue_detach),
297	DEVMETHOD(device_shutdown, aue_shutdown),
298
299	/* bus interface */
300	DEVMETHOD(bus_print_child, bus_generic_print_child),
301	DEVMETHOD(bus_driver_added, bus_generic_driver_added),
302
303	/* MII interface */
304	DEVMETHOD(miibus_readreg, aue_cfg_miibus_readreg),
305	DEVMETHOD(miibus_writereg, aue_cfg_miibus_writereg),
306	DEVMETHOD(miibus_statchg, aue_cfg_miibus_statchg),
307
308	{0, 0}
309};
310
311static driver_t aue_driver = {
312	.name = "aue",
313	.methods = aue_methods,
314	.size = sizeof(struct aue_softc)
315};
316
317static devclass_t aue_devclass;
318
319DRIVER_MODULE(aue, ushub, aue_driver, aue_devclass, NULL, 0);
320DRIVER_MODULE(miibus, aue, miibus_driver, miibus_devclass, 0, 0);
321
322static void
323aue_cfg_do_request(struct aue_softc *sc, struct usb2_device_request *req,
324    void *data)
325{
326	uint16_t length;
327	usb2_error_t err;
328
329	if (usb2_config_td_is_gone(&sc->sc_config_td)) {
330		goto error;
331	}
332	err = usb2_do_request_flags
333	    (sc->sc_udev, &sc->sc_mtx, req, data, 0, NULL, 1000);
334
335	if (err) {
336
337		DPRINTF("device request failed, err=%s "
338		    "(ignored)\n", usb2_errstr(err));
339
340error:
341		length = UGETW(req->wLength);
342
343		if ((req->bmRequestType & UT_READ) && length) {
344			bzero(data, length);
345		}
346	}
347}
348
349#define	AUE_CFG_SETBIT(sc, reg, x) \
350	aue_cfg_csr_write_1(sc, reg, aue_cfg_csr_read_1(sc, reg) | (x))
351
352#define	AUE_CFG_CLRBIT(sc, reg, x) \
353	aue_cfg_csr_write_1(sc, reg, aue_cfg_csr_read_1(sc, reg) & ~(x))
354
355static uint8_t
356aue_cfg_csr_read_1(struct aue_softc *sc, uint16_t reg)
357{
358	struct usb2_device_request req;
359	uint8_t val;
360
361	req.bmRequestType = UT_READ_VENDOR_DEVICE;
362	req.bRequest = AUE_UR_READREG;
363	USETW(req.wValue, 0);
364	USETW(req.wIndex, reg);
365	USETW(req.wLength, 1);
366
367	aue_cfg_do_request(sc, &req, &val);
368	return (val);
369}
370
371static uint16_t
372aue_cfg_csr_read_2(struct aue_softc *sc, uint16_t reg)
373{
374	struct usb2_device_request req;
375	uint16_t val;
376
377	req.bmRequestType = UT_READ_VENDOR_DEVICE;
378	req.bRequest = AUE_UR_READREG;
379	USETW(req.wValue, 0);
380	USETW(req.wIndex, reg);
381	USETW(req.wLength, 2);
382
383	aue_cfg_do_request(sc, &req, &val);
384	return (le16toh(val));
385}
386
387static void
388aue_cfg_csr_write_1(struct aue_softc *sc, uint16_t reg, uint8_t val)
389{
390	struct usb2_device_request req;
391
392	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
393	req.bRequest = AUE_UR_WRITEREG;
394	req.wValue[0] = val;
395	req.wValue[1] = 0;
396	USETW(req.wIndex, reg);
397	USETW(req.wLength, 1);
398
399	aue_cfg_do_request(sc, &req, &val);
400}
401
402static void
403aue_cfg_csr_write_2(struct aue_softc *sc, uint16_t reg, uint16_t val)
404{
405	struct usb2_device_request req;
406
407	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
408	req.bRequest = AUE_UR_WRITEREG;
409	USETW(req.wValue, val);
410	USETW(req.wIndex, reg);
411	USETW(req.wLength, 2);
412
413	val = htole16(val);
414
415	aue_cfg_do_request(sc, &req, &val);
416}
417
418/*
419 * Read a word of data stored in the EEPROM at address 'addr.'
420 */
421static void
422aue_cfg_eeprom_getword(struct aue_softc *sc, uint8_t addr,
423    uint8_t *dest)
424{
425	uint16_t i;
426
427	aue_cfg_csr_write_1(sc, AUE_EE_REG, addr);
428	aue_cfg_csr_write_1(sc, AUE_EE_CTL, AUE_EECTL_READ);
429
430	for (i = 0;; i++) {
431
432		if (i < AUE_TIMEOUT) {
433
434			if (aue_cfg_csr_read_1(sc, AUE_EE_CTL) & AUE_EECTL_DONE) {
435				break;
436			}
437			if (usb2_config_td_sleep(&sc->sc_config_td, hz / 100)) {
438				break;
439			}
440		} else {
441			DPRINTF("EEPROM read timed out!\n");
442			break;
443		}
444	}
445
446	i = aue_cfg_csr_read_2(sc, AUE_EE_DATA);
447
448	dest[0] = (i & 0xFF);
449	dest[1] = (i >> 8);
450}
451
452/*
453 * Read a sequence of words from the EEPROM.
454 */
455static void
456aue_cfg_read_eeprom(struct aue_softc *sc, uint8_t *dest,
457    uint16_t off, uint16_t len)
458{
459	uint16_t i;
460
461	for (i = 0; i < len; i++) {
462		aue_cfg_eeprom_getword(sc, off + i, dest + (i * 2));
463	}
464}
465
466static int
467aue_cfg_miibus_readreg(device_t dev, int phy, int reg)
468{
469	struct aue_softc *sc = device_get_softc(dev);
470	uint16_t i;
471	uint8_t do_unlock;
472
473	/* avoid recursive locking */
474	if (mtx_owned(&sc->sc_mtx)) {
475		do_unlock = 0;
476	} else {
477		mtx_lock(&sc->sc_mtx);
478		do_unlock = 1;
479	}
480
481	/*
482	 * The Am79C901 HomePNA PHY actually contains
483	 * two transceivers: a 1Mbps HomePNA PHY and a
484	 * 10Mbps full/half duplex ethernet PHY with
485	 * NWAY autoneg. However in the ADMtek adapter,
486	 * only the 1Mbps PHY is actually connected to
487	 * anything, so we ignore the 10Mbps one. It
488	 * happens to be configured for MII address 3,
489	 * so we filter that out.
490	 */
491	if (sc->sc_flags & AUE_FLAG_DUAL_PHY) {
492
493		if (phy == 3) {
494			i = 0;
495			goto done;
496		}
497#if 0
498		if (phy != 1) {
499			i = 0;
500			goto done;
501		}
502#endif
503	}
504	aue_cfg_csr_write_1(sc, AUE_PHY_ADDR, phy);
505	aue_cfg_csr_write_1(sc, AUE_PHY_CTL, reg | AUE_PHYCTL_READ);
506
507	for (i = 0;; i++) {
508
509		if (i < AUE_TIMEOUT) {
510
511			if (aue_cfg_csr_read_1(sc, AUE_PHY_CTL) & AUE_PHYCTL_DONE) {
512				break;
513			}
514			if (usb2_config_td_sleep(&sc->sc_config_td, hz / 100)) {
515				break;
516			}
517		} else {
518			DPRINTF("MII read timed out\n");
519			break;
520		}
521	}
522
523	i = aue_cfg_csr_read_2(sc, AUE_PHY_DATA);
524
525done:
526	if (do_unlock) {
527		mtx_unlock(&sc->sc_mtx);
528	}
529	return (i);
530}
531
532static int
533aue_cfg_miibus_writereg(device_t dev, int phy, int reg, int data)
534{
535	struct aue_softc *sc = device_get_softc(dev);
536	uint16_t i;
537	uint8_t do_unlock;
538
539	if (phy == 3) {
540		return (0);
541	}
542	/* avoid recursive locking */
543	if (mtx_owned(&sc->sc_mtx)) {
544		do_unlock = 0;
545	} else {
546		mtx_lock(&sc->sc_mtx);
547		do_unlock = 1;
548	}
549
550	aue_cfg_csr_write_2(sc, AUE_PHY_DATA, data);
551	aue_cfg_csr_write_1(sc, AUE_PHY_ADDR, phy);
552	aue_cfg_csr_write_1(sc, AUE_PHY_CTL, reg | AUE_PHYCTL_WRITE);
553
554	for (i = 0;; i++) {
555
556		if (i < AUE_TIMEOUT) {
557			if (aue_cfg_csr_read_1(sc, AUE_PHY_CTL) & AUE_PHYCTL_DONE) {
558				break;
559			}
560			if (usb2_config_td_sleep(&sc->sc_config_td, hz / 100)) {
561				break;
562			}
563		} else {
564			DPRINTF("MII write timed out\n");
565			break;
566		}
567	}
568
569	if (do_unlock) {
570		mtx_unlock(&sc->sc_mtx);
571	}
572	return (0);
573}
574
575static void
576aue_cfg_miibus_statchg(device_t dev)
577{
578	struct aue_softc *sc = device_get_softc(dev);
579	struct mii_data *mii = GET_MII(sc);
580	uint8_t do_unlock;
581
582	/* avoid recursive locking */
583	if (mtx_owned(&sc->sc_mtx)) {
584		do_unlock = 0;
585	} else {
586		mtx_lock(&sc->sc_mtx);
587		do_unlock = 1;
588	}
589
590	AUE_CFG_CLRBIT(sc, AUE_CTL0, AUE_CTL0_RX_ENB | AUE_CTL0_TX_ENB);
591
592	if (IFM_SUBTYPE(mii->mii_media_active) == IFM_100_TX) {
593		AUE_CFG_SETBIT(sc, AUE_CTL1, AUE_CTL1_SPEEDSEL);
594	} else {
595		AUE_CFG_CLRBIT(sc, AUE_CTL1, AUE_CTL1_SPEEDSEL);
596	}
597
598	if ((mii->mii_media_active & IFM_GMASK) == IFM_FDX) {
599		AUE_CFG_SETBIT(sc, AUE_CTL1, AUE_CTL1_DUPLEX);
600	} else {
601		AUE_CFG_CLRBIT(sc, AUE_CTL1, AUE_CTL1_DUPLEX);
602	}
603
604	AUE_CFG_SETBIT(sc, AUE_CTL0, AUE_CTL0_RX_ENB | AUE_CTL0_TX_ENB);
605
606	/*
607	 * Set the LED modes on the LinkSys adapter.
608	 * This turns on the 'dual link LED' bin in the auxmode
609	 * register of the Broadcom PHY.
610	 */
611	if (sc->sc_flags & AUE_FLAG_LSYS) {
612		uint16_t auxmode;
613
614		auxmode = aue_cfg_miibus_readreg(dev, 0, 0x1b);
615		aue_cfg_miibus_writereg(dev, 0, 0x1b, auxmode | 0x04);
616	}
617	if (do_unlock) {
618		mtx_unlock(&sc->sc_mtx);
619	}
620}
621
622static void
623aue_cfg_setmulti(struct aue_softc *sc,
624    struct usb2_config_td_cc *cc, uint16_t refcount)
625{
626	uint16_t i;
627
628	if ((cc->if_flags & IFF_ALLMULTI) ||
629	    (cc->if_flags & IFF_PROMISC)) {
630		AUE_CFG_SETBIT(sc, AUE_CTL0, AUE_CTL0_ALLMULTI);
631		return;
632	}
633	AUE_CFG_CLRBIT(sc, AUE_CTL0, AUE_CTL0_ALLMULTI);
634
635	/* clear existing ones */
636	for (i = 0; i < 8; i++) {
637		aue_cfg_csr_write_1(sc, AUE_MAR0 + i, 0);
638	}
639
640	/* now program new ones */
641	for (i = 0; i < 8; i++) {
642		aue_cfg_csr_write_1(sc, AUE_MAR0 + i, cc->if_hash[i]);
643	}
644}
645
646static void
647aue_cfg_reset_pegasus_II(struct aue_softc *sc)
648{
649	/* Magic constants taken from Linux driver. */
650	aue_cfg_csr_write_1(sc, AUE_REG_1D, 0);
651	aue_cfg_csr_write_1(sc, AUE_REG_7B, 2);
652#if 0
653	if ((sc->sc_flags & HAS_HOME_PNA) && mii_mode)
654		aue_cfg_csr_write_1(sc, AUE_REG_81, 6);
655	else
656#endif
657		aue_cfg_csr_write_1(sc, AUE_REG_81, 2);
658}
659
660static void
661aue_cfg_reset(struct aue_softc *sc)
662{
663	uint16_t i;
664
665	AUE_CFG_SETBIT(sc, AUE_CTL1, AUE_CTL1_RESETMAC);
666
667	for (i = 0;; i++) {
668
669		if (i < AUE_TIMEOUT) {
670
671			if (!(aue_cfg_csr_read_1(sc, AUE_CTL1) & AUE_CTL1_RESETMAC)) {
672				break;
673			}
674			if (usb2_config_td_sleep(&sc->sc_config_td, hz / 100)) {
675				break;
676			}
677		} else {
678			DPRINTF("reset timed out\n");
679			break;
680		}
681	}
682
683	/*
684	 * The PHY(s) attached to the Pegasus chip may be held
685	 * in reset until we flip on the GPIO outputs. Make sure
686	 * to set the GPIO pins high so that the PHY(s) will
687	 * be enabled.
688	 *
689	 * Note: We force all of the GPIO pins low first, *then*
690	 * enable the ones we want.
691	 */
692	aue_cfg_csr_write_1(sc, AUE_GPIO0, (AUE_GPIO_OUT0 | AUE_GPIO_SEL0));
693	aue_cfg_csr_write_1(sc, AUE_GPIO0, (AUE_GPIO_OUT0 | AUE_GPIO_SEL0 |
694	    AUE_GPIO_SEL1));
695
696	if (sc->sc_flags & AUE_FLAG_LSYS) {
697		/* Grrr. LinkSys has to be different from everyone else. */
698		aue_cfg_csr_write_1(sc, AUE_GPIO0,
699		    (AUE_GPIO_SEL0 | AUE_GPIO_SEL1));
700		aue_cfg_csr_write_1(sc, AUE_GPIO0,
701		    (AUE_GPIO_SEL0 |
702		    AUE_GPIO_SEL1 |
703		    AUE_GPIO_OUT0));
704	}
705	if (sc->sc_flags & AUE_FLAG_PII) {
706		aue_cfg_reset_pegasus_II(sc);
707	}
708	/* wait a little while for the chip to get its brains in order: */
709	usb2_config_td_sleep(&sc->sc_config_td, hz / 100);
710}
711
712/*
713 * Probe for a Pegasus chip.
714 */
715static int
716aue_probe(device_t dev)
717{
718	struct usb2_attach_arg *uaa = device_get_ivars(dev);
719
720	if (uaa->usb2_mode != USB_MODE_HOST) {
721		return (ENXIO);
722	}
723	if (uaa->info.bConfigIndex != AUE_CONFIG_INDEX) {
724		return (ENXIO);
725	}
726	if (uaa->info.bIfaceIndex != AUE_IFACE_IDX) {
727		return (ENXIO);
728	}
729	/*
730	 * Belkin USB Bluetooth dongles of the F8T012xx1 model series
731	 * conflict with older Belkin USB2LAN adapters. Skip if_aue if
732	 * we detect one of the devices that look like Bluetooth
733	 * adapters.
734	 */
735	if ((uaa->info.idVendor == USB_VENDOR_BELKIN) &&
736	    (uaa->info.idProduct == USB_PRODUCT_BELKIN_F8T012) &&
737	    (uaa->info.bcdDevice == 0x0413)) {
738		return (ENXIO);
739	}
740	return (usb2_lookup_id_by_uaa(aue_devs, sizeof(aue_devs), uaa));
741}
742
743/*
744 * Attach the interface. Allocate softc structures, do ifmedia
745 * setup and ethernet/BPF attach.
746 */
747static int
748aue_attach(device_t dev)
749{
750	struct usb2_attach_arg *uaa = device_get_ivars(dev);
751	struct aue_softc *sc = device_get_softc(dev);
752	int32_t error;
753	uint8_t iface_index;
754
755	if (sc == NULL) {
756		return (ENOMEM);
757	}
758	sc->sc_udev = uaa->device;
759	sc->sc_dev = dev;
760	sc->sc_unit = device_get_unit(dev);
761	sc->sc_flags = USB_GET_DRIVER_INFO(uaa);
762
763	if (uaa->info.bcdDevice >= 0x0201) {
764		sc->sc_flags |= AUE_FLAG_VER_2;	/* XXX currently undocumented */
765	}
766	device_set_usb2_desc(dev);
767
768	snprintf(sc->sc_name, sizeof(sc->sc_name), "%s",
769	    device_get_nameunit(dev));
770
771	mtx_init(&sc->sc_mtx, "aue lock", NULL, MTX_DEF | MTX_RECURSE);
772
773	usb2_callout_init_mtx(&sc->sc_watchdog, &sc->sc_mtx, 0);
774
775	iface_index = AUE_IFACE_IDX;
776	error = usb2_transfer_setup(uaa->device, &iface_index,
777	    sc->sc_xfer, aue_config, AUE_ENDPT_MAX,
778	    sc, &sc->sc_mtx);
779	if (error) {
780		device_printf(dev, "allocating USB "
781		    "transfers failed!\n");
782		goto detach;
783	}
784	error = usb2_config_td_setup(&sc->sc_config_td, sc, &sc->sc_mtx,
785	    NULL, sizeof(struct usb2_config_td_cc), 16);
786	if (error) {
787		device_printf(dev, "could not setup config "
788		    "thread!\n");
789		goto detach;
790	}
791	mtx_lock(&sc->sc_mtx);
792
793	sc->sc_flags |= AUE_FLAG_WAIT_LINK;
794
795	/* start setup */
796
797	usb2_config_td_queue_command
798	    (&sc->sc_config_td, NULL, &aue_cfg_first_time_setup, 0, 0);
799
800	aue_watchdog(sc);
801	mtx_unlock(&sc->sc_mtx);
802	return (0);			/* success */
803
804detach:
805	aue_detach(dev);
806	return (ENXIO);			/* failure */
807}
808
809static void
810aue_cfg_first_time_setup(struct aue_softc *sc,
811    struct usb2_config_td_cc *cc, uint16_t refcount)
812{
813	struct ifnet *ifp;
814	int error;
815	uint8_t eaddr[min(ETHER_ADDR_LEN, 6)];
816
817	/* reset the adapter */
818	aue_cfg_reset(sc);
819
820	/* set default value */
821	bzero(eaddr, sizeof(eaddr));
822
823	/* get station address from the EEPROM */
824	aue_cfg_read_eeprom(sc, eaddr, 0, 3);
825
826	mtx_unlock(&sc->sc_mtx);
827
828	ifp = if_alloc(IFT_ETHER);
829
830	mtx_lock(&sc->sc_mtx);
831
832	if (ifp == NULL) {
833		printf("%s: could not if_alloc()\n",
834		    sc->sc_name);
835		goto done;
836	}
837	sc->sc_evilhack = ifp;
838
839	ifp->if_softc = sc;
840	if_initname(ifp, "aue", sc->sc_unit);
841	ifp->if_mtu = ETHERMTU;
842	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
843	ifp->if_ioctl = aue_ioctl_cb;
844	ifp->if_start = aue_start_cb;
845	ifp->if_watchdog = NULL;
846	ifp->if_init = aue_init_cb;
847	IFQ_SET_MAXLEN(&ifp->if_snd, IFQ_MAXLEN);
848	ifp->if_snd.ifq_drv_maxlen = IFQ_MAXLEN;
849	IFQ_SET_READY(&ifp->if_snd);
850
851	/*
852	 * XXX need Giant when accessing the device structures !
853	 */
854
855	mtx_unlock(&sc->sc_mtx);
856
857	mtx_lock(&Giant);
858
859	error = mii_phy_probe(sc->sc_dev, &sc->sc_miibus,
860	    &aue_ifmedia_upd_cb,
861	    &aue_ifmedia_sts_cb);
862
863	mtx_unlock(&Giant);
864
865	mtx_lock(&sc->sc_mtx);
866
867	/*
868	 * Do MII setup.
869	 * NOTE: Doing this causes child devices to be attached to us,
870	 * which we would normally disconnect at in the detach routine
871	 * using device_delete_child(). However the USB code is set up
872	 * such that when this driver is removed, all children devices
873	 * are removed as well. In effect, the USB code ends up detaching
874	 * all of our children for us, so we don't have to do is ourselves
875	 * in aue_detach(). It's important to point this out since if
876	 * we *do* try to detach the child devices ourselves, we will
877	 * end up getting the children deleted twice, which will crash
878	 * the system.
879	 */
880	if (error) {
881		printf("%s: MII without any PHY!\n",
882		    sc->sc_name);
883		if_free(ifp);
884		goto done;
885	}
886	sc->sc_ifp = ifp;
887
888	mtx_unlock(&sc->sc_mtx);
889
890	/*
891	 * Call MI attach routine.
892	 */
893	ether_ifattach(ifp, eaddr);
894
895	mtx_lock(&sc->sc_mtx);
896
897done:
898	return;
899}
900
901static int
902aue_detach(device_t dev)
903{
904	struct aue_softc *sc = device_get_softc(dev);
905	struct ifnet *ifp;
906
907	usb2_config_td_drain(&sc->sc_config_td);
908
909	mtx_lock(&sc->sc_mtx);
910
911	usb2_callout_stop(&sc->sc_watchdog);
912
913	aue_cfg_pre_stop(sc, NULL, 0);
914
915	ifp = sc->sc_ifp;
916
917	mtx_unlock(&sc->sc_mtx);
918
919	/* stop all USB transfers first */
920	usb2_transfer_unsetup(sc->sc_xfer, AUE_ENDPT_MAX);
921
922	/* get rid of any late children */
923	bus_generic_detach(dev);
924
925	if (ifp) {
926		ether_ifdetach(ifp);
927		if_free(ifp);
928	}
929	usb2_config_td_unsetup(&sc->sc_config_td);
930
931	usb2_callout_drain(&sc->sc_watchdog);
932
933	mtx_destroy(&sc->sc_mtx);
934
935	return (0);
936}
937
938static void
939aue_intr_clear_stall_callback(struct usb2_xfer *xfer)
940{
941	struct aue_softc *sc = xfer->priv_sc;
942	struct usb2_xfer *xfer_other = sc->sc_xfer[4];
943
944	if (usb2_clear_stall_callback(xfer, xfer_other)) {
945		DPRINTF("stall cleared\n");
946		sc->sc_flags &= ~AUE_FLAG_INTR_STALL;
947		usb2_transfer_start(xfer_other);
948	}
949}
950
951static void
952aue_intr_callback(struct usb2_xfer *xfer)
953{
954	struct aue_softc *sc = xfer->priv_sc;
955	struct ifnet *ifp = sc->sc_ifp;
956	struct aue_intrpkt pkt;
957
958	switch (USB_GET_STATE(xfer)) {
959	case USB_ST_TRANSFERRED:
960
961		if (ifp && (ifp->if_drv_flags & IFF_DRV_RUNNING) &&
962		    (xfer->actlen >= sizeof(pkt))) {
963
964			usb2_copy_out(xfer->frbuffers, 0, &pkt, sizeof(pkt));
965
966			if (pkt.aue_txstat0) {
967				ifp->if_oerrors++;
968			}
969			if (pkt.aue_txstat0 & (AUE_TXSTAT0_LATECOLL &
970			    AUE_TXSTAT0_EXCESSCOLL)) {
971				ifp->if_collisions++;
972			}
973		}
974	case USB_ST_SETUP:
975		if (sc->sc_flags & AUE_FLAG_INTR_STALL) {
976			usb2_transfer_start(sc->sc_xfer[5]);
977		} else {
978			xfer->frlengths[0] = xfer->max_data_length;
979			usb2_start_hardware(xfer);
980		}
981		return;
982
983	default:			/* Error */
984		if (xfer->error != USB_ERR_CANCELLED) {
985			/* start clear stall */
986			sc->sc_flags |= AUE_FLAG_INTR_STALL;
987			usb2_transfer_start(sc->sc_xfer[5]);
988		}
989		return;
990	}
991}
992
993static void
994aue_bulk_read_clear_stall_callback(struct usb2_xfer *xfer)
995{
996	struct aue_softc *sc = xfer->priv_sc;
997	struct usb2_xfer *xfer_other = sc->sc_xfer[1];
998
999	if (usb2_clear_stall_callback(xfer, xfer_other)) {
1000		DPRINTF("stall cleared\n");
1001		sc->sc_flags &= ~AUE_FLAG_READ_STALL;
1002		usb2_transfer_start(xfer_other);
1003	}
1004}
1005
1006static void
1007aue_bulk_read_callback(struct usb2_xfer *xfer)
1008{
1009	struct aue_softc *sc = xfer->priv_sc;
1010	struct ifnet *ifp = sc->sc_ifp;
1011	struct mbuf *m = NULL;
1012
1013	switch (USB_GET_STATE(xfer)) {
1014	case USB_ST_TRANSFERRED:
1015		DPRINTFN(11, "received %d bytes\n", xfer->actlen);
1016
1017		if (sc->sc_flags & AUE_FLAG_VER_2) {
1018
1019			if (xfer->actlen == 0) {
1020				ifp->if_ierrors++;
1021				goto tr_setup;
1022			}
1023		} else {
1024
1025			if (xfer->actlen <= (4 + ETHER_CRC_LEN)) {
1026				ifp->if_ierrors++;
1027				goto tr_setup;
1028			}
1029			usb2_copy_out(xfer->frbuffers, xfer->actlen - 4, &sc->sc_rxpkt,
1030			    sizeof(sc->sc_rxpkt));
1031
1032			/*
1033			 * turn off all the non-error bits in the rx status
1034			 * word:
1035			 */
1036			sc->sc_rxpkt.aue_rxstat &= AUE_RXSTAT_MASK;
1037
1038			if (sc->sc_rxpkt.aue_rxstat) {
1039				ifp->if_ierrors++;
1040				goto tr_setup;
1041			}
1042			/* No errors; receive the packet. */
1043			xfer->actlen -= (4 + ETHER_CRC_LEN);
1044		}
1045
1046		m = usb2_ether_get_mbuf();
1047
1048		if (m == NULL) {
1049			ifp->if_ierrors++;
1050			goto tr_setup;
1051		}
1052		xfer->actlen = min(xfer->actlen, m->m_len);
1053
1054		usb2_copy_out(xfer->frbuffers, 0, m->m_data, xfer->actlen);
1055
1056		ifp->if_ipackets++;
1057		m->m_pkthdr.rcvif = ifp;
1058		m->m_pkthdr.len = m->m_len = xfer->actlen;
1059
1060	case USB_ST_SETUP:
1061tr_setup:
1062
1063		if (sc->sc_flags & AUE_FLAG_READ_STALL) {
1064			usb2_transfer_start(sc->sc_xfer[3]);
1065		} else {
1066			xfer->frlengths[0] = xfer->max_data_length;
1067			usb2_start_hardware(xfer);
1068		}
1069
1070		/*
1071		 * At the end of a USB callback it is always safe to unlock
1072		 * the private mutex of a device! That is why we do the
1073		 * "if_input" here, and not some lines up!
1074		 */
1075		if (m) {
1076			mtx_unlock(&sc->sc_mtx);
1077			(ifp->if_input) (ifp, m);
1078			mtx_lock(&sc->sc_mtx);
1079		}
1080		return;
1081
1082	default:			/* Error */
1083		if (xfer->error != USB_ERR_CANCELLED) {
1084			/* try to clear stall first */
1085			sc->sc_flags |= AUE_FLAG_READ_STALL;
1086			usb2_transfer_start(sc->sc_xfer[3]);
1087		}
1088		DPRINTF("bulk read error, %s\n",
1089		    usb2_errstr(xfer->error));
1090		return;
1091
1092	}
1093}
1094
1095static void
1096aue_bulk_write_clear_stall_callback(struct usb2_xfer *xfer)
1097{
1098	struct aue_softc *sc = xfer->priv_sc;
1099	struct usb2_xfer *xfer_other = sc->sc_xfer[0];
1100
1101	if (usb2_clear_stall_callback(xfer, xfer_other)) {
1102		DPRINTF("stall cleared\n");
1103		sc->sc_flags &= ~AUE_FLAG_WRITE_STALL;
1104		usb2_transfer_start(xfer_other);
1105	}
1106}
1107
1108static void
1109aue_bulk_write_callback(struct usb2_xfer *xfer)
1110{
1111	struct aue_softc *sc = xfer->priv_sc;
1112	struct ifnet *ifp = sc->sc_ifp;
1113	struct mbuf *m;
1114	uint8_t buf[2];
1115
1116	switch (USB_GET_STATE(xfer)) {
1117	case USB_ST_TRANSFERRED:
1118		DPRINTFN(11, "transfer of %d bytes complete\n", xfer->actlen);
1119
1120		ifp->if_opackets++;
1121
1122	case USB_ST_SETUP:
1123
1124		if (sc->sc_flags & AUE_FLAG_WRITE_STALL) {
1125			usb2_transfer_start(sc->sc_xfer[2]);
1126			goto done;
1127		}
1128		if (sc->sc_flags & AUE_FLAG_WAIT_LINK) {
1129			/*
1130			 * don't send anything if there is no link !
1131			 */
1132			goto done;
1133		}
1134		IFQ_DRV_DEQUEUE(&ifp->if_snd, m);
1135
1136		if (m == NULL) {
1137			goto done;
1138		}
1139		if (m->m_pkthdr.len > MCLBYTES) {
1140			m->m_pkthdr.len = MCLBYTES;
1141		}
1142		if (sc->sc_flags & AUE_FLAG_VER_2) {
1143
1144			xfer->frlengths[0] = m->m_pkthdr.len;
1145
1146			usb2_m_copy_in(xfer->frbuffers, 0,
1147			    m, 0, m->m_pkthdr.len);
1148
1149		} else {
1150
1151			xfer->frlengths[0] = (m->m_pkthdr.len + 2);
1152
1153			/*
1154		         * The ADMtek documentation says that the packet length is
1155		         * supposed to be specified in the first two bytes of the
1156		         * transfer, however it actually seems to ignore this info
1157		         * and base the frame size on the bulk transfer length.
1158		         */
1159			buf[0] = (uint8_t)(m->m_pkthdr.len);
1160			buf[1] = (uint8_t)(m->m_pkthdr.len >> 8);
1161
1162			usb2_copy_in(xfer->frbuffers, 0, buf, 2);
1163
1164			usb2_m_copy_in(xfer->frbuffers, 2,
1165			    m, 0, m->m_pkthdr.len);
1166		}
1167
1168		/*
1169		 * if there's a BPF listener, bounce a copy
1170		 * of this frame to him:
1171		 */
1172		BPF_MTAP(ifp, m);
1173
1174		m_freem(m);
1175
1176		usb2_start_hardware(xfer);
1177
1178done:
1179		return;
1180
1181	default:			/* Error */
1182		DPRINTFN(11, "transfer error, %s\n",
1183		    usb2_errstr(xfer->error));
1184
1185		if (xfer->error != USB_ERR_CANCELLED) {
1186			/* try to clear stall first */
1187			sc->sc_flags |= AUE_FLAG_WRITE_STALL;
1188			usb2_transfer_start(sc->sc_xfer[2]);
1189		}
1190		ifp->if_oerrors++;
1191		return;
1192
1193	}
1194}
1195
1196#define	AUE_BITS 6
1197
1198static void
1199aue_mchash(struct usb2_config_td_cc *cc, const uint8_t *ptr)
1200{
1201	uint8_t h;
1202
1203	h = ether_crc32_le(ptr, ETHER_ADDR_LEN) &
1204	    ((1 << AUE_BITS) - 1);
1205	cc->if_hash[(h >> 3)] |= (1 << (h & 7));
1206}
1207
1208static void
1209aue_config_copy(struct aue_softc *sc,
1210    struct usb2_config_td_cc *cc, uint16_t refcount)
1211{
1212	bzero(cc, sizeof(*cc));
1213	usb2_ether_cc(sc->sc_ifp, &aue_mchash, cc);
1214}
1215
1216static void
1217aue_cfg_tick(struct aue_softc *sc,
1218    struct usb2_config_td_cc *cc, uint16_t refcount)
1219{
1220	struct ifnet *ifp = sc->sc_ifp;
1221	struct mii_data *mii = GET_MII(sc);
1222
1223	if ((ifp == NULL) ||
1224	    (mii == NULL)) {
1225		/* not ready */
1226		return;
1227	}
1228	mii_tick(mii);
1229
1230	mii_pollstat(mii);
1231
1232	if ((sc->sc_flags & AUE_FLAG_WAIT_LINK) &&
1233	    (mii->mii_media_status & IFM_ACTIVE) &&
1234	    (IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE)) {
1235		sc->sc_flags &= ~AUE_FLAG_WAIT_LINK;
1236	}
1237	sc->sc_media_active = mii->mii_media_active;
1238	sc->sc_media_status = mii->mii_media_status;
1239
1240	/* start stopped transfers, if any */
1241
1242	aue_start_transfers(sc);
1243}
1244
1245static void
1246aue_start_cb(struct ifnet *ifp)
1247{
1248	struct aue_softc *sc = ifp->if_softc;
1249
1250	mtx_lock(&sc->sc_mtx);
1251
1252	aue_start_transfers(sc);
1253
1254	mtx_unlock(&sc->sc_mtx);
1255}
1256
1257static void
1258aue_init_cb(void *arg)
1259{
1260	struct aue_softc *sc = arg;
1261
1262	mtx_lock(&sc->sc_mtx);
1263	usb2_config_td_queue_command
1264	    (&sc->sc_config_td, &aue_cfg_pre_init, &aue_cfg_init, 0, 0);
1265	mtx_unlock(&sc->sc_mtx);
1266}
1267
1268static void
1269aue_start_transfers(struct aue_softc *sc)
1270{
1271	if ((sc->sc_flags & AUE_FLAG_LL_READY) &&
1272	    (sc->sc_flags & AUE_FLAG_HL_READY)) {
1273
1274		/*
1275		 * start the USB transfers, if not already started:
1276		 */
1277		usb2_transfer_start(sc->sc_xfer[4]);
1278		usb2_transfer_start(sc->sc_xfer[1]);
1279		usb2_transfer_start(sc->sc_xfer[0]);
1280	}
1281}
1282
1283static void
1284aue_cfg_pre_init(struct aue_softc *sc,
1285    struct usb2_config_td_cc *cc, uint16_t refcount)
1286{
1287	struct ifnet *ifp = sc->sc_ifp;
1288
1289	/* immediate configuration */
1290
1291	aue_cfg_pre_stop(sc, cc, 0);
1292
1293	ifp->if_drv_flags |= IFF_DRV_RUNNING;
1294
1295	sc->sc_flags |= AUE_FLAG_HL_READY;
1296}
1297
1298static void
1299aue_cfg_init(struct aue_softc *sc,
1300    struct usb2_config_td_cc *cc, uint16_t refcount)
1301{
1302	struct mii_data *mii = GET_MII(sc);
1303	uint8_t i;
1304
1305	/*
1306	 * Cancel pending I/O
1307	 */
1308	aue_cfg_stop(sc, cc, 0);
1309
1310	/* Set MAC address */
1311	for (i = 0; i < ETHER_ADDR_LEN; i++) {
1312		aue_cfg_csr_write_1(sc, AUE_PAR0 + i, cc->if_lladdr[i]);
1313	}
1314
1315	/* update promiscuous setting */
1316	aue_cfg_promisc_upd(sc, cc, 0);
1317
1318	/* load the multicast filter */
1319	aue_cfg_setmulti(sc, cc, 0);
1320
1321	/* enable RX and TX */
1322	aue_cfg_csr_write_1(sc, AUE_CTL0,
1323	    (AUE_CTL0_RXSTAT_APPEND |
1324	    AUE_CTL0_RX_ENB));
1325
1326	AUE_CFG_SETBIT(sc, AUE_CTL0, AUE_CTL0_TX_ENB);
1327	AUE_CFG_SETBIT(sc, AUE_CTL2, AUE_CTL2_EP3_CLR);
1328
1329	mii_mediachg(mii);
1330
1331	sc->sc_flags |= (AUE_FLAG_READ_STALL |
1332	    AUE_FLAG_WRITE_STALL |
1333	    AUE_FLAG_LL_READY);
1334
1335	aue_start_transfers(sc);
1336}
1337
1338static void
1339aue_cfg_promisc_upd(struct aue_softc *sc,
1340    struct usb2_config_td_cc *cc, uint16_t refcount)
1341{
1342	/* if we want promiscuous mode, set the allframes bit: */
1343	if (cc->if_flags & IFF_PROMISC) {
1344		AUE_CFG_SETBIT(sc, AUE_CTL2, AUE_CTL2_RX_PROMISC);
1345	} else {
1346		AUE_CFG_CLRBIT(sc, AUE_CTL2, AUE_CTL2_RX_PROMISC);
1347	}
1348}
1349
1350/*
1351 * Set media options.
1352 */
1353static int
1354aue_ifmedia_upd_cb(struct ifnet *ifp)
1355{
1356	struct aue_softc *sc = ifp->if_softc;
1357
1358	mtx_lock(&sc->sc_mtx);
1359	usb2_config_td_queue_command
1360	    (&sc->sc_config_td, NULL, &aue_cfg_ifmedia_upd, 0, 0);
1361	mtx_unlock(&sc->sc_mtx);
1362
1363	return (0);
1364}
1365
1366static void
1367aue_cfg_ifmedia_upd(struct aue_softc *sc,
1368    struct usb2_config_td_cc *cc, uint16_t refcount)
1369{
1370	struct ifnet *ifp = sc->sc_ifp;
1371	struct mii_data *mii = GET_MII(sc);
1372
1373	if ((ifp == NULL) ||
1374	    (mii == NULL)) {
1375		/* not ready */
1376		return;
1377	}
1378	sc->sc_flags |= AUE_FLAG_WAIT_LINK;
1379
1380	if (mii->mii_instance) {
1381		struct mii_softc *miisc;
1382
1383		LIST_FOREACH(miisc, &mii->mii_phys, mii_list) {
1384			mii_phy_reset(miisc);
1385		}
1386	}
1387	mii_mediachg(mii);
1388}
1389
1390/*
1391 * Report current media status.
1392 */
1393static void
1394aue_ifmedia_sts_cb(struct ifnet *ifp, struct ifmediareq *ifmr)
1395{
1396	struct aue_softc *sc = ifp->if_softc;
1397
1398	mtx_lock(&sc->sc_mtx);
1399
1400	ifmr->ifm_active = sc->sc_media_active;
1401	ifmr->ifm_status = sc->sc_media_status;
1402
1403	mtx_unlock(&sc->sc_mtx);
1404}
1405
1406static int
1407aue_ioctl_cb(struct ifnet *ifp, u_long command, caddr_t data)
1408{
1409	struct aue_softc *sc = ifp->if_softc;
1410	struct mii_data *mii;
1411	int error = 0;
1412
1413	switch (command) {
1414	case SIOCSIFFLAGS:
1415		mtx_lock(&sc->sc_mtx);
1416		if (ifp->if_flags & IFF_UP) {
1417			if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
1418				usb2_config_td_queue_command
1419				    (&sc->sc_config_td, &aue_config_copy,
1420				    &aue_cfg_promisc_upd, 0, 0);
1421			} else {
1422				usb2_config_td_queue_command
1423				    (&sc->sc_config_td, &aue_cfg_pre_init,
1424				    &aue_cfg_init, 0, 0);
1425			}
1426		} else {
1427			if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
1428				usb2_config_td_queue_command
1429				    (&sc->sc_config_td, &aue_cfg_pre_stop,
1430				    &aue_cfg_stop, 0, 0);
1431			}
1432		}
1433		mtx_unlock(&sc->sc_mtx);
1434		break;
1435
1436	case SIOCADDMULTI:
1437	case SIOCDELMULTI:
1438		mtx_lock(&sc->sc_mtx);
1439		usb2_config_td_queue_command
1440		    (&sc->sc_config_td, &aue_config_copy,
1441		    &aue_cfg_setmulti, 0, 0);
1442		mtx_unlock(&sc->sc_mtx);
1443		break;
1444
1445	case SIOCGIFMEDIA:
1446	case SIOCSIFMEDIA:
1447		mii = GET_MII(sc);
1448		if (mii == NULL) {
1449			error = EINVAL;
1450		} else {
1451			error = ifmedia_ioctl
1452			    (ifp, (void *)data, &mii->mii_media, command);
1453		}
1454		break;
1455
1456	default:
1457		error = ether_ioctl(ifp, command, data);
1458		break;
1459	}
1460	return (error);
1461}
1462
1463static void
1464aue_watchdog(void *arg)
1465{
1466	struct aue_softc *sc = arg;
1467
1468	mtx_assert(&sc->sc_mtx, MA_OWNED);
1469
1470	usb2_config_td_queue_command
1471	    (&sc->sc_config_td, NULL, &aue_cfg_tick, 0, 0);
1472
1473	usb2_callout_reset(&sc->sc_watchdog,
1474	    hz, &aue_watchdog, sc);
1475}
1476
1477/*
1478 * Stop the adapter and free any mbufs allocated to the
1479 * RX and TX lists.
1480 *
1481 * NOTE: can be called when "ifp" is NULL
1482 */
1483static void
1484aue_cfg_pre_stop(struct aue_softc *sc,
1485    struct usb2_config_td_cc *cc, uint16_t refcount)
1486{
1487	struct ifnet *ifp = sc->sc_ifp;
1488
1489	if (cc) {
1490		/* copy the needed configuration */
1491		aue_config_copy(sc, cc, refcount);
1492	}
1493	/* immediate configuration */
1494
1495	if (ifp) {
1496		/* clear flags */
1497		ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1498	}
1499	sc->sc_flags &= ~(AUE_FLAG_HL_READY |
1500	    AUE_FLAG_LL_READY);
1501
1502	sc->sc_flags |= AUE_FLAG_WAIT_LINK;
1503
1504	/*
1505	 * stop all the transfers, if not already stopped:
1506	 */
1507	usb2_transfer_stop(sc->sc_xfer[0]);
1508	usb2_transfer_stop(sc->sc_xfer[1]);
1509	usb2_transfer_stop(sc->sc_xfer[2]);
1510	usb2_transfer_stop(sc->sc_xfer[3]);
1511	usb2_transfer_stop(sc->sc_xfer[4]);
1512	usb2_transfer_stop(sc->sc_xfer[5]);
1513}
1514
1515static void
1516aue_cfg_stop(struct aue_softc *sc,
1517    struct usb2_config_td_cc *cc, uint16_t refcount)
1518{
1519	aue_cfg_csr_write_1(sc, AUE_CTL0, 0);
1520	aue_cfg_csr_write_1(sc, AUE_CTL1, 0);
1521	aue_cfg_reset(sc);
1522}
1523
1524/*
1525 * Stop all chip I/O so that the kernel's probe routines don't
1526 * get confused by errant DMAs when rebooting.
1527 */
1528static int
1529aue_shutdown(device_t dev)
1530{
1531	struct aue_softc *sc = device_get_softc(dev);
1532
1533	mtx_lock(&sc->sc_mtx);
1534
1535	usb2_config_td_queue_command
1536	    (&sc->sc_config_td, &aue_cfg_pre_stop,
1537	    &aue_cfg_stop, 0, 0);
1538
1539	mtx_unlock(&sc->sc_mtx);
1540
1541	return (0);
1542}
1543