BPF has been in the kernel for ages and is supported by all NICs but I4B.
[dragonfly.git] / sys / dev / netif / ep / if_ep_pccard.c
1 /*
2  * Copyright (c) 1994 Herb Peyerl <hpeyerl@novatel.ca>
3  * 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 Herb Peyerl.
16  * 4. The name of Herb Peyerl may not be used to endorse or promote products
17  *    derived from this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  *
30  * $FreeBSD: src/sys/dev/ep/if_ep_pccard.c,v 1.12.2.2 2000/08/08 23:55:02 peter Exp $
31  * $DragonFly: src/sys/dev/netif/ep/if_ep_pccard.c,v 1.6 2004/03/14 15:36:49 joerg Exp $
32  */
33
34 /*
35  * Pccard support for 3C589 by:
36  *              HAMADA Naoki
37  *              nao@tom-yam.or.jp
38  */
39
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/kernel.h>
43 #include <sys/socket.h>
44
45 #include <sys/module.h>
46 #include <sys/bus.h>
47
48 #include <machine/bus.h>
49 #include <machine/resource.h>
50 #include <sys/rman.h>
51  
52 #include <net/ethernet.h>
53 #include <net/if.h> 
54 #include <net/if_arp.h>
55 #include <net/if_media.h>
56
57 #include <machine/clock.h>
58
59 #include "if_epreg.h"
60 #include "if_epvar.h"
61
62 #include <bus/pccard/pccardvar.h>
63 #include <bus/pccard/pccarddevs.h>
64
65 #include "card_if.h"
66
67 static const char *ep_pccard_identify(u_short id);
68
69 /*
70  * Initialize the device - called from Slot manager.
71  */
72 static int
73 ep_pccard_probe(device_t dev)
74 {
75         struct ep_softc *       sc = device_get_softc(dev);
76         struct ep_board *       epb = &sc->epb;
77         const char *            desc;
78         int                     error;
79
80         error = ep_alloc(dev);
81         if (error)
82                 return error;
83
84         /*
85          * XXX - Certain (newer?) 3Com cards need epb->cmd_off ==
86          * 2. Sadly, you need to have a correct cmd_off in order to
87          * identify the card.  So we have to hit it with both and
88          * cross our virtual fingers.  There's got to be a better way
89          * to do this.  jyoung@accessus.net 09/11/1999 
90          */
91
92         epb->cmd_off = 0;
93         epb->prod_id = get_e(sc, EEPROM_PROD_ID);
94         if ((desc = ep_pccard_identify(epb->prod_id)) == NULL) {
95                 if (bootverbose) 
96                         device_printf(dev, "Pass 1 of 2 detection "
97                             "failed (nonfatal)\n");
98                 epb->cmd_off = 2;
99                 epb->prod_id = get_e(sc, EEPROM_PROD_ID);
100                 if ((desc = ep_pccard_identify(epb->prod_id)) == NULL) {
101                         device_printf(dev, "Unit failed to come ready or "
102                             "product ID unknown! (id 0x%x)\n", epb->prod_id);
103                         ep_free(dev);
104                         return (ENXIO);
105                 }
106         }
107         device_set_desc(dev, desc);
108
109         /*
110          * For some reason the 3c574 needs this.
111          */
112         ep_get_macaddr(sc, (u_char *)&sc->arpcom.ac_enaddr);
113
114         ep_free(dev);
115         return (0);
116 }
117
118 static const char *
119 ep_pccard_identify(u_short id)
120 {
121         /* Determine device type and associated MII capabilities  */
122         switch (id) {
123         case 0x6055: /* 3C556 */
124                 return ("3Com 3C556");
125         case 0x4057: /* 3C574 */
126                 return ("3Com 3C574");
127         case 0x4b57: /* 3C574B */
128                 return ("3Com 3C574B, Megahertz 3CCFE574BT or "
129                     "Fast Etherlink 3C574-TX");
130         case 0x2b57: /* 3CXSH572BT */
131                 return ("3Com OfficeConnect 572BT");
132         case 0x9058: /* 3C589 */
133                 return ("3Com Etherlink III 3C589");
134         case 0x2056: /* 3C562/3C563 */
135                 return ("3Com 3C562D/3C563D");
136         }
137         return (NULL);
138 }
139
140 static int
141 ep_pccard_card_attach(struct ep_board *epb)
142 {
143         /* Determine device type and associated MII capabilities  */
144         switch (epb->prod_id) {
145         case 0x6055: /* 3C556 */
146         case 0x2b57: /* 3C572BT */
147         case 0x4057: /* 3C574 */
148         case 0x4b57: /* 3C574B */
149                 epb->mii_trans = 1;
150                 return (1);
151         case 0x2056: /* 3C562D/3C563D */
152         case 0x9058: /* 3C589 */
153                 epb->mii_trans = 0;
154                 return (1);
155         }
156         return (0);
157 }
158
159 static int
160 ep_pccard_attach(device_t dev)
161 {
162         struct ep_softc *       sc = device_get_softc(dev);
163         int                     error = 0;
164
165         if ((error = ep_alloc(dev))) {
166                 device_printf(dev, "ep_alloc() failed! (%d)\n", error);
167                 goto bad;
168         }
169
170         sc->epb.cmd_off = 0;
171         sc->epb.prod_id = get_e(sc, EEPROM_PROD_ID);
172         if (!ep_pccard_card_attach(&sc->epb)) {
173                 sc->epb.cmd_off = 2;
174                 sc->epb.prod_id = get_e(sc, EEPROM_PROD_ID);
175                 sc->epb.res_cfg = get_e(sc, EEPROM_RESOURCE_CFG);
176                 if (!ep_pccard_card_attach(&sc->epb)) {
177                         device_printf(dev,
178                             "Probe found ID, attach failed so ignore card!\n");
179                         error = ENXIO;
180                         goto bad;
181                 }
182         }
183
184         /* ROM size = 0, ROM base = 0 */
185         /* For now, ignore AUTO SELECT feature of 3C589B and later. */
186         outw(BASE + EP_W0_ADDRESS_CFG, get_e(sc, EEPROM_ADDR_CFG) & 0xc000);
187
188         /* Fake IRQ must be 3 */
189         outw(BASE + EP_W0_RESOURCE_CFG, (sc->epb.res_cfg & 0x0fff) | 0x3000);
190
191         outw(BASE + EP_W0_PRODUCT_ID, sc->epb.prod_id);
192
193         if (sc->epb.mii_trans) {
194                 /*
195                  * turn on the MII transciever
196                  */
197                 GO_WINDOW(3);
198                 outw(BASE + EP_W3_OPTIONS, 0x8040);
199                 DELAY(1000);
200                 outw(BASE + EP_W3_OPTIONS, 0xc040);
201                 outw(BASE + EP_COMMAND, RX_RESET);
202                 outw(BASE + EP_COMMAND, TX_RESET);
203                 while (inw(BASE + EP_STATUS) & S_COMMAND_IN_PROGRESS);
204                 DELAY(1000);
205                 outw(BASE + EP_W3_OPTIONS, 0x8040);
206         } else {
207                 ep_get_media(sc);
208         }
209
210         if ((error = ep_attach(sc))) {
211                 device_printf(dev, "ep_attach() failed! (%d)\n", error);
212                 goto bad;
213         }
214
215         if ((error = bus_setup_intr(dev, sc->irq, INTR_TYPE_NET, ep_intr,
216                                     sc, &sc->ep_intrhand))) {
217                 device_printf(dev, "bus_setup_intr() failed! (%d)\n", error);
218                 goto bad;
219         }
220
221         return (0);
222 bad:
223         ep_free(dev);
224         return (error);
225 }
226
227 static int
228 ep_pccard_detach(device_t dev)
229 {
230         struct ep_softc *sc = device_get_softc(dev);
231
232         if (sc->gone) {
233                 device_printf(dev, "already unloaded\n");
234                 return (0);
235         }
236         sc->arpcom.ac_if.if_flags &= ~IFF_RUNNING; 
237         ether_ifdetach(&sc->arpcom.ac_if);
238         sc->gone = 1;
239         bus_teardown_intr(dev, sc->irq, sc->ep_intrhand);
240         ep_free(dev);
241         return (0);
242 }
243
244 static const struct pccard_product ep_pccard_products[] = {
245         PCMCIA_CARD(3COM, 3C1, 0),
246         PCMCIA_CARD(3COM, 3C562, 0),
247         PCMCIA_CARD(3COM, 3C574, 0),    /* ROADRUNNER */
248         PCMCIA_CARD(3COM, 3C589, 0),
249         PCMCIA_CARD(3COM, 3CCFEM556BI, 0),      /* ROADRUNNER */
250         PCMCIA_CARD(3COM, 3CXEM556, 0),
251         PCMCIA_CARD(3COM, 3CXEM556INT, 0),
252         {NULL}
253 };
254
255 static int
256 ep_pccard_match(device_t dev)
257 {
258         const struct pccard_product *pp;
259
260         if ((pp = pccard_product_lookup(dev, ep_pccard_products,
261                     sizeof(ep_pccard_products[0]), NULL)) != NULL) {
262                 if (pp->pp_name != NULL)
263                         device_set_desc(dev, pp->pp_name);
264                 return 0;
265         }
266         return EIO;
267 }
268
269 static device_method_t ep_pccard_methods[] = {
270         /* Device interface */
271         DEVMETHOD(device_probe, pccard_compat_probe),
272         DEVMETHOD(device_attach, pccard_compat_attach),
273         DEVMETHOD(device_detach, ep_pccard_detach),
274
275         /* Card interface */
276         DEVMETHOD(card_compat_match, ep_pccard_match),
277         DEVMETHOD(card_compat_probe, ep_pccard_probe),
278         DEVMETHOD(card_compat_attach, ep_pccard_attach),
279
280         { 0, 0 }
281 };
282
283 static driver_t ep_pccard_driver = {
284         "ep",
285         ep_pccard_methods,
286         sizeof(struct ep_softc),
287 };
288
289 extern devclass_t ep_devclass;
290
291 DRIVER_MODULE(if_ep, pccard, ep_pccard_driver, ep_devclass, 0, 0);