if_smsc.c revision 240806
1239275Sgonzo/*-
2239275Sgonzo * Copyright (c) 2012
3239275Sgonzo *	Ben Gray <bgray@freebsd.org>.
4239275Sgonzo * All rights reserved.
5239275Sgonzo *
6239275Sgonzo * Redistribution and use in source and binary forms, with or without
7239275Sgonzo * modification, are permitted provided that the following conditions
8239275Sgonzo * are met:
9239275Sgonzo * 1. Redistributions of source code must retain the above copyright
10239275Sgonzo *    notice, this list of conditions and the following disclaimer.
11239275Sgonzo * 2. Redistributions in binary form must reproduce the above copyright
12239275Sgonzo *    notice, this list of conditions and the following disclaimer in the
13239275Sgonzo *    documentation and/or other materials provided with the distribution.
14239275Sgonzo *
15239275Sgonzo * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16239275Sgonzo * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17239275Sgonzo * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18239275Sgonzo * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19239275Sgonzo * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20239275Sgonzo * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21239275Sgonzo * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22239275Sgonzo * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23239275Sgonzo * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24239275Sgonzo * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25239275Sgonzo */
26239275Sgonzo
27239275Sgonzo#include <sys/cdefs.h>
28239275Sgonzo__FBSDID("$FreeBSD: head/sys/dev/usb/net/if_smsc.c 240806 2012-09-22 08:02:42Z hselasky $");
29239275Sgonzo
30239275Sgonzo/*
31239275Sgonzo * SMSC LAN9xxx devices (http://www.smsc.com/)
32239275Sgonzo *
33239275Sgonzo * The LAN9500 & LAN9500A devices are stand-alone USB to Ethernet chips that
34239275Sgonzo * support USB 2.0 and 10/100 Mbps Ethernet.
35239275Sgonzo *
36239275Sgonzo * The LAN951x devices are an integrated USB hub and USB to Ethernet adapter.
37239275Sgonzo * The driver only covers the Ethernet part, the standard USB hub driver
38239275Sgonzo * supports the hub part.
39239275Sgonzo *
40239275Sgonzo * This driver is closely modelled on the Linux driver written and copyrighted
41239275Sgonzo * by SMSC.
42239275Sgonzo *
43239275Sgonzo *
44239275Sgonzo *
45239275Sgonzo *
46239275Sgonzo * H/W TCP & UDP Checksum Offloading
47239275Sgonzo * ---------------------------------
48239275Sgonzo * The chip supports both tx and rx offloading of UDP & TCP checksums, this
49239275Sgonzo * feature can be dynamically enabled/disabled.
50239275Sgonzo *
51239275Sgonzo * RX checksuming is performed across bytes after the IPv4 header to the end of
52239275Sgonzo * the Ethernet frame, this means if the frame is padded with non-zero values
53239275Sgonzo * the H/W checksum will be incorrect, however the rx code compensates for this.
54239275Sgonzo *
55239275Sgonzo * TX checksuming is more complicated, the device requires a special header to
56239275Sgonzo * be prefixed onto the start of the frame which indicates the start and end
57239275Sgonzo * positions of the UDP or TCP frame.  This requires the driver to manually
58239275Sgonzo * go through the packet data and decode the headers prior to sending.
59239275Sgonzo * On Linux they generally provide cues to the location of the csum and the
60239275Sgonzo * area to calculate it over, on FreeBSD we seem to have to do it all ourselves,
61239275Sgonzo * hence this is not as optimal and therefore h/w tX checksum is currently not
62239275Sgonzo * implemented.
63239275Sgonzo *
64239275Sgonzo */
65239275Sgonzo#include <sys/stdint.h>
66239275Sgonzo#include <sys/stddef.h>
67239275Sgonzo#include <sys/param.h>
68239275Sgonzo#include <sys/queue.h>
69239275Sgonzo#include <sys/types.h>
70239275Sgonzo#include <sys/systm.h>
71239275Sgonzo#include <sys/kernel.h>
72239275Sgonzo#include <sys/bus.h>
73239275Sgonzo#include <sys/module.h>
74239275Sgonzo#include <sys/lock.h>
75239275Sgonzo#include <sys/mutex.h>
76239275Sgonzo#include <sys/condvar.h>
77257241Sglebius#include <sys/sysctl.h>
78239275Sgonzo#include <sys/sx.h>
79239275Sgonzo#include <sys/unistd.h>
80239275Sgonzo#include <sys/callout.h>
81239275Sgonzo#include <sys/malloc.h>
82239275Sgonzo#include <sys/priv.h>
83239275Sgonzo#include <sys/random.h>
84239275Sgonzo
85239275Sgonzo#include <dev/usb/usb.h>
86257241Sglebius#include <dev/usb/usbdi.h>
87257241Sglebius#include <dev/usb/usbdi_util.h>
88257241Sglebius#include "usbdevs.h"
89265371Stuexen
90265371Stuexen#define	USB_DEBUG_VAR smsc_debug
91265371Stuexen#include <dev/usb/usb_debug.h>
92243421Sgonzo#include <dev/usb/usb_process.h>
93243421Sgonzo
94243421Sgonzo#include <dev/usb/usb_device.h>
95243421Sgonzo#include <dev/usb/net/usb_ethernet.h>
96243421Sgonzo#include "if_smscreg.h"
97243421Sgonzo
98243421Sgonzo#ifdef USB_DEBUG
99243421Sgonzostatic int smsc_debug = 0;
100239275Sgonzo
101239275SgonzoSYSCTL_NODE(_hw_usb, OID_AUTO, smsc, CTLFLAG_RW, 0, "USB smsc");
102239275SgonzoSYSCTL_INT(_hw_usb_smsc, OID_AUTO, debug, CTLFLAG_RW, &smsc_debug, 0,
103239275Sgonzo    "Debug level");
104239275Sgonzo#endif
105239275Sgonzo
106239275Sgonzo/*
107239275Sgonzo * Various supported device vendors/products.
108239275Sgonzo */
109239275Sgonzostatic const struct usb_device_id smsc_devs[] = {
110239275Sgonzo#define	SMSC_DEV(p,i) { USB_VPI(USB_VENDOR_SMC2, USB_PRODUCT_SMC2_##p, i) }
111246615Shselasky	SMSC_DEV(LAN9514_ETH, 0),
112246615Shselasky#undef SMSC_DEV
113239275Sgonzo};
114239275Sgonzo
115239275Sgonzo
116239275Sgonzo#ifdef USB_DEBUG
117276701Shselasky#define smsc_dbg_printf(sc, fmt, args...) \
118239275Sgonzo	do { \
119239275Sgonzo		if (smsc_debug > 0) \
120239275Sgonzo			device_printf((sc)->sc_ue.ue_dev, "debug: " fmt, ##args); \
121239275Sgonzo	} while(0)
122239275Sgonzo#else
123239275Sgonzo#define smsc_dbg_printf(sc, fmt, args...)
124239275Sgonzo#endif
125239275Sgonzo
126272160Sgavin#define smsc_warn_printf(sc, fmt, args...) \
127272160Sgavin	device_printf((sc)->sc_ue.ue_dev, "warning: " fmt, ##args)
128272160Sgavin
129272160Sgavin#define smsc_err_printf(sc, fmt, args...) \
130272160Sgavin	device_printf((sc)->sc_ue.ue_dev, "error: " fmt, ##args)
131272160Sgavin
132272160Sgavin
133239275Sgonzo#define ETHER_IS_ZERO(addr) \
134272160Sgavin	(!(addr[0] | addr[1] | addr[2] | addr[3] | addr[4] | addr[5]))
135272160Sgavin
136272160Sgavin#define ETHER_IS_VALID(addr) \
137272160Sgavin	(!ETHER_IS_MULTICAST(addr) && !ETHER_IS_ZERO(addr))
138272160Sgavin
139272160Sgavinstatic device_probe_t smsc_probe;
140272160Sgavinstatic device_attach_t smsc_attach;
141272160Sgavinstatic device_detach_t smsc_detach;
142272160Sgavin
143272160Sgavinstatic usb_callback_t smsc_bulk_read_callback;
144239275Sgonzostatic usb_callback_t smsc_bulk_write_callback;
145239275Sgonzo
146239275Sgonzostatic miibus_readreg_t smsc_miibus_readreg;
147239275Sgonzostatic miibus_writereg_t smsc_miibus_writereg;
148239275Sgonzostatic miibus_statchg_t smsc_miibus_statchg;
149239275Sgonzo
150239275Sgonzo#if __FreeBSD_version > 1000000
151239275Sgonzostatic int smsc_attach_post_sub(struct usb_ether *ue);
152239275Sgonzo#endif
153239275Sgonzostatic uether_fn_t smsc_attach_post;
154239275Sgonzostatic uether_fn_t smsc_init;
155239275Sgonzostatic uether_fn_t smsc_stop;
156239275Sgonzostatic uether_fn_t smsc_start;
157239275Sgonzostatic uether_fn_t smsc_tick;
158239275Sgonzostatic uether_fn_t smsc_setmulti;
159239275Sgonzostatic uether_fn_t smsc_setpromisc;
160239275Sgonzo
161239275Sgonzostatic int	smsc_ifmedia_upd(struct ifnet *);
162239275Sgonzostatic void	smsc_ifmedia_sts(struct ifnet *, struct ifmediareq *);
163239275Sgonzo
164239275Sgonzostatic int smsc_chip_init(struct smsc_softc *sc);
165239275Sgonzostatic int smsc_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data);
166239275Sgonzo
167239275Sgonzostatic const struct usb_config smsc_config[SMSC_N_TRANSFER] = {
168239275Sgonzo
169239275Sgonzo	[SMSC_BULK_DT_WR] = {
170239275Sgonzo		.type = UE_BULK,
171239275Sgonzo		.endpoint = UE_ADDR_ANY,
172239275Sgonzo		.direction = UE_DIR_OUT,
173239275Sgonzo		.frames = 16,
174239275Sgonzo		.bufsize = 16 * (MCLBYTES + 16),
175239275Sgonzo		.flags = {.pipe_bof = 1,.force_short_xfer = 1,},
176239275Sgonzo		.callback = smsc_bulk_write_callback,
177239275Sgonzo		.timeout = 10000,	/* 10 seconds */
178239275Sgonzo	},
179239275Sgonzo
180239275Sgonzo	[SMSC_BULK_DT_RD] = {
181239275Sgonzo		.type = UE_BULK,
182239275Sgonzo		.endpoint = UE_ADDR_ANY,
183239275Sgonzo		.direction = UE_DIR_IN,
184239275Sgonzo		.bufsize = 20480,	/* bytes */
185239275Sgonzo		.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
186239275Sgonzo		.callback = smsc_bulk_read_callback,
187239275Sgonzo		.timeout = 0,	/* no timeout */
188239275Sgonzo	},
189239275Sgonzo
190239275Sgonzo	/* The SMSC chip supports an interrupt endpoints, however they aren't
191239275Sgonzo	 * needed as we poll on the MII status.
192239275Sgonzo	 */
193239275Sgonzo};
194239275Sgonzo
195239275Sgonzostatic const struct usb_ether_methods smsc_ue_methods = {
196239275Sgonzo	.ue_attach_post = smsc_attach_post,
197239275Sgonzo#if __FreeBSD_version > 1000000
198239275Sgonzo	.ue_attach_post_sub = smsc_attach_post_sub,
199239275Sgonzo#endif
200239275Sgonzo	.ue_start = smsc_start,
201239275Sgonzo	.ue_ioctl = smsc_ioctl,
202239275Sgonzo	.ue_init = smsc_init,
203239275Sgonzo	.ue_stop = smsc_stop,
204239275Sgonzo	.ue_tick = smsc_tick,
205239275Sgonzo	.ue_setmulti = smsc_setmulti,
206239275Sgonzo	.ue_setpromisc = smsc_setpromisc,
207239275Sgonzo	.ue_mii_upd = smsc_ifmedia_upd,
208239275Sgonzo	.ue_mii_sts = smsc_ifmedia_sts,
209239275Sgonzo};
210239275Sgonzo
211239275Sgonzo/**
212239275Sgonzo *	smsc_read_reg - Reads a 32-bit register on the device
213239275Sgonzo *	@sc: driver soft context
214239275Sgonzo *	@off: offset of the register
215239275Sgonzo *	@data: pointer a value that will be populated with the register value
216239275Sgonzo *
217239275Sgonzo *	LOCKING:
218239275Sgonzo *	The device lock must be held before calling this function.
219239275Sgonzo *
220239275Sgonzo *	RETURNS:
221239275Sgonzo *	0 on success, a USB_ERR_?? error code on failure.
222239275Sgonzo */
223239275Sgonzostatic int
224239275Sgonzosmsc_read_reg(struct smsc_softc *sc, uint32_t off, uint32_t *data)
225239275Sgonzo{
226239275Sgonzo	struct usb_device_request req;
227239275Sgonzo	uint32_t buf;
228239275Sgonzo	usb_error_t err;
229239275Sgonzo
230239275Sgonzo	SMSC_LOCK_ASSERT(sc, MA_OWNED);
231239275Sgonzo
232239275Sgonzo	req.bmRequestType = UT_READ_VENDOR_DEVICE;
233239275Sgonzo	req.bRequest = SMSC_UR_READ_REG;
234239275Sgonzo	USETW(req.wValue, 0);
235239275Sgonzo	USETW(req.wIndex, off);
236239275Sgonzo	USETW(req.wLength, 4);
237239275Sgonzo
238239275Sgonzo	err = uether_do_request(&sc->sc_ue, &req, &buf, 1000);
239239275Sgonzo	if (err != 0)
240239275Sgonzo		smsc_warn_printf(sc, "Failed to read register 0x%0x\n", off);
241239275Sgonzo
242239275Sgonzo	*data = le32toh(buf);
243239275Sgonzo
244239275Sgonzo	return (err);
245239275Sgonzo}
246239275Sgonzo
247239275Sgonzo/**
248239275Sgonzo *	smsc_write_reg - Writes a 32-bit register on the device
249239275Sgonzo *	@sc: driver soft context
250239275Sgonzo *	@off: offset of the register
251239275Sgonzo *	@data: the 32-bit value to write into the register
252239275Sgonzo *
253239275Sgonzo *	LOCKING:
254239275Sgonzo *	The device lock must be held before calling this function.
255239275Sgonzo *
256239275Sgonzo *	RETURNS:
257239275Sgonzo *	0 on success, a USB_ERR_?? error code on failure.
258239275Sgonzo */
259239275Sgonzostatic int
260239275Sgonzosmsc_write_reg(struct smsc_softc *sc, uint32_t off, uint32_t data)
261239275Sgonzo{
262239275Sgonzo	struct usb_device_request req;
263239275Sgonzo	uint32_t buf;
264239275Sgonzo	usb_error_t err;
265239275Sgonzo
266239275Sgonzo	SMSC_LOCK_ASSERT(sc, MA_OWNED);
267239275Sgonzo
268239275Sgonzo	buf = htole32(data);
269239275Sgonzo
270239275Sgonzo	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
271239275Sgonzo	req.bRequest = SMSC_UR_WRITE_REG;
272239275Sgonzo	USETW(req.wValue, 0);
273239275Sgonzo	USETW(req.wIndex, off);
274239275Sgonzo	USETW(req.wLength, 4);
275239275Sgonzo
276239275Sgonzo	err = uether_do_request(&sc->sc_ue, &req, &buf, 1000);
277239275Sgonzo	if (err != 0)
278239275Sgonzo		smsc_warn_printf(sc, "Failed to write register 0x%0x\n", off);
279239275Sgonzo
280239275Sgonzo	return (err);
281239275Sgonzo}
282239275Sgonzo
283239275Sgonzo/**
284239275Sgonzo *	smsc_wait_for_bits - Polls on a register value until bits are cleared
285239275Sgonzo *	@sc: soft context
286239275Sgonzo *	@reg: offset of the register
287239275Sgonzo *	@bits: if the bits are clear the function returns
288239275Sgonzo *
289239275Sgonzo *	LOCKING:
290239275Sgonzo *	The device lock must be held before calling this function.
291239275Sgonzo *
292239275Sgonzo *	RETURNS:
293239275Sgonzo *	0 on success, or a USB_ERR_?? error code on failure.
294239275Sgonzo */
295239275Sgonzostatic int
296239275Sgonzosmsc_wait_for_bits(struct smsc_softc *sc, uint32_t reg, uint32_t bits)
297239275Sgonzo{
298239275Sgonzo	usb_ticks_t start_ticks;
299239275Sgonzo	const usb_ticks_t max_ticks = USB_MS_TO_TICKS(1000);
300239275Sgonzo	uint32_t val;
301239275Sgonzo	int err;
302239275Sgonzo
303239275Sgonzo	SMSC_LOCK_ASSERT(sc, MA_OWNED);
304239275Sgonzo
305239275Sgonzo	start_ticks = (usb_ticks_t)ticks;
306239275Sgonzo	do {
307239275Sgonzo		if ((err = smsc_read_reg(sc, reg, &val)) != 0)
308239275Sgonzo			return (err);
309239275Sgonzo		if (!(val & bits))
310239275Sgonzo			return (0);
311239275Sgonzo
312239275Sgonzo		uether_pause(&sc->sc_ue, hz / 100);
313239275Sgonzo	} while (((usb_ticks_t)(ticks - start_ticks)) < max_ticks);
314239275Sgonzo
315239275Sgonzo	return (USB_ERR_TIMEOUT);
316239275Sgonzo}
317239275Sgonzo
318239275Sgonzo/**
319239275Sgonzo *	smsc_eeprom_read - Reads the attached EEPROM
320239275Sgonzo *	@sc: soft context
321239275Sgonzo *	@off: the eeprom address offset
322239275Sgonzo *	@buf: stores the bytes
323239275Sgonzo *	@buflen: the number of bytes to read
324239275Sgonzo *
325239275Sgonzo *	Simply reads bytes from an attached eeprom.
326239275Sgonzo *
327239275Sgonzo *	LOCKING:
328239275Sgonzo *	The function takes and releases the device lock if it is not already held.
329239275Sgonzo *
330239275Sgonzo *	RETURNS:
331240804Shselasky *	0 on success, or a USB_ERR_?? error code on failure.
332239275Sgonzo */
333239275Sgonzostatic int
334239275Sgonzosmsc_eeprom_read(struct smsc_softc *sc, uint16_t off, uint8_t *buf, uint16_t buflen)
335239275Sgonzo{
336239275Sgonzo	usb_ticks_t start_ticks;
337239275Sgonzo	const usb_ticks_t max_ticks = USB_MS_TO_TICKS(1000);
338239275Sgonzo	int err;
339239275Sgonzo	int locked;
340239275Sgonzo	uint32_t val;
341239275Sgonzo	uint16_t i;
342239275Sgonzo
343239275Sgonzo	locked = mtx_owned(&sc->sc_mtx);
344239275Sgonzo	if (!locked)
345240804Shselasky		SMSC_LOCK(sc);
346239275Sgonzo
347239275Sgonzo	err = smsc_wait_for_bits(sc, SMSC_EEPROM_CMD, SMSC_EEPROM_CMD_BUSY);
348239275Sgonzo	if (err != 0) {
349239275Sgonzo		smsc_warn_printf(sc, "eeprom busy, failed to read data\n");
350239275Sgonzo		goto done;
351239275Sgonzo	}
352239275Sgonzo
353239275Sgonzo	/* start reading the bytes, one at a time */
354239275Sgonzo	for (i = 0; i < buflen; i++) {
355239275Sgonzo
356239275Sgonzo		val = SMSC_EEPROM_CMD_BUSY | (SMSC_EEPROM_CMD_ADDR_MASK & (off + i));
357239275Sgonzo		if ((err = smsc_write_reg(sc, SMSC_EEPROM_CMD, val)) != 0)
358239275Sgonzo			goto done;
359239275Sgonzo
360239275Sgonzo		start_ticks = (usb_ticks_t)ticks;
361239275Sgonzo		do {
362239275Sgonzo			if ((err = smsc_read_reg(sc, SMSC_EEPROM_CMD, &val)) != 0)
363239275Sgonzo				goto done;
364239275Sgonzo			if (!(val & SMSC_EEPROM_CMD_BUSY) || (val & SMSC_EEPROM_CMD_TIMEOUT))
365239275Sgonzo				break;
366239275Sgonzo
367239275Sgonzo			uether_pause(&sc->sc_ue, hz / 100);
368239275Sgonzo		} while (((usb_ticks_t)(ticks - start_ticks)) < max_ticks);
369240806Shselasky
370239275Sgonzo		if (val & (SMSC_EEPROM_CMD_BUSY | SMSC_EEPROM_CMD_TIMEOUT)) {
371239275Sgonzo			smsc_warn_printf(sc, "eeprom command failed\n");
372239275Sgonzo			err = USB_ERR_IOERROR;
373239275Sgonzo			break;
374239275Sgonzo		}
375239275Sgonzo
376239275Sgonzo		if ((err = smsc_read_reg(sc, SMSC_EEPROM_DATA, &val)) != 0)
377239275Sgonzo			goto done;
378239275Sgonzo
379239275Sgonzo		buf[i] = (val & 0xff);
380239275Sgonzo	}
381239275Sgonzo
382239275Sgonzodone:
383239275Sgonzo	if (!locked)
384239275Sgonzo		SMSC_UNLOCK(sc);
385239275Sgonzo
386239275Sgonzo	return (err);
387239275Sgonzo}
388239275Sgonzo
389239275Sgonzo/**
390239275Sgonzo *	smsc_miibus_readreg - Reads a MII/MDIO register
391239275Sgonzo *	@dev: usb ether device
392239275Sgonzo *	@phy: the number of phy reading from
393239275Sgonzo *	@reg: the register address
394239275Sgonzo *
395239275Sgonzo *	Attempts to read a phy register over the MII bus.
396239275Sgonzo *
397239275Sgonzo *	LOCKING:
398239275Sgonzo *	Takes and releases the device mutex lock if not already held.
399239275Sgonzo *
400240806Shselasky *	RETURNS:
401240806Shselasky *	Returns the 16-bits read from the MII register, if this function fails 0
402239275Sgonzo *	is returned.
403239275Sgonzo */
404239275Sgonzostatic int
405239275Sgonzosmsc_miibus_readreg(device_t dev, int phy, int reg)
406239275Sgonzo{
407239275Sgonzo	struct smsc_softc *sc = device_get_softc(dev);
408239275Sgonzo	int locked;
409239275Sgonzo	uint32_t addr;
410239275Sgonzo	uint32_t val = 0;
411239275Sgonzo
412239275Sgonzo	locked = mtx_owned(&sc->sc_mtx);
413239275Sgonzo	if (!locked)
414239275Sgonzo		SMSC_LOCK(sc);
415239275Sgonzo
416239275Sgonzo	if (smsc_wait_for_bits(sc, SMSC_MII_ADDR, SMSC_MII_BUSY) != 0) {
417239275Sgonzo		smsc_warn_printf(sc, "MII is busy\n");
418239275Sgonzo		goto done;
419239275Sgonzo	}
420239275Sgonzo
421239275Sgonzo	addr = (phy << 11) | (reg << 6) | SMSC_MII_READ;
422239275Sgonzo	smsc_write_reg(sc, SMSC_MII_ADDR, addr);
423239275Sgonzo
424239275Sgonzo	if (smsc_wait_for_bits(sc, SMSC_MII_ADDR, SMSC_MII_BUSY) != 0)
425239275Sgonzo		smsc_warn_printf(sc, "MII read timeout\n");
426239275Sgonzo
427239275Sgonzo	smsc_read_reg(sc, SMSC_MII_DATA, &val);
428239275Sgonzo	val = le32toh(val);
429239275Sgonzo
430239275Sgonzodone:
431239275Sgonzo	if (!locked)
432239275Sgonzo		SMSC_UNLOCK(sc);
433239275Sgonzo
434239275Sgonzo	return (val & 0xFFFF);
435239275Sgonzo}
436239275Sgonzo
437239275Sgonzo/**
438239275Sgonzo *	smsc_miibus_writereg - Writes a MII/MDIO register
439239275Sgonzo *	@dev: usb ether device
440239275Sgonzo *	@phy: the number of phy writing to
441239275Sgonzo *	@reg: the register address
442239275Sgonzo *	@val: the value to write
443239275Sgonzo *
444239275Sgonzo *	Attempts to write a phy register over the MII bus.
445239275Sgonzo *
446239275Sgonzo *	LOCKING:
447239275Sgonzo *	Takes and releases the device mutex lock if not already held.
448239275Sgonzo *
449239275Sgonzo *	RETURNS:
450239275Sgonzo *	Always returns 0 regardless of success or failure.
451239275Sgonzo */
452239275Sgonzostatic int
453239275Sgonzosmsc_miibus_writereg(device_t dev, int phy, int reg, int val)
454239275Sgonzo{
455239275Sgonzo	struct smsc_softc *sc = device_get_softc(dev);
456239275Sgonzo	int locked;
457239275Sgonzo	uint32_t addr;
458239275Sgonzo
459239275Sgonzo	if (sc->sc_phyno != phy)
460239275Sgonzo		return (0);
461239275Sgonzo
462239275Sgonzo	locked = mtx_owned(&sc->sc_mtx);
463239275Sgonzo	if (!locked)
464239275Sgonzo		SMSC_LOCK(sc);
465239275Sgonzo
466239275Sgonzo	if (smsc_wait_for_bits(sc, SMSC_MII_ADDR, SMSC_MII_BUSY) != 0) {
467239275Sgonzo		smsc_warn_printf(sc, "MII is busy\n");
468239275Sgonzo		goto done;
469239275Sgonzo	}
470239275Sgonzo
471239275Sgonzo	val = htole32(val);
472239275Sgonzo	smsc_write_reg(sc, SMSC_MII_DATA, val);
473239275Sgonzo
474239275Sgonzo	addr = (phy << 11) | (reg << 6) | SMSC_MII_WRITE;
475239275Sgonzo	smsc_write_reg(sc, SMSC_MII_ADDR, addr);
476239275Sgonzo
477239275Sgonzo	if (smsc_wait_for_bits(sc, SMSC_MII_ADDR, SMSC_MII_BUSY) != 0)
478239275Sgonzo		smsc_warn_printf(sc, "MII write timeout\n");
479239275Sgonzo
480239275Sgonzodone:
481239275Sgonzo	if (!locked)
482239275Sgonzo		SMSC_UNLOCK(sc);
483239275Sgonzo	return (0);
484239275Sgonzo}
485239275Sgonzo
486239275Sgonzo
487239275Sgonzo
488239275Sgonzo/**
489239275Sgonzo *	smsc_miibus_statchg - Called to detect phy status change
490239275Sgonzo *	@dev: usb ether device
491239275Sgonzo *
492239275Sgonzo *	This function is called periodically by the system to poll for status
493239275Sgonzo *	changes of the link.
494239275Sgonzo *
495239275Sgonzo *	LOCKING:
496239275Sgonzo *	Takes and releases the device mutex lock if not already held.
497239275Sgonzo */
498239275Sgonzostatic void
499239275Sgonzosmsc_miibus_statchg(device_t dev)
500239275Sgonzo{
501239275Sgonzo	struct smsc_softc *sc = device_get_softc(dev);
502239275Sgonzo	struct mii_data *mii = uether_getmii(&sc->sc_ue);
503239275Sgonzo	struct ifnet *ifp;
504239275Sgonzo	int locked;
505239275Sgonzo	int err;
506239275Sgonzo	uint32_t flow;
507239275Sgonzo	uint32_t afc_cfg;
508239275Sgonzo
509239275Sgonzo	locked = mtx_owned(&sc->sc_mtx);
510239275Sgonzo	if (!locked)
511239275Sgonzo		SMSC_LOCK(sc);
512239275Sgonzo
513239275Sgonzo	ifp = uether_getifp(&sc->sc_ue);
514239275Sgonzo	if (mii == NULL || ifp == NULL ||
515239275Sgonzo	    (ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
516239275Sgonzo		goto done;
517239275Sgonzo
518239275Sgonzo	/* Use the MII status to determine link status */
519239275Sgonzo	sc->sc_flags &= ~SMSC_FLAG_LINK;
520239275Sgonzo	if ((mii->mii_media_status & (IFM_ACTIVE | IFM_AVALID)) ==
521239275Sgonzo	    (IFM_ACTIVE | IFM_AVALID)) {
522239275Sgonzo		switch (IFM_SUBTYPE(mii->mii_media_active)) {
523239275Sgonzo			case IFM_10_T:
524239275Sgonzo			case IFM_100_TX:
525239275Sgonzo				sc->sc_flags |= SMSC_FLAG_LINK;
526239275Sgonzo				break;
527239275Sgonzo			case IFM_1000_T:
528239275Sgonzo				/* Gigabit ethernet not supported by chipset */
529239275Sgonzo				break;
530239275Sgonzo			default:
531239275Sgonzo				break;
532239275Sgonzo		}
533239275Sgonzo	}
534239275Sgonzo
535239275Sgonzo	/* Lost link, do nothing. */
536239275Sgonzo	if ((sc->sc_flags & SMSC_FLAG_LINK) == 0) {
537239275Sgonzo		smsc_dbg_printf(sc, "link flag not set\n");
538239275Sgonzo		goto done;
539239275Sgonzo	}
540239275Sgonzo
541239275Sgonzo	err = smsc_read_reg(sc, SMSC_AFC_CFG, &afc_cfg);
542239275Sgonzo	if (err) {
543239275Sgonzo		smsc_warn_printf(sc, "failed to read initial AFC_CFG, error %d\n", err);
544239275Sgonzo		goto done;
545239275Sgonzo	}
546239275Sgonzo
547239275Sgonzo	/* Enable/disable full duplex operation and TX/RX pause */
548239275Sgonzo	if ((IFM_OPTIONS(mii->mii_media_active) & IFM_FDX) != 0) {
549239275Sgonzo		smsc_dbg_printf(sc, "full duplex operation\n");
550239275Sgonzo		sc->sc_mac_csr &= ~SMSC_MAC_CSR_RCVOWN;
551239275Sgonzo		sc->sc_mac_csr |= SMSC_MAC_CSR_FDPX;
552239275Sgonzo
553239275Sgonzo		if ((IFM_OPTIONS(mii->mii_media_active) & IFM_ETH_RXPAUSE) != 0)
554239275Sgonzo			flow = 0xffff0002;
555239275Sgonzo		else
556239275Sgonzo			flow = 0;
557239275Sgonzo
558239275Sgonzo		if ((IFM_OPTIONS(mii->mii_media_active) & IFM_ETH_TXPAUSE) != 0)
559239275Sgonzo			afc_cfg |= 0xf;
560239275Sgonzo		else
561239275Sgonzo			afc_cfg &= ~0xf;
562239275Sgonzo
563239275Sgonzo	} else {
564239275Sgonzo		smsc_dbg_printf(sc, "half duplex operation\n");
565239275Sgonzo		sc->sc_mac_csr &= ~SMSC_MAC_CSR_FDPX;
566239275Sgonzo		sc->sc_mac_csr |= SMSC_MAC_CSR_RCVOWN;
567239275Sgonzo
568239275Sgonzo		flow = 0;
569239275Sgonzo		afc_cfg |= 0xf;
570239275Sgonzo	}
571239275Sgonzo
572239275Sgonzo	err = smsc_write_reg(sc, SMSC_MAC_CSR, sc->sc_mac_csr);
573239275Sgonzo	err += smsc_write_reg(sc, SMSC_FLOW, flow);
574239275Sgonzo	err += smsc_write_reg(sc, SMSC_AFC_CFG, afc_cfg);
575239275Sgonzo	if (err)
576239275Sgonzo		smsc_warn_printf(sc, "media change failed, error %d\n", err);
577239275Sgonzo
578239275Sgonzodone:
579239275Sgonzo	if (!locked)
580239275Sgonzo		SMSC_UNLOCK(sc);
581239275Sgonzo}
582239275Sgonzo
583239275Sgonzo/**
584239275Sgonzo *	smsc_ifmedia_upd - Set media options
585239275Sgonzo *	@ifp: interface pointer
586239275Sgonzo *
587239275Sgonzo *	Basically boilerplate code that simply calls the mii functions to set the
588239275Sgonzo *	media options.
589239275Sgonzo *
590239275Sgonzo *	LOCKING:
591239275Sgonzo *	The device lock must be held before this function is called.
592239275Sgonzo *
593239275Sgonzo *	RETURNS:
594239275Sgonzo *	Returns 0 on success or a negative error code.
595239275Sgonzo */
596239275Sgonzostatic int
597239275Sgonzosmsc_ifmedia_upd(struct ifnet *ifp)
598239275Sgonzo{
599239275Sgonzo	struct smsc_softc *sc = ifp->if_softc;
600239275Sgonzo	struct mii_data *mii = uether_getmii(&sc->sc_ue);
601239275Sgonzo	int err;
602239275Sgonzo
603239275Sgonzo	SMSC_LOCK_ASSERT(sc, MA_OWNED);
604239275Sgonzo
605239275Sgonzo	if (mii->mii_instance) {
606239275Sgonzo		struct mii_softc *miisc;
607239275Sgonzo
608239275Sgonzo		LIST_FOREACH(miisc, &mii->mii_phys, mii_list)
609239275Sgonzo			mii_phy_reset(miisc);
610239275Sgonzo	}
611239275Sgonzo	err = mii_mediachg(mii);
612239275Sgonzo	return (err);
613239275Sgonzo}
614239275Sgonzo
615239275Sgonzo/**
616239275Sgonzo *	smsc_ifmedia_sts - Report current media status
617239275Sgonzo *	@ifp: inet interface pointer
618239275Sgonzo *	@ifmr: interface media request
619239275Sgonzo *
620239275Sgonzo *	Basically boilerplate code that simply calls the mii functions to get the
621239275Sgonzo *	media status.
622239275Sgonzo *
623239275Sgonzo *	LOCKING:
624239275Sgonzo *	Internally takes and releases the device lock.
625239275Sgonzo */
626239275Sgonzostatic void
627239275Sgonzosmsc_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
628239275Sgonzo{
629239275Sgonzo	struct smsc_softc *sc = ifp->if_softc;
630239275Sgonzo	struct mii_data *mii = uether_getmii(&sc->sc_ue);
631239275Sgonzo
632239275Sgonzo	SMSC_LOCK(sc);
633251734Skevlo
634239275Sgonzo	mii_pollstat(mii);
635239275Sgonzo
636239275Sgonzo	SMSC_UNLOCK(sc);
637239275Sgonzo
638251734Skevlo	ifmr->ifm_active = mii->mii_media_active;
639251734Skevlo	ifmr->ifm_status = mii->mii_media_status;
640239275Sgonzo}
641239275Sgonzo
642239275Sgonzo/**
643239275Sgonzo *	smsc_hash - Calculate the hash of a mac address
644239275Sgonzo *	@addr: The mac address to calculate the hash on
645239275Sgonzo *
646239275Sgonzo *	This function is used when configuring a range of m'cast mac addresses to
647239275Sgonzo *	filter on.  The hash of the mac address is put in the device's mac hash
648239275Sgonzo *	table.
649239275Sgonzo *
650239275Sgonzo *	RETURNS:
651239275Sgonzo *	Returns a value from 0-63 value which is the hash of the mac address.
652239275Sgonzo */
653239275Sgonzostatic inline uint32_t
654239275Sgonzosmsc_hash(uint8_t addr[ETHER_ADDR_LEN])
655239275Sgonzo{
656239275Sgonzo	return (ether_crc32_be(addr, ETHER_ADDR_LEN) >> 26) & 0x3f;
657239275Sgonzo}
658239275Sgonzo
659239275Sgonzo/**
660239275Sgonzo *	smsc_setmulti - Setup multicast
661239275Sgonzo *	@ue: usb ethernet device context
662239275Sgonzo *
663239275Sgonzo *	Tells the device to either accept frames with a multicast mac address, a
664239275Sgonzo *	select group of m'cast mac addresses or just the devices mac address.
665251734Skevlo *
666239275Sgonzo *	LOCKING:
667239275Sgonzo *	Should be called with the SMSC lock held.
668239275Sgonzo */
669239275Sgonzostatic void
670239275Sgonzosmsc_setmulti(struct usb_ether *ue)
671239275Sgonzo{
672239275Sgonzo	struct smsc_softc *sc = uether_getsc(ue);
673239275Sgonzo	struct ifnet *ifp = uether_getifp(ue);
674239275Sgonzo	struct ifmultiaddr *ifma;
675239275Sgonzo	uint32_t hashtbl[2] = { 0, 0 };
676239275Sgonzo	uint32_t hash;
677239275Sgonzo
678239275Sgonzo	SMSC_LOCK_ASSERT(sc, MA_OWNED);
679239275Sgonzo
680239275Sgonzo	if (ifp->if_flags & (IFF_ALLMULTI | IFF_PROMISC)) {
681239275Sgonzo		smsc_dbg_printf(sc, "receive all multicast enabled\n");
682239275Sgonzo		sc->sc_mac_csr |= SMSC_MAC_CSR_MCPAS;
683239275Sgonzo		sc->sc_mac_csr &= ~SMSC_MAC_CSR_HPFILT;
684239275Sgonzo
685239275Sgonzo	} else {
686239275Sgonzo		/* Take the lock of the mac address list before hashing each of them */
687239275Sgonzo		if_maddr_rlock(ifp);
688239275Sgonzo
689239275Sgonzo		if (!TAILQ_EMPTY(&ifp->if_multiaddrs)) {
690239275Sgonzo			/* We are filtering on a set of address so calculate hashes of each
691239275Sgonzo			 * of the address and set the corresponding bits in the register.
692239275Sgonzo			 */
693239275Sgonzo			sc->sc_mac_csr |= SMSC_MAC_CSR_HPFILT;
694239275Sgonzo			sc->sc_mac_csr &= ~(SMSC_MAC_CSR_PRMS | SMSC_MAC_CSR_MCPAS);
695239275Sgonzo
696239275Sgonzo			TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
697239275Sgonzo				if (ifma->ifma_addr->sa_family != AF_LINK)
698239275Sgonzo					continue;
699239275Sgonzo
700239275Sgonzo				hash = smsc_hash(LLADDR((struct sockaddr_dl *)ifma->ifma_addr));
701239275Sgonzo				hashtbl[hash >> 5] |= 1 << (hash & 0x1F);
702239275Sgonzo			}
703239275Sgonzo		} else {
704239275Sgonzo			/* Only receive packets with destination set to our mac address */
705239275Sgonzo			sc->sc_mac_csr &= ~(SMSC_MAC_CSR_MCPAS | SMSC_MAC_CSR_HPFILT);
706239275Sgonzo		}
707239275Sgonzo
708239275Sgonzo		if_maddr_runlock(ifp);
709239275Sgonzo
710239275Sgonzo		/* Debug */
711239275Sgonzo		if (sc->sc_mac_csr & SMSC_MAC_CSR_HPFILT)
712239275Sgonzo			smsc_dbg_printf(sc, "receive select group of macs\n");
713239275Sgonzo		else
714239275Sgonzo			smsc_dbg_printf(sc, "receive own packets only\n");
715239275Sgonzo	}
716239275Sgonzo
717239275Sgonzo	/* Write the hash table and mac control registers */
718239275Sgonzo	smsc_write_reg(sc, SMSC_HASHH, hashtbl[1]);
719239275Sgonzo	smsc_write_reg(sc, SMSC_HASHL, hashtbl[0]);
720239275Sgonzo	smsc_write_reg(sc, SMSC_MAC_CSR, sc->sc_mac_csr);
721239275Sgonzo}
722239275Sgonzo
723239275Sgonzo
724239275Sgonzo/**
725239275Sgonzo *	smsc_setpromisc - Enables/disables promiscuous mode
726239275Sgonzo *	@ue: usb ethernet device context
727239275Sgonzo *
728239275Sgonzo *	LOCKING:
729239275Sgonzo *	Should be called with the SMSC lock held.
730239275Sgonzo */
731239275Sgonzostatic void
732239275Sgonzosmsc_setpromisc(struct usb_ether *ue)
733239275Sgonzo{
734239275Sgonzo	struct smsc_softc *sc = uether_getsc(ue);
735239275Sgonzo	struct ifnet *ifp = uether_getifp(ue);
736239275Sgonzo
737239275Sgonzo	smsc_dbg_printf(sc, "promiscuous mode %sabled\n",
738239275Sgonzo	                (ifp->if_flags & IFF_PROMISC) ? "en" : "dis");
739239275Sgonzo
740239275Sgonzo	SMSC_LOCK_ASSERT(sc, MA_OWNED);
741239275Sgonzo
742239275Sgonzo	if (ifp->if_flags & IFF_PROMISC)
743239275Sgonzo		sc->sc_mac_csr |= SMSC_MAC_CSR_PRMS;
744239275Sgonzo	else
745239275Sgonzo		sc->sc_mac_csr &= ~SMSC_MAC_CSR_PRMS;
746239275Sgonzo
747239275Sgonzo	smsc_write_reg(sc, SMSC_MAC_CSR, sc->sc_mac_csr);
748239275Sgonzo}
749239275Sgonzo
750239275Sgonzo
751239275Sgonzo/**
752239275Sgonzo *	smsc_sethwcsum - Enable or disable H/W UDP and TCP checksumming
753239275Sgonzo *	@sc: driver soft context
754239275Sgonzo *
755239275Sgonzo *	LOCKING:
756239275Sgonzo *	Should be called with the SMSC lock held.
757239275Sgonzo *
758239275Sgonzo *	RETURNS:
759239275Sgonzo *	Returns 0 on success or a negative error code.
760239275Sgonzo */
761239275Sgonzostatic int smsc_sethwcsum(struct smsc_softc *sc)
762239275Sgonzo{
763239275Sgonzo	struct ifnet *ifp = uether_getifp(&sc->sc_ue);
764239275Sgonzo	uint32_t val;
765239275Sgonzo	int err;
766239275Sgonzo
767239275Sgonzo	if (!ifp)
768239275Sgonzo		return (-EIO);
769239275Sgonzo
770239275Sgonzo	SMSC_LOCK_ASSERT(sc, MA_OWNED);
771239275Sgonzo
772239275Sgonzo	err = smsc_read_reg(sc, SMSC_COE_CTRL, &val);
773239275Sgonzo	if (err != 0) {
774239275Sgonzo		smsc_warn_printf(sc, "failed to read SMSC_COE_CTRL (err=%d)\n", err);
775239275Sgonzo		return (err);
776239275Sgonzo	}
777239275Sgonzo
778239275Sgonzo	/* Enable/disable the Rx checksum */
779239275Sgonzo	if ((ifp->if_capabilities & ifp->if_capenable) & IFCAP_RXCSUM)
780239275Sgonzo		val |= SMSC_COE_CTRL_RX_EN;
781239275Sgonzo	else
782239275Sgonzo		val &= ~SMSC_COE_CTRL_RX_EN;
783239275Sgonzo
784239275Sgonzo	/* Enable/disable the Tx checksum (currently not supported) */
785239275Sgonzo	if ((ifp->if_capabilities & ifp->if_capenable) & IFCAP_TXCSUM)
786239275Sgonzo		val |= SMSC_COE_CTRL_TX_EN;
787239275Sgonzo	else
788239275Sgonzo		val &= ~SMSC_COE_CTRL_TX_EN;
789239275Sgonzo
790239275Sgonzo	err = smsc_write_reg(sc, SMSC_COE_CTRL, val);
791239275Sgonzo	if (err != 0) {
792239275Sgonzo		smsc_warn_printf(sc, "failed to write SMSC_COE_CTRL (err=%d)\n", err);
793239275Sgonzo		return (err);
794239275Sgonzo	}
795239275Sgonzo
796239275Sgonzo	return (0);
797239275Sgonzo}
798239275Sgonzo
799239275Sgonzo
800239275Sgonzo/**
801239275Sgonzo *	smsc_setmacaddress - Sets the mac address in the device
802239275Sgonzo *	@sc: driver soft context
803239275Sgonzo *	@addr: pointer to array contain at least 6 bytes of the mac
804239275Sgonzo *
805239275Sgonzo *	Writes the MAC address into the device, usually the MAC is programmed with
806239275Sgonzo *	values from the EEPROM.
807239275Sgonzo *
808239275Sgonzo *	LOCKING:
809239275Sgonzo *	Should be called with the SMSC lock held.
810239275Sgonzo *
811239275Sgonzo *	RETURNS:
812239275Sgonzo *	Returns 0 on success or a negative error code.
813239275Sgonzo */
814239275Sgonzostatic int
815239275Sgonzosmsc_setmacaddress(struct smsc_softc *sc, const uint8_t *addr)
816239275Sgonzo{
817239275Sgonzo	int err;
818239275Sgonzo	uint32_t val;
819239275Sgonzo
820239275Sgonzo	smsc_dbg_printf(sc, "setting mac address to %02x:%02x:%02x:%02x:%02x:%02x\n",
821239275Sgonzo	                addr[0], addr[1], addr[2], addr[3], addr[4], addr[5]);
822239275Sgonzo
823239275Sgonzo	SMSC_LOCK_ASSERT(sc, MA_OWNED);
824239275Sgonzo
825239275Sgonzo	val = (addr[3] << 24) | (addr[2] << 16) | (addr[1] << 8) | addr[0];
826239275Sgonzo	if ((err = smsc_write_reg(sc, SMSC_MAC_ADDRL, val)) != 0)
827239275Sgonzo		goto done;
828239275Sgonzo
829239275Sgonzo	val = (addr[5] << 8) | addr[4];
830239275Sgonzo	err = smsc_write_reg(sc, SMSC_MAC_ADDRH, val);
831239275Sgonzo
832239275Sgonzodone:
833239275Sgonzo	return (err);
834239275Sgonzo}
835239275Sgonzo
836239275Sgonzo/**
837239275Sgonzo *	smsc_reset - Reset the SMSC chip
838239275Sgonzo *	@sc: device soft context
839239275Sgonzo *
840239275Sgonzo *	LOCKING:
841239275Sgonzo *	Should be called with the SMSC lock held.
842239275Sgonzo */
843239275Sgonzostatic void
844239275Sgonzosmsc_reset(struct smsc_softc *sc)
845239275Sgonzo{
846239275Sgonzo	struct usb_config_descriptor *cd;
847239275Sgonzo	usb_error_t err;
848239275Sgonzo
849239275Sgonzo	cd = usbd_get_config_descriptor(sc->sc_ue.ue_udev);
850239275Sgonzo
851239275Sgonzo	err = usbd_req_set_config(sc->sc_ue.ue_udev, &sc->sc_mtx,
852239275Sgonzo	                          cd->bConfigurationValue);
853239275Sgonzo	if (err)
854239275Sgonzo		smsc_warn_printf(sc, "reset failed (ignored)\n");
855239275Sgonzo
856239275Sgonzo	/* Wait a little while for the chip to get its brains in order. */
857239275Sgonzo	uether_pause(&sc->sc_ue, hz / 100);
858239275Sgonzo
859239275Sgonzo	/* Reinitialize controller to achieve full reset. */
860239275Sgonzo	smsc_chip_init(sc);
861239275Sgonzo}
862239275Sgonzo
863239275Sgonzo
864239275Sgonzo/**
865239275Sgonzo *	smsc_init - Initialises the LAN95xx chip
866239275Sgonzo *	@ue: USB ether interface
867239275Sgonzo *
868239275Sgonzo *	Called when the interface is brought up (i.e. ifconfig ue0 up), this
869239275Sgonzo *	initialise the interface and the rx/tx pipes.
870239275Sgonzo *
871239275Sgonzo *	LOCKING:
872239275Sgonzo *	Should be called with the SMSC lock held.
873239275Sgonzo */
874239275Sgonzostatic void
875239275Sgonzosmsc_init(struct usb_ether *ue)
876239275Sgonzo{
877239275Sgonzo	struct smsc_softc *sc = uether_getsc(ue);
878239275Sgonzo	struct ifnet *ifp = uether_getifp(ue);
879239275Sgonzo
880239275Sgonzo	SMSC_LOCK_ASSERT(sc, MA_OWNED);
881239275Sgonzo
882239275Sgonzo	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0)
883239275Sgonzo		return;
884239275Sgonzo
885239275Sgonzo	/* Cancel pending I/O */
886239275Sgonzo	smsc_stop(ue);
887239275Sgonzo
888239275Sgonzo#if __FreeBSD_version <= 1000000
889239275Sgonzo	/* On earlier versions this was the first place we could tell the system
890239275Sgonzo	 * that we supported h/w csuming, however this is only called after the
891239275Sgonzo	 * the interface has been brought up - not ideal.
892239275Sgonzo	 */
893239275Sgonzo	if (!(ifp->if_capabilities & IFCAP_RXCSUM)) {
894239275Sgonzo		ifp->if_capabilities |= IFCAP_RXCSUM;
895239275Sgonzo		ifp->if_capenable |= IFCAP_RXCSUM;
896239275Sgonzo		ifp->if_hwassist = 0;
897239275Sgonzo	}
898239275Sgonzo
899239275Sgonzo	/* TX checksuming is disabled for now
900239275Sgonzo	ifp->if_capabilities |= IFCAP_TXCSUM;
901239275Sgonzo	ifp->if_capenable |= IFCAP_TXCSUM;
902239275Sgonzo	ifp->if_hwassist = CSUM_TCP | CSUM_UDP;
903239275Sgonzo	*/
904239275Sgonzo#endif
905239275Sgonzo
906239275Sgonzo	/* Reset the ethernet interface. */
907239275Sgonzo	smsc_reset(sc);
908239275Sgonzo
909239275Sgonzo	/* Load the multicast filter. */
910239275Sgonzo	smsc_setmulti(ue);
911239275Sgonzo
912239275Sgonzo	/* TCP/UDP checksum offload engines. */
913239275Sgonzo	smsc_sethwcsum(sc);
914239275Sgonzo
915239275Sgonzo	usbd_xfer_set_stall(sc->sc_xfer[SMSC_BULK_DT_WR]);
916239275Sgonzo
917239275Sgonzo	/* Indicate we are up and running. */
918239275Sgonzo	ifp->if_drv_flags |= IFF_DRV_RUNNING;
919239275Sgonzo
920239275Sgonzo	/* Switch to selected media. */
921239275Sgonzo	smsc_ifmedia_upd(ifp);
922239275Sgonzo	smsc_start(ue);
923239275Sgonzo}
924239275Sgonzo
925239275Sgonzo/**
926239275Sgonzo *	smsc_bulk_read_callback - Read callback used to process the USB URB
927239275Sgonzo *	@xfer: the USB transfer
928239275Sgonzo *	@error:
929239275Sgonzo *
930239275Sgonzo *	Reads the URB data which can contain one or more ethernet frames, the
931239275Sgonzo *	frames are copyed into a mbuf and given to the system.
932239275Sgonzo *
933239275Sgonzo *	LOCKING:
934239275Sgonzo *	No locking required, doesn't access internal driver settings.
935239275Sgonzo */
936239275Sgonzostatic void
937239275Sgonzosmsc_bulk_read_callback(struct usb_xfer *xfer, usb_error_t error)
938239275Sgonzo{
939239275Sgonzo	struct smsc_softc *sc = usbd_xfer_softc(xfer);
940239275Sgonzo	struct usb_ether *ue = &sc->sc_ue;
941239275Sgonzo	struct ifnet *ifp = uether_getifp(ue);
942239275Sgonzo	struct mbuf *m;
943239275Sgonzo	struct usb_page_cache *pc;
944239275Sgonzo	uint32_t rxhdr;
945239275Sgonzo	uint16_t pktlen;
946239275Sgonzo	int off;
947239275Sgonzo	int actlen;
948239275Sgonzo
949239275Sgonzo	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
950239275Sgonzo	smsc_dbg_printf(sc, "rx : actlen %d\n", actlen);
951239275Sgonzo
952239275Sgonzo	switch (USB_GET_STATE(xfer)) {
953239275Sgonzo	case USB_ST_TRANSFERRED:
954239275Sgonzo
955239275Sgonzo		/* There is always a zero length frame after bringing the IF up */
956239275Sgonzo		if (actlen < (sizeof(rxhdr) + ETHER_CRC_LEN))
957239275Sgonzo			goto tr_setup;
958239275Sgonzo
959239275Sgonzo		/* There maybe multiple packets in the USB frame, each will have a
960239275Sgonzo		 * header and each needs to have it's own mbuf allocated and populated
961239275Sgonzo		 * for it.
962239275Sgonzo		 */
963239275Sgonzo		pc = usbd_xfer_get_frame(xfer, 0);
964239275Sgonzo		off = 0;
965239275Sgonzo
966239275Sgonzo		while (off < actlen) {
967239275Sgonzo
968239275Sgonzo			/* The frame header is always aligned on a 4 byte boundary */
969239275Sgonzo			off = ((off + 0x3) & ~0x3);
970239275Sgonzo
971239275Sgonzo			usbd_copy_out(pc, off, &rxhdr, sizeof(rxhdr));
972239275Sgonzo			off += (sizeof(rxhdr) + ETHER_ALIGN);
973239275Sgonzo			rxhdr = le32toh(rxhdr);
974239275Sgonzo
975239275Sgonzo			pktlen = (uint16_t)SMSC_RX_STAT_FRM_LENGTH(rxhdr);
976239275Sgonzo
977239275Sgonzo			smsc_dbg_printf(sc, "rx : rxhdr 0x%08x : pktlen %d : actlen %d : "
978239275Sgonzo			                "off %d\n", rxhdr, pktlen, actlen, off);
979239275Sgonzo
980239275Sgonzo
981239275Sgonzo			if (rxhdr & SMSC_RX_STAT_ERROR) {
982239275Sgonzo				smsc_dbg_printf(sc, "rx error (hdr 0x%08x)\n", rxhdr);
983239275Sgonzo				ifp->if_ierrors++;
984239275Sgonzo				if (rxhdr & SMSC_RX_STAT_COLLISION)
985239275Sgonzo					ifp->if_collisions++;
986239275Sgonzo			} else {
987239275Sgonzo
988239275Sgonzo				/* Check if the ethernet frame is too big or too small */
989239275Sgonzo				if ((pktlen < ETHER_HDR_LEN) || (pktlen > (actlen - off)))
990239275Sgonzo					goto tr_setup;
991239275Sgonzo
992239275Sgonzo				/* Create a new mbuf to store the packet in */
993239275Sgonzo				m = uether_newbuf();
994239275Sgonzo				if (m == NULL) {
995239275Sgonzo					smsc_warn_printf(sc, "failed to create new mbuf\n");
996239275Sgonzo					ifp->if_iqdrops++;
997239275Sgonzo					goto tr_setup;
998239275Sgonzo				}
999239275Sgonzo
1000239275Sgonzo				usbd_copy_out(pc, off, mtod(m, uint8_t *), pktlen);
1001239275Sgonzo
1002239275Sgonzo				/* Check if RX TCP/UDP checksumming is being offloaded */
1003239275Sgonzo				if ((ifp->if_capenable & IFCAP_RXCSUM) != 0) {
1004239275Sgonzo
1005239275Sgonzo					/* Remove the extra 2 bytes of the csum */
1006239275Sgonzo					pktlen -= 2;
1007239275Sgonzo
1008239275Sgonzo					/* The checksum appears to be simplistically calculated
1009271832Sglebius					 * over the udp/tcp header and data up to the end of the
1010239275Sgonzo					 * eth frame.  Which means if the eth frame is padded
1011271832Sglebius					 * the csum calculation is incorrectly performed over
1012239275Sgonzo					 * the padding bytes as well. Therefore to be safe we
1013239275Sgonzo					 * ignore the H/W csum on frames less than or equal to
1014239275Sgonzo					 * 64 bytes.
1015239275Sgonzo					 */
1016239275Sgonzo					if (pktlen > ETHER_MIN_LEN) {
1017239275Sgonzo
1018239275Sgonzo						/* Indicate the UDP/TCP csum has been calculated */
1019239275Sgonzo						m->m_pkthdr.csum_flags |= CSUM_DATA_VALID;
1020239275Sgonzo
1021239275Sgonzo						/* Copy the TCP/UDP checksum from the last 2 bytes
1022271832Sglebius						 * of the transfer and put in the csum_data field.
1023239275Sgonzo						 */
1024239275Sgonzo						usbd_copy_out(pc, (off + pktlen),
1025239275Sgonzo									  &m->m_pkthdr.csum_data, 2);
1026239275Sgonzo
1027239275Sgonzo						/* The data is copied in network order, but the
1028239275Sgonzo						 * csum algorithm in the kernel expects it to be
1029239275Sgonzo						 * in host network order.
1030246196Shselasky						 */
1031246196Shselasky						m->m_pkthdr.csum_data = ntohs(m->m_pkthdr.csum_data);
1032246196Shselasky
1033246196Shselasky						smsc_dbg_printf(sc, "RX checksum offloaded (0x%04x)\n",
1034239275Sgonzo										m->m_pkthdr.csum_data);
1035239275Sgonzo					}
1036239275Sgonzo
1037239275Sgonzo					/* Need to adjust the offset as well or we'll be off
1038239275Sgonzo					 * by 2 because the csum is removed from the packet
1039239275Sgonzo					 * length.
1040239275Sgonzo					 */
1041239275Sgonzo					off += 2;
1042239275Sgonzo				}
1043239275Sgonzo
1044239275Sgonzo				/* Finally enqueue the mbuf on the receive queue */
1045246196Shselasky				uether_rxmbuf(ue, m, pktlen);
1046246196Shselasky			}
1047239275Sgonzo
1048265371Stuexen			/* Update the offset to move to the next potential packet */
1049265371Stuexen			off += pktlen;
1050265371Stuexen		}
1051265371Stuexen
1052265371Stuexen		/* FALLTHROUGH */
1053265371Stuexen
1054265371Stuexen	case USB_ST_SETUP:
1055265371Stuexentr_setup:
1056265371Stuexen		usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
1057265371Stuexen		usbd_transfer_submit(xfer);
1058265371Stuexen		uether_rxflush(ue);
1059265371Stuexen		return;
1060265371Stuexen
1061265371Stuexen	default:
1062265371Stuexen		if (error != USB_ERR_CANCELLED) {
1063265371Stuexen			smsc_warn_printf(sc, "bulk read error, %s\n", usbd_errstr(error));
1064265371Stuexen			usbd_xfer_set_stall(xfer);
1065265371Stuexen			goto tr_setup;
1066265371Stuexen		}
1067265371Stuexen		return;
1068265371Stuexen	}
1069265371Stuexen}
1070265371Stuexen
1071265371Stuexen/**
1072265371Stuexen *	smsc_bulk_write_callback - Write callback used to send ethernet frame(s)
1073265371Stuexen *	@xfer: the USB transfer
1074239275Sgonzo *	@error: error code if the transfers is in an errored state
1075239275Sgonzo *
1076239275Sgonzo *	The main write function that pulls ethernet frames off the queue and sends
1077239275Sgonzo *	them out.
1078239275Sgonzo *
1079239275Sgonzo *	LOCKING:
1080239275Sgonzo *
1081239275Sgonzo */
1082239275Sgonzostatic void
1083239275Sgonzosmsc_bulk_write_callback(struct usb_xfer *xfer, usb_error_t error)
1084241033Shselasky{
1085241034Shselasky	struct smsc_softc *sc = usbd_xfer_softc(xfer);
1086241034Shselasky	struct ifnet *ifp = uether_getifp(&sc->sc_ue);
1087241034Shselasky	struct usb_page_cache *pc;
1088241034Shselasky	struct mbuf *m;
1089241034Shselasky	uint32_t txhdr;
1090239275Sgonzo	uint32_t frm_len = 0;
1091239275Sgonzo	int nframes;
1092239275Sgonzo
1093239275Sgonzo	switch (USB_GET_STATE(xfer)) {
1094239275Sgonzo	case USB_ST_TRANSFERRED:
1095239275Sgonzo		ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1096239275Sgonzo		/* FALLTHROUGH */
1097239275Sgonzo
1098239275Sgonzo	case USB_ST_SETUP:
1099239275Sgonzotr_setup:
1100239275Sgonzo		if ((sc->sc_flags & SMSC_FLAG_LINK) == 0 ||
1101239275Sgonzo			(ifp->if_drv_flags & IFF_DRV_OACTIVE) != 0) {
1102239275Sgonzo			/* Don't send anything if there is no link or controller is busy. */
1103239275Sgonzo			return;
1104239275Sgonzo		}
1105239275Sgonzo
1106239275Sgonzo		for (nframes = 0; nframes < 16 &&
1107239275Sgonzo		    !IFQ_DRV_IS_EMPTY(&ifp->if_snd); nframes++) {
1108239275Sgonzo			IFQ_DRV_DEQUEUE(&ifp->if_snd, m);
1109239275Sgonzo			if (m == NULL)
1110239275Sgonzo				break;
1111239275Sgonzo			usbd_xfer_set_frame_offset(xfer, nframes * MCLBYTES,
1112239275Sgonzo			    nframes);
1113239275Sgonzo			frm_len = 0;
1114239275Sgonzo			pc = usbd_xfer_get_frame(xfer, nframes);
1115239275Sgonzo
1116239275Sgonzo			/* Each frame is prefixed with two 32-bit values describing the
1117239275Sgonzo			 * length of the packet and buffer.
1118239275Sgonzo			 */
1119239275Sgonzo			txhdr = SMSC_TX_CTRL_0_BUF_SIZE(m->m_pkthdr.len) |
1120239275Sgonzo					SMSC_TX_CTRL_0_FIRST_SEG | SMSC_TX_CTRL_0_LAST_SEG;
1121239275Sgonzo			txhdr = htole32(txhdr);
1122239275Sgonzo			usbd_copy_in(pc, 0, &txhdr, sizeof(txhdr));
1123239275Sgonzo
1124239275Sgonzo			txhdr = SMSC_TX_CTRL_1_PKT_LENGTH(m->m_pkthdr.len);
1125239275Sgonzo			txhdr = htole32(txhdr);
1126239275Sgonzo			usbd_copy_in(pc, 4, &txhdr, sizeof(txhdr));
1127239275Sgonzo
1128239275Sgonzo			frm_len += 8;
1129239275Sgonzo
1130239275Sgonzo			/* Next copy in the actual packet */
1131239275Sgonzo			usbd_m_copy_in(pc, frm_len, m, 0, m->m_pkthdr.len);
1132239275Sgonzo			frm_len += m->m_pkthdr.len;
1133239275Sgonzo
1134239275Sgonzo			ifp->if_opackets++;
1135239275Sgonzo
1136239275Sgonzo			/* If there's a BPF listener, bounce a copy of this frame to him */
1137239275Sgonzo			BPF_MTAP(ifp, m);
1138239275Sgonzo
1139239275Sgonzo			m_freem(m);
1140239275Sgonzo
1141239275Sgonzo			/* Set frame length. */
1142239275Sgonzo			usbd_xfer_set_frame_len(xfer, nframes, frm_len);
1143239275Sgonzo		}
1144239275Sgonzo		if (nframes != 0) {
1145239275Sgonzo			usbd_xfer_set_frames(xfer, nframes);
1146239275Sgonzo			usbd_transfer_submit(xfer);
1147239275Sgonzo			ifp->if_drv_flags |= IFF_DRV_OACTIVE;
1148239275Sgonzo		}
1149239275Sgonzo		return;
1150239275Sgonzo
1151239275Sgonzo	default:
1152239275Sgonzo		ifp->if_oerrors++;
1153239275Sgonzo		ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1154239275Sgonzo
1155239275Sgonzo		if (error != USB_ERR_CANCELLED) {
1156239275Sgonzo			smsc_err_printf(sc, "usb error on tx: %s\n", usbd_errstr(error));
1157239275Sgonzo			usbd_xfer_set_stall(xfer);
1158239275Sgonzo			goto tr_setup;
1159239275Sgonzo		}
1160239275Sgonzo		return;
1161239275Sgonzo	}
1162239275Sgonzo}
1163239275Sgonzo
1164239275Sgonzo/**
1165239275Sgonzo *	smsc_tick - Called periodically to monitor the state of the LAN95xx chip
1166239275Sgonzo *	@ue: USB ether interface
1167239275Sgonzo *
1168239275Sgonzo *	Simply calls the mii status functions to check the state of the link.
1169239275Sgonzo *
1170239275Sgonzo *	LOCKING:
1171239275Sgonzo *	Should be called with the SMSC lock held.
1172239275Sgonzo */
1173239275Sgonzostatic void
1174239275Sgonzosmsc_tick(struct usb_ether *ue)
1175239275Sgonzo{
1176239275Sgonzo	struct smsc_softc *sc = uether_getsc(ue);
1177239275Sgonzo	struct mii_data *mii = uether_getmii(&sc->sc_ue);;
1178271832Sglebius
1179239275Sgonzo	SMSC_LOCK_ASSERT(sc, MA_OWNED);
1180239275Sgonzo
1181239275Sgonzo	mii_tick(mii);
1182239275Sgonzo	if ((sc->sc_flags & SMSC_FLAG_LINK) == 0) {
1183239275Sgonzo		smsc_miibus_statchg(ue->ue_dev);
1184239275Sgonzo		if ((sc->sc_flags & SMSC_FLAG_LINK) != 0)
1185239275Sgonzo			smsc_start(ue);
1186239275Sgonzo	}
1187239275Sgonzo}
1188239275Sgonzo
1189239275Sgonzo/**
1190239275Sgonzo *	smsc_start - Starts communication with the LAN95xx chip
1191239275Sgonzo *	@ue: USB ether interface
1192239275Sgonzo *
1193239275Sgonzo *
1194239275Sgonzo *
1195239275Sgonzo */
1196271832Sglebiusstatic void
1197239275Sgonzosmsc_start(struct usb_ether *ue)
1198239275Sgonzo{
1199239275Sgonzo	struct smsc_softc *sc = uether_getsc(ue);
1200239275Sgonzo
1201239275Sgonzo	/*
1202239275Sgonzo	 * start the USB transfers, if not already started:
1203239275Sgonzo	 */
1204239275Sgonzo	usbd_transfer_start(sc->sc_xfer[SMSC_BULK_DT_RD]);
1205239275Sgonzo	usbd_transfer_start(sc->sc_xfer[SMSC_BULK_DT_WR]);
1206239275Sgonzo}
1207239275Sgonzo
1208239275Sgonzo/**
1209239275Sgonzo *	smsc_stop - Stops communication with the LAN95xx chip
1210239275Sgonzo *	@ue: USB ether interface
1211239275Sgonzo *
1212239275Sgonzo *
1213239275Sgonzo *
1214239275Sgonzo */
1215239275Sgonzostatic void
1216239275Sgonzosmsc_stop(struct usb_ether *ue)
1217239275Sgonzo{
1218239275Sgonzo	struct smsc_softc *sc = uether_getsc(ue);
1219239275Sgonzo	struct ifnet *ifp = uether_getifp(ue);
1220239275Sgonzo
1221241844Seadler	SMSC_LOCK_ASSERT(sc, MA_OWNED);
1222239275Sgonzo
1223239275Sgonzo	ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
1224239275Sgonzo	sc->sc_flags &= ~SMSC_FLAG_LINK;
1225239275Sgonzo
1226239275Sgonzo	/*
1227239275Sgonzo	 * stop all the transfers, if not already stopped:
1228239275Sgonzo	 */
1229239275Sgonzo	usbd_transfer_stop(sc->sc_xfer[SMSC_BULK_DT_WR]);
1230239275Sgonzo	usbd_transfer_stop(sc->sc_xfer[SMSC_BULK_DT_RD]);
1231239275Sgonzo}
1232239275Sgonzo
1233239275Sgonzo/**
1234239275Sgonzo *	smsc_phy_init - Initialises the in-built SMSC phy
1235239275Sgonzo *	@sc: driver soft context
1236239275Sgonzo *
1237239275Sgonzo *	Resets the PHY part of the chip and then initialises it to default
1238239275Sgonzo *	values.  The 'link down' and 'auto-negotiation complete' interrupts
1239239275Sgonzo *	from the PHY are also enabled, however we don't monitor the interrupt
1240239275Sgonzo *	endpoints for the moment.
1241239275Sgonzo *
1242239275Sgonzo *	RETURNS:
1243239275Sgonzo *	Returns 0 on success or EIO if failed to reset the PHY.
1244239275Sgonzo */
1245239275Sgonzostatic int
1246239275Sgonzosmsc_phy_init(struct smsc_softc *sc)
1247239275Sgonzo{
1248239275Sgonzo	int bmcr;
1249239275Sgonzo	usb_ticks_t start_ticks;
1250239275Sgonzo	const usb_ticks_t max_ticks = USB_MS_TO_TICKS(1000);
1251239275Sgonzo
1252239275Sgonzo	SMSC_LOCK_ASSERT(sc, MA_OWNED);
1253239275Sgonzo
1254239275Sgonzo	/* Reset phy and wait for reset to complete */
1255239275Sgonzo	smsc_miibus_writereg(sc->sc_ue.ue_dev, sc->sc_phyno, MII_BMCR, BMCR_RESET);
1256239275Sgonzo
1257239275Sgonzo	start_ticks = ticks;
1258239275Sgonzo	do {
1259239275Sgonzo		uether_pause(&sc->sc_ue, hz / 100);
1260239275Sgonzo		bmcr = smsc_miibus_readreg(sc->sc_ue.ue_dev, sc->sc_phyno, MII_BMCR);
1261239275Sgonzo	} while ((bmcr & MII_BMCR) && ((ticks - start_ticks) < max_ticks));
1262239275Sgonzo
1263239275Sgonzo	if (((usb_ticks_t)(ticks - start_ticks)) >= max_ticks) {
1264239275Sgonzo		smsc_err_printf(sc, "PHY reset timed-out");
1265239275Sgonzo		return (EIO);
1266239275Sgonzo	}
1267239275Sgonzo
1268239275Sgonzo	smsc_miibus_writereg(sc->sc_ue.ue_dev, sc->sc_phyno, MII_ANAR,
1269239275Sgonzo	                     ANAR_10 | ANAR_10_FD | ANAR_TX | ANAR_TX_FD |  /* all modes */
1270239275Sgonzo	                     ANAR_CSMA |
1271239275Sgonzo	                     ANAR_FC |
1272239275Sgonzo	                     ANAR_PAUSE_ASYM);
1273239275Sgonzo
1274239275Sgonzo	/* Setup the phy to interrupt when the link goes down or autoneg completes */
1275239275Sgonzo	smsc_miibus_readreg(sc->sc_ue.ue_dev, sc->sc_phyno, SMSC_PHY_INTR_STAT);
1276239275Sgonzo	smsc_miibus_writereg(sc->sc_ue.ue_dev, sc->sc_phyno, SMSC_PHY_INTR_MASK,
1277239275Sgonzo	                     (SMSC_PHY_INTR_ANEG_COMP | SMSC_PHY_INTR_LINK_DOWN));
1278239275Sgonzo
1279239275Sgonzo	/* Restart auto-negotation */
1280239275Sgonzo	bmcr = smsc_miibus_readreg(sc->sc_ue.ue_dev, sc->sc_phyno, MII_BMCR);
1281239275Sgonzo	bmcr |= BMCR_STARTNEG;
1282239275Sgonzo	smsc_miibus_writereg(sc->sc_ue.ue_dev, sc->sc_phyno, MII_BMCR, bmcr);
1283239275Sgonzo
1284239275Sgonzo	return (0);
1285239275Sgonzo}
1286239275Sgonzo
1287239275Sgonzo
1288239275Sgonzo/**
1289239275Sgonzo *	smsc_chip_init - Initialises the chip after power on
1290239275Sgonzo *	@sc: driver soft context
1291239275Sgonzo *
1292239275Sgonzo *	This initialisation sequence is modelled on the procedure in the Linux
1293239275Sgonzo *	driver.
1294240806Shselasky *
1295239275Sgonzo *	RETURNS:
1296239275Sgonzo *	Returns 0 on success or an error code on failure.
1297239275Sgonzo */
1298239275Sgonzostatic int
1299239275Sgonzosmsc_chip_init(struct smsc_softc *sc)
1300239275Sgonzo{
1301239275Sgonzo	int err;
1302239275Sgonzo	int locked;
1303239275Sgonzo	uint32_t reg_val;
1304239275Sgonzo	int burst_cap;
1305239275Sgonzo
1306239275Sgonzo	locked = mtx_owned(&sc->sc_mtx);
1307240806Shselasky	if (!locked)
1308239275Sgonzo		SMSC_LOCK(sc);
1309239275Sgonzo
1310239275Sgonzo	/* Enter H/W config mode */
1311239275Sgonzo	smsc_write_reg(sc, SMSC_HW_CFG, SMSC_HW_CFG_LRST);
1312239275Sgonzo
1313239275Sgonzo	if ((err = smsc_wait_for_bits(sc, SMSC_HW_CFG, SMSC_HW_CFG_LRST)) != 0) {
1314239275Sgonzo		smsc_warn_printf(sc, "timed-out waiting for reset to complete\n");
1315239275Sgonzo		goto init_failed;
1316239275Sgonzo	}
1317239275Sgonzo
1318239275Sgonzo	/* Reset the PHY */
1319239275Sgonzo	smsc_write_reg(sc, SMSC_PM_CTRL, SMSC_PM_CTRL_PHY_RST);
1320239275Sgonzo
1321239275Sgonzo	if ((err = smsc_wait_for_bits(sc, SMSC_PM_CTRL, SMSC_PM_CTRL_PHY_RST) != 0)) {
1322239275Sgonzo		smsc_warn_printf(sc, "timed-out waiting for phy reset to complete\n");
1323239275Sgonzo		goto init_failed;
1324239275Sgonzo	}
1325239275Sgonzo
1326239275Sgonzo	/* Set the mac address */
1327239275Sgonzo	if ((err = smsc_setmacaddress(sc, sc->sc_ue.ue_eaddr)) != 0) {
1328239275Sgonzo		smsc_warn_printf(sc, "failed to set the MAC address\n");
1329239275Sgonzo		goto init_failed;
1330239275Sgonzo	}
1331239275Sgonzo
1332239275Sgonzo	/* Don't know what the HW_CFG_BIR bit is, but following the reset sequence
1333239275Sgonzo	 * as used in the Linux driver.
1334239275Sgonzo	 */
1335239275Sgonzo	if ((err = smsc_read_reg(sc, SMSC_HW_CFG, &reg_val)) != 0) {
1336239275Sgonzo		smsc_warn_printf(sc, "failed to read HW_CFG: %d\n", err);
1337239275Sgonzo		goto init_failed;
1338239275Sgonzo	}
1339239275Sgonzo	reg_val |= SMSC_HW_CFG_BIR;
1340239275Sgonzo	smsc_write_reg(sc, SMSC_HW_CFG, reg_val);
1341239275Sgonzo
1342239275Sgonzo	/* There is a so called 'turbo mode' that the linux driver supports, it
1343239275Sgonzo	 * seems to allow you to jam multiple frames per Rx transaction.  By default
1344239275Sgonzo	 * this driver supports that and therefore allows multiple frames per URB.
1345239275Sgonzo	 *
1346239275Sgonzo	 * The xfer buffer size needs to reflect this as well, therefore based on
1347239275Sgonzo	 * the calculations in the Linux driver the RX bufsize is set to 18944,
1348239275Sgonzo	 *     bufsz = (16 * 1024 + 5 * 512)
1349239275Sgonzo	 *
1350239275Sgonzo	 * Burst capability is the number of URBs that can be in a burst of data/
1351239275Sgonzo	 * ethernet frames.
1352239275Sgonzo	 */
1353239275Sgonzo	if (usbd_get_speed(sc->sc_ue.ue_udev) == USB_SPEED_HIGH)
1354239275Sgonzo		burst_cap = 37;
1355239275Sgonzo	else
1356239275Sgonzo		burst_cap = 128;
1357239275Sgonzo
1358239275Sgonzo	smsc_write_reg(sc, SMSC_BURST_CAP, burst_cap);
1359239275Sgonzo
1360239275Sgonzo	/* Set the default bulk in delay (magic value from Linux driver) */
1361239275Sgonzo	smsc_write_reg(sc, SMSC_BULK_IN_DLY, 0x00002000);
1362239275Sgonzo
1363239275Sgonzo
1364239275Sgonzo
1365239275Sgonzo	/*
1366239275Sgonzo	 * Initialise the RX interface
1367239275Sgonzo	 */
1368239275Sgonzo	if ((err = smsc_read_reg(sc, SMSC_HW_CFG, &reg_val)) < 0) {
1369239275Sgonzo		smsc_warn_printf(sc, "failed to read HW_CFG: (err = %d)\n", err);
1370239275Sgonzo		goto init_failed;
1371239275Sgonzo	}
1372239275Sgonzo
1373239275Sgonzo	/* Adjust the packet offset in the buffer (designed to try and align IP
1374239275Sgonzo	 * header on 4 byte boundary)
1375239275Sgonzo	 */
1376239275Sgonzo	reg_val &= ~SMSC_HW_CFG_RXDOFF;
1377239275Sgonzo	reg_val |= (ETHER_ALIGN << 9) & SMSC_HW_CFG_RXDOFF;
1378239275Sgonzo
1379239275Sgonzo	/* The following setings are used for 'turbo mode', a.k.a multiple frames
1380239275Sgonzo	 * per Rx transaction (again info taken form Linux driver).
1381239275Sgonzo	 */
1382239275Sgonzo	reg_val |= (SMSC_HW_CFG_MEF | SMSC_HW_CFG_BCE);
1383239275Sgonzo
1384239275Sgonzo	smsc_write_reg(sc, SMSC_HW_CFG, reg_val);
1385239275Sgonzo
1386239275Sgonzo	/* Clear the status register ? */
1387239275Sgonzo	smsc_write_reg(sc, SMSC_INTR_STATUS, 0xffffffff);
1388239275Sgonzo
1389239275Sgonzo	/* Read and display the revision register */
1390239275Sgonzo	if ((err = smsc_read_reg(sc, SMSC_ID_REV, &sc->sc_rev_id)) < 0) {
1391239275Sgonzo		smsc_warn_printf(sc, "failed to read ID_REV (err = %d)\n", err);
1392239275Sgonzo		goto init_failed;
1393239275Sgonzo	}
1394239275Sgonzo
1395239275Sgonzo	device_printf(sc->sc_ue.ue_dev, "chip 0x%04lx, rev. %04lx\n",
1396239275Sgonzo	    (sc->sc_rev_id & SMSC_ID_REV_CHIP_ID_MASK) >> 16,
1397239275Sgonzo	    (sc->sc_rev_id & SMSC_ID_REV_CHIP_REV_MASK));
1398239275Sgonzo
1399239275Sgonzo	/* GPIO/LED setup */
1400239275Sgonzo	reg_val = SMSC_LED_GPIO_CFG_SPD_LED | SMSC_LED_GPIO_CFG_LNK_LED |
1401239275Sgonzo	          SMSC_LED_GPIO_CFG_FDX_LED;
1402239275Sgonzo	smsc_write_reg(sc, SMSC_LED_GPIO_CFG, reg_val);
1403239275Sgonzo
1404239275Sgonzo	/*
1405239275Sgonzo	 * Initialise the TX interface
1406239275Sgonzo	 */
1407239275Sgonzo	smsc_write_reg(sc, SMSC_FLOW, 0);
1408239275Sgonzo
1409239275Sgonzo	smsc_write_reg(sc, SMSC_AFC_CFG, AFC_CFG_DEFAULT);
1410239275Sgonzo
1411239275Sgonzo	/* Read the current MAC configuration */
1412239275Sgonzo	if ((err = smsc_read_reg(sc, SMSC_MAC_CSR, &sc->sc_mac_csr)) < 0) {
1413239275Sgonzo		smsc_warn_printf(sc, "failed to read MAC_CSR (err=%d)\n", err);
1414239275Sgonzo		goto init_failed;
1415239275Sgonzo	}
1416239275Sgonzo
1417239275Sgonzo	/* Vlan */
1418239275Sgonzo	smsc_write_reg(sc, SMSC_VLAN1, (uint32_t)ETHERTYPE_VLAN);
1419239275Sgonzo
1420239275Sgonzo	/*
1421239275Sgonzo	 * Initialise the PHY
1422239275Sgonzo	 */
1423239275Sgonzo	if ((err = smsc_phy_init(sc)) != 0)
1424239275Sgonzo		goto init_failed;
1425239275Sgonzo
1426239275Sgonzo
1427239275Sgonzo	/*
1428239275Sgonzo	 * Start TX
1429239275Sgonzo	 */
1430239275Sgonzo	sc->sc_mac_csr |= SMSC_MAC_CSR_TXEN;
1431239275Sgonzo	smsc_write_reg(sc, SMSC_MAC_CSR, sc->sc_mac_csr);
1432239275Sgonzo	smsc_write_reg(sc, SMSC_TX_CFG, SMSC_TX_CFG_ON);
1433239275Sgonzo
1434239275Sgonzo	/*
1435239275Sgonzo	 * Start RX
1436239275Sgonzo	 */
1437239275Sgonzo	sc->sc_mac_csr |= SMSC_MAC_CSR_RXEN;
1438239275Sgonzo	smsc_write_reg(sc, SMSC_MAC_CSR, sc->sc_mac_csr);
1439239275Sgonzo
1440239275Sgonzo	if (!locked)
1441239275Sgonzo		SMSC_UNLOCK(sc);
1442239275Sgonzo
1443239275Sgonzo	return (0);
1444239275Sgonzo
1445239275Sgonzoinit_failed:
1446239275Sgonzo	if (!locked)
1447239275Sgonzo		SMSC_UNLOCK(sc);
1448239275Sgonzo
1449239275Sgonzo	smsc_err_printf(sc, "smsc_chip_init failed (err=%d)\n", err);
1450239275Sgonzo	return (err);
1451239275Sgonzo}
1452239275Sgonzo
1453239275Sgonzo
1454239275Sgonzo/**
1455239275Sgonzo *	smsc_ioctl - ioctl function for the device
1456239275Sgonzo *	@ifp: interface pointer
1457239275Sgonzo *	@cmd: the ioctl command
1458239275Sgonzo *	@data: data passed in the ioctl call, typically a pointer to struct ifreq.
1459239275Sgonzo *
1460239275Sgonzo *	The ioctl routine is overridden to detect change requests for the H/W
1461239275Sgonzo *	checksum capabilities.
1462239275Sgonzo *
1463239275Sgonzo *	RETURNS:
1464239275Sgonzo *	0 on success and an error code on failure.
1465239275Sgonzo */
1466239275Sgonzostatic int
1467239275Sgonzosmsc_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1468239275Sgonzo{
1469239275Sgonzo	struct usb_ether *ue = ifp->if_softc;
1470239275Sgonzo	struct smsc_softc *sc;
1471239275Sgonzo	struct ifreq *ifr;
1472239275Sgonzo	int rc;
1473239275Sgonzo	int mask;
1474239275Sgonzo	int reinit;
1475239275Sgonzo
1476239275Sgonzo	if (cmd == SIOCSIFCAP) {
1477239275Sgonzo
1478239275Sgonzo		sc = uether_getsc(ue);
1479239275Sgonzo		ifr = (struct ifreq *)data;
1480239275Sgonzo
1481239275Sgonzo		SMSC_LOCK(sc);
1482239275Sgonzo
1483239275Sgonzo		rc = 0;
1484239275Sgonzo		reinit = 0;
1485239275Sgonzo
1486239275Sgonzo		mask = ifr->ifr_reqcap ^ ifp->if_capenable;
1487239275Sgonzo
1488239275Sgonzo		/* Modify the RX CSUM enable bits */
1489239275Sgonzo		if ((mask & IFCAP_RXCSUM) != 0 &&
1490239275Sgonzo		    (ifp->if_capabilities & IFCAP_RXCSUM) != 0) {
1491239275Sgonzo			ifp->if_capenable ^= IFCAP_RXCSUM;
1492239275Sgonzo
1493239275Sgonzo			if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
1494239275Sgonzo				ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1495239275Sgonzo				reinit = 1;
1496239275Sgonzo			}
1497239275Sgonzo		}
1498239275Sgonzo
1499239275Sgonzo		SMSC_UNLOCK(sc);
1500239275Sgonzo		if (reinit)
1501239275Sgonzo#if __FreeBSD_version > 1000000
1502239275Sgonzo			uether_init(ue);
1503239275Sgonzo#else
1504239275Sgonzo			ifp->if_init(ue);
1505239275Sgonzo#endif
1506239275Sgonzo
1507239275Sgonzo	} else {
1508239275Sgonzo		rc = uether_ioctl(ifp, cmd, data);
1509239275Sgonzo	}
1510239275Sgonzo
1511239275Sgonzo	return (rc);
1512239275Sgonzo}
1513239275Sgonzo
1514239275Sgonzo
1515239275Sgonzo/**
1516239275Sgonzo *	smsc_attach_post - Called after the driver attached to the USB interface
1517239275Sgonzo *	@ue: the USB ethernet device
1518239275Sgonzo *
1519239275Sgonzo *	This is where the chip is intialised for the first time.  This is different
1520239275Sgonzo *	from the smsc_init() function in that that one is designed to setup the
1521239275Sgonzo *	H/W to match the UE settings and can be called after a reset.
1522239275Sgonzo *
1523239275Sgonzo *
1524239275Sgonzo */
1525239275Sgonzostatic void
1526239275Sgonzosmsc_attach_post(struct usb_ether *ue)
1527239275Sgonzo{
1528239275Sgonzo	struct smsc_softc *sc = uether_getsc(ue);
1529239275Sgonzo	uint32_t mac_h, mac_l;
1530239275Sgonzo	int err;
1531239275Sgonzo
1532239275Sgonzo	smsc_dbg_printf(sc, "smsc_attach_post\n");
1533239275Sgonzo
1534239275Sgonzo	/* Setup some of the basics */
1535239275Sgonzo	sc->sc_phyno = 1;
1536239275Sgonzo
1537239275Sgonzo
1538239275Sgonzo	/* Attempt to get the mac address, if an EEPROM is not attached this
1539239275Sgonzo	 * will just return FF:FF:FF:FF:FF:FF, so in such cases we invent a MAC
1540239275Sgonzo	 * address based on urandom.
1541239275Sgonzo	 */
1542239275Sgonzo	memset(sc->sc_ue.ue_eaddr, 0xff, ETHER_ADDR_LEN);
1543239275Sgonzo
1544239275Sgonzo	/* Check if there is already a MAC address in the register */
1545239275Sgonzo	if ((smsc_read_reg(sc, SMSC_MAC_ADDRL, &mac_l) == 0) &&
1546239275Sgonzo	    (smsc_read_reg(sc, SMSC_MAC_ADDRH, &mac_h) == 0)) {
1547239275Sgonzo		sc->sc_ue.ue_eaddr[5] = (uint8_t)((mac_h >> 8) & 0xff);
1548239275Sgonzo		sc->sc_ue.ue_eaddr[4] = (uint8_t)((mac_h) & 0xff);
1549239275Sgonzo		sc->sc_ue.ue_eaddr[3] = (uint8_t)((mac_l >> 24) & 0xff);
1550239275Sgonzo		sc->sc_ue.ue_eaddr[2] = (uint8_t)((mac_l >> 16) & 0xff);
1551239275Sgonzo		sc->sc_ue.ue_eaddr[1] = (uint8_t)((mac_l >> 8) & 0xff);
1552239275Sgonzo		sc->sc_ue.ue_eaddr[0] = (uint8_t)((mac_l) & 0xff);
1553239275Sgonzo	}
1554239275Sgonzo
1555239275Sgonzo	/* MAC address is not set so try to read from EEPROM, if that fails generate
1556239275Sgonzo	 * a random MAC address.
1557239275Sgonzo	 */
1558243421Sgonzo	if (!ETHER_IS_VALID(sc->sc_ue.ue_eaddr)) {
1559273546Sloos
1560273546Sloos		err = smsc_eeprom_read(sc, 0x01, sc->sc_ue.ue_eaddr, ETHER_ADDR_LEN);
1561273546Sloos		if ((err != 0) || (!ETHER_IS_VALID(sc->sc_ue.ue_eaddr))) {
1562273546Sloos
1563273546Sloos			read_random(sc->sc_ue.ue_eaddr, ETHER_ADDR_LEN);
1564273546Sloos			sc->sc_ue.ue_eaddr[0] &= ~0x01;     /* unicast */
1565273546Sloos			sc->sc_ue.ue_eaddr[0] |=  0x02;     /* locally administered */
1566273546Sloos		}
1567273546Sloos	}
1568273546Sloos
1569273546Sloos	/* Initialise the chip for the first time */
1570273546Sloos	smsc_chip_init(sc);
1571273546Sloos}
1572273546Sloos
1573273546Sloos
1574273546Sloos/**
1575273546Sloos *	smsc_attach_post_sub - Called after the driver attached to the USB interface
1576273546Sloos *	@ue: the USB ethernet device
1577243421Sgonzo *
1578273546Sloos *	Most of this is boilerplate code and copied from the base USB ethernet
1579273546Sloos *	driver.  It has been overriden so that we can indicate to the system that
1580273546Sloos *	the chip supports H/W checksumming.
1581243421Sgonzo *
1582243421Sgonzo *	RETURNS:
1583243421Sgonzo *	Returns 0 on success or a negative error code.
1584243421Sgonzo */
1585273546Sloos#if __FreeBSD_version > 1000000
1586243421Sgonzostatic int
1587239275Sgonzosmsc_attach_post_sub(struct usb_ether *ue)
1588243421Sgonzo{
1589273546Sloos	struct smsc_softc *sc;
1590273546Sloos	struct ifnet *ifp;
1591243421Sgonzo	int error;
1592273546Sloos
1593273546Sloos	sc = uether_getsc(ue);
1594273546Sloos	ifp = ue->ue_ifp;
1595273546Sloos	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
1596243421Sgonzo	ifp->if_start = uether_start;
1597273546Sloos	ifp->if_ioctl = smsc_ioctl;
1598273546Sloos	ifp->if_init = uether_init;
1599273546Sloos	IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen);
1600243421Sgonzo	ifp->if_snd.ifq_drv_maxlen = ifqmaxlen;
1601243421Sgonzo	IFQ_SET_READY(&ifp->if_snd);
1602273546Sloos
1603273546Sloos	/* The chip supports TCP/UDP checksum offloading on TX and RX paths, however
1604273546Sloos	 * currently only RX checksum is supported in the driver (see top of file).
1605243421Sgonzo	 */
1606273546Sloos	ifp->if_capabilities |= IFCAP_RXCSUM;
1607273546Sloos	ifp->if_hwassist = 0;
1608273546Sloos
1609243421Sgonzo	/* TX checksuming is disabled (for now?)
1610243421Sgonzo	ifp->if_capabilities |= IFCAP_TXCSUM;
1611243421Sgonzo	ifp->if_capenable |= IFCAP_TXCSUM;
1612243421Sgonzo	ifp->if_hwassist = CSUM_TCP | CSUM_UDP;
1613243421Sgonzo	*/
1614243421Sgonzo
1615243421Sgonzo	ifp->if_capenable = ifp->if_capabilities;
1616239275Sgonzo
1617239275Sgonzo	mtx_lock(&Giant);
1618239275Sgonzo	error = mii_attach(ue->ue_dev, &ue->ue_miibus, ifp,
1619239275Sgonzo	    uether_ifmedia_upd, ue->ue_methods->ue_mii_sts,
1620239275Sgonzo	    BMSR_DEFCAPMASK, sc->sc_phyno, MII_OFFSET_ANY, 0);
1621239275Sgonzo	mtx_unlock(&Giant);
1622239275Sgonzo
1623239275Sgonzo	return (error);
1624239275Sgonzo}
1625239275Sgonzo#endif /* __FreeBSD_version > 1000000 */
1626239275Sgonzo
1627239275Sgonzo
1628239275Sgonzo/**
1629239275Sgonzo *	smsc_probe - Probe the interface.
1630239275Sgonzo *	@dev: smsc device handle
1631239275Sgonzo *
1632239275Sgonzo *	Checks if the device is a match for this driver.
1633239275Sgonzo *
1634239275Sgonzo *	RETURNS:
1635239275Sgonzo *	Returns 0 on success or an error code on failure.
1636239275Sgonzo */
1637239275Sgonzostatic int
1638239275Sgonzosmsc_probe(device_t dev)
1639239275Sgonzo{
1640239275Sgonzo	struct usb_attach_arg *uaa = device_get_ivars(dev);
1641239275Sgonzo
1642239275Sgonzo	if (uaa->usb_mode != USB_MODE_HOST)
1643239275Sgonzo		return (ENXIO);
1644239275Sgonzo	if (uaa->info.bConfigIndex != SMSC_CONFIG_INDEX)
1645239275Sgonzo		return (ENXIO);
1646239275Sgonzo	if (uaa->info.bIfaceIndex != SMSC_IFACE_IDX)
1647239275Sgonzo		return (ENXIO);
1648239275Sgonzo
1649239275Sgonzo	return (usbd_lookup_id_by_uaa(smsc_devs, sizeof(smsc_devs), uaa));
1650239275Sgonzo}
1651239275Sgonzo
1652239275Sgonzo
1653239275Sgonzo/**
1654239275Sgonzo *	smsc_attach - Attach the interface.
1655239275Sgonzo *	@dev: smsc device handle
1656239275Sgonzo *
1657239275Sgonzo *	Allocate softc structures, do ifmedia setup and ethernet/BPF attach.
1658239275Sgonzo *
1659239275Sgonzo *	RETURNS:
1660239275Sgonzo *	Returns 0 on success or a negative error code.
1661239275Sgonzo */
1662243421Sgonzostatic int
1663243421Sgonzosmsc_attach(device_t dev)
1664243421Sgonzo{
1665243421Sgonzo	struct usb_attach_arg *uaa = device_get_ivars(dev);
1666239275Sgonzo	struct smsc_softc *sc = device_get_softc(dev);
1667239275Sgonzo	struct usb_ether *ue = &sc->sc_ue;
1668239275Sgonzo	uint8_t iface_index;
1669239275Sgonzo	int err;
1670239275Sgonzo
1671239275Sgonzo	sc->sc_flags = USB_GET_DRIVER_INFO(uaa);
1672239275Sgonzo
1673239275Sgonzo	device_set_usb_desc(dev);
1674239275Sgonzo
1675239275Sgonzo	mtx_init(&sc->sc_mtx, device_get_nameunit(dev), NULL, MTX_DEF);
1676239275Sgonzo
1677239275Sgonzo	/* Setup the endpoints for the SMSC LAN95xx device(s) */
1678239275Sgonzo	iface_index = SMSC_IFACE_IDX;
1679239275Sgonzo	err = usbd_transfer_setup(uaa->device, &iface_index, sc->sc_xfer,
1680239275Sgonzo	                          smsc_config, SMSC_N_TRANSFER, sc, &sc->sc_mtx);
1681239275Sgonzo	if (err) {
1682239275Sgonzo		device_printf(dev, "error: allocating USB transfers failed\n");
1683239275Sgonzo		goto detach;
1684239275Sgonzo	}
1685239275Sgonzo
1686239275Sgonzo	ue->ue_sc = sc;
1687239275Sgonzo	ue->ue_dev = dev;
1688239275Sgonzo	ue->ue_udev = uaa->device;
1689239275Sgonzo	ue->ue_mtx = &sc->sc_mtx;
1690239275Sgonzo	ue->ue_methods = &smsc_ue_methods;
1691239275Sgonzo
1692239275Sgonzo	err = uether_ifattach(ue);
1693239275Sgonzo	if (err) {
1694239275Sgonzo		device_printf(dev, "error: could not attach interface\n");
1695239275Sgonzo		goto detach;
1696239275Sgonzo	}
1697239275Sgonzo	return (0);			/* success */
1698239275Sgonzo
1699239275Sgonzodetach:
1700239275Sgonzo	smsc_detach(dev);
1701239275Sgonzo	return (ENXIO);		/* failure */
1702239275Sgonzo}
1703239275Sgonzo
1704239275Sgonzo/**
1705239275Sgonzo *	smsc_detach - Detach the interface.
1706239275Sgonzo *	@dev: smsc device handle
1707239275Sgonzo *
1708239275Sgonzo *	RETURNS:
1709239275Sgonzo *	Returns 0.
1710239275Sgonzo */
1711239275Sgonzostatic int
1712239275Sgonzosmsc_detach(device_t dev)
1713239275Sgonzo{
1714239275Sgonzo	struct smsc_softc *sc = device_get_softc(dev);
1715239275Sgonzo	struct usb_ether *ue = &sc->sc_ue;
1716239275Sgonzo
1717239275Sgonzo	usbd_transfer_unsetup(sc->sc_xfer, SMSC_N_TRANSFER);
1718239275Sgonzo	uether_ifdetach(ue);
1719239275Sgonzo	mtx_destroy(&sc->sc_mtx);
1720239275Sgonzo
1721239275Sgonzo	return (0);
1722239275Sgonzo}
1723239275Sgonzo
1724239275Sgonzostatic device_method_t smsc_methods[] = {
1725239275Sgonzo	/* Device interface */
1726239275Sgonzo	DEVMETHOD(device_probe, smsc_probe),
1727239275Sgonzo	DEVMETHOD(device_attach, smsc_attach),
1728239275Sgonzo	DEVMETHOD(device_detach, smsc_detach),
1729239275Sgonzo
1730239275Sgonzo	/* bus interface */
1731239275Sgonzo	DEVMETHOD(bus_print_child, bus_generic_print_child),
1732239275Sgonzo	DEVMETHOD(bus_driver_added, bus_generic_driver_added),
1733239275Sgonzo
1734239275Sgonzo	/* MII interface */
1735239275Sgonzo	DEVMETHOD(miibus_readreg, smsc_miibus_readreg),
1736239275Sgonzo	DEVMETHOD(miibus_writereg, smsc_miibus_writereg),
1737239275Sgonzo	DEVMETHOD(miibus_statchg, smsc_miibus_statchg),
1738239275Sgonzo
1739239275Sgonzo	{0, 0}
1740239275Sgonzo};
1741239275Sgonzo
1742239275Sgonzostatic driver_t smsc_driver = {
1743239275Sgonzo	.name = "smsc",
1744239275Sgonzo	.methods = smsc_methods,
1745239275Sgonzo	.size = sizeof(struct smsc_softc),
1746239275Sgonzo};
1747239275Sgonzo
1748239275Sgonzostatic devclass_t smsc_devclass;
1749239275Sgonzo
1750239275SgonzoDRIVER_MODULE(smsc, uhub, smsc_driver, smsc_devclass, NULL, 0);
1751239275SgonzoDRIVER_MODULE(miibus, smsc, miibus_driver, miibus_devclass, 0, 0);
1752239275SgonzoMODULE_DEPEND(smsc, uether, 1, 1, 1);
1753239275SgonzoMODULE_DEPEND(smsc, usb, 1, 1, 1);
1754239275SgonzoMODULE_DEPEND(smsc, ether, 1, 1, 1);
1755239275SgonzoMODULE_DEPEND(smsc, miibus, 1, 1, 1);
1756239275SgonzoMODULE_VERSION(smsc, 1);
1757239275Sgonzo