1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3 *
4 * Copyright (c) 1999 Luoqi Chen.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31
32 #include <sys/param.h>
33 #include <sys/callout.h>
34 #include <sys/kernel.h>
35 #include <sys/lock.h>
36 #include <sys/module.h>
37 #include <sys/mutex.h>
38 #include <sys/bus.h>
39
40 #include <machine/bus.h>
41 #include <machine/resource.h>
42 #include <sys/rman.h>
43
44 #include <dev/aic/aicvar.h>
45 #include <dev/pccard/pccardvar.h>
46
47 #include "card_if.h"
48 #include "pccarddevs.h"
49
50 struct aic_pccard_softc {
51 struct aic_softc sc_aic;
52 struct resource *sc_port;
53 struct resource *sc_irq;
54 void *sc_ih;
55 };
56
57 static int aic_pccard_alloc_resources(device_t);
58 static void aic_pccard_release_resources(device_t);
59 static int aic_pccard_probe(device_t);
60 static int aic_pccard_attach(device_t);
61
62 static const struct pccard_product aic_pccard_products[] = {
63 PCMCIA_CARD(ADAPTEC, APA1460),
64 PCMCIA_CARD(ADAPTEC, APA1460A),
65 PCMCIA_CARD(NEWMEDIA, BUSTOASTER),
66 PCMCIA_CARD(NEWMEDIA, BUSTOASTER2),
67 PCMCIA_CARD(NEWMEDIA, BUSTOASTER3),
68 { NULL }
69 };
70
71 #define AIC_PCCARD_PORTSIZE 0x20
72
73 static int
74 aic_pccard_alloc_resources(device_t dev)
75 {
76 struct aic_pccard_softc *sc = device_get_softc(dev);
77 int rid;
78
79 sc->sc_port = sc->sc_irq = NULL;
80
81 rid = 0;
82 sc->sc_port = bus_alloc_resource_anywhere(dev, SYS_RES_IOPORT, &rid,
83 AIC_PCCARD_PORTSIZE, RF_ACTIVE);
84 if (!sc->sc_port)
85 return (ENOMEM);
86
87 rid = 0;
88 sc->sc_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, RF_ACTIVE);
89 if (!sc->sc_irq) {
90 aic_pccard_release_resources(dev);
91 return (ENOMEM);
92 }
93
94 sc->sc_aic.dev = dev;
95 sc->sc_aic.res = sc->sc_port;
96 mtx_init(&sc->sc_aic.lock, "aic", NULL, MTX_DEF);
97 return (0);
98 }
99
100 static void
101 aic_pccard_release_resources(device_t dev)
102 {
103 struct aic_pccard_softc *sc = device_get_softc(dev);
104
105 if (sc->sc_port)
106 bus_release_resource(dev, SYS_RES_IOPORT, 0, sc->sc_port);
107 if (sc->sc_irq)
108 bus_release_resource(dev, SYS_RES_IRQ, 0, sc->sc_irq);
109 sc->sc_port = sc->sc_irq = NULL;
110 mtx_destroy(&sc->sc_aic.lock);
111 }
112
113 static int
114 aic_pccard_probe(device_t dev)
115 {
116 const struct pccard_product *pp;
117
118 if ((pp = pccard_product_lookup(dev, aic_pccard_products,
119 sizeof(aic_pccard_products[0]), NULL)) != NULL) {
120 if (pp->pp_name != NULL)
121 device_set_desc(dev, pp->pp_name);
122 else
123 device_set_desc(dev,
124 "Adaptec 6260/6360 SCSI controller");
125 return (BUS_PROBE_DEFAULT);
126 }
127 return (ENXIO);
128 }
129
130 static int
131 aic_pccard_attach(device_t dev)
132 {
133 struct aic_pccard_softc *sc = device_get_softc(dev);
134 struct aic_softc *aic = &sc->sc_aic;
135 int error;
136
137 if (aic_pccard_alloc_resources(dev))
138 return (ENXIO);
139 if (aic_probe(aic)) {
140 aic_pccard_release_resources(dev);
141 return (ENXIO);
142 }
143
144 error = aic_attach(aic);
145 if (error) {
146 device_printf(dev, "attach failed\n");
147 aic_pccard_release_resources(dev);
148 return (error);
149 }
150
151 error = bus_setup_intr(dev, sc->sc_irq, INTR_TYPE_CAM | INTR_ENTROPY |
152 INTR_MPSAFE, NULL, aic_intr, aic, &sc->sc_ih);
153 if (error) {
154 device_printf(dev, "failed to register interrupt handler\n");
155 aic_pccard_release_resources(dev);
156 return (error);
157 }
158 return (0);
159 }
160
161 static int
162 aic_pccard_detach(device_t dev)
163 {
164 struct aic_pccard_softc *sc = device_get_softc(dev);
165 struct aic_softc *aic = &sc->sc_aic;
166 int error;
167
168 error = bus_teardown_intr(dev, sc->sc_irq, sc->sc_ih);
169 if (error) {
170 device_printf(dev, "failed to unregister interrupt handler\n");
171 }
172
173 error = aic_detach(aic);
174 if (error) {
175 device_printf(dev, "detach failed\n");
176 return (error);
177 }
178
179 aic_pccard_release_resources(dev);
180 return (0);
181 }
182
183 static device_method_t aic_pccard_methods[] = {
184 /* Device interface */
185 DEVMETHOD(device_probe, aic_pccard_probe),
186 DEVMETHOD(device_attach, aic_pccard_attach),
187 DEVMETHOD(device_detach, aic_pccard_detach),
188
189 { 0, 0 }
190 };
191
192 static driver_t aic_pccard_driver = {
193 "aic",
194 aic_pccard_methods, sizeof(struct aic_pccard_softc),
195 };
196
197 extern devclass_t aic_devclass;
198
199 MODULE_DEPEND(aic, cam, 1,1,1);
200 DRIVER_MODULE(aic, pccard, aic_pccard_driver, aic_devclass, 0, 0);
201 PCCARD_PNP_INFO(aic_pccard_products);
Cache object: 0bea14099e0568ac64e2970c8ee436f4
|