Merge from vendor branch HOSTAPD:
[dragonfly.git] / sys / dev / netif / ed / if_ed_pci.c
1 /*
2  *
3  * Copyright (c) 1996 Stefan Esser <se@freebsd.org>
4  * 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 immediately at the beginning of the file, without modification,
11  *    this list of conditions, and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Absolutely no warranty of function or purpose is made by the author
16  *    Stefan Esser.
17  * 4. Modifications may be freely made to this file if the above conditions
18  *    are met.
19  *
20  * $FreeBSD: src/sys/dev/ed/if_ed_pci.c,v 1.34 2003/10/31 18:31:58 brooks Exp $
21  * $DragonFly: src/sys/dev/netif/ed/if_ed_pci.c,v 1.12 2006/08/01 18:01:38 swildner Exp $
22  */
23
24 #include <sys/param.h>
25 #include <sys/systm.h>
26 #include <sys/socket.h>
27 #include <sys/kernel.h>
28
29 #include <sys/module.h>
30 #include <sys/bus.h>
31
32 #include <machine/bus.h>
33 #include <sys/rman.h>
34 #include <machine/resource.h>
35
36 #include <net/if.h>
37 #include <net/if_arp.h>
38 #include <net/if_mib.h>
39
40 #include <bus/pci/pcidevs.h>
41 #include <bus/pci/pcireg.h>
42 #include <bus/pci/pcivar.h>
43
44 #include "if_edvar.h"
45
46 static int ed_pci_detach(device_t dev);
47
48 static struct ed_type {
49         uint16_t         ed_vid;
50         uint16_t         ed_did;
51         const char      *ed_name;
52 } ed_devs[] = {
53         { PCI_VENDOR_REALTEK, PCI_PRODUCT_REALTEK_RT8029,
54                 "NE2000 PCI Ethernet (RealTek 8029)" },
55         { PCI_VENDOR_NETVIN, PCI_PRODUCT_NETVIN_5000,
56                 "NE2000 PCI Ethernet (NetVin 5000)" },
57         { PCI_VENDOR_PROLAN, PCI_PRODUCT_PROLAN_NE2KETHER,
58                 "NE2000 PCI Ethernet (ProLAN)" },
59         { PCI_VENDOR_COMPEX, PCI_PRODUCT_COMPEX_NE2KETHER,
60                 "NE2000 PCI Ethernet (Compex)" },
61         { PCI_VENDOR_KTI, PCI_PRODUCT_KTI_NE2KETHER,
62                 "NE2000 PCI Ethernet (KTI)" },
63         { PCI_VENDOR_WINBOND, PCI_PRODUCT_WINBOND_W89C940F,
64                 "NE2000 PCI Ethernet (Winbond W89C940)" },
65         { PCI_VENDOR_SURECOM, PCI_PRODUCT_SURECOM_NE34,
66                 "NE2000 PCI Ethernet (Surecom NE-34)" },
67         { PCI_VENDOR_VIATECH, PCI_PRODUCT_VIATECH_VT86C926,
68                 "NE2000 PCI Ethernet (VIA VT86C926)" },
69         { 0, 0, NULL }
70 };
71
72 static int      ed_pci_probe    (device_t);
73 static int      ed_pci_attach   (device_t);
74
75 static int
76 ed_pci_probe(device_t dev)
77 {
78         struct ed_type *t;
79         uint16_t product = pci_get_device(dev);
80         uint16_t vendor = pci_get_vendor(dev);
81
82         for (t = ed_devs; t->ed_name != NULL; t++) {
83                 if (vendor == t->ed_vid && product == t->ed_did) {
84                         device_set_desc(dev, t->ed_name);
85                         return 0;
86                 }
87         }
88         return ENXIO;
89 }
90
91 static int
92 ed_pci_attach(device_t dev)
93 {
94         struct  ed_softc *sc = device_get_softc(dev);
95         int     flags = 0;
96         int     error;
97
98         error = ed_probe_Novell(dev, PCIR_MAPS, flags);
99         if (error)
100                 return (error);
101
102         error = ed_alloc_irq(dev, 0, RF_SHAREABLE);
103         if (error) {
104                 ed_release_resources(dev);
105                 return (error);
106         }
107
108         error = ed_attach(dev);
109         if (error == 0) {
110                 error = bus_setup_intr(dev, sc->irq_res, INTR_NETSAFE,
111                                        edintr, sc, &sc->irq_handle, 
112                                        sc->arpcom.ac_if.if_serializer);
113                 if (error)
114                         ed_pci_detach(dev);
115         } else {
116                 ed_release_resources(dev);
117         }
118         return (error);
119 }
120
121 static int
122 ed_pci_detach(device_t dev)
123 {
124         struct ed_softc *sc = device_get_softc(dev);
125         struct ifnet *ifp = &sc->arpcom.ac_if;
126
127         lwkt_serialize_enter(ifp->if_serializer);
128
129         if (sc->gone) {
130                 device_printf(dev, "already unloaded\n");
131                 lwkt_serialize_exit(ifp->if_serializer);
132                 return (0);
133         }
134         ed_stop(sc);
135         ifp->if_flags &= ~IFF_RUNNING;
136         sc->gone = 1;
137         bus_teardown_intr(dev, sc->irq_res, sc->irq_handle);
138
139         lwkt_serialize_exit(ifp->if_serializer);
140
141         ether_ifdetach(ifp);
142         ed_release_resources(dev);
143         return (0);
144 }
145
146 static device_method_t ed_pci_methods[] = {
147         /* Device interface */
148         DEVMETHOD(device_probe,         ed_pci_probe),
149         DEVMETHOD(device_attach,        ed_pci_attach),
150         DEVMETHOD(device_attach,        ed_pci_detach),
151
152         { 0, 0 }
153 };
154
155 static driver_t ed_pci_driver = {
156         "ed",
157         ed_pci_methods,
158         sizeof(struct ed_softc),
159 };
160
161 static devclass_t ed_devclass;
162
163 DRIVER_MODULE(ed, pci, ed_pci_driver, ed_devclass, 0, 0);
164 MODULE_DEPEND(ed, pci, 1, 1, 1);