xref: /freebsd/sys/dev/usb/net/if_cue.c (revision ca48e43ba9ee73a07cdbad8365117793b01273bb)
1 /*-
2  * SPDX-License-Identifier: BSD-4-Clause
3  *
4  * Copyright (c) 1997, 1998, 1999, 2000
5  *	Bill Paul <wpaul@ee.columbia.edu>.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *	This product includes software developed by Bill Paul.
18  * 4. Neither the name of the author nor the names of any co-contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
26  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
32  * THE POSSIBILITY OF SUCH DAMAGE.
33  */
34 
35 /*
36  * CATC USB-EL1210A USB to ethernet driver. Used in the CATC Netmate
37  * adapters and others.
38  *
39  * Written by Bill Paul <wpaul@ee.columbia.edu>
40  * Electrical Engineering Department
41  * Columbia University, New York City
42  */
43 
44 /*
45  * The CATC USB-EL1210A provides USB ethernet support at 10Mbps. The
46  * RX filter uses a 512-bit multicast hash table, single perfect entry
47  * for the station address, and promiscuous mode. Unlike the ADMtek
48  * and KLSI chips, the CATC ASIC supports read and write combining
49  * mode where multiple packets can be transferred using a single bulk
50  * transaction, which helps performance a great deal.
51  */
52 
53 #include <sys/stdint.h>
54 #include <sys/stddef.h>
55 #include <sys/param.h>
56 #include <sys/queue.h>
57 #include <sys/types.h>
58 #include <sys/systm.h>
59 #include <sys/socket.h>
60 #include <sys/kernel.h>
61 #include <sys/bus.h>
62 #include <sys/module.h>
63 #include <sys/lock.h>
64 #include <sys/mutex.h>
65 #include <sys/condvar.h>
66 #include <sys/sysctl.h>
67 #include <sys/sx.h>
68 #include <sys/unistd.h>
69 #include <sys/callout.h>
70 #include <sys/malloc.h>
71 #include <sys/priv.h>
72 
73 #include <net/if.h>
74 #include <net/if_var.h>
75 
76 #include <dev/usb/usb.h>
77 #include <dev/usb/usbdi.h>
78 #include <dev/usb/usbdi_util.h>
79 #include "usbdevs.h"
80 
81 #define	USB_DEBUG_VAR cue_debug
82 #include <dev/usb/usb_debug.h>
83 #include <dev/usb/usb_process.h>
84 
85 #include <dev/usb/net/usb_ethernet.h>
86 #include <dev/usb/net/if_cuereg.h>
87 
88 /*
89  * Various supported device vendors/products.
90  */
91 
92 /* Belkin F5U111 adapter covered by NETMATE entry */
93 
94 static const STRUCT_USB_HOST_ID cue_devs[] = {
95 #define	CUE_DEV(v,p) { USB_VP(USB_VENDOR_##v, USB_PRODUCT_##v##_##p) }
96 	CUE_DEV(CATC, NETMATE),
97 	CUE_DEV(CATC, NETMATE2),
98 	CUE_DEV(SMARTBRIDGES, SMARTLINK),
99 #undef CUE_DEV
100 };
101 
102 /* prototypes */
103 
104 static device_probe_t cue_probe;
105 static device_attach_t cue_attach;
106 static device_detach_t cue_detach;
107 
108 static usb_callback_t cue_bulk_read_callback;
109 static usb_callback_t cue_bulk_write_callback;
110 
111 static uether_fn_t cue_attach_post;
112 static uether_fn_t cue_init;
113 static uether_fn_t cue_stop;
114 static uether_fn_t cue_start;
115 static uether_fn_t cue_tick;
116 static uether_fn_t cue_setmulti;
117 static uether_fn_t cue_setpromisc;
118 
119 static uint8_t	cue_csr_read_1(struct cue_softc *, uint16_t);
120 static uint16_t	cue_csr_read_2(struct cue_softc *, uint8_t);
121 static int	cue_csr_write_1(struct cue_softc *, uint16_t, uint16_t);
122 static int	cue_mem(struct cue_softc *, uint8_t, uint16_t, void *, int);
123 static int	cue_getmac(struct cue_softc *, void *);
124 static uint32_t	cue_mchash(const uint8_t *);
125 static void	cue_reset(struct cue_softc *);
126 
127 #ifdef USB_DEBUG
128 static int cue_debug = 0;
129 
130 static SYSCTL_NODE(_hw_usb, OID_AUTO, cue, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
131     "USB cue");
132 SYSCTL_INT(_hw_usb_cue, OID_AUTO, debug, CTLFLAG_RWTUN, &cue_debug, 0,
133     "Debug level");
134 #endif
135 
136 static const struct usb_config cue_config[CUE_N_TRANSFER] = {
137 	[CUE_BULK_DT_WR] = {
138 		.type = UE_BULK,
139 		.endpoint = UE_ADDR_ANY,
140 		.direction = UE_DIR_OUT,
141 		.bufsize = (MCLBYTES + 2),
142 		.flags = {.pipe_bof = 1,},
143 		.callback = cue_bulk_write_callback,
144 		.timeout = 10000,	/* 10 seconds */
145 	},
146 
147 	[CUE_BULK_DT_RD] = {
148 		.type = UE_BULK,
149 		.endpoint = UE_ADDR_ANY,
150 		.direction = UE_DIR_IN,
151 		.bufsize = (MCLBYTES + 2),
152 		.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
153 		.callback = cue_bulk_read_callback,
154 	},
155 };
156 
157 static device_method_t cue_methods[] = {
158 	/* Device interface */
159 	DEVMETHOD(device_probe, cue_probe),
160 	DEVMETHOD(device_attach, cue_attach),
161 	DEVMETHOD(device_detach, cue_detach),
162 
163 	DEVMETHOD_END
164 };
165 
166 static driver_t cue_driver = {
167 	.name = "cue",
168 	.methods = cue_methods,
169 	.size = sizeof(struct cue_softc),
170 };
171 
172 DRIVER_MODULE(cue, uhub, cue_driver, NULL, NULL);
173 MODULE_DEPEND(cue, uether, 1, 1, 1);
174 MODULE_DEPEND(cue, usb, 1, 1, 1);
175 MODULE_DEPEND(cue, ether, 1, 1, 1);
176 MODULE_VERSION(cue, 1);
177 USB_PNP_HOST_INFO(cue_devs);
178 
179 static const struct usb_ether_methods cue_ue_methods = {
180 	.ue_attach_post = cue_attach_post,
181 	.ue_start = cue_start,
182 	.ue_init = cue_init,
183 	.ue_stop = cue_stop,
184 	.ue_tick = cue_tick,
185 	.ue_setmulti = cue_setmulti,
186 	.ue_setpromisc = cue_setpromisc,
187 };
188 
189 #define	CUE_SETBIT(sc, reg, x)				\
190 	cue_csr_write_1(sc, reg, cue_csr_read_1(sc, reg) | (x))
191 
192 #define	CUE_CLRBIT(sc, reg, x)				\
193 	cue_csr_write_1(sc, reg, cue_csr_read_1(sc, reg) & ~(x))
194 
195 static uint8_t
cue_csr_read_1(struct cue_softc * sc,uint16_t reg)196 cue_csr_read_1(struct cue_softc *sc, uint16_t reg)
197 {
198 	struct usb_device_request req;
199 	uint8_t val;
200 
201 	req.bmRequestType = UT_READ_VENDOR_DEVICE;
202 	req.bRequest = CUE_CMD_READREG;
203 	USETW(req.wValue, 0);
204 	USETW(req.wIndex, reg);
205 	USETW(req.wLength, 1);
206 
207 	if (uether_do_request(&sc->sc_ue, &req, &val, 1000)) {
208 		/* ignore any errors */
209 	}
210 	return (val);
211 }
212 
213 static uint16_t
cue_csr_read_2(struct cue_softc * sc,uint8_t reg)214 cue_csr_read_2(struct cue_softc *sc, uint8_t reg)
215 {
216 	struct usb_device_request req;
217 	uint16_t val;
218 
219 	req.bmRequestType = UT_READ_VENDOR_DEVICE;
220 	req.bRequest = CUE_CMD_READREG;
221 	USETW(req.wValue, 0);
222 	USETW(req.wIndex, reg);
223 	USETW(req.wLength, 2);
224 
225 	(void)uether_do_request(&sc->sc_ue, &req, &val, 1000);
226 	return (le16toh(val));
227 }
228 
229 static int
cue_csr_write_1(struct cue_softc * sc,uint16_t reg,uint16_t val)230 cue_csr_write_1(struct cue_softc *sc, uint16_t reg, uint16_t val)
231 {
232 	struct usb_device_request req;
233 
234 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
235 	req.bRequest = CUE_CMD_WRITEREG;
236 	USETW(req.wValue, val);
237 	USETW(req.wIndex, reg);
238 	USETW(req.wLength, 0);
239 
240 	return (uether_do_request(&sc->sc_ue, &req, NULL, 1000));
241 }
242 
243 static int
cue_mem(struct cue_softc * sc,uint8_t cmd,uint16_t addr,void * buf,int len)244 cue_mem(struct cue_softc *sc, uint8_t cmd, uint16_t addr, void *buf, int len)
245 {
246 	struct usb_device_request req;
247 
248 	if (cmd == CUE_CMD_READSRAM)
249 		req.bmRequestType = UT_READ_VENDOR_DEVICE;
250 	else
251 		req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
252 	req.bRequest = cmd;
253 	USETW(req.wValue, 0);
254 	USETW(req.wIndex, addr);
255 	USETW(req.wLength, len);
256 
257 	return (uether_do_request(&sc->sc_ue, &req, buf, 1000));
258 }
259 
260 static int
cue_getmac(struct cue_softc * sc,void * buf)261 cue_getmac(struct cue_softc *sc, void *buf)
262 {
263 	struct usb_device_request req;
264 
265 	req.bmRequestType = UT_READ_VENDOR_DEVICE;
266 	req.bRequest = CUE_CMD_GET_MACADDR;
267 	USETW(req.wValue, 0);
268 	USETW(req.wIndex, 0);
269 	USETW(req.wLength, ETHER_ADDR_LEN);
270 
271 	return (uether_do_request(&sc->sc_ue, &req, buf, 1000));
272 }
273 
274 #define	CUE_BITS 9
275 
276 static uint32_t
cue_mchash(const uint8_t * addr)277 cue_mchash(const uint8_t *addr)
278 {
279 	uint32_t crc;
280 
281 	/* Compute CRC for the address value. */
282 	crc = ether_crc32_le(addr, ETHER_ADDR_LEN);
283 
284 	return (crc & ((1 << CUE_BITS) - 1));
285 }
286 
287 static void
cue_setpromisc(struct usb_ether * ue)288 cue_setpromisc(struct usb_ether *ue)
289 {
290 	struct cue_softc *sc = uether_getsc(ue);
291 	if_t ifp = uether_getifp(ue);
292 
293 	CUE_LOCK_ASSERT(sc, MA_OWNED);
294 
295 	/* if we want promiscuous mode, set the allframes bit */
296 	if (if_getflags(ifp) & IFF_PROMISC)
297 		CUE_SETBIT(sc, CUE_ETHCTL, CUE_ETHCTL_PROMISC);
298 	else
299 		CUE_CLRBIT(sc, CUE_ETHCTL, CUE_ETHCTL_PROMISC);
300 
301 	/* write multicast hash-bits */
302 	cue_setmulti(ue);
303 }
304 
305 static u_int
cue_hash_maddr(void * arg,struct sockaddr_dl * sdl,u_int cnt)306 cue_hash_maddr(void *arg, struct sockaddr_dl *sdl, u_int cnt)
307 {
308 	uint8_t *hashtbl = arg;
309 	uint32_t h;
310 
311 	h = cue_mchash(LLADDR(sdl));
312 	hashtbl[h >> 3] |= 1 << (h & 0x7);
313 
314 	return (1);
315 }
316 
317 static void
cue_setmulti(struct usb_ether * ue)318 cue_setmulti(struct usb_ether *ue)
319 {
320 	struct cue_softc *sc = uether_getsc(ue);
321 	if_t ifp = uether_getifp(ue);
322 	uint32_t h, i;
323 	uint8_t hashtbl[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
324 
325 	CUE_LOCK_ASSERT(sc, MA_OWNED);
326 
327 	if (if_getflags(ifp) & IFF_ALLMULTI || if_getflags(ifp) & IFF_PROMISC) {
328 		for (i = 0; i < 8; i++)
329 			hashtbl[i] = 0xff;
330 		cue_mem(sc, CUE_CMD_WRITESRAM, CUE_MCAST_TABLE_ADDR,
331 		    &hashtbl, 8);
332 		return;
333 	}
334 
335 	/* now program new ones */
336 	if_foreach_llmaddr(ifp, cue_hash_maddr, hashtbl);
337 
338 	/*
339 	 * Also include the broadcast address in the filter
340 	 * so we can receive broadcast frames.
341  	 */
342 	if (if_getflags(ifp) & IFF_BROADCAST) {
343 		h = cue_mchash(if_getbroadcastaddr(ifp));
344 		hashtbl[h >> 3] |= 1 << (h & 0x7);
345 	}
346 
347 	cue_mem(sc, CUE_CMD_WRITESRAM, CUE_MCAST_TABLE_ADDR, &hashtbl, 8);
348 }
349 
350 static void
cue_reset(struct cue_softc * sc)351 cue_reset(struct cue_softc *sc)
352 {
353 	struct usb_device_request req;
354 
355 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
356 	req.bRequest = CUE_CMD_RESET;
357 	USETW(req.wValue, 0);
358 	USETW(req.wIndex, 0);
359 	USETW(req.wLength, 0);
360 
361 	if (uether_do_request(&sc->sc_ue, &req, NULL, 1000)) {
362 		/* ignore any errors */
363 	}
364 
365 	/*
366 	 * wait a little while for the chip to get its brains in order:
367 	 */
368 	uether_pause(&sc->sc_ue, hz / 100);
369 }
370 
371 static void
cue_attach_post(struct usb_ether * ue)372 cue_attach_post(struct usb_ether *ue)
373 {
374 	struct cue_softc *sc = uether_getsc(ue);
375 
376 	cue_getmac(sc, ue->ue_eaddr);
377 }
378 
379 static int
cue_probe(device_t dev)380 cue_probe(device_t dev)
381 {
382 	struct usb_attach_arg *uaa = device_get_ivars(dev);
383 
384 	if (uaa->usb_mode != USB_MODE_HOST)
385 		return (ENXIO);
386 	if (uaa->info.bConfigIndex != CUE_CONFIG_IDX)
387 		return (ENXIO);
388 	if (uaa->info.bIfaceIndex != CUE_IFACE_IDX)
389 		return (ENXIO);
390 
391 	return (usbd_lookup_id_by_uaa(cue_devs, sizeof(cue_devs), uaa));
392 }
393 
394 /*
395  * Attach the interface. Allocate softc structures, do ifmedia
396  * setup and ethernet/BPF attach.
397  */
398 static int
cue_attach(device_t dev)399 cue_attach(device_t dev)
400 {
401 	struct usb_attach_arg *uaa = device_get_ivars(dev);
402 	struct cue_softc *sc = device_get_softc(dev);
403 	struct usb_ether *ue = &sc->sc_ue;
404 	uint8_t iface_index;
405 	int error;
406 
407 	device_set_usb_desc(dev);
408 	mtx_init(&sc->sc_mtx, device_get_nameunit(dev), NULL, MTX_DEF);
409 
410 	iface_index = CUE_IFACE_IDX;
411 	error = usbd_transfer_setup(uaa->device, &iface_index,
412 	    sc->sc_xfer, cue_config, CUE_N_TRANSFER, sc, &sc->sc_mtx);
413 	if (error) {
414 		device_printf(dev, "allocating USB transfers failed\n");
415 		goto detach;
416 	}
417 
418 	ue->ue_sc = sc;
419 	ue->ue_dev = dev;
420 	ue->ue_udev = uaa->device;
421 	ue->ue_mtx = &sc->sc_mtx;
422 	ue->ue_methods = &cue_ue_methods;
423 
424 	error = uether_ifattach(ue);
425 	if (error) {
426 		device_printf(dev, "could not attach interface\n");
427 		goto detach;
428 	}
429 	return (0);			/* success */
430 
431 detach:
432 	cue_detach(dev);
433 	return (ENXIO);			/* failure */
434 }
435 
436 static int
cue_detach(device_t dev)437 cue_detach(device_t dev)
438 {
439 	struct cue_softc *sc = device_get_softc(dev);
440 	struct usb_ether *ue = &sc->sc_ue;
441 
442 	usbd_transfer_unsetup(sc->sc_xfer, CUE_N_TRANSFER);
443 	uether_ifdetach(ue);
444 	mtx_destroy(&sc->sc_mtx);
445 
446 	return (0);
447 }
448 
449 static void
cue_bulk_read_callback(struct usb_xfer * xfer,usb_error_t error)450 cue_bulk_read_callback(struct usb_xfer *xfer, usb_error_t error)
451 {
452 	struct cue_softc *sc = usbd_xfer_softc(xfer);
453 	struct usb_ether *ue = &sc->sc_ue;
454 	if_t ifp = uether_getifp(ue);
455 	struct usb_page_cache *pc;
456 	uint8_t buf[2];
457 	int len;
458 	int actlen;
459 
460 	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
461 
462 	switch (USB_GET_STATE(xfer)) {
463 	case USB_ST_TRANSFERRED:
464 
465 		if (actlen <= (int)(2 + sizeof(struct ether_header))) {
466 			if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
467 			goto tr_setup;
468 		}
469 		pc = usbd_xfer_get_frame(xfer, 0);
470 		usbd_copy_out(pc, 0, buf, 2);
471 		actlen -= 2;
472 		len = buf[0] | (buf[1] << 8);
473 		len = min(actlen, len);
474 
475 		uether_rxbuf(ue, pc, 2, len);
476 		/* FALLTHROUGH */
477 	case USB_ST_SETUP:
478 tr_setup:
479 		usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
480 		usbd_transfer_submit(xfer);
481 		uether_rxflush(ue);
482 		return;
483 
484 	default:			/* Error */
485 		DPRINTF("bulk read error, %s\n",
486 		    usbd_errstr(error));
487 
488 		if (error != USB_ERR_CANCELLED) {
489 			/* try to clear stall first */
490 			usbd_xfer_set_stall(xfer);
491 			goto tr_setup;
492 		}
493 		return;
494 	}
495 }
496 
497 static void
cue_bulk_write_callback(struct usb_xfer * xfer,usb_error_t error)498 cue_bulk_write_callback(struct usb_xfer *xfer, usb_error_t error)
499 {
500 	struct cue_softc *sc = usbd_xfer_softc(xfer);
501 	if_t ifp = uether_getifp(&sc->sc_ue);
502 	struct usb_page_cache *pc;
503 	struct mbuf *m;
504 	uint8_t buf[2];
505 
506 	switch (USB_GET_STATE(xfer)) {
507 	case USB_ST_TRANSFERRED:
508 		DPRINTFN(11, "transfer complete\n");
509 		if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
510 
511 		/* FALLTHROUGH */
512 	case USB_ST_SETUP:
513 tr_setup:
514 		m = if_dequeue(ifp);
515 
516 		if (m == NULL)
517 			return;
518 		if (m->m_pkthdr.len > MCLBYTES)
519 			m->m_pkthdr.len = MCLBYTES;
520 		usbd_xfer_set_frame_len(xfer, 0, (m->m_pkthdr.len + 2));
521 
522 		/* the first two bytes are the frame length */
523 
524 		buf[0] = (uint8_t)(m->m_pkthdr.len);
525 		buf[1] = (uint8_t)(m->m_pkthdr.len >> 8);
526 
527 		pc = usbd_xfer_get_frame(xfer, 0);
528 		usbd_copy_in(pc, 0, buf, 2);
529 		usbd_m_copy_in(pc, 2, m, 0, m->m_pkthdr.len);
530 
531 		/*
532 		 * If there's a BPF listener, bounce a copy of this frame
533 		 * to him.
534 		 */
535 		BPF_MTAP(ifp, m);
536 
537 		m_freem(m);
538 
539 		usbd_transfer_submit(xfer);
540 
541 		return;
542 
543 	default:			/* Error */
544 		DPRINTFN(11, "transfer error, %s\n",
545 		    usbd_errstr(error));
546 
547 		if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
548 
549 		if (error != USB_ERR_CANCELLED) {
550 			/* try to clear stall first */
551 			usbd_xfer_set_stall(xfer);
552 			goto tr_setup;
553 		}
554 		return;
555 	}
556 }
557 
558 static void
cue_tick(struct usb_ether * ue)559 cue_tick(struct usb_ether *ue)
560 {
561 	struct cue_softc *sc = uether_getsc(ue);
562 	if_t ifp = uether_getifp(ue);
563 
564 	CUE_LOCK_ASSERT(sc, MA_OWNED);
565 
566 	if_inc_counter(ifp, IFCOUNTER_COLLISIONS, cue_csr_read_2(sc, CUE_TX_SINGLECOLL));
567 	if_inc_counter(ifp, IFCOUNTER_COLLISIONS, cue_csr_read_2(sc, CUE_TX_MULTICOLL));
568 	if_inc_counter(ifp, IFCOUNTER_COLLISIONS, cue_csr_read_2(sc, CUE_TX_EXCESSCOLL));
569 
570 	if (cue_csr_read_2(sc, CUE_RX_FRAMEERR))
571 		if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
572 }
573 
574 static void
cue_start(struct usb_ether * ue)575 cue_start(struct usb_ether *ue)
576 {
577 	struct cue_softc *sc = uether_getsc(ue);
578 
579 	/*
580 	 * start the USB transfers, if not already started:
581 	 */
582 	usbd_transfer_start(sc->sc_xfer[CUE_BULK_DT_RD]);
583 	usbd_transfer_start(sc->sc_xfer[CUE_BULK_DT_WR]);
584 }
585 
586 static void
cue_init(struct usb_ether * ue)587 cue_init(struct usb_ether *ue)
588 {
589 	struct cue_softc *sc = uether_getsc(ue);
590 	if_t ifp = uether_getifp(ue);
591 	int i;
592 
593 	CUE_LOCK_ASSERT(sc, MA_OWNED);
594 
595 	/*
596 	 * Cancel pending I/O and free all RX/TX buffers.
597 	 */
598 	cue_stop(ue);
599 #if 0
600 	cue_reset(sc);
601 #endif
602 	/* Set MAC address */
603 	for (i = 0; i < ETHER_ADDR_LEN; i++)
604 		cue_csr_write_1(sc, CUE_PAR0 - i, if_getlladdr(ifp)[i]);
605 
606 	/* Enable RX logic. */
607 	cue_csr_write_1(sc, CUE_ETHCTL, CUE_ETHCTL_RX_ON | CUE_ETHCTL_MCAST_ON);
608 
609 	/* Load the multicast filter */
610 	cue_setpromisc(ue);
611 
612 	/*
613 	 * Set the number of RX and TX buffers that we want
614 	 * to reserve inside the ASIC.
615 	 */
616 	cue_csr_write_1(sc, CUE_RX_BUFPKTS, CUE_RX_FRAMES);
617 	cue_csr_write_1(sc, CUE_TX_BUFPKTS, CUE_TX_FRAMES);
618 
619 	/* Set advanced operation modes. */
620 	cue_csr_write_1(sc, CUE_ADVANCED_OPMODES,
621 	    CUE_AOP_EMBED_RXLEN | 0x01);/* 1 wait state */
622 
623 	/* Program the LED operation. */
624 	cue_csr_write_1(sc, CUE_LEDCTL, CUE_LEDCTL_FOLLOW_LINK);
625 
626 	usbd_xfer_set_stall(sc->sc_xfer[CUE_BULK_DT_WR]);
627 
628 	if_setdrvflagbits(ifp, IFF_DRV_RUNNING, 0);
629 	cue_start(ue);
630 }
631 
632 /*
633  * Stop the adapter and free any mbufs allocated to the
634  * RX and TX lists.
635  */
636 static void
cue_stop(struct usb_ether * ue)637 cue_stop(struct usb_ether *ue)
638 {
639 	struct cue_softc *sc = uether_getsc(ue);
640 	if_t ifp = uether_getifp(ue);
641 
642 	CUE_LOCK_ASSERT(sc, MA_OWNED);
643 
644 	if_setdrvflagbits(ifp, 0, IFF_DRV_RUNNING);
645 
646 	/*
647 	 * stop all the transfers, if not already stopped:
648 	 */
649 	usbd_transfer_stop(sc->sc_xfer[CUE_BULK_DT_WR]);
650 	usbd_transfer_stop(sc->sc_xfer[CUE_BULK_DT_RD]);
651 
652 	cue_csr_write_1(sc, CUE_ETHCTL, 0);
653 	cue_reset(sc);
654 }
655