1184610Salfred/*-
2184610Salfred * Copyright (c) 1997, 1998, 1999, 2000
3184610Salfred *	Bill Paul <wpaul@ee.columbia.edu>.  All rights reserved.
4184610Salfred *
5188412Sthompsa * Copyright (c) 2006
6189002Sed *      Alfred Perlstein <alfred@FreeBSD.org>. All rights reserved.
7188412Sthompsa *
8184610Salfred * Redistribution and use in source and binary forms, with or without
9184610Salfred * modification, are permitted provided that the following conditions
10184610Salfred * are met:
11184610Salfred * 1. Redistributions of source code must retain the above copyright
12184610Salfred *    notice, this list of conditions and the following disclaimer.
13184610Salfred * 2. Redistributions in binary form must reproduce the above copyright
14184610Salfred *    notice, this list of conditions and the following disclaimer in the
15184610Salfred *    documentation and/or other materials provided with the distribution.
16184610Salfred * 3. All advertising materials mentioning features or use of this software
17184610Salfred *    must display the following acknowledgement:
18184610Salfred *	This product includes software developed by Bill Paul.
19184610Salfred * 4. Neither the name of the author nor the names of any co-contributors
20184610Salfred *    may be used to endorse or promote products derived from this software
21184610Salfred *    without specific prior written permission.
22184610Salfred *
23184610Salfred * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
24184610Salfred * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25184610Salfred * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26184610Salfred * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
27184610Salfred * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28184610Salfred * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29184610Salfred * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30184610Salfred * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31184610Salfred * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32184610Salfred * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
33184610Salfred * THE POSSIBILITY OF SUCH DAMAGE.
34184610Salfred */
35184610Salfred
36184610Salfred#include <sys/cdefs.h>
37184610Salfred__FBSDID("$FreeBSD$");
38184610Salfred
39184610Salfred/*
40184610Salfred * ADMtek AN986 Pegasus and AN8511 Pegasus II USB to ethernet driver.
41184610Salfred * Datasheet is available from http://www.admtek.com.tw.
42184610Salfred *
43184610Salfred * Written by Bill Paul <wpaul@ee.columbia.edu>
44184610Salfred * Electrical Engineering Department
45184610Salfred * Columbia University, New York City
46188412Sthompsa *
47189002Sed * SMP locking by Alfred Perlstein <alfred@FreeBSD.org>.
48188412Sthompsa * RED Inc.
49184610Salfred */
50184610Salfred
51184610Salfred/*
52184610Salfred * The Pegasus chip uses four USB "endpoints" to provide 10/100 ethernet
53184610Salfred * support: the control endpoint for reading/writing registers, burst
54184610Salfred * read endpoint for packet reception, burst write for packet transmission
55184610Salfred * and one for "interrupts." The chip uses the same RX filter scheme
56184610Salfred * as the other ADMtek ethernet parts: one perfect filter entry for the
57184610Salfred * the station address and a 64-bit multicast hash table. The chip supports
58184610Salfred * both MII and HomePNA attachments.
59184610Salfred *
60184610Salfred * Since the maximum data transfer speed of USB is supposed to be 12Mbps,
61184610Salfred * you're never really going to get 100Mbps speeds from this device. I
62184610Salfred * think the idea is to allow the device to connect to 10 or 100Mbps
63184610Salfred * networks, not necessarily to provide 100Mbps performance. Also, since
64184610Salfred * the controller uses an external PHY chip, it's possible that board
65184610Salfred * designers might simply choose a 10Mbps PHY.
66184610Salfred *
67194228Sthompsa * Registers are accessed using uether_do_request(). Packet
68194228Sthompsa * transfers are done using usbd_transfer() and friends.
69184610Salfred */
70184610Salfred
71194677Sthompsa#include <sys/stdint.h>
72194677Sthompsa#include <sys/stddef.h>
73194677Sthompsa#include <sys/param.h>
74194677Sthompsa#include <sys/queue.h>
75194677Sthompsa#include <sys/types.h>
76194677Sthompsa#include <sys/systm.h>
77257176Sglebius#include <sys/socket.h>
78194677Sthompsa#include <sys/kernel.h>
79194677Sthompsa#include <sys/bus.h>
80194677Sthompsa#include <sys/module.h>
81194677Sthompsa#include <sys/lock.h>
82194677Sthompsa#include <sys/mutex.h>
83194677Sthompsa#include <sys/condvar.h>
84194677Sthompsa#include <sys/sysctl.h>
85194677Sthompsa#include <sys/sx.h>
86194677Sthompsa#include <sys/unistd.h>
87194677Sthompsa#include <sys/callout.h>
88194677Sthompsa#include <sys/malloc.h>
89194677Sthompsa#include <sys/priv.h>
90194677Sthompsa
91257176Sglebius#include <net/if.h>
92257176Sglebius#include <net/if_var.h>
93257176Sglebius
94194677Sthompsa#include <dev/usb/usb.h>
95194677Sthompsa#include <dev/usb/usbdi.h>
96194677Sthompsa#include <dev/usb/usbdi_util.h>
97188746Sthompsa#include "usbdevs.h"
98184610Salfred
99184610Salfred#define	USB_DEBUG_VAR aue_debug
100194677Sthompsa#include <dev/usb/usb_debug.h>
101188942Sthompsa#include <dev/usb/usb_process.h>
102184610Salfred
103188942Sthompsa#include <dev/usb/net/usb_ethernet.h>
104188942Sthompsa#include <dev/usb/net/if_auereg.h>
105184610Salfred
106207077Sthompsa#ifdef USB_DEBUG
107184610Salfredstatic int aue_debug = 0;
108184610Salfred
109227309Sedstatic SYSCTL_NODE(_hw_usb, OID_AUTO, aue, CTLFLAG_RW, 0, "USB aue");
110276701ShselaskySYSCTL_INT(_hw_usb_aue, OID_AUTO, debug, CTLFLAG_RWTUN, &aue_debug, 0,
111184610Salfred    "Debug level");
112184610Salfred#endif
113184610Salfred
114184610Salfred/*
115184610Salfred * Various supported device vendors/products.
116184610Salfred */
117223486Shselaskystatic const STRUCT_USB_HOST_ID aue_devs[] = {
118201028Sthompsa#define	AUE_DEV(v,p,i) { USB_VPI(USB_VENDOR_##v, USB_PRODUCT_##v##_##p, i) }
119201028Sthompsa    AUE_DEV(3COM, 3C460B, AUE_FLAG_PII),
120201028Sthompsa    AUE_DEV(ABOCOM, DSB650TX_PNA, 0),
121201028Sthompsa    AUE_DEV(ABOCOM, UFE1000, AUE_FLAG_LSYS),
122201028Sthompsa    AUE_DEV(ABOCOM, XX10, 0),
123201028Sthompsa    AUE_DEV(ABOCOM, XX1, AUE_FLAG_PNA | AUE_FLAG_PII),
124201028Sthompsa    AUE_DEV(ABOCOM, XX2, AUE_FLAG_PII),
125201028Sthompsa    AUE_DEV(ABOCOM, XX4, AUE_FLAG_PNA),
126201028Sthompsa    AUE_DEV(ABOCOM, XX5, AUE_FLAG_PNA),
127201028Sthompsa    AUE_DEV(ABOCOM, XX6, AUE_FLAG_PII),
128201028Sthompsa    AUE_DEV(ABOCOM, XX7, AUE_FLAG_PII),
129201028Sthompsa    AUE_DEV(ABOCOM, XX8, AUE_FLAG_PII),
130201028Sthompsa    AUE_DEV(ABOCOM, XX9, AUE_FLAG_PNA),
131201028Sthompsa    AUE_DEV(ACCTON, SS1001, AUE_FLAG_PII),
132201028Sthompsa    AUE_DEV(ACCTON, USB320_EC, 0),
133201028Sthompsa    AUE_DEV(ADMTEK, PEGASUSII_2, AUE_FLAG_PII),
134201028Sthompsa    AUE_DEV(ADMTEK, PEGASUSII_3, AUE_FLAG_PII),
135201028Sthompsa    AUE_DEV(ADMTEK, PEGASUSII_4, AUE_FLAG_PII),
136201028Sthompsa    AUE_DEV(ADMTEK, PEGASUSII, AUE_FLAG_PII),
137201028Sthompsa    AUE_DEV(ADMTEK, PEGASUS, AUE_FLAG_PNA | AUE_FLAG_DUAL_PHY),
138201028Sthompsa    AUE_DEV(AEI, FASTETHERNET, AUE_FLAG_PII),
139201028Sthompsa    AUE_DEV(ALLIEDTELESYN, ATUSB100, AUE_FLAG_PII),
140201028Sthompsa    AUE_DEV(ATEN, UC110T, AUE_FLAG_PII),
141201028Sthompsa    AUE_DEV(BELKIN, USB2LAN, AUE_FLAG_PII),
142201028Sthompsa    AUE_DEV(BILLIONTON, USB100, 0),
143201028Sthompsa    AUE_DEV(BILLIONTON, USBE100, AUE_FLAG_PII),
144201028Sthompsa    AUE_DEV(BILLIONTON, USBEL100, 0),
145201028Sthompsa    AUE_DEV(BILLIONTON, USBLP100, AUE_FLAG_PNA),
146201028Sthompsa    AUE_DEV(COREGA, FETHER_USB_TXS, AUE_FLAG_PII),
147201028Sthompsa    AUE_DEV(COREGA, FETHER_USB_TX, 0),
148201028Sthompsa    AUE_DEV(DLINK, DSB650TX1, AUE_FLAG_LSYS),
149201028Sthompsa    AUE_DEV(DLINK, DSB650TX2, AUE_FLAG_LSYS | AUE_FLAG_PII),
150201028Sthompsa    AUE_DEV(DLINK, DSB650TX3, AUE_FLAG_LSYS | AUE_FLAG_PII),
151201028Sthompsa    AUE_DEV(DLINK, DSB650TX4, AUE_FLAG_LSYS | AUE_FLAG_PII),
152201028Sthompsa    AUE_DEV(DLINK, DSB650TX_PNA, AUE_FLAG_PNA),
153201028Sthompsa    AUE_DEV(DLINK, DSB650TX, AUE_FLAG_LSYS),
154201028Sthompsa    AUE_DEV(DLINK, DSB650, AUE_FLAG_LSYS),
155201028Sthompsa    AUE_DEV(ELCON, PLAN, AUE_FLAG_PNA | AUE_FLAG_PII),
156201028Sthompsa    AUE_DEV(ELECOM, LDUSB20, AUE_FLAG_PII),
157201028Sthompsa    AUE_DEV(ELECOM, LDUSBLTX, AUE_FLAG_PII),
158201028Sthompsa    AUE_DEV(ELECOM, LDUSBTX0, 0),
159201028Sthompsa    AUE_DEV(ELECOM, LDUSBTX1, AUE_FLAG_LSYS),
160201028Sthompsa    AUE_DEV(ELECOM, LDUSBTX2, 0),
161201028Sthompsa    AUE_DEV(ELECOM, LDUSBTX3, AUE_FLAG_LSYS),
162201028Sthompsa    AUE_DEV(ELSA, USB2ETHERNET, 0),
163201028Sthompsa    AUE_DEV(GIGABYTE, GNBR402W, 0),
164201028Sthompsa    AUE_DEV(HAWKING, UF100, AUE_FLAG_PII),
165201028Sthompsa    AUE_DEV(HP, HN210E, AUE_FLAG_PII),
166201028Sthompsa    AUE_DEV(IODATA, USBETTXS, AUE_FLAG_PII),
167201028Sthompsa    AUE_DEV(IODATA, USBETTX, 0),
168201028Sthompsa    AUE_DEV(KINGSTON, KNU101TX, 0),
169201028Sthompsa    AUE_DEV(LINKSYS, USB100H1, AUE_FLAG_LSYS | AUE_FLAG_PNA),
170201028Sthompsa    AUE_DEV(LINKSYS, USB100TX, AUE_FLAG_LSYS),
171201028Sthompsa    AUE_DEV(LINKSYS, USB10TA, AUE_FLAG_LSYS),
172201028Sthompsa    AUE_DEV(LINKSYS, USB10TX1, AUE_FLAG_LSYS | AUE_FLAG_PII),
173201028Sthompsa    AUE_DEV(LINKSYS, USB10TX2, AUE_FLAG_LSYS | AUE_FLAG_PII),
174201028Sthompsa    AUE_DEV(LINKSYS, USB10T, AUE_FLAG_LSYS),
175201028Sthompsa    AUE_DEV(MELCO, LUA2TX5, AUE_FLAG_PII),
176201028Sthompsa    AUE_DEV(MELCO, LUATX1, 0),
177201028Sthompsa    AUE_DEV(MELCO, LUATX5, 0),
178201028Sthompsa    AUE_DEV(MICROSOFT, MN110, AUE_FLAG_PII),
179201028Sthompsa    AUE_DEV(NETGEAR, FA101, AUE_FLAG_PII),
180201028Sthompsa    AUE_DEV(SIEMENS, SPEEDSTREAM, AUE_FLAG_PII),
181201028Sthompsa    AUE_DEV(SIIG2, USBTOETHER, AUE_FLAG_PII),
182201028Sthompsa    AUE_DEV(SMARTBRIDGES, SMARTNIC, AUE_FLAG_PII),
183201028Sthompsa    AUE_DEV(SMC, 2202USB, 0),
184201028Sthompsa    AUE_DEV(SMC, 2206USB, AUE_FLAG_PII),
185201028Sthompsa    AUE_DEV(SOHOWARE, NUB100, 0),
186201028Sthompsa    AUE_DEV(SOHOWARE, NUB110, AUE_FLAG_PII),
187201028Sthompsa#undef AUE_DEV
188184610Salfred};
189184610Salfred
190184610Salfred/* prototypes */
191184610Salfred
192184610Salfredstatic device_probe_t aue_probe;
193184610Salfredstatic device_attach_t aue_attach;
194184610Salfredstatic device_detach_t aue_detach;
195188412Sthompsastatic miibus_readreg_t aue_miibus_readreg;
196188412Sthompsastatic miibus_writereg_t aue_miibus_writereg;
197188412Sthompsastatic miibus_statchg_t aue_miibus_statchg;
198184610Salfred
199193045Sthompsastatic usb_callback_t aue_intr_callback;
200193045Sthompsastatic usb_callback_t aue_bulk_read_callback;
201193045Sthompsastatic usb_callback_t aue_bulk_write_callback;
202184610Salfred
203193045Sthompsastatic uether_fn_t aue_attach_post;
204193045Sthompsastatic uether_fn_t aue_init;
205193045Sthompsastatic uether_fn_t aue_stop;
206193045Sthompsastatic uether_fn_t aue_start;
207193045Sthompsastatic uether_fn_t aue_tick;
208193045Sthompsastatic uether_fn_t aue_setmulti;
209193045Sthompsastatic uether_fn_t aue_setpromisc;
210188412Sthompsa
211188412Sthompsastatic uint8_t	aue_csr_read_1(struct aue_softc *, uint16_t);
212188412Sthompsastatic uint16_t	aue_csr_read_2(struct aue_softc *, uint16_t);
213188412Sthompsastatic void	aue_csr_write_1(struct aue_softc *, uint16_t, uint8_t);
214188412Sthompsastatic void	aue_csr_write_2(struct aue_softc *, uint16_t, uint16_t);
215264062Syongaristatic uint16_t	aue_eeprom_getword(struct aue_softc *, int);
216188412Sthompsastatic void	aue_reset(struct aue_softc *);
217188412Sthompsastatic void	aue_reset_pegasus_II(struct aue_softc *);
218184610Salfred
219188412Sthompsastatic int	aue_ifmedia_upd(struct ifnet *);
220188412Sthompsastatic void	aue_ifmedia_sts(struct ifnet *, struct ifmediareq *);
221184610Salfred
222192984Sthompsastatic const struct usb_config aue_config[AUE_N_TRANSFER] = {
223184610Salfred
224187259Sthompsa	[AUE_BULK_DT_WR] = {
225184610Salfred		.type = UE_BULK,
226184610Salfred		.endpoint = UE_ADDR_ANY,
227184610Salfred		.direction = UE_DIR_OUT,
228190734Sthompsa		.bufsize = (MCLBYTES + 2),
229190734Sthompsa		.flags = {.pipe_bof = 1,.force_short_xfer = 1,},
230190734Sthompsa		.callback = aue_bulk_write_callback,
231190734Sthompsa		.timeout = 10000,	/* 10 seconds */
232184610Salfred	},
233184610Salfred
234187259Sthompsa	[AUE_BULK_DT_RD] = {
235184610Salfred		.type = UE_BULK,
236184610Salfred		.endpoint = UE_ADDR_ANY,
237184610Salfred		.direction = UE_DIR_IN,
238190734Sthompsa		.bufsize = (MCLBYTES + 4 + ETHER_CRC_LEN),
239190734Sthompsa		.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
240190734Sthompsa		.callback = aue_bulk_read_callback,
241184610Salfred	},
242184610Salfred
243187259Sthompsa	[AUE_INTR_DT_RD] = {
244184610Salfred		.type = UE_INTERRUPT,
245184610Salfred		.endpoint = UE_ADDR_ANY,
246184610Salfred		.direction = UE_DIR_IN,
247190734Sthompsa		.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
248190734Sthompsa		.bufsize = 0,	/* use wMaxPacketSize */
249190734Sthompsa		.callback = aue_intr_callback,
250184610Salfred	},
251184610Salfred};
252184610Salfred
253184610Salfredstatic device_method_t aue_methods[] = {
254184610Salfred	/* Device interface */
255184610Salfred	DEVMETHOD(device_probe, aue_probe),
256184610Salfred	DEVMETHOD(device_attach, aue_attach),
257184610Salfred	DEVMETHOD(device_detach, aue_detach),
258184610Salfred
259184610Salfred	/* MII interface */
260188412Sthompsa	DEVMETHOD(miibus_readreg, aue_miibus_readreg),
261188412Sthompsa	DEVMETHOD(miibus_writereg, aue_miibus_writereg),
262188412Sthompsa	DEVMETHOD(miibus_statchg, aue_miibus_statchg),
263184610Salfred
264227843Smarius	DEVMETHOD_END
265184610Salfred};
266184610Salfred
267184610Salfredstatic driver_t aue_driver = {
268184610Salfred	.name = "aue",
269184610Salfred	.methods = aue_methods,
270184610Salfred	.size = sizeof(struct aue_softc)
271184610Salfred};
272184610Salfred
273184610Salfredstatic devclass_t aue_devclass;
274184610Salfred
275189275SthompsaDRIVER_MODULE(aue, uhub, aue_driver, aue_devclass, NULL, 0);
276184610SalfredDRIVER_MODULE(miibus, aue, miibus_driver, miibus_devclass, 0, 0);
277188942SthompsaMODULE_DEPEND(aue, uether, 1, 1, 1);
278188942SthompsaMODULE_DEPEND(aue, usb, 1, 1, 1);
279188412SthompsaMODULE_DEPEND(aue, ether, 1, 1, 1);
280188412SthompsaMODULE_DEPEND(aue, miibus, 1, 1, 1);
281212122SthompsaMODULE_VERSION(aue, 1);
282292080SimpUSB_PNP_HOST_INFO(aue_devs);
283184610Salfred
284192984Sthompsastatic const struct usb_ether_methods aue_ue_methods = {
285188412Sthompsa	.ue_attach_post = aue_attach_post,
286188412Sthompsa	.ue_start = aue_start,
287188412Sthompsa	.ue_init = aue_init,
288188412Sthompsa	.ue_stop = aue_stop,
289188412Sthompsa	.ue_tick = aue_tick,
290188412Sthompsa	.ue_setmulti = aue_setmulti,
291188412Sthompsa	.ue_setpromisc = aue_setpromisc,
292188412Sthompsa	.ue_mii_upd = aue_ifmedia_upd,
293188412Sthompsa	.ue_mii_sts = aue_ifmedia_sts,
294188412Sthompsa};
295184610Salfred
296188412Sthompsa#define	AUE_SETBIT(sc, reg, x) \
297188412Sthompsa	aue_csr_write_1(sc, reg, aue_csr_read_1(sc, reg) | (x))
298184610Salfred
299188412Sthompsa#define	AUE_CLRBIT(sc, reg, x) \
300188412Sthompsa	aue_csr_write_1(sc, reg, aue_csr_read_1(sc, reg) & ~(x))
301184610Salfred
302184610Salfredstatic uint8_t
303188412Sthompsaaue_csr_read_1(struct aue_softc *sc, uint16_t reg)
304184610Salfred{
305192984Sthompsa	struct usb_device_request req;
306193045Sthompsa	usb_error_t err;
307184610Salfred	uint8_t val;
308184610Salfred
309184610Salfred	req.bmRequestType = UT_READ_VENDOR_DEVICE;
310184610Salfred	req.bRequest = AUE_UR_READREG;
311184610Salfred	USETW(req.wValue, 0);
312184610Salfred	USETW(req.wIndex, reg);
313184610Salfred	USETW(req.wLength, 1);
314184610Salfred
315194228Sthompsa	err = uether_do_request(&sc->sc_ue, &req, &val, 1000);
316188412Sthompsa	if (err)
317188412Sthompsa		return (0);
318184610Salfred	return (val);
319184610Salfred}
320184610Salfred
321184610Salfredstatic uint16_t
322188412Sthompsaaue_csr_read_2(struct aue_softc *sc, uint16_t reg)
323184610Salfred{
324192984Sthompsa	struct usb_device_request req;
325193045Sthompsa	usb_error_t err;
326184610Salfred	uint16_t val;
327184610Salfred
328184610Salfred	req.bmRequestType = UT_READ_VENDOR_DEVICE;
329184610Salfred	req.bRequest = AUE_UR_READREG;
330184610Salfred	USETW(req.wValue, 0);
331184610Salfred	USETW(req.wIndex, reg);
332184610Salfred	USETW(req.wLength, 2);
333184610Salfred
334194228Sthompsa	err = uether_do_request(&sc->sc_ue, &req, &val, 1000);
335188412Sthompsa	if (err)
336188412Sthompsa		return (0);
337184610Salfred	return (le16toh(val));
338184610Salfred}
339184610Salfred
340184610Salfredstatic void
341188412Sthompsaaue_csr_write_1(struct aue_softc *sc, uint16_t reg, uint8_t val)
342184610Salfred{
343192984Sthompsa	struct usb_device_request req;
344184610Salfred
345184610Salfred	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
346184610Salfred	req.bRequest = AUE_UR_WRITEREG;
347184610Salfred	req.wValue[0] = val;
348184610Salfred	req.wValue[1] = 0;
349184610Salfred	USETW(req.wIndex, reg);
350184610Salfred	USETW(req.wLength, 1);
351184610Salfred
352194228Sthompsa	if (uether_do_request(&sc->sc_ue, &req, &val, 1000)) {
353188412Sthompsa		/* error ignored */
354188412Sthompsa	}
355184610Salfred}
356184610Salfred
357184610Salfredstatic void
358188412Sthompsaaue_csr_write_2(struct aue_softc *sc, uint16_t reg, uint16_t val)
359184610Salfred{
360192984Sthompsa	struct usb_device_request req;
361184610Salfred
362184610Salfred	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
363184610Salfred	req.bRequest = AUE_UR_WRITEREG;
364184610Salfred	USETW(req.wValue, val);
365184610Salfred	USETW(req.wIndex, reg);
366184610Salfred	USETW(req.wLength, 2);
367184610Salfred
368184610Salfred	val = htole16(val);
369184610Salfred
370194228Sthompsa	if (uether_do_request(&sc->sc_ue, &req, &val, 1000)) {
371188412Sthompsa		/* error ignored */
372188412Sthompsa	}
373184610Salfred}
374184610Salfred
375184610Salfred/*
376184610Salfred * Read a word of data stored in the EEPROM at address 'addr.'
377184610Salfred */
378264062Syongaristatic uint16_t
379264062Syongariaue_eeprom_getword(struct aue_softc *sc, int addr)
380184610Salfred{
381188412Sthompsa	int i;
382184610Salfred
383188412Sthompsa	aue_csr_write_1(sc, AUE_EE_REG, addr);
384188412Sthompsa	aue_csr_write_1(sc, AUE_EE_CTL, AUE_EECTL_READ);
385184610Salfred
386188412Sthompsa	for (i = 0; i != AUE_TIMEOUT; i++) {
387188412Sthompsa		if (aue_csr_read_1(sc, AUE_EE_CTL) & AUE_EECTL_DONE)
388184610Salfred			break;
389194228Sthompsa		if (uether_pause(&sc->sc_ue, hz / 100))
390188412Sthompsa			break;
391184610Salfred	}
392184610Salfred
393188412Sthompsa	if (i == AUE_TIMEOUT)
394188412Sthompsa		device_printf(sc->sc_ue.ue_dev, "EEPROM read timed out\n");
395184610Salfred
396264062Syongari	return (aue_csr_read_2(sc, AUE_EE_DATA));
397184610Salfred}
398184610Salfred
399184610Salfred/*
400264062Syongari * Read station address(offset 0) from the EEPROM.
401184610Salfred */
402184610Salfredstatic void
403264062Syongariaue_read_mac(struct aue_softc *sc, uint8_t *eaddr)
404184610Salfred{
405264062Syongari	int i, offset;
406264062Syongari	uint16_t word;
407184610Salfred
408264062Syongari	for (i = 0, offset = 0; i < ETHER_ADDR_LEN / 2; i++) {
409264062Syongari		word = aue_eeprom_getword(sc, offset + i);
410264062Syongari		eaddr[i * 2] = (uint8_t)word;
411264062Syongari		eaddr[i * 2 + 1] = (uint8_t)(word >> 8);
412264062Syongari	}
413184610Salfred}
414184610Salfred
415184610Salfredstatic int
416188412Sthompsaaue_miibus_readreg(device_t dev, int phy, int reg)
417184610Salfred{
418184610Salfred	struct aue_softc *sc = device_get_softc(dev);
419188412Sthompsa	int i, locked;
420188412Sthompsa	uint16_t val = 0;
421184610Salfred
422188412Sthompsa	locked = mtx_owned(&sc->sc_mtx);
423188412Sthompsa	if (!locked)
424188412Sthompsa		AUE_LOCK(sc);
425184610Salfred
426184610Salfred	/*
427188412Sthompsa	 * The Am79C901 HomePNA PHY actually contains two transceivers: a 1Mbps
428188412Sthompsa	 * HomePNA PHY and a 10Mbps full/half duplex ethernet PHY with NWAY
429188412Sthompsa	 * autoneg. However in the ADMtek adapter, only the 1Mbps PHY is
430188412Sthompsa	 * actually connected to anything, so we ignore the 10Mbps one. It
431188412Sthompsa	 * happens to be configured for MII address 3, so we filter that out.
432184610Salfred	 */
433184610Salfred	if (sc->sc_flags & AUE_FLAG_DUAL_PHY) {
434188412Sthompsa		if (phy == 3)
435184610Salfred			goto done;
436184610Salfred#if 0
437188412Sthompsa		if (phy != 1)
438184610Salfred			goto done;
439184610Salfred#endif
440184610Salfred	}
441188412Sthompsa	aue_csr_write_1(sc, AUE_PHY_ADDR, phy);
442188412Sthompsa	aue_csr_write_1(sc, AUE_PHY_CTL, reg | AUE_PHYCTL_READ);
443184610Salfred
444188412Sthompsa	for (i = 0; i != AUE_TIMEOUT; i++) {
445188412Sthompsa		if (aue_csr_read_1(sc, AUE_PHY_CTL) & AUE_PHYCTL_DONE)
446184610Salfred			break;
447194228Sthompsa		if (uether_pause(&sc->sc_ue, hz / 100))
448188412Sthompsa			break;
449184610Salfred	}
450184610Salfred
451188412Sthompsa	if (i == AUE_TIMEOUT)
452188412Sthompsa		device_printf(sc->sc_ue.ue_dev, "MII read timed out\n");
453184610Salfred
454188412Sthompsa	val = aue_csr_read_2(sc, AUE_PHY_DATA);
455188412Sthompsa
456184610Salfreddone:
457188412Sthompsa	if (!locked)
458188412Sthompsa		AUE_UNLOCK(sc);
459188412Sthompsa	return (val);
460184610Salfred}
461184610Salfred
462184610Salfredstatic int
463188412Sthompsaaue_miibus_writereg(device_t dev, int phy, int reg, int data)
464184610Salfred{
465184610Salfred	struct aue_softc *sc = device_get_softc(dev);
466188412Sthompsa	int i;
467188412Sthompsa	int locked;
468184610Salfred
469188412Sthompsa	if (phy == 3)
470184610Salfred		return (0);
471184610Salfred
472188412Sthompsa	locked = mtx_owned(&sc->sc_mtx);
473188412Sthompsa	if (!locked)
474188412Sthompsa		AUE_LOCK(sc);
475184610Salfred
476188412Sthompsa	aue_csr_write_2(sc, AUE_PHY_DATA, data);
477188412Sthompsa	aue_csr_write_1(sc, AUE_PHY_ADDR, phy);
478188412Sthompsa	aue_csr_write_1(sc, AUE_PHY_CTL, reg | AUE_PHYCTL_WRITE);
479184610Salfred
480188412Sthompsa	for (i = 0; i != AUE_TIMEOUT; i++) {
481188412Sthompsa		if (aue_csr_read_1(sc, AUE_PHY_CTL) & AUE_PHYCTL_DONE)
482184610Salfred			break;
483194228Sthompsa		if (uether_pause(&sc->sc_ue, hz / 100))
484188412Sthompsa			break;
485184610Salfred	}
486184610Salfred
487188412Sthompsa	if (i == AUE_TIMEOUT)
488196491Salfred		device_printf(sc->sc_ue.ue_dev, "MII write timed out\n");
489188412Sthompsa
490188412Sthompsa	if (!locked)
491188412Sthompsa		AUE_UNLOCK(sc);
492184610Salfred	return (0);
493184610Salfred}
494184610Salfred
495184610Salfredstatic void
496188412Sthompsaaue_miibus_statchg(device_t dev)
497184610Salfred{
498184610Salfred	struct aue_softc *sc = device_get_softc(dev);
499184610Salfred	struct mii_data *mii = GET_MII(sc);
500188412Sthompsa	int locked;
501184610Salfred
502188412Sthompsa	locked = mtx_owned(&sc->sc_mtx);
503188412Sthompsa	if (!locked)
504188412Sthompsa		AUE_LOCK(sc);
505184610Salfred
506188412Sthompsa	AUE_CLRBIT(sc, AUE_CTL0, AUE_CTL0_RX_ENB | AUE_CTL0_TX_ENB);
507188412Sthompsa	if (IFM_SUBTYPE(mii->mii_media_active) == IFM_100_TX)
508188412Sthompsa		AUE_SETBIT(sc, AUE_CTL1, AUE_CTL1_SPEEDSEL);
509188412Sthompsa	else
510188412Sthompsa		AUE_CLRBIT(sc, AUE_CTL1, AUE_CTL1_SPEEDSEL);
511184610Salfred
512188412Sthompsa	if ((mii->mii_media_active & IFM_GMASK) == IFM_FDX)
513188412Sthompsa		AUE_SETBIT(sc, AUE_CTL1, AUE_CTL1_DUPLEX);
514188412Sthompsa	else
515188412Sthompsa		AUE_CLRBIT(sc, AUE_CTL1, AUE_CTL1_DUPLEX);
516184610Salfred
517188412Sthompsa	AUE_SETBIT(sc, AUE_CTL0, AUE_CTL0_RX_ENB | AUE_CTL0_TX_ENB);
518184610Salfred
519184610Salfred	/*
520184610Salfred	 * Set the LED modes on the LinkSys adapter.
521184610Salfred	 * This turns on the 'dual link LED' bin in the auxmode
522184610Salfred	 * register of the Broadcom PHY.
523184610Salfred	 */
524184610Salfred	if (sc->sc_flags & AUE_FLAG_LSYS) {
525184610Salfred		uint16_t auxmode;
526184610Salfred
527188412Sthompsa		auxmode = aue_miibus_readreg(dev, 0, 0x1b);
528188412Sthompsa		aue_miibus_writereg(dev, 0, 0x1b, auxmode | 0x04);
529184610Salfred	}
530188412Sthompsa	if (!locked)
531188412Sthompsa		AUE_UNLOCK(sc);
532184610Salfred}
533184610Salfred
534188412Sthompsa#define	AUE_BITS	6
535184610Salfredstatic void
536192984Sthompsaaue_setmulti(struct usb_ether *ue)
537184610Salfred{
538194228Sthompsa	struct aue_softc *sc = uether_getsc(ue);
539194228Sthompsa	struct ifnet *ifp = uether_getifp(ue);
540188412Sthompsa	struct ifmultiaddr *ifma;
541188412Sthompsa	uint32_t h = 0;
542188412Sthompsa	uint32_t i;
543188412Sthompsa	uint8_t hashtbl[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
544184610Salfred
545188412Sthompsa	AUE_LOCK_ASSERT(sc, MA_OWNED);
546188412Sthompsa
547188412Sthompsa	if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) {
548188412Sthompsa		AUE_SETBIT(sc, AUE_CTL0, AUE_CTL0_ALLMULTI);
549184610Salfred		return;
550184610Salfred	}
551184610Salfred
552188412Sthompsa	AUE_CLRBIT(sc, AUE_CTL0, AUE_CTL0_ALLMULTI);
553184610Salfred
554184610Salfred	/* now program new ones */
555195049Srwatson	if_maddr_rlock(ifp);
556188412Sthompsa	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
557188412Sthompsa		if (ifma->ifma_addr->sa_family != AF_LINK)
558188412Sthompsa			continue;
559188412Sthompsa		h = ether_crc32_le(LLADDR((struct sockaddr_dl *)
560188412Sthompsa		    ifma->ifma_addr), ETHER_ADDR_LEN) & ((1 << AUE_BITS) - 1);
561188412Sthompsa		hashtbl[(h >> 3)] |=  1 << (h & 0x7);
562184610Salfred	}
563195049Srwatson	if_maddr_runlock(ifp);
564188412Sthompsa
565188412Sthompsa	/* write the hashtable */
566188412Sthompsa	for (i = 0; i != 8; i++)
567188412Sthompsa		aue_csr_write_1(sc, AUE_MAR0 + i, hashtbl[i]);
568184610Salfred}
569184610Salfred
570184610Salfredstatic void
571188412Sthompsaaue_reset_pegasus_II(struct aue_softc *sc)
572184610Salfred{
573184610Salfred	/* Magic constants taken from Linux driver. */
574188412Sthompsa	aue_csr_write_1(sc, AUE_REG_1D, 0);
575188412Sthompsa	aue_csr_write_1(sc, AUE_REG_7B, 2);
576184610Salfred#if 0
577184610Salfred	if ((sc->sc_flags & HAS_HOME_PNA) && mii_mode)
578188412Sthompsa		aue_csr_write_1(sc, AUE_REG_81, 6);
579184610Salfred	else
580184610Salfred#endif
581188412Sthompsa		aue_csr_write_1(sc, AUE_REG_81, 2);
582184610Salfred}
583184610Salfred
584184610Salfredstatic void
585188412Sthompsaaue_reset(struct aue_softc *sc)
586184610Salfred{
587188412Sthompsa	int i;
588184610Salfred
589188412Sthompsa	AUE_SETBIT(sc, AUE_CTL1, AUE_CTL1_RESETMAC);
590184610Salfred
591188412Sthompsa	for (i = 0; i != AUE_TIMEOUT; i++) {
592188412Sthompsa		if (!(aue_csr_read_1(sc, AUE_CTL1) & AUE_CTL1_RESETMAC))
593184610Salfred			break;
594194228Sthompsa		if (uether_pause(&sc->sc_ue, hz / 100))
595188412Sthompsa			break;
596184610Salfred	}
597184610Salfred
598188412Sthompsa	if (i == AUE_TIMEOUT)
599188412Sthompsa		device_printf(sc->sc_ue.ue_dev, "reset failed\n");
600188412Sthompsa
601184610Salfred	/*
602184610Salfred	 * The PHY(s) attached to the Pegasus chip may be held
603184610Salfred	 * in reset until we flip on the GPIO outputs. Make sure
604184610Salfred	 * to set the GPIO pins high so that the PHY(s) will
605184610Salfred	 * be enabled.
606184610Salfred	 *
607196491Salfred	 * NOTE: We used to force all of the GPIO pins low first and then
608196491Salfred	 * enable the ones we want. This has been changed to better
609196491Salfred	 * match the ADMtek's reference design to avoid setting the
610196491Salfred	 * power-down configuration line of the PHY at the same time
611196491Salfred	 * it is reset.
612184610Salfred	 */
613196491Salfred	aue_csr_write_1(sc, AUE_GPIO0, AUE_GPIO_SEL0|AUE_GPIO_SEL1);
614196491Salfred	aue_csr_write_1(sc, AUE_GPIO0, AUE_GPIO_SEL0|AUE_GPIO_SEL1|AUE_GPIO_OUT0);
615184610Salfred
616184610Salfred	if (sc->sc_flags & AUE_FLAG_LSYS) {
617184610Salfred		/* Grrr. LinkSys has to be different from everyone else. */
618188412Sthompsa		aue_csr_write_1(sc, AUE_GPIO0, AUE_GPIO_SEL0|AUE_GPIO_SEL1);
619188412Sthompsa		aue_csr_write_1(sc, AUE_GPIO0,
620188412Sthompsa		    AUE_GPIO_SEL0|AUE_GPIO_SEL1|AUE_GPIO_OUT0);
621184610Salfred	}
622188412Sthompsa	if (sc->sc_flags & AUE_FLAG_PII)
623188412Sthompsa		aue_reset_pegasus_II(sc);
624188412Sthompsa
625188412Sthompsa	/* Wait a little while for the chip to get its brains in order: */
626194228Sthompsa	uether_pause(&sc->sc_ue, hz / 100);
627184610Salfred}
628184610Salfred
629188412Sthompsastatic void
630192984Sthompsaaue_attach_post(struct usb_ether *ue)
631188412Sthompsa{
632194228Sthompsa	struct aue_softc *sc = uether_getsc(ue);
633188412Sthompsa
634188412Sthompsa	/* reset the adapter */
635188412Sthompsa	aue_reset(sc);
636188412Sthompsa
637188412Sthompsa	/* get station address from the EEPROM */
638264062Syongari	aue_read_mac(sc, ue->ue_eaddr);
639188412Sthompsa}
640188412Sthompsa
641184610Salfred/*
642184610Salfred * Probe for a Pegasus chip.
643184610Salfred */
644184610Salfredstatic int
645184610Salfredaue_probe(device_t dev)
646184610Salfred{
647192984Sthompsa	struct usb_attach_arg *uaa = device_get_ivars(dev);
648184610Salfred
649192499Sthompsa	if (uaa->usb_mode != USB_MODE_HOST)
650184610Salfred		return (ENXIO);
651188412Sthompsa	if (uaa->info.bConfigIndex != AUE_CONFIG_INDEX)
652184610Salfred		return (ENXIO);
653188412Sthompsa	if (uaa->info.bIfaceIndex != AUE_IFACE_IDX)
654184610Salfred		return (ENXIO);
655185290Salfred	/*
656188412Sthompsa	 * Belkin USB Bluetooth dongles of the F8T012xx1 model series conflict
657188412Sthompsa	 * with older Belkin USB2LAN adapters.  Skip if_aue if we detect one of
658188412Sthompsa	 * the devices that look like Bluetooth adapters.
659185290Salfred	 */
660188412Sthompsa	if (uaa->info.idVendor == USB_VENDOR_BELKIN &&
661188412Sthompsa	    uaa->info.idProduct == USB_PRODUCT_BELKIN_F8T012 &&
662188412Sthompsa	    uaa->info.bcdDevice == 0x0413)
663185290Salfred		return (ENXIO);
664188412Sthompsa
665194228Sthompsa	return (usbd_lookup_id_by_uaa(aue_devs, sizeof(aue_devs), uaa));
666184610Salfred}
667184610Salfred
668184610Salfred/*
669184610Salfred * Attach the interface. Allocate softc structures, do ifmedia
670184610Salfred * setup and ethernet/BPF attach.
671184610Salfred */
672184610Salfredstatic int
673184610Salfredaue_attach(device_t dev)
674184610Salfred{
675192984Sthompsa	struct usb_attach_arg *uaa = device_get_ivars(dev);
676184610Salfred	struct aue_softc *sc = device_get_softc(dev);
677192984Sthompsa	struct usb_ether *ue = &sc->sc_ue;
678184610Salfred	uint8_t iface_index;
679188412Sthompsa	int error;
680184610Salfred
681184610Salfred	sc->sc_flags = USB_GET_DRIVER_INFO(uaa);
682184610Salfred
683184610Salfred	if (uaa->info.bcdDevice >= 0x0201) {
684188412Sthompsa		/* XXX currently undocumented */
685188412Sthompsa		sc->sc_flags |= AUE_FLAG_VER_2;
686184610Salfred	}
687188412Sthompsa
688194228Sthompsa	device_set_usb_desc(dev);
689188412Sthompsa	mtx_init(&sc->sc_mtx, device_get_nameunit(dev), NULL, MTX_DEF);
690184610Salfred
691184610Salfred	iface_index = AUE_IFACE_IDX;
692194228Sthompsa	error = usbd_transfer_setup(uaa->device, &iface_index,
693187259Sthompsa	    sc->sc_xfer, aue_config, AUE_N_TRANSFER,
694184610Salfred	    sc, &sc->sc_mtx);
695184610Salfred	if (error) {
696199816Sthompsa		device_printf(dev, "allocating USB transfers failed\n");
697184610Salfred		goto detach;
698184610Salfred	}
699188412Sthompsa
700188412Sthompsa	ue->ue_sc = sc;
701188412Sthompsa	ue->ue_dev = dev;
702188412Sthompsa	ue->ue_udev = uaa->device;
703188412Sthompsa	ue->ue_mtx = &sc->sc_mtx;
704188412Sthompsa	ue->ue_methods = &aue_ue_methods;
705188412Sthompsa
706194228Sthompsa	error = uether_ifattach(ue);
707184610Salfred	if (error) {
708188412Sthompsa		device_printf(dev, "could not attach interface\n");
709184610Salfred		goto detach;
710184610Salfred	}
711184610Salfred	return (0);			/* success */
712184610Salfred
713184610Salfreddetach:
714184610Salfred	aue_detach(dev);
715184610Salfred	return (ENXIO);			/* failure */
716184610Salfred}
717184610Salfred
718184610Salfredstatic int
719184610Salfredaue_detach(device_t dev)
720184610Salfred{
721184610Salfred	struct aue_softc *sc = device_get_softc(dev);
722192984Sthompsa	struct usb_ether *ue = &sc->sc_ue;
723184610Salfred
724194228Sthompsa	usbd_transfer_unsetup(sc->sc_xfer, AUE_N_TRANSFER);
725194228Sthompsa	uether_ifdetach(ue);
726184610Salfred	mtx_destroy(&sc->sc_mtx);
727184610Salfred
728184610Salfred	return (0);
729184610Salfred}
730184610Salfred
731184610Salfredstatic void
732194677Sthompsaaue_intr_callback(struct usb_xfer *xfer, usb_error_t error)
733184610Salfred{
734194677Sthompsa	struct aue_softc *sc = usbd_xfer_softc(xfer);
735194228Sthompsa	struct ifnet *ifp = uether_getifp(&sc->sc_ue);
736184610Salfred	struct aue_intrpkt pkt;
737194677Sthompsa	struct usb_page_cache *pc;
738194677Sthompsa	int actlen;
739184610Salfred
740194677Sthompsa	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
741194677Sthompsa
742184610Salfred	switch (USB_GET_STATE(xfer)) {
743184610Salfred	case USB_ST_TRANSFERRED:
744184610Salfred
745188412Sthompsa		if ((ifp->if_drv_flags & IFF_DRV_RUNNING) &&
746233774Shselasky		    actlen >= (int)sizeof(pkt)) {
747184610Salfred
748194677Sthompsa			pc = usbd_xfer_get_frame(xfer, 0);
749194677Sthompsa			usbd_copy_out(pc, 0, &pkt, sizeof(pkt));
750184610Salfred
751188412Sthompsa			if (pkt.aue_txstat0)
752271832Sglebius				if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
753270992Shselasky			if (pkt.aue_txstat0 & (AUE_TXSTAT0_LATECOLL |
754188412Sthompsa			    AUE_TXSTAT0_EXCESSCOLL))
755271832Sglebius				if_inc_counter(ifp, IFCOUNTER_COLLISIONS, 1);
756184610Salfred		}
757188412Sthompsa		/* FALLTHROUGH */
758184610Salfred	case USB_ST_SETUP:
759188412Sthompsatr_setup:
760194677Sthompsa		usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
761194228Sthompsa		usbd_transfer_submit(xfer);
762184610Salfred		return;
763184610Salfred
764184610Salfred	default:			/* Error */
765194677Sthompsa		if (error != USB_ERR_CANCELLED) {
766188412Sthompsa			/* try to clear stall first */
767194677Sthompsa			usbd_xfer_set_stall(xfer);
768188412Sthompsa			goto tr_setup;
769184610Salfred		}
770184610Salfred		return;
771184610Salfred	}
772184610Salfred}
773184610Salfred
774184610Salfredstatic void
775194677Sthompsaaue_bulk_read_callback(struct usb_xfer *xfer, usb_error_t error)
776184610Salfred{
777194677Sthompsa	struct aue_softc *sc = usbd_xfer_softc(xfer);
778192984Sthompsa	struct usb_ether *ue = &sc->sc_ue;
779194228Sthompsa	struct ifnet *ifp = uether_getifp(ue);
780188412Sthompsa	struct aue_rxpkt stat;
781194677Sthompsa	struct usb_page_cache *pc;
782194677Sthompsa	int actlen;
783184610Salfred
784194677Sthompsa	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
785194677Sthompsa	pc = usbd_xfer_get_frame(xfer, 0);
786194677Sthompsa
787184610Salfred	switch (USB_GET_STATE(xfer)) {
788184610Salfred	case USB_ST_TRANSFERRED:
789194677Sthompsa		DPRINTFN(11, "received %d bytes\n", actlen);
790184610Salfred
791184610Salfred		if (sc->sc_flags & AUE_FLAG_VER_2) {
792184610Salfred
793194677Sthompsa			if (actlen == 0) {
794271832Sglebius				if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
795184610Salfred				goto tr_setup;
796184610Salfred			}
797184610Salfred		} else {
798184610Salfred
799233774Shselasky			if (actlen <= (int)(sizeof(stat) + ETHER_CRC_LEN)) {
800271832Sglebius				if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
801184610Salfred				goto tr_setup;
802184610Salfred			}
803194677Sthompsa			usbd_copy_out(pc, actlen - sizeof(stat), &stat,
804194677Sthompsa			    sizeof(stat));
805184610Salfred
806184610Salfred			/*
807184610Salfred			 * turn off all the non-error bits in the rx status
808184610Salfred			 * word:
809184610Salfred			 */
810188412Sthompsa			stat.aue_rxstat &= AUE_RXSTAT_MASK;
811188412Sthompsa			if (stat.aue_rxstat) {
812271832Sglebius				if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
813184610Salfred				goto tr_setup;
814184610Salfred			}
815184610Salfred			/* No errors; receive the packet. */
816194677Sthompsa			actlen -= (sizeof(stat) + ETHER_CRC_LEN);
817184610Salfred		}
818194677Sthompsa		uether_rxbuf(ue, pc, 0, actlen);
819184610Salfred
820188412Sthompsa		/* FALLTHROUGH */
821184610Salfred	case USB_ST_SETUP:
822184610Salfredtr_setup:
823194677Sthompsa		usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
824194228Sthompsa		usbd_transfer_submit(xfer);
825194228Sthompsa		uether_rxflush(ue);
826184610Salfred		return;
827184610Salfred
828184610Salfred	default:			/* Error */
829188412Sthompsa		DPRINTF("bulk read error, %s\n",
830194677Sthompsa		    usbd_errstr(error));
831188412Sthompsa
832194677Sthompsa		if (error != USB_ERR_CANCELLED) {
833184610Salfred			/* try to clear stall first */
834194677Sthompsa			usbd_xfer_set_stall(xfer);
835188412Sthompsa			goto tr_setup;
836184610Salfred		}
837184610Salfred		return;
838184610Salfred	}
839184610Salfred}
840184610Salfred
841184610Salfredstatic void
842194677Sthompsaaue_bulk_write_callback(struct usb_xfer *xfer, usb_error_t error)
843184610Salfred{
844194677Sthompsa	struct aue_softc *sc = usbd_xfer_softc(xfer);
845194228Sthompsa	struct ifnet *ifp = uether_getifp(&sc->sc_ue);
846194677Sthompsa	struct usb_page_cache *pc;
847184610Salfred	struct mbuf *m;
848184610Salfred	uint8_t buf[2];
849194677Sthompsa	int actlen;
850184610Salfred
851194677Sthompsa	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
852194677Sthompsa	pc = usbd_xfer_get_frame(xfer, 0);
853194677Sthompsa
854184610Salfred	switch (USB_GET_STATE(xfer)) {
855184610Salfred	case USB_ST_TRANSFERRED:
856194677Sthompsa		DPRINTFN(11, "transfer of %d bytes complete\n", actlen);
857271832Sglebius		if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
858184610Salfred
859188412Sthompsa		/* FALLTHROUGH */
860184610Salfred	case USB_ST_SETUP:
861188412Sthompsatr_setup:
862188412Sthompsa		if ((sc->sc_flags & AUE_FLAG_LINK) == 0) {
863184610Salfred			/*
864184610Salfred			 * don't send anything if there is no link !
865184610Salfred			 */
866188412Sthompsa			return;
867184610Salfred		}
868184610Salfred		IFQ_DRV_DEQUEUE(&ifp->if_snd, m);
869184610Salfred
870188412Sthompsa		if (m == NULL)
871188412Sthompsa			return;
872188412Sthompsa		if (m->m_pkthdr.len > MCLBYTES)
873184610Salfred			m->m_pkthdr.len = MCLBYTES;
874184610Salfred		if (sc->sc_flags & AUE_FLAG_VER_2) {
875184610Salfred
876194677Sthompsa			usbd_xfer_set_frame_len(xfer, 0, m->m_pkthdr.len);
877184610Salfred
878194677Sthompsa			usbd_m_copy_in(pc, 0, m, 0, m->m_pkthdr.len);
879184610Salfred
880184610Salfred		} else {
881184610Salfred
882194677Sthompsa			usbd_xfer_set_frame_len(xfer, 0, (m->m_pkthdr.len + 2));
883184610Salfred
884184610Salfred			/*
885188412Sthompsa		         * The ADMtek documentation says that the
886188412Sthompsa		         * packet length is supposed to be specified
887188412Sthompsa		         * in the first two bytes of the transfer,
888188412Sthompsa		         * however it actually seems to ignore this
889188412Sthompsa		         * info and base the frame size on the bulk
890188412Sthompsa		         * transfer length.
891184610Salfred		         */
892184610Salfred			buf[0] = (uint8_t)(m->m_pkthdr.len);
893184610Salfred			buf[1] = (uint8_t)(m->m_pkthdr.len >> 8);
894184610Salfred
895194677Sthompsa			usbd_copy_in(pc, 0, buf, 2);
896194677Sthompsa			usbd_m_copy_in(pc, 2, m, 0, m->m_pkthdr.len);
897184610Salfred		}
898184610Salfred
899184610Salfred		/*
900184610Salfred		 * if there's a BPF listener, bounce a copy
901184610Salfred		 * of this frame to him:
902184610Salfred		 */
903184610Salfred		BPF_MTAP(ifp, m);
904184610Salfred
905184610Salfred		m_freem(m);
906184610Salfred
907194228Sthompsa		usbd_transfer_submit(xfer);
908184610Salfred		return;
909184610Salfred
910184610Salfred	default:			/* Error */
911184610Salfred		DPRINTFN(11, "transfer error, %s\n",
912194677Sthompsa		    usbd_errstr(error));
913184610Salfred
914271832Sglebius		if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
915188412Sthompsa
916194677Sthompsa		if (error != USB_ERR_CANCELLED) {
917184610Salfred			/* try to clear stall first */
918194677Sthompsa			usbd_xfer_set_stall(xfer);
919188412Sthompsa			goto tr_setup;
920184610Salfred		}
921184610Salfred		return;
922184610Salfred	}
923184610Salfred}
924184610Salfred
925184610Salfredstatic void
926192984Sthompsaaue_tick(struct usb_ether *ue)
927184610Salfred{
928194228Sthompsa	struct aue_softc *sc = uether_getsc(ue);
929188412Sthompsa	struct mii_data *mii = GET_MII(sc);
930184610Salfred
931188412Sthompsa	AUE_LOCK_ASSERT(sc, MA_OWNED);
932184610Salfred
933184610Salfred	mii_tick(mii);
934188412Sthompsa	if ((sc->sc_flags & AUE_FLAG_LINK) == 0
935188412Sthompsa	    && mii->mii_media_status & IFM_ACTIVE &&
936188412Sthompsa	    IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
937188412Sthompsa		sc->sc_flags |= AUE_FLAG_LINK;
938188412Sthompsa		aue_start(ue);
939184610Salfred	}
940184610Salfred}
941184610Salfred
942184610Salfredstatic void
943192984Sthompsaaue_start(struct usb_ether *ue)
944184610Salfred{
945194228Sthompsa	struct aue_softc *sc = uether_getsc(ue);
946184610Salfred
947188412Sthompsa	/*
948188412Sthompsa	 * start the USB transfers, if not already started:
949188412Sthompsa	 */
950194228Sthompsa	usbd_transfer_start(sc->sc_xfer[AUE_INTR_DT_RD]);
951194228Sthompsa	usbd_transfer_start(sc->sc_xfer[AUE_BULK_DT_RD]);
952194228Sthompsa	usbd_transfer_start(sc->sc_xfer[AUE_BULK_DT_WR]);
953184610Salfred}
954184610Salfred
955184610Salfredstatic void
956192984Sthompsaaue_init(struct usb_ether *ue)
957184610Salfred{
958194228Sthompsa	struct aue_softc *sc = uether_getsc(ue);
959194228Sthompsa	struct ifnet *ifp = uether_getifp(ue);
960188412Sthompsa	int i;
961184610Salfred
962188412Sthompsa	AUE_LOCK_ASSERT(sc, MA_OWNED);
963184610Salfred
964184610Salfred	/*
965184610Salfred	 * Cancel pending I/O
966184610Salfred	 */
967188412Sthompsa	aue_reset(sc);
968184610Salfred
969184610Salfred	/* Set MAC address */
970188412Sthompsa	for (i = 0; i != ETHER_ADDR_LEN; i++)
971188412Sthompsa		aue_csr_write_1(sc, AUE_PAR0 + i, IF_LLADDR(ifp)[i]);
972184610Salfred
973184610Salfred	/* update promiscuous setting */
974188412Sthompsa	aue_setpromisc(ue);
975184610Salfred
976188412Sthompsa	/* Load the multicast filter. */
977188412Sthompsa	aue_setmulti(ue);
978184610Salfred
979188412Sthompsa	/* Enable RX and TX */
980188412Sthompsa	aue_csr_write_1(sc, AUE_CTL0, AUE_CTL0_RXSTAT_APPEND | AUE_CTL0_RX_ENB);
981188412Sthompsa	AUE_SETBIT(sc, AUE_CTL0, AUE_CTL0_TX_ENB);
982188412Sthompsa	AUE_SETBIT(sc, AUE_CTL2, AUE_CTL2_EP3_CLR);
983184610Salfred
984194677Sthompsa	usbd_xfer_set_stall(sc->sc_xfer[AUE_BULK_DT_WR]);
985184610Salfred
986188412Sthompsa	ifp->if_drv_flags |= IFF_DRV_RUNNING;
987188412Sthompsa	aue_start(ue);
988184610Salfred}
989184610Salfred
990184610Salfredstatic void
991192984Sthompsaaue_setpromisc(struct usb_ether *ue)
992184610Salfred{
993194228Sthompsa	struct aue_softc *sc = uether_getsc(ue);
994194228Sthompsa	struct ifnet *ifp = uether_getifp(ue);
995188412Sthompsa
996188412Sthompsa	AUE_LOCK_ASSERT(sc, MA_OWNED);
997188412Sthompsa
998184610Salfred	/* if we want promiscuous mode, set the allframes bit: */
999188412Sthompsa	if (ifp->if_flags & IFF_PROMISC)
1000188412Sthompsa		AUE_SETBIT(sc, AUE_CTL2, AUE_CTL2_RX_PROMISC);
1001188412Sthompsa	else
1002188412Sthompsa		AUE_CLRBIT(sc, AUE_CTL2, AUE_CTL2_RX_PROMISC);
1003184610Salfred}
1004184610Salfred
1005184610Salfred/*
1006184610Salfred * Set media options.
1007184610Salfred */
1008184610Salfredstatic int
1009188412Sthompsaaue_ifmedia_upd(struct ifnet *ifp)
1010184610Salfred{
1011184610Salfred	struct aue_softc *sc = ifp->if_softc;
1012184610Salfred	struct mii_data *mii = GET_MII(sc);
1013221407Smarius	struct mii_softc *miisc;
1014251734Skevlo	int error;
1015184610Salfred
1016188412Sthompsa	AUE_LOCK_ASSERT(sc, MA_OWNED);
1017184610Salfred
1018188412Sthompsa        sc->sc_flags &= ~AUE_FLAG_LINK;
1019221407Smarius	LIST_FOREACH(miisc, &mii->mii_phys, mii_list)
1020221407Smarius		PHY_RESET(miisc);
1021251734Skevlo	error = mii_mediachg(mii);
1022251734Skevlo	return (error);
1023184610Salfred}
1024184610Salfred
1025184610Salfred/*
1026184610Salfred * Report current media status.
1027184610Salfred */
1028184610Salfredstatic void
1029188412Sthompsaaue_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
1030184610Salfred{
1031184610Salfred	struct aue_softc *sc = ifp->if_softc;
1032188412Sthompsa	struct mii_data *mii = GET_MII(sc);
1033184610Salfred
1034188412Sthompsa	AUE_LOCK(sc);
1035188412Sthompsa	mii_pollstat(mii);
1036188412Sthompsa	ifmr->ifm_active = mii->mii_media_active;
1037188412Sthompsa	ifmr->ifm_status = mii->mii_media_status;
1038226479Syongari	AUE_UNLOCK(sc);
1039184610Salfred}
1040184610Salfred
1041184610Salfred/*
1042184610Salfred * Stop the adapter and free any mbufs allocated to the
1043184610Salfred * RX and TX lists.
1044184610Salfred */
1045184610Salfredstatic void
1046192984Sthompsaaue_stop(struct usb_ether *ue)
1047184610Salfred{
1048194228Sthompsa	struct aue_softc *sc = uether_getsc(ue);
1049194228Sthompsa	struct ifnet *ifp = uether_getifp(ue);
1050184610Salfred
1051188412Sthompsa	AUE_LOCK_ASSERT(sc, MA_OWNED);
1052184610Salfred
1053188412Sthompsa	ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1054188412Sthompsa	sc->sc_flags &= ~AUE_FLAG_LINK;
1055184610Salfred
1056184610Salfred	/*
1057184610Salfred	 * stop all the transfers, if not already stopped:
1058184610Salfred	 */
1059194228Sthompsa	usbd_transfer_stop(sc->sc_xfer[AUE_BULK_DT_WR]);
1060194228Sthompsa	usbd_transfer_stop(sc->sc_xfer[AUE_BULK_DT_RD]);
1061194228Sthompsa	usbd_transfer_stop(sc->sc_xfer[AUE_INTR_DT_RD]);
1062184610Salfred
1063188412Sthompsa	aue_csr_write_1(sc, AUE_CTL0, 0);
1064188412Sthompsa	aue_csr_write_1(sc, AUE_CTL1, 0);
1065188412Sthompsa	aue_reset(sc);
1066184610Salfred}
1067