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