if_aue.c revision 185290
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 185290 2008-11-25 08:04:40Z alfred $");
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_aue2_reg.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 *sc, struct usb2_device_request *req, void *data);
194static uint8_t aue_cfg_csr_read_1(struct aue_softc *sc, uint16_t reg);
195static uint16_t aue_cfg_csr_read_2(struct aue_softc *sc, uint16_t reg);
196static void aue_cfg_csr_write_1(struct aue_softc *sc, uint16_t reg, uint8_t val);
197static void aue_cfg_csr_write_2(struct aue_softc *sc, uint16_t reg, uint16_t val);
198static void aue_cfg_eeprom_getword(struct aue_softc *sc, uint8_t addr, uint8_t *dest);
199static void aue_cfg_read_eeprom(struct aue_softc *sc, uint8_t *dest, uint16_t off, uint16_t len);
200
201static miibus_readreg_t aue_cfg_miibus_readreg;
202static miibus_writereg_t aue_cfg_miibus_writereg;
203static miibus_statchg_t aue_cfg_miibus_statchg;
204
205static usb2_config_td_command_t aue_cfg_setmulti;
206static usb2_config_td_command_t aue_cfg_first_time_setup;
207static usb2_config_td_command_t aue_config_copy;
208static usb2_config_td_command_t aue_cfg_tick;
209static usb2_config_td_command_t aue_cfg_pre_init;
210static usb2_config_td_command_t aue_cfg_init;
211static usb2_config_td_command_t aue_cfg_promisc_upd;
212static usb2_config_td_command_t aue_cfg_ifmedia_upd;
213static usb2_config_td_command_t aue_cfg_pre_stop;
214static usb2_config_td_command_t aue_cfg_stop;
215
216static void aue_cfg_reset_pegasus_II(struct aue_softc *sc);
217static void aue_cfg_reset(struct aue_softc *sc);
218static void aue_start_cb(struct ifnet *ifp);
219static void aue_init_cb(void *arg);
220static void aue_start_transfers(struct aue_softc *sc);
221static int aue_ifmedia_upd_cb(struct ifnet *ifp);
222static void aue_ifmedia_sts_cb(struct ifnet *ifp, struct ifmediareq *ifmr);
223static int aue_ioctl_cb(struct ifnet *ifp, u_long command, caddr_t data);
224static void aue_watchdog(void *arg);
225
226static const struct usb2_config aue_config[AUE_ENDPT_MAX] = {
227
228	[0] = {
229		.type = UE_BULK,
230		.endpoint = UE_ADDR_ANY,
231		.direction = UE_DIR_OUT,
232		.mh.bufsize = (MCLBYTES + 2),
233		.mh.flags = {.pipe_bof = 1,.force_short_xfer = 1,},
234		.mh.callback = &aue_bulk_write_callback,
235		.mh.timeout = 10000,	/* 10 seconds */
236	},
237
238	[1] = {
239		.type = UE_BULK,
240		.endpoint = UE_ADDR_ANY,
241		.direction = UE_DIR_IN,
242		.mh.bufsize = (MCLBYTES + 4 + ETHER_CRC_LEN),
243		.mh.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
244		.mh.callback = &aue_bulk_read_callback,
245	},
246
247	[2] = {
248		.type = UE_CONTROL,
249		.endpoint = 0x00,	/* Control pipe */
250		.direction = UE_DIR_ANY,
251		.mh.bufsize = sizeof(struct usb2_device_request),
252		.mh.flags = {},
253		.mh.callback = &aue_bulk_write_clear_stall_callback,
254		.mh.timeout = 1000,	/* 1 second */
255		.mh.interval = 50,	/* 50ms */
256	},
257
258	[3] = {
259		.type = UE_CONTROL,
260		.endpoint = 0x00,	/* Control pipe */
261		.direction = UE_DIR_ANY,
262		.mh.bufsize = sizeof(struct usb2_device_request),
263		.mh.flags = {},
264		.mh.callback = &aue_bulk_read_clear_stall_callback,
265		.mh.timeout = 1000,	/* 1 second */
266		.mh.interval = 50,	/* 50ms */
267	},
268
269	[4] = {
270		.type = UE_INTERRUPT,
271		.endpoint = UE_ADDR_ANY,
272		.direction = UE_DIR_IN,
273		.mh.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
274		.mh.bufsize = 0,	/* use wMaxPacketSize */
275		.mh.callback = &aue_intr_callback,
276	},
277
278	[5] = {
279		.type = UE_CONTROL,
280		.endpoint = 0x00,	/* Control pipe */
281		.direction = UE_DIR_ANY,
282		.mh.bufsize = sizeof(struct usb2_device_request),
283		.mh.flags = {},
284		.mh.callback = &aue_intr_clear_stall_callback,
285		.mh.timeout = 1000,	/* 1 second */
286		.mh.interval = 50,	/* 50ms */
287	},
288};
289
290static device_method_t aue_methods[] = {
291	/* Device interface */
292	DEVMETHOD(device_probe, aue_probe),
293	DEVMETHOD(device_attach, aue_attach),
294	DEVMETHOD(device_detach, aue_detach),
295	DEVMETHOD(device_shutdown, aue_shutdown),
296
297	/* bus interface */
298	DEVMETHOD(bus_print_child, bus_generic_print_child),
299	DEVMETHOD(bus_driver_added, bus_generic_driver_added),
300
301	/* MII interface */
302	DEVMETHOD(miibus_readreg, aue_cfg_miibus_readreg),
303	DEVMETHOD(miibus_writereg, aue_cfg_miibus_writereg),
304	DEVMETHOD(miibus_statchg, aue_cfg_miibus_statchg),
305
306	{0, 0}
307};
308
309static driver_t aue_driver = {
310	.name = "aue",
311	.methods = aue_methods,
312	.size = sizeof(struct aue_softc)
313};
314
315static devclass_t aue_devclass;
316
317DRIVER_MODULE(aue, ushub, aue_driver, aue_devclass, NULL, 0);
318DRIVER_MODULE(miibus, aue, miibus_driver, miibus_devclass, 0, 0);
319
320static void
321aue_cfg_do_request(struct aue_softc *sc, struct usb2_device_request *req,
322    void *data)
323{
324	uint16_t length;
325	usb2_error_t err;
326
327	if (usb2_config_td_is_gone(&sc->sc_config_td)) {
328		goto error;
329	}
330	err = usb2_do_request_flags
331	    (sc->sc_udev, &sc->sc_mtx, req, data, 0, NULL, 1000);
332
333	if (err) {
334
335		DPRINTF("device request failed, err=%s "
336		    "(ignored)\n", usb2_errstr(err));
337
338error:
339		length = UGETW(req->wLength);
340
341		if ((req->bmRequestType & UT_READ) && length) {
342			bzero(data, length);
343		}
344	}
345	return;
346}
347
348#define	AUE_CFG_SETBIT(sc, reg, x) \
349	aue_cfg_csr_write_1(sc, reg, aue_cfg_csr_read_1(sc, reg) | (x))
350
351#define	AUE_CFG_CLRBIT(sc, reg, x) \
352	aue_cfg_csr_write_1(sc, reg, aue_cfg_csr_read_1(sc, reg) & ~(x))
353
354static uint8_t
355aue_cfg_csr_read_1(struct aue_softc *sc, uint16_t reg)
356{
357	struct usb2_device_request req;
358	uint8_t val;
359
360	req.bmRequestType = UT_READ_VENDOR_DEVICE;
361	req.bRequest = AUE_UR_READREG;
362	USETW(req.wValue, 0);
363	USETW(req.wIndex, reg);
364	USETW(req.wLength, 1);
365
366	aue_cfg_do_request(sc, &req, &val);
367	return (val);
368}
369
370static uint16_t
371aue_cfg_csr_read_2(struct aue_softc *sc, uint16_t reg)
372{
373	struct usb2_device_request req;
374	uint16_t val;
375
376	req.bmRequestType = UT_READ_VENDOR_DEVICE;
377	req.bRequest = AUE_UR_READREG;
378	USETW(req.wValue, 0);
379	USETW(req.wIndex, reg);
380	USETW(req.wLength, 2);
381
382	aue_cfg_do_request(sc, &req, &val);
383	return (le16toh(val));
384}
385
386static void
387aue_cfg_csr_write_1(struct aue_softc *sc, uint16_t reg, uint8_t val)
388{
389	struct usb2_device_request req;
390
391	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
392	req.bRequest = AUE_UR_WRITEREG;
393	req.wValue[0] = val;
394	req.wValue[1] = 0;
395	USETW(req.wIndex, reg);
396	USETW(req.wLength, 1);
397
398	aue_cfg_do_request(sc, &req, &val);
399	return;
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	return;
417}
418
419/*
420 * Read a word of data stored in the EEPROM at address 'addr.'
421 */
422static void
423aue_cfg_eeprom_getword(struct aue_softc *sc, uint8_t addr,
424    uint8_t *dest)
425{
426	uint16_t i;
427
428	aue_cfg_csr_write_1(sc, AUE_EE_REG, addr);
429	aue_cfg_csr_write_1(sc, AUE_EE_CTL, AUE_EECTL_READ);
430
431	for (i = 0;; i++) {
432
433		if (i < AUE_TIMEOUT) {
434
435			if (aue_cfg_csr_read_1(sc, AUE_EE_CTL) & AUE_EECTL_DONE) {
436				break;
437			}
438			if (usb2_config_td_sleep(&sc->sc_config_td, hz / 100)) {
439				break;
440			}
441		} else {
442			DPRINTF("EEPROM read timed out!\n");
443			break;
444		}
445	}
446
447	i = aue_cfg_csr_read_2(sc, AUE_EE_DATA);
448
449	dest[0] = (i & 0xFF);
450	dest[1] = (i >> 8);
451
452	return;
453}
454
455/*
456 * Read a sequence of words from the EEPROM.
457 */
458static void
459aue_cfg_read_eeprom(struct aue_softc *sc, uint8_t *dest,
460    uint16_t off, uint16_t len)
461{
462	uint16_t i;
463
464	for (i = 0; i < len; i++) {
465		aue_cfg_eeprom_getword(sc, off + i, dest + (i * 2));
466	}
467	return;
468}
469
470static int
471aue_cfg_miibus_readreg(device_t dev, int phy, int reg)
472{
473	struct aue_softc *sc = device_get_softc(dev);
474	uint16_t i;
475	uint8_t do_unlock;
476
477	/* avoid recursive locking */
478	if (mtx_owned(&sc->sc_mtx)) {
479		do_unlock = 0;
480	} else {
481		mtx_lock(&sc->sc_mtx);
482		do_unlock = 1;
483	}
484
485	/*
486	 * The Am79C901 HomePNA PHY actually contains
487	 * two transceivers: a 1Mbps HomePNA PHY and a
488	 * 10Mbps full/half duplex ethernet PHY with
489	 * NWAY autoneg. However in the ADMtek adapter,
490	 * only the 1Mbps PHY is actually connected to
491	 * anything, so we ignore the 10Mbps one. It
492	 * happens to be configured for MII address 3,
493	 * so we filter that out.
494	 */
495	if (sc->sc_flags & AUE_FLAG_DUAL_PHY) {
496
497		if (phy == 3) {
498			i = 0;
499			goto done;
500		}
501#if 0
502		if (phy != 1) {
503			i = 0;
504			goto done;
505		}
506#endif
507	}
508	aue_cfg_csr_write_1(sc, AUE_PHY_ADDR, phy);
509	aue_cfg_csr_write_1(sc, AUE_PHY_CTL, reg | AUE_PHYCTL_READ);
510
511	for (i = 0;; i++) {
512
513		if (i < AUE_TIMEOUT) {
514
515			if (aue_cfg_csr_read_1(sc, AUE_PHY_CTL) & AUE_PHYCTL_DONE) {
516				break;
517			}
518			if (usb2_config_td_sleep(&sc->sc_config_td, hz / 100)) {
519				break;
520			}
521		} else {
522			DPRINTF("MII read timed out\n");
523			break;
524		}
525	}
526
527	i = aue_cfg_csr_read_2(sc, AUE_PHY_DATA);
528
529done:
530	if (do_unlock) {
531		mtx_unlock(&sc->sc_mtx);
532	}
533	return (i);
534}
535
536static int
537aue_cfg_miibus_writereg(device_t dev, int phy, int reg, int data)
538{
539	struct aue_softc *sc = device_get_softc(dev);
540	uint16_t i;
541	uint8_t do_unlock;
542
543	if (phy == 3) {
544		return (0);
545	}
546	/* avoid recursive locking */
547	if (mtx_owned(&sc->sc_mtx)) {
548		do_unlock = 0;
549	} else {
550		mtx_lock(&sc->sc_mtx);
551		do_unlock = 1;
552	}
553
554	aue_cfg_csr_write_2(sc, AUE_PHY_DATA, data);
555	aue_cfg_csr_write_1(sc, AUE_PHY_ADDR, phy);
556	aue_cfg_csr_write_1(sc, AUE_PHY_CTL, reg | AUE_PHYCTL_WRITE);
557
558	for (i = 0;; i++) {
559
560		if (i < AUE_TIMEOUT) {
561			if (aue_cfg_csr_read_1(sc, AUE_PHY_CTL) & AUE_PHYCTL_DONE) {
562				break;
563			}
564			if (usb2_config_td_sleep(&sc->sc_config_td, hz / 100)) {
565				break;
566			}
567		} else {
568			DPRINTF("MII write timed out\n");
569			break;
570		}
571	}
572
573	if (do_unlock) {
574		mtx_unlock(&sc->sc_mtx);
575	}
576	return (0);
577}
578
579static void
580aue_cfg_miibus_statchg(device_t dev)
581{
582	struct aue_softc *sc = device_get_softc(dev);
583	struct mii_data *mii = GET_MII(sc);
584	uint8_t do_unlock;
585
586	/* avoid recursive locking */
587	if (mtx_owned(&sc->sc_mtx)) {
588		do_unlock = 0;
589	} else {
590		mtx_lock(&sc->sc_mtx);
591		do_unlock = 1;
592	}
593
594	AUE_CFG_CLRBIT(sc, AUE_CTL0, AUE_CTL0_RX_ENB | AUE_CTL0_TX_ENB);
595
596	if (IFM_SUBTYPE(mii->mii_media_active) == IFM_100_TX) {
597		AUE_CFG_SETBIT(sc, AUE_CTL1, AUE_CTL1_SPEEDSEL);
598	} else {
599		AUE_CFG_CLRBIT(sc, AUE_CTL1, AUE_CTL1_SPEEDSEL);
600	}
601
602	if ((mii->mii_media_active & IFM_GMASK) == IFM_FDX) {
603		AUE_CFG_SETBIT(sc, AUE_CTL1, AUE_CTL1_DUPLEX);
604	} else {
605		AUE_CFG_CLRBIT(sc, AUE_CTL1, AUE_CTL1_DUPLEX);
606	}
607
608	AUE_CFG_SETBIT(sc, AUE_CTL0, AUE_CTL0_RX_ENB | AUE_CTL0_TX_ENB);
609
610	/*
611	 * Set the LED modes on the LinkSys adapter.
612	 * This turns on the 'dual link LED' bin in the auxmode
613	 * register of the Broadcom PHY.
614	 */
615	if (sc->sc_flags & AUE_FLAG_LSYS) {
616		uint16_t auxmode;
617
618		auxmode = aue_cfg_miibus_readreg(dev, 0, 0x1b);
619		aue_cfg_miibus_writereg(dev, 0, 0x1b, auxmode | 0x04);
620	}
621	if (do_unlock) {
622		mtx_unlock(&sc->sc_mtx);
623	}
624	return;
625}
626
627static void
628aue_cfg_setmulti(struct aue_softc *sc,
629    struct usb2_config_td_cc *cc, uint16_t refcount)
630{
631	uint16_t i;
632
633	if ((cc->if_flags & IFF_ALLMULTI) ||
634	    (cc->if_flags & IFF_PROMISC)) {
635		AUE_CFG_SETBIT(sc, AUE_CTL0, AUE_CTL0_ALLMULTI);
636		return;
637	}
638	AUE_CFG_CLRBIT(sc, AUE_CTL0, AUE_CTL0_ALLMULTI);
639
640	/* clear existing ones */
641	for (i = 0; i < 8; i++) {
642		aue_cfg_csr_write_1(sc, AUE_MAR0 + i, 0);
643	}
644
645	/* now program new ones */
646	for (i = 0; i < 8; i++) {
647		aue_cfg_csr_write_1(sc, AUE_MAR0 + i, cc->if_hash[i]);
648	}
649	return;
650}
651
652static void
653aue_cfg_reset_pegasus_II(struct aue_softc *sc)
654{
655	/* Magic constants taken from Linux driver. */
656	aue_cfg_csr_write_1(sc, AUE_REG_1D, 0);
657	aue_cfg_csr_write_1(sc, AUE_REG_7B, 2);
658#if 0
659	if ((sc->sc_flags & HAS_HOME_PNA) && mii_mode)
660		aue_cfg_csr_write_1(sc, AUE_REG_81, 6);
661	else
662#endif
663		aue_cfg_csr_write_1(sc, AUE_REG_81, 2);
664
665	return;
666}
667
668static void
669aue_cfg_reset(struct aue_softc *sc)
670{
671	uint16_t i;
672
673	AUE_CFG_SETBIT(sc, AUE_CTL1, AUE_CTL1_RESETMAC);
674
675	for (i = 0;; i++) {
676
677		if (i < AUE_TIMEOUT) {
678
679			if (!(aue_cfg_csr_read_1(sc, AUE_CTL1) & AUE_CTL1_RESETMAC)) {
680				break;
681			}
682			if (usb2_config_td_sleep(&sc->sc_config_td, hz / 100)) {
683				break;
684			}
685		} else {
686			DPRINTF("reset timed out\n");
687			break;
688		}
689	}
690
691	/*
692	 * The PHY(s) attached to the Pegasus chip may be held
693	 * in reset until we flip on the GPIO outputs. Make sure
694	 * to set the GPIO pins high so that the PHY(s) will
695	 * be enabled.
696	 *
697	 * Note: We force all of the GPIO pins low first, *then*
698	 * enable the ones we want.
699	 */
700	aue_cfg_csr_write_1(sc, AUE_GPIO0, (AUE_GPIO_OUT0 | AUE_GPIO_SEL0));
701	aue_cfg_csr_write_1(sc, AUE_GPIO0, (AUE_GPIO_OUT0 | AUE_GPIO_SEL0 |
702	    AUE_GPIO_SEL1));
703
704	if (sc->sc_flags & AUE_FLAG_LSYS) {
705		/* Grrr. LinkSys has to be different from everyone else. */
706		aue_cfg_csr_write_1(sc, AUE_GPIO0,
707		    (AUE_GPIO_SEL0 | AUE_GPIO_SEL1));
708		aue_cfg_csr_write_1(sc, AUE_GPIO0,
709		    (AUE_GPIO_SEL0 |
710		    AUE_GPIO_SEL1 |
711		    AUE_GPIO_OUT0));
712	}
713	if (sc->sc_flags & AUE_FLAG_PII) {
714		aue_cfg_reset_pegasus_II(sc);
715	}
716	/* wait a little while for the chip to get its brains in order: */
717	usb2_config_td_sleep(&sc->sc_config_td, hz / 100);
718
719	return;
720}
721
722/*
723 * Probe for a Pegasus chip.
724 */
725static int
726aue_probe(device_t dev)
727{
728	struct usb2_attach_arg *uaa = device_get_ivars(dev);
729
730	if (uaa->usb2_mode != USB_MODE_HOST) {
731		return (ENXIO);
732	}
733	if (uaa->info.bConfigIndex != AUE_CONFIG_INDEX) {
734		return (ENXIO);
735	}
736	if (uaa->info.bIfaceIndex != AUE_IFACE_IDX) {
737		return (ENXIO);
738	}
739	/*
740	 * Belkin USB Bluetooth dongles of the F8T012xx1 model series
741	 * conflict with older Belkin USB2LAN adapters. Skip if_aue if
742	 * we detect one of the devices that look like Bluetooth
743	 * adapters.
744	 */
745	if ((uaa->info.idVendor == USB_VENDOR_BELKIN) &&
746	    (uaa->info.idProduct == USB_PRODUCT_BELKIN_F8T012) &&
747	    (uaa->info.bcdDevice == 0x0413)) {
748		return (ENXIO);
749	}
750	return (usb2_lookup_id_by_uaa(aue_devs, sizeof(aue_devs), uaa));
751}
752
753/*
754 * Attach the interface. Allocate softc structures, do ifmedia
755 * setup and ethernet/BPF attach.
756 */
757static int
758aue_attach(device_t dev)
759{
760	struct usb2_attach_arg *uaa = device_get_ivars(dev);
761	struct aue_softc *sc = device_get_softc(dev);
762	int32_t error;
763	uint8_t iface_index;
764
765	if (sc == NULL) {
766		return (ENOMEM);
767	}
768	sc->sc_udev = uaa->device;
769	sc->sc_dev = dev;
770	sc->sc_unit = device_get_unit(dev);
771	sc->sc_flags = USB_GET_DRIVER_INFO(uaa);
772
773	if (uaa->info.bcdDevice >= 0x0201) {
774		sc->sc_flags |= AUE_FLAG_VER_2;	/* XXX currently undocumented */
775	}
776	device_set_usb2_desc(dev);
777
778	snprintf(sc->sc_name, sizeof(sc->sc_name), "%s",
779	    device_get_nameunit(dev));
780
781	mtx_init(&sc->sc_mtx, "aue lock", NULL, MTX_DEF | MTX_RECURSE);
782
783	usb2_callout_init_mtx(&sc->sc_watchdog,
784	    &sc->sc_mtx, CALLOUT_RETURNUNLOCKED);
785
786	iface_index = AUE_IFACE_IDX;
787	error = usb2_transfer_setup(uaa->device, &iface_index,
788	    sc->sc_xfer, aue_config, AUE_ENDPT_MAX,
789	    sc, &sc->sc_mtx);
790	if (error) {
791		device_printf(dev, "allocating USB "
792		    "transfers failed!\n");
793		goto detach;
794	}
795	error = usb2_config_td_setup(&sc->sc_config_td, sc, &sc->sc_mtx,
796	    NULL, sizeof(struct usb2_config_td_cc), 16);
797	if (error) {
798		device_printf(dev, "could not setup config "
799		    "thread!\n");
800		goto detach;
801	}
802	mtx_lock(&sc->sc_mtx);
803
804	sc->sc_flags |= AUE_FLAG_WAIT_LINK;
805
806	/* start setup */
807
808	usb2_config_td_queue_command
809	    (&sc->sc_config_td, NULL, &aue_cfg_first_time_setup, 0, 0);
810
811	/* start watchdog (will exit mutex) */
812
813	aue_watchdog(sc);
814
815	return (0);			/* success */
816
817detach:
818	aue_detach(dev);
819	return (ENXIO);			/* failure */
820}
821
822static void
823aue_cfg_first_time_setup(struct aue_softc *sc,
824    struct usb2_config_td_cc *cc, uint16_t refcount)
825{
826	struct ifnet *ifp;
827	int error;
828	uint8_t eaddr[min(ETHER_ADDR_LEN, 6)];
829
830	/* reset the adapter */
831	aue_cfg_reset(sc);
832
833	/* set default value */
834	bzero(eaddr, sizeof(eaddr));
835
836	/* get station address from the EEPROM */
837	aue_cfg_read_eeprom(sc, eaddr, 0, 3);
838
839	mtx_unlock(&sc->sc_mtx);
840
841	ifp = if_alloc(IFT_ETHER);
842
843	mtx_lock(&sc->sc_mtx);
844
845	if (ifp == NULL) {
846		printf("%s: could not if_alloc()\n",
847		    sc->sc_name);
848		goto done;
849	}
850	sc->sc_evilhack = ifp;
851
852	ifp->if_softc = sc;
853	if_initname(ifp, "aue", sc->sc_unit);
854	ifp->if_mtu = ETHERMTU;
855	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
856	ifp->if_ioctl = aue_ioctl_cb;
857	ifp->if_start = aue_start_cb;
858	ifp->if_watchdog = NULL;
859	ifp->if_init = aue_init_cb;
860	IFQ_SET_MAXLEN(&ifp->if_snd, IFQ_MAXLEN);
861	ifp->if_snd.ifq_drv_maxlen = IFQ_MAXLEN;
862	IFQ_SET_READY(&ifp->if_snd);
863
864	/*
865	 * XXX need Giant when accessing the device structures !
866	 */
867
868	mtx_unlock(&sc->sc_mtx);
869
870	mtx_lock(&Giant);
871
872	error = mii_phy_probe(sc->sc_dev, &sc->sc_miibus,
873	    &aue_ifmedia_upd_cb,
874	    &aue_ifmedia_sts_cb);
875
876	mtx_unlock(&Giant);
877
878	mtx_lock(&sc->sc_mtx);
879
880	/*
881	 * Do MII setup.
882	 * NOTE: Doing this causes child devices to be attached to us,
883	 * which we would normally disconnect at in the detach routine
884	 * using device_delete_child(). However the USB code is set up
885	 * such that when this driver is removed, all children devices
886	 * are removed as well. In effect, the USB code ends up detaching
887	 * all of our children for us, so we don't have to do is ourselves
888	 * in aue_detach(). It's important to point this out since if
889	 * we *do* try to detach the child devices ourselves, we will
890	 * end up getting the children deleted twice, which will crash
891	 * the system.
892	 */
893	if (error) {
894		printf("%s: MII without any PHY!\n",
895		    sc->sc_name);
896		if_free(ifp);
897		goto done;
898	}
899	sc->sc_ifp = ifp;
900
901	mtx_unlock(&sc->sc_mtx);
902
903	/*
904	 * Call MI attach routine.
905	 */
906	ether_ifattach(ifp, eaddr);
907
908	mtx_lock(&sc->sc_mtx);
909
910done:
911	return;
912}
913
914static int
915aue_detach(device_t dev)
916{
917	struct aue_softc *sc = device_get_softc(dev);
918	struct ifnet *ifp;
919
920	usb2_config_td_drain(&sc->sc_config_td);
921
922	mtx_lock(&sc->sc_mtx);
923
924	usb2_callout_stop(&sc->sc_watchdog);
925
926	aue_cfg_pre_stop(sc, NULL, 0);
927
928	ifp = sc->sc_ifp;
929
930	mtx_unlock(&sc->sc_mtx);
931
932	/* stop all USB transfers first */
933	usb2_transfer_unsetup(sc->sc_xfer, AUE_ENDPT_MAX);
934
935	/* get rid of any late children */
936	bus_generic_detach(dev);
937
938	if (ifp) {
939		ether_ifdetach(ifp);
940		if_free(ifp);
941	}
942	usb2_config_td_unsetup(&sc->sc_config_td);
943
944	usb2_callout_drain(&sc->sc_watchdog);
945
946	mtx_destroy(&sc->sc_mtx);
947
948	return (0);
949}
950
951static void
952aue_intr_clear_stall_callback(struct usb2_xfer *xfer)
953{
954	struct aue_softc *sc = xfer->priv_sc;
955	struct usb2_xfer *xfer_other = sc->sc_xfer[4];
956
957	if (usb2_clear_stall_callback(xfer, xfer_other)) {
958		DPRINTF("stall cleared\n");
959		sc->sc_flags &= ~AUE_FLAG_INTR_STALL;
960		usb2_transfer_start(xfer_other);
961	}
962	return;
963}
964
965static void
966aue_intr_callback(struct usb2_xfer *xfer)
967{
968	struct aue_softc *sc = xfer->priv_sc;
969	struct ifnet *ifp = sc->sc_ifp;
970	struct aue_intrpkt pkt;
971
972	switch (USB_GET_STATE(xfer)) {
973	case USB_ST_TRANSFERRED:
974
975		if (ifp && (ifp->if_drv_flags & IFF_DRV_RUNNING) &&
976		    (xfer->actlen >= sizeof(pkt))) {
977
978			usb2_copy_out(xfer->frbuffers, 0, &pkt, sizeof(pkt));
979
980			if (pkt.aue_txstat0) {
981				ifp->if_oerrors++;
982			}
983			if (pkt.aue_txstat0 & (AUE_TXSTAT0_LATECOLL &
984			    AUE_TXSTAT0_EXCESSCOLL)) {
985				ifp->if_collisions++;
986			}
987		}
988	case USB_ST_SETUP:
989		if (sc->sc_flags & AUE_FLAG_INTR_STALL) {
990			usb2_transfer_start(sc->sc_xfer[5]);
991		} else {
992			xfer->frlengths[0] = xfer->max_data_length;
993			usb2_start_hardware(xfer);
994		}
995		return;
996
997	default:			/* Error */
998		if (xfer->error != USB_ERR_CANCELLED) {
999			/* start clear stall */
1000			sc->sc_flags |= AUE_FLAG_INTR_STALL;
1001			usb2_transfer_start(sc->sc_xfer[5]);
1002		}
1003		return;
1004	}
1005}
1006
1007static void
1008aue_bulk_read_clear_stall_callback(struct usb2_xfer *xfer)
1009{
1010	struct aue_softc *sc = xfer->priv_sc;
1011	struct usb2_xfer *xfer_other = sc->sc_xfer[1];
1012
1013	if (usb2_clear_stall_callback(xfer, xfer_other)) {
1014		DPRINTF("stall cleared\n");
1015		sc->sc_flags &= ~AUE_FLAG_READ_STALL;
1016		usb2_transfer_start(xfer_other);
1017	}
1018	return;
1019}
1020
1021static void
1022aue_bulk_read_callback(struct usb2_xfer *xfer)
1023{
1024	struct aue_softc *sc = xfer->priv_sc;
1025	struct ifnet *ifp = sc->sc_ifp;
1026	struct mbuf *m = NULL;
1027
1028	switch (USB_GET_STATE(xfer)) {
1029	case USB_ST_TRANSFERRED:
1030		DPRINTFN(11, "received %d bytes\n", xfer->actlen);
1031
1032		if (sc->sc_flags & AUE_FLAG_VER_2) {
1033
1034			if (xfer->actlen == 0) {
1035				ifp->if_ierrors++;
1036				goto tr_setup;
1037			}
1038		} else {
1039
1040			if (xfer->actlen <= (4 + ETHER_CRC_LEN)) {
1041				ifp->if_ierrors++;
1042				goto tr_setup;
1043			}
1044			usb2_copy_out(xfer->frbuffers, xfer->actlen - 4, &sc->sc_rxpkt,
1045			    sizeof(sc->sc_rxpkt));
1046
1047			/*
1048			 * turn off all the non-error bits in the rx status
1049			 * word:
1050			 */
1051			sc->sc_rxpkt.aue_rxstat &= AUE_RXSTAT_MASK;
1052
1053			if (sc->sc_rxpkt.aue_rxstat) {
1054				ifp->if_ierrors++;
1055				goto tr_setup;
1056			}
1057			/* No errors; receive the packet. */
1058			xfer->actlen -= (4 + ETHER_CRC_LEN);
1059		}
1060
1061		m = usb2_ether_get_mbuf();
1062
1063		if (m == NULL) {
1064			ifp->if_ierrors++;
1065			goto tr_setup;
1066		}
1067		xfer->actlen = min(xfer->actlen, m->m_len);
1068
1069		usb2_copy_out(xfer->frbuffers, 0, m->m_data, xfer->actlen);
1070
1071		ifp->if_ipackets++;
1072		m->m_pkthdr.rcvif = ifp;
1073		m->m_pkthdr.len = m->m_len = xfer->actlen;
1074
1075	case USB_ST_SETUP:
1076tr_setup:
1077
1078		if (sc->sc_flags & AUE_FLAG_READ_STALL) {
1079			usb2_transfer_start(sc->sc_xfer[3]);
1080		} else {
1081			xfer->frlengths[0] = xfer->max_data_length;
1082			usb2_start_hardware(xfer);
1083		}
1084
1085		/*
1086		 * At the end of a USB callback it is always safe to unlock
1087		 * the private mutex of a device! That is why we do the
1088		 * "if_input" here, and not some lines up!
1089		 */
1090		if (m) {
1091			mtx_unlock(&sc->sc_mtx);
1092			(ifp->if_input) (ifp, m);
1093			mtx_lock(&sc->sc_mtx);
1094		}
1095		return;
1096
1097	default:			/* Error */
1098		if (xfer->error != USB_ERR_CANCELLED) {
1099			/* try to clear stall first */
1100			sc->sc_flags |= AUE_FLAG_READ_STALL;
1101			usb2_transfer_start(sc->sc_xfer[3]);
1102		}
1103		DPRINTF("bulk read error, %s\n",
1104		    usb2_errstr(xfer->error));
1105		return;
1106
1107	}
1108}
1109
1110static void
1111aue_bulk_write_clear_stall_callback(struct usb2_xfer *xfer)
1112{
1113	struct aue_softc *sc = xfer->priv_sc;
1114	struct usb2_xfer *xfer_other = sc->sc_xfer[0];
1115
1116	if (usb2_clear_stall_callback(xfer, xfer_other)) {
1117		DPRINTF("stall cleared\n");
1118		sc->sc_flags &= ~AUE_FLAG_WRITE_STALL;
1119		usb2_transfer_start(xfer_other);
1120	}
1121	return;
1122}
1123
1124static void
1125aue_bulk_write_callback(struct usb2_xfer *xfer)
1126{
1127	struct aue_softc *sc = xfer->priv_sc;
1128	struct ifnet *ifp = sc->sc_ifp;
1129	struct mbuf *m;
1130	uint8_t buf[2];
1131
1132	switch (USB_GET_STATE(xfer)) {
1133	case USB_ST_TRANSFERRED:
1134		DPRINTFN(11, "transfer of %d bytes complete\n", xfer->actlen);
1135
1136		ifp->if_opackets++;
1137
1138	case USB_ST_SETUP:
1139
1140		if (sc->sc_flags & AUE_FLAG_WRITE_STALL) {
1141			usb2_transfer_start(sc->sc_xfer[2]);
1142			goto done;
1143		}
1144		if (sc->sc_flags & AUE_FLAG_WAIT_LINK) {
1145			/*
1146			 * don't send anything if there is no link !
1147			 */
1148			goto done;
1149		}
1150		IFQ_DRV_DEQUEUE(&ifp->if_snd, m);
1151
1152		if (m == NULL) {
1153			goto done;
1154		}
1155		if (m->m_pkthdr.len > MCLBYTES) {
1156			m->m_pkthdr.len = MCLBYTES;
1157		}
1158		if (sc->sc_flags & AUE_FLAG_VER_2) {
1159
1160			xfer->frlengths[0] = m->m_pkthdr.len;
1161
1162			usb2_m_copy_in(xfer->frbuffers, 0,
1163			    m, 0, m->m_pkthdr.len);
1164
1165		} else {
1166
1167			xfer->frlengths[0] = (m->m_pkthdr.len + 2);
1168
1169			/*
1170		         * The ADMtek documentation says that the packet length is
1171		         * supposed to be specified in the first two bytes of the
1172		         * transfer, however it actually seems to ignore this info
1173		         * and base the frame size on the bulk transfer length.
1174		         */
1175			buf[0] = (uint8_t)(m->m_pkthdr.len);
1176			buf[1] = (uint8_t)(m->m_pkthdr.len >> 8);
1177
1178			usb2_copy_in(xfer->frbuffers, 0, buf, 2);
1179
1180			usb2_m_copy_in(xfer->frbuffers, 2,
1181			    m, 0, m->m_pkthdr.len);
1182		}
1183
1184		/*
1185		 * if there's a BPF listener, bounce a copy
1186		 * of this frame to him:
1187		 */
1188		BPF_MTAP(ifp, m);
1189
1190		m_freem(m);
1191
1192		usb2_start_hardware(xfer);
1193
1194done:
1195		return;
1196
1197	default:			/* Error */
1198		DPRINTFN(11, "transfer error, %s\n",
1199		    usb2_errstr(xfer->error));
1200
1201		if (xfer->error != USB_ERR_CANCELLED) {
1202			/* try to clear stall first */
1203			sc->sc_flags |= AUE_FLAG_WRITE_STALL;
1204			usb2_transfer_start(sc->sc_xfer[2]);
1205		}
1206		ifp->if_oerrors++;
1207		return;
1208
1209	}
1210}
1211
1212#define	AUE_BITS 6
1213
1214static void
1215aue_mchash(struct usb2_config_td_cc *cc, const uint8_t *ptr)
1216{
1217	uint8_t h;
1218
1219	h = ether_crc32_le(ptr, ETHER_ADDR_LEN) &
1220	    ((1 << AUE_BITS) - 1);
1221	cc->if_hash[(h >> 3)] |= (1 << (h & 7));
1222	return;
1223}
1224
1225static void
1226aue_config_copy(struct aue_softc *sc,
1227    struct usb2_config_td_cc *cc, uint16_t refcount)
1228{
1229	bzero(cc, sizeof(*cc));
1230	usb2_ether_cc(sc->sc_ifp, &aue_mchash, cc);
1231	return;
1232}
1233
1234static void
1235aue_cfg_tick(struct aue_softc *sc,
1236    struct usb2_config_td_cc *cc, uint16_t refcount)
1237{
1238	struct ifnet *ifp = sc->sc_ifp;
1239	struct mii_data *mii = GET_MII(sc);
1240
1241	if ((ifp == NULL) ||
1242	    (mii == NULL)) {
1243		/* not ready */
1244		return;
1245	}
1246	mii_tick(mii);
1247
1248	mii_pollstat(mii);
1249
1250	if ((sc->sc_flags & AUE_FLAG_WAIT_LINK) &&
1251	    (mii->mii_media_status & IFM_ACTIVE) &&
1252	    (IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE)) {
1253		sc->sc_flags &= ~AUE_FLAG_WAIT_LINK;
1254	}
1255	sc->sc_media_active = mii->mii_media_active;
1256	sc->sc_media_status = mii->mii_media_status;
1257
1258	/* start stopped transfers, if any */
1259
1260	aue_start_transfers(sc);
1261
1262	return;
1263}
1264
1265static void
1266aue_start_cb(struct ifnet *ifp)
1267{
1268	struct aue_softc *sc = ifp->if_softc;
1269
1270	mtx_lock(&sc->sc_mtx);
1271
1272	aue_start_transfers(sc);
1273
1274	mtx_unlock(&sc->sc_mtx);
1275
1276	return;
1277}
1278
1279static void
1280aue_init_cb(void *arg)
1281{
1282	struct aue_softc *sc = arg;
1283
1284	mtx_lock(&sc->sc_mtx);
1285	usb2_config_td_queue_command
1286	    (&sc->sc_config_td, &aue_cfg_pre_init, &aue_cfg_init, 0, 0);
1287	mtx_unlock(&sc->sc_mtx);
1288
1289	return;
1290}
1291
1292static void
1293aue_start_transfers(struct aue_softc *sc)
1294{
1295	if ((sc->sc_flags & AUE_FLAG_LL_READY) &&
1296	    (sc->sc_flags & AUE_FLAG_HL_READY)) {
1297
1298		/*
1299		 * start the USB transfers, if not already started:
1300		 */
1301		usb2_transfer_start(sc->sc_xfer[4]);
1302		usb2_transfer_start(sc->sc_xfer[1]);
1303		usb2_transfer_start(sc->sc_xfer[0]);
1304	}
1305	return;
1306}
1307
1308static void
1309aue_cfg_pre_init(struct aue_softc *sc,
1310    struct usb2_config_td_cc *cc, uint16_t refcount)
1311{
1312	struct ifnet *ifp = sc->sc_ifp;
1313
1314	/* immediate configuration */
1315
1316	aue_cfg_pre_stop(sc, cc, 0);
1317
1318	ifp->if_drv_flags |= IFF_DRV_RUNNING;
1319
1320	sc->sc_flags |= AUE_FLAG_HL_READY;
1321	return;
1322}
1323
1324static void
1325aue_cfg_init(struct aue_softc *sc,
1326    struct usb2_config_td_cc *cc, uint16_t refcount)
1327{
1328	struct mii_data *mii = GET_MII(sc);
1329	uint8_t i;
1330
1331	/*
1332	 * Cancel pending I/O
1333	 */
1334	aue_cfg_stop(sc, cc, 0);
1335
1336	/* Set MAC address */
1337	for (i = 0; i < ETHER_ADDR_LEN; i++) {
1338		aue_cfg_csr_write_1(sc, AUE_PAR0 + i, cc->if_lladdr[i]);
1339	}
1340
1341	/* update promiscuous setting */
1342	aue_cfg_promisc_upd(sc, cc, 0);
1343
1344	/* load the multicast filter */
1345	aue_cfg_setmulti(sc, cc, 0);
1346
1347	/* enable RX and TX */
1348	aue_cfg_csr_write_1(sc, AUE_CTL0,
1349	    (AUE_CTL0_RXSTAT_APPEND |
1350	    AUE_CTL0_RX_ENB));
1351
1352	AUE_CFG_SETBIT(sc, AUE_CTL0, AUE_CTL0_TX_ENB);
1353	AUE_CFG_SETBIT(sc, AUE_CTL2, AUE_CTL2_EP3_CLR);
1354
1355	mii_mediachg(mii);
1356
1357	sc->sc_flags |= (AUE_FLAG_READ_STALL |
1358	    AUE_FLAG_WRITE_STALL |
1359	    AUE_FLAG_LL_READY);
1360
1361	aue_start_transfers(sc);
1362	return;
1363}
1364
1365static void
1366aue_cfg_promisc_upd(struct aue_softc *sc,
1367    struct usb2_config_td_cc *cc, uint16_t refcount)
1368{
1369	/* if we want promiscuous mode, set the allframes bit: */
1370	if (cc->if_flags & IFF_PROMISC) {
1371		AUE_CFG_SETBIT(sc, AUE_CTL2, AUE_CTL2_RX_PROMISC);
1372	} else {
1373		AUE_CFG_CLRBIT(sc, AUE_CTL2, AUE_CTL2_RX_PROMISC);
1374	}
1375	return;
1376}
1377
1378/*
1379 * Set media options.
1380 */
1381static int
1382aue_ifmedia_upd_cb(struct ifnet *ifp)
1383{
1384	struct aue_softc *sc = ifp->if_softc;
1385
1386	mtx_lock(&sc->sc_mtx);
1387	usb2_config_td_queue_command
1388	    (&sc->sc_config_td, NULL, &aue_cfg_ifmedia_upd, 0, 0);
1389	mtx_unlock(&sc->sc_mtx);
1390
1391	return (0);
1392}
1393
1394static void
1395aue_cfg_ifmedia_upd(struct aue_softc *sc,
1396    struct usb2_config_td_cc *cc, uint16_t refcount)
1397{
1398	struct ifnet *ifp = sc->sc_ifp;
1399	struct mii_data *mii = GET_MII(sc);
1400
1401	if ((ifp == NULL) ||
1402	    (mii == NULL)) {
1403		/* not ready */
1404		return;
1405	}
1406	sc->sc_flags |= AUE_FLAG_WAIT_LINK;
1407
1408	if (mii->mii_instance) {
1409		struct mii_softc *miisc;
1410
1411		LIST_FOREACH(miisc, &mii->mii_phys, mii_list) {
1412			mii_phy_reset(miisc);
1413		}
1414	}
1415	mii_mediachg(mii);
1416
1417	return;
1418}
1419
1420/*
1421 * Report current media status.
1422 */
1423static void
1424aue_ifmedia_sts_cb(struct ifnet *ifp, struct ifmediareq *ifmr)
1425{
1426	struct aue_softc *sc = ifp->if_softc;
1427
1428	mtx_lock(&sc->sc_mtx);
1429
1430	ifmr->ifm_active = sc->sc_media_active;
1431	ifmr->ifm_status = sc->sc_media_status;
1432
1433	mtx_unlock(&sc->sc_mtx);
1434	return;
1435}
1436
1437static int
1438aue_ioctl_cb(struct ifnet *ifp, u_long command, caddr_t data)
1439{
1440	struct aue_softc *sc = ifp->if_softc;
1441	struct mii_data *mii;
1442	int error = 0;
1443
1444	switch (command) {
1445	case SIOCSIFFLAGS:
1446		mtx_lock(&sc->sc_mtx);
1447		if (ifp->if_flags & IFF_UP) {
1448			if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
1449				usb2_config_td_queue_command
1450				    (&sc->sc_config_td, &aue_config_copy,
1451				    &aue_cfg_promisc_upd, 0, 0);
1452			} else {
1453				usb2_config_td_queue_command
1454				    (&sc->sc_config_td, &aue_cfg_pre_init,
1455				    &aue_cfg_init, 0, 0);
1456			}
1457		} else {
1458			if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
1459				usb2_config_td_queue_command
1460				    (&sc->sc_config_td, &aue_cfg_pre_stop,
1461				    &aue_cfg_stop, 0, 0);
1462			}
1463		}
1464		mtx_unlock(&sc->sc_mtx);
1465		break;
1466
1467	case SIOCADDMULTI:
1468	case SIOCDELMULTI:
1469		mtx_lock(&sc->sc_mtx);
1470		usb2_config_td_queue_command
1471		    (&sc->sc_config_td, &aue_config_copy,
1472		    &aue_cfg_setmulti, 0, 0);
1473		mtx_unlock(&sc->sc_mtx);
1474		break;
1475
1476	case SIOCGIFMEDIA:
1477	case SIOCSIFMEDIA:
1478		mii = GET_MII(sc);
1479		if (mii == NULL) {
1480			error = EINVAL;
1481		} else {
1482			error = ifmedia_ioctl
1483			    (ifp, (void *)data, &mii->mii_media, command);
1484		}
1485		break;
1486
1487	default:
1488		error = ether_ioctl(ifp, command, data);
1489		break;
1490	}
1491	return (error);
1492}
1493
1494static void
1495aue_watchdog(void *arg)
1496{
1497	struct aue_softc *sc = arg;
1498
1499	mtx_assert(&sc->sc_mtx, MA_OWNED);
1500
1501	usb2_config_td_queue_command
1502	    (&sc->sc_config_td, NULL, &aue_cfg_tick, 0, 0);
1503
1504	usb2_callout_reset(&sc->sc_watchdog,
1505	    hz, &aue_watchdog, sc);
1506
1507	mtx_unlock(&sc->sc_mtx);
1508	return;
1509}
1510
1511/*
1512 * Stop the adapter and free any mbufs allocated to the
1513 * RX and TX lists.
1514 *
1515 * NOTE: can be called when "ifp" is NULL
1516 */
1517static void
1518aue_cfg_pre_stop(struct aue_softc *sc,
1519    struct usb2_config_td_cc *cc, uint16_t refcount)
1520{
1521	struct ifnet *ifp = sc->sc_ifp;
1522
1523	if (cc) {
1524		/* copy the needed configuration */
1525		aue_config_copy(sc, cc, refcount);
1526	}
1527	/* immediate configuration */
1528
1529	if (ifp) {
1530		/* clear flags */
1531		ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1532	}
1533	sc->sc_flags &= ~(AUE_FLAG_HL_READY |
1534	    AUE_FLAG_LL_READY);
1535
1536	sc->sc_flags |= AUE_FLAG_WAIT_LINK;
1537
1538	/*
1539	 * stop all the transfers, if not already stopped:
1540	 */
1541	usb2_transfer_stop(sc->sc_xfer[0]);
1542	usb2_transfer_stop(sc->sc_xfer[1]);
1543	usb2_transfer_stop(sc->sc_xfer[2]);
1544	usb2_transfer_stop(sc->sc_xfer[3]);
1545	usb2_transfer_stop(sc->sc_xfer[4]);
1546	usb2_transfer_stop(sc->sc_xfer[5]);
1547	return;
1548}
1549
1550static void
1551aue_cfg_stop(struct aue_softc *sc,
1552    struct usb2_config_td_cc *cc, uint16_t refcount)
1553{
1554	aue_cfg_csr_write_1(sc, AUE_CTL0, 0);
1555	aue_cfg_csr_write_1(sc, AUE_CTL1, 0);
1556	aue_cfg_reset(sc);
1557	return;
1558}
1559
1560/*
1561 * Stop all chip I/O so that the kernel's probe routines don't
1562 * get confused by errant DMAs when rebooting.
1563 */
1564static int
1565aue_shutdown(device_t dev)
1566{
1567	struct aue_softc *sc = device_get_softc(dev);
1568
1569	mtx_lock(&sc->sc_mtx);
1570
1571	usb2_config_td_queue_command
1572	    (&sc->sc_config_td, &aue_cfg_pre_stop,
1573	    &aue_cfg_stop, 0, 0);
1574
1575	mtx_unlock(&sc->sc_mtx);
1576
1577	return (0);
1578}
1579