if_aue.c revision 1.159 1 /* $NetBSD: if_aue.c,v 1.159 2019/08/22 07:38:06 mrg Exp $ */
2
3 /*
4 * Copyright (c) 1997, 1998, 1999, 2000
5 * Bill Paul <wpaul (at) 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 * $FreeBSD: src/sys/dev/usb/if_aue.c,v 1.11 2000/01/14 01:36:14 wpaul Exp $
35 */
36
37 /*
38 * ADMtek AN986 Pegasus and AN8511 Pegasus II USB to ethernet driver.
39 * Datasheet is available from http://www.admtek.com.tw.
40 *
41 * Written by Bill Paul <wpaul (at) ee.columbia.edu>
42 * Electrical Engineering Department
43 * Columbia University, New York City
44 */
45
46 /*
47 * The Pegasus chip uses four USB "endpoints" to provide 10/100 ethernet
48 * support: the control endpoint for reading/writing registers, burst
49 * read endpoint for packet reception, burst write for packet transmission
50 * and one for "interrupts." The chip uses the same RX filter scheme
51 * as the other ADMtek ethernet parts: one perfect filter entry for the
52 * the station address and a 64-bit multicast hash table. The chip supports
53 * both MII and HomePNA attachments.
54 *
55 * Since the maximum data transfer speed of USB is supposed to be 12Mbps,
56 * you're never really going to get 100Mbps speeds from this device. I
57 * think the idea is to allow the device to connect to 10 or 100Mbps
58 * networks, not necessarily to provide 100Mbps performance. Also, since
59 * the controller uses an external PHY chip, it's possible that board
60 * designers might simply choose a 10Mbps PHY.
61 *
62 * Registers are accessed using usbd_do_request(). Packet transfers are
63 * done using usbd_transfer() and friends.
64 */
65
66 /*
67 * Ported to NetBSD and somewhat rewritten by Lennart Augustsson.
68 */
69
70 /*
71 * TODO:
72 * better error messages from rxstat
73 * split out if_auevar.h
74 * more error checks
75 * investigate short rx problem
76 * proper cleanup on errors
77 */
78
79 #include <sys/cdefs.h>
80 __KERNEL_RCSID(0, "$NetBSD: if_aue.c,v 1.159 2019/08/22 07:38:06 mrg Exp $");
81
82 #ifdef _KERNEL_OPT
83 #include "opt_usb.h"
84 #include "opt_inet.h"
85 #endif
86
87 #include <sys/param.h>
88 #include <sys/systm.h>
89 #include <sys/sockio.h>
90 #include <sys/mutex.h>
91 #include <sys/mbuf.h>
92 #include <sys/kernel.h>
93 #include <sys/socket.h>
94 #include <sys/device.h>
95 #include <sys/rndsource.h>
96
97 #include <net/if.h>
98 #include <net/if_arp.h>
99 #include <net/if_dl.h>
100 #include <net/if_media.h>
101
102 #include <net/bpf.h>
103
104 #include <net/if_ether.h>
105 #ifdef INET
106 #include <netinet/in.h>
107 #include <netinet/if_inarp.h>
108 #endif
109
110
111
112 #include <dev/mii/mii.h>
113 #include <dev/mii/miivar.h>
114
115 #include <dev/usb/usb.h>
116 #include <dev/usb/usbdi.h>
117 #include <dev/usb/usbdi_util.h>
118 #include <dev/usb/usbdevs.h>
119 #include <dev/usb/usbhist.h>
120
121 #include <sys/condvar.h>
122 #include <sys/kthread.h>
123
124 #include <dev/usb/if_auereg.h>
125
126 #ifdef USB_DEBUG
127 #ifndef AUE_DEBUG
128 #define auedebug 0
129 #else
130 static int auedebug = 0;
131
132 SYSCTL_SETUP(sysctl_hw_aue_setup, "sysctl hw.aue setup")
133 {
134 int err;
135 const struct sysctlnode *rnode;
136 const struct sysctlnode *cnode;
137
138 err = sysctl_createv(clog, 0, NULL, &rnode,
139 CTLFLAG_PERMANENT, CTLTYPE_NODE, "aue",
140 SYSCTL_DESCR("aue global controls"),
141 NULL, 0, NULL, 0, CTL_HW, CTL_CREATE, CTL_EOL);
142
143 if (err)
144 goto fail;
145
146 /* control debugging printfs */
147 err = sysctl_createv(clog, 0, &rnode, &cnode,
148 CTLFLAG_PERMANENT | CTLFLAG_READWRITE, CTLTYPE_INT,
149 "debug", SYSCTL_DESCR("Enable debugging output"),
150 NULL, 0, &auedebug, sizeof(auedebug), CTL_CREATE, CTL_EOL);
151 if (err)
152 goto fail;
153
154 return;
155 fail:
156 aprint_error("%s: sysctl_createv failed (err = %d)\n", __func__, err);
157 }
158
159 #endif /* AXE_DEBUG */
160 #endif /* USB_DEBUG */
161
162 #define DPRINTF(FMT,A,B,C,D) USBHIST_LOGN(auedebug,1,FMT,A,B,C,D)
163 #define DPRINTFN(N,FMT,A,B,C,D) USBHIST_LOGN(auedebug,N,FMT,A,B,C,D)
164 #define AUEHIST_FUNC() USBHIST_FUNC()
165 #define AUEHIST_CALLED(name) USBHIST_CALLED(auedebug)
166 #define AUEHIST_CALLARGS(FMT,A,B,C,D) \
167 USBHIST_CALLARGS(auedebug,FMT,A,B,C,D)
168 #define AUEHIST_CALLARGSN(N,FMT,A,B,C,D) \
169 USBHIST_CALLARGSN(auedebug,N,FMT,A,B,C,D)
170
171 /*
172 * Various supported device vendors/products.
173 */
174 struct aue_type {
175 struct usb_devno aue_dev;
176 uint16_t aue_flags;
177 #define LSYS 0x0001 /* use Linksys reset */
178 #define PNA 0x0002 /* has Home PNA */
179 #define PII 0x0004 /* Pegasus II chip */
180 };
181
182 Static const struct aue_type aue_devs[] = {
183 {{ USB_VENDOR_3COM, USB_PRODUCT_3COM_3C460B}, PII },
184 {{ USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_XX1}, PNA | PII },
185 {{ USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_XX2}, PII },
186 {{ USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_UFE1000}, LSYS },
187 {{ USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_XX4}, PNA },
188 {{ USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_XX5}, PNA },
189 {{ USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_XX6}, PII },
190 {{ USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_XX7}, PII },
191 {{ USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_XX8}, PII },
192 {{ USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_XX9}, PNA },
193 {{ USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_XX10}, 0 },
194 {{ USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_DSB650TX_PNA}, 0 },
195 {{ USB_VENDOR_ACCTON, USB_PRODUCT_ACCTON_USB320_EC}, 0 },
196 {{ USB_VENDOR_ACCTON, USB_PRODUCT_ACCTON_SS1001}, PII },
197 {{ USB_VENDOR_ADMTEK, USB_PRODUCT_ADMTEK_PEGASUS}, PNA },
198 {{ USB_VENDOR_ADMTEK, USB_PRODUCT_ADMTEK_PEGASUSII}, PII },
199 {{ USB_VENDOR_ADMTEK, USB_PRODUCT_ADMTEK_PEGASUSII_2}, PII },
200 {{ USB_VENDOR_ADMTEK, USB_PRODUCT_ADMTEK_PEGASUSII_3}, PII },
201 {{ USB_VENDOR_AEI, USB_PRODUCT_AEI_USBTOLAN}, PII },
202 {{ USB_VENDOR_BELKIN, USB_PRODUCT_BELKIN_USB2LAN}, PII },
203 {{ USB_VENDOR_BILLIONTON, USB_PRODUCT_BILLIONTON_USB100}, 0 },
204 {{ USB_VENDOR_BILLIONTON, USB_PRODUCT_BILLIONTON_USBLP100}, PNA },
205 {{ USB_VENDOR_BILLIONTON, USB_PRODUCT_BILLIONTON_USBEL100}, 0 },
206 {{ USB_VENDOR_BILLIONTON, USB_PRODUCT_BILLIONTON_USBE100}, PII },
207 {{ USB_VENDOR_COMPAQ, USB_PRODUCT_COMPAQ_HNE200}, PII },
208 {{ USB_VENDOR_COREGA, USB_PRODUCT_COREGA_FETHER_USB_TX}, 0 },
209 {{ USB_VENDOR_COREGA, USB_PRODUCT_COREGA_FETHER_USB_TXS},PII },
210 {{ USB_VENDOR_DLINK, USB_PRODUCT_DLINK_DSB650TX4}, LSYS | PII },
211 {{ USB_VENDOR_DLINK, USB_PRODUCT_DLINK_DSB650TX1}, LSYS },
212 {{ USB_VENDOR_DLINK, USB_PRODUCT_DLINK_DSB650TX}, LSYS },
213 {{ USB_VENDOR_DLINK, USB_PRODUCT_DLINK_DSB650TX_PNA}, PNA },
214 {{ USB_VENDOR_DLINK, USB_PRODUCT_DLINK_DSB650TX3}, LSYS | PII },
215 {{ USB_VENDOR_DLINK, USB_PRODUCT_DLINK_DSB650TX2}, LSYS | PII },
216 {{ USB_VENDOR_DLINK, USB_PRODUCT_DLINK_DSB650}, 0 },
217 {{ USB_VENDOR_ELECOM, USB_PRODUCT_ELECOM_LDUSBTX0}, 0 },
218 {{ USB_VENDOR_ELECOM, USB_PRODUCT_ELECOM_LDUSBTX1}, LSYS },
219 {{ USB_VENDOR_ELECOM, USB_PRODUCT_ELECOM_LDUSBTX2}, 0 },
220 {{ USB_VENDOR_ELECOM, USB_PRODUCT_ELECOM_LDUSBTX3}, LSYS },
221 {{ USB_VENDOR_ELECOM, USB_PRODUCT_ELECOM_LDUSBLTX}, PII },
222 {{ USB_VENDOR_ELSA, USB_PRODUCT_ELSA_USB2ETHERNET}, 0 },
223 {{ USB_VENDOR_HAWKING, USB_PRODUCT_HAWKING_UF100}, PII },
224 {{ USB_VENDOR_HP, USB_PRODUCT_HP_HN210E}, PII },
225 {{ USB_VENDOR_IODATA, USB_PRODUCT_IODATA_USBETTX}, 0 },
226 {{ USB_VENDOR_IODATA, USB_PRODUCT_IODATA_USBETTXS}, PII },
227 {{ USB_VENDOR_IODATA, USB_PRODUCT_IODATA_ETXUS2}, PII },
228 {{ USB_VENDOR_KINGSTON, USB_PRODUCT_KINGSTON_KNU101TX}, 0 },
229 {{ USB_VENDOR_LINKSYS, USB_PRODUCT_LINKSYS_USB10TX1}, LSYS | PII },
230 {{ USB_VENDOR_LINKSYS, USB_PRODUCT_LINKSYS_USB10T}, LSYS },
231 {{ USB_VENDOR_LINKSYS, USB_PRODUCT_LINKSYS_USB100TX}, LSYS },
232 {{ USB_VENDOR_LINKSYS, USB_PRODUCT_LINKSYS_USB100H1}, LSYS | PNA },
233 {{ USB_VENDOR_LINKSYS, USB_PRODUCT_LINKSYS_USB10TA}, LSYS },
234 {{ USB_VENDOR_LINKSYS, USB_PRODUCT_LINKSYS_USB10TX2}, LSYS | PII },
235 {{ USB_VENDOR_MELCO, USB_PRODUCT_MELCO_LUATX1}, 0 },
236 {{ USB_VENDOR_MELCO, USB_PRODUCT_MELCO_LUATX5}, 0 },
237 {{ USB_VENDOR_MELCO, USB_PRODUCT_MELCO_LUA2TX5}, PII },
238 {{ USB_VENDOR_MICROSOFT, USB_PRODUCT_MICROSOFT_MN110}, PII },
239 {{ USB_VENDOR_NETGEAR, USB_PRODUCT_NETGEAR_FA101}, PII },
240 {{ USB_VENDOR_SIEMENS, USB_PRODUCT_SIEMENS_SPEEDSTREAM}, PII },
241 {{ USB_VENDOR_SMARTBRIDGES, USB_PRODUCT_SMARTBRIDGES_SMARTNIC},PII },
242 {{ USB_VENDOR_SMC, USB_PRODUCT_SMC_2202USB}, 0 },
243 {{ USB_VENDOR_SMC, USB_PRODUCT_SMC_2206USB}, PII },
244 {{ USB_VENDOR_SOHOWARE, USB_PRODUCT_SOHOWARE_NUB100}, 0 },
245 };
246 #define aue_lookup(v, p) ((const struct aue_type *)usb_lookup(aue_devs, v, p))
247
248 int aue_match(device_t, cfdata_t, void *);
249 void aue_attach(device_t, device_t, void *);
250 int aue_detach(device_t, int);
251 int aue_activate(device_t, enum devact);
252
253 CFATTACH_DECL_NEW(aue, sizeof(struct aue_softc), aue_match, aue_attach,
254 aue_detach, aue_activate);
255
256 Static void aue_multithread(void *);
257
258 Static void aue_reset_pegasus_II(struct aue_softc *);
259 Static int aue_tx_list_init(struct aue_softc *);
260 Static int aue_rx_list_init(struct aue_softc *);
261 Static int aue_newbuf(struct aue_softc *, struct aue_chain *, struct mbuf *);
262 Static int aue_send(struct aue_softc *, struct mbuf *, int);
263 Static void aue_intr(struct usbd_xfer *, void *, usbd_status);
264 Static void aue_rxeof(struct usbd_xfer *, void *, usbd_status);
265 Static void aue_txeof(struct usbd_xfer *, void *, usbd_status);
266 Static void aue_tick(void *);
267 Static void aue_tick_task(void *);
268 Static void aue_start(struct ifnet *);
269 Static int aue_ioctl(struct ifnet *, u_long, void *);
270 Static void aue_init(void *);
271 Static void aue_stop(struct aue_softc *);
272 Static void aue_watchdog(struct ifnet *);
273 Static int aue_openpipes(struct aue_softc *);
274 Static int aue_ifmedia_upd(struct ifnet *);
275
276 Static int aue_eeprom_getword(struct aue_softc *, int);
277 Static void aue_read_mac(struct aue_softc *, u_char *);
278 Static int aue_miibus_readreg(device_t, int, int, uint16_t *);
279 Static int aue_miibus_writereg(device_t, int, int, uint16_t);
280 Static void aue_miibus_statchg(struct ifnet *);
281
282 Static void aue_lock_mii(struct aue_softc *);
283 Static void aue_unlock_mii(struct aue_softc *);
284
285 Static void aue_setmulti(struct aue_softc *);
286 Static uint32_t aue_crc(void *);
287 Static void aue_reset(struct aue_softc *);
288
289 Static int aue_csr_read_1(struct aue_softc *, int);
290 Static int aue_csr_write_1(struct aue_softc *, int, int);
291 Static int aue_csr_read_2(struct aue_softc *, int);
292 Static int aue_csr_write_2(struct aue_softc *, int, int);
293
294 #define AUE_SETBIT(sc, reg, x) \
295 aue_csr_write_1(sc, reg, aue_csr_read_1(sc, reg) | (x))
296
297 #define AUE_CLRBIT(sc, reg, x) \
298 aue_csr_write_1(sc, reg, aue_csr_read_1(sc, reg) & ~(x))
299
300 Static int
301 aue_csr_read_1(struct aue_softc *sc, int reg)
302 {
303 usb_device_request_t req;
304 usbd_status err;
305 uByte val = 0;
306
307 if (sc->aue_dying)
308 return 0;
309
310 req.bmRequestType = UT_READ_VENDOR_DEVICE;
311 req.bRequest = AUE_UR_READREG;
312 USETW(req.wValue, 0);
313 USETW(req.wIndex, reg);
314 USETW(req.wLength, 1);
315
316 err = usbd_do_request(sc->aue_udev, &req, &val);
317
318 if (err) {
319 AUEHIST_FUNC();
320 AUEHIST_CALLARGS("%d: aue_csr_read_1: reg=%#x err=%d",
321 device_unit(sc->aue_dev), reg, err, 0);
322 return 0;
323 }
324
325 return val;
326 }
327
328 Static int
329 aue_csr_read_2(struct aue_softc *sc, int reg)
330 {
331 usb_device_request_t req;
332 usbd_status err;
333 uWord val;
334
335 if (sc->aue_dying)
336 return 0;
337
338 req.bmRequestType = UT_READ_VENDOR_DEVICE;
339 req.bRequest = AUE_UR_READREG;
340 USETW(req.wValue, 0);
341 USETW(req.wIndex, reg);
342 USETW(req.wLength, 2);
343
344 err = usbd_do_request(sc->aue_udev, &req, &val);
345
346 if (err) {
347 AUEHIST_FUNC();
348 AUEHIST_CALLARGS("%d: aue_csr_read_2: reg=%#x err=%d",
349 device_unit(sc->aue_dev), reg, err, 0);
350 return 0;
351 }
352
353 return UGETW(val);
354 }
355
356 Static int
357 aue_csr_write_1(struct aue_softc *sc, int reg, int aval)
358 {
359 usb_device_request_t req;
360 usbd_status err;
361 uByte val;
362
363 if (sc->aue_dying)
364 return 0;
365
366 val = aval;
367 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
368 req.bRequest = AUE_UR_WRITEREG;
369 USETW(req.wValue, val);
370 USETW(req.wIndex, reg);
371 USETW(req.wLength, 1);
372
373 err = usbd_do_request(sc->aue_udev, &req, &val);
374
375 if (err) {
376 AUEHIST_FUNC();
377 AUEHIST_CALLARGS("%d: aue_csr_write_1: reg=%#x err=%d",
378 device_unit(sc->aue_dev), reg, err, 0);
379 return -1;
380 }
381
382 return 0;
383 }
384
385 Static int
386 aue_csr_write_2(struct aue_softc *sc, int reg, int aval)
387 {
388 usb_device_request_t req;
389 usbd_status err;
390 uWord val;
391
392 if (sc->aue_dying)
393 return 0;
394
395 USETW(val, aval);
396 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
397 req.bRequest = AUE_UR_WRITEREG;
398 USETW(req.wValue, aval);
399 USETW(req.wIndex, reg);
400 USETW(req.wLength, 2);
401
402 err = usbd_do_request(sc->aue_udev, &req, &val);
403
404 if (err) {
405 AUEHIST_FUNC();
406 AUEHIST_CALLARGS("%s: aue_csr_write_2: reg=%#x err=%d",
407 device_unit(sc->aue_dev), reg, err, 0);
408 return -1;
409 }
410
411 return 0;
412 }
413
414 /*
415 * Read a word of data stored in the EEPROM at address 'addr.'
416 */
417 Static int
418 aue_eeprom_getword(struct aue_softc *sc, int addr)
419 {
420 int i;
421 AUEHIST_FUNC(); AUEHIST_CALLED();
422
423 aue_csr_write_1(sc, AUE_EE_REG, addr);
424 aue_csr_write_1(sc, AUE_EE_CTL, AUE_EECTL_READ);
425
426 for (i = 0; i < AUE_TIMEOUT; i++) {
427 if (aue_csr_read_1(sc, AUE_EE_CTL) & AUE_EECTL_DONE)
428 break;
429 }
430
431 if (i == AUE_TIMEOUT) {
432 printf("%s: EEPROM read timed out\n",
433 device_xname(sc->aue_dev));
434 }
435
436 return aue_csr_read_2(sc, AUE_EE_DATA);
437 }
438
439 /*
440 * Read the MAC from the EEPROM. It's at offset 0.
441 */
442 Static void
443 aue_read_mac(struct aue_softc *sc, u_char *dest)
444 {
445 int i;
446 int off = 0;
447 int word;
448
449 AUEHIST_FUNC();
450 AUEHIST_CALLARGS("%d: enter",
451 device_unit(sc->aue_dev), 0, 0, 0);
452
453 for (i = 0; i < 3; i++) {
454 word = aue_eeprom_getword(sc, off + i);
455 dest[2 * i] = (u_char)word;
456 dest[2 * i + 1] = (u_char)(word >> 8);
457 }
458 }
459
460 /* Get exclusive access to the MII registers */
461 Static void
462 aue_lock_mii(struct aue_softc *sc)
463 {
464 sc->aue_refcnt++;
465 mutex_enter(&sc->aue_mii_lock);
466 }
467
468 Static void
469 aue_unlock_mii(struct aue_softc *sc)
470 {
471 mutex_exit(&sc->aue_mii_lock);
472 if (--sc->aue_refcnt < 0)
473 usb_detach_wakeupold(sc->aue_dev);
474 }
475
476 Static int
477 aue_miibus_readreg(device_t dev, int phy, int reg, uint16_t *val)
478 {
479 struct aue_softc *sc = device_private(dev);
480 int i, rv = 0;
481
482 AUEHIST_FUNC();
483
484 if (sc->aue_dying) {
485 AUEHIST_CALLARGS("%d: phy=%jx reg=%jx is dying",
486 device_unit(sc->aue_dev), phy, reg, 0);
487 return -1;
488 }
489
490 #if 0
491 /*
492 * The Am79C901 HomePNA PHY actually contains
493 * two transceivers: a 1Mbps HomePNA PHY and a
494 * 10Mbps full/half duplex ethernet PHY with
495 * NWAY autoneg. However in the ADMtek adapter,
496 * only the 1Mbps PHY is actually connected to
497 * anything, so we ignore the 10Mbps one. It
498 * happens to be configured for MII address 3,
499 * so we filter that out.
500 */
501 if (sc->aue_vendor == USB_VENDOR_ADMTEK &&
502 sc->aue_product == USB_PRODUCT_ADMTEK_PEGASUS) {
503 if (phy == 3)
504 return -1;
505 }
506 #endif
507
508 aue_lock_mii(sc);
509 aue_csr_write_1(sc, AUE_PHY_ADDR, phy);
510 aue_csr_write_1(sc, AUE_PHY_CTL, reg | AUE_PHYCTL_READ);
511
512 for (i = 0; i < AUE_TIMEOUT; i++) {
513 if (aue_csr_read_1(sc, AUE_PHY_CTL) & AUE_PHYCTL_DONE)
514 break;
515 }
516
517 if (i == AUE_TIMEOUT) {
518 AUEHIST_CALLARGS("%d: phy=%jx reg=%jx read timed out",
519 device_unit(sc->aue_dev), phy, reg, 0);
520 rv = ETIMEDOUT;
521 goto out;
522 }
523
524 *val = aue_csr_read_2(sc, AUE_PHY_DATA);
525
526 AUEHIST_CALLARGS("%d: phy=%jx reg=%jx => %#04hx",
527 device_unit(sc->aue_dev), phy, reg, *val);
528
529 out:
530 aue_unlock_mii(sc);
531 return rv;
532 }
533
534 Static int
535 aue_miibus_writereg(device_t dev, int phy, int reg, uint16_t val)
536 {
537 struct aue_softc *sc = device_private(dev);
538 int i, rv = 0;
539
540 AUEHIST_FUNC();
541 AUEHIST_CALLARGS("%d: phy=%d reg=%d data=%#04hx",
542 device_unit(sc->aue_dev), phy, reg, val);
543
544 #if 0
545 if (sc->aue_vendor == USB_VENDOR_ADMTEK &&
546 sc->aue_product == USB_PRODUCT_ADMTEK_PEGASUS) {
547 if (phy == 3)
548 return -1;
549 }
550 #endif
551
552 aue_lock_mii(sc);
553 aue_csr_write_2(sc, AUE_PHY_DATA, val);
554 aue_csr_write_1(sc, AUE_PHY_ADDR, phy);
555 aue_csr_write_1(sc, AUE_PHY_CTL, reg | AUE_PHYCTL_WRITE);
556
557 for (i = 0; i < AUE_TIMEOUT; i++) {
558 if (aue_csr_read_1(sc, AUE_PHY_CTL) & AUE_PHYCTL_DONE)
559 break;
560 }
561
562 if (i == AUE_TIMEOUT) {
563 DPRINTF("%d: phy=%jx reg=%jx val=%jx write timed out",
564 device_unit(sc->aue_dev), phy, reg, val);
565 rv = ETIMEDOUT;
566 }
567 aue_unlock_mii(sc);
568
569 return rv;
570 }
571
572 Static void
573 aue_miibus_statchg(struct ifnet *ifp)
574 {
575 struct aue_softc *sc = ifp->if_softc;
576 struct mii_data *mii = GET_MII(sc);
577
578 AUEHIST_FUNC(); AUEHIST_CALLED();
579 AUEHIST_CALLARGS("%d: ifp=%jx",
580 device_unit(sc->aue_dev), (uintptr_t)ifp, 0, 0);
581
582 aue_lock_mii(sc);
583 AUE_CLRBIT(sc, AUE_CTL0, AUE_CTL0_RX_ENB | AUE_CTL0_TX_ENB);
584
585 if (IFM_SUBTYPE(mii->mii_media_active) == IFM_100_TX) {
586 AUE_SETBIT(sc, AUE_CTL1, AUE_CTL1_SPEEDSEL);
587 } else {
588 AUE_CLRBIT(sc, AUE_CTL1, AUE_CTL1_SPEEDSEL);
589 }
590
591 if ((mii->mii_media_active & IFM_FDX) != 0)
592 AUE_SETBIT(sc, AUE_CTL1, AUE_CTL1_DUPLEX);
593 else
594 AUE_CLRBIT(sc, AUE_CTL1, AUE_CTL1_DUPLEX);
595
596 AUE_SETBIT(sc, AUE_CTL0, AUE_CTL0_RX_ENB | AUE_CTL0_TX_ENB);
597 aue_unlock_mii(sc);
598
599 /*
600 * Set the LED modes on the LinkSys adapter.
601 * This turns on the 'dual link LED' bin in the auxmode
602 * register of the Broadcom PHY.
603 */
604 if (!sc->aue_dying && (sc->aue_flags & LSYS)) {
605 uint16_t auxmode;
606 aue_miibus_readreg(sc->aue_dev, 0, 0x1b, &auxmode);
607 aue_miibus_writereg(sc->aue_dev, 0, 0x1b, auxmode | 0x04);
608 }
609 }
610
611 #define AUE_POLY 0xEDB88320
612 #define AUE_BITS 6
613
614 Static uint32_t
615 aue_crc(void *addrv)
616 {
617 uint32_t idx, bit, data, crc;
618 char *addr = addrv;
619
620 /* Compute CRC for the address value. */
621 crc = 0xFFFFFFFF; /* initial value */
622
623 for (idx = 0; idx < 6; idx++) {
624 for (data = *addr++, bit = 0; bit < 8; bit++, data >>= 1)
625 crc = (crc >> 1) ^ (((crc ^ data) & 1) ? AUE_POLY : 0);
626 }
627
628 return crc & ((1 << AUE_BITS) - 1);
629 }
630
631 Static void
632 aue_setmulti(struct aue_softc *sc)
633 {
634 struct ethercom *ec = &sc->aue_ec;
635 struct ifnet *ifp;
636 struct ether_multi *enm;
637 struct ether_multistep step;
638 uint32_t h = 0, i;
639 uint8_t hashtbl[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
640
641 AUEHIST_FUNC();
642 AUEHIST_CALLARGSN(5, "%d: enter",
643 device_unit(sc->aue_dev), 0, 0, 0);
644
645 ifp = GET_IFP(sc);
646
647 if (ifp->if_flags & IFF_PROMISC) {
648 allmulti:
649 ifp->if_flags |= IFF_ALLMULTI;
650 AUE_SETBIT(sc, AUE_CTL0, AUE_CTL0_ALLMULTI);
651 return;
652 }
653
654 AUE_CLRBIT(sc, AUE_CTL0, AUE_CTL0_ALLMULTI);
655
656 /* now program new ones */
657 ETHER_LOCK(ec);
658 ETHER_FIRST_MULTI(step, ec, enm);
659 while (enm != NULL) {
660 if (memcmp(enm->enm_addrlo,
661 enm->enm_addrhi, ETHER_ADDR_LEN) != 0) {
662 ETHER_UNLOCK(ec);
663 goto allmulti;
664 }
665
666 h = aue_crc(enm->enm_addrlo);
667 hashtbl[h >> 3] |= 1 << (h & 0x7);
668 ETHER_NEXT_MULTI(step, enm);
669 }
670 ETHER_UNLOCK(ec);
671
672 /* write the hashtable */
673 for (i = 0; i < 8; i++)
674 aue_csr_write_1(sc, AUE_MAR0 + i, hashtbl[i]);
675
676 ifp->if_flags &= ~IFF_ALLMULTI;
677 }
678
679 Static void
680 aue_reset_pegasus_II(struct aue_softc *sc)
681 {
682 /* Magic constants taken from Linux driver. */
683 aue_csr_write_1(sc, AUE_REG_1D, 0);
684 aue_csr_write_1(sc, AUE_REG_7B, 2);
685 #if 0
686 if ((sc->aue_flags & HAS_HOME_PNA) && mii_mode)
687 aue_csr_write_1(sc, AUE_REG_81, 6);
688 else
689 #endif
690 aue_csr_write_1(sc, AUE_REG_81, 2);
691 }
692
693 Static void
694 aue_reset(struct aue_softc *sc)
695 {
696 int i;
697
698 AUEHIST_FUNC();
699 AUEHIST_CALLARGSN(2, "%d: enter",
700 device_unit(sc->aue_dev), 0, 0, 0);
701
702 AUE_SETBIT(sc, AUE_CTL1, AUE_CTL1_RESETMAC);
703
704 for (i = 0; i < AUE_TIMEOUT; i++) {
705 if (!(aue_csr_read_1(sc, AUE_CTL1) & AUE_CTL1_RESETMAC))
706 break;
707 }
708
709 if (i == AUE_TIMEOUT)
710 printf("%s: reset failed\n", device_xname(sc->aue_dev));
711
712 #if 0
713 /* XXX what is mii_mode supposed to be */
714 if (sc->aue_mii_mode && (sc->aue_flags & PNA))
715 aue_csr_write_1(sc, AUE_GPIO1, 0x34);
716 else
717 aue_csr_write_1(sc, AUE_GPIO1, 0x26);
718 #endif
719
720 /*
721 * The PHY(s) attached to the Pegasus chip may be held
722 * in reset until we flip on the GPIO outputs. Make sure
723 * to set the GPIO pins high so that the PHY(s) will
724 * be enabled.
725 *
726 * Note: We force all of the GPIO pins low first, *then*
727 * enable the ones we want.
728 */
729 if (sc->aue_flags & LSYS) {
730 /* Grrr. LinkSys has to be different from everyone else. */
731 aue_csr_write_1(sc, AUE_GPIO0,
732 AUE_GPIO_SEL0 | AUE_GPIO_SEL1);
733 } else {
734 aue_csr_write_1(sc, AUE_GPIO0,
735 AUE_GPIO_OUT0 | AUE_GPIO_SEL0);
736 }
737 aue_csr_write_1(sc, AUE_GPIO0,
738 AUE_GPIO_OUT0 | AUE_GPIO_SEL0 | AUE_GPIO_SEL1);
739
740 if (sc->aue_flags & PII)
741 aue_reset_pegasus_II(sc);
742
743 /* Wait a little while for the chip to get its brains in order. */
744 delay(10000); /* XXX */
745 }
746
747 /*
748 * Probe for a Pegasus chip.
749 */
750 int
751 aue_match(device_t parent, cfdata_t match, void *aux)
752 {
753 struct usb_attach_arg *uaa = aux;
754
755 /*
756 * Some manufacturers use the same vendor and product id for
757 * different devices. We need to sanity check the DeviceClass
758 * in this case
759 * Currently known guilty products:
760 * 0x050d/0x0121 Belkin Bluetooth and USB2LAN
761 *
762 * If this turns out to be more common, we could use a quirk
763 * table.
764 */
765 if (uaa->uaa_vendor == USB_VENDOR_BELKIN &&
766 uaa->uaa_product == USB_PRODUCT_BELKIN_USB2LAN) {
767 usb_device_descriptor_t *dd;
768
769 dd = usbd_get_device_descriptor(uaa->uaa_device);
770 if (dd != NULL &&
771 dd->bDeviceClass != UDCLASS_IN_INTERFACE)
772 return UMATCH_NONE;
773 }
774
775 return aue_lookup(uaa->uaa_vendor, uaa->uaa_product) != NULL ?
776 UMATCH_VENDOR_PRODUCT : UMATCH_NONE;
777 }
778
779 /*
780 * Attach the interface. Allocate softc structures, do ifmedia
781 * setup and ethernet/BPF attach.
782 */
783 void
784 aue_attach(device_t parent, device_t self, void *aux)
785 {
786 struct aue_softc *sc = device_private(self);
787 struct usb_attach_arg *uaa = aux;
788 char *devinfop;
789 int s;
790 u_char eaddr[ETHER_ADDR_LEN];
791 struct ifnet *ifp;
792 struct mii_data *mii;
793 struct usbd_device *dev = uaa->uaa_device;
794 struct usbd_interface *iface;
795 usbd_status err;
796 usb_interface_descriptor_t *id;
797 usb_endpoint_descriptor_t *ed;
798 int i;
799
800 sc->aue_dev = self;
801
802 AUEHIST_FUNC();
803 AUEHIST_CALLARGSN(2, "%d: enter sc=%jx",
804 device_unit(sc->aue_dev), (uintptr_t)sc, 0, 0);
805
806 aprint_naive("\n");
807 aprint_normal("\n");
808
809 devinfop = usbd_devinfo_alloc(uaa->uaa_device, 0);
810 aprint_normal_dev(self, "%s\n", devinfop);
811 usbd_devinfo_free(devinfop);
812
813 err = usbd_set_config_no(dev, AUE_CONFIG_NO, 1);
814 if (err) {
815 aprint_error_dev(self, "failed to set configuration"
816 ", err=%s\n", usbd_errstr(err));
817 return;
818 }
819
820 usb_init_task(&sc->aue_tick_task, aue_tick_task, sc, 0);
821 usb_init_task(&sc->aue_stop_task, (void (*)(void *))aue_stop, sc, 0);
822 mutex_init(&sc->aue_mii_lock, MUTEX_DEFAULT, IPL_NONE);
823
824 err = usbd_device2interface_handle(dev, AUE_IFACE_IDX, &iface);
825 if (err) {
826 aprint_error_dev(self, "getting interface handle failed\n");
827 return;
828 }
829 sc->aue_closing = 0;
830
831 mutex_init(&sc->aue_mcmtx, MUTEX_DRIVER, IPL_NET);
832 cv_init(&sc->aue_domc, "auemc");
833 cv_init(&sc->aue_closemc, "auemccl");
834
835 err = kthread_create(PRI_NONE, 0, NULL,
836 aue_multithread, sc, &sc->aue_thread,
837 "%s-mc", device_xname(sc->aue_dev));
838
839 if (err) {
840 aprint_error_dev(self,
841 "creating multicast configuration thread\n");
842 return;
843 }
844 sc->aue_flags = aue_lookup(uaa->uaa_vendor,
845 uaa->uaa_product)->aue_flags;
846
847 sc->aue_udev = dev;
848 sc->aue_iface = iface;
849 sc->aue_product = uaa->uaa_product;
850 sc->aue_vendor = uaa->uaa_vendor;
851
852 id = usbd_get_interface_descriptor(iface);
853
854 /* Find endpoints. */
855 for (i = 0; i < id->bNumEndpoints; i++) {
856 ed = usbd_interface2endpoint_descriptor(iface, i);
857 if (ed == NULL) {
858 aprint_error_dev(self,
859 "couldn't get endpoint descriptor %d\n", i);
860 return;
861 }
862 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
863 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
864 sc->aue_ed[AUE_ENDPT_RX] = ed->bEndpointAddress;
865 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
866 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
867 sc->aue_ed[AUE_ENDPT_TX] = ed->bEndpointAddress;
868 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
869 UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) {
870 sc->aue_ed[AUE_ENDPT_INTR] = ed->bEndpointAddress;
871 }
872 }
873
874 if (sc->aue_ed[AUE_ENDPT_RX] == 0 || sc->aue_ed[AUE_ENDPT_TX] == 0 ||
875 sc->aue_ed[AUE_ENDPT_INTR] == 0) {
876 aprint_error_dev(self, "missing endpoint\n");
877 return;
878 }
879
880
881 s = splnet();
882
883 /* Reset the adapter. */
884 aue_reset(sc);
885
886 /*
887 * Get station address from the EEPROM.
888 */
889 aue_read_mac(sc, eaddr);
890
891 /*
892 * A Pegasus chip was detected. Inform the world.
893 */
894 ifp = GET_IFP(sc);
895 aprint_normal_dev(self, "Ethernet address %s\n", ether_sprintf(eaddr));
896
897 /* Initialize interface info.*/
898 ifp->if_softc = sc;
899 ifp->if_mtu = ETHERMTU;
900 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
901 ifp->if_ioctl = aue_ioctl;
902 ifp->if_start = aue_start;
903 ifp->if_watchdog = aue_watchdog;
904 strlcpy(ifp->if_xname, device_xname(sc->aue_dev), IFNAMSIZ);
905
906 IFQ_SET_READY(&ifp->if_snd);
907
908 /* Initialize MII/media info. */
909 mii = &sc->aue_mii;
910 mii->mii_ifp = ifp;
911 mii->mii_readreg = aue_miibus_readreg;
912 mii->mii_writereg = aue_miibus_writereg;
913 mii->mii_statchg = aue_miibus_statchg;
914 mii->mii_flags = MIIF_AUTOTSLEEP;
915 sc->aue_ec.ec_mii = mii;
916 ifmedia_init(&mii->mii_media, 0, aue_ifmedia_upd, ether_mediastatus);
917 mii_attach(self, mii, 0xffffffff, MII_PHY_ANY, MII_OFFSET_ANY, 0);
918 if (LIST_FIRST(&mii->mii_phys) == NULL) {
919 ifmedia_add(&mii->mii_media, IFM_ETHER | IFM_NONE, 0, NULL);
920 ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_NONE);
921 } else
922 ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_AUTO);
923
924 /* Attach the interface. */
925 if_attach(ifp);
926 ether_ifattach(ifp, eaddr);
927 rnd_attach_source(&sc->rnd_source, device_xname(sc->aue_dev),
928 RND_TYPE_NET, RND_FLAG_DEFAULT);
929
930 callout_init(&(sc->aue_stat_ch), 0);
931
932 sc->aue_attached = 1;
933 splx(s);
934
935 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->aue_udev, sc->aue_dev);
936
937 if (!pmf_device_register(self, NULL, NULL))
938 aprint_error_dev(self, "couldn't establish power handler\n");
939
940 return;
941 }
942
943 int
944 aue_detach(device_t self, int flags)
945 {
946 struct aue_softc *sc = device_private(self);
947 struct ifnet *ifp = GET_IFP(sc);
948 int s;
949
950 AUEHIST_FUNC();
951 AUEHIST_CALLARGSN(2, "%d: enter",
952 device_unit(sc->aue_dev), 0, 0, 0);
953
954 if (!sc->aue_attached) {
955 /* Detached before attached finished, so just bail out. */
956 return 0;
957 }
958
959 pmf_device_deregister(self);
960
961 /*
962 * XXX Halting callout guarantees no more tick tasks. What
963 * guarantees no more stop tasks? What guarantees no more
964 * calls to aue_send? Don't we need to wait for if_detach or
965 * something? Should we set sc->aue_dying here? Is device
966 * deactivation guaranteed to have already happened?
967 */
968 callout_halt(&sc->aue_stat_ch, NULL);
969 usb_rem_task_wait(sc->aue_udev, &sc->aue_tick_task, USB_TASKQ_DRIVER,
970 NULL);
971 usb_rem_task_wait(sc->aue_udev, &sc->aue_stop_task, USB_TASKQ_DRIVER,
972 NULL);
973
974 sc->aue_closing = 1;
975 cv_signal(&sc->aue_domc);
976
977 mutex_enter(&sc->aue_mcmtx);
978 cv_wait(&sc->aue_closemc,&sc->aue_mcmtx);
979 mutex_exit(&sc->aue_mcmtx);
980
981 mutex_destroy(&sc->aue_mcmtx);
982 cv_destroy(&sc->aue_domc);
983 cv_destroy(&sc->aue_closemc);
984
985 s = splusb();
986
987 if (ifp->if_flags & IFF_RUNNING)
988 aue_stop(sc);
989
990 rnd_detach_source(&sc->rnd_source);
991 mii_detach(&sc->aue_mii, MII_PHY_ANY, MII_OFFSET_ANY);
992 ifmedia_delete_instance(&sc->aue_mii.mii_media, IFM_INST_ANY);
993 ether_ifdetach(ifp);
994
995 if_detach(ifp);
996
997 #ifdef DIAGNOSTIC
998 if (sc->aue_ep[AUE_ENDPT_TX] != NULL ||
999 sc->aue_ep[AUE_ENDPT_RX] != NULL ||
1000 sc->aue_ep[AUE_ENDPT_INTR] != NULL)
1001 aprint_error_dev(self, "detach has active endpoints\n");
1002 #endif
1003
1004 sc->aue_attached = 0;
1005
1006 if (--sc->aue_refcnt >= 0) {
1007 /* Wait for processes to go away. */
1008 usb_detach_waitold(sc->aue_dev);
1009 }
1010 splx(s);
1011
1012 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->aue_udev, sc->aue_dev);
1013
1014 mutex_destroy(&sc->aue_mii_lock);
1015 #if 0
1016 mutex_destroy(&sc->wkmtx);
1017 #endif
1018 return 0;
1019 }
1020
1021 int
1022 aue_activate(device_t self, enum devact act)
1023 {
1024 struct aue_softc *sc = device_private(self);
1025
1026 AUEHIST_FUNC();
1027 AUEHIST_CALLARGSN(2, "%d: enter",
1028 device_unit(sc->aue_dev), 0, 0, 0);
1029
1030 switch (act) {
1031 case DVACT_DEACTIVATE:
1032 if_deactivate(&sc->aue_ec.ec_if);
1033 sc->aue_dying = 1;
1034 return 0;
1035 default:
1036 return EOPNOTSUPP;
1037 }
1038 }
1039
1040 /*
1041 * Initialize an RX descriptor and attach an MBUF cluster.
1042 */
1043 Static int
1044 aue_newbuf(struct aue_softc *sc, struct aue_chain *c, struct mbuf *m)
1045 {
1046 struct mbuf *m_new = NULL;
1047
1048 AUEHIST_FUNC();
1049 AUEHIST_CALLARGSN(10, "%d: enter",
1050 device_unit(sc->aue_dev), 0, 0, 0);
1051
1052 if (m == NULL) {
1053 MGETHDR(m_new, M_DONTWAIT, MT_DATA);
1054 if (m_new == NULL) {
1055 aprint_error_dev(sc->aue_dev, "no memory for rx list "
1056 "-- packet dropped!\n");
1057 return ENOBUFS;
1058 }
1059
1060 MCLGET(m_new, M_DONTWAIT);
1061 if (!(m_new->m_flags & M_EXT)) {
1062 aprint_error_dev(sc->aue_dev, "no memory for rx "
1063 "list -- packet dropped!\n");
1064 m_freem(m_new);
1065 return ENOBUFS;
1066 }
1067 m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
1068 } else {
1069 m_new = m;
1070 m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
1071 m_new->m_data = m_new->m_ext.ext_buf;
1072 }
1073
1074 m_adj(m_new, ETHER_ALIGN);
1075 c->aue_mbuf = m_new;
1076
1077 return 0;
1078 }
1079
1080 Static int
1081 aue_rx_list_init(struct aue_softc *sc)
1082 {
1083 struct aue_cdata *cd;
1084 struct aue_chain *c;
1085 int i;
1086
1087 AUEHIST_FUNC();
1088 AUEHIST_CALLARGSN(5, "%d: enter",
1089 device_unit(sc->aue_dev), 0, 0, 0);
1090
1091 cd = &sc->aue_cdata;
1092 for (i = 0; i < AUE_RX_LIST_CNT; i++) {
1093 c = &cd->aue_rx_chain[i];
1094 c->aue_sc = sc;
1095 if (aue_newbuf(sc, c, NULL) == ENOBUFS)
1096 return ENOBUFS;
1097 if (c->aue_xfer == NULL) {
1098 int err = usbd_create_xfer(sc->aue_ep[AUE_ENDPT_RX],
1099 AUE_BUFSZ, 0, 0, &c->aue_xfer);
1100 if (err) {
1101 return err;
1102 }
1103 c->aue_buf = usbd_get_buffer(c->aue_xfer);
1104 }
1105 }
1106
1107 return 0;
1108 }
1109
1110 Static int
1111 aue_tx_list_init(struct aue_softc *sc)
1112 {
1113 struct aue_cdata *cd;
1114 struct aue_chain *c;
1115 int i;
1116
1117 AUEHIST_FUNC();
1118 AUEHIST_CALLARGSN(5, "%d: enter",
1119 device_unit(sc->aue_dev), 0, 0, 0);
1120
1121 cd = &sc->aue_cdata;
1122 for (i = 0; i < AUE_TX_LIST_CNT; i++) {
1123 c = &cd->aue_tx_chain[i];
1124 c->aue_sc = sc;
1125 c->aue_mbuf = NULL;
1126 if (c->aue_xfer == NULL) {
1127 int err = usbd_create_xfer(sc->aue_ep[AUE_ENDPT_TX],
1128 AUE_BUFSZ, USBD_FORCE_SHORT_XFER, 0, &c->aue_xfer);
1129 if (err) {
1130 return err;
1131 }
1132 c->aue_buf = usbd_get_buffer(c->aue_xfer);
1133 }
1134 }
1135
1136 return 0;
1137 }
1138
1139 Static void
1140 aue_intr(struct usbd_xfer *xfer, void *priv,
1141 usbd_status status)
1142 {
1143 struct aue_softc *sc = priv;
1144 struct ifnet *ifp = GET_IFP(sc);
1145 struct aue_intrpkt *p = &sc->aue_cdata.aue_ibuf;
1146
1147 AUEHIST_FUNC();
1148 AUEHIST_CALLARGSN(15, "%d: enter",
1149 device_unit(sc->aue_dev), 0, 0, 0);
1150
1151 if (sc->aue_dying)
1152 return;
1153
1154 if (!(ifp->if_flags & IFF_RUNNING))
1155 return;
1156
1157 if (status != USBD_NORMAL_COMPLETION) {
1158 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
1159 return;
1160 }
1161 sc->aue_intr_errs++;
1162 if (usbd_ratecheck(&sc->aue_rx_notice)) {
1163 aprint_debug_dev(sc->aue_dev,
1164 "%u usb errors on intr: %s\n", sc->aue_intr_errs,
1165 usbd_errstr(status));
1166 sc->aue_intr_errs = 0;
1167 }
1168 if (status == USBD_STALLED)
1169 usbd_clear_endpoint_stall_async(sc->aue_ep[AUE_ENDPT_RX]);
1170 return;
1171 }
1172
1173 if (p->aue_txstat0)
1174 ifp->if_oerrors++;
1175
1176 if (p->aue_txstat0 & (AUE_TXSTAT0_LATECOLL | AUE_TXSTAT0_EXCESSCOLL))
1177 ifp->if_collisions++;
1178 }
1179
1180 /*
1181 * A frame has been uploaded: pass the resulting mbuf chain up to
1182 * the higher level protocols.
1183 */
1184 Static void
1185 aue_rxeof(struct usbd_xfer *xfer, void *priv, usbd_status status)
1186 {
1187 struct aue_chain *c = priv;
1188 struct aue_softc *sc = c->aue_sc;
1189 struct ifnet *ifp = GET_IFP(sc);
1190 struct mbuf *m;
1191 uint32_t total_len;
1192 struct aue_rxpkt r;
1193 int s;
1194
1195 AUEHIST_FUNC();
1196 AUEHIST_CALLARGSN(10, "%d: enter",
1197 device_unit(sc->aue_dev), 0, 0, 0);
1198
1199 if (sc->aue_dying)
1200 return;
1201
1202 if (!(ifp->if_flags & IFF_RUNNING))
1203 return;
1204
1205 if (status != USBD_NORMAL_COMPLETION) {
1206 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
1207 return;
1208 sc->aue_rx_errs++;
1209 if (usbd_ratecheck(&sc->aue_rx_notice)) {
1210 aprint_error_dev(sc->aue_dev,
1211 "%u usb errors on rx: %s\n", sc->aue_rx_errs,
1212 usbd_errstr(status));
1213 sc->aue_rx_errs = 0;
1214 }
1215 if (status == USBD_STALLED)
1216 usbd_clear_endpoint_stall_async(sc->aue_ep[AUE_ENDPT_RX]);
1217 goto done;
1218 }
1219
1220 usbd_get_xfer_status(xfer, NULL, NULL, &total_len, NULL);
1221
1222 memcpy(mtod(c->aue_mbuf, char *), c->aue_buf, total_len);
1223
1224 if (total_len <= 4 + ETHER_CRC_LEN) {
1225 ifp->if_ierrors++;
1226 goto done;
1227 }
1228
1229 memcpy(&r, c->aue_buf + total_len - 4, sizeof(r));
1230
1231 /* Turn off all the non-error bits in the rx status word. */
1232 r.aue_rxstat &= AUE_RXSTAT_MASK;
1233 if (r.aue_rxstat) {
1234 ifp->if_ierrors++;
1235 goto done;
1236 }
1237
1238 /* No errors; receive the packet. */
1239 m = c->aue_mbuf;
1240 total_len -= ETHER_CRC_LEN + 4;
1241 m->m_pkthdr.len = m->m_len = total_len;
1242
1243 m_set_rcvif(m, ifp);
1244
1245 s = splnet();
1246
1247 /* XXX ugly */
1248 if (aue_newbuf(sc, c, NULL) == ENOBUFS) {
1249 ifp->if_ierrors++;
1250 goto done1;
1251 }
1252
1253 DPRINTFN(10, "%d: deliver %d",
1254 device_unit(sc->aue_dev), m->m_len, 0, 0);
1255 if_percpuq_enqueue(ifp->if_percpuq, m);
1256 done1:
1257 splx(s);
1258
1259 done:
1260
1261 /* Setup new transfer. */
1262 usbd_setup_xfer(xfer, c, c->aue_buf, AUE_BUFSZ,
1263 USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, aue_rxeof);
1264 usbd_transfer(xfer);
1265
1266 DPRINTFN(10, "%d: start rx", device_unit(sc->aue_dev), 0, 0, 0);
1267 }
1268
1269 /*
1270 * A frame was downloaded to the chip. It's safe for us to clean up
1271 * the list buffers.
1272 */
1273
1274 Static void
1275 aue_txeof(struct usbd_xfer *xfer, void *priv,
1276 usbd_status status)
1277 {
1278 struct aue_chain *c = priv;
1279 struct aue_softc *sc = c->aue_sc;
1280 struct ifnet *ifp = GET_IFP(sc);
1281 int s;
1282
1283 if (sc->aue_dying)
1284 return;
1285
1286 s = splnet();
1287
1288 AUEHIST_FUNC();
1289 AUEHIST_CALLARGSN(10, "%d: enter status=%d",
1290 device_unit(sc->aue_dev), status, 0, 0);
1291
1292 ifp->if_timer = 0;
1293 ifp->if_flags &= ~IFF_OACTIVE;
1294
1295 if (status != USBD_NORMAL_COMPLETION) {
1296 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
1297 splx(s);
1298 return;
1299 }
1300 ifp->if_oerrors++;
1301 aprint_error_dev(sc->aue_dev, "usb error on tx: %s\n",
1302 usbd_errstr(status));
1303 if (status == USBD_STALLED)
1304 usbd_clear_endpoint_stall_async(sc->aue_ep[AUE_ENDPT_TX]);
1305 splx(s);
1306 return;
1307 }
1308
1309 ifp->if_opackets++;
1310
1311 m_freem(c->aue_mbuf);
1312 c->aue_mbuf = NULL;
1313
1314 if (IFQ_IS_EMPTY(&ifp->if_snd) == 0)
1315 aue_start(ifp);
1316
1317 splx(s);
1318 }
1319
1320 Static void
1321 aue_tick(void *xsc)
1322 {
1323 struct aue_softc *sc = xsc;
1324
1325 AUEHIST_FUNC();
1326 AUEHIST_CALLARGSN(15, "%d: enter", device_unit(sc->aue_dev), 0, 0, 0);
1327
1328 if (sc == NULL)
1329 return;
1330
1331 if (sc->aue_dying)
1332 return;
1333
1334 /* Perform periodic stuff in process context. */
1335 usb_add_task(sc->aue_udev, &sc->aue_tick_task, USB_TASKQ_DRIVER);
1336 }
1337
1338 Static void
1339 aue_tick_task(void *xsc)
1340 {
1341 struct aue_softc *sc = xsc;
1342 struct ifnet *ifp;
1343 struct mii_data *mii;
1344 int s;
1345
1346 AUEHIST_FUNC();
1347 AUEHIST_CALLARGSN(15, "%d: enter", device_unit(sc->aue_dev), 0, 0, 0);
1348
1349 if (sc->aue_dying)
1350 return;
1351
1352 ifp = GET_IFP(sc);
1353 mii = GET_MII(sc);
1354 if (mii == NULL)
1355 return;
1356
1357 s = splnet();
1358
1359 mii_tick(mii);
1360 if (!sc->aue_link) {
1361 mii_pollstat(mii); /* XXX FreeBSD has removed this call */
1362 if (mii->mii_media_status & IFM_ACTIVE &&
1363 IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
1364 DPRINTFN(2, "%d: got link",
1365 device_unit(sc->aue_dev), 0, 0, 0);
1366 sc->aue_link++;
1367 if (IFQ_IS_EMPTY(&ifp->if_snd) == 0)
1368 aue_start(ifp);
1369 }
1370 }
1371
1372 callout_reset(&(sc->aue_stat_ch), (hz), (aue_tick), (sc));
1373
1374 splx(s);
1375 }
1376
1377 Static int
1378 aue_send(struct aue_softc *sc, struct mbuf *m, int idx)
1379 {
1380 int total_len;
1381 struct aue_chain *c;
1382 usbd_status err;
1383
1384 AUEHIST_FUNC();
1385 AUEHIST_CALLARGSN(10, "%d: enter", device_unit(sc->aue_dev), 0, 0, 0);
1386
1387 c = &sc->aue_cdata.aue_tx_chain[idx];
1388
1389 /*
1390 * Copy the mbuf data into a contiguous buffer, leaving two
1391 * bytes at the beginning to hold the frame length.
1392 */
1393 m_copydata(m, 0, m->m_pkthdr.len, c->aue_buf + 2);
1394 c->aue_mbuf = m;
1395
1396 /*
1397 * The ADMtek documentation says that the packet length is
1398 * supposed to be specified in the first two bytes of the
1399 * transfer, however it actually seems to ignore this info
1400 * and base the frame size on the bulk transfer length.
1401 */
1402 c->aue_buf[0] = (uint8_t)m->m_pkthdr.len;
1403 c->aue_buf[1] = (uint8_t)(m->m_pkthdr.len >> 8);
1404 total_len = m->m_pkthdr.len + 2;
1405
1406 usbd_setup_xfer(c->aue_xfer, c, c->aue_buf, total_len,
1407 USBD_FORCE_SHORT_XFER, AUE_TX_TIMEOUT, aue_txeof);
1408
1409 /* Transmit */
1410 err = usbd_transfer(c->aue_xfer);
1411 if (err != USBD_IN_PROGRESS) {
1412 aprint_error_dev(sc->aue_dev, "aue_send error=%s\n",
1413 usbd_errstr(err));
1414 /* Stop the interface from process context. */
1415 usb_add_task(sc->aue_udev, &sc->aue_stop_task,
1416 USB_TASKQ_DRIVER);
1417 return EIO;
1418 }
1419 DPRINTFN(5, "%d: send %d bytes",
1420 device_unit(sc->aue_dev), total_len, 0, 0);
1421
1422 sc->aue_cdata.aue_tx_cnt++;
1423
1424 return 0;
1425 }
1426
1427 Static void
1428 aue_start(struct ifnet *ifp)
1429 {
1430 struct aue_softc *sc = ifp->if_softc;
1431 struct mbuf *m_head = NULL;
1432
1433 AUEHIST_FUNC();
1434 AUEHIST_CALLARGSN(5, "%d: enter link=%d",
1435 device_unit(sc->aue_dev), sc->aue_link, 0, 0);
1436
1437 if (sc->aue_dying)
1438 return;
1439
1440 if (!sc->aue_link)
1441 return;
1442
1443 if (ifp->if_flags & IFF_OACTIVE)
1444 return;
1445
1446 IFQ_POLL(&ifp->if_snd, m_head);
1447 if (m_head == NULL)
1448 return;
1449
1450 if (aue_send(sc, m_head, 0)) {
1451 ifp->if_flags |= IFF_OACTIVE;
1452 return;
1453 }
1454
1455 IFQ_DEQUEUE(&ifp->if_snd, m_head);
1456
1457 /*
1458 * If there's a BPF listener, bounce a copy of this frame
1459 * to him.
1460 */
1461 bpf_mtap(ifp, m_head, BPF_D_OUT);
1462
1463 ifp->if_flags |= IFF_OACTIVE;
1464
1465 /*
1466 * Set a timeout in case the chip goes out to lunch.
1467 */
1468 ifp->if_timer = 5;
1469 }
1470
1471 Static void
1472 aue_init(void *xsc)
1473 {
1474 struct aue_softc *sc = xsc;
1475 struct ifnet *ifp = GET_IFP(sc);
1476 struct mii_data *mii = GET_MII(sc);
1477 int i, s;
1478 const u_char *eaddr;
1479
1480 AUEHIST_FUNC();
1481 AUEHIST_CALLARGSN(5, "%d: enter",
1482 device_unit(sc->aue_dev), 0, 0, 0);
1483
1484 if (sc->aue_dying)
1485 return;
1486
1487 if (ifp->if_flags & IFF_RUNNING)
1488 return;
1489
1490 s = splnet();
1491
1492 /*
1493 * Cancel pending I/O and free all RX/TX buffers.
1494 */
1495 aue_reset(sc);
1496
1497 eaddr = CLLADDR(ifp->if_sadl);
1498 for (i = 0; i < ETHER_ADDR_LEN; i++)
1499 aue_csr_write_1(sc, AUE_PAR0 + i, eaddr[i]);
1500
1501 /* If we want promiscuous mode, set the allframes bit. */
1502 if (ifp->if_flags & IFF_PROMISC)
1503 AUE_SETBIT(sc, AUE_CTL2, AUE_CTL2_RX_PROMISC);
1504 else
1505 AUE_CLRBIT(sc, AUE_CTL2, AUE_CTL2_RX_PROMISC);
1506
1507 if (sc->aue_ep[AUE_ENDPT_RX] == NULL) {
1508 if (aue_openpipes(sc)) {
1509 splx(s);
1510 return;
1511 }
1512 }
1513 /* Init TX ring. */
1514 if (aue_tx_list_init(sc)) {
1515 aprint_error_dev(sc->aue_dev, "tx list init failed\n");
1516 splx(s);
1517 return;
1518 }
1519
1520 /* Init RX ring. */
1521 if (aue_rx_list_init(sc)) {
1522 aprint_error_dev(sc->aue_dev, "rx list init failed\n");
1523 splx(s);
1524 return;
1525 }
1526
1527 /* Start up the receive pipe. */
1528 for (i = 0; i < AUE_RX_LIST_CNT; i++) {
1529 struct aue_chain *c = &sc->aue_cdata.aue_rx_chain[i];
1530
1531 usbd_setup_xfer(c->aue_xfer, c, c->aue_buf, AUE_BUFSZ,
1532 USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, aue_rxeof);
1533 (void)usbd_transfer(c->aue_xfer); /* XXX */
1534 DPRINTFN(5, "%d: start read",
1535 device_unit(sc->aue_dev), 0, 0, 0);
1536
1537 }
1538
1539 /* Load the multicast filter. */
1540 aue_setmulti(sc);
1541
1542 /* Enable RX and TX */
1543 aue_csr_write_1(sc, AUE_CTL0, AUE_CTL0_RXSTAT_APPEND | AUE_CTL0_RX_ENB);
1544 AUE_SETBIT(sc, AUE_CTL0, AUE_CTL0_TX_ENB);
1545 AUE_SETBIT(sc, AUE_CTL2, AUE_CTL2_EP3_CLR);
1546
1547 mii_mediachg(mii);
1548
1549 ifp->if_flags |= IFF_RUNNING;
1550 ifp->if_flags &= ~IFF_OACTIVE;
1551
1552 splx(s);
1553
1554 callout_reset(&(sc->aue_stat_ch), (hz), (aue_tick), (sc));
1555 }
1556
1557 Static int
1558 aue_openpipes(struct aue_softc *sc)
1559 {
1560 usbd_status err;
1561
1562 /* Open RX and TX pipes. */
1563 err = usbd_open_pipe(sc->aue_iface, sc->aue_ed[AUE_ENDPT_RX],
1564 USBD_EXCLUSIVE_USE, &sc->aue_ep[AUE_ENDPT_RX]);
1565 if (err) {
1566 aprint_error_dev(sc->aue_dev, "open rx pipe failed: %s\n",
1567 usbd_errstr(err));
1568 return EIO;
1569 }
1570 err = usbd_open_pipe(sc->aue_iface, sc->aue_ed[AUE_ENDPT_TX],
1571 USBD_EXCLUSIVE_USE, &sc->aue_ep[AUE_ENDPT_TX]);
1572 if (err) {
1573 aprint_error_dev(sc->aue_dev, "open tx pipe failed: %s\n",
1574 usbd_errstr(err));
1575 return EIO;
1576 }
1577 err = usbd_open_pipe_intr(sc->aue_iface, sc->aue_ed[AUE_ENDPT_INTR],
1578 USBD_EXCLUSIVE_USE, &sc->aue_ep[AUE_ENDPT_INTR], sc,
1579 &sc->aue_cdata.aue_ibuf, AUE_INTR_PKTLEN, aue_intr,
1580 AUE_INTR_INTERVAL);
1581 if (err) {
1582 aprint_error_dev(sc->aue_dev, "open intr pipe failed: %s\n",
1583 usbd_errstr(err));
1584 return EIO;
1585 }
1586
1587 return 0;
1588 }
1589
1590 /*
1591 * Set media options.
1592 */
1593 Static int
1594 aue_ifmedia_upd(struct ifnet *ifp)
1595 {
1596 struct aue_softc *sc = ifp->if_softc;
1597
1598 AUEHIST_FUNC();
1599 AUEHIST_CALLARGSN(5, "%d: enter",
1600 device_unit(sc->aue_dev), 0, 0, 0);
1601
1602 if (sc->aue_dying)
1603 return 0;
1604
1605 return ether_mediachange(ifp);
1606 }
1607
1608 Static int
1609 aue_ioctl(struct ifnet *ifp, u_long command, void *data)
1610 {
1611 struct aue_softc *sc = ifp->if_softc;
1612 struct ifaddr *ifa = (struct ifaddr *)data;
1613 struct ifreq *ifr = (struct ifreq *)data;
1614 int s, error = 0;
1615
1616 if (sc->aue_dying)
1617 return EIO;
1618
1619 s = splnet();
1620
1621 switch (command) {
1622 case SIOCINITIFADDR:
1623 ifp->if_flags |= IFF_UP;
1624 aue_init(sc);
1625
1626 switch (ifa->ifa_addr->sa_family) {
1627 #ifdef INET
1628 case AF_INET:
1629 arp_ifinit(ifp, ifa);
1630 break;
1631 #endif /* INET */
1632 }
1633 break;
1634
1635 case SIOCSIFMTU:
1636 if (ifr->ifr_mtu < ETHERMIN || ifr->ifr_mtu > ETHERMTU)
1637 error = EINVAL;
1638 else if ((error = ifioctl_common(ifp, command, data)) == ENETRESET)
1639 error = 0;
1640 break;
1641
1642 case SIOCSIFFLAGS:
1643 if ((error = ifioctl_common(ifp, command, data)) != 0)
1644 break;
1645 if (ifp->if_flags & IFF_UP) {
1646 if (ifp->if_flags & IFF_RUNNING &&
1647 ifp->if_flags & IFF_PROMISC &&
1648 !(sc->aue_if_flags & IFF_PROMISC)) {
1649 AUE_SETBIT(sc, AUE_CTL2, AUE_CTL2_RX_PROMISC);
1650 } else if (ifp->if_flags & IFF_RUNNING &&
1651 !(ifp->if_flags & IFF_PROMISC) &&
1652 sc->aue_if_flags & IFF_PROMISC) {
1653 AUE_CLRBIT(sc, AUE_CTL2, AUE_CTL2_RX_PROMISC);
1654 } else if (!(ifp->if_flags & IFF_RUNNING))
1655 aue_init(sc);
1656 } else {
1657 if (ifp->if_flags & IFF_RUNNING)
1658 aue_stop(sc);
1659 }
1660 sc->aue_if_flags = ifp->if_flags;
1661 error = 0;
1662 break;
1663 default:
1664 if ((error = ether_ioctl(ifp, command, data)) == ENETRESET) {
1665 if (ifp->if_flags & IFF_RUNNING) {
1666 cv_signal(&sc->aue_domc);
1667 }
1668 error = 0;
1669 }
1670 break;
1671 }
1672
1673 splx(s);
1674
1675 return error;
1676 }
1677
1678 Static void
1679 aue_watchdog(struct ifnet *ifp)
1680 {
1681 struct aue_softc *sc = ifp->if_softc;
1682 struct aue_chain *c;
1683 usbd_status stat;
1684 int s;
1685
1686 AUEHIST_FUNC();
1687 AUEHIST_CALLARGSN(5, "%d: enter",
1688 device_unit(sc->aue_dev), 0, 0, 0);
1689
1690 ifp->if_oerrors++;
1691 aprint_error_dev(sc->aue_dev, "watchdog timeout\n");
1692
1693 s = splusb();
1694 c = &sc->aue_cdata.aue_tx_chain[0];
1695 usbd_get_xfer_status(c->aue_xfer, NULL, NULL, NULL, &stat);
1696 aue_txeof(c->aue_xfer, c, stat);
1697
1698 if (IFQ_IS_EMPTY(&ifp->if_snd) == 0)
1699 aue_start(ifp);
1700 splx(s);
1701 }
1702
1703 /*
1704 * Stop the adapter and free any mbufs allocated to the
1705 * RX and TX lists.
1706 */
1707 Static void
1708 aue_stop(struct aue_softc *sc)
1709 {
1710 usbd_status err;
1711 struct ifnet *ifp;
1712 int i;
1713
1714 AUEHIST_FUNC();
1715 AUEHIST_CALLARGSN(5, "%d: enter",
1716 device_unit(sc->aue_dev), 0, 0, 0);
1717
1718 ifp = GET_IFP(sc);
1719 ifp->if_timer = 0;
1720
1721 aue_csr_write_1(sc, AUE_CTL0, 0);
1722 aue_csr_write_1(sc, AUE_CTL1, 0);
1723 aue_reset(sc);
1724 callout_stop(&sc->aue_stat_ch);
1725
1726 /* Stop transfers. */
1727 if (sc->aue_ep[AUE_ENDPT_RX] != NULL) {
1728 err = usbd_abort_pipe(sc->aue_ep[AUE_ENDPT_RX]);
1729 if (err) {
1730 printf("%s: abort rx pipe failed: %s\n",
1731 device_xname(sc->aue_dev), usbd_errstr(err));
1732 }
1733 err = usbd_close_pipe(sc->aue_ep[AUE_ENDPT_RX]);
1734 if (err) {
1735 printf("%s: close rx pipe failed: %s\n",
1736 device_xname(sc->aue_dev), usbd_errstr(err));
1737 }
1738 sc->aue_ep[AUE_ENDPT_RX] = NULL;
1739 }
1740
1741 if (sc->aue_ep[AUE_ENDPT_TX] != NULL) {
1742 err = usbd_abort_pipe(sc->aue_ep[AUE_ENDPT_TX]);
1743 if (err) {
1744 printf("%s: abort tx pipe failed: %s\n",
1745 device_xname(sc->aue_dev), usbd_errstr(err));
1746 }
1747 }
1748
1749 if (sc->aue_ep[AUE_ENDPT_INTR] != NULL) {
1750 err = usbd_abort_pipe(sc->aue_ep[AUE_ENDPT_INTR]);
1751 if (err) {
1752 printf("%s: abort intr pipe failed: %s\n",
1753 device_xname(sc->aue_dev), usbd_errstr(err));
1754 }
1755 }
1756
1757 /* Free RX resources. */
1758 for (i = 0; i < AUE_RX_LIST_CNT; i++) {
1759 if (sc->aue_cdata.aue_rx_chain[i].aue_mbuf != NULL) {
1760 m_freem(sc->aue_cdata.aue_rx_chain[i].aue_mbuf);
1761 sc->aue_cdata.aue_rx_chain[i].aue_mbuf = NULL;
1762 }
1763 if (sc->aue_cdata.aue_rx_chain[i].aue_xfer != NULL) {
1764 usbd_destroy_xfer(sc->aue_cdata.aue_rx_chain[i].aue_xfer);
1765 sc->aue_cdata.aue_rx_chain[i].aue_xfer = NULL;
1766 }
1767 }
1768
1769 /* Free TX resources. */
1770 for (i = 0; i < AUE_TX_LIST_CNT; i++) {
1771 if (sc->aue_cdata.aue_tx_chain[i].aue_mbuf != NULL) {
1772 m_freem(sc->aue_cdata.aue_tx_chain[i].aue_mbuf);
1773 sc->aue_cdata.aue_tx_chain[i].aue_mbuf = NULL;
1774 }
1775 if (sc->aue_cdata.aue_tx_chain[i].aue_xfer != NULL) {
1776 usbd_destroy_xfer(sc->aue_cdata.aue_tx_chain[i].aue_xfer);
1777 sc->aue_cdata.aue_tx_chain[i].aue_xfer = NULL;
1778 }
1779 }
1780
1781 /* Close pipes */
1782 if (sc->aue_ep[AUE_ENDPT_TX] != NULL) {
1783 err = usbd_close_pipe(sc->aue_ep[AUE_ENDPT_TX]);
1784 if (err) {
1785 printf("%s: close tx pipe failed: %s\n",
1786 device_xname(sc->aue_dev), usbd_errstr(err));
1787 }
1788 sc->aue_ep[AUE_ENDPT_TX] = NULL;
1789 }
1790
1791 if (sc->aue_ep[AUE_ENDPT_INTR] != NULL) {
1792 err = usbd_close_pipe(sc->aue_ep[AUE_ENDPT_INTR]);
1793 if (err) {
1794 printf("%s: close intr pipe failed: %s\n",
1795 device_xname(sc->aue_dev), usbd_errstr(err));
1796 }
1797 sc->aue_ep[AUE_ENDPT_INTR] = NULL;
1798 }
1799
1800 sc->aue_link = 0;
1801
1802 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
1803 }
1804
1805 Static void
1806 aue_multithread(void *arg)
1807 {
1808 struct aue_softc *sc;
1809 int s;
1810
1811 sc = (struct aue_softc *)arg;
1812
1813 while (1) {
1814 mutex_enter(&sc->aue_mcmtx);
1815 cv_wait(&sc->aue_domc,&sc->aue_mcmtx);
1816 mutex_exit(&sc->aue_mcmtx);
1817
1818 if (sc->aue_closing)
1819 break;
1820
1821 s = splnet();
1822 aue_init(sc);
1823 /* XXX called by aue_init, but rc ifconfig hangs without it: */
1824 aue_setmulti(sc);
1825 splx(s);
1826 }
1827
1828 cv_signal(&sc->aue_closemc);
1829
1830 kthread_exit(0);
1831 }
1832