if_ed_pccard.c revision 64630
1/*
2 * Copyright (c) 1995, David Greenman
3 * All rights reserved.
4 *
5 * Redistribution 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 NEGLIGENCE 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 * $FreeBSD: head/sys/dev/ed/if_ed_pccard.c 64630 2000-08-14 04:31:07Z tanimura $
28 */
29
30#include <sys/param.h>
31#include <sys/systm.h>
32#include <sys/socket.h>
33#include <sys/kernel.h>
34#include <sys/conf.h>
35#include <sys/uio.h>
36#include <sys/select.h>
37#include <machine/clock.h>
38
39#include <sys/module.h>
40#include <sys/bus.h>
41#include <machine/bus.h>
42
43#include <net/ethernet.h>
44#include <net/if.h>
45#include <net/if_arp.h>
46#include <net/if_mib.h>
47
48#include <dev/ed/if_edreg.h>
49#include <dev/ed/if_edvar.h>
50#include <dev/pccard/pccardvar.h>
51#include <pccard/cardinfo.h>
52#include <pccard/slot.h>
53
54#define CARD_MAJOR	50
55
56/*
57 *      PC-Card (PCMCIA) specific code.
58 */
59static int	ed_pccard_probe(device_t);
60static int	ed_pccard_attach(device_t);
61static int	ed_pccard_detach(device_t);
62
63static void	ax88190_geteprom(device_t);
64static int	ed_pccard_memwrite(device_t dev, off_t offset, u_char byte);
65static int	ed_pccard_memread(device_t dev, off_t offset, u_char *buf, int size);
66
67static device_method_t ed_pccard_methods[] = {
68	/* Device interface */
69	DEVMETHOD(device_probe,		ed_pccard_probe),
70	DEVMETHOD(device_attach,	ed_pccard_attach),
71	DEVMETHOD(device_detach,	ed_pccard_detach),
72
73	{ 0, 0 }
74};
75
76static driver_t ed_pccard_driver = {
77	"ed",
78	ed_pccard_methods,
79	sizeof(struct ed_softc)
80};
81
82static devclass_t ed_pccard_devclass;
83
84DRIVER_MODULE(ed, pccard, ed_pccard_driver, ed_pccard_devclass, 0, 0);
85
86/*
87 *      ed_pccard_detach - unload the driver and clear the table.
88 *      XXX TODO:
89 *      This is usually called when the card is ejected, but
90 *      can be caused by a modunload of a controller driver.
91 *      The idea is to reset the driver's view of the device
92 *      and ensure that any driver entry points such as
93 *      read and write do not hang.
94 */
95static int
96ed_pccard_detach(device_t dev)
97{
98	struct ed_softc *sc = device_get_softc(dev);
99	struct ifnet *ifp = &sc->arpcom.ac_if;
100
101	if (sc->gone) {
102		device_printf(dev, "already unloaded\n");
103		return (0);
104	}
105	ed_stop(sc);
106	ifp->if_flags &= ~IFF_RUNNING;
107	ether_ifdetach(ifp, ETHER_BPF_SUPPORTED);
108	sc->gone = 1;
109	bus_teardown_intr(dev, sc->irq_res, sc->irq_handle);
110	ed_release_resources(dev);
111	return (0);
112}
113
114/*
115 * Probe framework for pccards.  Replicates the standard framework,
116 * minus the pccard driver registration and ignores the ether address
117 * supplied (from the CIS), relying on the probe to find it instead.
118 */
119static int
120ed_pccard_probe(device_t dev)
121{
122	int     error;
123	int	flags;
124	struct ed_softc *sc = device_get_softc(dev);
125
126	flags = device_get_flags(dev);
127
128	if (ED_FLAGS_GETTYPE(flags) == ED_FLAGS_AX88190) {
129		/* Special setup for AX88190 */
130		u_char rdbuf[4];
131		int iobase;
132		int attr_ioport;
133
134		/* XXX Allocate the port resource during setup. */
135		error = ed_alloc_port(dev, 0, ED_NOVELL_IO_PORTS);
136		if (error != 0)
137			return (error);
138
139		sc->chip_type = ED_CHIP_TYPE_AX88190;
140		/*
141		 * Check & Set Attribute Memory IOBASE Register
142		 */
143		ed_pccard_memread(dev,ED_AX88190_IOBASE0,rdbuf,4);
144		attr_ioport = rdbuf[2] << 8 | rdbuf[0];
145		iobase = rman_get_start(sc->port_res);
146		if (attr_ioport != iobase) {
147#if notdef
148			printf("AX88190 IOBASE MISMATCH %04x -> %04x Setting\n",attr_ioport,iobase);
149#endif /* notdef */
150			ed_pccard_memwrite(dev,ED_AX88190_IOBASE0,iobase & 0xff);
151			ed_pccard_memwrite(dev,ED_AX88190_IOBASE1,(iobase >> 8) & 0xff);
152		}
153		ed_ax88190_geteprom(sc);
154
155		ed_release_resources(dev);
156	}
157
158	error = ed_probe_Novell(dev);
159	if (error == 0)
160		goto end;
161	ed_release_resources(dev);
162
163	error = ed_probe_WD80x3(dev);
164	if (error == 0)
165		goto end;
166	ed_release_resources(dev);
167
168end:
169	if (error == 0)
170		error = ed_alloc_irq(dev, 0, 0);
171
172	ed_release_resources(dev);
173	return (error);
174}
175
176static int
177ed_pccard_attach(device_t dev)
178{
179	struct ed_softc *sc = device_get_softc(dev);
180	int flags = device_get_flags(dev);
181	int error;
182	int i;
183	u_char sum;
184	u_char ether_addr[ETHER_ADDR_LEN];
185
186	if (sc->port_used > 0)
187		ed_alloc_port(dev, sc->port_rid, sc->port_used);
188	if (sc->mem_used)
189		ed_alloc_memory(dev, sc->mem_rid, sc->mem_used);
190	ed_alloc_irq(dev, sc->irq_rid, 0);
191
192	error = bus_setup_intr(dev, sc->irq_res, INTR_TYPE_NET,
193			       edintr, sc, &sc->irq_handle);
194	if (error) {
195		printf("setup intr failed %d \n", error);
196		ed_release_resources(dev);
197		return (error);
198	}
199
200	if (ed_get_Linksys(sc) == 0) {
201		pccard_get_ether(dev, ether_addr);
202		for (i = 0, sum = 0; i < ETHER_ADDR_LEN; i++)
203			sum |= ether_addr[i];
204		if (sum)
205			bcopy(ether_addr, sc->arpcom.ac_enaddr, ETHER_ADDR_LEN);
206	}
207
208	error = ed_attach(sc, device_get_unit(dev), flags);
209	return (error);
210}
211
212/* XXX: Warner-san, any plan to provide access to the attribute memory? */
213static int
214ed_pccard_memwrite(device_t dev, off_t offset, u_char byte)
215{
216	struct pccard_devinfo *devi;
217	dev_t d;
218	struct iovec iov;
219	struct uio uios;
220
221	devi = device_get_ivars(dev);
222
223	iov.iov_base = &byte;
224	iov.iov_len = sizeof(byte);
225
226	uios.uio_iov = &iov;
227	uios.uio_iovcnt = 1;
228	uios.uio_offset = offset;
229	uios.uio_resid = sizeof(byte);
230	uios.uio_segflg = UIO_SYSSPACE;
231	uios.uio_rw = UIO_WRITE;
232	uios.uio_procp = 0;
233
234	d = makedev(CARD_MAJOR, devi->slt->slotnum);
235	return devsw(d)->d_write(d, &uios, 0);
236}
237
238static int
239ed_pccard_memread(device_t dev, off_t offset, u_char *buf, int size)
240{
241	struct pccard_devinfo *devi;
242	dev_t d;
243	struct iovec iov;
244	struct uio uios;
245
246	devi = device_get_ivars(dev);
247
248	iov.iov_base = buf;
249	iov.iov_len = size;
250
251	uios.uio_iov = &iov;
252	uios.uio_iovcnt = 1;
253	uios.uio_offset = offset;
254	uios.uio_resid = size;
255	uios.uio_segflg = UIO_SYSSPACE;
256	uios.uio_rw = UIO_READ;
257	uios.uio_procp = 0;
258
259	d = makedev(CARD_MAJOR, devi->slt->slotnum);
260	return devsw(d)->d_read(d, &uios, 0);
261}
262