xref: /freebsd/sys/dev/sbni/if_sbni_isa.c (revision 5ca8e32633c4ffbbcd6762e5888b6a4ba0708c6c)
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/param.h>
30 #include <sys/systm.h>
31 #include <sys/socket.h>
32 
33 #include <sys/bus.h>
34 #include <sys/kernel.h>
35 #include <sys/module.h>
36 #include <machine/bus.h>
37 #include <machine/resource.h>
38 #include <sys/rman.h>
39 
40 #include <net/if.h>
41 #include <net/if_var.h>
42 #include <net/ethernet.h>
43 
44 #include <isa/isavar.h>
45 
46 #include <dev/sbni/if_sbnireg.h>
47 #include <dev/sbni/if_sbnivar.h>
48 
49 static int	sbni_probe_isa(device_t);
50 static int	sbni_attach_isa(device_t);
51 
52 static device_method_t sbni_isa_methods[] = {
53 	/* Device interface */
54 	DEVMETHOD(device_probe,	sbni_probe_isa),
55 	DEVMETHOD(device_attach, sbni_attach_isa),
56 	{ 0, 0 }
57 };
58 
59 static driver_t sbni_isa_driver = {
60 	"sbni",
61 	sbni_isa_methods,
62 	sizeof(struct sbni_softc)
63 };
64 
65 static struct isa_pnp_id  sbni_ids[] = {
66 	{ 0, NULL }	/* we have no pnp sbni cards atm.  */
67 };
68 
69 static int
70 sbni_probe_isa(device_t dev)
71 {
72 	struct sbni_softc *sc;
73 	int error;
74 
75 	error = ISA_PNP_PROBE(device_get_parent(dev), dev, sbni_ids);
76 	if (error && error != ENOENT)
77 		return (error);
78 
79 	sc = device_get_softc(dev);
80 
81  	sc->io_res = bus_alloc_resource_anywhere(dev, SYS_RES_IOPORT,
82  						 &sc->io_rid, SBNI_PORTS,
83  						 RF_ACTIVE);
84 	if (!sc->io_res) {
85 		printf("sbni: cannot allocate io ports!\n");
86 		return (ENOENT);
87 	}
88 
89 	if (sbni_probe(sc) != 0) {
90 		sbni_release_resources(sc);
91 		return (ENXIO);
92 	}
93 
94 	device_set_desc(dev, "Granch SBNI12/ISA adapter");
95 	return (0);
96 }
97 
98 static int
99 sbni_attach_isa(device_t dev)
100 {
101 	struct sbni_softc *sc;
102 	struct sbni_flags flags;
103 	int error;
104 
105 	sc = device_get_softc(dev);
106 	sc->dev = dev;
107 
108 	sc->irq_res = bus_alloc_resource_any(
109 	    dev, SYS_RES_IRQ, &sc->irq_rid, RF_ACTIVE);
110 
111 #ifndef SBNI_DUAL_COMPOUND
112 
113 	if (sc->irq_res == NULL) {
114 		device_printf(dev, "irq conflict!\n");
115 		sbni_release_resources(sc);
116 		return (ENOENT);
117 	}
118 
119 #else	/* SBNI_DUAL_COMPOUND */
120 
121 	if (sc->irq_res) {
122 		sbni_add(sc);
123 	} else {
124 		struct sbni_softc  *master;
125 
126 		if ((master = connect_to_master(sc)) == NULL) {
127 			device_printf(dev, "failed to alloc irq\n");
128 			sbni_release_resources(sc);
129 			return (ENXIO);
130 		} else {
131 			device_printf(dev, "shared irq with %s\n",
132 			       if_name(master->ifp));
133 		}
134 	}
135 #endif	/* SBNI_DUAL_COMPOUND */
136 
137 	*(u_int32_t*)&flags = device_get_flags(dev);
138 
139 	error = sbni_attach(sc, device_get_unit(dev) * 2, flags);
140 	if (error) {
141 		device_printf(dev, "cannot initialize driver\n");
142 		sbni_release_resources(sc);
143 		return (error);
144 	}
145 
146 	if (sc->irq_res) {
147 		error = bus_setup_intr(
148 		    dev, sc->irq_res, INTR_TYPE_NET | INTR_MPSAFE,
149 		    NULL, sbni_intr, sc, &sc->irq_handle);
150 		if (error) {
151 			device_printf(dev, "bus_setup_intr\n");
152 			sbni_detach(sc);
153 			sbni_release_resources(sc);
154 			return (error);
155 		}
156 	}
157 
158 	return (0);
159 }
160 
161 DRIVER_MODULE(sbni, isa, sbni_isa_driver, 0, 0);
162 MODULE_DEPEND(sbni, isa, 1, 1, 1);
163 ISA_PNP_INFO(sbni_ids);
164