]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - 6/sys/dev/usb/if_axe.c
Clone Kip's Xen on stable/6 tree so that I can work on improving FreeBSD/amd64
[FreeBSD/FreeBSD.git] / 6 / sys / dev / usb / if_axe.c
1 /*-
2  * Copyright (c) 1997, 1998, 1999, 2000-2003
3  *      Bill Paul <wpaul@windriver.com>.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *      This product includes software developed by Bill Paul.
16  * 4. Neither the name of the author nor the names of any co-contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
24  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
30  * THE POSSIBILITY OF SUCH DAMAGE.
31  */
32
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
35
36 /*
37  * ASIX Electronics AX88172 USB 2.0 ethernet driver. Used in the
38  * LinkSys USB200M and various other adapters.
39  *
40  * Manuals available from:
41  * http://www.asix.com.tw/datasheet/mac/Ax88172.PDF
42  * Note: you need the manual for the AX88170 chip (USB 1.x ethernet
43  * controller) to find the definitions for the RX control register.
44  * http://www.asix.com.tw/datasheet/mac/Ax88170.PDF
45  *
46  * Written by Bill Paul <wpaul@windriver.com>
47  * Senior Engineer
48  * Wind River Systems
49  */
50
51 /*
52  * The AX88172 provides USB ethernet supports at 10 and 100Mbps.
53  * It uses an external PHY (reference designs use a RealTek chip),
54  * and has a 64-bit multicast hash filter. There is some information
55  * missing from the manual which one needs to know in order to make
56  * the chip function:
57  *
58  * - You must set bit 7 in the RX control register, otherwise the
59  *   chip won't receive any packets.
60  * - You must initialize all 3 IPG registers, or you won't be able
61  *   to send any packets.
62  *
63  * Note that this device appears to only support loading the station
64  * address via autload from the EEPROM (i.e. there's no way to manaully
65  * set it).
66  *
67  * (Adam Weinberger wanted me to name this driver if_gir.c.)
68  */
69
70 #include <sys/param.h>
71 #include <sys/systm.h>
72 #include <sys/sockio.h>
73 #include <sys/mbuf.h>
74 #include <sys/malloc.h>
75 #include <sys/kernel.h>
76 #include <sys/module.h>
77 #include <sys/socket.h>
78
79 #include <net/if.h>
80 #include <net/if_arp.h>
81 #include <net/ethernet.h>
82 #include <net/if_dl.h>
83 #include <net/if_media.h>
84 #include <net/if_types.h>
85
86 #include <net/bpf.h>
87
88 #include <sys/bus.h>
89 #include <machine/bus.h>
90
91 #include <dev/usb/usb.h>
92 #include <dev/usb/usbdi.h>
93 #include <dev/usb/usbdi_util.h>
94 #include <dev/usb/usbdivar.h>
95 #include "usbdevs.h"
96 #include <dev/usb/usb_ethersubr.h>
97
98 #include <dev/mii/mii.h>
99 #include <dev/mii/miivar.h>
100
101 /* "controller miibus0" required.  See GENERIC if you get errors here. */
102 #include "miibus_if.h"
103
104 #include <dev/usb/if_axereg.h>
105
106 /*
107  * Various supported device vendors/products.
108  */
109 Static struct axe_type axe_devs[] = {
110         { USB_VENDOR_ASIX, USB_PRODUCT_ASIX_AX88172 },
111         { USB_VENDOR_DLINK, USB_PRODUCT_DLINK_DUBE100 },
112         { USB_VENDOR_JVC, USB_PRODUCT_JVC_MP_PRX1 },
113         { USB_VENDOR_LINKSYS2, USB_PRODUCT_LINKSYS2_USB200M },
114         { USB_VENDOR_MELCO, USB_PRODUCT_MELCO_LUAU2KTX },
115         { USB_VENDOR_NETGEAR, USB_PRODUCT_NETGEAR_FA120 },
116         { USB_VENDOR_SYSTEMTALKS, USB_PRODUCT_SYSTEMTALKS_SGCX2UL },
117         { USB_VENDOR_SITECOM, USB_PRODUCT_SITECOM_LN029 },
118         { 0, 0 }
119 };
120
121 Static int axe_match(device_ptr_t);
122 Static int axe_attach(device_ptr_t);
123 Static int axe_detach(device_ptr_t);
124
125 Static int axe_encap(struct axe_softc *, struct mbuf *, int);
126 Static void axe_rxeof(usbd_xfer_handle, usbd_private_handle, usbd_status);
127 Static void axe_txeof(usbd_xfer_handle, usbd_private_handle, usbd_status);
128 Static void axe_tick(void *);
129 Static void axe_rxstart(struct ifnet *);
130 Static void axe_start(struct ifnet *);
131 Static int axe_ioctl(struct ifnet *, u_long, caddr_t);
132 Static void axe_init(void *);
133 Static void axe_stop(struct axe_softc *);
134 Static void axe_watchdog(struct ifnet *);
135 Static void axe_shutdown(device_ptr_t);
136 Static int axe_miibus_readreg(device_ptr_t, int, int);
137 Static int axe_miibus_writereg(device_ptr_t, int, int, int);
138 Static void axe_miibus_statchg(device_ptr_t);
139 Static int axe_cmd(struct axe_softc *, int, int, int, void *);
140 Static int axe_ifmedia_upd(struct ifnet *);
141 Static void axe_ifmedia_sts(struct ifnet *, struct ifmediareq *);
142
143 Static void axe_setmulti(struct axe_softc *);
144
145 Static device_method_t axe_methods[] = {
146         /* Device interface */
147         DEVMETHOD(device_probe,         axe_match),
148         DEVMETHOD(device_attach,        axe_attach),
149         DEVMETHOD(device_detach,        axe_detach),
150         DEVMETHOD(device_shutdown,      axe_shutdown),
151
152         /* bus interface */
153         DEVMETHOD(bus_print_child,      bus_generic_print_child),
154         DEVMETHOD(bus_driver_added,     bus_generic_driver_added),
155
156         /* MII interface */
157         DEVMETHOD(miibus_readreg,       axe_miibus_readreg),
158         DEVMETHOD(miibus_writereg,      axe_miibus_writereg),
159         DEVMETHOD(miibus_statchg,       axe_miibus_statchg),
160
161         { 0, 0 }
162 };
163
164 Static driver_t axe_driver = {
165         "axe",
166         axe_methods,
167         sizeof(struct axe_softc)
168 };
169
170 Static devclass_t axe_devclass;
171
172 DRIVER_MODULE(axe, uhub, axe_driver, axe_devclass, usbd_driver_load, 0);
173 DRIVER_MODULE(miibus, axe, miibus_driver, miibus_devclass, 0, 0);
174 MODULE_DEPEND(axe, usb, 1, 1, 1);
175 MODULE_DEPEND(axe, miibus, 1, 1, 1);
176
177 Static int
178 axe_cmd(struct axe_softc *sc, int cmd, int index, int val, void *buf)
179 {
180         usb_device_request_t    req;
181         usbd_status             err;
182
183         if (sc->axe_dying)
184                 return(0);
185
186         if (AXE_CMD_DIR(cmd))
187                 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
188         else
189                 req.bmRequestType = UT_READ_VENDOR_DEVICE;
190         req.bRequest = AXE_CMD_CMD(cmd);
191         USETW(req.wValue, val);
192         USETW(req.wIndex, index);
193         USETW(req.wLength, AXE_CMD_LEN(cmd));
194
195         err = usbd_do_request(sc->axe_udev, &req, buf);
196
197         if (err)
198                 return(-1);
199
200         return(0);
201 }
202
203 Static int
204 axe_miibus_readreg(device_ptr_t dev, int phy, int reg)
205 {
206         struct axe_softc        *sc = USBGETSOFTC(dev);
207         usbd_status             err;
208         u_int16_t               val;
209
210         if (sc->axe_dying)
211                 return(0);
212
213 #ifdef notdef
214         /*
215          * The chip tells us the MII address of any supported
216          * PHYs attached to the chip, so only read from those.
217          */
218
219         if (sc->axe_phyaddrs[0] != AXE_NOPHY && phy != sc->axe_phyaddrs[0])
220                 return (0);
221
222         if (sc->axe_phyaddrs[1] != AXE_NOPHY && phy != sc->axe_phyaddrs[1])
223                 return (0);
224 #endif
225         if (sc->axe_phyaddrs[0] != 0xFF && sc->axe_phyaddrs[0] != phy)
226                 return (0);
227
228         AXE_LOCK(sc);
229         axe_cmd(sc, AXE_CMD_MII_OPMODE_SW, 0, 0, NULL);
230         err = axe_cmd(sc, AXE_CMD_MII_READ_REG, reg, phy, (void *)&val);
231         axe_cmd(sc, AXE_CMD_MII_OPMODE_HW, 0, 0, NULL);
232         AXE_UNLOCK(sc);
233
234         if (err) {
235                 printf("axe%d: read PHY failed\n", sc->axe_unit);
236                 return(-1);
237         }
238
239         if (val)
240                 sc->axe_phyaddrs[0] = phy;
241
242         return (val);
243 }
244
245 Static int
246 axe_miibus_writereg(device_ptr_t dev, int phy, int reg, int val)
247 {
248         struct axe_softc        *sc = USBGETSOFTC(dev);
249         usbd_status             err;
250
251         if (sc->axe_dying)
252                 return(0);
253
254         AXE_LOCK(sc);
255         axe_cmd(sc, AXE_CMD_MII_OPMODE_SW, 0, 0, NULL);
256         err = axe_cmd(sc, AXE_CMD_MII_WRITE_REG, reg, phy, (void *)&val);
257         axe_cmd(sc, AXE_CMD_MII_OPMODE_HW, 0, 0, NULL);
258         AXE_UNLOCK(sc);
259
260         if (err) {
261                 printf("axe%d: write PHY failed\n", sc->axe_unit);
262                 return(-1);
263         }
264
265         return (0);
266 }
267
268 Static void
269 axe_miibus_statchg(device_ptr_t dev)
270 {
271 #ifdef notdef
272         struct axe_softc        *sc = USBGETSOFTC(dev);
273         struct mii_data         *mii = GET_MII(sc);
274 #endif
275         /* doesn't seem to be necessary */
276
277         return;
278 }
279
280 /*
281  * Set media options.
282  */
283 Static int
284 axe_ifmedia_upd(struct ifnet *ifp)
285 {
286         struct axe_softc        *sc = ifp->if_softc;
287         struct mii_data         *mii = GET_MII(sc);
288
289         sc->axe_link = 0;
290         if (mii->mii_instance) {
291                 struct mii_softc        *miisc;
292                 LIST_FOREACH(miisc, &mii->mii_phys, mii_list)
293                          mii_phy_reset(miisc);
294         }
295         mii_mediachg(mii);
296
297         return (0);
298 }
299
300 /*
301  * Report current media status.
302  */
303 Static void
304 axe_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
305 {
306         struct axe_softc        *sc = ifp->if_softc;
307         struct mii_data         *mii = GET_MII(sc);
308
309         mii_pollstat(mii);
310         ifmr->ifm_active = mii->mii_media_active;
311         ifmr->ifm_status = mii->mii_media_status;
312
313         return;
314 }
315
316 Static void
317 axe_setmulti(struct axe_softc *sc)
318 {
319         struct ifnet            *ifp;
320         struct ifmultiaddr      *ifma;
321         u_int32_t               h = 0;
322         u_int16_t               rxmode;
323         u_int8_t                hashtbl[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
324
325         ifp = sc->axe_ifp;
326
327         AXE_LOCK(sc);
328         axe_cmd(sc, AXE_CMD_RXCTL_READ, 0, 0, (void *)&rxmode);
329
330         if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) {
331                 rxmode |= AXE_RXCMD_ALLMULTI;
332                 axe_cmd(sc, AXE_CMD_RXCTL_WRITE, 0, rxmode, NULL);
333                 AXE_UNLOCK(sc);
334                 return;
335         } else
336                 rxmode &= ~AXE_RXCMD_ALLMULTI;
337
338         IF_ADDR_LOCK(ifp);
339         TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
340         {
341                 if (ifma->ifma_addr->sa_family != AF_LINK)
342                         continue;
343                 h = ether_crc32_be(LLADDR((struct sockaddr_dl *)
344                     ifma->ifma_addr), ETHER_ADDR_LEN) >> 26;
345                 hashtbl[h / 8] |= 1 << (h % 8);
346         }
347         IF_ADDR_UNLOCK(ifp);
348
349         axe_cmd(sc, AXE_CMD_WRITE_MCAST, 0, 0, (void *)&hashtbl);
350         axe_cmd(sc, AXE_CMD_RXCTL_WRITE, 0, rxmode, NULL);
351         AXE_UNLOCK(sc);
352
353         return;
354 }
355
356 Static void
357 axe_reset(struct axe_softc *sc)
358 {
359         if (sc->axe_dying)
360                 return;
361
362         if (usbd_set_config_no(sc->axe_udev, AXE_CONFIG_NO, 1) ||
363             usbd_device2interface_handle(sc->axe_udev, AXE_IFACE_IDX,
364             &sc->axe_iface)) {
365                 printf("axe%d: getting interface handle failed\n",
366                     sc->axe_unit);
367         }
368
369         /* Wait a little while for the chip to get its brains in order. */
370         DELAY(1000);
371         return;
372 }
373
374 /*
375  * Probe for a AX88172 chip.
376  */
377 USB_MATCH(axe)
378 {
379         USB_MATCH_START(axe, uaa);
380         struct axe_type                 *t;
381
382         if (!uaa->iface)
383                 return(UMATCH_NONE);
384
385         t = axe_devs;
386         while(t->axe_vid) {
387                 if (uaa->vendor == t->axe_vid &&
388                     uaa->product == t->axe_did) {
389                         return(UMATCH_VENDOR_PRODUCT);
390                 }
391                 t++;
392         }
393
394         return(UMATCH_NONE);
395 }
396
397 /*
398  * Attach the interface. Allocate softc structures, do ifmedia
399  * setup and ethernet/BPF attach.
400  */
401 USB_ATTACH(axe)
402 {
403         USB_ATTACH_START(axe, sc, uaa);
404         char                    devinfo[1024];
405         u_char                  eaddr[ETHER_ADDR_LEN];
406         struct ifnet            *ifp;
407         usb_interface_descriptor_t      *id;
408         usb_endpoint_descriptor_t       *ed;
409         int                     i;
410
411         bzero(sc, sizeof(struct axe_softc));
412         sc->axe_udev = uaa->device;
413         sc->axe_dev = self;
414         sc->axe_unit = device_get_unit(self);
415
416         if (usbd_set_config_no(sc->axe_udev, AXE_CONFIG_NO, 1)) {
417                 printf("axe%d: getting interface handle failed\n",
418                     sc->axe_unit);
419                 USB_ATTACH_ERROR_RETURN;
420         }
421
422         if (usbd_device2interface_handle(uaa->device,
423             AXE_IFACE_IDX, &sc->axe_iface)) {
424                 printf("axe%d: getting interface handle failed\n",
425                     sc->axe_unit);
426                 USB_ATTACH_ERROR_RETURN;
427         }
428
429         id = usbd_get_interface_descriptor(sc->axe_iface);
430
431         usbd_devinfo(uaa->device, 0, devinfo);
432         device_set_desc_copy(self, devinfo);
433         printf("%s: %s\n", USBDEVNAME(self), devinfo);
434
435         /* Find endpoints. */
436         for (i = 0; i < id->bNumEndpoints; i++) {
437                 ed = usbd_interface2endpoint_descriptor(sc->axe_iface, i);
438                 if (!ed) {
439                         printf("axe%d: couldn't get ep %d\n",
440                             sc->axe_unit, i);
441                         USB_ATTACH_ERROR_RETURN;
442                 }
443                 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
444                     UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
445                         sc->axe_ed[AXE_ENDPT_RX] = ed->bEndpointAddress;
446                 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
447                            UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
448                         sc->axe_ed[AXE_ENDPT_TX] = ed->bEndpointAddress;
449                 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
450                            UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) {
451                         sc->axe_ed[AXE_ENDPT_INTR] = ed->bEndpointAddress;
452                 }
453         }
454
455         mtx_init(&sc->axe_mtx, device_get_nameunit(self), MTX_NETWORK_LOCK,
456             MTX_DEF | MTX_RECURSE);
457         AXE_LOCK(sc);
458
459         /*
460          * Get station address.
461          */
462         axe_cmd(sc, AXE_CMD_READ_NODEID, 0, 0, &eaddr);
463
464         /*
465          * Load IPG values and PHY indexes.
466          */
467         axe_cmd(sc, AXE_CMD_READ_IPG012, 0, 0, (void *)&sc->axe_ipgs);
468         axe_cmd(sc, AXE_CMD_READ_PHYID, 0, 0, (void *)&sc->axe_phyaddrs);
469
470         /*
471          * Work around broken adapters that appear to lie about
472          * their PHY addresses.
473          */
474         sc->axe_phyaddrs[0] = sc->axe_phyaddrs[1] = 0xFF;
475
476         ifp = sc->axe_ifp = if_alloc(IFT_ETHER);
477         if (ifp == NULL) {
478                 printf("axe%d: can not if_alloc()\n", sc->axe_unit);
479                 AXE_UNLOCK(sc);
480                 mtx_destroy(&sc->axe_mtx);
481                 USB_ATTACH_ERROR_RETURN;
482         }
483         ifp->if_softc = sc;
484         if_initname(ifp, "axe", sc->axe_unit);
485         ifp->if_mtu = ETHERMTU;
486         ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST |
487             IFF_NEEDSGIANT;
488         ifp->if_ioctl = axe_ioctl;
489         ifp->if_start = axe_start;
490         ifp->if_watchdog = axe_watchdog;
491         ifp->if_init = axe_init;
492         IFQ_SET_MAXLEN(&ifp->if_snd, IFQ_MAXLEN);
493         ifp->if_snd.ifq_drv_maxlen = IFQ_MAXLEN;
494         IFQ_SET_READY(&ifp->if_snd);
495
496         sc->axe_qdat.ifp = ifp;
497         sc->axe_qdat.if_rxstart = axe_rxstart;
498
499         if (mii_phy_probe(self, &sc->axe_miibus,
500             axe_ifmedia_upd, axe_ifmedia_sts)) {
501                 printf("axe%d: MII without any PHY!\n", sc->axe_unit);
502                 if_free(ifp);
503                 AXE_UNLOCK(sc);
504                 mtx_destroy(&sc->axe_mtx);
505                 USB_ATTACH_ERROR_RETURN;
506         }
507
508         /*
509          * Call MI attach routine.
510          */
511
512         ether_ifattach(ifp, eaddr);
513         callout_handle_init(&sc->axe_stat_ch);
514         usb_register_netisr();
515
516         sc->axe_dying = 0;
517
518         AXE_UNLOCK(sc);
519
520         USB_ATTACH_SUCCESS_RETURN;
521 }
522
523 Static int
524 axe_detach(device_ptr_t dev)
525 {
526         struct axe_softc        *sc;
527         struct ifnet            *ifp;
528
529         sc = device_get_softc(dev);
530         AXE_LOCK(sc);
531         ifp = sc->axe_ifp;
532
533         sc->axe_dying = 1;
534         untimeout(axe_tick, sc, sc->axe_stat_ch);
535         ether_ifdetach(ifp);
536         if_free(ifp);
537
538         if (sc->axe_ep[AXE_ENDPT_TX] != NULL)
539                 usbd_abort_pipe(sc->axe_ep[AXE_ENDPT_TX]);
540         if (sc->axe_ep[AXE_ENDPT_RX] != NULL)
541                 usbd_abort_pipe(sc->axe_ep[AXE_ENDPT_RX]);
542         if (sc->axe_ep[AXE_ENDPT_INTR] != NULL)
543                 usbd_abort_pipe(sc->axe_ep[AXE_ENDPT_INTR]);
544
545         AXE_UNLOCK(sc);
546         mtx_destroy(&sc->axe_mtx);
547
548         return(0);
549 }
550
551 Static void
552 axe_rxstart(struct ifnet *ifp)
553 {
554         struct axe_softc        *sc;
555         struct ue_chain *c;
556
557         sc = ifp->if_softc;
558         AXE_LOCK(sc);
559         c = &sc->axe_cdata.ue_rx_chain[sc->axe_cdata.ue_rx_prod];
560
561         c->ue_mbuf = usb_ether_newbuf();
562         if (c->ue_mbuf == NULL) {
563                 printf("%s: no memory for rx list "
564                     "-- packet dropped!\n", USBDEVNAME(sc->axe_dev));
565                 ifp->if_ierrors++;
566                 AXE_UNLOCK(sc);
567                 return;
568         }
569
570         /* Setup new transfer. */
571         usbd_setup_xfer(c->ue_xfer, sc->axe_ep[AXE_ENDPT_RX],
572             c, mtod(c->ue_mbuf, char *), UE_BUFSZ, USBD_SHORT_XFER_OK,
573             USBD_NO_TIMEOUT, axe_rxeof);
574         usbd_transfer(c->ue_xfer);
575         AXE_UNLOCK(sc);
576
577         return;
578 }
579
580 /*
581  * A frame has been uploaded: pass the resulting mbuf chain up to
582  * the higher level protocols.
583  */
584 Static void
585 axe_rxeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
586 {
587         struct axe_softc        *sc;
588         struct ue_chain *c;
589         struct mbuf             *m;
590         struct ifnet            *ifp;
591         int                     total_len = 0;
592
593         c = priv;
594         sc = c->ue_sc;
595         AXE_LOCK(sc);
596         ifp = sc->axe_ifp;
597
598         if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
599                 AXE_UNLOCK(sc);
600                 return;
601         }
602
603         if (status != USBD_NORMAL_COMPLETION) {
604                 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
605                         AXE_UNLOCK(sc);
606                         return;
607                 }
608                 if (usbd_ratecheck(&sc->axe_rx_notice))
609                         printf("axe%d: usb error on rx: %s\n", sc->axe_unit,
610                             usbd_errstr(status));
611                 if (status == USBD_STALLED)
612                         usbd_clear_endpoint_stall(sc->axe_ep[AXE_ENDPT_RX]);
613                 goto done;
614         }
615
616         usbd_get_xfer_status(xfer, NULL, NULL, &total_len, NULL);
617
618         m = c->ue_mbuf;
619
620         if (total_len < sizeof(struct ether_header)) {
621                 ifp->if_ierrors++;
622                 goto done;
623         }
624
625         ifp->if_ipackets++;
626         m->m_pkthdr.rcvif = (void *)&sc->axe_qdat;
627         m->m_pkthdr.len = m->m_len = total_len;
628
629         /* Put the packet on the special USB input queue. */
630         usb_ether_input(m);
631         AXE_UNLOCK(sc);
632
633         return;
634 done:
635         /* Setup new transfer. */
636         usbd_setup_xfer(c->ue_xfer, sc->axe_ep[AXE_ENDPT_RX],
637             c, mtod(c->ue_mbuf, char *), UE_BUFSZ, USBD_SHORT_XFER_OK,
638             USBD_NO_TIMEOUT, axe_rxeof);
639         usbd_transfer(c->ue_xfer);
640         AXE_UNLOCK(sc);
641
642         return;
643 }
644
645 /*
646  * A frame was downloaded to the chip. It's safe for us to clean up
647  * the list buffers.
648  */
649
650 Static void
651 axe_txeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
652 {
653         struct axe_softc        *sc;
654         struct ue_chain *c;
655         struct ifnet            *ifp;
656         usbd_status             err;
657
658         c = priv;
659         sc = c->ue_sc;
660         AXE_LOCK(sc);
661         ifp = sc->axe_ifp;
662
663         if (status != USBD_NORMAL_COMPLETION) {
664                 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
665                         AXE_UNLOCK(sc);
666                         return;
667                 }
668                 printf("axe%d: usb error on tx: %s\n", sc->axe_unit,
669                     usbd_errstr(status));
670                 if (status == USBD_STALLED)
671                         usbd_clear_endpoint_stall(sc->axe_ep[AXE_ENDPT_TX]);
672                 AXE_UNLOCK(sc);
673                 return;
674         }
675
676         ifp->if_timer = 0;
677         ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
678         usbd_get_xfer_status(c->ue_xfer, NULL, NULL, NULL, &err);
679
680         if (c->ue_mbuf != NULL) {
681                 c->ue_mbuf->m_pkthdr.rcvif = ifp;
682                 usb_tx_done(c->ue_mbuf);
683                 c->ue_mbuf = NULL;
684         }
685
686         if (err)
687                 ifp->if_oerrors++;
688         else
689                 ifp->if_opackets++;
690
691         AXE_UNLOCK(sc);
692
693         return;
694 }
695
696 Static void
697 axe_tick(void *xsc)
698 {
699         struct axe_softc        *sc;
700         struct ifnet            *ifp;
701         struct mii_data         *mii;
702
703         sc = xsc;
704
705         if (sc == NULL)
706                 return;
707
708         AXE_LOCK(sc);
709
710         ifp = sc->axe_ifp;
711         mii = GET_MII(sc);
712         if (mii == NULL) {
713                 AXE_UNLOCK(sc);
714                 return;
715         }
716
717         mii_tick(mii);
718         if (!sc->axe_link && mii->mii_media_status & IFM_ACTIVE &&
719             IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
720                 sc->axe_link++;
721                 if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
722                         axe_start(ifp);
723         }
724
725         sc->axe_stat_ch = timeout(axe_tick, sc, hz);
726
727         AXE_UNLOCK(sc);
728
729         return;
730 }
731
732 Static int
733 axe_encap(struct axe_softc *sc, struct mbuf *m, int idx)
734 {
735         struct ue_chain *c;
736         usbd_status             err;
737
738         c = &sc->axe_cdata.ue_tx_chain[idx];
739
740         /*
741          * Copy the mbuf data into a contiguous buffer, leaving two
742          * bytes at the beginning to hold the frame length.
743          */
744         m_copydata(m, 0, m->m_pkthdr.len, c->ue_buf);
745         c->ue_mbuf = m;
746
747         usbd_setup_xfer(c->ue_xfer, sc->axe_ep[AXE_ENDPT_TX],
748             c, c->ue_buf, m->m_pkthdr.len, USBD_FORCE_SHORT_XFER,
749             10000, axe_txeof);
750
751         /* Transmit */
752         err = usbd_transfer(c->ue_xfer);
753         if (err != USBD_IN_PROGRESS) {
754                 axe_stop(sc);
755                 return(EIO);
756         }
757
758         sc->axe_cdata.ue_tx_cnt++;
759
760         return(0);
761 }
762
763 Static void
764 axe_start(struct ifnet *ifp)
765 {
766         struct axe_softc        *sc;
767         struct mbuf             *m_head = NULL;
768
769         sc = ifp->if_softc;
770         AXE_LOCK(sc);
771
772         if (!sc->axe_link) {
773                 AXE_UNLOCK(sc);
774                 return;
775         }
776
777         if (ifp->if_drv_flags & IFF_DRV_OACTIVE) {
778                 AXE_UNLOCK(sc);
779                 return;
780         }
781
782         IFQ_DRV_DEQUEUE(&ifp->if_snd, m_head);
783         if (m_head == NULL) {
784                 AXE_UNLOCK(sc);
785                 return;
786         }
787
788         if (axe_encap(sc, m_head, 0)) {
789                 IFQ_DRV_PREPEND(&ifp->if_snd, m_head);
790                 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
791                 AXE_UNLOCK(sc);
792                 return;
793         }
794
795         /*
796          * If there's a BPF listener, bounce a copy of this frame
797          * to him.
798          */
799         BPF_MTAP(ifp, m_head);
800
801         ifp->if_drv_flags |= IFF_DRV_OACTIVE;
802
803         /*
804          * Set a timeout in case the chip goes out to lunch.
805          */
806         ifp->if_timer = 5;
807         AXE_UNLOCK(sc);
808
809         return;
810 }
811
812 Static void
813 axe_init(void *xsc)
814 {
815         struct axe_softc        *sc = xsc;
816         struct ifnet            *ifp = sc->axe_ifp;
817         struct ue_chain *c;
818         usbd_status             err;
819         int                     i;
820         int                     rxmode;
821
822         if (ifp->if_drv_flags & IFF_DRV_RUNNING)
823                 return;
824
825         AXE_LOCK(sc);
826
827         /*
828          * Cancel pending I/O and free all RX/TX buffers.
829          */
830
831         axe_reset(sc);
832
833 #ifdef notdef
834         /* Set MAC address */
835         axe_mac(sc, IFP2ENADDR(sc->axe_ifp), 1);
836 #endif
837
838         /* Enable RX logic. */
839
840         /* Init TX ring. */
841         if (usb_ether_tx_list_init(sc, &sc->axe_cdata,
842             sc->axe_udev) == ENOBUFS) {
843                 printf("axe%d: tx list init failed\n", sc->axe_unit);
844                 AXE_UNLOCK(sc);
845                 return;
846         }
847
848         /* Init RX ring. */
849         if (usb_ether_rx_list_init(sc, &sc->axe_cdata,
850             sc->axe_udev) == ENOBUFS) {
851                 printf("axe%d: rx list init failed\n", sc->axe_unit);
852                 AXE_UNLOCK(sc);
853                 return;
854         }
855
856         /* Set transmitter IPG values */
857         axe_cmd(sc, AXE_CMD_WRITE_IPG0, 0, sc->axe_ipgs[0], NULL);
858         axe_cmd(sc, AXE_CMD_WRITE_IPG1, 0, sc->axe_ipgs[1], NULL);
859         axe_cmd(sc, AXE_CMD_WRITE_IPG2, 0, sc->axe_ipgs[2], NULL);
860
861         /* Enable receiver, set RX mode */
862         rxmode = AXE_RXCMD_UNICAST|AXE_RXCMD_MULTICAST|AXE_RXCMD_ENABLE;
863
864         /* If we want promiscuous mode, set the allframes bit. */
865         if (ifp->if_flags & IFF_PROMISC)
866                 rxmode |= AXE_RXCMD_PROMISC;
867
868         if (ifp->if_flags & IFF_BROADCAST)
869                 rxmode |= AXE_RXCMD_BROADCAST;
870
871         axe_cmd(sc, AXE_CMD_RXCTL_WRITE, 0, rxmode, NULL);
872
873         /* Load the multicast filter. */
874         axe_setmulti(sc);
875
876         /* Open RX and TX pipes. */
877         err = usbd_open_pipe(sc->axe_iface, sc->axe_ed[AXE_ENDPT_RX],
878             USBD_EXCLUSIVE_USE, &sc->axe_ep[AXE_ENDPT_RX]);
879         if (err) {
880                 printf("axe%d: open rx pipe failed: %s\n",
881                     sc->axe_unit, usbd_errstr(err));
882                 AXE_UNLOCK(sc);
883                 return;
884         }
885
886         err = usbd_open_pipe(sc->axe_iface, sc->axe_ed[AXE_ENDPT_TX],
887             USBD_EXCLUSIVE_USE, &sc->axe_ep[AXE_ENDPT_TX]);
888         if (err) {
889                 printf("axe%d: open tx pipe failed: %s\n",
890                     sc->axe_unit, usbd_errstr(err));
891                 AXE_UNLOCK(sc);
892                 return;
893         }
894
895         /* Start up the receive pipe. */
896         for (i = 0; i < UE_RX_LIST_CNT; i++) {
897                 c = &sc->axe_cdata.ue_rx_chain[i];
898                 usbd_setup_xfer(c->ue_xfer, sc->axe_ep[AXE_ENDPT_RX],
899                     c, mtod(c->ue_mbuf, char *), UE_BUFSZ,
900                     USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, axe_rxeof);
901                 usbd_transfer(c->ue_xfer);
902         }
903
904         ifp->if_drv_flags |= IFF_DRV_RUNNING;
905         ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
906
907         AXE_UNLOCK(sc);
908
909         sc->axe_stat_ch = timeout(axe_tick, sc, hz);
910
911         return;
912 }
913
914 Static int
915 axe_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
916 {
917         struct axe_softc        *sc = ifp->if_softc;
918         struct ifreq            *ifr = (struct ifreq *)data;
919         struct mii_data         *mii;
920         u_int16_t               rxmode;
921         int                     error = 0;
922
923         switch(command) {
924         case SIOCSIFFLAGS:
925                 if (ifp->if_flags & IFF_UP) {
926                         if (ifp->if_drv_flags & IFF_DRV_RUNNING &&
927                             ifp->if_flags & IFF_PROMISC &&
928                             !(sc->axe_if_flags & IFF_PROMISC)) {
929                                 AXE_LOCK(sc);
930                                 axe_cmd(sc, AXE_CMD_RXCTL_READ,
931                                         0, 0, (void *)&rxmode);
932                                 rxmode |= AXE_RXCMD_PROMISC;
933                                 axe_cmd(sc, AXE_CMD_RXCTL_WRITE,
934                                         0, rxmode, NULL);
935                                 AXE_UNLOCK(sc);
936                                 axe_setmulti(sc);
937                         } else if (ifp->if_drv_flags & IFF_DRV_RUNNING &&
938                             !(ifp->if_flags & IFF_PROMISC) &&
939                             sc->axe_if_flags & IFF_PROMISC) {
940                                 AXE_LOCK(sc);
941                                 axe_cmd(sc, AXE_CMD_RXCTL_READ,
942                                         0, 0, (void *)&rxmode);
943                                 rxmode &= ~AXE_RXCMD_PROMISC;
944                                 axe_cmd(sc, AXE_CMD_RXCTL_WRITE,
945                                         0, rxmode, NULL);
946                                 AXE_UNLOCK(sc);
947                                 axe_setmulti(sc);
948                         } else if (!(ifp->if_drv_flags & IFF_DRV_RUNNING))
949                                 axe_init(sc);
950                 } else {
951                         if (ifp->if_drv_flags & IFF_DRV_RUNNING)
952                                 axe_stop(sc);
953                 }
954                 sc->axe_if_flags = ifp->if_flags;
955                 error = 0;
956                 break;
957         case SIOCADDMULTI:
958         case SIOCDELMULTI:
959                 axe_setmulti(sc);
960                 error = 0;
961                 break;
962         case SIOCGIFMEDIA:
963         case SIOCSIFMEDIA:
964                 mii = GET_MII(sc);
965                 error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, command);
966                 break;
967
968         default:
969                 error = ether_ioctl(ifp, command, data);
970                 break;
971         }
972
973         AXE_UNLOCK(sc);
974
975         return(error);
976 }
977
978 Static void
979 axe_watchdog(struct ifnet *ifp)
980 {
981         struct axe_softc        *sc;
982         struct ue_chain *c;
983         usbd_status             stat;
984
985         sc = ifp->if_softc;
986         AXE_LOCK(sc);
987
988         ifp->if_oerrors++;
989         printf("axe%d: watchdog timeout\n", sc->axe_unit);
990
991         c = &sc->axe_cdata.ue_tx_chain[0];
992         usbd_get_xfer_status(c->ue_xfer, NULL, NULL, NULL, &stat);
993         axe_txeof(c->ue_xfer, c, stat);
994
995         AXE_UNLOCK(sc);
996
997         if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
998                 axe_start(ifp);
999
1000         return;
1001 }
1002
1003 /*
1004  * Stop the adapter and free any mbufs allocated to the
1005  * RX and TX lists.
1006  */
1007 Static void
1008 axe_stop(struct axe_softc *sc)
1009 {
1010         usbd_status             err;
1011         struct ifnet            *ifp;
1012
1013         AXE_LOCK(sc);
1014
1015         ifp = sc->axe_ifp;
1016         ifp->if_timer = 0;
1017
1018         untimeout(axe_tick, sc, sc->axe_stat_ch);
1019
1020         /* Stop transfers. */
1021         if (sc->axe_ep[AXE_ENDPT_RX] != NULL) {
1022                 err = usbd_abort_pipe(sc->axe_ep[AXE_ENDPT_RX]);
1023                 if (err) {
1024                         printf("axe%d: abort rx pipe failed: %s\n",
1025                         sc->axe_unit, usbd_errstr(err));
1026                 }
1027                 err = usbd_close_pipe(sc->axe_ep[AXE_ENDPT_RX]);
1028                 if (err) {
1029                         printf("axe%d: close rx pipe failed: %s\n",
1030                         sc->axe_unit, usbd_errstr(err));
1031                 }
1032                 sc->axe_ep[AXE_ENDPT_RX] = NULL;
1033         }
1034
1035         if (sc->axe_ep[AXE_ENDPT_TX] != NULL) {
1036                 err = usbd_abort_pipe(sc->axe_ep[AXE_ENDPT_TX]);
1037                 if (err) {
1038                         printf("axe%d: abort tx pipe failed: %s\n",
1039                         sc->axe_unit, usbd_errstr(err));
1040                 }
1041                 err = usbd_close_pipe(sc->axe_ep[AXE_ENDPT_TX]);
1042                 if (err) {
1043                         printf("axe%d: close tx pipe failed: %s\n",
1044                             sc->axe_unit, usbd_errstr(err));
1045                 }
1046                 sc->axe_ep[AXE_ENDPT_TX] = NULL;
1047         }
1048
1049         if (sc->axe_ep[AXE_ENDPT_INTR] != NULL) {
1050                 err = usbd_abort_pipe(sc->axe_ep[AXE_ENDPT_INTR]);
1051                 if (err) {
1052                         printf("axe%d: abort intr pipe failed: %s\n",
1053                         sc->axe_unit, usbd_errstr(err));
1054                 }
1055                 err = usbd_close_pipe(sc->axe_ep[AXE_ENDPT_INTR]);
1056                 if (err) {
1057                         printf("axe%d: close intr pipe failed: %s\n",
1058                             sc->axe_unit, usbd_errstr(err));
1059                 }
1060                 sc->axe_ep[AXE_ENDPT_INTR] = NULL;
1061         }
1062
1063         axe_reset(sc);
1064
1065         /* Free RX resources. */
1066         usb_ether_rx_list_free(&sc->axe_cdata);
1067         /* Free TX resources. */
1068         usb_ether_tx_list_free(&sc->axe_cdata);
1069
1070         ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
1071         sc->axe_link = 0;
1072         AXE_UNLOCK(sc);
1073
1074         return;
1075 }
1076
1077 /*
1078  * Stop all chip I/O so that the kernel's probe routines don't
1079  * get confused by errant DMAs when rebooting.
1080  */
1081 Static void
1082 axe_shutdown(device_ptr_t dev)
1083 {
1084         struct axe_softc        *sc;
1085
1086         sc = device_get_softc(dev);
1087
1088         axe_stop(sc);
1089
1090         return;
1091 }