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