xref: /freebsd/sys/dev/sbni/if_sbni_isa.c (revision 4d846d260e2b9a3d4d0a701462568268cbfe7a5b)
1 /*-
2  * Copyright (c) 1997-2001 Granch, Ltd. All rights reserved.
3  * Author: Denis I.Timofeev <timofeev@granch.ru>
4  *
5  * Redistributon 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 unmodified, this list of conditions, and the following
10  *    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  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  *
27  */
28 
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31 
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/socket.h>
35 
36 #include <sys/bus.h>
37 #include <sys/kernel.h>
38 #include <sys/module.h>
39 #include <machine/bus.h>
40 #include <machine/resource.h>
41 #include <sys/rman.h>
42 
43 #include <net/if.h>
44 #include <net/if_var.h>
45 #include <net/ethernet.h>
46 
47 #include <isa/isavar.h>
48 
49 #include <dev/sbni/if_sbnireg.h>
50 #include <dev/sbni/if_sbnivar.h>
51 
52 static int	sbni_probe_isa(device_t);
53 static int	sbni_attach_isa(device_t);
54 
55 static device_method_t sbni_isa_methods[] = {
56 	/* Device interface */
57 	DEVMETHOD(device_probe,	sbni_probe_isa),
58 	DEVMETHOD(device_attach, sbni_attach_isa),
59 	{ 0, 0 }
60 };
61 
62 static driver_t sbni_isa_driver = {
63 	"sbni",
64 	sbni_isa_methods,
65 	sizeof(struct sbni_softc)
66 };
67 
68 static struct isa_pnp_id  sbni_ids[] = {
69 	{ 0, NULL }	/* we have no pnp sbni cards atm.  */
70 };
71 
72 static int
73 sbni_probe_isa(device_t dev)
74 {
75 	struct sbni_softc *sc;
76 	int error;
77 
78 	error = ISA_PNP_PROBE(device_get_parent(dev), dev, sbni_ids);
79 	if (error && error != ENOENT)
80 		return (error);
81 
82 	sc = device_get_softc(dev);
83 
84  	sc->io_res = bus_alloc_resource_anywhere(dev, SYS_RES_IOPORT,
85  						 &sc->io_rid, SBNI_PORTS,
86  						 RF_ACTIVE);
87 	if (!sc->io_res) {
88 		printf("sbni: cannot allocate io ports!\n");
89 		return (ENOENT);
90 	}
91 
92 	if (sbni_probe(sc) != 0) {
93 		sbni_release_resources(sc);
94 		return (ENXIO);
95 	}
96 
97 	device_set_desc(dev, "Granch SBNI12/ISA adapter");
98 	return (0);
99 }
100 
101 static int
102 sbni_attach_isa(device_t dev)
103 {
104 	struct sbni_softc *sc;
105 	struct sbni_flags flags;
106 	int error;
107 
108 	sc = device_get_softc(dev);
109 	sc->dev = dev;
110 
111 	sc->irq_res = bus_alloc_resource_any(
112 	    dev, SYS_RES_IRQ, &sc->irq_rid, RF_ACTIVE);
113 
114 #ifndef SBNI_DUAL_COMPOUND
115 
116 	if (sc->irq_res == NULL) {
117 		device_printf(dev, "irq conflict!\n");
118 		sbni_release_resources(sc);
119 		return (ENOENT);
120 	}
121 
122 #else	/* SBNI_DUAL_COMPOUND */
123 
124 	if (sc->irq_res) {
125 		sbni_add(sc);
126 	} else {
127 		struct sbni_softc  *master;
128 
129 		if ((master = connect_to_master(sc)) == NULL) {
130 			device_printf(dev, "failed to alloc irq\n");
131 			sbni_release_resources(sc);
132 			return (ENXIO);
133 		} else {
134 			device_printf(dev, "shared irq with %s\n",
135 			       if_name(master->ifp));
136 		}
137 	}
138 #endif	/* SBNI_DUAL_COMPOUND */
139 
140 	*(u_int32_t*)&flags = device_get_flags(dev);
141 
142 	error = sbni_attach(sc, device_get_unit(dev) * 2, flags);
143 	if (error) {
144 		device_printf(dev, "cannot initialize driver\n");
145 		sbni_release_resources(sc);
146 		return (error);
147 	}
148 
149 	if (sc->irq_res) {
150 		error = bus_setup_intr(
151 		    dev, sc->irq_res, INTR_TYPE_NET | INTR_MPSAFE,
152 		    NULL, sbni_intr, sc, &sc->irq_handle);
153 		if (error) {
154 			device_printf(dev, "bus_setup_intr\n");
155 			sbni_detach(sc);
156 			sbni_release_resources(sc);
157 			return (error);
158 		}
159 	}
160 
161 	return (0);
162 }
163 
164 DRIVER_MODULE(sbni, isa, sbni_isa_driver, 0, 0);
165 MODULE_DEPEND(sbni, isa, 1, 1, 1);
166 ISA_PNP_INFO(sbni_ids);
167