if_aue.c revision 187378
1178479Sjb/*-
2178479Sjb * Copyright (c) 1997, 1998, 1999, 2000
3178479Sjb *	Bill Paul <wpaul@ee.columbia.edu>.  All rights reserved.
4178479Sjb *
5178479Sjb * Redistribution and use in source and binary forms, with or without
6178479Sjb * modification, are permitted provided that the following conditions
7178479Sjb * are met:
8178479Sjb * 1. Redistributions of source code must retain the above copyright
9178479Sjb *    notice, this list of conditions and the following disclaimer.
10178479Sjb * 2. Redistributions in binary form must reproduce the above copyright
11178479Sjb *    notice, this list of conditions and the following disclaimer in the
12178479Sjb *    documentation and/or other materials provided with the distribution.
13178479Sjb * 3. All advertising materials mentioning features or use of this software
14178479Sjb *    must display the following acknowledgement:
15178479Sjb *	This product includes software developed by Bill Paul.
16178479Sjb * 4. Neither the name of the author nor the names of any co-contributors
17178479Sjb *    may be used to endorse or promote products derived from this software
18178479Sjb *    without specific prior written permission.
19178479Sjb *
20178479Sjb * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
21178479Sjb * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22178479Sjb * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23178479Sjb * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
24178479Sjb * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25178479Sjb * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26178479Sjb * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27178479Sjb * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28178479Sjb * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29178479Sjb * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
30178479Sjb * THE POSSIBILITY OF SUCH DAMAGE.
31178479Sjb */
32178479Sjb
33178479Sjb#include <sys/cdefs.h>
34178479Sjb__FBSDID("$FreeBSD: head/sys/dev/usb2/ethernet/if_aue2.c 187378 2009-01-18 05:35:58Z thompsa $");
35178479Sjb
36178479Sjb/*
37178479Sjb * ADMtek AN986 Pegasus and AN8511 Pegasus II USB to ethernet driver.
38178479Sjb * Datasheet is available from http://www.admtek.com.tw.
39178479Sjb *
40178479Sjb * Written by Bill Paul <wpaul@ee.columbia.edu>
41178479Sjb * Electrical Engineering Department
42178479Sjb * Columbia University, New York City
43178479Sjb */
44178479Sjb
45178479Sjb/*
46178479Sjb * The Pegasus chip uses four USB "endpoints" to provide 10/100 ethernet
47178479Sjb * support: the control endpoint for reading/writing registers, burst
48178479Sjb * read endpoint for packet reception, burst write for packet transmission
49178479Sjb * and one for "interrupts." The chip uses the same RX filter scheme
50178479Sjb * as the other ADMtek ethernet parts: one perfect filter entry for the
51178479Sjb * the station address and a 64-bit multicast hash table. The chip supports
52178479Sjb * both MII and HomePNA attachments.
53178479Sjb *
54178479Sjb * Since the maximum data transfer speed of USB is supposed to be 12Mbps,
55178479Sjb * you're never really going to get 100Mbps speeds from this device. I
56178479Sjb * think the idea is to allow the device to connect to 10 or 100Mbps
57178479Sjb * networks, not necessarily to provide 100Mbps performance. Also, since
58178479Sjb * the controller uses an external PHY chip, it's possible that board
59178479Sjb * designers might simply choose a 10Mbps PHY.
60178479Sjb *
61178479Sjb * Registers are accessed using usb2_do_request(). Packet transfers are
62178479Sjb * done using usb2_transfer() and friends.
63178479Sjb */
64178479Sjb
65178479Sjb/*
66178479Sjb * NOTE: all function names beginning like "aue_cfg_" can only
67178479Sjb * be called from within the config thread function !
68178479Sjb */
69178479Sjb
70178479Sjb#include <dev/usb2/include/usb2_devid.h>
71178479Sjb#include <dev/usb2/include/usb2_standard.h>
72178479Sjb#include <dev/usb2/include/usb2_mfunc.h>
73178479Sjb#include <dev/usb2/include/usb2_error.h>
74178479Sjb
75178479Sjb#define	usb2_config_td_cc usb2_ether_cc
76178479Sjb#define	usb2_config_td_softc aue_softc
77178479Sjb
78178479Sjb#define	USB_DEBUG_VAR aue_debug
79178479Sjb
80178479Sjb#include <dev/usb2/core/usb2_core.h>
81178479Sjb#include <dev/usb2/core/usb2_lookup.h>
82178479Sjb#include <dev/usb2/core/usb2_process.h>
83178479Sjb#include <dev/usb2/core/usb2_config_td.h>
84178479Sjb#include <dev/usb2/core/usb2_debug.h>
85178551Sjb#include <dev/usb2/core/usb2_request.h>
86178479Sjb#include <dev/usb2/core/usb2_busdma.h>
87178479Sjb#include <dev/usb2/core/usb2_util.h>
88178479Sjb
89178479Sjb#include <dev/usb2/ethernet/usb2_ethernet.h>
90178479Sjb#include <dev/usb2/ethernet/if_auereg.h>
91178479Sjb
92178479SjbMODULE_DEPEND(aue, usb2_ethernet, 1, 1, 1);
93178479SjbMODULE_DEPEND(aue, usb2_core, 1, 1, 1);
94178479SjbMODULE_DEPEND(aue, ether, 1, 1, 1);
95178551SjbMODULE_DEPEND(aue, miibus, 1, 1, 1);
96178551Sjb
97178551Sjb#if USB_DEBUG
98178551Sjbstatic int aue_debug = 0;
99178551Sjb
100178551SjbSYSCTL_NODE(_hw_usb2, OID_AUTO, aue, CTLFLAG_RW, 0, "USB aue");
101178551SjbSYSCTL_INT(_hw_usb2_aue, OID_AUTO, debug, CTLFLAG_RW, &aue_debug, 0,
102178551Sjb    "Debug level");
103178551Sjb#endif
104178479Sjb
105178551Sjb/*
106178551Sjb * Various supported device vendors/products.
107178551Sjb */
108178551Sjbstatic const struct usb2_device_id aue_devs[] = {
109178551Sjb	{USB_VPI(USB_VENDOR_3COM, USB_PRODUCT_3COM_3C460B, AUE_FLAG_PII)},
110178551Sjb	{USB_VPI(USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_DSB650TX_PNA, 0)},
111178551Sjb	{USB_VPI(USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_UFE1000, AUE_FLAG_LSYS)},
112178551Sjb	{USB_VPI(USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_XX10, 0)},
113178479Sjb	{USB_VPI(USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_XX1, AUE_FLAG_PNA | AUE_FLAG_PII)},
114178479Sjb	{USB_VPI(USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_XX2, AUE_FLAG_PII)},
115178479Sjb	{USB_VPI(USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_XX4, AUE_FLAG_PNA)},
116178479Sjb	{USB_VPI(USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_XX5, AUE_FLAG_PNA)},
117178479Sjb	{USB_VPI(USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_XX6, AUE_FLAG_PII)},
118178479Sjb	{USB_VPI(USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_XX7, AUE_FLAG_PII)},
119178479Sjb	{USB_VPI(USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_XX8, AUE_FLAG_PII)},
120178479Sjb	{USB_VPI(USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_XX9, AUE_FLAG_PNA)},
121178479Sjb	{USB_VPI(USB_VENDOR_ACCTON, USB_PRODUCT_ACCTON_SS1001, AUE_FLAG_PII)},
122178479Sjb	{USB_VPI(USB_VENDOR_ACCTON, USB_PRODUCT_ACCTON_USB320_EC, 0)},
123178479Sjb	{USB_VPI(USB_VENDOR_ADMTEK, USB_PRODUCT_ADMTEK_PEGASUSII_2, AUE_FLAG_PII)},
124178479Sjb	{USB_VPI(USB_VENDOR_ADMTEK, USB_PRODUCT_ADMTEK_PEGASUSII_3, AUE_FLAG_PII)},
125178479Sjb	{USB_VPI(USB_VENDOR_ADMTEK, USB_PRODUCT_ADMTEK_PEGASUSII_4, AUE_FLAG_PII)},
126178479Sjb	{USB_VPI(USB_VENDOR_ADMTEK, USB_PRODUCT_ADMTEK_PEGASUSII, AUE_FLAG_PII)},
127178479Sjb	{USB_VPI(USB_VENDOR_ADMTEK, USB_PRODUCT_ADMTEK_PEGASUS, AUE_FLAG_PNA | AUE_FLAG_DUAL_PHY)},
128178479Sjb	{USB_VPI(USB_VENDOR_AEI, USB_PRODUCT_AEI_FASTETHERNET, AUE_FLAG_PII)},
129178479Sjb	{USB_VPI(USB_VENDOR_ALLIEDTELESYN, USB_PRODUCT_ALLIEDTELESYN_ATUSB100, AUE_FLAG_PII)},
130178479Sjb	{USB_VPI(USB_VENDOR_ATEN, USB_PRODUCT_ATEN_UC110T, AUE_FLAG_PII)},
131178479Sjb	{USB_VPI(USB_VENDOR_BELKIN, USB_PRODUCT_BELKIN_USB2LAN, AUE_FLAG_PII)},
132178479Sjb	{USB_VPI(USB_VENDOR_BILLIONTON, USB_PRODUCT_BILLIONTON_USB100, 0)},
133178479Sjb	{USB_VPI(USB_VENDOR_BILLIONTON, USB_PRODUCT_BILLIONTON_USBE100, AUE_FLAG_PII)},
134178479Sjb	{USB_VPI(USB_VENDOR_BILLIONTON, USB_PRODUCT_BILLIONTON_USBEL100, 0)},
135178479Sjb	{USB_VPI(USB_VENDOR_BILLIONTON, USB_PRODUCT_BILLIONTON_USBLP100, AUE_FLAG_PNA)},
136178479Sjb	{USB_VPI(USB_VENDOR_COREGA, USB_PRODUCT_COREGA_FETHER_USB_TXS, AUE_FLAG_PII)},
137178479Sjb	{USB_VPI(USB_VENDOR_COREGA, USB_PRODUCT_COREGA_FETHER_USB_TX, 0)},
138178479Sjb	{USB_VPI(USB_VENDOR_DLINK, USB_PRODUCT_DLINK_DSB650TX1, AUE_FLAG_LSYS)},
139178479Sjb	{USB_VPI(USB_VENDOR_DLINK, USB_PRODUCT_DLINK_DSB650TX2, AUE_FLAG_LSYS | AUE_FLAG_PII)},
140178479Sjb	{USB_VPI(USB_VENDOR_DLINK, USB_PRODUCT_DLINK_DSB650TX3, AUE_FLAG_LSYS | AUE_FLAG_PII)},
141178479Sjb	{USB_VPI(USB_VENDOR_DLINK, USB_PRODUCT_DLINK_DSB650TX4, AUE_FLAG_LSYS | AUE_FLAG_PII)},
142178479Sjb	{USB_VPI(USB_VENDOR_DLINK, USB_PRODUCT_DLINK_DSB650TX_PNA, AUE_FLAG_PNA)},
143178479Sjb	{USB_VPI(USB_VENDOR_DLINK, USB_PRODUCT_DLINK_DSB650TX, AUE_FLAG_LSYS)},
144178479Sjb	{USB_VPI(USB_VENDOR_DLINK, USB_PRODUCT_DLINK_DSB650, AUE_FLAG_LSYS)},
145178479Sjb	{USB_VPI(USB_VENDOR_ELCON, USB_PRODUCT_ELCON_PLAN, AUE_FLAG_PNA | AUE_FLAG_PII)},
146178479Sjb	{USB_VPI(USB_VENDOR_ELECOM, USB_PRODUCT_ELECOM_LDUSB20, AUE_FLAG_PII)},
147178479Sjb	{USB_VPI(USB_VENDOR_ELECOM, USB_PRODUCT_ELECOM_LDUSBLTX, AUE_FLAG_PII)},
148178479Sjb	{USB_VPI(USB_VENDOR_ELECOM, USB_PRODUCT_ELECOM_LDUSBTX0, 0)},
149178479Sjb	{USB_VPI(USB_VENDOR_ELECOM, USB_PRODUCT_ELECOM_LDUSBTX1, AUE_FLAG_LSYS)},
150178479Sjb	{USB_VPI(USB_VENDOR_ELECOM, USB_PRODUCT_ELECOM_LDUSBTX2, 0)},
151178479Sjb	{USB_VPI(USB_VENDOR_ELECOM, USB_PRODUCT_ELECOM_LDUSBTX3, AUE_FLAG_LSYS)},
152178479Sjb	{USB_VPI(USB_VENDOR_ELSA, USB_PRODUCT_ELSA_USB2ETHERNET, 0)},
153178479Sjb	{USB_VPI(USB_VENDOR_GIGABYTE, USB_PRODUCT_GIGABYTE_GNBR402W, 0)},
154178479Sjb	{USB_VPI(USB_VENDOR_HAWKING, USB_PRODUCT_HAWKING_UF100, AUE_FLAG_PII)},
155178479Sjb	{USB_VPI(USB_VENDOR_HP, USB_PRODUCT_HP_HN210E, AUE_FLAG_PII)},
156178479Sjb	{USB_VPI(USB_VENDOR_IODATA, USB_PRODUCT_IODATA_USBETTXS, AUE_FLAG_PII)},
157178479Sjb	{USB_VPI(USB_VENDOR_IODATA, USB_PRODUCT_IODATA_USBETTX, 0)},
158178479Sjb	{USB_VPI(USB_VENDOR_KINGSTON, USB_PRODUCT_KINGSTON_KNU101TX, 0)},
159178479Sjb	{USB_VPI(USB_VENDOR_LINKSYS, USB_PRODUCT_LINKSYS_USB100H1, AUE_FLAG_LSYS | AUE_FLAG_PNA)},
160178479Sjb	{USB_VPI(USB_VENDOR_LINKSYS, USB_PRODUCT_LINKSYS_USB100TX, AUE_FLAG_LSYS)},
161178479Sjb	{USB_VPI(USB_VENDOR_LINKSYS, USB_PRODUCT_LINKSYS_USB10TA, AUE_FLAG_LSYS)},
162178479Sjb	{USB_VPI(USB_VENDOR_LINKSYS, USB_PRODUCT_LINKSYS_USB10TX1, AUE_FLAG_LSYS | AUE_FLAG_PII)},
163178479Sjb	{USB_VPI(USB_VENDOR_LINKSYS, USB_PRODUCT_LINKSYS_USB10TX2, AUE_FLAG_LSYS | AUE_FLAG_PII)},
164178479Sjb	{USB_VPI(USB_VENDOR_LINKSYS, USB_PRODUCT_LINKSYS_USB10T, AUE_FLAG_LSYS)},
165178479Sjb	{USB_VPI(USB_VENDOR_MELCO, USB_PRODUCT_MELCO_LUA2TX5, AUE_FLAG_PII)},
166178479Sjb	{USB_VPI(USB_VENDOR_MELCO, USB_PRODUCT_MELCO_LUATX1, 0)},
167178479Sjb	{USB_VPI(USB_VENDOR_MELCO, USB_PRODUCT_MELCO_LUATX5, 0)},
168178479Sjb	{USB_VPI(USB_VENDOR_MICROSOFT, USB_PRODUCT_MICROSOFT_MN110, AUE_FLAG_PII)},
169178479Sjb	{USB_VPI(USB_VENDOR_NETGEAR, USB_PRODUCT_NETGEAR_FA101, AUE_FLAG_PII)},
170178479Sjb	{USB_VPI(USB_VENDOR_SIEMENS, USB_PRODUCT_SIEMENS_SPEEDSTREAM, AUE_FLAG_PII)},
171178479Sjb	{USB_VPI(USB_VENDOR_SIIG2, USB_PRODUCT_SIIG2_USBTOETHER, AUE_FLAG_PII)},
172178479Sjb	{USB_VPI(USB_VENDOR_SMARTBRIDGES, USB_PRODUCT_SMARTBRIDGES_SMARTNIC, AUE_FLAG_PII)},
173178479Sjb	{USB_VPI(USB_VENDOR_SMC, USB_PRODUCT_SMC_2202USB, 0)},
174178479Sjb	{USB_VPI(USB_VENDOR_SMC, USB_PRODUCT_SMC_2206USB, AUE_FLAG_PII)},
175178479Sjb	{USB_VPI(USB_VENDOR_SOHOWARE, USB_PRODUCT_SOHOWARE_NUB100, 0)},
176178479Sjb	{USB_VPI(USB_VENDOR_SOHOWARE, USB_PRODUCT_SOHOWARE_NUB110, AUE_FLAG_PII)},
177178479Sjb};
178178479Sjb
179178479Sjb/* prototypes */
180178479Sjb
181178479Sjbstatic device_probe_t aue_probe;
182178479Sjbstatic device_attach_t aue_attach;
183178479Sjbstatic device_detach_t aue_detach;
184178479Sjbstatic device_shutdown_t aue_shutdown;
185178551Sjb
186178479Sjbstatic usb2_callback_t aue_intr_clear_stall_callback;
187270214Smarkjstatic usb2_callback_t aue_intr_callback;
188178479Sjbstatic usb2_callback_t aue_bulk_read_clear_stall_callback;
189178479Sjbstatic usb2_callback_t aue_bulk_read_callback;
190178479Sjbstatic usb2_callback_t aue_bulk_write_clear_stall_callback;
191178479Sjbstatic usb2_callback_t aue_bulk_write_callback;
192178479Sjb
193178479Sjbstatic void	aue_cfg_do_request(struct aue_softc *,
194178479Sjb		    struct usb2_device_request *, void *);
195178479Sjbstatic uint8_t	aue_cfg_csr_read_1(struct aue_softc *, uint16_t);
196178479Sjbstatic uint16_t	aue_cfg_csr_read_2(struct aue_softc *, uint16_t);
197178479Sjbstatic void	aue_cfg_csr_write_1(struct aue_softc *, uint16_t, uint8_t);
198178479Sjbstatic void	aue_cfg_csr_write_2(struct aue_softc *, uint16_t, uint16_t);
199178479Sjbstatic void	aue_cfg_eeprom_getword(struct aue_softc *, uint8_t, uint8_t *);
200178479Sjbstatic void	aue_cfg_read_eeprom(struct aue_softc *, uint8_t *, uint16_t,
201178479Sjb		    uint16_t);
202178479Sjb
203178479Sjbstatic miibus_readreg_t aue_cfg_miibus_readreg;
204178479Sjbstatic miibus_writereg_t aue_cfg_miibus_writereg;
205178479Sjbstatic miibus_statchg_t aue_cfg_miibus_statchg;
206178479Sjb
207178551Sjbstatic usb2_config_td_command_t aue_cfg_setmulti;
208178551Sjbstatic usb2_config_td_command_t aue_cfg_first_time_setup;
209270214Smarkjstatic usb2_config_td_command_t aue_config_copy;
210270214Smarkjstatic usb2_config_td_command_t aue_cfg_tick;
211178479Sjbstatic usb2_config_td_command_t aue_cfg_pre_init;
212178479Sjbstatic usb2_config_td_command_t aue_cfg_init;
213270214Smarkjstatic usb2_config_td_command_t aue_cfg_promisc_upd;
214270214Smarkjstatic usb2_config_td_command_t aue_cfg_ifmedia_upd;
215178479Sjbstatic usb2_config_td_command_t aue_cfg_pre_stop;
216178479Sjbstatic usb2_config_td_command_t aue_cfg_stop;
217178479Sjb
218178479Sjbstatic void	aue_cfg_reset_pegasus_II(struct aue_softc *);
219178479Sjbstatic void	aue_cfg_reset(struct aue_softc *);
220178479Sjbstatic void	aue_start_cb(struct ifnet *);
221178479Sjbstatic void	aue_init_cb(void *);
222178479Sjbstatic void	aue_start_transfers(struct aue_softc *);
223178479Sjbstatic int	aue_ifmedia_upd_cb(struct ifnet *);
224178479Sjbstatic void	aue_ifmedia_sts_cb(struct ifnet *, struct ifmediareq *);
225178479Sjbstatic int	aue_ioctl_cb(struct ifnet *, u_long, caddr_t);
226178479Sjbstatic void	aue_watchdog(void *);
227178479Sjb
228178479Sjbstatic const struct usb2_config aue_config[AUE_N_TRANSFER] = {
229178479Sjb
230178479Sjb	[AUE_BULK_DT_WR] = {
231178479Sjb		.type = UE_BULK,
232178479Sjb		.endpoint = UE_ADDR_ANY,
233178479Sjb		.direction = UE_DIR_OUT,
234178479Sjb		.mh.bufsize = (MCLBYTES + 2),
235178479Sjb		.mh.flags = {.pipe_bof = 1,.force_short_xfer = 1,},
236178479Sjb		.mh.callback = &aue_bulk_write_callback,
237178479Sjb		.mh.timeout = 10000,	/* 10 seconds */
238178479Sjb	},
239178479Sjb
240178479Sjb	[AUE_BULK_DT_RD] = {
241178479Sjb		.type = UE_BULK,
242178479Sjb		.endpoint = UE_ADDR_ANY,
243178479Sjb		.direction = UE_DIR_IN,
244178479Sjb		.mh.bufsize = (MCLBYTES + 4 + ETHER_CRC_LEN),
245178479Sjb		.mh.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
246178479Sjb		.mh.callback = &aue_bulk_read_callback,
247178479Sjb	},
248178479Sjb
249178479Sjb	[AUE_BULK_CS_WR] = {
250178479Sjb		.type = UE_CONTROL,
251178479Sjb		.endpoint = 0x00,	/* Control pipe */
252178479Sjb		.direction = UE_DIR_ANY,
253178479Sjb		.mh.bufsize = sizeof(struct usb2_device_request),
254178479Sjb		.mh.flags = {},
255178479Sjb		.mh.callback = &aue_bulk_write_clear_stall_callback,
256178479Sjb		.mh.timeout = 1000,	/* 1 second */
257178479Sjb		.mh.interval = 50,	/* 50ms */
258178479Sjb	},
259178479Sjb
260178479Sjb	[AUE_BULK_CS_RD] = {
261178479Sjb		.type = UE_CONTROL,
262178479Sjb		.endpoint = 0x00,	/* Control pipe */
263178479Sjb		.direction = UE_DIR_ANY,
264178479Sjb		.mh.bufsize = sizeof(struct usb2_device_request),
265178479Sjb		.mh.flags = {},
266178479Sjb		.mh.callback = &aue_bulk_read_clear_stall_callback,
267178479Sjb		.mh.timeout = 1000,	/* 1 second */
268178479Sjb		.mh.interval = 50,	/* 50ms */
269178479Sjb	},
270178479Sjb
271178479Sjb	[AUE_INTR_DT_RD] = {
272178479Sjb		.type = UE_INTERRUPT,
273178479Sjb		.endpoint = UE_ADDR_ANY,
274178479Sjb		.direction = UE_DIR_IN,
275178479Sjb		.mh.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
276178479Sjb		.mh.bufsize = 0,	/* use wMaxPacketSize */
277178479Sjb		.mh.callback = &aue_intr_callback,
278178479Sjb	},
279178479Sjb
280178479Sjb	[AUE_INTR_CS_RD] = {
281178479Sjb		.type = UE_CONTROL,
282178479Sjb		.endpoint = 0x00,	/* Control pipe */
283178479Sjb		.direction = UE_DIR_ANY,
284178479Sjb		.mh.bufsize = sizeof(struct usb2_device_request),
285178479Sjb		.mh.flags = {},
286178479Sjb		.mh.callback = &aue_intr_clear_stall_callback,
287178479Sjb		.mh.timeout = 1000,	/* 1 second */
288178479Sjb		.mh.interval = 50,	/* 50ms */
289178479Sjb	},
290178479Sjb};
291178479Sjb
292178479Sjbstatic device_method_t aue_methods[] = {
293178479Sjb	/* Device interface */
294178479Sjb	DEVMETHOD(device_probe, aue_probe),
295178479Sjb	DEVMETHOD(device_attach, aue_attach),
296178479Sjb	DEVMETHOD(device_detach, aue_detach),
297178479Sjb	DEVMETHOD(device_shutdown, aue_shutdown),
298178479Sjb
299178479Sjb	/* bus interface */
300178479Sjb	DEVMETHOD(bus_print_child, bus_generic_print_child),
301178479Sjb	DEVMETHOD(bus_driver_added, bus_generic_driver_added),
302178479Sjb
303178479Sjb	/* MII interface */
304178479Sjb	DEVMETHOD(miibus_readreg, aue_cfg_miibus_readreg),
305178479Sjb	DEVMETHOD(miibus_writereg, aue_cfg_miibus_writereg),
306178479Sjb	DEVMETHOD(miibus_statchg, aue_cfg_miibus_statchg),
307178479Sjb
308178479Sjb	{0, 0}
309178479Sjb};
310178479Sjb
311178479Sjbstatic driver_t aue_driver = {
312178479Sjb	.name = "aue",
313178479Sjb	.methods = aue_methods,
314178479Sjb	.size = sizeof(struct aue_softc)
315178479Sjb};
316178479Sjb
317178479Sjbstatic devclass_t aue_devclass;
318178479Sjb
319178479SjbDRIVER_MODULE(aue, ushub, aue_driver, aue_devclass, NULL, 0);
320178479SjbDRIVER_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_N_TRANSFER,
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
838	ifp->if_softc = sc;
839	if_initname(ifp, "aue", sc->sc_unit);
840	ifp->if_mtu = ETHERMTU;
841	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
842	ifp->if_ioctl = aue_ioctl_cb;
843	ifp->if_start = aue_start_cb;
844	ifp->if_watchdog = NULL;
845	ifp->if_init = aue_init_cb;
846	IFQ_SET_MAXLEN(&ifp->if_snd, IFQ_MAXLEN);
847	ifp->if_snd.ifq_drv_maxlen = IFQ_MAXLEN;
848	IFQ_SET_READY(&ifp->if_snd);
849
850	/*
851	 * XXX need Giant when accessing the device structures !
852	 */
853
854	mtx_unlock(&sc->sc_mtx);
855
856	mtx_lock(&Giant);
857
858	error = mii_phy_probe(sc->sc_dev, &sc->sc_miibus,
859	    &aue_ifmedia_upd_cb,
860	    &aue_ifmedia_sts_cb);
861
862	mtx_unlock(&Giant);
863
864	mtx_lock(&sc->sc_mtx);
865
866	/*
867	 * Do MII setup.
868	 * NOTE: Doing this causes child devices to be attached to us,
869	 * which we would normally disconnect at in the detach routine
870	 * using device_delete_child(). However the USB code is set up
871	 * such that when this driver is removed, all children devices
872	 * are removed as well. In effect, the USB code ends up detaching
873	 * all of our children for us, so we don't have to do is ourselves
874	 * in aue_detach(). It's important to point this out since if
875	 * we *do* try to detach the child devices ourselves, we will
876	 * end up getting the children deleted twice, which will crash
877	 * the system.
878	 */
879	if (error) {
880		printf("%s: MII without any PHY!\n",
881		    sc->sc_name);
882		if_free(ifp);
883		goto done;
884	}
885	sc->sc_ifp = ifp;
886
887	mtx_unlock(&sc->sc_mtx);
888
889	/*
890	 * Call MI attach routine.
891	 */
892	ether_ifattach(ifp, eaddr);
893
894	mtx_lock(&sc->sc_mtx);
895
896done:
897	return;
898}
899
900static int
901aue_detach(device_t dev)
902{
903	struct aue_softc *sc = device_get_softc(dev);
904	struct ifnet *ifp;
905
906	usb2_config_td_drain(&sc->sc_config_td);
907
908	mtx_lock(&sc->sc_mtx);
909
910	usb2_callout_stop(&sc->sc_watchdog);
911
912	aue_cfg_pre_stop(sc, NULL, 0);
913
914	ifp = sc->sc_ifp;
915
916	mtx_unlock(&sc->sc_mtx);
917
918	/* stop all USB transfers first */
919	usb2_transfer_unsetup(sc->sc_xfer, AUE_N_TRANSFER);
920
921	/* get rid of any late children */
922	bus_generic_detach(dev);
923
924	if (ifp) {
925		ether_ifdetach(ifp);
926		if_free(ifp);
927	}
928	usb2_config_td_unsetup(&sc->sc_config_td);
929
930	usb2_callout_drain(&sc->sc_watchdog);
931
932	mtx_destroy(&sc->sc_mtx);
933
934	return (0);
935}
936
937static void
938aue_intr_clear_stall_callback(struct usb2_xfer *xfer)
939{
940	struct aue_softc *sc = xfer->priv_sc;
941	struct usb2_xfer *xfer_other = sc->sc_xfer[AUE_INTR_DT_RD];
942
943	if (usb2_clear_stall_callback(xfer, xfer_other)) {
944		DPRINTF("stall cleared\n");
945		sc->sc_flags &= ~AUE_FLAG_INTR_STALL;
946		usb2_transfer_start(xfer_other);
947	}
948}
949
950static void
951aue_intr_callback(struct usb2_xfer *xfer)
952{
953	struct aue_softc *sc = xfer->priv_sc;
954	struct ifnet *ifp = sc->sc_ifp;
955	struct aue_intrpkt pkt;
956
957	switch (USB_GET_STATE(xfer)) {
958	case USB_ST_TRANSFERRED:
959
960		if (ifp && (ifp->if_drv_flags & IFF_DRV_RUNNING) &&
961		    (xfer->actlen >= sizeof(pkt))) {
962
963			usb2_copy_out(xfer->frbuffers, 0, &pkt, sizeof(pkt));
964
965			if (pkt.aue_txstat0) {
966				ifp->if_oerrors++;
967			}
968			if (pkt.aue_txstat0 & (AUE_TXSTAT0_LATECOLL &
969			    AUE_TXSTAT0_EXCESSCOLL)) {
970				ifp->if_collisions++;
971			}
972		}
973	case USB_ST_SETUP:
974		if (sc->sc_flags & AUE_FLAG_INTR_STALL) {
975			usb2_transfer_start(sc->sc_xfer[AUE_INTR_CS_RD]);
976		} else {
977			xfer->frlengths[0] = xfer->max_data_length;
978			usb2_start_hardware(xfer);
979		}
980		return;
981
982	default:			/* Error */
983		if (xfer->error != USB_ERR_CANCELLED) {
984			/* start clear stall */
985			sc->sc_flags |= AUE_FLAG_INTR_STALL;
986			usb2_transfer_start(sc->sc_xfer[AUE_INTR_CS_RD]);
987		}
988		return;
989	}
990}
991
992static void
993aue_bulk_read_clear_stall_callback(struct usb2_xfer *xfer)
994{
995	struct aue_softc *sc = xfer->priv_sc;
996	struct usb2_xfer *xfer_other = sc->sc_xfer[AUE_BULK_DT_RD];
997
998	if (usb2_clear_stall_callback(xfer, xfer_other)) {
999		DPRINTF("stall cleared\n");
1000		sc->sc_flags &= ~AUE_FLAG_READ_STALL;
1001		usb2_transfer_start(xfer_other);
1002	}
1003}
1004
1005static void
1006aue_bulk_read_callback(struct usb2_xfer *xfer)
1007{
1008	struct aue_softc *sc = xfer->priv_sc;
1009	struct ifnet *ifp = sc->sc_ifp;
1010	struct mbuf *m = NULL;
1011
1012	switch (USB_GET_STATE(xfer)) {
1013	case USB_ST_TRANSFERRED:
1014		DPRINTFN(11, "received %d bytes\n", xfer->actlen);
1015
1016		if (sc->sc_flags & AUE_FLAG_VER_2) {
1017
1018			if (xfer->actlen == 0) {
1019				ifp->if_ierrors++;
1020				goto tr_setup;
1021			}
1022		} else {
1023
1024			if (xfer->actlen <= (4 + ETHER_CRC_LEN)) {
1025				ifp->if_ierrors++;
1026				goto tr_setup;
1027			}
1028			usb2_copy_out(xfer->frbuffers, xfer->actlen - 4, &sc->sc_rxpkt,
1029			    sizeof(sc->sc_rxpkt));
1030
1031			/*
1032			 * turn off all the non-error bits in the rx status
1033			 * word:
1034			 */
1035			sc->sc_rxpkt.aue_rxstat &= AUE_RXSTAT_MASK;
1036
1037			if (sc->sc_rxpkt.aue_rxstat) {
1038				ifp->if_ierrors++;
1039				goto tr_setup;
1040			}
1041			/* No errors; receive the packet. */
1042			xfer->actlen -= (4 + ETHER_CRC_LEN);
1043		}
1044
1045		m = usb2_ether_get_mbuf();
1046
1047		if (m == NULL) {
1048			ifp->if_ierrors++;
1049			goto tr_setup;
1050		}
1051		xfer->actlen = min(xfer->actlen, m->m_len);
1052
1053		usb2_copy_out(xfer->frbuffers, 0, m->m_data, xfer->actlen);
1054
1055		ifp->if_ipackets++;
1056		m->m_pkthdr.rcvif = ifp;
1057		m->m_pkthdr.len = m->m_len = xfer->actlen;
1058
1059	case USB_ST_SETUP:
1060tr_setup:
1061
1062		if (sc->sc_flags & AUE_FLAG_READ_STALL) {
1063			usb2_transfer_start(sc->sc_xfer[AUE_BULK_CS_RD]);
1064		} else {
1065			xfer->frlengths[0] = xfer->max_data_length;
1066			usb2_start_hardware(xfer);
1067		}
1068
1069		/*
1070		 * At the end of a USB callback it is always safe to unlock
1071		 * the private mutex of a device! That is why we do the
1072		 * "if_input" here, and not some lines up!
1073		 */
1074		if (m) {
1075			mtx_unlock(&sc->sc_mtx);
1076			(ifp->if_input) (ifp, m);
1077			mtx_lock(&sc->sc_mtx);
1078		}
1079		return;
1080
1081	default:			/* Error */
1082		if (xfer->error != USB_ERR_CANCELLED) {
1083			/* try to clear stall first */
1084			sc->sc_flags |= AUE_FLAG_READ_STALL;
1085			usb2_transfer_start(sc->sc_xfer[AUE_BULK_CS_RD]);
1086		}
1087		DPRINTF("bulk read error, %s\n",
1088		    usb2_errstr(xfer->error));
1089		return;
1090
1091	}
1092}
1093
1094static void
1095aue_bulk_write_clear_stall_callback(struct usb2_xfer *xfer)
1096{
1097	struct aue_softc *sc = xfer->priv_sc;
1098	struct usb2_xfer *xfer_other = sc->sc_xfer[AUE_BULK_DT_WR];
1099
1100	if (usb2_clear_stall_callback(xfer, xfer_other)) {
1101		DPRINTF("stall cleared\n");
1102		sc->sc_flags &= ~AUE_FLAG_WRITE_STALL;
1103		usb2_transfer_start(xfer_other);
1104	}
1105}
1106
1107static void
1108aue_bulk_write_callback(struct usb2_xfer *xfer)
1109{
1110	struct aue_softc *sc = xfer->priv_sc;
1111	struct ifnet *ifp = sc->sc_ifp;
1112	struct mbuf *m;
1113	uint8_t buf[2];
1114
1115	switch (USB_GET_STATE(xfer)) {
1116	case USB_ST_TRANSFERRED:
1117		DPRINTFN(11, "transfer of %d bytes complete\n", xfer->actlen);
1118
1119		ifp->if_opackets++;
1120
1121	case USB_ST_SETUP:
1122
1123		if (sc->sc_flags & AUE_FLAG_WRITE_STALL) {
1124			usb2_transfer_start(sc->sc_xfer[AUE_BULK_CS_WR]);
1125			goto done;
1126		}
1127		if (sc->sc_flags & AUE_FLAG_WAIT_LINK) {
1128			/*
1129			 * don't send anything if there is no link !
1130			 */
1131			goto done;
1132		}
1133		IFQ_DRV_DEQUEUE(&ifp->if_snd, m);
1134
1135		if (m == NULL) {
1136			goto done;
1137		}
1138		if (m->m_pkthdr.len > MCLBYTES) {
1139			m->m_pkthdr.len = MCLBYTES;
1140		}
1141		if (sc->sc_flags & AUE_FLAG_VER_2) {
1142
1143			xfer->frlengths[0] = m->m_pkthdr.len;
1144
1145			usb2_m_copy_in(xfer->frbuffers, 0,
1146			    m, 0, m->m_pkthdr.len);
1147
1148		} else {
1149
1150			xfer->frlengths[0] = (m->m_pkthdr.len + 2);
1151
1152			/*
1153		         * The ADMtek documentation says that the packet length is
1154		         * supposed to be specified in the first two bytes of the
1155		         * transfer, however it actually seems to ignore this info
1156		         * and base the frame size on the bulk transfer length.
1157		         */
1158			buf[0] = (uint8_t)(m->m_pkthdr.len);
1159			buf[1] = (uint8_t)(m->m_pkthdr.len >> 8);
1160
1161			usb2_copy_in(xfer->frbuffers, 0, buf, 2);
1162
1163			usb2_m_copy_in(xfer->frbuffers, 2,
1164			    m, 0, m->m_pkthdr.len);
1165		}
1166
1167		/*
1168		 * if there's a BPF listener, bounce a copy
1169		 * of this frame to him:
1170		 */
1171		BPF_MTAP(ifp, m);
1172
1173		m_freem(m);
1174
1175		usb2_start_hardware(xfer);
1176
1177done:
1178		return;
1179
1180	default:			/* Error */
1181		DPRINTFN(11, "transfer error, %s\n",
1182		    usb2_errstr(xfer->error));
1183
1184		if (xfer->error != USB_ERR_CANCELLED) {
1185			/* try to clear stall first */
1186			sc->sc_flags |= AUE_FLAG_WRITE_STALL;
1187			usb2_transfer_start(sc->sc_xfer[AUE_BULK_CS_WR]);
1188		}
1189		ifp->if_oerrors++;
1190		return;
1191
1192	}
1193}
1194
1195#define	AUE_BITS 6
1196
1197static void
1198aue_mchash(struct usb2_config_td_cc *cc, const uint8_t *ptr)
1199{
1200	uint8_t h;
1201
1202	h = ether_crc32_le(ptr, ETHER_ADDR_LEN) &
1203	    ((1 << AUE_BITS) - 1);
1204	cc->if_hash[(h >> 3)] |= (1 << (h & 7));
1205}
1206
1207static void
1208aue_config_copy(struct aue_softc *sc,
1209    struct usb2_config_td_cc *cc, uint16_t refcount)
1210{
1211	bzero(cc, sizeof(*cc));
1212	usb2_ether_cc(sc->sc_ifp, &aue_mchash, cc);
1213}
1214
1215static void
1216aue_cfg_tick(struct aue_softc *sc,
1217    struct usb2_config_td_cc *cc, uint16_t refcount)
1218{
1219	struct ifnet *ifp = sc->sc_ifp;
1220	struct mii_data *mii = GET_MII(sc);
1221
1222	if ((ifp == NULL) ||
1223	    (mii == NULL)) {
1224		/* not ready */
1225		return;
1226	}
1227	mii_tick(mii);
1228
1229	mii_pollstat(mii);
1230
1231	if ((sc->sc_flags & AUE_FLAG_WAIT_LINK) &&
1232	    (mii->mii_media_status & IFM_ACTIVE) &&
1233	    (IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE)) {
1234		sc->sc_flags &= ~AUE_FLAG_WAIT_LINK;
1235	}
1236	sc->sc_media_active = mii->mii_media_active;
1237	sc->sc_media_status = mii->mii_media_status;
1238
1239	/* start stopped transfers, if any */
1240
1241	aue_start_transfers(sc);
1242}
1243
1244static void
1245aue_start_cb(struct ifnet *ifp)
1246{
1247	struct aue_softc *sc = ifp->if_softc;
1248
1249	mtx_lock(&sc->sc_mtx);
1250
1251	aue_start_transfers(sc);
1252
1253	mtx_unlock(&sc->sc_mtx);
1254}
1255
1256static void
1257aue_init_cb(void *arg)
1258{
1259	struct aue_softc *sc = arg;
1260
1261	mtx_lock(&sc->sc_mtx);
1262	usb2_config_td_queue_command
1263	    (&sc->sc_config_td, &aue_cfg_pre_init, &aue_cfg_init, 0, 0);
1264	mtx_unlock(&sc->sc_mtx);
1265}
1266
1267static void
1268aue_start_transfers(struct aue_softc *sc)
1269{
1270	if ((sc->sc_flags & AUE_FLAG_LL_READY) &&
1271	    (sc->sc_flags & AUE_FLAG_HL_READY)) {
1272
1273		/*
1274		 * start the USB transfers, if not already started:
1275		 */
1276		usb2_transfer_start(sc->sc_xfer[AUE_INTR_DT_RD]);
1277		usb2_transfer_start(sc->sc_xfer[AUE_BULK_DT_RD]);
1278		usb2_transfer_start(sc->sc_xfer[AUE_BULK_DT_WR]);
1279	}
1280}
1281
1282static void
1283aue_cfg_pre_init(struct aue_softc *sc,
1284    struct usb2_config_td_cc *cc, uint16_t refcount)
1285{
1286	struct ifnet *ifp = sc->sc_ifp;
1287
1288	/* immediate configuration */
1289
1290	aue_cfg_pre_stop(sc, cc, 0);
1291
1292	ifp->if_drv_flags |= IFF_DRV_RUNNING;
1293
1294	sc->sc_flags |= AUE_FLAG_HL_READY;
1295}
1296
1297static void
1298aue_cfg_init(struct aue_softc *sc,
1299    struct usb2_config_td_cc *cc, uint16_t refcount)
1300{
1301	struct mii_data *mii = GET_MII(sc);
1302	uint8_t i;
1303
1304	/*
1305	 * Cancel pending I/O
1306	 */
1307	aue_cfg_stop(sc, cc, 0);
1308
1309	/* Set MAC address */
1310	for (i = 0; i < ETHER_ADDR_LEN; i++) {
1311		aue_cfg_csr_write_1(sc, AUE_PAR0 + i, cc->if_lladdr[i]);
1312	}
1313
1314	/* update promiscuous setting */
1315	aue_cfg_promisc_upd(sc, cc, 0);
1316
1317	/* load the multicast filter */
1318	aue_cfg_setmulti(sc, cc, 0);
1319
1320	/* enable RX and TX */
1321	aue_cfg_csr_write_1(sc, AUE_CTL0,
1322	    (AUE_CTL0_RXSTAT_APPEND |
1323	    AUE_CTL0_RX_ENB));
1324
1325	AUE_CFG_SETBIT(sc, AUE_CTL0, AUE_CTL0_TX_ENB);
1326	AUE_CFG_SETBIT(sc, AUE_CTL2, AUE_CTL2_EP3_CLR);
1327
1328	mii_mediachg(mii);
1329
1330	sc->sc_flags |= (AUE_FLAG_READ_STALL |
1331	    AUE_FLAG_WRITE_STALL |
1332	    AUE_FLAG_LL_READY);
1333
1334	aue_start_transfers(sc);
1335}
1336
1337static void
1338aue_cfg_promisc_upd(struct aue_softc *sc,
1339    struct usb2_config_td_cc *cc, uint16_t refcount)
1340{
1341	/* if we want promiscuous mode, set the allframes bit: */
1342	if (cc->if_flags & IFF_PROMISC) {
1343		AUE_CFG_SETBIT(sc, AUE_CTL2, AUE_CTL2_RX_PROMISC);
1344	} else {
1345		AUE_CFG_CLRBIT(sc, AUE_CTL2, AUE_CTL2_RX_PROMISC);
1346	}
1347}
1348
1349/*
1350 * Set media options.
1351 */
1352static int
1353aue_ifmedia_upd_cb(struct ifnet *ifp)
1354{
1355	struct aue_softc *sc = ifp->if_softc;
1356
1357	mtx_lock(&sc->sc_mtx);
1358	usb2_config_td_queue_command
1359	    (&sc->sc_config_td, NULL, &aue_cfg_ifmedia_upd, 0, 0);
1360	mtx_unlock(&sc->sc_mtx);
1361
1362	return (0);
1363}
1364
1365static void
1366aue_cfg_ifmedia_upd(struct aue_softc *sc,
1367    struct usb2_config_td_cc *cc, uint16_t refcount)
1368{
1369	struct ifnet *ifp = sc->sc_ifp;
1370	struct mii_data *mii = GET_MII(sc);
1371
1372	if ((ifp == NULL) ||
1373	    (mii == NULL)) {
1374		/* not ready */
1375		return;
1376	}
1377	sc->sc_flags |= AUE_FLAG_WAIT_LINK;
1378
1379	if (mii->mii_instance) {
1380		struct mii_softc *miisc;
1381
1382		LIST_FOREACH(miisc, &mii->mii_phys, mii_list) {
1383			mii_phy_reset(miisc);
1384		}
1385	}
1386	mii_mediachg(mii);
1387}
1388
1389/*
1390 * Report current media status.
1391 */
1392static void
1393aue_ifmedia_sts_cb(struct ifnet *ifp, struct ifmediareq *ifmr)
1394{
1395	struct aue_softc *sc = ifp->if_softc;
1396
1397	mtx_lock(&sc->sc_mtx);
1398
1399	ifmr->ifm_active = sc->sc_media_active;
1400	ifmr->ifm_status = sc->sc_media_status;
1401
1402	mtx_unlock(&sc->sc_mtx);
1403}
1404
1405static int
1406aue_ioctl_cb(struct ifnet *ifp, u_long command, caddr_t data)
1407{
1408	struct aue_softc *sc = ifp->if_softc;
1409	struct mii_data *mii;
1410	int error = 0;
1411
1412	switch (command) {
1413	case SIOCSIFFLAGS:
1414		mtx_lock(&sc->sc_mtx);
1415		if (ifp->if_flags & IFF_UP) {
1416			if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
1417				usb2_config_td_queue_command
1418				    (&sc->sc_config_td, &aue_config_copy,
1419				    &aue_cfg_promisc_upd, 0, 0);
1420			} else {
1421				usb2_config_td_queue_command
1422				    (&sc->sc_config_td, &aue_cfg_pre_init,
1423				    &aue_cfg_init, 0, 0);
1424			}
1425		} else {
1426			if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
1427				usb2_config_td_queue_command
1428				    (&sc->sc_config_td, &aue_cfg_pre_stop,
1429				    &aue_cfg_stop, 0, 0);
1430			}
1431		}
1432		mtx_unlock(&sc->sc_mtx);
1433		break;
1434
1435	case SIOCADDMULTI:
1436	case SIOCDELMULTI:
1437		mtx_lock(&sc->sc_mtx);
1438		usb2_config_td_queue_command
1439		    (&sc->sc_config_td, &aue_config_copy,
1440		    &aue_cfg_setmulti, 0, 0);
1441		mtx_unlock(&sc->sc_mtx);
1442		break;
1443
1444	case SIOCGIFMEDIA:
1445	case SIOCSIFMEDIA:
1446		mii = GET_MII(sc);
1447		if (mii == NULL) {
1448			error = EINVAL;
1449		} else {
1450			error = ifmedia_ioctl
1451			    (ifp, (void *)data, &mii->mii_media, command);
1452		}
1453		break;
1454
1455	default:
1456		error = ether_ioctl(ifp, command, data);
1457		break;
1458	}
1459	return (error);
1460}
1461
1462static void
1463aue_watchdog(void *arg)
1464{
1465	struct aue_softc *sc = arg;
1466
1467	mtx_assert(&sc->sc_mtx, MA_OWNED);
1468
1469	usb2_config_td_queue_command
1470	    (&sc->sc_config_td, NULL, &aue_cfg_tick, 0, 0);
1471
1472	usb2_callout_reset(&sc->sc_watchdog,
1473	    hz, &aue_watchdog, sc);
1474}
1475
1476/*
1477 * Stop the adapter and free any mbufs allocated to the
1478 * RX and TX lists.
1479 *
1480 * NOTE: can be called when "ifp" is NULL
1481 */
1482static void
1483aue_cfg_pre_stop(struct aue_softc *sc,
1484    struct usb2_config_td_cc *cc, uint16_t refcount)
1485{
1486	struct ifnet *ifp = sc->sc_ifp;
1487
1488	if (cc) {
1489		/* copy the needed configuration */
1490		aue_config_copy(sc, cc, refcount);
1491	}
1492	/* immediate configuration */
1493
1494	if (ifp) {
1495		/* clear flags */
1496		ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1497	}
1498	sc->sc_flags &= ~(AUE_FLAG_HL_READY |
1499	    AUE_FLAG_LL_READY);
1500
1501	sc->sc_flags |= AUE_FLAG_WAIT_LINK;
1502
1503	/*
1504	 * stop all the transfers, if not already stopped:
1505	 */
1506	usb2_transfer_stop(sc->sc_xfer[AUE_BULK_DT_WR]);
1507	usb2_transfer_stop(sc->sc_xfer[AUE_BULK_DT_RD]);
1508	usb2_transfer_stop(sc->sc_xfer[AUE_BULK_CS_WR]);
1509	usb2_transfer_stop(sc->sc_xfer[AUE_BULK_CS_RD]);
1510	usb2_transfer_stop(sc->sc_xfer[AUE_INTR_DT_RD]);
1511	usb2_transfer_stop(sc->sc_xfer[AUE_INTR_CS_RD]);
1512}
1513
1514static void
1515aue_cfg_stop(struct aue_softc *sc,
1516    struct usb2_config_td_cc *cc, uint16_t refcount)
1517{
1518	aue_cfg_csr_write_1(sc, AUE_CTL0, 0);
1519	aue_cfg_csr_write_1(sc, AUE_CTL1, 0);
1520	aue_cfg_reset(sc);
1521}
1522
1523/*
1524 * Stop all chip I/O so that the kernel's probe routines don't
1525 * get confused by errant DMAs when rebooting.
1526 */
1527static int
1528aue_shutdown(device_t dev)
1529{
1530	struct aue_softc *sc = device_get_softc(dev);
1531
1532	mtx_lock(&sc->sc_mtx);
1533
1534	usb2_config_td_queue_command
1535	    (&sc->sc_config_td, &aue_cfg_pre_stop,
1536	    &aue_cfg_stop, 0, 0);
1537
1538	mtx_unlock(&sc->sc_mtx);
1539
1540	return (0);
1541}
1542