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