if_aue.c revision 1.146 1 /* $NetBSD: if_aue.c,v 1.146 2019/01/22 03:42:28 msaitoh Exp $ */
2
3 /*
4 * Copyright (c) 1997, 1998, 1999, 2000
5 * Bill Paul <wpaul (at) ee.columbia.edu>. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by Bill Paul.
18 * 4. Neither the name of the author nor the names of any co-contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
26 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
32 * THE POSSIBILITY OF SUCH DAMAGE.
33 *
34 * $FreeBSD: src/sys/dev/usb/if_aue.c,v 1.11 2000/01/14 01:36:14 wpaul Exp $
35 */
36
37 /*
38 * ADMtek AN986 Pegasus and AN8511 Pegasus II USB to ethernet driver.
39 * Datasheet is available from http://www.admtek.com.tw.
40 *
41 * Written by Bill Paul <wpaul (at) ee.columbia.edu>
42 * Electrical Engineering Department
43 * Columbia University, New York City
44 */
45
46 /*
47 * The Pegasus chip uses four USB "endpoints" to provide 10/100 ethernet
48 * support: the control endpoint for reading/writing registers, burst
49 * read endpoint for packet reception, burst write for packet transmission
50 * and one for "interrupts." The chip uses the same RX filter scheme
51 * as the other ADMtek ethernet parts: one perfect filter entry for the
52 * the station address and a 64-bit multicast hash table. The chip supports
53 * both MII and HomePNA attachments.
54 *
55 * Since the maximum data transfer speed of USB is supposed to be 12Mbps,
56 * you're never really going to get 100Mbps speeds from this device. I
57 * think the idea is to allow the device to connect to 10 or 100Mbps
58 * networks, not necessarily to provide 100Mbps performance. Also, since
59 * the controller uses an external PHY chip, it's possible that board
60 * designers might simply choose a 10Mbps PHY.
61 *
62 * Registers are accessed using usbd_do_request(). Packet transfers are
63 * done using usbd_transfer() and friends.
64 */
65
66 /*
67 * Ported to NetBSD and somewhat rewritten by Lennart Augustsson.
68 */
69
70 /*
71 * TODO:
72 * better error messages from rxstat
73 * split out if_auevar.h
74 * add thread to avoid register reads from interrupt context
75 * more error checks
76 * investigate short rx problem
77 * proper cleanup on errors
78 */
79
80 #include <sys/cdefs.h>
81 __KERNEL_RCSID(0, "$NetBSD: if_aue.c,v 1.146 2019/01/22 03:42:28 msaitoh Exp $");
82
83 #ifdef _KERNEL_OPT
84 #include "opt_usb.h"
85 #include "opt_inet.h"
86 #endif
87
88 #include <sys/param.h>
89 #include <sys/systm.h>
90 #include <sys/sockio.h>
91 #include <sys/mutex.h>
92 #include <sys/mbuf.h>
93 #include <sys/kernel.h>
94 #include <sys/socket.h>
95 #include <sys/device.h>
96 #include <sys/rndsource.h>
97
98 #include <net/if.h>
99 #include <net/if_arp.h>
100 #include <net/if_dl.h>
101 #include <net/if_media.h>
102
103 #include <net/bpf.h>
104
105 #include <net/if_ether.h>
106 #ifdef INET
107 #include <netinet/in.h>
108 #include <netinet/if_inarp.h>
109 #endif
110
111
112
113 #include <dev/mii/mii.h>
114 #include <dev/mii/miivar.h>
115
116 #include <dev/usb/usb.h>
117 #include <dev/usb/usbdi.h>
118 #include <dev/usb/usbdi_util.h>
119 #include <dev/usb/usbdevs.h>
120
121 #include <sys/condvar.h>
122 #include <sys/kthread.h>
123
124 #include <dev/usb/if_auereg.h>
125
126 #ifdef AUE_DEBUG
127 #define DPRINTF(x) if (auedebug) printf x
128 #define DPRINTFN(n,x) if (auedebug >= (n)) printf x
129 int auedebug = 0;
130 #else
131 #define DPRINTF(x)
132 #define DPRINTFN(n,x)
133 #endif
134
135 /*
136 * Various supported device vendors/products.
137 */
138 struct aue_type {
139 struct usb_devno aue_dev;
140 uint16_t aue_flags;
141 #define LSYS 0x0001 /* use Linksys reset */
142 #define PNA 0x0002 /* has Home PNA */
143 #define PII 0x0004 /* Pegasus II chip */
144 };
145
146 Static const struct aue_type aue_devs[] = {
147 {{ USB_VENDOR_3COM, USB_PRODUCT_3COM_3C460B}, PII },
148 {{ USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_XX1}, PNA|PII },
149 {{ USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_XX2}, PII },
150 {{ USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_UFE1000}, LSYS },
151 {{ USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_XX4}, PNA },
152 {{ USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_XX5}, PNA },
153 {{ USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_XX6}, PII },
154 {{ USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_XX7}, PII },
155 {{ USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_XX8}, PII },
156 {{ USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_XX9}, PNA },
157 {{ USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_XX10}, 0 },
158 {{ USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_DSB650TX_PNA}, 0 },
159 {{ USB_VENDOR_ACCTON, USB_PRODUCT_ACCTON_USB320_EC}, 0 },
160 {{ USB_VENDOR_ACCTON, USB_PRODUCT_ACCTON_SS1001}, PII },
161 {{ USB_VENDOR_ADMTEK, USB_PRODUCT_ADMTEK_PEGASUS}, PNA },
162 {{ USB_VENDOR_ADMTEK, USB_PRODUCT_ADMTEK_PEGASUSII}, PII },
163 {{ USB_VENDOR_ADMTEK, USB_PRODUCT_ADMTEK_PEGASUSII_2}, PII },
164 {{ USB_VENDOR_ADMTEK, USB_PRODUCT_ADMTEK_PEGASUSII_3}, PII },
165 {{ USB_VENDOR_AEI, USB_PRODUCT_AEI_USBTOLAN}, PII },
166 {{ USB_VENDOR_BELKIN, USB_PRODUCT_BELKIN_USB2LAN}, PII },
167 {{ USB_VENDOR_BILLIONTON, USB_PRODUCT_BILLIONTON_USB100}, 0 },
168 {{ USB_VENDOR_BILLIONTON, USB_PRODUCT_BILLIONTON_USBLP100}, PNA },
169 {{ USB_VENDOR_BILLIONTON, USB_PRODUCT_BILLIONTON_USBEL100}, 0 },
170 {{ USB_VENDOR_BILLIONTON, USB_PRODUCT_BILLIONTON_USBE100}, PII },
171 {{ USB_VENDOR_COMPAQ, USB_PRODUCT_COMPAQ_HNE200}, PII },
172 {{ USB_VENDOR_COREGA, USB_PRODUCT_COREGA_FETHER_USB_TX}, 0 },
173 {{ USB_VENDOR_COREGA, USB_PRODUCT_COREGA_FETHER_USB_TXS},PII },
174 {{ USB_VENDOR_DLINK, USB_PRODUCT_DLINK_DSB650TX4}, LSYS|PII },
175 {{ USB_VENDOR_DLINK, USB_PRODUCT_DLINK_DSB650TX1}, LSYS },
176 {{ USB_VENDOR_DLINK, USB_PRODUCT_DLINK_DSB650TX}, LSYS },
177 {{ USB_VENDOR_DLINK, USB_PRODUCT_DLINK_DSB650TX_PNA}, PNA },
178 {{ USB_VENDOR_DLINK, USB_PRODUCT_DLINK_DSB650TX3}, LSYS|PII },
179 {{ USB_VENDOR_DLINK, USB_PRODUCT_DLINK_DSB650TX2}, LSYS|PII },
180 {{ USB_VENDOR_DLINK, USB_PRODUCT_DLINK_DSB650}, 0 },
181 {{ USB_VENDOR_ELECOM, USB_PRODUCT_ELECOM_LDUSBTX0}, 0 },
182 {{ USB_VENDOR_ELECOM, USB_PRODUCT_ELECOM_LDUSBTX1}, LSYS },
183 {{ USB_VENDOR_ELECOM, USB_PRODUCT_ELECOM_LDUSBTX2}, 0 },
184 {{ USB_VENDOR_ELECOM, USB_PRODUCT_ELECOM_LDUSBTX3}, LSYS },
185 {{ USB_VENDOR_ELECOM, USB_PRODUCT_ELECOM_LDUSBLTX}, PII },
186 {{ USB_VENDOR_ELSA, USB_PRODUCT_ELSA_USB2ETHERNET}, 0 },
187 {{ USB_VENDOR_HAWKING, USB_PRODUCT_HAWKING_UF100}, PII },
188 {{ USB_VENDOR_HP, USB_PRODUCT_HP_HN210E}, PII },
189 {{ USB_VENDOR_IODATA, USB_PRODUCT_IODATA_USBETTX}, 0 },
190 {{ USB_VENDOR_IODATA, USB_PRODUCT_IODATA_USBETTXS}, PII },
191 {{ USB_VENDOR_IODATA, USB_PRODUCT_IODATA_ETXUS2}, PII },
192 {{ USB_VENDOR_KINGSTON, USB_PRODUCT_KINGSTON_KNU101TX}, 0 },
193 {{ USB_VENDOR_LINKSYS, USB_PRODUCT_LINKSYS_USB10TX1}, LSYS|PII },
194 {{ USB_VENDOR_LINKSYS, USB_PRODUCT_LINKSYS_USB10T}, LSYS },
195 {{ USB_VENDOR_LINKSYS, USB_PRODUCT_LINKSYS_USB100TX}, LSYS },
196 {{ USB_VENDOR_LINKSYS, USB_PRODUCT_LINKSYS_USB100H1}, LSYS|PNA },
197 {{ USB_VENDOR_LINKSYS, USB_PRODUCT_LINKSYS_USB10TA}, LSYS },
198 {{ USB_VENDOR_LINKSYS, USB_PRODUCT_LINKSYS_USB10TX2}, LSYS|PII },
199 {{ USB_VENDOR_MELCO, USB_PRODUCT_MELCO_LUATX1}, 0 },
200 {{ USB_VENDOR_MELCO, USB_PRODUCT_MELCO_LUATX5}, 0 },
201 {{ USB_VENDOR_MELCO, USB_PRODUCT_MELCO_LUA2TX5}, PII },
202 {{ USB_VENDOR_MICROSOFT, USB_PRODUCT_MICROSOFT_MN110}, PII },
203 {{ USB_VENDOR_NETGEAR, USB_PRODUCT_NETGEAR_FA101}, PII },
204 {{ USB_VENDOR_SIEMENS, USB_PRODUCT_SIEMENS_SPEEDSTREAM}, PII },
205 {{ USB_VENDOR_SMARTBRIDGES, USB_PRODUCT_SMARTBRIDGES_SMARTNIC},PII },
206 {{ USB_VENDOR_SMC, USB_PRODUCT_SMC_2202USB}, 0 },
207 {{ USB_VENDOR_SMC, USB_PRODUCT_SMC_2206USB}, PII },
208 {{ USB_VENDOR_SOHOWARE, USB_PRODUCT_SOHOWARE_NUB100}, 0 },
209 };
210 #define aue_lookup(v, p) ((const struct aue_type *)usb_lookup(aue_devs, v, p))
211
212 int aue_match(device_t, cfdata_t, void *);
213 void aue_attach(device_t, device_t, void *);
214 int aue_detach(device_t, int);
215 int aue_activate(device_t, enum devact);
216 extern struct cfdriver aue_cd;
217 CFATTACH_DECL_NEW(aue, sizeof(struct aue_softc), aue_match, aue_attach,
218 aue_detach, aue_activate);
219
220 Static void aue_multithread(void *);
221
222 Static void aue_reset_pegasus_II(struct aue_softc *);
223 Static int aue_tx_list_init(struct aue_softc *);
224 Static int aue_rx_list_init(struct aue_softc *);
225 Static int aue_newbuf(struct aue_softc *, struct aue_chain *, struct mbuf *);
226 Static int aue_send(struct aue_softc *, struct mbuf *, int);
227 Static void aue_intr(struct usbd_xfer *, void *, usbd_status);
228 Static void aue_rxeof(struct usbd_xfer *, void *, usbd_status);
229 Static void aue_txeof(struct usbd_xfer *, void *, usbd_status);
230 Static void aue_tick(void *);
231 Static void aue_tick_task(void *);
232 Static void aue_start(struct ifnet *);
233 Static int aue_ioctl(struct ifnet *, u_long, void *);
234 Static void aue_init(void *);
235 Static void aue_stop(struct aue_softc *);
236 Static void aue_watchdog(struct ifnet *);
237 Static int aue_openpipes(struct aue_softc *);
238 Static int aue_ifmedia_upd(struct ifnet *);
239
240 Static int aue_eeprom_getword(struct aue_softc *, int);
241 Static void aue_read_mac(struct aue_softc *, u_char *);
242 Static int aue_miibus_readreg(device_t, int, int, uint16_t *);
243 Static int aue_miibus_writereg(device_t, int, int, uint16_t);
244 Static void aue_miibus_statchg(struct ifnet *);
245
246 Static void aue_lock_mii(struct aue_softc *);
247 Static void aue_unlock_mii(struct aue_softc *);
248
249 Static void aue_setmulti(struct aue_softc *);
250 Static uint32_t aue_crc(void *);
251 Static void aue_reset(struct aue_softc *);
252
253 Static int aue_csr_read_1(struct aue_softc *, int);
254 Static int aue_csr_write_1(struct aue_softc *, int, int);
255 Static int aue_csr_read_2(struct aue_softc *, int);
256 Static int aue_csr_write_2(struct aue_softc *, int, int);
257
258 #define AUE_SETBIT(sc, reg, x) \
259 aue_csr_write_1(sc, reg, aue_csr_read_1(sc, reg) | (x))
260
261 #define AUE_CLRBIT(sc, reg, x) \
262 aue_csr_write_1(sc, reg, aue_csr_read_1(sc, reg) & ~(x))
263
264 Static int
265 aue_csr_read_1(struct aue_softc *sc, int reg)
266 {
267 usb_device_request_t req;
268 usbd_status err;
269 uByte val = 0;
270
271 if (sc->aue_dying)
272 return 0;
273
274 req.bmRequestType = UT_READ_VENDOR_DEVICE;
275 req.bRequest = AUE_UR_READREG;
276 USETW(req.wValue, 0);
277 USETW(req.wIndex, reg);
278 USETW(req.wLength, 1);
279
280 err = usbd_do_request(sc->aue_udev, &req, &val);
281
282 if (err) {
283 DPRINTF(("%s: aue_csr_read_1: reg=0x%x err=%s\n",
284 device_xname(sc->aue_dev), reg, usbd_errstr(err)));
285 return 0;
286 }
287
288 return val;
289 }
290
291 Static int
292 aue_csr_read_2(struct aue_softc *sc, int reg)
293 {
294 usb_device_request_t req;
295 usbd_status err;
296 uWord val;
297
298 if (sc->aue_dying)
299 return 0;
300
301 req.bmRequestType = UT_READ_VENDOR_DEVICE;
302 req.bRequest = AUE_UR_READREG;
303 USETW(req.wValue, 0);
304 USETW(req.wIndex, reg);
305 USETW(req.wLength, 2);
306
307 err = usbd_do_request(sc->aue_udev, &req, &val);
308
309 if (err) {
310 DPRINTF(("%s: aue_csr_read_2: reg=0x%x err=%s\n",
311 device_xname(sc->aue_dev), reg, usbd_errstr(err)));
312 return 0;
313 }
314
315 return UGETW(val);
316 }
317
318 Static int
319 aue_csr_write_1(struct aue_softc *sc, int reg, int aval)
320 {
321 usb_device_request_t req;
322 usbd_status err;
323 uByte val;
324
325 if (sc->aue_dying)
326 return 0;
327
328 val = aval;
329 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
330 req.bRequest = AUE_UR_WRITEREG;
331 USETW(req.wValue, val);
332 USETW(req.wIndex, reg);
333 USETW(req.wLength, 1);
334
335 err = usbd_do_request(sc->aue_udev, &req, &val);
336
337 if (err) {
338 DPRINTF(("%s: aue_csr_write_1: reg=0x%x err=%s\n",
339 device_xname(sc->aue_dev), reg, usbd_errstr(err)));
340 return -1;
341 }
342
343 return 0;
344 }
345
346 Static int
347 aue_csr_write_2(struct aue_softc *sc, int reg, int aval)
348 {
349 usb_device_request_t req;
350 usbd_status err;
351 uWord val;
352
353 if (sc->aue_dying)
354 return 0;
355
356 USETW(val, aval);
357 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
358 req.bRequest = AUE_UR_WRITEREG;
359 USETW(req.wValue, aval);
360 USETW(req.wIndex, reg);
361 USETW(req.wLength, 2);
362
363 err = usbd_do_request(sc->aue_udev, &req, &val);
364
365 if (err) {
366 DPRINTF(("%s: aue_csr_write_2: reg=0x%x err=%s\n",
367 device_xname(sc->aue_dev), reg, usbd_errstr(err)));
368 return -1;
369 }
370
371 return 0;
372 }
373
374 /*
375 * Read a word of data stored in the EEPROM at address 'addr.'
376 */
377 Static int
378 aue_eeprom_getword(struct aue_softc *sc, int addr)
379 {
380 int i;
381
382 aue_csr_write_1(sc, AUE_EE_REG, addr);
383 aue_csr_write_1(sc, AUE_EE_CTL, AUE_EECTL_READ);
384
385 for (i = 0; i < AUE_TIMEOUT; i++) {
386 if (aue_csr_read_1(sc, AUE_EE_CTL) & AUE_EECTL_DONE)
387 break;
388 }
389
390 if (i == AUE_TIMEOUT) {
391 printf("%s: EEPROM read timed out\n",
392 device_xname(sc->aue_dev));
393 }
394
395 return aue_csr_read_2(sc, AUE_EE_DATA);
396 }
397
398 /*
399 * Read the MAC from the EEPROM. It's at offset 0.
400 */
401 Static void
402 aue_read_mac(struct aue_softc *sc, u_char *dest)
403 {
404 int i;
405 int off = 0;
406 int word;
407
408 DPRINTFN(5,("%s: %s: enter\n", device_xname(sc->aue_dev), __func__));
409
410 for (i = 0; i < 3; i++) {
411 word = aue_eeprom_getword(sc, off + i);
412 dest[2 * i] = (u_char)word;
413 dest[2 * i + 1] = (u_char)(word >> 8);
414 }
415 }
416
417 /* Get exclusive access to the MII registers */
418 Static void
419 aue_lock_mii(struct aue_softc *sc)
420 {
421 sc->aue_refcnt++;
422 mutex_enter(&sc->aue_mii_lock);
423 }
424
425 Static void
426 aue_unlock_mii(struct aue_softc *sc)
427 {
428 mutex_exit(&sc->aue_mii_lock);
429 if (--sc->aue_refcnt < 0)
430 usb_detach_wakeupold(sc->aue_dev);
431 }
432
433 Static int
434 aue_miibus_readreg(device_t dev, int phy, int reg, uint16_t *val)
435 {
436 struct aue_softc *sc = device_private(dev);
437 int i, rv = 0;
438
439 if (sc->aue_dying) {
440 #ifdef DIAGNOSTIC
441 printf("%s: dying\n", device_xname(sc->aue_dev));
442 #endif
443 return -1;
444 }
445
446 #if 0
447 /*
448 * The Am79C901 HomePNA PHY actually contains
449 * two transceivers: a 1Mbps HomePNA PHY and a
450 * 10Mbps full/half duplex ethernet PHY with
451 * NWAY autoneg. However in the ADMtek adapter,
452 * only the 1Mbps PHY is actually connected to
453 * anything, so we ignore the 10Mbps one. It
454 * happens to be configured for MII address 3,
455 * so we filter that out.
456 */
457 if (sc->aue_vendor == USB_VENDOR_ADMTEK &&
458 sc->aue_product == USB_PRODUCT_ADMTEK_PEGASUS) {
459 if (phy == 3)
460 return -1;
461 }
462 #endif
463
464 aue_lock_mii(sc);
465 aue_csr_write_1(sc, AUE_PHY_ADDR, phy);
466 aue_csr_write_1(sc, AUE_PHY_CTL, reg | AUE_PHYCTL_READ);
467
468 for (i = 0; i < AUE_TIMEOUT; i++) {
469 if (aue_csr_read_1(sc, AUE_PHY_CTL) & AUE_PHYCTL_DONE)
470 break;
471 }
472
473 if (i == AUE_TIMEOUT) {
474 printf("%s: MII read timed out\n", device_xname(sc->aue_dev));
475 rv = ETIMEDOUT;
476 goto out;
477 }
478
479 *val = aue_csr_read_2(sc, AUE_PHY_DATA);
480
481 DPRINTFN(11,("%s: %s: phy=%d reg=%d => 0x%04hx\n",
482 device_xname(sc->aue_dev), __func__, phy, reg, *val));
483
484 out:
485 aue_unlock_mii(sc);
486 return rv;
487 }
488
489 Static int
490 aue_miibus_writereg(device_t dev, int phy, int reg, uint16_t val)
491 {
492 struct aue_softc *sc = device_private(dev);
493 int i, rv = 0;
494
495 #if 0
496 if (sc->aue_vendor == USB_VENDOR_ADMTEK &&
497 sc->aue_product == USB_PRODUCT_ADMTEK_PEGASUS) {
498 if (phy == 3)
499 return -1;
500 }
501 #endif
502
503 DPRINTFN(11,("%s: %s: phy=%d reg=%d data=0x%04hx\n",
504 device_xname(sc->aue_dev), __func__, phy, reg, val));
505
506 aue_lock_mii(sc);
507 aue_csr_write_2(sc, AUE_PHY_DATA, val);
508 aue_csr_write_1(sc, AUE_PHY_ADDR, phy);
509 aue_csr_write_1(sc, AUE_PHY_CTL, reg | AUE_PHYCTL_WRITE);
510
511 for (i = 0; i < AUE_TIMEOUT; i++) {
512 if (aue_csr_read_1(sc, AUE_PHY_CTL) & AUE_PHYCTL_DONE)
513 break;
514 }
515
516 if (i == AUE_TIMEOUT) {
517 printf("%s: MII read timed out\n", device_xname(sc->aue_dev));
518 rv = ETIMEDOUT;
519 }
520 aue_unlock_mii(sc);
521
522 return rv;
523 }
524
525 Static void
526 aue_miibus_statchg(struct ifnet *ifp)
527 {
528 struct aue_softc *sc = ifp->if_softc;
529 struct mii_data *mii = GET_MII(sc);
530
531 DPRINTFN(5,("%s: %s: enter\n", device_xname(sc->aue_dev), __func__));
532
533 aue_lock_mii(sc);
534 AUE_CLRBIT(sc, AUE_CTL0, AUE_CTL0_RX_ENB | AUE_CTL0_TX_ENB);
535
536 if (IFM_SUBTYPE(mii->mii_media_active) == IFM_100_TX) {
537 AUE_SETBIT(sc, AUE_CTL1, AUE_CTL1_SPEEDSEL);
538 } else {
539 AUE_CLRBIT(sc, AUE_CTL1, AUE_CTL1_SPEEDSEL);
540 }
541
542 if ((mii->mii_media_active & IFM_GMASK) == IFM_FDX)
543 AUE_SETBIT(sc, AUE_CTL1, AUE_CTL1_DUPLEX);
544 else
545 AUE_CLRBIT(sc, AUE_CTL1, AUE_CTL1_DUPLEX);
546
547 AUE_SETBIT(sc, AUE_CTL0, AUE_CTL0_RX_ENB | AUE_CTL0_TX_ENB);
548 aue_unlock_mii(sc);
549
550 /*
551 * Set the LED modes on the LinkSys adapter.
552 * This turns on the 'dual link LED' bin in the auxmode
553 * register of the Broadcom PHY.
554 */
555 if (!sc->aue_dying && (sc->aue_flags & LSYS)) {
556 uint16_t auxmode;
557 aue_miibus_readreg(sc->aue_dev, 0, 0x1b, &auxmode);
558 aue_miibus_writereg(sc->aue_dev, 0, 0x1b, auxmode | 0x04);
559 }
560 DPRINTFN(5,("%s: %s: exit\n", device_xname(sc->aue_dev), __func__));
561 }
562
563 #define AUE_POLY 0xEDB88320
564 #define AUE_BITS 6
565
566 Static uint32_t
567 aue_crc(void *addrv)
568 {
569 uint32_t idx, bit, data, crc;
570 char *addr = addrv;
571
572 /* Compute CRC for the address value. */
573 crc = 0xFFFFFFFF; /* initial value */
574
575 for (idx = 0; idx < 6; idx++) {
576 for (data = *addr++, bit = 0; bit < 8; bit++, data >>= 1)
577 crc = (crc >> 1) ^ (((crc ^ data) & 1) ? AUE_POLY : 0);
578 }
579
580 return crc & ((1 << AUE_BITS) - 1);
581 }
582
583 Static void
584 aue_setmulti(struct aue_softc *sc)
585 {
586 struct ifnet *ifp;
587 struct ether_multi *enm;
588 struct ether_multistep step;
589 uint32_t h = 0, i;
590
591 DPRINTFN(5,("%s: %s: enter\n", device_xname(sc->aue_dev), __func__));
592
593 ifp = GET_IFP(sc);
594
595 if (ifp->if_flags & IFF_PROMISC) {
596 allmulti:
597 ifp->if_flags |= IFF_ALLMULTI;
598 AUE_SETBIT(sc, AUE_CTL0, AUE_CTL0_ALLMULTI);
599 return;
600 }
601
602 AUE_CLRBIT(sc, AUE_CTL0, AUE_CTL0_ALLMULTI);
603
604 /* first, zot all the existing hash bits */
605 for (i = 0; i < 8; i++)
606 aue_csr_write_1(sc, AUE_MAR0 + i, 0);
607
608 /* now program new ones */
609 ETHER_FIRST_MULTI(step, &sc->aue_ec, enm);
610 while (enm != NULL) {
611 if (memcmp(enm->enm_addrlo,
612 enm->enm_addrhi, ETHER_ADDR_LEN) != 0)
613 goto allmulti;
614
615 h = aue_crc(enm->enm_addrlo);
616 AUE_SETBIT(sc, AUE_MAR + (h >> 3), 1 << (h & 0x7));
617 ETHER_NEXT_MULTI(step, enm);
618 }
619
620 ifp->if_flags &= ~IFF_ALLMULTI;
621 }
622
623 Static void
624 aue_reset_pegasus_II(struct aue_softc *sc)
625 {
626 /* Magic constants taken from Linux driver. */
627 aue_csr_write_1(sc, AUE_REG_1D, 0);
628 aue_csr_write_1(sc, AUE_REG_7B, 2);
629 #if 0
630 if ((sc->aue_flags & HAS_HOME_PNA) && mii_mode)
631 aue_csr_write_1(sc, AUE_REG_81, 6);
632 else
633 #endif
634 aue_csr_write_1(sc, AUE_REG_81, 2);
635 }
636
637 Static void
638 aue_reset(struct aue_softc *sc)
639 {
640 int i;
641
642 DPRINTFN(2,("%s: %s: enter\n", device_xname(sc->aue_dev), __func__));
643
644 AUE_SETBIT(sc, AUE_CTL1, AUE_CTL1_RESETMAC);
645
646 for (i = 0; i < AUE_TIMEOUT; i++) {
647 if (!(aue_csr_read_1(sc, AUE_CTL1) & AUE_CTL1_RESETMAC))
648 break;
649 }
650
651 if (i == AUE_TIMEOUT)
652 printf("%s: reset failed\n", device_xname(sc->aue_dev));
653
654 #if 0
655 /* XXX what is mii_mode supposed to be */
656 if (sc->aue_mii_mode && (sc->aue_flags & PNA))
657 aue_csr_write_1(sc, AUE_GPIO1, 0x34);
658 else
659 aue_csr_write_1(sc, AUE_GPIO1, 0x26);
660 #endif
661
662 /*
663 * The PHY(s) attached to the Pegasus chip may be held
664 * in reset until we flip on the GPIO outputs. Make sure
665 * to set the GPIO pins high so that the PHY(s) will
666 * be enabled.
667 *
668 * Note: We force all of the GPIO pins low first, *then*
669 * enable the ones we want.
670 */
671 if (sc->aue_flags & LSYS) {
672 /* Grrr. LinkSys has to be different from everyone else. */
673 aue_csr_write_1(sc, AUE_GPIO0,
674 AUE_GPIO_SEL0 | AUE_GPIO_SEL1);
675 } else {
676 aue_csr_write_1(sc, AUE_GPIO0,
677 AUE_GPIO_OUT0 | AUE_GPIO_SEL0);
678 }
679 aue_csr_write_1(sc, AUE_GPIO0,
680 AUE_GPIO_OUT0 | AUE_GPIO_SEL0 | AUE_GPIO_SEL1);
681
682 if (sc->aue_flags & PII)
683 aue_reset_pegasus_II(sc);
684
685 /* Wait a little while for the chip to get its brains in order. */
686 delay(10000); /* XXX */
687 }
688
689 /*
690 * Probe for a Pegasus chip.
691 */
692 int
693 aue_match(device_t parent, cfdata_t match, void *aux)
694 {
695 struct usb_attach_arg *uaa = aux;
696
697 /*
698 * Some manufacturers use the same vendor and product id for
699 * different devices. We need to sanity check the DeviceClass
700 * in this case
701 * Currently known guilty products:
702 * 0x050d/0x0121 Belkin Bluetooth and USB2LAN
703 *
704 * If this turns out to be more common, we could use a quirk
705 * table.
706 */
707 if (uaa->uaa_vendor == USB_VENDOR_BELKIN &&
708 uaa->uaa_product == USB_PRODUCT_BELKIN_USB2LAN) {
709 usb_device_descriptor_t *dd;
710
711 dd = usbd_get_device_descriptor(uaa->uaa_device);
712 if (dd != NULL &&
713 dd->bDeviceClass != UDCLASS_IN_INTERFACE)
714 return UMATCH_NONE;
715 }
716
717 return aue_lookup(uaa->uaa_vendor, uaa->uaa_product) != NULL ?
718 UMATCH_VENDOR_PRODUCT : UMATCH_NONE;
719 }
720
721 /*
722 * Attach the interface. Allocate softc structures, do ifmedia
723 * setup and ethernet/BPF attach.
724 */
725 void
726 aue_attach(device_t parent, device_t self, void *aux)
727 {
728 struct aue_softc *sc = device_private(self);
729 struct usb_attach_arg *uaa = aux;
730 char *devinfop;
731 int s;
732 u_char eaddr[ETHER_ADDR_LEN];
733 struct ifnet *ifp;
734 struct mii_data *mii;
735 struct usbd_device *dev = uaa->uaa_device;
736 struct usbd_interface *iface;
737 usbd_status err;
738 usb_interface_descriptor_t *id;
739 usb_endpoint_descriptor_t *ed;
740 int i;
741
742 DPRINTFN(5,(" : aue_attach: sc=%p", sc));
743
744 sc->aue_dev = self;
745
746 aprint_naive("\n");
747 aprint_normal("\n");
748
749 devinfop = usbd_devinfo_alloc(uaa->uaa_device, 0);
750 aprint_normal_dev(self, "%s\n", devinfop);
751 usbd_devinfo_free(devinfop);
752
753 err = usbd_set_config_no(dev, AUE_CONFIG_NO, 1);
754 if (err) {
755 aprint_error_dev(self, "failed to set configuration"
756 ", err=%s\n", usbd_errstr(err));
757 return;
758 }
759
760 usb_init_task(&sc->aue_tick_task, aue_tick_task, sc, 0);
761 usb_init_task(&sc->aue_stop_task, (void (*)(void *))aue_stop, sc, 0);
762 mutex_init(&sc->aue_mii_lock, MUTEX_DEFAULT, IPL_NONE);
763
764 err = usbd_device2interface_handle(dev, AUE_IFACE_IDX, &iface);
765 if (err) {
766 aprint_error_dev(self, "getting interface handle failed\n");
767 return;
768 }
769 sc->aue_closing = 0;
770
771 mutex_init(&sc->aue_mcmtx, MUTEX_DRIVER, IPL_NET);
772 cv_init(&sc->aue_domc, "auemc");
773 cv_init(&sc->aue_closemc, "auemccl");
774
775 err = kthread_create(PRI_NONE, 0, NULL,
776 aue_multithread, sc, &sc->aue_thread,
777 "%s-mc", device_xname(sc->aue_dev));
778
779 if (err) {
780 aprint_error_dev(self,
781 "creating multicast configuration thread\n");
782 return;
783 }
784 sc->aue_flags = aue_lookup(uaa->uaa_vendor,
785 uaa->uaa_product)->aue_flags;
786
787 sc->aue_udev = dev;
788 sc->aue_iface = iface;
789 sc->aue_product = uaa->uaa_product;
790 sc->aue_vendor = uaa->uaa_vendor;
791
792 id = usbd_get_interface_descriptor(iface);
793
794 /* Find endpoints. */
795 for (i = 0; i < id->bNumEndpoints; i++) {
796 ed = usbd_interface2endpoint_descriptor(iface, i);
797 if (ed == NULL) {
798 aprint_error_dev(self,
799 "couldn't get endpoint descriptor %d\n", i);
800 return;
801 }
802 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
803 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
804 sc->aue_ed[AUE_ENDPT_RX] = ed->bEndpointAddress;
805 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
806 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
807 sc->aue_ed[AUE_ENDPT_TX] = ed->bEndpointAddress;
808 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
809 UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) {
810 sc->aue_ed[AUE_ENDPT_INTR] = ed->bEndpointAddress;
811 }
812 }
813
814 if (sc->aue_ed[AUE_ENDPT_RX] == 0 || sc->aue_ed[AUE_ENDPT_TX] == 0 ||
815 sc->aue_ed[AUE_ENDPT_INTR] == 0) {
816 aprint_error_dev(self, "missing endpoint\n");
817 return;
818 }
819
820
821 s = splnet();
822
823 /* Reset the adapter. */
824 aue_reset(sc);
825
826 /*
827 * Get station address from the EEPROM.
828 */
829 aue_read_mac(sc, eaddr);
830
831 /*
832 * A Pegasus chip was detected. Inform the world.
833 */
834 ifp = GET_IFP(sc);
835 aprint_normal_dev(self, "Ethernet address %s\n", ether_sprintf(eaddr));
836
837 /* Initialize interface info.*/
838 ifp->if_softc = sc;
839 ifp->if_mtu = ETHERMTU;
840 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
841 ifp->if_ioctl = aue_ioctl;
842 ifp->if_start = aue_start;
843 ifp->if_watchdog = aue_watchdog;
844 strlcpy(ifp->if_xname, device_xname(sc->aue_dev), IFNAMSIZ);
845
846 IFQ_SET_READY(&ifp->if_snd);
847
848 /* Initialize MII/media info. */
849 mii = &sc->aue_mii;
850 mii->mii_ifp = ifp;
851 mii->mii_readreg = aue_miibus_readreg;
852 mii->mii_writereg = aue_miibus_writereg;
853 mii->mii_statchg = aue_miibus_statchg;
854 mii->mii_flags = MIIF_AUTOTSLEEP;
855 sc->aue_ec.ec_mii = mii;
856 ifmedia_init(&mii->mii_media, 0, aue_ifmedia_upd, ether_mediastatus);
857 mii_attach(self, mii, 0xffffffff, MII_PHY_ANY, MII_OFFSET_ANY, 0);
858 if (LIST_FIRST(&mii->mii_phys) == NULL) {
859 ifmedia_add(&mii->mii_media, IFM_ETHER | IFM_NONE, 0, NULL);
860 ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_NONE);
861 } else
862 ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_AUTO);
863
864 /* Attach the interface. */
865 if_attach(ifp);
866 ether_ifattach(ifp, eaddr);
867 rnd_attach_source(&sc->rnd_source, device_xname(sc->aue_dev),
868 RND_TYPE_NET, RND_FLAG_DEFAULT);
869
870 callout_init(&(sc->aue_stat_ch), 0);
871
872 sc->aue_attached = 1;
873 splx(s);
874
875 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->aue_udev, sc->aue_dev);
876
877 return;
878 }
879
880 int
881 aue_detach(device_t self, int flags)
882 {
883 struct aue_softc *sc = device_private(self);
884 struct ifnet *ifp = GET_IFP(sc);
885 int s;
886
887 DPRINTFN(2,("%s: %s: enter\n", device_xname(sc->aue_dev), __func__));
888
889 if (!sc->aue_attached) {
890 /* Detached before attached finished, so just bail out. */
891 return 0;
892 }
893
894 /*
895 * XXX Halting callout guarantees no more tick tasks. What
896 * guarantees no more stop tasks? What guarantees no more
897 * calls to aue_send? Don't we need to wait for if_detach or
898 * something? Should we set sc->aue_dying here? Is device
899 * deactivation guaranteed to have already happened?
900 */
901 callout_halt(&sc->aue_stat_ch, NULL);
902 usb_rem_task_wait(sc->aue_udev, &sc->aue_tick_task, USB_TASKQ_DRIVER,
903 NULL);
904 usb_rem_task_wait(sc->aue_udev, &sc->aue_stop_task, USB_TASKQ_DRIVER,
905 NULL);
906
907 sc->aue_closing = 1;
908 cv_signal(&sc->aue_domc);
909
910 mutex_enter(&sc->aue_mcmtx);
911 cv_wait(&sc->aue_closemc,&sc->aue_mcmtx);
912 mutex_exit(&sc->aue_mcmtx);
913
914 mutex_destroy(&sc->aue_mcmtx);
915 cv_destroy(&sc->aue_domc);
916 cv_destroy(&sc->aue_closemc);
917
918 s = splusb();
919
920 if (ifp->if_flags & IFF_RUNNING)
921 aue_stop(sc);
922
923 rnd_detach_source(&sc->rnd_source);
924 mii_detach(&sc->aue_mii, MII_PHY_ANY, MII_OFFSET_ANY);
925 ifmedia_delete_instance(&sc->aue_mii.mii_media, IFM_INST_ANY);
926 ether_ifdetach(ifp);
927
928 if_detach(ifp);
929
930 #ifdef DIAGNOSTIC
931 if (sc->aue_ep[AUE_ENDPT_TX] != NULL ||
932 sc->aue_ep[AUE_ENDPT_RX] != NULL ||
933 sc->aue_ep[AUE_ENDPT_INTR] != NULL)
934 aprint_error_dev(self, "detach has active endpoints\n");
935 #endif
936
937 sc->aue_attached = 0;
938
939 if (--sc->aue_refcnt >= 0) {
940 /* Wait for processes to go away. */
941 usb_detach_waitold(sc->aue_dev);
942 }
943 splx(s);
944
945 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->aue_udev, sc->aue_dev);
946
947 mutex_destroy(&sc->aue_mii_lock);
948 #if 0
949 mutex_destroy(&sc->wkmtx);
950 #endif
951 return 0;
952 }
953
954 int
955 aue_activate(device_t self, enum devact act)
956 {
957 struct aue_softc *sc = device_private(self);
958
959 DPRINTFN(2,("%s: %s: enter\n", device_xname(sc->aue_dev), __func__));
960
961 switch (act) {
962 case DVACT_DEACTIVATE:
963 if_deactivate(&sc->aue_ec.ec_if);
964 sc->aue_dying = 1;
965 return 0;
966 default:
967 return EOPNOTSUPP;
968 }
969 }
970
971 /*
972 * Initialize an RX descriptor and attach an MBUF cluster.
973 */
974 Static int
975 aue_newbuf(struct aue_softc *sc, struct aue_chain *c, struct mbuf *m)
976 {
977 struct mbuf *m_new = NULL;
978
979 DPRINTFN(10,("%s: %s: enter\n", device_xname(sc->aue_dev),__func__));
980
981 if (m == NULL) {
982 MGETHDR(m_new, M_DONTWAIT, MT_DATA);
983 if (m_new == NULL) {
984 aprint_error_dev(sc->aue_dev, "no memory for rx list "
985 "-- packet dropped!\n");
986 return ENOBUFS;
987 }
988
989 MCLGET(m_new, M_DONTWAIT);
990 if (!(m_new->m_flags & M_EXT)) {
991 aprint_error_dev(sc->aue_dev, "no memory for rx "
992 "list -- packet dropped!\n");
993 m_freem(m_new);
994 return ENOBUFS;
995 }
996 m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
997 } else {
998 m_new = m;
999 m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
1000 m_new->m_data = m_new->m_ext.ext_buf;
1001 }
1002
1003 m_adj(m_new, ETHER_ALIGN);
1004 c->aue_mbuf = m_new;
1005
1006 return 0;
1007 }
1008
1009 Static int
1010 aue_rx_list_init(struct aue_softc *sc)
1011 {
1012 struct aue_cdata *cd;
1013 struct aue_chain *c;
1014 int i;
1015
1016 DPRINTFN(5,("%s: %s: enter\n", device_xname(sc->aue_dev), __func__));
1017
1018 cd = &sc->aue_cdata;
1019 for (i = 0; i < AUE_RX_LIST_CNT; i++) {
1020 c = &cd->aue_rx_chain[i];
1021 c->aue_sc = sc;
1022 c->aue_idx = i;
1023 if (aue_newbuf(sc, c, NULL) == ENOBUFS)
1024 return ENOBUFS;
1025 if (c->aue_xfer == NULL) {
1026 int err = usbd_create_xfer(sc->aue_ep[AUE_ENDPT_RX],
1027 AUE_BUFSZ, 0, 0, &c->aue_xfer);
1028 if (err) {
1029 return err;
1030 }
1031 c->aue_buf = usbd_get_buffer(c->aue_xfer);
1032 }
1033 }
1034
1035 return 0;
1036 }
1037
1038 Static int
1039 aue_tx_list_init(struct aue_softc *sc)
1040 {
1041 struct aue_cdata *cd;
1042 struct aue_chain *c;
1043 int i;
1044
1045 DPRINTFN(5,("%s: %s: enter\n", device_xname(sc->aue_dev), __func__));
1046
1047 cd = &sc->aue_cdata;
1048 for (i = 0; i < AUE_TX_LIST_CNT; i++) {
1049 c = &cd->aue_tx_chain[i];
1050 c->aue_sc = sc;
1051 c->aue_idx = i;
1052 c->aue_mbuf = NULL;
1053 if (c->aue_xfer == NULL) {
1054 int err = usbd_create_xfer(sc->aue_ep[AUE_ENDPT_TX],
1055 AUE_BUFSZ, USBD_FORCE_SHORT_XFER, 0, &c->aue_xfer);
1056 if (err) {
1057 return err;
1058 }
1059 c->aue_buf = usbd_get_buffer(c->aue_xfer);
1060 }
1061 }
1062
1063 return 0;
1064 }
1065
1066 Static void
1067 aue_intr(struct usbd_xfer *xfer, void *priv,
1068 usbd_status status)
1069 {
1070 struct aue_softc *sc = priv;
1071 struct ifnet *ifp = GET_IFP(sc);
1072 struct aue_intrpkt *p = &sc->aue_cdata.aue_ibuf;
1073
1074 DPRINTFN(15,("%s: %s: enter\n", device_xname(sc->aue_dev),__func__));
1075
1076 if (sc->aue_dying)
1077 return;
1078
1079 if (!(ifp->if_flags & IFF_RUNNING))
1080 return;
1081
1082 if (status != USBD_NORMAL_COMPLETION) {
1083 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
1084 return;
1085 }
1086 sc->aue_intr_errs++;
1087 if (usbd_ratecheck(&sc->aue_rx_notice)) {
1088 aprint_debug_dev(sc->aue_dev,
1089 "%u usb errors on intr: %s\n", sc->aue_intr_errs,
1090 usbd_errstr(status));
1091 sc->aue_intr_errs = 0;
1092 }
1093 if (status == USBD_STALLED)
1094 usbd_clear_endpoint_stall_async(sc->aue_ep[AUE_ENDPT_RX]);
1095 return;
1096 }
1097
1098 if (p->aue_txstat0)
1099 ifp->if_oerrors++;
1100
1101 if (p->aue_txstat0 & (AUE_TXSTAT0_LATECOLL | AUE_TXSTAT0_EXCESSCOLL))
1102 ifp->if_collisions++;
1103 }
1104
1105 /*
1106 * A frame has been uploaded: pass the resulting mbuf chain up to
1107 * the higher level protocols.
1108 */
1109 Static void
1110 aue_rxeof(struct usbd_xfer *xfer, void *priv, usbd_status status)
1111 {
1112 struct aue_chain *c = priv;
1113 struct aue_softc *sc = c->aue_sc;
1114 struct ifnet *ifp = GET_IFP(sc);
1115 struct mbuf *m;
1116 uint32_t total_len;
1117 struct aue_rxpkt r;
1118 int s;
1119
1120 DPRINTFN(10,("%s: %s: enter\n", device_xname(sc->aue_dev),__func__));
1121
1122 if (sc->aue_dying)
1123 return;
1124
1125 if (!(ifp->if_flags & IFF_RUNNING))
1126 return;
1127
1128 if (status != USBD_NORMAL_COMPLETION) {
1129 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
1130 return;
1131 sc->aue_rx_errs++;
1132 if (usbd_ratecheck(&sc->aue_rx_notice)) {
1133 aprint_error_dev(sc->aue_dev,
1134 "%u usb errors on rx: %s\n", sc->aue_rx_errs,
1135 usbd_errstr(status));
1136 sc->aue_rx_errs = 0;
1137 }
1138 if (status == USBD_STALLED)
1139 usbd_clear_endpoint_stall_async(sc->aue_ep[AUE_ENDPT_RX]);
1140 goto done;
1141 }
1142
1143 usbd_get_xfer_status(xfer, NULL, NULL, &total_len, NULL);
1144
1145 memcpy(mtod(c->aue_mbuf, char *), c->aue_buf, total_len);
1146
1147 if (total_len <= 4 + ETHER_CRC_LEN) {
1148 ifp->if_ierrors++;
1149 goto done;
1150 }
1151
1152 memcpy(&r, c->aue_buf + total_len - 4, sizeof(r));
1153
1154 /* Turn off all the non-error bits in the rx status word. */
1155 r.aue_rxstat &= AUE_RXSTAT_MASK;
1156 if (r.aue_rxstat) {
1157 ifp->if_ierrors++;
1158 goto done;
1159 }
1160
1161 /* No errors; receive the packet. */
1162 m = c->aue_mbuf;
1163 total_len -= ETHER_CRC_LEN + 4;
1164 m->m_pkthdr.len = m->m_len = total_len;
1165
1166 m_set_rcvif(m, ifp);
1167
1168 s = splnet();
1169
1170 /* XXX ugly */
1171 if (aue_newbuf(sc, c, NULL) == ENOBUFS) {
1172 ifp->if_ierrors++;
1173 goto done1;
1174 }
1175
1176 DPRINTFN(10,("%s: %s: deliver %d\n", device_xname(sc->aue_dev),
1177 __func__, m->m_len));
1178 if_percpuq_enqueue(ifp->if_percpuq, m);
1179 done1:
1180 splx(s);
1181
1182 done:
1183
1184 /* Setup new transfer. */
1185 usbd_setup_xfer(xfer, c, c->aue_buf, AUE_BUFSZ,
1186 USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, aue_rxeof);
1187 usbd_transfer(xfer);
1188
1189 DPRINTFN(10,("%s: %s: start rx\n", device_xname(sc->aue_dev),
1190 __func__));
1191 }
1192
1193 /*
1194 * A frame was downloaded to the chip. It's safe for us to clean up
1195 * the list buffers.
1196 */
1197
1198 Static void
1199 aue_txeof(struct usbd_xfer *xfer, void *priv,
1200 usbd_status status)
1201 {
1202 struct aue_chain *c = priv;
1203 struct aue_softc *sc = c->aue_sc;
1204 struct ifnet *ifp = GET_IFP(sc);
1205 int s;
1206
1207 if (sc->aue_dying)
1208 return;
1209
1210 s = splnet();
1211
1212 DPRINTFN(10,("%s: %s: enter status=%d\n", device_xname(sc->aue_dev),
1213 __func__, status));
1214
1215 ifp->if_timer = 0;
1216 ifp->if_flags &= ~IFF_OACTIVE;
1217
1218 if (status != USBD_NORMAL_COMPLETION) {
1219 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
1220 splx(s);
1221 return;
1222 }
1223 ifp->if_oerrors++;
1224 aprint_error_dev(sc->aue_dev, "usb error on tx: %s\n",
1225 usbd_errstr(status));
1226 if (status == USBD_STALLED)
1227 usbd_clear_endpoint_stall_async(sc->aue_ep[AUE_ENDPT_TX]);
1228 splx(s);
1229 return;
1230 }
1231
1232 ifp->if_opackets++;
1233
1234 m_freem(c->aue_mbuf);
1235 c->aue_mbuf = NULL;
1236
1237 if (IFQ_IS_EMPTY(&ifp->if_snd) == 0)
1238 aue_start(ifp);
1239
1240 splx(s);
1241 }
1242
1243 Static void
1244 aue_tick(void *xsc)
1245 {
1246 struct aue_softc *sc = xsc;
1247
1248 DPRINTFN(15,("%s: %s: enter\n", device_xname(sc->aue_dev),__func__));
1249
1250 if (sc == NULL)
1251 return;
1252
1253 if (sc->aue_dying)
1254 return;
1255
1256 /* Perform periodic stuff in process context. */
1257 usb_add_task(sc->aue_udev, &sc->aue_tick_task, USB_TASKQ_DRIVER);
1258 }
1259
1260 Static void
1261 aue_tick_task(void *xsc)
1262 {
1263 struct aue_softc *sc = xsc;
1264 struct ifnet *ifp;
1265 struct mii_data *mii;
1266 int s;
1267
1268 DPRINTFN(15,("%s: %s: enter\n", device_xname(sc->aue_dev),__func__));
1269
1270 if (sc->aue_dying)
1271 return;
1272
1273 ifp = GET_IFP(sc);
1274 mii = GET_MII(sc);
1275 if (mii == NULL)
1276 return;
1277
1278 s = splnet();
1279
1280 mii_tick(mii);
1281 if (!sc->aue_link) {
1282 mii_pollstat(mii); /* XXX FreeBSD has removed this call */
1283 if (mii->mii_media_status & IFM_ACTIVE &&
1284 IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
1285 DPRINTFN(2,("%s: %s: got link\n",
1286 device_xname(sc->aue_dev), __func__));
1287 sc->aue_link++;
1288 if (IFQ_IS_EMPTY(&ifp->if_snd) == 0)
1289 aue_start(ifp);
1290 }
1291 }
1292
1293 callout_reset(&(sc->aue_stat_ch), (hz), (aue_tick), (sc));
1294
1295 splx(s);
1296 }
1297
1298 Static int
1299 aue_send(struct aue_softc *sc, struct mbuf *m, int idx)
1300 {
1301 int total_len;
1302 struct aue_chain *c;
1303 usbd_status err;
1304
1305 DPRINTFN(10,("%s: %s: enter\n", device_xname(sc->aue_dev),__func__));
1306
1307 c = &sc->aue_cdata.aue_tx_chain[idx];
1308
1309 /*
1310 * Copy the mbuf data into a contiguous buffer, leaving two
1311 * bytes at the beginning to hold the frame length.
1312 */
1313 m_copydata(m, 0, m->m_pkthdr.len, c->aue_buf + 2);
1314 c->aue_mbuf = m;
1315
1316 /*
1317 * The ADMtek documentation says that the packet length is
1318 * supposed to be specified in the first two bytes of the
1319 * transfer, however it actually seems to ignore this info
1320 * and base the frame size on the bulk transfer length.
1321 */
1322 c->aue_buf[0] = (uint8_t)m->m_pkthdr.len;
1323 c->aue_buf[1] = (uint8_t)(m->m_pkthdr.len >> 8);
1324 total_len = m->m_pkthdr.len + 2;
1325
1326 usbd_setup_xfer(c->aue_xfer, c, c->aue_buf, total_len,
1327 USBD_FORCE_SHORT_XFER, AUE_TX_TIMEOUT, aue_txeof);
1328
1329 /* Transmit */
1330 err = usbd_transfer(c->aue_xfer);
1331 if (err != USBD_IN_PROGRESS) {
1332 aprint_error_dev(sc->aue_dev, "aue_send error=%s\n",
1333 usbd_errstr(err));
1334 /* Stop the interface from process context. */
1335 usb_add_task(sc->aue_udev, &sc->aue_stop_task,
1336 USB_TASKQ_DRIVER);
1337 return EIO;
1338 }
1339 DPRINTFN(5,("%s: %s: send %d bytes\n", device_xname(sc->aue_dev),
1340 __func__, total_len));
1341
1342 sc->aue_cdata.aue_tx_cnt++;
1343
1344 return 0;
1345 }
1346
1347 Static void
1348 aue_start(struct ifnet *ifp)
1349 {
1350 struct aue_softc *sc = ifp->if_softc;
1351 struct mbuf *m_head = NULL;
1352
1353 DPRINTFN(5,("%s: %s: enter, link=%d\n", device_xname(sc->aue_dev),
1354 __func__, sc->aue_link));
1355
1356 if (sc->aue_dying)
1357 return;
1358
1359 if (!sc->aue_link)
1360 return;
1361
1362 if (ifp->if_flags & IFF_OACTIVE)
1363 return;
1364
1365 IFQ_POLL(&ifp->if_snd, m_head);
1366 if (m_head == NULL)
1367 return;
1368
1369 if (aue_send(sc, m_head, 0)) {
1370 ifp->if_flags |= IFF_OACTIVE;
1371 return;
1372 }
1373
1374 IFQ_DEQUEUE(&ifp->if_snd, m_head);
1375
1376 /*
1377 * If there's a BPF listener, bounce a copy of this frame
1378 * to him.
1379 */
1380 bpf_mtap(ifp, m_head, BPF_D_OUT);
1381
1382 ifp->if_flags |= IFF_OACTIVE;
1383
1384 /*
1385 * Set a timeout in case the chip goes out to lunch.
1386 */
1387 ifp->if_timer = 5;
1388 }
1389
1390 Static void
1391 aue_init(void *xsc)
1392 {
1393 struct aue_softc *sc = xsc;
1394 struct ifnet *ifp = GET_IFP(sc);
1395 struct mii_data *mii = GET_MII(sc);
1396 int i, s;
1397 const u_char *eaddr;
1398
1399 DPRINTFN(5,("%s: %s: enter\n", device_xname(sc->aue_dev), __func__));
1400
1401 if (sc->aue_dying)
1402 return;
1403
1404 if (ifp->if_flags & IFF_RUNNING)
1405 return;
1406
1407 s = splnet();
1408
1409 /*
1410 * Cancel pending I/O and free all RX/TX buffers.
1411 */
1412 aue_reset(sc);
1413
1414 eaddr = CLLADDR(ifp->if_sadl);
1415 for (i = 0; i < ETHER_ADDR_LEN; i++)
1416 aue_csr_write_1(sc, AUE_PAR0 + i, eaddr[i]);
1417
1418 /* If we want promiscuous mode, set the allframes bit. */
1419 if (ifp->if_flags & IFF_PROMISC)
1420 AUE_SETBIT(sc, AUE_CTL2, AUE_CTL2_RX_PROMISC);
1421 else
1422 AUE_CLRBIT(sc, AUE_CTL2, AUE_CTL2_RX_PROMISC);
1423
1424 if (sc->aue_ep[AUE_ENDPT_RX] == NULL) {
1425 if (aue_openpipes(sc)) {
1426 splx(s);
1427 return;
1428 }
1429 }
1430 /* Init TX ring. */
1431 if (aue_tx_list_init(sc)) {
1432 aprint_error_dev(sc->aue_dev, "tx list init failed\n");
1433 splx(s);
1434 return;
1435 }
1436
1437 /* Init RX ring. */
1438 if (aue_rx_list_init(sc)) {
1439 aprint_error_dev(sc->aue_dev, "rx list init failed\n");
1440 splx(s);
1441 return;
1442 }
1443
1444 /* Start up the receive pipe. */
1445 for (i = 0; i < AUE_RX_LIST_CNT; i++) {
1446 struct aue_chain *c = &sc->aue_cdata.aue_rx_chain[i];
1447
1448 usbd_setup_xfer(c->aue_xfer, c, c->aue_buf, AUE_BUFSZ,
1449 USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, aue_rxeof);
1450 (void)usbd_transfer(c->aue_xfer); /* XXX */
1451 DPRINTFN(5,("%s: %s: start read\n", device_xname(sc->aue_dev),
1452 __func__));
1453
1454 }
1455
1456 /* Load the multicast filter. */
1457 aue_setmulti(sc);
1458
1459 /* Enable RX and TX */
1460 aue_csr_write_1(sc, AUE_CTL0, AUE_CTL0_RXSTAT_APPEND | AUE_CTL0_RX_ENB);
1461 AUE_SETBIT(sc, AUE_CTL0, AUE_CTL0_TX_ENB);
1462 AUE_SETBIT(sc, AUE_CTL2, AUE_CTL2_EP3_CLR);
1463
1464 mii_mediachg(mii);
1465
1466 ifp->if_flags |= IFF_RUNNING;
1467 ifp->if_flags &= ~IFF_OACTIVE;
1468
1469 splx(s);
1470
1471 callout_reset(&(sc->aue_stat_ch), (hz), (aue_tick), (sc));
1472 }
1473
1474 Static int
1475 aue_openpipes(struct aue_softc *sc)
1476 {
1477 usbd_status err;
1478
1479 /* Open RX and TX pipes. */
1480 err = usbd_open_pipe(sc->aue_iface, sc->aue_ed[AUE_ENDPT_RX],
1481 USBD_EXCLUSIVE_USE, &sc->aue_ep[AUE_ENDPT_RX]);
1482 if (err) {
1483 aprint_error_dev(sc->aue_dev, "open rx pipe failed: %s\n",
1484 usbd_errstr(err));
1485 return EIO;
1486 }
1487 err = usbd_open_pipe(sc->aue_iface, sc->aue_ed[AUE_ENDPT_TX],
1488 USBD_EXCLUSIVE_USE, &sc->aue_ep[AUE_ENDPT_TX]);
1489 if (err) {
1490 aprint_error_dev(sc->aue_dev, "open tx pipe failed: %s\n",
1491 usbd_errstr(err));
1492 return EIO;
1493 }
1494 err = usbd_open_pipe_intr(sc->aue_iface, sc->aue_ed[AUE_ENDPT_INTR],
1495 USBD_EXCLUSIVE_USE, &sc->aue_ep[AUE_ENDPT_INTR], sc,
1496 &sc->aue_cdata.aue_ibuf, AUE_INTR_PKTLEN, aue_intr,
1497 AUE_INTR_INTERVAL);
1498 if (err) {
1499 aprint_error_dev(sc->aue_dev, "open intr pipe failed: %s\n",
1500 usbd_errstr(err));
1501 return EIO;
1502 }
1503
1504 return 0;
1505 }
1506
1507 /*
1508 * Set media options.
1509 */
1510 Static int
1511 aue_ifmedia_upd(struct ifnet *ifp)
1512 {
1513 struct aue_softc *sc = ifp->if_softc;
1514 struct mii_data *mii = GET_MII(sc);
1515 int rc;
1516
1517 DPRINTFN(5,("%s: %s: enter\n", device_xname(sc->aue_dev), __func__));
1518
1519 if (sc->aue_dying)
1520 return 0;
1521
1522 sc->aue_link = 0;
1523
1524 if ((rc = mii_mediachg(mii)) == ENXIO)
1525 return 0;
1526 return rc;
1527 }
1528
1529 Static int
1530 aue_ioctl(struct ifnet *ifp, u_long command, void *data)
1531 {
1532 struct aue_softc *sc = ifp->if_softc;
1533 struct ifaddr *ifa = (struct ifaddr *)data;
1534 struct ifreq *ifr = (struct ifreq *)data;
1535 int s, error = 0;
1536
1537 if (sc->aue_dying)
1538 return EIO;
1539
1540 s = splnet();
1541
1542 switch(command) {
1543 case SIOCINITIFADDR:
1544 ifp->if_flags |= IFF_UP;
1545 aue_init(sc);
1546
1547 switch (ifa->ifa_addr->sa_family) {
1548 #ifdef INET
1549 case AF_INET:
1550 arp_ifinit(ifp, ifa);
1551 break;
1552 #endif /* INET */
1553 }
1554 break;
1555
1556 case SIOCSIFMTU:
1557 if (ifr->ifr_mtu < ETHERMIN || ifr->ifr_mtu > ETHERMTU)
1558 error = EINVAL;
1559 else if ((error = ifioctl_common(ifp, command, data)) == ENETRESET)
1560 error = 0;
1561 break;
1562
1563 case SIOCSIFFLAGS:
1564 if ((error = ifioctl_common(ifp, command, data)) != 0)
1565 break;
1566 if (ifp->if_flags & IFF_UP) {
1567 if (ifp->if_flags & IFF_RUNNING &&
1568 ifp->if_flags & IFF_PROMISC &&
1569 !(sc->aue_if_flags & IFF_PROMISC)) {
1570 AUE_SETBIT(sc, AUE_CTL2, AUE_CTL2_RX_PROMISC);
1571 } else if (ifp->if_flags & IFF_RUNNING &&
1572 !(ifp->if_flags & IFF_PROMISC) &&
1573 sc->aue_if_flags & IFF_PROMISC) {
1574 AUE_CLRBIT(sc, AUE_CTL2, AUE_CTL2_RX_PROMISC);
1575 } else if (!(ifp->if_flags & IFF_RUNNING))
1576 aue_init(sc);
1577 } else {
1578 if (ifp->if_flags & IFF_RUNNING)
1579 aue_stop(sc);
1580 }
1581 sc->aue_if_flags = ifp->if_flags;
1582 error = 0;
1583 break;
1584 case SIOCADDMULTI:
1585 case SIOCDELMULTI:
1586 case SIOCGIFMEDIA:
1587 case SIOCSIFMEDIA:
1588 if ((error = ether_ioctl(ifp, command, data)) == ENETRESET) {
1589 if (ifp->if_flags & IFF_RUNNING) {
1590 cv_signal(&sc->aue_domc);
1591 }
1592 error = 0;
1593 }
1594 break;
1595 default:
1596 error = ether_ioctl(ifp, command, data);
1597 break;
1598 }
1599
1600 splx(s);
1601
1602 return error;
1603 }
1604
1605 Static void
1606 aue_watchdog(struct ifnet *ifp)
1607 {
1608 struct aue_softc *sc = ifp->if_softc;
1609 struct aue_chain *c;
1610 usbd_status stat;
1611 int s;
1612
1613 DPRINTFN(5,("%s: %s: enter\n", device_xname(sc->aue_dev), __func__));
1614
1615 ifp->if_oerrors++;
1616 aprint_error_dev(sc->aue_dev, "watchdog timeout\n");
1617
1618 s = splusb();
1619 c = &sc->aue_cdata.aue_tx_chain[0];
1620 usbd_get_xfer_status(c->aue_xfer, NULL, NULL, NULL, &stat);
1621 aue_txeof(c->aue_xfer, c, stat);
1622
1623 if (IFQ_IS_EMPTY(&ifp->if_snd) == 0)
1624 aue_start(ifp);
1625 splx(s);
1626 }
1627
1628 /*
1629 * Stop the adapter and free any mbufs allocated to the
1630 * RX and TX lists.
1631 */
1632 Static void
1633 aue_stop(struct aue_softc *sc)
1634 {
1635 usbd_status err;
1636 struct ifnet *ifp;
1637 int i;
1638
1639 DPRINTFN(5,("%s: %s: enter\n", device_xname(sc->aue_dev), __func__));
1640
1641 ifp = GET_IFP(sc);
1642 ifp->if_timer = 0;
1643
1644 aue_csr_write_1(sc, AUE_CTL0, 0);
1645 aue_csr_write_1(sc, AUE_CTL1, 0);
1646 aue_reset(sc);
1647 callout_stop(&sc->aue_stat_ch);
1648
1649 /* Stop transfers. */
1650 if (sc->aue_ep[AUE_ENDPT_RX] != NULL) {
1651 err = usbd_abort_pipe(sc->aue_ep[AUE_ENDPT_RX]);
1652 if (err) {
1653 printf("%s: abort rx pipe failed: %s\n",
1654 device_xname(sc->aue_dev), usbd_errstr(err));
1655 }
1656 err = usbd_close_pipe(sc->aue_ep[AUE_ENDPT_RX]);
1657 if (err) {
1658 printf("%s: close rx pipe failed: %s\n",
1659 device_xname(sc->aue_dev), usbd_errstr(err));
1660 }
1661 sc->aue_ep[AUE_ENDPT_RX] = NULL;
1662 }
1663
1664 if (sc->aue_ep[AUE_ENDPT_TX] != NULL) {
1665 err = usbd_abort_pipe(sc->aue_ep[AUE_ENDPT_TX]);
1666 if (err) {
1667 printf("%s: abort tx pipe failed: %s\n",
1668 device_xname(sc->aue_dev), usbd_errstr(err));
1669 }
1670 }
1671
1672 if (sc->aue_ep[AUE_ENDPT_INTR] != NULL) {
1673 err = usbd_abort_pipe(sc->aue_ep[AUE_ENDPT_INTR]);
1674 if (err) {
1675 printf("%s: abort intr pipe failed: %s\n",
1676 device_xname(sc->aue_dev), usbd_errstr(err));
1677 }
1678 }
1679
1680 /* Free RX resources. */
1681 for (i = 0; i < AUE_RX_LIST_CNT; i++) {
1682 if (sc->aue_cdata.aue_rx_chain[i].aue_mbuf != NULL) {
1683 m_freem(sc->aue_cdata.aue_rx_chain[i].aue_mbuf);
1684 sc->aue_cdata.aue_rx_chain[i].aue_mbuf = NULL;
1685 }
1686 if (sc->aue_cdata.aue_rx_chain[i].aue_xfer != NULL) {
1687 usbd_destroy_xfer(sc->aue_cdata.aue_rx_chain[i].aue_xfer);
1688 sc->aue_cdata.aue_rx_chain[i].aue_xfer = NULL;
1689 }
1690 }
1691
1692 /* Free TX resources. */
1693 for (i = 0; i < AUE_TX_LIST_CNT; i++) {
1694 if (sc->aue_cdata.aue_tx_chain[i].aue_mbuf != NULL) {
1695 m_freem(sc->aue_cdata.aue_tx_chain[i].aue_mbuf);
1696 sc->aue_cdata.aue_tx_chain[i].aue_mbuf = NULL;
1697 }
1698 if (sc->aue_cdata.aue_tx_chain[i].aue_xfer != NULL) {
1699 usbd_destroy_xfer(sc->aue_cdata.aue_tx_chain[i].aue_xfer);
1700 sc->aue_cdata.aue_tx_chain[i].aue_xfer = NULL;
1701 }
1702 }
1703
1704 /* Close pipes */
1705 if (sc->aue_ep[AUE_ENDPT_TX] != NULL) {
1706 err = usbd_close_pipe(sc->aue_ep[AUE_ENDPT_TX]);
1707 if (err) {
1708 printf("%s: close tx pipe failed: %s\n",
1709 device_xname(sc->aue_dev), usbd_errstr(err));
1710 }
1711 sc->aue_ep[AUE_ENDPT_TX] = NULL;
1712 }
1713
1714 if (sc->aue_ep[AUE_ENDPT_INTR] != NULL) {
1715 err = usbd_close_pipe(sc->aue_ep[AUE_ENDPT_INTR]);
1716 if (err) {
1717 printf("%s: close intr pipe failed: %s\n",
1718 device_xname(sc->aue_dev), usbd_errstr(err));
1719 }
1720 sc->aue_ep[AUE_ENDPT_INTR] = NULL;
1721 }
1722
1723 sc->aue_link = 0;
1724
1725 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
1726 }
1727
1728 Static void
1729 aue_multithread(void *arg)
1730 {
1731 struct aue_softc *sc;
1732 int s;
1733
1734 sc = (struct aue_softc *)arg;
1735
1736 while (1) {
1737 mutex_enter(&sc->aue_mcmtx);
1738 cv_wait(&sc->aue_domc,&sc->aue_mcmtx);
1739 mutex_exit(&sc->aue_mcmtx);
1740
1741 if (sc->aue_closing)
1742 break;
1743
1744 s = splnet();
1745 aue_init(sc);
1746 /* XXX called by aue_init, but rc ifconfig hangs without it: */
1747 aue_setmulti(sc);
1748 splx(s);
1749 }
1750
1751 cv_signal(&sc->aue_closemc);
1752
1753 kthread_exit(0);
1754 }
1755