if_aue.c revision 187970
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 187970 2009-02-01 00:51:25Z thompsa $");
35
36/*
37 * ADMtek AN986 Pegasus and AN8511 Pegasus II USB to ethernet driver.
38 * Datasheet is available from http://www.admtek.com.tw.
39 *
40 * Written by Bill Paul <wpaul@ee.columbia.edu>
41 * Electrical Engineering Department
42 * Columbia University, New York City
43 */
44
45/*
46 * The Pegasus chip uses four USB "endpoints" to provide 10/100 ethernet
47 * support: the control endpoint for reading/writing registers, burst
48 * read endpoint for packet reception, burst write for packet transmission
49 * and one for "interrupts." The chip uses the same RX filter scheme
50 * as the other ADMtek ethernet parts: one perfect filter entry for the
51 * the station address and a 64-bit multicast hash table. The chip supports
52 * both MII and HomePNA attachments.
53 *
54 * Since the maximum data transfer speed of USB is supposed to be 12Mbps,
55 * you're never really going to get 100Mbps speeds from this device. I
56 * think the idea is to allow the device to connect to 10 or 100Mbps
57 * networks, not necessarily to provide 100Mbps performance. Also, since
58 * the controller uses an external PHY chip, it's possible that board
59 * designers might simply choose a 10Mbps PHY.
60 *
61 * Registers are accessed using usb2_do_request(). Packet transfers are
62 * done using usb2_transfer() and friends.
63 */
64
65/*
66 * NOTE: all function names beginning like "aue_cfg_" can only
67 * be called from within the config thread function !
68 */
69
70#include <dev/usb2/include/usb2_devid.h>
71#include <dev/usb2/include/usb2_standard.h>
72#include <dev/usb2/include/usb2_mfunc.h>
73#include <dev/usb2/include/usb2_error.h>
74
75#define	usb2_config_td_cc usb2_ether_cc
76#define	usb2_config_td_softc aue_softc
77
78#define	USB_DEBUG_VAR aue_debug
79
80#include <dev/usb2/core/usb2_core.h>
81#include <dev/usb2/core/usb2_lookup.h>
82#include <dev/usb2/core/usb2_process.h>
83#include <dev/usb2/core/usb2_config_td.h>
84#include <dev/usb2/core/usb2_debug.h>
85#include <dev/usb2/core/usb2_request.h>
86#include <dev/usb2/core/usb2_busdma.h>
87#include <dev/usb2/core/usb2_util.h>
88
89#include <dev/usb2/ethernet/usb2_ethernet.h>
90#include <dev/usb2/ethernet/if_auereg.h>
91
92MODULE_DEPEND(aue, usb2_ethernet, 1, 1, 1);
93MODULE_DEPEND(aue, usb2_core, 1, 1, 1);
94MODULE_DEPEND(aue, ether, 1, 1, 1);
95MODULE_DEPEND(aue, miibus, 1, 1, 1);
96
97#if USB_DEBUG
98static int aue_debug = 0;
99
100SYSCTL_NODE(_hw_usb2, OID_AUTO, aue, CTLFLAG_RW, 0, "USB aue");
101SYSCTL_INT(_hw_usb2_aue, OID_AUTO, debug, CTLFLAG_RW, &aue_debug, 0,
102    "Debug level");
103#endif
104
105/*
106 * Various supported device vendors/products.
107 */
108static const struct usb2_device_id aue_devs[] = {
109	{USB_VPI(USB_VENDOR_3COM, USB_PRODUCT_3COM_3C460B, AUE_FLAG_PII)},
110	{USB_VPI(USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_DSB650TX_PNA, 0)},
111	{USB_VPI(USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_UFE1000, AUE_FLAG_LSYS)},
112	{USB_VPI(USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_XX10, 0)},
113	{USB_VPI(USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_XX1, AUE_FLAG_PNA | AUE_FLAG_PII)},
114	{USB_VPI(USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_XX2, AUE_FLAG_PII)},
115	{USB_VPI(USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_XX4, AUE_FLAG_PNA)},
116	{USB_VPI(USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_XX5, AUE_FLAG_PNA)},
117	{USB_VPI(USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_XX6, AUE_FLAG_PII)},
118	{USB_VPI(USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_XX7, AUE_FLAG_PII)},
119	{USB_VPI(USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_XX8, AUE_FLAG_PII)},
120	{USB_VPI(USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_XX9, AUE_FLAG_PNA)},
121	{USB_VPI(USB_VENDOR_ACCTON, USB_PRODUCT_ACCTON_SS1001, AUE_FLAG_PII)},
122	{USB_VPI(USB_VENDOR_ACCTON, USB_PRODUCT_ACCTON_USB320_EC, 0)},
123	{USB_VPI(USB_VENDOR_ADMTEK, USB_PRODUCT_ADMTEK_PEGASUSII_2, AUE_FLAG_PII)},
124	{USB_VPI(USB_VENDOR_ADMTEK, USB_PRODUCT_ADMTEK_PEGASUSII_3, AUE_FLAG_PII)},
125	{USB_VPI(USB_VENDOR_ADMTEK, USB_PRODUCT_ADMTEK_PEGASUSII_4, AUE_FLAG_PII)},
126	{USB_VPI(USB_VENDOR_ADMTEK, USB_PRODUCT_ADMTEK_PEGASUSII, AUE_FLAG_PII)},
127	{USB_VPI(USB_VENDOR_ADMTEK, USB_PRODUCT_ADMTEK_PEGASUS, AUE_FLAG_PNA | AUE_FLAG_DUAL_PHY)},
128	{USB_VPI(USB_VENDOR_AEI, USB_PRODUCT_AEI_FASTETHERNET, AUE_FLAG_PII)},
129	{USB_VPI(USB_VENDOR_ALLIEDTELESYN, USB_PRODUCT_ALLIEDTELESYN_ATUSB100, AUE_FLAG_PII)},
130	{USB_VPI(USB_VENDOR_ATEN, USB_PRODUCT_ATEN_UC110T, AUE_FLAG_PII)},
131	{USB_VPI(USB_VENDOR_BELKIN, USB_PRODUCT_BELKIN_USB2LAN, AUE_FLAG_PII)},
132	{USB_VPI(USB_VENDOR_BILLIONTON, USB_PRODUCT_BILLIONTON_USB100, 0)},
133	{USB_VPI(USB_VENDOR_BILLIONTON, USB_PRODUCT_BILLIONTON_USBE100, AUE_FLAG_PII)},
134	{USB_VPI(USB_VENDOR_BILLIONTON, USB_PRODUCT_BILLIONTON_USBEL100, 0)},
135	{USB_VPI(USB_VENDOR_BILLIONTON, USB_PRODUCT_BILLIONTON_USBLP100, AUE_FLAG_PNA)},
136	{USB_VPI(USB_VENDOR_COREGA, USB_PRODUCT_COREGA_FETHER_USB_TXS, AUE_FLAG_PII)},
137	{USB_VPI(USB_VENDOR_COREGA, USB_PRODUCT_COREGA_FETHER_USB_TX, 0)},
138	{USB_VPI(USB_VENDOR_DLINK, USB_PRODUCT_DLINK_DSB650TX1, AUE_FLAG_LSYS)},
139	{USB_VPI(USB_VENDOR_DLINK, USB_PRODUCT_DLINK_DSB650TX2, AUE_FLAG_LSYS | AUE_FLAG_PII)},
140	{USB_VPI(USB_VENDOR_DLINK, USB_PRODUCT_DLINK_DSB650TX3, AUE_FLAG_LSYS | AUE_FLAG_PII)},
141	{USB_VPI(USB_VENDOR_DLINK, USB_PRODUCT_DLINK_DSB650TX4, AUE_FLAG_LSYS | AUE_FLAG_PII)},
142	{USB_VPI(USB_VENDOR_DLINK, USB_PRODUCT_DLINK_DSB650TX_PNA, AUE_FLAG_PNA)},
143	{USB_VPI(USB_VENDOR_DLINK, USB_PRODUCT_DLINK_DSB650TX, AUE_FLAG_LSYS)},
144	{USB_VPI(USB_VENDOR_DLINK, USB_PRODUCT_DLINK_DSB650, AUE_FLAG_LSYS)},
145	{USB_VPI(USB_VENDOR_ELCON, USB_PRODUCT_ELCON_PLAN, AUE_FLAG_PNA | AUE_FLAG_PII)},
146	{USB_VPI(USB_VENDOR_ELECOM, USB_PRODUCT_ELECOM_LDUSB20, AUE_FLAG_PII)},
147	{USB_VPI(USB_VENDOR_ELECOM, USB_PRODUCT_ELECOM_LDUSBLTX, AUE_FLAG_PII)},
148	{USB_VPI(USB_VENDOR_ELECOM, USB_PRODUCT_ELECOM_LDUSBTX0, 0)},
149	{USB_VPI(USB_VENDOR_ELECOM, USB_PRODUCT_ELECOM_LDUSBTX1, AUE_FLAG_LSYS)},
150	{USB_VPI(USB_VENDOR_ELECOM, USB_PRODUCT_ELECOM_LDUSBTX2, 0)},
151	{USB_VPI(USB_VENDOR_ELECOM, USB_PRODUCT_ELECOM_LDUSBTX3, AUE_FLAG_LSYS)},
152	{USB_VPI(USB_VENDOR_ELSA, USB_PRODUCT_ELSA_USB2ETHERNET, 0)},
153	{USB_VPI(USB_VENDOR_GIGABYTE, USB_PRODUCT_GIGABYTE_GNBR402W, 0)},
154	{USB_VPI(USB_VENDOR_HAWKING, USB_PRODUCT_HAWKING_UF100, AUE_FLAG_PII)},
155	{USB_VPI(USB_VENDOR_HP, USB_PRODUCT_HP_HN210E, AUE_FLAG_PII)},
156	{USB_VPI(USB_VENDOR_IODATA, USB_PRODUCT_IODATA_USBETTXS, AUE_FLAG_PII)},
157	{USB_VPI(USB_VENDOR_IODATA, USB_PRODUCT_IODATA_USBETTX, 0)},
158	{USB_VPI(USB_VENDOR_KINGSTON, USB_PRODUCT_KINGSTON_KNU101TX, 0)},
159	{USB_VPI(USB_VENDOR_LINKSYS, USB_PRODUCT_LINKSYS_USB100H1, AUE_FLAG_LSYS | AUE_FLAG_PNA)},
160	{USB_VPI(USB_VENDOR_LINKSYS, USB_PRODUCT_LINKSYS_USB100TX, AUE_FLAG_LSYS)},
161	{USB_VPI(USB_VENDOR_LINKSYS, USB_PRODUCT_LINKSYS_USB10TA, AUE_FLAG_LSYS)},
162	{USB_VPI(USB_VENDOR_LINKSYS, USB_PRODUCT_LINKSYS_USB10TX1, AUE_FLAG_LSYS | AUE_FLAG_PII)},
163	{USB_VPI(USB_VENDOR_LINKSYS, USB_PRODUCT_LINKSYS_USB10TX2, AUE_FLAG_LSYS | AUE_FLAG_PII)},
164	{USB_VPI(USB_VENDOR_LINKSYS, USB_PRODUCT_LINKSYS_USB10T, AUE_FLAG_LSYS)},
165	{USB_VPI(USB_VENDOR_MELCO, USB_PRODUCT_MELCO_LUA2TX5, AUE_FLAG_PII)},
166	{USB_VPI(USB_VENDOR_MELCO, USB_PRODUCT_MELCO_LUATX1, 0)},
167	{USB_VPI(USB_VENDOR_MELCO, USB_PRODUCT_MELCO_LUATX5, 0)},
168	{USB_VPI(USB_VENDOR_MICROSOFT, USB_PRODUCT_MICROSOFT_MN110, AUE_FLAG_PII)},
169	{USB_VPI(USB_VENDOR_NETGEAR, USB_PRODUCT_NETGEAR_FA101, AUE_FLAG_PII)},
170	{USB_VPI(USB_VENDOR_SIEMENS, USB_PRODUCT_SIEMENS_SPEEDSTREAM, AUE_FLAG_PII)},
171	{USB_VPI(USB_VENDOR_SIIG2, USB_PRODUCT_SIIG2_USBTOETHER, AUE_FLAG_PII)},
172	{USB_VPI(USB_VENDOR_SMARTBRIDGES, USB_PRODUCT_SMARTBRIDGES_SMARTNIC, AUE_FLAG_PII)},
173	{USB_VPI(USB_VENDOR_SMC, USB_PRODUCT_SMC_2202USB, 0)},
174	{USB_VPI(USB_VENDOR_SMC, USB_PRODUCT_SMC_2206USB, AUE_FLAG_PII)},
175	{USB_VPI(USB_VENDOR_SOHOWARE, USB_PRODUCT_SOHOWARE_NUB100, 0)},
176	{USB_VPI(USB_VENDOR_SOHOWARE, USB_PRODUCT_SOHOWARE_NUB110, AUE_FLAG_PII)},
177};
178
179/* prototypes */
180
181static device_probe_t aue_probe;
182static device_attach_t aue_attach;
183static device_detach_t aue_detach;
184static device_shutdown_t aue_shutdown;
185
186static usb2_callback_t aue_intr_clear_stall_callback;
187static usb2_callback_t aue_intr_callback;
188static usb2_callback_t aue_bulk_read_clear_stall_callback;
189static usb2_callback_t aue_bulk_read_callback;
190static usb2_callback_t aue_bulk_write_clear_stall_callback;
191static usb2_callback_t aue_bulk_write_callback;
192
193static void	aue_cfg_do_request(struct aue_softc *,
194		    struct usb2_device_request *, void *);
195static uint8_t	aue_cfg_csr_read_1(struct aue_softc *, uint16_t);
196static uint16_t	aue_cfg_csr_read_2(struct aue_softc *, uint16_t);
197static void	aue_cfg_csr_write_1(struct aue_softc *, uint16_t, uint8_t);
198static void	aue_cfg_csr_write_2(struct aue_softc *, uint16_t, uint16_t);
199static void	aue_cfg_eeprom_getword(struct aue_softc *, uint8_t, uint8_t *);
200static void	aue_cfg_read_eeprom(struct aue_softc *, uint8_t *, uint16_t,
201		    uint16_t);
202
203static miibus_readreg_t aue_cfg_miibus_readreg;
204static miibus_writereg_t aue_cfg_miibus_writereg;
205static miibus_statchg_t aue_cfg_miibus_statchg;
206
207static usb2_config_td_command_t aue_cfg_setmulti;
208static usb2_config_td_command_t aue_cfg_first_time_setup;
209static usb2_config_td_command_t aue_config_copy;
210static usb2_config_td_command_t aue_cfg_tick;
211static usb2_config_td_command_t aue_cfg_pre_init;
212static usb2_config_td_command_t aue_cfg_init;
213static usb2_config_td_command_t aue_cfg_promisc_upd;
214static usb2_config_td_command_t aue_cfg_ifmedia_upd;
215static usb2_config_td_command_t aue_cfg_pre_stop;
216static usb2_config_td_command_t aue_cfg_stop;
217
218static void	aue_cfg_reset_pegasus_II(struct aue_softc *);
219static void	aue_cfg_reset(struct aue_softc *);
220static void	aue_start_cb(struct ifnet *);
221static void	aue_init_cb(void *);
222static void	aue_start_transfers(struct aue_softc *);
223static int	aue_ifmedia_upd_cb(struct ifnet *);
224static void	aue_ifmedia_sts_cb(struct ifnet *, struct ifmediareq *);
225static int	aue_ioctl_cb(struct ifnet *, u_long, caddr_t);
226static void	aue_watchdog(void *);
227
228static const struct usb2_config aue_config[AUE_N_TRANSFER] = {
229
230	[AUE_BULK_DT_WR] = {
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	[AUE_BULK_DT_RD] = {
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	[AUE_BULK_CS_WR] = {
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	[AUE_BULK_CS_RD] = {
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	[AUE_INTR_DT_RD] = {
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	[AUE_INTR_CS_RD] = {
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	sc->sc_udev = uaa->device;
756	sc->sc_dev = dev;
757	sc->sc_unit = device_get_unit(dev);
758	sc->sc_flags = USB_GET_DRIVER_INFO(uaa);
759
760	if (uaa->info.bcdDevice >= 0x0201) {
761		sc->sc_flags |= AUE_FLAG_VER_2;	/* XXX currently undocumented */
762	}
763	device_set_usb2_desc(dev);
764
765	snprintf(sc->sc_name, sizeof(sc->sc_name), "%s",
766	    device_get_nameunit(dev));
767
768	mtx_init(&sc->sc_mtx, "aue lock", NULL, MTX_DEF | MTX_RECURSE);
769
770	usb2_callout_init_mtx(&sc->sc_watchdog, &sc->sc_mtx, 0);
771
772	iface_index = AUE_IFACE_IDX;
773	error = usb2_transfer_setup(uaa->device, &iface_index,
774	    sc->sc_xfer, aue_config, AUE_N_TRANSFER,
775	    sc, &sc->sc_mtx);
776	if (error) {
777		device_printf(dev, "allocating USB "
778		    "transfers failed!\n");
779		goto detach;
780	}
781	error = usb2_config_td_setup(&sc->sc_config_td, sc, &sc->sc_mtx,
782	    NULL, sizeof(struct usb2_config_td_cc), 16);
783	if (error) {
784		device_printf(dev, "could not setup config "
785		    "thread!\n");
786		goto detach;
787	}
788	mtx_lock(&sc->sc_mtx);
789
790	sc->sc_flags |= AUE_FLAG_WAIT_LINK;
791
792	/* start setup */
793
794	usb2_config_td_queue_command
795	    (&sc->sc_config_td, NULL, &aue_cfg_first_time_setup, 0, 0);
796
797	aue_watchdog(sc);
798	mtx_unlock(&sc->sc_mtx);
799	return (0);			/* success */
800
801detach:
802	aue_detach(dev);
803	return (ENXIO);			/* failure */
804}
805
806static void
807aue_cfg_first_time_setup(struct aue_softc *sc,
808    struct usb2_config_td_cc *cc, uint16_t refcount)
809{
810	struct ifnet *ifp;
811	int error;
812	uint8_t eaddr[min(ETHER_ADDR_LEN, 6)];
813
814	/* reset the adapter */
815	aue_cfg_reset(sc);
816
817	/* set default value */
818	bzero(eaddr, sizeof(eaddr));
819
820	/* get station address from the EEPROM */
821	aue_cfg_read_eeprom(sc, eaddr, 0, 3);
822
823	mtx_unlock(&sc->sc_mtx);
824
825	ifp = if_alloc(IFT_ETHER);
826
827	mtx_lock(&sc->sc_mtx);
828
829	if (ifp == NULL) {
830		printf("%s: could not if_alloc()\n",
831		    sc->sc_name);
832		goto done;
833	}
834
835	ifp->if_softc = sc;
836	if_initname(ifp, "aue", sc->sc_unit);
837	ifp->if_mtu = ETHERMTU;
838	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
839	ifp->if_ioctl = aue_ioctl_cb;
840	ifp->if_start = aue_start_cb;
841	ifp->if_watchdog = NULL;
842	ifp->if_init = aue_init_cb;
843	IFQ_SET_MAXLEN(&ifp->if_snd, IFQ_MAXLEN);
844	ifp->if_snd.ifq_drv_maxlen = IFQ_MAXLEN;
845	IFQ_SET_READY(&ifp->if_snd);
846
847	/*
848	 * XXX need Giant when accessing the device structures !
849	 */
850
851	mtx_unlock(&sc->sc_mtx);
852
853	mtx_lock(&Giant);
854
855	error = mii_phy_probe(sc->sc_dev, &sc->sc_miibus,
856	    &aue_ifmedia_upd_cb,
857	    &aue_ifmedia_sts_cb);
858
859	mtx_unlock(&Giant);
860
861	mtx_lock(&sc->sc_mtx);
862
863	/*
864	 * Do MII setup.
865	 * NOTE: Doing this causes child devices to be attached to us,
866	 * which we would normally disconnect at in the detach routine
867	 * using device_delete_child(). However the USB code is set up
868	 * such that when this driver is removed, all children devices
869	 * are removed as well. In effect, the USB code ends up detaching
870	 * all of our children for us, so we don't have to do is ourselves
871	 * in aue_detach(). It's important to point this out since if
872	 * we *do* try to detach the child devices ourselves, we will
873	 * end up getting the children deleted twice, which will crash
874	 * the system.
875	 */
876	if (error) {
877		printf("%s: MII without any PHY!\n",
878		    sc->sc_name);
879		if_free(ifp);
880		goto done;
881	}
882	sc->sc_ifp = ifp;
883
884	mtx_unlock(&sc->sc_mtx);
885
886	/*
887	 * Call MI attach routine.
888	 */
889	ether_ifattach(ifp, eaddr);
890
891	mtx_lock(&sc->sc_mtx);
892
893done:
894	return;
895}
896
897static int
898aue_detach(device_t dev)
899{
900	struct aue_softc *sc = device_get_softc(dev);
901	struct ifnet *ifp;
902
903	usb2_config_td_drain(&sc->sc_config_td);
904
905	mtx_lock(&sc->sc_mtx);
906
907	usb2_callout_stop(&sc->sc_watchdog);
908
909	aue_cfg_pre_stop(sc, NULL, 0);
910
911	ifp = sc->sc_ifp;
912
913	mtx_unlock(&sc->sc_mtx);
914
915	/* stop all USB transfers first */
916	usb2_transfer_unsetup(sc->sc_xfer, AUE_N_TRANSFER);
917
918	/* get rid of any late children */
919	bus_generic_detach(dev);
920
921	if (ifp) {
922		ether_ifdetach(ifp);
923		if_free(ifp);
924	}
925	usb2_config_td_unsetup(&sc->sc_config_td);
926
927	usb2_callout_drain(&sc->sc_watchdog);
928
929	mtx_destroy(&sc->sc_mtx);
930
931	return (0);
932}
933
934static void
935aue_intr_clear_stall_callback(struct usb2_xfer *xfer)
936{
937	struct aue_softc *sc = xfer->priv_sc;
938	struct usb2_xfer *xfer_other = sc->sc_xfer[AUE_INTR_DT_RD];
939
940	if (usb2_clear_stall_callback(xfer, xfer_other)) {
941		DPRINTF("stall cleared\n");
942		sc->sc_flags &= ~AUE_FLAG_INTR_STALL;
943		usb2_transfer_start(xfer_other);
944	}
945}
946
947static void
948aue_intr_callback(struct usb2_xfer *xfer)
949{
950	struct aue_softc *sc = xfer->priv_sc;
951	struct ifnet *ifp = sc->sc_ifp;
952	struct aue_intrpkt pkt;
953
954	switch (USB_GET_STATE(xfer)) {
955	case USB_ST_TRANSFERRED:
956
957		if (ifp && (ifp->if_drv_flags & IFF_DRV_RUNNING) &&
958		    (xfer->actlen >= sizeof(pkt))) {
959
960			usb2_copy_out(xfer->frbuffers, 0, &pkt, sizeof(pkt));
961
962			if (pkt.aue_txstat0) {
963				ifp->if_oerrors++;
964			}
965			if (pkt.aue_txstat0 & (AUE_TXSTAT0_LATECOLL &
966			    AUE_TXSTAT0_EXCESSCOLL)) {
967				ifp->if_collisions++;
968			}
969		}
970	case USB_ST_SETUP:
971		if (sc->sc_flags & AUE_FLAG_INTR_STALL) {
972			usb2_transfer_start(sc->sc_xfer[AUE_INTR_CS_RD]);
973		} else {
974			xfer->frlengths[0] = xfer->max_data_length;
975			usb2_start_hardware(xfer);
976		}
977		return;
978
979	default:			/* Error */
980		if (xfer->error != USB_ERR_CANCELLED) {
981			/* start clear stall */
982			sc->sc_flags |= AUE_FLAG_INTR_STALL;
983			usb2_transfer_start(sc->sc_xfer[AUE_INTR_CS_RD]);
984		}
985		return;
986	}
987}
988
989static void
990aue_bulk_read_clear_stall_callback(struct usb2_xfer *xfer)
991{
992	struct aue_softc *sc = xfer->priv_sc;
993	struct usb2_xfer *xfer_other = sc->sc_xfer[AUE_BULK_DT_RD];
994
995	if (usb2_clear_stall_callback(xfer, xfer_other)) {
996		DPRINTF("stall cleared\n");
997		sc->sc_flags &= ~AUE_FLAG_READ_STALL;
998		usb2_transfer_start(xfer_other);
999	}
1000}
1001
1002static void
1003aue_bulk_read_callback(struct usb2_xfer *xfer)
1004{
1005	struct aue_softc *sc = xfer->priv_sc;
1006	struct ifnet *ifp = sc->sc_ifp;
1007	struct mbuf *m = NULL;
1008
1009	switch (USB_GET_STATE(xfer)) {
1010	case USB_ST_TRANSFERRED:
1011		DPRINTFN(11, "received %d bytes\n", xfer->actlen);
1012
1013		if (sc->sc_flags & AUE_FLAG_VER_2) {
1014
1015			if (xfer->actlen == 0) {
1016				ifp->if_ierrors++;
1017				goto tr_setup;
1018			}
1019		} else {
1020
1021			if (xfer->actlen <= (4 + ETHER_CRC_LEN)) {
1022				ifp->if_ierrors++;
1023				goto tr_setup;
1024			}
1025			usb2_copy_out(xfer->frbuffers, xfer->actlen - 4, &sc->sc_rxpkt,
1026			    sizeof(sc->sc_rxpkt));
1027
1028			/*
1029			 * turn off all the non-error bits in the rx status
1030			 * word:
1031			 */
1032			sc->sc_rxpkt.aue_rxstat &= AUE_RXSTAT_MASK;
1033
1034			if (sc->sc_rxpkt.aue_rxstat) {
1035				ifp->if_ierrors++;
1036				goto tr_setup;
1037			}
1038			/* No errors; receive the packet. */
1039			xfer->actlen -= (4 + ETHER_CRC_LEN);
1040		}
1041
1042		m = usb2_ether_get_mbuf();
1043
1044		if (m == NULL) {
1045			ifp->if_ierrors++;
1046			goto tr_setup;
1047		}
1048		xfer->actlen = min(xfer->actlen, m->m_len);
1049
1050		usb2_copy_out(xfer->frbuffers, 0, m->m_data, xfer->actlen);
1051
1052		ifp->if_ipackets++;
1053		m->m_pkthdr.rcvif = ifp;
1054		m->m_pkthdr.len = m->m_len = xfer->actlen;
1055
1056	case USB_ST_SETUP:
1057tr_setup:
1058
1059		if (sc->sc_flags & AUE_FLAG_READ_STALL) {
1060			usb2_transfer_start(sc->sc_xfer[AUE_BULK_CS_RD]);
1061		} else {
1062			xfer->frlengths[0] = xfer->max_data_length;
1063			usb2_start_hardware(xfer);
1064		}
1065
1066		/*
1067		 * At the end of a USB callback it is always safe to unlock
1068		 * the private mutex of a device! That is why we do the
1069		 * "if_input" here, and not some lines up!
1070		 */
1071		if (m) {
1072			mtx_unlock(&sc->sc_mtx);
1073			(ifp->if_input) (ifp, m);
1074			mtx_lock(&sc->sc_mtx);
1075		}
1076		return;
1077
1078	default:			/* Error */
1079		if (xfer->error != USB_ERR_CANCELLED) {
1080			/* try to clear stall first */
1081			sc->sc_flags |= AUE_FLAG_READ_STALL;
1082			usb2_transfer_start(sc->sc_xfer[AUE_BULK_CS_RD]);
1083		}
1084		DPRINTF("bulk read error, %s\n",
1085		    usb2_errstr(xfer->error));
1086		return;
1087
1088	}
1089}
1090
1091static void
1092aue_bulk_write_clear_stall_callback(struct usb2_xfer *xfer)
1093{
1094	struct aue_softc *sc = xfer->priv_sc;
1095	struct usb2_xfer *xfer_other = sc->sc_xfer[AUE_BULK_DT_WR];
1096
1097	if (usb2_clear_stall_callback(xfer, xfer_other)) {
1098		DPRINTF("stall cleared\n");
1099		sc->sc_flags &= ~AUE_FLAG_WRITE_STALL;
1100		usb2_transfer_start(xfer_other);
1101	}
1102}
1103
1104static void
1105aue_bulk_write_callback(struct usb2_xfer *xfer)
1106{
1107	struct aue_softc *sc = xfer->priv_sc;
1108	struct ifnet *ifp = sc->sc_ifp;
1109	struct mbuf *m;
1110	uint8_t buf[2];
1111
1112	switch (USB_GET_STATE(xfer)) {
1113	case USB_ST_TRANSFERRED:
1114		DPRINTFN(11, "transfer of %d bytes complete\n", xfer->actlen);
1115
1116		ifp->if_opackets++;
1117
1118	case USB_ST_SETUP:
1119
1120		if (sc->sc_flags & AUE_FLAG_WRITE_STALL) {
1121			usb2_transfer_start(sc->sc_xfer[AUE_BULK_CS_WR]);
1122			goto done;
1123		}
1124		if (sc->sc_flags & AUE_FLAG_WAIT_LINK) {
1125			/*
1126			 * don't send anything if there is no link !
1127			 */
1128			goto done;
1129		}
1130		IFQ_DRV_DEQUEUE(&ifp->if_snd, m);
1131
1132		if (m == NULL) {
1133			goto done;
1134		}
1135		if (m->m_pkthdr.len > MCLBYTES) {
1136			m->m_pkthdr.len = MCLBYTES;
1137		}
1138		if (sc->sc_flags & AUE_FLAG_VER_2) {
1139
1140			xfer->frlengths[0] = m->m_pkthdr.len;
1141
1142			usb2_m_copy_in(xfer->frbuffers, 0,
1143			    m, 0, m->m_pkthdr.len);
1144
1145		} else {
1146
1147			xfer->frlengths[0] = (m->m_pkthdr.len + 2);
1148
1149			/*
1150		         * The ADMtek documentation says that the packet length is
1151		         * supposed to be specified in the first two bytes of the
1152		         * transfer, however it actually seems to ignore this info
1153		         * and base the frame size on the bulk transfer length.
1154		         */
1155			buf[0] = (uint8_t)(m->m_pkthdr.len);
1156			buf[1] = (uint8_t)(m->m_pkthdr.len >> 8);
1157
1158			usb2_copy_in(xfer->frbuffers, 0, buf, 2);
1159
1160			usb2_m_copy_in(xfer->frbuffers, 2,
1161			    m, 0, m->m_pkthdr.len);
1162		}
1163
1164		/*
1165		 * if there's a BPF listener, bounce a copy
1166		 * of this frame to him:
1167		 */
1168		BPF_MTAP(ifp, m);
1169
1170		m_freem(m);
1171
1172		usb2_start_hardware(xfer);
1173
1174done:
1175		return;
1176
1177	default:			/* Error */
1178		DPRINTFN(11, "transfer error, %s\n",
1179		    usb2_errstr(xfer->error));
1180
1181		if (xfer->error != USB_ERR_CANCELLED) {
1182			/* try to clear stall first */
1183			sc->sc_flags |= AUE_FLAG_WRITE_STALL;
1184			usb2_transfer_start(sc->sc_xfer[AUE_BULK_CS_WR]);
1185		}
1186		ifp->if_oerrors++;
1187		return;
1188
1189	}
1190}
1191
1192#define	AUE_BITS 6
1193
1194static void
1195aue_mchash(struct usb2_config_td_cc *cc, const uint8_t *ptr)
1196{
1197	uint8_t h;
1198
1199	h = ether_crc32_le(ptr, ETHER_ADDR_LEN) &
1200	    ((1 << AUE_BITS) - 1);
1201	cc->if_hash[(h >> 3)] |= (1 << (h & 7));
1202}
1203
1204static void
1205aue_config_copy(struct aue_softc *sc,
1206    struct usb2_config_td_cc *cc, uint16_t refcount)
1207{
1208	bzero(cc, sizeof(*cc));
1209	usb2_ether_cc(sc->sc_ifp, &aue_mchash, cc);
1210}
1211
1212static void
1213aue_cfg_tick(struct aue_softc *sc,
1214    struct usb2_config_td_cc *cc, uint16_t refcount)
1215{
1216	struct ifnet *ifp = sc->sc_ifp;
1217	struct mii_data *mii = GET_MII(sc);
1218
1219	if ((ifp == NULL) ||
1220	    (mii == NULL)) {
1221		/* not ready */
1222		return;
1223	}
1224	mii_tick(mii);
1225
1226	mii_pollstat(mii);
1227
1228	if ((sc->sc_flags & AUE_FLAG_WAIT_LINK) &&
1229	    (mii->mii_media_status & IFM_ACTIVE) &&
1230	    (IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE)) {
1231		sc->sc_flags &= ~AUE_FLAG_WAIT_LINK;
1232	}
1233	sc->sc_media_active = mii->mii_media_active;
1234	sc->sc_media_status = mii->mii_media_status;
1235
1236	/* start stopped transfers, if any */
1237
1238	aue_start_transfers(sc);
1239}
1240
1241static void
1242aue_start_cb(struct ifnet *ifp)
1243{
1244	struct aue_softc *sc = ifp->if_softc;
1245
1246	mtx_lock(&sc->sc_mtx);
1247
1248	aue_start_transfers(sc);
1249
1250	mtx_unlock(&sc->sc_mtx);
1251}
1252
1253static void
1254aue_init_cb(void *arg)
1255{
1256	struct aue_softc *sc = arg;
1257
1258	mtx_lock(&sc->sc_mtx);
1259	usb2_config_td_queue_command
1260	    (&sc->sc_config_td, &aue_cfg_pre_init, &aue_cfg_init, 0, 0);
1261	mtx_unlock(&sc->sc_mtx);
1262}
1263
1264static void
1265aue_start_transfers(struct aue_softc *sc)
1266{
1267	if ((sc->sc_flags & AUE_FLAG_LL_READY) &&
1268	    (sc->sc_flags & AUE_FLAG_HL_READY)) {
1269
1270		/*
1271		 * start the USB transfers, if not already started:
1272		 */
1273		usb2_transfer_start(sc->sc_xfer[AUE_INTR_DT_RD]);
1274		usb2_transfer_start(sc->sc_xfer[AUE_BULK_DT_RD]);
1275		usb2_transfer_start(sc->sc_xfer[AUE_BULK_DT_WR]);
1276	}
1277}
1278
1279static void
1280aue_cfg_pre_init(struct aue_softc *sc,
1281    struct usb2_config_td_cc *cc, uint16_t refcount)
1282{
1283	struct ifnet *ifp = sc->sc_ifp;
1284
1285	/* immediate configuration */
1286
1287	aue_cfg_pre_stop(sc, cc, 0);
1288
1289	ifp->if_drv_flags |= IFF_DRV_RUNNING;
1290
1291	sc->sc_flags |= AUE_FLAG_HL_READY;
1292}
1293
1294static void
1295aue_cfg_init(struct aue_softc *sc,
1296    struct usb2_config_td_cc *cc, uint16_t refcount)
1297{
1298	struct mii_data *mii = GET_MII(sc);
1299	uint8_t i;
1300
1301	/*
1302	 * Cancel pending I/O
1303	 */
1304	aue_cfg_stop(sc, cc, 0);
1305
1306	/* Set MAC address */
1307	for (i = 0; i < ETHER_ADDR_LEN; i++) {
1308		aue_cfg_csr_write_1(sc, AUE_PAR0 + i, cc->if_lladdr[i]);
1309	}
1310
1311	/* update promiscuous setting */
1312	aue_cfg_promisc_upd(sc, cc, 0);
1313
1314	/* load the multicast filter */
1315	aue_cfg_setmulti(sc, cc, 0);
1316
1317	/* enable RX and TX */
1318	aue_cfg_csr_write_1(sc, AUE_CTL0,
1319	    (AUE_CTL0_RXSTAT_APPEND |
1320	    AUE_CTL0_RX_ENB));
1321
1322	AUE_CFG_SETBIT(sc, AUE_CTL0, AUE_CTL0_TX_ENB);
1323	AUE_CFG_SETBIT(sc, AUE_CTL2, AUE_CTL2_EP3_CLR);
1324
1325	mii_mediachg(mii);
1326
1327	sc->sc_flags |= (AUE_FLAG_READ_STALL |
1328	    AUE_FLAG_WRITE_STALL |
1329	    AUE_FLAG_LL_READY);
1330
1331	aue_start_transfers(sc);
1332}
1333
1334static void
1335aue_cfg_promisc_upd(struct aue_softc *sc,
1336    struct usb2_config_td_cc *cc, uint16_t refcount)
1337{
1338	/* if we want promiscuous mode, set the allframes bit: */
1339	if (cc->if_flags & IFF_PROMISC) {
1340		AUE_CFG_SETBIT(sc, AUE_CTL2, AUE_CTL2_RX_PROMISC);
1341	} else {
1342		AUE_CFG_CLRBIT(sc, AUE_CTL2, AUE_CTL2_RX_PROMISC);
1343	}
1344}
1345
1346/*
1347 * Set media options.
1348 */
1349static int
1350aue_ifmedia_upd_cb(struct ifnet *ifp)
1351{
1352	struct aue_softc *sc = ifp->if_softc;
1353
1354	mtx_lock(&sc->sc_mtx);
1355	usb2_config_td_queue_command
1356	    (&sc->sc_config_td, NULL, &aue_cfg_ifmedia_upd, 0, 0);
1357	mtx_unlock(&sc->sc_mtx);
1358
1359	return (0);
1360}
1361
1362static void
1363aue_cfg_ifmedia_upd(struct aue_softc *sc,
1364    struct usb2_config_td_cc *cc, uint16_t refcount)
1365{
1366	struct ifnet *ifp = sc->sc_ifp;
1367	struct mii_data *mii = GET_MII(sc);
1368
1369	if ((ifp == NULL) ||
1370	    (mii == NULL)) {
1371		/* not ready */
1372		return;
1373	}
1374	sc->sc_flags |= AUE_FLAG_WAIT_LINK;
1375
1376	if (mii->mii_instance) {
1377		struct mii_softc *miisc;
1378
1379		LIST_FOREACH(miisc, &mii->mii_phys, mii_list) {
1380			mii_phy_reset(miisc);
1381		}
1382	}
1383	mii_mediachg(mii);
1384}
1385
1386/*
1387 * Report current media status.
1388 */
1389static void
1390aue_ifmedia_sts_cb(struct ifnet *ifp, struct ifmediareq *ifmr)
1391{
1392	struct aue_softc *sc = ifp->if_softc;
1393
1394	mtx_lock(&sc->sc_mtx);
1395
1396	ifmr->ifm_active = sc->sc_media_active;
1397	ifmr->ifm_status = sc->sc_media_status;
1398
1399	mtx_unlock(&sc->sc_mtx);
1400}
1401
1402static int
1403aue_ioctl_cb(struct ifnet *ifp, u_long command, caddr_t data)
1404{
1405	struct aue_softc *sc = ifp->if_softc;
1406	struct mii_data *mii;
1407	int error = 0;
1408
1409	switch (command) {
1410	case SIOCSIFFLAGS:
1411		mtx_lock(&sc->sc_mtx);
1412		if (ifp->if_flags & IFF_UP) {
1413			if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
1414				usb2_config_td_queue_command
1415				    (&sc->sc_config_td, &aue_config_copy,
1416				    &aue_cfg_promisc_upd, 0, 0);
1417			} else {
1418				usb2_config_td_queue_command
1419				    (&sc->sc_config_td, &aue_cfg_pre_init,
1420				    &aue_cfg_init, 0, 0);
1421			}
1422		} else {
1423			if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
1424				usb2_config_td_queue_command
1425				    (&sc->sc_config_td, &aue_cfg_pre_stop,
1426				    &aue_cfg_stop, 0, 0);
1427			}
1428		}
1429		mtx_unlock(&sc->sc_mtx);
1430		break;
1431
1432	case SIOCADDMULTI:
1433	case SIOCDELMULTI:
1434		mtx_lock(&sc->sc_mtx);
1435		usb2_config_td_queue_command
1436		    (&sc->sc_config_td, &aue_config_copy,
1437		    &aue_cfg_setmulti, 0, 0);
1438		mtx_unlock(&sc->sc_mtx);
1439		break;
1440
1441	case SIOCGIFMEDIA:
1442	case SIOCSIFMEDIA:
1443		mii = GET_MII(sc);
1444		if (mii == NULL) {
1445			error = EINVAL;
1446		} else {
1447			error = ifmedia_ioctl
1448			    (ifp, (void *)data, &mii->mii_media, command);
1449		}
1450		break;
1451
1452	default:
1453		error = ether_ioctl(ifp, command, data);
1454		break;
1455	}
1456	return (error);
1457}
1458
1459static void
1460aue_watchdog(void *arg)
1461{
1462	struct aue_softc *sc = arg;
1463
1464	mtx_assert(&sc->sc_mtx, MA_OWNED);
1465
1466	usb2_config_td_queue_command
1467	    (&sc->sc_config_td, NULL, &aue_cfg_tick, 0, 0);
1468
1469	usb2_callout_reset(&sc->sc_watchdog,
1470	    hz, &aue_watchdog, sc);
1471}
1472
1473/*
1474 * Stop the adapter and free any mbufs allocated to the
1475 * RX and TX lists.
1476 *
1477 * NOTE: can be called when "ifp" is NULL
1478 */
1479static void
1480aue_cfg_pre_stop(struct aue_softc *sc,
1481    struct usb2_config_td_cc *cc, uint16_t refcount)
1482{
1483	struct ifnet *ifp = sc->sc_ifp;
1484
1485	if (cc) {
1486		/* copy the needed configuration */
1487		aue_config_copy(sc, cc, refcount);
1488	}
1489	/* immediate configuration */
1490
1491	if (ifp) {
1492		/* clear flags */
1493		ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1494	}
1495	sc->sc_flags &= ~(AUE_FLAG_HL_READY |
1496	    AUE_FLAG_LL_READY);
1497
1498	sc->sc_flags |= AUE_FLAG_WAIT_LINK;
1499
1500	/*
1501	 * stop all the transfers, if not already stopped:
1502	 */
1503	usb2_transfer_stop(sc->sc_xfer[AUE_BULK_DT_WR]);
1504	usb2_transfer_stop(sc->sc_xfer[AUE_BULK_DT_RD]);
1505	usb2_transfer_stop(sc->sc_xfer[AUE_BULK_CS_WR]);
1506	usb2_transfer_stop(sc->sc_xfer[AUE_BULK_CS_RD]);
1507	usb2_transfer_stop(sc->sc_xfer[AUE_INTR_DT_RD]);
1508	usb2_transfer_stop(sc->sc_xfer[AUE_INTR_CS_RD]);
1509}
1510
1511static void
1512aue_cfg_stop(struct aue_softc *sc,
1513    struct usb2_config_td_cc *cc, uint16_t refcount)
1514{
1515	aue_cfg_csr_write_1(sc, AUE_CTL0, 0);
1516	aue_cfg_csr_write_1(sc, AUE_CTL1, 0);
1517	aue_cfg_reset(sc);
1518}
1519
1520/*
1521 * Stop all chip I/O so that the kernel's probe routines don't
1522 * get confused by errant DMAs when rebooting.
1523 */
1524static int
1525aue_shutdown(device_t dev)
1526{
1527	struct aue_softc *sc = device_get_softc(dev);
1528
1529	mtx_lock(&sc->sc_mtx);
1530
1531	usb2_config_td_queue_command
1532	    (&sc->sc_config_td, &aue_cfg_pre_stop,
1533	    &aue_cfg_stop, 0, 0);
1534
1535	mtx_unlock(&sc->sc_mtx);
1536
1537	return (0);
1538}
1539