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