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