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