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