if_aue.c revision 1.99.10.5 1 /* $NetBSD: if_aue.c,v 1.99.10.5 2007/06/22 10:44:54 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.5 2007/06/22 10:44:54 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 #ifdef __FreeBSD__ /* callback needs context */
1099 | USBD_CALLBACK_AS_TASK
1100 #endif
1101 ,
1102 USBD_NO_TIMEOUT, aue_rxeof);
1103 usbd_transfer(xfer);
1104
1105 DPRINTFN(10,("%s: %s: start rx\n", USBDEVNAME(sc->aue_dev),
1106 __func__));
1107 }
1108
1109 /*
1110 * A frame was downloaded to the chip. It's safe for us to clean up
1111 * the list buffers.
1112 */
1113
1114 Static void
1115 aue_txeof(usbd_xfer_handle xfer, usbd_private_handle priv,
1116 usbd_status status)
1117 {
1118 struct ue_chain *c = priv;
1119 struct aue_softc *sc = (void *)c->ue_dev;
1120 struct ifnet *ifp = GET_IFP(sc);
1121 int s;
1122
1123 if (sc->aue_dying)
1124 return;
1125
1126 usbd_unmap_buffer(xfer);
1127
1128 s = splnet();
1129
1130 DPRINTFN(10,("%s: %s: enter status=%d\n", USBDEVNAME(sc->aue_dev),
1131 __func__, status));
1132
1133 ifp->if_timer = 0;
1134 ifp->if_flags &= ~IFF_OACTIVE;
1135
1136 if (status != USBD_NORMAL_COMPLETION) {
1137 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
1138 splx(s);
1139 return;
1140 }
1141 ifp->if_oerrors++;
1142 printf("%s: usb error on tx: %s\n", USBDEVNAME(sc->aue_dev),
1143 usbd_errstr(status));
1144 if (status == USBD_STALLED)
1145 usbd_clear_endpoint_stall_async(sc->aue_ep[AUE_ENDPT_TX]);
1146 splx(s);
1147 return;
1148 }
1149
1150 ifp->if_opackets++;
1151
1152 m_freem(c->ue_mbuf);
1153 c->ue_mbuf = NULL;
1154
1155 if (IFQ_IS_EMPTY(&ifp->if_snd) == 0)
1156 aue_start(ifp);
1157
1158 splx(s);
1159 }
1160
1161 Static void
1162 aue_tick(void *xsc)
1163 {
1164 struct aue_softc *sc = xsc;
1165
1166 DPRINTFN(15,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev),__func__));
1167
1168 if (sc == NULL)
1169 return;
1170
1171 if (sc->aue_dying)
1172 return;
1173
1174 /* Perform periodic stuff in process context. */
1175 usb_add_task(sc->aue_udev, &sc->aue_tick_task, USB_TASKQ_DRIVER);
1176 }
1177
1178 Static void
1179 aue_tick_task(void *xsc)
1180 {
1181 struct aue_softc *sc = xsc;
1182 struct ifnet *ifp;
1183 struct mii_data *mii;
1184 int s;
1185
1186 DPRINTFN(15,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev),__func__));
1187
1188 if (sc->aue_dying)
1189 return;
1190
1191 ifp = GET_IFP(sc);
1192 mii = GET_MII(sc);
1193 if (mii == NULL)
1194 return;
1195
1196 s = splnet();
1197
1198 mii_tick(mii);
1199 if (!sc->aue_link) {
1200 mii_pollstat(mii); /* XXX FreeBSD has removed this call */
1201 if (mii->mii_media_status & IFM_ACTIVE &&
1202 IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
1203 DPRINTFN(2,("%s: %s: got link\n",
1204 USBDEVNAME(sc->aue_dev),__func__));
1205 sc->aue_link++;
1206 if (IFQ_IS_EMPTY(&ifp->if_snd) == 0)
1207 aue_start(ifp);
1208 }
1209 }
1210
1211 usb_callout(sc->aue_stat_ch, hz, aue_tick, sc);
1212
1213 splx(s);
1214 }
1215
1216 Static int
1217 aue_send(struct aue_softc *sc, struct mbuf *m, int idx)
1218 {
1219 int total_len;
1220 struct ue_chain *c;
1221 usbd_status err;
1222 int ret;
1223
1224 DPRINTFN(10,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev),__func__));
1225
1226 c = &sc->aue_cdata.aue_tx_chain[idx];
1227
1228 /* Prepend two bytes at the beginning to hold the frame length. */
1229 M_PREPEND(m, 2, M_DONTWAIT);
1230 if (m != NULL)
1231 m = m_pullup(m, 2); /* just in case */
1232 if (m == NULL) {
1233 GET_IFP(sc)->if_oerrors++;
1234 return (ENOBUFS);
1235 }
1236
1237 /*
1238 * The ADMtek documentation says that the packet length is
1239 * supposed to be specified in the first two bytes of the
1240 * transfer, however it actually seems to ignore this info
1241 * and base the frame size on the bulk transfer length.
1242 */
1243 USETW(mtod(m, char *), m->m_pkthdr.len - 2);
1244 total_len = m->m_pkthdr.len;
1245
1246 ret = usb_ether_map_tx_buffer_mbuf(c, m);
1247 if (ret) {
1248 m_freem(m);
1249 return (ret);
1250 }
1251
1252 usbd_setup_xfer(c->ue_xfer, sc->aue_ep[AUE_ENDPT_TX],
1253 c, NULL /* XXX buf */, total_len, USBD_FORCE_SHORT_XFER | USBD_NO_COPY
1254 #ifdef __FreeBSD__ /* callback needs context */
1255 | USBD_CALLBACK_AS_TASK
1256 #endif
1257 ,
1258 AUE_TX_TIMEOUT, aue_txeof);
1259
1260 /* Transmit */
1261 err = usbd_transfer(c->ue_xfer);
1262 if (err != USBD_IN_PROGRESS) {
1263 c->ue_mbuf = NULL;
1264 m_freem(m);
1265 printf("%s: aue_send error=%s\n", USBDEVNAME(sc->aue_dev),
1266 usbd_errstr(err));
1267 /* Stop the interface from process context. */
1268 usb_add_task(sc->aue_udev, &sc->aue_stop_task,
1269 USB_TASKQ_DRIVER);
1270 return (EIO);
1271 }
1272 DPRINTFN(5,("%s: %s: send %d bytes\n", USBDEVNAME(sc->aue_dev),
1273 __func__, total_len));
1274
1275 sc->aue_cdata.aue_tx_cnt++;
1276
1277 return (0);
1278 }
1279
1280 Static void
1281 aue_start(struct ifnet *ifp)
1282 {
1283 struct aue_softc *sc = ifp->if_softc;
1284 struct mbuf *m_head = NULL;
1285
1286 DPRINTFN(5,("%s: %s: enter, link=%d\n", USBDEVNAME(sc->aue_dev),
1287 __func__, sc->aue_link));
1288
1289 if (sc->aue_dying)
1290 return;
1291
1292 if (!sc->aue_link)
1293 return;
1294
1295 if (ifp->if_flags & IFF_OACTIVE)
1296 return;
1297
1298 IFQ_POLL(&ifp->if_snd, m_head);
1299 if (m_head == NULL)
1300 return;
1301
1302 IFQ_DEQUEUE(&ifp->if_snd, m_head);
1303
1304 #if NBPFILTER > 0
1305 /*
1306 * If there's a BPF listener, bounce a copy of this frame
1307 * to him.
1308 */
1309 if (ifp->if_bpf)
1310 BPF_MTAP(ifp, m_head);
1311 #endif
1312
1313 if (aue_send(sc, m_head, 0)) {
1314 ifp->if_flags |= IFF_OACTIVE;
1315 return;
1316 }
1317
1318 ifp->if_flags |= IFF_OACTIVE;
1319
1320 /*
1321 * Set a timeout in case the chip goes out to lunch.
1322 */
1323 ifp->if_timer = 5;
1324 }
1325
1326 Static int
1327 aue_init(struct ifnet *ifp)
1328 {
1329 struct aue_softc *sc = ifp->if_softc;
1330 struct mii_data *mii = GET_MII(sc);
1331 int i, s;
1332 u_char *eaddr;
1333 struct ue_chain *c;
1334
1335 DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __func__));
1336
1337 if (sc->aue_dying)
1338 return (EIO);
1339
1340 if (ifp->if_flags & IFF_RUNNING)
1341 return (EIO);
1342
1343 s = splnet();
1344
1345 /*
1346 * Cancel pending I/O and free all RX/TX buffers.
1347 */
1348 aue_reset(sc);
1349
1350 #if defined(__OpenBSD__)
1351 eaddr = sc->arpcom.ac_enaddr;
1352 #elif defined(__NetBSD__)
1353 eaddr = LLADDR(ifp->if_sadl);
1354 #endif /* defined(__NetBSD__) */
1355 for (i = 0; i < ETHER_ADDR_LEN; i++)
1356 aue_csr_write_1(sc, AUE_PAR0 + i, eaddr[i]);
1357
1358 /* If we want promiscuous mode, set the allframes bit. */
1359 if (ifp->if_flags & IFF_PROMISC)
1360 AUE_SETBIT(sc, AUE_CTL2, AUE_CTL2_RX_PROMISC);
1361 else
1362 AUE_CLRBIT(sc, AUE_CTL2, AUE_CTL2_RX_PROMISC);
1363
1364 /* Load the multicast filter. */
1365 aue_setmulti(sc);
1366
1367 /* Enable RX and TX */
1368 aue_csr_write_1(sc, AUE_CTL0, AUE_CTL0_RXSTAT_APPEND | AUE_CTL0_RX_ENB);
1369 AUE_SETBIT(sc, AUE_CTL0, AUE_CTL0_TX_ENB);
1370 AUE_SETBIT(sc, AUE_CTL2, AUE_CTL2_EP3_CLR);
1371
1372 mii_mediachg(mii);
1373
1374 if (sc->aue_ep[AUE_ENDPT_RX] == NULL) {
1375 if (aue_openpipes(sc)) {
1376 splx(s);
1377 return (EIO);
1378 }
1379 }
1380
1381 /* Init TX ring. */
1382 if ((i = usb_ether_tx_list_init(USBDEV(sc->aue_dev),
1383 sc->aue_cdata.aue_tx_chain, AUE_TX_LIST_CNT,
1384 sc->aue_udev, sc->aue_ep[AUE_ENDPT_TX], NULL))) {
1385 printf("%s: tx list init failed\n", USBDEVNAME(sc->aue_dev));
1386 splx(s);
1387 return (i);
1388 }
1389
1390 /* Init RX ring. */
1391 if ((i = usb_ether_rx_list_init(USBDEV(sc->aue_dev),
1392 sc->aue_cdata.aue_rx_chain, AUE_RX_LIST_CNT,
1393 sc->aue_udev, sc->aue_ep[AUE_ENDPT_RX]))) {
1394 printf("%s: rx list init failed\n", USBDEVNAME(sc->aue_dev));
1395 splx(s);
1396 return (i);
1397 }
1398
1399 /* Start up the receive pipe. */
1400 for (i = 0; i < AUE_RX_LIST_CNT; i++) {
1401 c = &sc->aue_cdata.aue_rx_chain[i];
1402 (void)usbd_map_buffer_mbuf(c->ue_xfer, c->ue_mbuf);
1403 usbd_setup_xfer(c->ue_xfer, sc->aue_ep[AUE_ENDPT_RX],
1404 c, NULL /* XXX buf */, AUE_BUFSZ,
1405 USBD_SHORT_XFER_OK | USBD_NO_COPY
1406 #ifdef __FreeBSD__ /* callback needs context */
1407 | USBD_CALLBACK_AS_TASK
1408 #endif
1409 , USBD_NO_TIMEOUT,
1410 aue_rxeof);
1411 (void)usbd_transfer(c->ue_xfer); /* XXX */
1412 DPRINTFN(5,("%s: %s: start read\n", USBDEVNAME(sc->aue_dev),
1413 __func__));
1414
1415 }
1416
1417 ifp->if_flags |= IFF_RUNNING;
1418 ifp->if_flags &= ~IFF_OACTIVE;
1419
1420 splx(s);
1421
1422 usb_callout(sc->aue_stat_ch, hz, aue_tick, sc);
1423
1424 return (0);
1425 }
1426
1427 Static int
1428 aue_openpipes(struct aue_softc *sc)
1429 {
1430 usbd_status err;
1431
1432 /* Open RX and TX pipes. */
1433 err = usbd_open_pipe(sc->aue_iface, sc->aue_ed[AUE_ENDPT_RX],
1434 USBD_EXCLUSIVE_USE, &sc->aue_ep[AUE_ENDPT_RX]);
1435 if (err) {
1436 printf("%s: open rx pipe failed: %s\n",
1437 USBDEVNAME(sc->aue_dev), usbd_errstr(err));
1438 return (EIO);
1439 }
1440 err = usbd_open_pipe(sc->aue_iface, sc->aue_ed[AUE_ENDPT_TX],
1441 USBD_EXCLUSIVE_USE, &sc->aue_ep[AUE_ENDPT_TX]);
1442 if (err) {
1443 printf("%s: open tx pipe failed: %s\n",
1444 USBDEVNAME(sc->aue_dev), usbd_errstr(err));
1445 return (EIO);
1446 }
1447 err = usbd_open_pipe_intr(sc->aue_iface, sc->aue_ed[AUE_ENDPT_INTR],
1448 USBD_EXCLUSIVE_USE, &sc->aue_ep[AUE_ENDPT_INTR], sc,
1449 &sc->aue_cdata.aue_ibuf, AUE_INTR_PKTLEN, aue_intr,
1450 AUE_INTR_INTERVAL);
1451 if (err) {
1452 printf("%s: open intr pipe failed: %s\n",
1453 USBDEVNAME(sc->aue_dev), usbd_errstr(err));
1454 return (EIO);
1455 }
1456
1457 return (0);
1458 }
1459
1460 /*
1461 * Set media options.
1462 */
1463 Static int
1464 aue_ifmedia_upd(struct ifnet *ifp)
1465 {
1466 struct aue_softc *sc = ifp->if_softc;
1467 struct mii_data *mii = GET_MII(sc);
1468
1469 DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __func__));
1470
1471 if (sc->aue_dying)
1472 return (0);
1473
1474 sc->aue_link = 0;
1475 if (mii->mii_instance) {
1476 struct mii_softc *miisc;
1477 for (miisc = LIST_FIRST(&mii->mii_phys); miisc != NULL;
1478 miisc = LIST_NEXT(miisc, mii_list))
1479 mii_phy_reset(miisc);
1480 }
1481 mii_mediachg(mii);
1482
1483 return (0);
1484 }
1485
1486 /*
1487 * Report current media status.
1488 */
1489 Static void
1490 aue_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
1491 {
1492 struct aue_softc *sc = ifp->if_softc;
1493 struct mii_data *mii = GET_MII(sc);
1494
1495 DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __func__));
1496
1497 mii_pollstat(mii);
1498 ifmr->ifm_active = mii->mii_media_active;
1499 ifmr->ifm_status = mii->mii_media_status;
1500 }
1501
1502 Static int
1503 aue_ioctl(struct ifnet *ifp, u_long command, usb_ioctlarg_t data)
1504 {
1505 struct aue_softc *sc = ifp->if_softc;
1506 #if 0
1507 struct ifaddr *ifa = (struct ifaddr *)data;
1508 #endif
1509 struct ifreq *ifr = (struct ifreq *)data;
1510 struct mii_data *mii;
1511 int s, error = 0;
1512
1513 if (sc->aue_dying)
1514 return (EIO);
1515
1516 s = splnet();
1517
1518 switch(command) {
1519 #if 0
1520 case SIOCSIFADDR:
1521 ifp->if_flags |= IFF_UP;
1522 aue_init(ifp);
1523
1524 switch (ifa->ifa_addr->sa_family) {
1525 #ifdef INET
1526 case AF_INET:
1527 #if defined(__NetBSD__)
1528 arp_ifinit(ifp, ifa);
1529 #else
1530 arp_ifinit(&sc->arpcom, ifa);
1531 #endif
1532 break;
1533 #endif /* INET */
1534 }
1535 break;
1536
1537 case SIOCSIFMTU:
1538 if (ifr->ifr_mtu > ETHERMTU)
1539 error = EINVAL;
1540 else
1541 ifp->if_mtu = ifr->ifr_mtu;
1542 break;
1543 #endif
1544
1545 case SIOCSIFFLAGS:
1546 if (ifp->if_flags & IFF_UP) {
1547 if (ifp->if_flags & IFF_RUNNING &&
1548 ifp->if_flags & IFF_PROMISC &&
1549 !(sc->aue_if_flags & IFF_PROMISC)) {
1550 AUE_SETBIT(sc, AUE_CTL2, AUE_CTL2_RX_PROMISC);
1551 } else if (ifp->if_flags & IFF_RUNNING &&
1552 !(ifp->if_flags & IFF_PROMISC) &&
1553 sc->aue_if_flags & IFF_PROMISC) {
1554 AUE_CLRBIT(sc, AUE_CTL2, AUE_CTL2_RX_PROMISC);
1555 } else if (!(ifp->if_flags & IFF_RUNNING))
1556 aue_init(ifp);
1557 } else {
1558 if (ifp->if_flags & IFF_RUNNING)
1559 aue_stop(ifp, 1);
1560 }
1561 sc->aue_if_flags = ifp->if_flags;
1562 error = 0;
1563 break;
1564 #if 0
1565 case SIOCADDMULTI:
1566 case SIOCDELMULTI:
1567 error = (command == SIOCADDMULTI) ?
1568 ether_addmulti(ifr, &sc->aue_ec) :
1569 ether_delmulti(ifr, &sc->aue_ec);
1570 if (error == ENETRESET) {
1571 if (ifp->if_flags & IFF_RUNNING) {
1572 #if defined(__NetBSD__)
1573 workqueue_enqueue(sc->wqp,&sc->wk);
1574 /* XXX */
1575 #else
1576 aue_init(ifp);
1577 aue_setmulti(sc);
1578 #endif
1579 }
1580 error = 0;
1581 }
1582 break;
1583 #endif
1584 case SIOCGIFMEDIA:
1585 case SIOCSIFMEDIA:
1586 mii = GET_MII(sc);
1587 error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, command);
1588 break;
1589 default:
1590 error = ether_ioctl(ifp, command, data);
1591 if (error == ENETRESET) {
1592 if (ifp->if_flags & IFF_RUNNING) {
1593 #if defined(__NetBSD__)
1594 workqueue_enqueue(sc->wqp,&sc->wk);
1595 /* XXX */
1596 #else
1597 aue_init(ifp);
1598 aue_setmulti(sc);
1599 #endif
1600 }
1601 error = 0;
1602 }
1603 break;
1604 }
1605
1606 splx(s);
1607
1608 return (error);
1609 }
1610
1611 Static void
1612 aue_watchdog(struct ifnet *ifp)
1613 {
1614 struct aue_softc *sc = ifp->if_softc;
1615 struct ue_chain *c;
1616 usbd_status stat;
1617 int s;
1618
1619 DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __func__));
1620
1621 ifp->if_oerrors++;
1622 printf("%s: watchdog timeout\n", USBDEVNAME(sc->aue_dev));
1623
1624 s = splusb();
1625 c = &sc->aue_cdata.aue_tx_chain[0];
1626 usbd_get_xfer_status(c->ue_xfer, NULL, NULL, NULL, &stat);
1627 aue_txeof(c->ue_xfer, c, stat);
1628
1629 if (IFQ_IS_EMPTY(&ifp->if_snd) == 0)
1630 aue_start(ifp);
1631 splx(s);
1632 }
1633
1634 /*
1635 * Stop the adapter and free any mbufs allocated to the
1636 * RX and TX lists.
1637 */
1638 Static void
1639 aue_stop(struct ifnet *ifp, int disable)
1640 {
1641 struct aue_softc *sc = ifp->if_softc;
1642 usbd_status err;
1643
1644 DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __func__));
1645
1646 ifp = GET_IFP(sc);
1647 ifp->if_timer = 0;
1648
1649 aue_csr_write_1(sc, AUE_CTL0, 0);
1650 aue_csr_write_1(sc, AUE_CTL1, 0);
1651 aue_reset(sc);
1652 usb_uncallout(sc->aue_stat_ch, aue_tick, sc);
1653
1654 /* Stop transfers. */
1655 if (sc->aue_ep[AUE_ENDPT_RX] != NULL) {
1656 err = usbd_abort_pipe(sc->aue_ep[AUE_ENDPT_RX]);
1657 if (err) {
1658 printf("%s: abort rx pipe failed: %s\n",
1659 USBDEVNAME(sc->aue_dev), usbd_errstr(err));
1660 }
1661 }
1662
1663 if (sc->aue_ep[AUE_ENDPT_TX] != NULL) {
1664 err = usbd_abort_pipe(sc->aue_ep[AUE_ENDPT_TX]);
1665 if (err) {
1666 printf("%s: abort tx pipe failed: %s\n",
1667 USBDEVNAME(sc->aue_dev), usbd_errstr(err));
1668 }
1669 }
1670
1671 if (sc->aue_ep[AUE_ENDPT_INTR] != NULL) {
1672 err = usbd_abort_pipe(sc->aue_ep[AUE_ENDPT_INTR]);
1673 if (err) {
1674 printf("%s: abort intr pipe failed: %s\n",
1675 USBDEVNAME(sc->aue_dev), usbd_errstr(err));
1676 }
1677 }
1678
1679 /* Free RX/TX resources. */
1680 usb_ether_rx_list_free(sc->aue_cdata.aue_rx_chain, AUE_RX_LIST_CNT);
1681 usb_ether_tx_list_free(sc->aue_cdata.aue_tx_chain, AUE_TX_LIST_CNT);
1682
1683 /* Close pipes. */
1684 if (sc->aue_ep[AUE_ENDPT_RX] != NULL) {
1685 err = usbd_close_pipe(sc->aue_ep[AUE_ENDPT_RX]);
1686 if (err) {
1687 printf("%s: close rx pipe failed: %s\n",
1688 USBDEVNAME(sc->aue_dev), usbd_errstr(err));
1689 }
1690 sc->aue_ep[AUE_ENDPT_RX] = NULL;
1691 }
1692
1693 if (sc->aue_ep[AUE_ENDPT_TX] != NULL) {
1694 err = usbd_close_pipe(sc->aue_ep[AUE_ENDPT_TX]);
1695 if (err) {
1696 printf("%s: close tx pipe failed: %s\n",
1697 USBDEVNAME(sc->aue_dev), usbd_errstr(err));
1698 }
1699 sc->aue_ep[AUE_ENDPT_TX] = NULL;
1700 }
1701
1702 if (sc->aue_ep[AUE_ENDPT_INTR] != NULL) {
1703 err = usbd_close_pipe(sc->aue_ep[AUE_ENDPT_INTR]);
1704 if (err) {
1705 printf("%s: close intr pipe failed: %s\n",
1706 USBDEVNAME(sc->aue_dev), usbd_errstr(err));
1707 }
1708 sc->aue_ep[AUE_ENDPT_INTR] = NULL;
1709 }
1710
1711 sc->aue_link = 0;
1712
1713 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
1714 }
1715
1716 #if defined(__NetBSD__)
1717 Static void
1718 aue_multiwork(struct work *wkp, void *arg) {
1719 struct aue_softc *sc;
1720
1721 sc = (struct aue_softc *)arg;
1722 (void)wkp;
1723
1724 aue_init(GET_IFP(sc));
1725 /* XXX called by aue_init, but rc ifconfig hangs without it: */
1726 aue_setmulti(sc);
1727 }
1728 #endif
1729