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