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>
77194677Sthompsa#include <sys/kernel.h>
78194677Sthompsa#include <sys/bus.h>
79194677Sthompsa#include <sys/module.h>
80194677Sthompsa#include <sys/lock.h>
81194677Sthompsa#include <sys/mutex.h>
82194677Sthompsa#include <sys/condvar.h>
83194677Sthompsa#include <sys/sysctl.h>
84194677Sthompsa#include <sys/sx.h>
85194677Sthompsa#include <sys/unistd.h>
86194677Sthompsa#include <sys/callout.h>
87194677Sthompsa#include <sys/malloc.h>
88194677Sthompsa#include <sys/priv.h>
89194677Sthompsa
90194677Sthompsa#include <dev/usb/usb.h>
91194677Sthompsa#include <dev/usb/usbdi.h>
92194677Sthompsa#include <dev/usb/usbdi_util.h>
93188746Sthompsa#include "usbdevs.h"
94184610Salfred
95184610Salfred#define	USB_DEBUG_VAR aue_debug
96194677Sthompsa#include <dev/usb/usb_debug.h>
97188942Sthompsa#include <dev/usb/usb_process.h>
98184610Salfred
99188942Sthompsa#include <dev/usb/net/usb_ethernet.h>
100188942Sthompsa#include <dev/usb/net/if_auereg.h>
101184610Salfred
102207077Sthompsa#ifdef USB_DEBUG
103184610Salfredstatic int aue_debug = 0;
104184610Salfred
105227309Sedstatic SYSCTL_NODE(_hw_usb, OID_AUTO, aue, CTLFLAG_RW, 0, "USB aue");
106192502SthompsaSYSCTL_INT(_hw_usb_aue, OID_AUTO, debug, CTLFLAG_RW, &aue_debug, 0,
107184610Salfred    "Debug level");
108184610Salfred#endif
109184610Salfred
110184610Salfred/*
111184610Salfred * Various supported device vendors/products.
112184610Salfred */
113223486Shselaskystatic const STRUCT_USB_HOST_ID aue_devs[] = {
114201028Sthompsa#define	AUE_DEV(v,p,i) { USB_VPI(USB_VENDOR_##v, USB_PRODUCT_##v##_##p, i) }
115201028Sthompsa    AUE_DEV(3COM, 3C460B, AUE_FLAG_PII),
116201028Sthompsa    AUE_DEV(ABOCOM, DSB650TX_PNA, 0),
117201028Sthompsa    AUE_DEV(ABOCOM, UFE1000, AUE_FLAG_LSYS),
118201028Sthompsa    AUE_DEV(ABOCOM, XX10, 0),
119201028Sthompsa    AUE_DEV(ABOCOM, XX1, AUE_FLAG_PNA | AUE_FLAG_PII),
120201028Sthompsa    AUE_DEV(ABOCOM, XX2, AUE_FLAG_PII),
121201028Sthompsa    AUE_DEV(ABOCOM, XX4, AUE_FLAG_PNA),
122201028Sthompsa    AUE_DEV(ABOCOM, XX5, AUE_FLAG_PNA),
123201028Sthompsa    AUE_DEV(ABOCOM, XX6, AUE_FLAG_PII),
124201028Sthompsa    AUE_DEV(ABOCOM, XX7, AUE_FLAG_PII),
125201028Sthompsa    AUE_DEV(ABOCOM, XX8, AUE_FLAG_PII),
126201028Sthompsa    AUE_DEV(ABOCOM, XX9, AUE_FLAG_PNA),
127201028Sthompsa    AUE_DEV(ACCTON, SS1001, AUE_FLAG_PII),
128201028Sthompsa    AUE_DEV(ACCTON, USB320_EC, 0),
129201028Sthompsa    AUE_DEV(ADMTEK, PEGASUSII_2, AUE_FLAG_PII),
130201028Sthompsa    AUE_DEV(ADMTEK, PEGASUSII_3, AUE_FLAG_PII),
131201028Sthompsa    AUE_DEV(ADMTEK, PEGASUSII_4, AUE_FLAG_PII),
132201028Sthompsa    AUE_DEV(ADMTEK, PEGASUSII, AUE_FLAG_PII),
133201028Sthompsa    AUE_DEV(ADMTEK, PEGASUS, AUE_FLAG_PNA | AUE_FLAG_DUAL_PHY),
134201028Sthompsa    AUE_DEV(AEI, FASTETHERNET, AUE_FLAG_PII),
135201028Sthompsa    AUE_DEV(ALLIEDTELESYN, ATUSB100, AUE_FLAG_PII),
136201028Sthompsa    AUE_DEV(ATEN, UC110T, AUE_FLAG_PII),
137201028Sthompsa    AUE_DEV(BELKIN, USB2LAN, AUE_FLAG_PII),
138201028Sthompsa    AUE_DEV(BILLIONTON, USB100, 0),
139201028Sthompsa    AUE_DEV(BILLIONTON, USBE100, AUE_FLAG_PII),
140201028Sthompsa    AUE_DEV(BILLIONTON, USBEL100, 0),
141201028Sthompsa    AUE_DEV(BILLIONTON, USBLP100, AUE_FLAG_PNA),
142201028Sthompsa    AUE_DEV(COREGA, FETHER_USB_TXS, AUE_FLAG_PII),
143201028Sthompsa    AUE_DEV(COREGA, FETHER_USB_TX, 0),
144201028Sthompsa    AUE_DEV(DLINK, DSB650TX1, AUE_FLAG_LSYS),
145201028Sthompsa    AUE_DEV(DLINK, DSB650TX2, AUE_FLAG_LSYS | AUE_FLAG_PII),
146201028Sthompsa    AUE_DEV(DLINK, DSB650TX3, AUE_FLAG_LSYS | AUE_FLAG_PII),
147201028Sthompsa    AUE_DEV(DLINK, DSB650TX4, AUE_FLAG_LSYS | AUE_FLAG_PII),
148201028Sthompsa    AUE_DEV(DLINK, DSB650TX_PNA, AUE_FLAG_PNA),
149201028Sthompsa    AUE_DEV(DLINK, DSB650TX, AUE_FLAG_LSYS),
150201028Sthompsa    AUE_DEV(DLINK, DSB650, AUE_FLAG_LSYS),
151201028Sthompsa    AUE_DEV(ELCON, PLAN, AUE_FLAG_PNA | AUE_FLAG_PII),
152201028Sthompsa    AUE_DEV(ELECOM, LDUSB20, AUE_FLAG_PII),
153201028Sthompsa    AUE_DEV(ELECOM, LDUSBLTX, AUE_FLAG_PII),
154201028Sthompsa    AUE_DEV(ELECOM, LDUSBTX0, 0),
155201028Sthompsa    AUE_DEV(ELECOM, LDUSBTX1, AUE_FLAG_LSYS),
156201028Sthompsa    AUE_DEV(ELECOM, LDUSBTX2, 0),
157201028Sthompsa    AUE_DEV(ELECOM, LDUSBTX3, AUE_FLAG_LSYS),
158201028Sthompsa    AUE_DEV(ELSA, USB2ETHERNET, 0),
159201028Sthompsa    AUE_DEV(GIGABYTE, GNBR402W, 0),
160201028Sthompsa    AUE_DEV(HAWKING, UF100, AUE_FLAG_PII),
161201028Sthompsa    AUE_DEV(HP, HN210E, AUE_FLAG_PII),
162201028Sthompsa    AUE_DEV(IODATA, USBETTXS, AUE_FLAG_PII),
163201028Sthompsa    AUE_DEV(IODATA, USBETTX, 0),
164201028Sthompsa    AUE_DEV(KINGSTON, KNU101TX, 0),
165201028Sthompsa    AUE_DEV(LINKSYS, USB100H1, AUE_FLAG_LSYS | AUE_FLAG_PNA),
166201028Sthompsa    AUE_DEV(LINKSYS, USB100TX, AUE_FLAG_LSYS),
167201028Sthompsa    AUE_DEV(LINKSYS, USB10TA, AUE_FLAG_LSYS),
168201028Sthompsa    AUE_DEV(LINKSYS, USB10TX1, AUE_FLAG_LSYS | AUE_FLAG_PII),
169201028Sthompsa    AUE_DEV(LINKSYS, USB10TX2, AUE_FLAG_LSYS | AUE_FLAG_PII),
170201028Sthompsa    AUE_DEV(LINKSYS, USB10T, AUE_FLAG_LSYS),
171201028Sthompsa    AUE_DEV(MELCO, LUA2TX5, AUE_FLAG_PII),
172201028Sthompsa    AUE_DEV(MELCO, LUATX1, 0),
173201028Sthompsa    AUE_DEV(MELCO, LUATX5, 0),
174201028Sthompsa    AUE_DEV(MICROSOFT, MN110, AUE_FLAG_PII),
175201028Sthompsa    AUE_DEV(NETGEAR, FA101, AUE_FLAG_PII),
176201028Sthompsa    AUE_DEV(SIEMENS, SPEEDSTREAM, AUE_FLAG_PII),
177201028Sthompsa    AUE_DEV(SIIG2, USBTOETHER, AUE_FLAG_PII),
178201028Sthompsa    AUE_DEV(SMARTBRIDGES, SMARTNIC, AUE_FLAG_PII),
179201028Sthompsa    AUE_DEV(SMC, 2202USB, 0),
180201028Sthompsa    AUE_DEV(SMC, 2206USB, AUE_FLAG_PII),
181201028Sthompsa    AUE_DEV(SOHOWARE, NUB100, 0),
182201028Sthompsa    AUE_DEV(SOHOWARE, NUB110, AUE_FLAG_PII),
183201028Sthompsa#undef AUE_DEV
184184610Salfred};
185184610Salfred
186184610Salfred/* prototypes */
187184610Salfred
188184610Salfredstatic device_probe_t aue_probe;
189184610Salfredstatic device_attach_t aue_attach;
190184610Salfredstatic device_detach_t aue_detach;
191188412Sthompsastatic miibus_readreg_t aue_miibus_readreg;
192188412Sthompsastatic miibus_writereg_t aue_miibus_writereg;
193188412Sthompsastatic miibus_statchg_t aue_miibus_statchg;
194184610Salfred
195193045Sthompsastatic usb_callback_t aue_intr_callback;
196193045Sthompsastatic usb_callback_t aue_bulk_read_callback;
197193045Sthompsastatic usb_callback_t aue_bulk_write_callback;
198184610Salfred
199193045Sthompsastatic uether_fn_t aue_attach_post;
200193045Sthompsastatic uether_fn_t aue_init;
201193045Sthompsastatic uether_fn_t aue_stop;
202193045Sthompsastatic uether_fn_t aue_start;
203193045Sthompsastatic uether_fn_t aue_tick;
204193045Sthompsastatic uether_fn_t aue_setmulti;
205193045Sthompsastatic uether_fn_t aue_setpromisc;
206188412Sthompsa
207188412Sthompsastatic uint8_t	aue_csr_read_1(struct aue_softc *, uint16_t);
208188412Sthompsastatic uint16_t	aue_csr_read_2(struct aue_softc *, uint16_t);
209188412Sthompsastatic void	aue_csr_write_1(struct aue_softc *, uint16_t, uint8_t);
210188412Sthompsastatic void	aue_csr_write_2(struct aue_softc *, uint16_t, uint16_t);
211264745Syongaristatic uint16_t	aue_eeprom_getword(struct aue_softc *, int);
212188412Sthompsastatic void	aue_reset(struct aue_softc *);
213188412Sthompsastatic void	aue_reset_pegasus_II(struct aue_softc *);
214184610Salfred
215188412Sthompsastatic int	aue_ifmedia_upd(struct ifnet *);
216188412Sthompsastatic void	aue_ifmedia_sts(struct ifnet *, struct ifmediareq *);
217184610Salfred
218192984Sthompsastatic const struct usb_config aue_config[AUE_N_TRANSFER] = {
219184610Salfred
220187259Sthompsa	[AUE_BULK_DT_WR] = {
221184610Salfred		.type = UE_BULK,
222184610Salfred		.endpoint = UE_ADDR_ANY,
223184610Salfred		.direction = UE_DIR_OUT,
224190734Sthompsa		.bufsize = (MCLBYTES + 2),
225190734Sthompsa		.flags = {.pipe_bof = 1,.force_short_xfer = 1,},
226190734Sthompsa		.callback = aue_bulk_write_callback,
227190734Sthompsa		.timeout = 10000,	/* 10 seconds */
228184610Salfred	},
229184610Salfred
230187259Sthompsa	[AUE_BULK_DT_RD] = {
231184610Salfred		.type = UE_BULK,
232184610Salfred		.endpoint = UE_ADDR_ANY,
233184610Salfred		.direction = UE_DIR_IN,
234190734Sthompsa		.bufsize = (MCLBYTES + 4 + ETHER_CRC_LEN),
235190734Sthompsa		.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
236190734Sthompsa		.callback = aue_bulk_read_callback,
237184610Salfred	},
238184610Salfred
239187259Sthompsa	[AUE_INTR_DT_RD] = {
240184610Salfred		.type = UE_INTERRUPT,
241184610Salfred		.endpoint = UE_ADDR_ANY,
242184610Salfred		.direction = UE_DIR_IN,
243190734Sthompsa		.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
244190734Sthompsa		.bufsize = 0,	/* use wMaxPacketSize */
245190734Sthompsa		.callback = aue_intr_callback,
246184610Salfred	},
247184610Salfred};
248184610Salfred
249184610Salfredstatic device_method_t aue_methods[] = {
250184610Salfred	/* Device interface */
251184610Salfred	DEVMETHOD(device_probe, aue_probe),
252184610Salfred	DEVMETHOD(device_attach, aue_attach),
253184610Salfred	DEVMETHOD(device_detach, aue_detach),
254184610Salfred
255184610Salfred	/* MII interface */
256188412Sthompsa	DEVMETHOD(miibus_readreg, aue_miibus_readreg),
257188412Sthompsa	DEVMETHOD(miibus_writereg, aue_miibus_writereg),
258188412Sthompsa	DEVMETHOD(miibus_statchg, aue_miibus_statchg),
259184610Salfred
260227843Smarius	DEVMETHOD_END
261184610Salfred};
262184610Salfred
263184610Salfredstatic driver_t aue_driver = {
264184610Salfred	.name = "aue",
265184610Salfred	.methods = aue_methods,
266184610Salfred	.size = sizeof(struct aue_softc)
267184610Salfred};
268184610Salfred
269184610Salfredstatic devclass_t aue_devclass;
270184610Salfred
271189275SthompsaDRIVER_MODULE(aue, uhub, aue_driver, aue_devclass, NULL, 0);
272184610SalfredDRIVER_MODULE(miibus, aue, miibus_driver, miibus_devclass, 0, 0);
273188942SthompsaMODULE_DEPEND(aue, uether, 1, 1, 1);
274188942SthompsaMODULE_DEPEND(aue, usb, 1, 1, 1);
275188412SthompsaMODULE_DEPEND(aue, ether, 1, 1, 1);
276188412SthompsaMODULE_DEPEND(aue, miibus, 1, 1, 1);
277212122SthompsaMODULE_VERSION(aue, 1);
278184610Salfred
279192984Sthompsastatic const struct usb_ether_methods aue_ue_methods = {
280188412Sthompsa	.ue_attach_post = aue_attach_post,
281188412Sthompsa	.ue_start = aue_start,
282188412Sthompsa	.ue_init = aue_init,
283188412Sthompsa	.ue_stop = aue_stop,
284188412Sthompsa	.ue_tick = aue_tick,
285188412Sthompsa	.ue_setmulti = aue_setmulti,
286188412Sthompsa	.ue_setpromisc = aue_setpromisc,
287188412Sthompsa	.ue_mii_upd = aue_ifmedia_upd,
288188412Sthompsa	.ue_mii_sts = aue_ifmedia_sts,
289188412Sthompsa};
290184610Salfred
291188412Sthompsa#define	AUE_SETBIT(sc, reg, x) \
292188412Sthompsa	aue_csr_write_1(sc, reg, aue_csr_read_1(sc, reg) | (x))
293184610Salfred
294188412Sthompsa#define	AUE_CLRBIT(sc, reg, x) \
295188412Sthompsa	aue_csr_write_1(sc, reg, aue_csr_read_1(sc, reg) & ~(x))
296184610Salfred
297184610Salfredstatic uint8_t
298188412Sthompsaaue_csr_read_1(struct aue_softc *sc, uint16_t reg)
299184610Salfred{
300192984Sthompsa	struct usb_device_request req;
301193045Sthompsa	usb_error_t err;
302184610Salfred	uint8_t val;
303184610Salfred
304184610Salfred	req.bmRequestType = UT_READ_VENDOR_DEVICE;
305184610Salfred	req.bRequest = AUE_UR_READREG;
306184610Salfred	USETW(req.wValue, 0);
307184610Salfred	USETW(req.wIndex, reg);
308184610Salfred	USETW(req.wLength, 1);
309184610Salfred
310194228Sthompsa	err = uether_do_request(&sc->sc_ue, &req, &val, 1000);
311188412Sthompsa	if (err)
312188412Sthompsa		return (0);
313184610Salfred	return (val);
314184610Salfred}
315184610Salfred
316184610Salfredstatic uint16_t
317188412Sthompsaaue_csr_read_2(struct aue_softc *sc, uint16_t reg)
318184610Salfred{
319192984Sthompsa	struct usb_device_request req;
320193045Sthompsa	usb_error_t err;
321184610Salfred	uint16_t val;
322184610Salfred
323184610Salfred	req.bmRequestType = UT_READ_VENDOR_DEVICE;
324184610Salfred	req.bRequest = AUE_UR_READREG;
325184610Salfred	USETW(req.wValue, 0);
326184610Salfred	USETW(req.wIndex, reg);
327184610Salfred	USETW(req.wLength, 2);
328184610Salfred
329194228Sthompsa	err = uether_do_request(&sc->sc_ue, &req, &val, 1000);
330188412Sthompsa	if (err)
331188412Sthompsa		return (0);
332184610Salfred	return (le16toh(val));
333184610Salfred}
334184610Salfred
335184610Salfredstatic void
336188412Sthompsaaue_csr_write_1(struct aue_softc *sc, uint16_t reg, uint8_t val)
337184610Salfred{
338192984Sthompsa	struct usb_device_request req;
339184610Salfred
340184610Salfred	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
341184610Salfred	req.bRequest = AUE_UR_WRITEREG;
342184610Salfred	req.wValue[0] = val;
343184610Salfred	req.wValue[1] = 0;
344184610Salfred	USETW(req.wIndex, reg);
345184610Salfred	USETW(req.wLength, 1);
346184610Salfred
347194228Sthompsa	if (uether_do_request(&sc->sc_ue, &req, &val, 1000)) {
348188412Sthompsa		/* error ignored */
349188412Sthompsa	}
350184610Salfred}
351184610Salfred
352184610Salfredstatic void
353188412Sthompsaaue_csr_write_2(struct aue_softc *sc, uint16_t reg, uint16_t val)
354184610Salfred{
355192984Sthompsa	struct usb_device_request req;
356184610Salfred
357184610Salfred	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
358184610Salfred	req.bRequest = AUE_UR_WRITEREG;
359184610Salfred	USETW(req.wValue, val);
360184610Salfred	USETW(req.wIndex, reg);
361184610Salfred	USETW(req.wLength, 2);
362184610Salfred
363184610Salfred	val = htole16(val);
364184610Salfred
365194228Sthompsa	if (uether_do_request(&sc->sc_ue, &req, &val, 1000)) {
366188412Sthompsa		/* error ignored */
367188412Sthompsa	}
368184610Salfred}
369184610Salfred
370184610Salfred/*
371184610Salfred * Read a word of data stored in the EEPROM at address 'addr.'
372184610Salfred */
373264745Syongaristatic uint16_t
374264745Syongariaue_eeprom_getword(struct aue_softc *sc, int addr)
375184610Salfred{
376188412Sthompsa	int i;
377184610Salfred
378188412Sthompsa	aue_csr_write_1(sc, AUE_EE_REG, addr);
379188412Sthompsa	aue_csr_write_1(sc, AUE_EE_CTL, AUE_EECTL_READ);
380184610Salfred
381188412Sthompsa	for (i = 0; i != AUE_TIMEOUT; i++) {
382188412Sthompsa		if (aue_csr_read_1(sc, AUE_EE_CTL) & AUE_EECTL_DONE)
383184610Salfred			break;
384194228Sthompsa		if (uether_pause(&sc->sc_ue, hz / 100))
385188412Sthompsa			break;
386184610Salfred	}
387184610Salfred
388188412Sthompsa	if (i == AUE_TIMEOUT)
389188412Sthompsa		device_printf(sc->sc_ue.ue_dev, "EEPROM read timed out\n");
390184610Salfred
391264745Syongari	return (aue_csr_read_2(sc, AUE_EE_DATA));
392184610Salfred}
393184610Salfred
394184610Salfred/*
395264745Syongari * Read station address(offset 0) from the EEPROM.
396184610Salfred */
397184610Salfredstatic void
398264745Syongariaue_read_mac(struct aue_softc *sc, uint8_t *eaddr)
399184610Salfred{
400264745Syongari	int i, offset;
401264745Syongari	uint16_t word;
402184610Salfred
403264745Syongari	for (i = 0, offset = 0; i < ETHER_ADDR_LEN / 2; i++) {
404264745Syongari		word = aue_eeprom_getword(sc, offset + i);
405264745Syongari		eaddr[i * 2] = (uint8_t)word;
406264745Syongari		eaddr[i * 2 + 1] = (uint8_t)(word >> 8);
407264745Syongari	}
408184610Salfred}
409184610Salfred
410184610Salfredstatic int
411188412Sthompsaaue_miibus_readreg(device_t dev, int phy, int reg)
412184610Salfred{
413184610Salfred	struct aue_softc *sc = device_get_softc(dev);
414188412Sthompsa	int i, locked;
415188412Sthompsa	uint16_t val = 0;
416184610Salfred
417188412Sthompsa	locked = mtx_owned(&sc->sc_mtx);
418188412Sthompsa	if (!locked)
419188412Sthompsa		AUE_LOCK(sc);
420184610Salfred
421184610Salfred	/*
422188412Sthompsa	 * The Am79C901 HomePNA PHY actually contains two transceivers: a 1Mbps
423188412Sthompsa	 * HomePNA PHY and a 10Mbps full/half duplex ethernet PHY with NWAY
424188412Sthompsa	 * autoneg. However in the ADMtek adapter, only the 1Mbps PHY is
425188412Sthompsa	 * actually connected to anything, so we ignore the 10Mbps one. It
426188412Sthompsa	 * happens to be configured for MII address 3, so we filter that out.
427184610Salfred	 */
428184610Salfred	if (sc->sc_flags & AUE_FLAG_DUAL_PHY) {
429188412Sthompsa		if (phy == 3)
430184610Salfred			goto done;
431184610Salfred#if 0
432188412Sthompsa		if (phy != 1)
433184610Salfred			goto done;
434184610Salfred#endif
435184610Salfred	}
436188412Sthompsa	aue_csr_write_1(sc, AUE_PHY_ADDR, phy);
437188412Sthompsa	aue_csr_write_1(sc, AUE_PHY_CTL, reg | AUE_PHYCTL_READ);
438184610Salfred
439188412Sthompsa	for (i = 0; i != AUE_TIMEOUT; i++) {
440188412Sthompsa		if (aue_csr_read_1(sc, AUE_PHY_CTL) & AUE_PHYCTL_DONE)
441184610Salfred			break;
442194228Sthompsa		if (uether_pause(&sc->sc_ue, hz / 100))
443188412Sthompsa			break;
444184610Salfred	}
445184610Salfred
446188412Sthompsa	if (i == AUE_TIMEOUT)
447188412Sthompsa		device_printf(sc->sc_ue.ue_dev, "MII read timed out\n");
448184610Salfred
449188412Sthompsa	val = aue_csr_read_2(sc, AUE_PHY_DATA);
450188412Sthompsa
451184610Salfreddone:
452188412Sthompsa	if (!locked)
453188412Sthompsa		AUE_UNLOCK(sc);
454188412Sthompsa	return (val);
455184610Salfred}
456184610Salfred
457184610Salfredstatic int
458188412Sthompsaaue_miibus_writereg(device_t dev, int phy, int reg, int data)
459184610Salfred{
460184610Salfred	struct aue_softc *sc = device_get_softc(dev);
461188412Sthompsa	int i;
462188412Sthompsa	int locked;
463184610Salfred
464188412Sthompsa	if (phy == 3)
465184610Salfred		return (0);
466184610Salfred
467188412Sthompsa	locked = mtx_owned(&sc->sc_mtx);
468188412Sthompsa	if (!locked)
469188412Sthompsa		AUE_LOCK(sc);
470184610Salfred
471188412Sthompsa	aue_csr_write_2(sc, AUE_PHY_DATA, data);
472188412Sthompsa	aue_csr_write_1(sc, AUE_PHY_ADDR, phy);
473188412Sthompsa	aue_csr_write_1(sc, AUE_PHY_CTL, reg | AUE_PHYCTL_WRITE);
474184610Salfred
475188412Sthompsa	for (i = 0; i != AUE_TIMEOUT; i++) {
476188412Sthompsa		if (aue_csr_read_1(sc, AUE_PHY_CTL) & AUE_PHYCTL_DONE)
477184610Salfred			break;
478194228Sthompsa		if (uether_pause(&sc->sc_ue, hz / 100))
479188412Sthompsa			break;
480184610Salfred	}
481184610Salfred
482188412Sthompsa	if (i == AUE_TIMEOUT)
483196491Salfred		device_printf(sc->sc_ue.ue_dev, "MII write timed out\n");
484188412Sthompsa
485188412Sthompsa	if (!locked)
486188412Sthompsa		AUE_UNLOCK(sc);
487184610Salfred	return (0);
488184610Salfred}
489184610Salfred
490184610Salfredstatic void
491188412Sthompsaaue_miibus_statchg(device_t dev)
492184610Salfred{
493184610Salfred	struct aue_softc *sc = device_get_softc(dev);
494184610Salfred	struct mii_data *mii = GET_MII(sc);
495188412Sthompsa	int locked;
496184610Salfred
497188412Sthompsa	locked = mtx_owned(&sc->sc_mtx);
498188412Sthompsa	if (!locked)
499188412Sthompsa		AUE_LOCK(sc);
500184610Salfred
501188412Sthompsa	AUE_CLRBIT(sc, AUE_CTL0, AUE_CTL0_RX_ENB | AUE_CTL0_TX_ENB);
502188412Sthompsa	if (IFM_SUBTYPE(mii->mii_media_active) == IFM_100_TX)
503188412Sthompsa		AUE_SETBIT(sc, AUE_CTL1, AUE_CTL1_SPEEDSEL);
504188412Sthompsa	else
505188412Sthompsa		AUE_CLRBIT(sc, AUE_CTL1, AUE_CTL1_SPEEDSEL);
506184610Salfred
507188412Sthompsa	if ((mii->mii_media_active & IFM_GMASK) == IFM_FDX)
508188412Sthompsa		AUE_SETBIT(sc, AUE_CTL1, AUE_CTL1_DUPLEX);
509188412Sthompsa	else
510188412Sthompsa		AUE_CLRBIT(sc, AUE_CTL1, AUE_CTL1_DUPLEX);
511184610Salfred
512188412Sthompsa	AUE_SETBIT(sc, AUE_CTL0, AUE_CTL0_RX_ENB | AUE_CTL0_TX_ENB);
513184610Salfred
514184610Salfred	/*
515184610Salfred	 * Set the LED modes on the LinkSys adapter.
516184610Salfred	 * This turns on the 'dual link LED' bin in the auxmode
517184610Salfred	 * register of the Broadcom PHY.
518184610Salfred	 */
519184610Salfred	if (sc->sc_flags & AUE_FLAG_LSYS) {
520184610Salfred		uint16_t auxmode;
521184610Salfred
522188412Sthompsa		auxmode = aue_miibus_readreg(dev, 0, 0x1b);
523188412Sthompsa		aue_miibus_writereg(dev, 0, 0x1b, auxmode | 0x04);
524184610Salfred	}
525188412Sthompsa	if (!locked)
526188412Sthompsa		AUE_UNLOCK(sc);
527184610Salfred}
528184610Salfred
529188412Sthompsa#define	AUE_BITS	6
530184610Salfredstatic void
531192984Sthompsaaue_setmulti(struct usb_ether *ue)
532184610Salfred{
533194228Sthompsa	struct aue_softc *sc = uether_getsc(ue);
534194228Sthompsa	struct ifnet *ifp = uether_getifp(ue);
535188412Sthompsa	struct ifmultiaddr *ifma;
536188412Sthompsa	uint32_t h = 0;
537188412Sthompsa	uint32_t i;
538188412Sthompsa	uint8_t hashtbl[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
539184610Salfred
540188412Sthompsa	AUE_LOCK_ASSERT(sc, MA_OWNED);
541188412Sthompsa
542188412Sthompsa	if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) {
543188412Sthompsa		AUE_SETBIT(sc, AUE_CTL0, AUE_CTL0_ALLMULTI);
544184610Salfred		return;
545184610Salfred	}
546184610Salfred
547188412Sthompsa	AUE_CLRBIT(sc, AUE_CTL0, AUE_CTL0_ALLMULTI);
548184610Salfred
549184610Salfred	/* now program new ones */
550195049Srwatson	if_maddr_rlock(ifp);
551188412Sthompsa	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
552188412Sthompsa		if (ifma->ifma_addr->sa_family != AF_LINK)
553188412Sthompsa			continue;
554188412Sthompsa		h = ether_crc32_le(LLADDR((struct sockaddr_dl *)
555188412Sthompsa		    ifma->ifma_addr), ETHER_ADDR_LEN) & ((1 << AUE_BITS) - 1);
556188412Sthompsa		hashtbl[(h >> 3)] |=  1 << (h & 0x7);
557184610Salfred	}
558195049Srwatson	if_maddr_runlock(ifp);
559188412Sthompsa
560188412Sthompsa	/* write the hashtable */
561188412Sthompsa	for (i = 0; i != 8; i++)
562188412Sthompsa		aue_csr_write_1(sc, AUE_MAR0 + i, hashtbl[i]);
563184610Salfred}
564184610Salfred
565184610Salfredstatic void
566188412Sthompsaaue_reset_pegasus_II(struct aue_softc *sc)
567184610Salfred{
568184610Salfred	/* Magic constants taken from Linux driver. */
569188412Sthompsa	aue_csr_write_1(sc, AUE_REG_1D, 0);
570188412Sthompsa	aue_csr_write_1(sc, AUE_REG_7B, 2);
571184610Salfred#if 0
572184610Salfred	if ((sc->sc_flags & HAS_HOME_PNA) && mii_mode)
573188412Sthompsa		aue_csr_write_1(sc, AUE_REG_81, 6);
574184610Salfred	else
575184610Salfred#endif
576188412Sthompsa		aue_csr_write_1(sc, AUE_REG_81, 2);
577184610Salfred}
578184610Salfred
579184610Salfredstatic void
580188412Sthompsaaue_reset(struct aue_softc *sc)
581184610Salfred{
582188412Sthompsa	int i;
583184610Salfred
584188412Sthompsa	AUE_SETBIT(sc, AUE_CTL1, AUE_CTL1_RESETMAC);
585184610Salfred
586188412Sthompsa	for (i = 0; i != AUE_TIMEOUT; i++) {
587188412Sthompsa		if (!(aue_csr_read_1(sc, AUE_CTL1) & AUE_CTL1_RESETMAC))
588184610Salfred			break;
589194228Sthompsa		if (uether_pause(&sc->sc_ue, hz / 100))
590188412Sthompsa			break;
591184610Salfred	}
592184610Salfred
593188412Sthompsa	if (i == AUE_TIMEOUT)
594188412Sthompsa		device_printf(sc->sc_ue.ue_dev, "reset failed\n");
595188412Sthompsa
596184610Salfred	/*
597184610Salfred	 * The PHY(s) attached to the Pegasus chip may be held
598184610Salfred	 * in reset until we flip on the GPIO outputs. Make sure
599184610Salfred	 * to set the GPIO pins high so that the PHY(s) will
600184610Salfred	 * be enabled.
601184610Salfred	 *
602196491Salfred	 * NOTE: We used to force all of the GPIO pins low first and then
603196491Salfred	 * enable the ones we want. This has been changed to better
604196491Salfred	 * match the ADMtek's reference design to avoid setting the
605196491Salfred	 * power-down configuration line of the PHY at the same time
606196491Salfred	 * it is reset.
607184610Salfred	 */
608196491Salfred	aue_csr_write_1(sc, AUE_GPIO0, AUE_GPIO_SEL0|AUE_GPIO_SEL1);
609196491Salfred	aue_csr_write_1(sc, AUE_GPIO0, AUE_GPIO_SEL0|AUE_GPIO_SEL1|AUE_GPIO_OUT0);
610184610Salfred
611184610Salfred	if (sc->sc_flags & AUE_FLAG_LSYS) {
612184610Salfred		/* Grrr. LinkSys has to be different from everyone else. */
613188412Sthompsa		aue_csr_write_1(sc, AUE_GPIO0, AUE_GPIO_SEL0|AUE_GPIO_SEL1);
614188412Sthompsa		aue_csr_write_1(sc, AUE_GPIO0,
615188412Sthompsa		    AUE_GPIO_SEL0|AUE_GPIO_SEL1|AUE_GPIO_OUT0);
616184610Salfred	}
617188412Sthompsa	if (sc->sc_flags & AUE_FLAG_PII)
618188412Sthompsa		aue_reset_pegasus_II(sc);
619188412Sthompsa
620188412Sthompsa	/* Wait a little while for the chip to get its brains in order: */
621194228Sthompsa	uether_pause(&sc->sc_ue, hz / 100);
622184610Salfred}
623184610Salfred
624188412Sthompsastatic void
625192984Sthompsaaue_attach_post(struct usb_ether *ue)
626188412Sthompsa{
627194228Sthompsa	struct aue_softc *sc = uether_getsc(ue);
628188412Sthompsa
629188412Sthompsa	/* reset the adapter */
630188412Sthompsa	aue_reset(sc);
631188412Sthompsa
632188412Sthompsa	/* get station address from the EEPROM */
633264745Syongari	aue_read_mac(sc, ue->ue_eaddr);
634188412Sthompsa}
635188412Sthompsa
636184610Salfred/*
637184610Salfred * Probe for a Pegasus chip.
638184610Salfred */
639184610Salfredstatic int
640184610Salfredaue_probe(device_t dev)
641184610Salfred{
642192984Sthompsa	struct usb_attach_arg *uaa = device_get_ivars(dev);
643184610Salfred
644192499Sthompsa	if (uaa->usb_mode != USB_MODE_HOST)
645184610Salfred		return (ENXIO);
646188412Sthompsa	if (uaa->info.bConfigIndex != AUE_CONFIG_INDEX)
647184610Salfred		return (ENXIO);
648188412Sthompsa	if (uaa->info.bIfaceIndex != AUE_IFACE_IDX)
649184610Salfred		return (ENXIO);
650185290Salfred	/*
651188412Sthompsa	 * Belkin USB Bluetooth dongles of the F8T012xx1 model series conflict
652188412Sthompsa	 * with older Belkin USB2LAN adapters.  Skip if_aue if we detect one of
653188412Sthompsa	 * the devices that look like Bluetooth adapters.
654185290Salfred	 */
655188412Sthompsa	if (uaa->info.idVendor == USB_VENDOR_BELKIN &&
656188412Sthompsa	    uaa->info.idProduct == USB_PRODUCT_BELKIN_F8T012 &&
657188412Sthompsa	    uaa->info.bcdDevice == 0x0413)
658185290Salfred		return (ENXIO);
659188412Sthompsa
660194228Sthompsa	return (usbd_lookup_id_by_uaa(aue_devs, sizeof(aue_devs), uaa));
661184610Salfred}
662184610Salfred
663184610Salfred/*
664184610Salfred * Attach the interface. Allocate softc structures, do ifmedia
665184610Salfred * setup and ethernet/BPF attach.
666184610Salfred */
667184610Salfredstatic int
668184610Salfredaue_attach(device_t dev)
669184610Salfred{
670192984Sthompsa	struct usb_attach_arg *uaa = device_get_ivars(dev);
671184610Salfred	struct aue_softc *sc = device_get_softc(dev);
672192984Sthompsa	struct usb_ether *ue = &sc->sc_ue;
673184610Salfred	uint8_t iface_index;
674188412Sthompsa	int error;
675184610Salfred
676184610Salfred	sc->sc_flags = USB_GET_DRIVER_INFO(uaa);
677184610Salfred
678184610Salfred	if (uaa->info.bcdDevice >= 0x0201) {
679188412Sthompsa		/* XXX currently undocumented */
680188412Sthompsa		sc->sc_flags |= AUE_FLAG_VER_2;
681184610Salfred	}
682188412Sthompsa
683194228Sthompsa	device_set_usb_desc(dev);
684188412Sthompsa	mtx_init(&sc->sc_mtx, device_get_nameunit(dev), NULL, MTX_DEF);
685184610Salfred
686184610Salfred	iface_index = AUE_IFACE_IDX;
687194228Sthompsa	error = usbd_transfer_setup(uaa->device, &iface_index,
688187259Sthompsa	    sc->sc_xfer, aue_config, AUE_N_TRANSFER,
689184610Salfred	    sc, &sc->sc_mtx);
690184610Salfred	if (error) {
691199816Sthompsa		device_printf(dev, "allocating USB transfers failed\n");
692184610Salfred		goto detach;
693184610Salfred	}
694188412Sthompsa
695188412Sthompsa	ue->ue_sc = sc;
696188412Sthompsa	ue->ue_dev = dev;
697188412Sthompsa	ue->ue_udev = uaa->device;
698188412Sthompsa	ue->ue_mtx = &sc->sc_mtx;
699188412Sthompsa	ue->ue_methods = &aue_ue_methods;
700188412Sthompsa
701194228Sthompsa	error = uether_ifattach(ue);
702184610Salfred	if (error) {
703188412Sthompsa		device_printf(dev, "could not attach interface\n");
704184610Salfred		goto detach;
705184610Salfred	}
706184610Salfred	return (0);			/* success */
707184610Salfred
708184610Salfreddetach:
709184610Salfred	aue_detach(dev);
710184610Salfred	return (ENXIO);			/* failure */
711184610Salfred}
712184610Salfred
713184610Salfredstatic int
714184610Salfredaue_detach(device_t dev)
715184610Salfred{
716184610Salfred	struct aue_softc *sc = device_get_softc(dev);
717192984Sthompsa	struct usb_ether *ue = &sc->sc_ue;
718184610Salfred
719194228Sthompsa	usbd_transfer_unsetup(sc->sc_xfer, AUE_N_TRANSFER);
720194228Sthompsa	uether_ifdetach(ue);
721184610Salfred	mtx_destroy(&sc->sc_mtx);
722184610Salfred
723184610Salfred	return (0);
724184610Salfred}
725184610Salfred
726184610Salfredstatic void
727194677Sthompsaaue_intr_callback(struct usb_xfer *xfer, usb_error_t error)
728184610Salfred{
729194677Sthompsa	struct aue_softc *sc = usbd_xfer_softc(xfer);
730194228Sthompsa	struct ifnet *ifp = uether_getifp(&sc->sc_ue);
731184610Salfred	struct aue_intrpkt pkt;
732194677Sthompsa	struct usb_page_cache *pc;
733194677Sthompsa	int actlen;
734184610Salfred
735194677Sthompsa	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
736194677Sthompsa
737184610Salfred	switch (USB_GET_STATE(xfer)) {
738184610Salfred	case USB_ST_TRANSFERRED:
739184610Salfred
740188412Sthompsa		if ((ifp->if_drv_flags & IFF_DRV_RUNNING) &&
741233774Shselasky		    actlen >= (int)sizeof(pkt)) {
742184610Salfred
743194677Sthompsa			pc = usbd_xfer_get_frame(xfer, 0);
744194677Sthompsa			usbd_copy_out(pc, 0, &pkt, sizeof(pkt));
745184610Salfred
746188412Sthompsa			if (pkt.aue_txstat0)
747184610Salfred				ifp->if_oerrors++;
748271355Shselasky			if (pkt.aue_txstat0 & (AUE_TXSTAT0_LATECOLL |
749188412Sthompsa			    AUE_TXSTAT0_EXCESSCOLL))
750184610Salfred				ifp->if_collisions++;
751184610Salfred		}
752188412Sthompsa		/* FALLTHROUGH */
753184610Salfred	case USB_ST_SETUP:
754188412Sthompsatr_setup:
755194677Sthompsa		usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
756194228Sthompsa		usbd_transfer_submit(xfer);
757184610Salfred		return;
758184610Salfred
759184610Salfred	default:			/* Error */
760194677Sthompsa		if (error != USB_ERR_CANCELLED) {
761188412Sthompsa			/* try to clear stall first */
762194677Sthompsa			usbd_xfer_set_stall(xfer);
763188412Sthompsa			goto tr_setup;
764184610Salfred		}
765184610Salfred		return;
766184610Salfred	}
767184610Salfred}
768184610Salfred
769184610Salfredstatic void
770194677Sthompsaaue_bulk_read_callback(struct usb_xfer *xfer, usb_error_t error)
771184610Salfred{
772194677Sthompsa	struct aue_softc *sc = usbd_xfer_softc(xfer);
773192984Sthompsa	struct usb_ether *ue = &sc->sc_ue;
774194228Sthompsa	struct ifnet *ifp = uether_getifp(ue);
775188412Sthompsa	struct aue_rxpkt stat;
776194677Sthompsa	struct usb_page_cache *pc;
777194677Sthompsa	int actlen;
778184610Salfred
779194677Sthompsa	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
780194677Sthompsa	pc = usbd_xfer_get_frame(xfer, 0);
781194677Sthompsa
782184610Salfred	switch (USB_GET_STATE(xfer)) {
783184610Salfred	case USB_ST_TRANSFERRED:
784194677Sthompsa		DPRINTFN(11, "received %d bytes\n", actlen);
785184610Salfred
786184610Salfred		if (sc->sc_flags & AUE_FLAG_VER_2) {
787184610Salfred
788194677Sthompsa			if (actlen == 0) {
789184610Salfred				ifp->if_ierrors++;
790184610Salfred				goto tr_setup;
791184610Salfred			}
792184610Salfred		} else {
793184610Salfred
794233774Shselasky			if (actlen <= (int)(sizeof(stat) + ETHER_CRC_LEN)) {
795184610Salfred				ifp->if_ierrors++;
796184610Salfred				goto tr_setup;
797184610Salfred			}
798194677Sthompsa			usbd_copy_out(pc, actlen - sizeof(stat), &stat,
799194677Sthompsa			    sizeof(stat));
800184610Salfred
801184610Salfred			/*
802184610Salfred			 * turn off all the non-error bits in the rx status
803184610Salfred			 * word:
804184610Salfred			 */
805188412Sthompsa			stat.aue_rxstat &= AUE_RXSTAT_MASK;
806188412Sthompsa			if (stat.aue_rxstat) {
807184610Salfred				ifp->if_ierrors++;
808184610Salfred				goto tr_setup;
809184610Salfred			}
810184610Salfred			/* No errors; receive the packet. */
811194677Sthompsa			actlen -= (sizeof(stat) + ETHER_CRC_LEN);
812184610Salfred		}
813194677Sthompsa		uether_rxbuf(ue, pc, 0, actlen);
814184610Salfred
815188412Sthompsa		/* FALLTHROUGH */
816184610Salfred	case USB_ST_SETUP:
817184610Salfredtr_setup:
818194677Sthompsa		usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
819194228Sthompsa		usbd_transfer_submit(xfer);
820194228Sthompsa		uether_rxflush(ue);
821184610Salfred		return;
822184610Salfred
823184610Salfred	default:			/* Error */
824188412Sthompsa		DPRINTF("bulk read error, %s\n",
825194677Sthompsa		    usbd_errstr(error));
826188412Sthompsa
827194677Sthompsa		if (error != USB_ERR_CANCELLED) {
828184610Salfred			/* try to clear stall first */
829194677Sthompsa			usbd_xfer_set_stall(xfer);
830188412Sthompsa			goto tr_setup;
831184610Salfred		}
832184610Salfred		return;
833184610Salfred	}
834184610Salfred}
835184610Salfred
836184610Salfredstatic void
837194677Sthompsaaue_bulk_write_callback(struct usb_xfer *xfer, usb_error_t error)
838184610Salfred{
839194677Sthompsa	struct aue_softc *sc = usbd_xfer_softc(xfer);
840194228Sthompsa	struct ifnet *ifp = uether_getifp(&sc->sc_ue);
841194677Sthompsa	struct usb_page_cache *pc;
842184610Salfred	struct mbuf *m;
843184610Salfred	uint8_t buf[2];
844194677Sthompsa	int actlen;
845184610Salfred
846194677Sthompsa	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
847194677Sthompsa	pc = usbd_xfer_get_frame(xfer, 0);
848194677Sthompsa
849184610Salfred	switch (USB_GET_STATE(xfer)) {
850184610Salfred	case USB_ST_TRANSFERRED:
851194677Sthompsa		DPRINTFN(11, "transfer of %d bytes complete\n", actlen);
852184610Salfred		ifp->if_opackets++;
853184610Salfred
854188412Sthompsa		/* FALLTHROUGH */
855184610Salfred	case USB_ST_SETUP:
856188412Sthompsatr_setup:
857188412Sthompsa		if ((sc->sc_flags & AUE_FLAG_LINK) == 0) {
858184610Salfred			/*
859184610Salfred			 * don't send anything if there is no link !
860184610Salfred			 */
861188412Sthompsa			return;
862184610Salfred		}
863184610Salfred		IFQ_DRV_DEQUEUE(&ifp->if_snd, m);
864184610Salfred
865188412Sthompsa		if (m == NULL)
866188412Sthompsa			return;
867188412Sthompsa		if (m->m_pkthdr.len > MCLBYTES)
868184610Salfred			m->m_pkthdr.len = MCLBYTES;
869184610Salfred		if (sc->sc_flags & AUE_FLAG_VER_2) {
870184610Salfred
871194677Sthompsa			usbd_xfer_set_frame_len(xfer, 0, m->m_pkthdr.len);
872184610Salfred
873194677Sthompsa			usbd_m_copy_in(pc, 0, m, 0, m->m_pkthdr.len);
874184610Salfred
875184610Salfred		} else {
876184610Salfred
877194677Sthompsa			usbd_xfer_set_frame_len(xfer, 0, (m->m_pkthdr.len + 2));
878184610Salfred
879184610Salfred			/*
880188412Sthompsa		         * The ADMtek documentation says that the
881188412Sthompsa		         * packet length is supposed to be specified
882188412Sthompsa		         * in the first two bytes of the transfer,
883188412Sthompsa		         * however it actually seems to ignore this
884188412Sthompsa		         * info and base the frame size on the bulk
885188412Sthompsa		         * transfer length.
886184610Salfred		         */
887184610Salfred			buf[0] = (uint8_t)(m->m_pkthdr.len);
888184610Salfred			buf[1] = (uint8_t)(m->m_pkthdr.len >> 8);
889184610Salfred
890194677Sthompsa			usbd_copy_in(pc, 0, buf, 2);
891194677Sthompsa			usbd_m_copy_in(pc, 2, m, 0, m->m_pkthdr.len);
892184610Salfred		}
893184610Salfred
894184610Salfred		/*
895184610Salfred		 * if there's a BPF listener, bounce a copy
896184610Salfred		 * of this frame to him:
897184610Salfred		 */
898184610Salfred		BPF_MTAP(ifp, m);
899184610Salfred
900184610Salfred		m_freem(m);
901184610Salfred
902194228Sthompsa		usbd_transfer_submit(xfer);
903184610Salfred		return;
904184610Salfred
905184610Salfred	default:			/* Error */
906184610Salfred		DPRINTFN(11, "transfer error, %s\n",
907194677Sthompsa		    usbd_errstr(error));
908184610Salfred
909188412Sthompsa		ifp->if_oerrors++;
910188412Sthompsa
911194677Sthompsa		if (error != USB_ERR_CANCELLED) {
912184610Salfred			/* try to clear stall first */
913194677Sthompsa			usbd_xfer_set_stall(xfer);
914188412Sthompsa			goto tr_setup;
915184610Salfred		}
916184610Salfred		return;
917184610Salfred	}
918184610Salfred}
919184610Salfred
920184610Salfredstatic void
921192984Sthompsaaue_tick(struct usb_ether *ue)
922184610Salfred{
923194228Sthompsa	struct aue_softc *sc = uether_getsc(ue);
924188412Sthompsa	struct mii_data *mii = GET_MII(sc);
925184610Salfred
926188412Sthompsa	AUE_LOCK_ASSERT(sc, MA_OWNED);
927184610Salfred
928184610Salfred	mii_tick(mii);
929188412Sthompsa	if ((sc->sc_flags & AUE_FLAG_LINK) == 0
930188412Sthompsa	    && mii->mii_media_status & IFM_ACTIVE &&
931188412Sthompsa	    IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
932188412Sthompsa		sc->sc_flags |= AUE_FLAG_LINK;
933188412Sthompsa		aue_start(ue);
934184610Salfred	}
935184610Salfred}
936184610Salfred
937184610Salfredstatic void
938192984Sthompsaaue_start(struct usb_ether *ue)
939184610Salfred{
940194228Sthompsa	struct aue_softc *sc = uether_getsc(ue);
941184610Salfred
942188412Sthompsa	/*
943188412Sthompsa	 * start the USB transfers, if not already started:
944188412Sthompsa	 */
945194228Sthompsa	usbd_transfer_start(sc->sc_xfer[AUE_INTR_DT_RD]);
946194228Sthompsa	usbd_transfer_start(sc->sc_xfer[AUE_BULK_DT_RD]);
947194228Sthompsa	usbd_transfer_start(sc->sc_xfer[AUE_BULK_DT_WR]);
948184610Salfred}
949184610Salfred
950184610Salfredstatic void
951192984Sthompsaaue_init(struct usb_ether *ue)
952184610Salfred{
953194228Sthompsa	struct aue_softc *sc = uether_getsc(ue);
954194228Sthompsa	struct ifnet *ifp = uether_getifp(ue);
955188412Sthompsa	int i;
956184610Salfred
957188412Sthompsa	AUE_LOCK_ASSERT(sc, MA_OWNED);
958184610Salfred
959184610Salfred	/*
960184610Salfred	 * Cancel pending I/O
961184610Salfred	 */
962188412Sthompsa	aue_reset(sc);
963184610Salfred
964184610Salfred	/* Set MAC address */
965188412Sthompsa	for (i = 0; i != ETHER_ADDR_LEN; i++)
966188412Sthompsa		aue_csr_write_1(sc, AUE_PAR0 + i, IF_LLADDR(ifp)[i]);
967184610Salfred
968184610Salfred	/* update promiscuous setting */
969188412Sthompsa	aue_setpromisc(ue);
970184610Salfred
971188412Sthompsa	/* Load the multicast filter. */
972188412Sthompsa	aue_setmulti(ue);
973184610Salfred
974188412Sthompsa	/* Enable RX and TX */
975188412Sthompsa	aue_csr_write_1(sc, AUE_CTL0, AUE_CTL0_RXSTAT_APPEND | AUE_CTL0_RX_ENB);
976188412Sthompsa	AUE_SETBIT(sc, AUE_CTL0, AUE_CTL0_TX_ENB);
977188412Sthompsa	AUE_SETBIT(sc, AUE_CTL2, AUE_CTL2_EP3_CLR);
978184610Salfred
979194677Sthompsa	usbd_xfer_set_stall(sc->sc_xfer[AUE_BULK_DT_WR]);
980184610Salfred
981188412Sthompsa	ifp->if_drv_flags |= IFF_DRV_RUNNING;
982188412Sthompsa	aue_start(ue);
983184610Salfred}
984184610Salfred
985184610Salfredstatic void
986192984Sthompsaaue_setpromisc(struct usb_ether *ue)
987184610Salfred{
988194228Sthompsa	struct aue_softc *sc = uether_getsc(ue);
989194228Sthompsa	struct ifnet *ifp = uether_getifp(ue);
990188412Sthompsa
991188412Sthompsa	AUE_LOCK_ASSERT(sc, MA_OWNED);
992188412Sthompsa
993184610Salfred	/* if we want promiscuous mode, set the allframes bit: */
994188412Sthompsa	if (ifp->if_flags & IFF_PROMISC)
995188412Sthompsa		AUE_SETBIT(sc, AUE_CTL2, AUE_CTL2_RX_PROMISC);
996188412Sthompsa	else
997188412Sthompsa		AUE_CLRBIT(sc, AUE_CTL2, AUE_CTL2_RX_PROMISC);
998184610Salfred}
999184610Salfred
1000184610Salfred/*
1001184610Salfred * Set media options.
1002184610Salfred */
1003184610Salfredstatic int
1004188412Sthompsaaue_ifmedia_upd(struct ifnet *ifp)
1005184610Salfred{
1006184610Salfred	struct aue_softc *sc = ifp->if_softc;
1007184610Salfred	struct mii_data *mii = GET_MII(sc);
1008221407Smarius	struct mii_softc *miisc;
1009251734Skevlo	int error;
1010184610Salfred
1011188412Sthompsa	AUE_LOCK_ASSERT(sc, MA_OWNED);
1012184610Salfred
1013188412Sthompsa        sc->sc_flags &= ~AUE_FLAG_LINK;
1014221407Smarius	LIST_FOREACH(miisc, &mii->mii_phys, mii_list)
1015221407Smarius		PHY_RESET(miisc);
1016251734Skevlo	error = mii_mediachg(mii);
1017251734Skevlo	return (error);
1018184610Salfred}
1019184610Salfred
1020184610Salfred/*
1021184610Salfred * Report current media status.
1022184610Salfred */
1023184610Salfredstatic void
1024188412Sthompsaaue_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
1025184610Salfred{
1026184610Salfred	struct aue_softc *sc = ifp->if_softc;
1027188412Sthompsa	struct mii_data *mii = GET_MII(sc);
1028184610Salfred
1029188412Sthompsa	AUE_LOCK(sc);
1030188412Sthompsa	mii_pollstat(mii);
1031188412Sthompsa	ifmr->ifm_active = mii->mii_media_active;
1032188412Sthompsa	ifmr->ifm_status = mii->mii_media_status;
1033226479Syongari	AUE_UNLOCK(sc);
1034184610Salfred}
1035184610Salfred
1036184610Salfred/*
1037184610Salfred * Stop the adapter and free any mbufs allocated to the
1038184610Salfred * RX and TX lists.
1039184610Salfred */
1040184610Salfredstatic void
1041192984Sthompsaaue_stop(struct usb_ether *ue)
1042184610Salfred{
1043194228Sthompsa	struct aue_softc *sc = uether_getsc(ue);
1044194228Sthompsa	struct ifnet *ifp = uether_getifp(ue);
1045184610Salfred
1046188412Sthompsa	AUE_LOCK_ASSERT(sc, MA_OWNED);
1047184610Salfred
1048188412Sthompsa	ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1049188412Sthompsa	sc->sc_flags &= ~AUE_FLAG_LINK;
1050184610Salfred
1051184610Salfred	/*
1052184610Salfred	 * stop all the transfers, if not already stopped:
1053184610Salfred	 */
1054194228Sthompsa	usbd_transfer_stop(sc->sc_xfer[AUE_BULK_DT_WR]);
1055194228Sthompsa	usbd_transfer_stop(sc->sc_xfer[AUE_BULK_DT_RD]);
1056194228Sthompsa	usbd_transfer_stop(sc->sc_xfer[AUE_INTR_DT_RD]);
1057184610Salfred
1058188412Sthompsa	aue_csr_write_1(sc, AUE_CTL0, 0);
1059188412Sthompsa	aue_csr_write_1(sc, AUE_CTL1, 0);
1060188412Sthompsa	aue_reset(sc);
1061184610Salfred}
1062