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