if_ed_pccard.c revision 54384
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 54384 1999-12-10 07:22:53Z imp $
28 */
29
30#include <sys/param.h>
31#include <sys/systm.h>
32#include <sys/socket.h>
33#include <sys/kernel.h>
34
35#include <sys/module.h>
36#include <sys/bus.h>
37#include <machine/bus.h>
38
39#include <net/ethernet.h>
40#include <net/if.h>
41#include <net/if_arp.h>
42#include <net/if_mib.h>
43
44#include <dev/ed/if_edvar.h>
45
46/*
47 *      PC-Card (PCMCIA) specific code.
48 */
49static int	ed_pccard_probe(device_t);
50static int	ed_pccard_attach(device_t);
51static int	ed_pccard_detach(device_t);
52
53static device_method_t ed_pccard_methods[] = {
54	/* Device interface */
55	DEVMETHOD(device_probe,		ed_pccard_probe),
56	DEVMETHOD(device_attach,	ed_pccard_attach),
57	DEVMETHOD(device_detach,	ed_pccard_detach),
58
59	{ 0, 0 }
60};
61
62static driver_t ed_pccard_driver = {
63	"ed",
64	ed_pccard_methods,
65	sizeof(struct ed_softc)
66};
67
68static devclass_t ed_pccard_devclass;
69
70DRIVER_MODULE(ed, pccard, ed_pccard_driver, ed_pccard_devclass, 0, 0);
71
72/*
73 *      ed_pccard_detach - unload the driver and clear the table.
74 *      XXX TODO:
75 *      This is usually called when the card is ejected, but
76 *      can be caused by a modunload of a controller driver.
77 *      The idea is to reset the driver's view of the device
78 *      and ensure that any driver entry points such as
79 *      read and write do not hang.
80 */
81static int
82ed_pccard_detach(device_t dev)
83{
84	struct ed_softc *sc = device_get_softc(dev);
85	struct ifnet *ifp = &sc->arpcom.ac_if;
86
87	if (sc->gone) {
88		device_printf(dev, "already unloaded\n");
89		return (0);
90	}
91	ed_stop(sc);
92	ifp->if_flags &= ~IFF_RUNNING;
93	if_detach(ifp);
94	sc->gone = 1;
95	bus_teardown_intr(dev, sc->irq_res, sc->irq_handle);
96	ed_release_resources(dev);
97	device_printf(dev, "unload\n");
98	return (0);
99}
100
101/*
102 * Probe framework for pccards.  Replicates the standard framework,
103 * minus the pccard driver registration and ignores the ether address
104 * supplied (from the CIS), relying on the probe to find it instead.
105 */
106static int
107ed_pccard_probe(device_t dev)
108{
109	int     error;
110
111	error = ed_probe_WD80x3(dev);
112	if (error == 0)
113		goto end;
114	ed_release_resources(dev);
115
116	error = ed_probe_Novell(dev);
117	if (error == 0)
118		goto end;
119	ed_release_resources(dev);
120
121end:
122	if (error == 0)
123		error = ed_alloc_irq(dev, 0, 0);
124
125	ed_release_resources(dev);
126	return (error);
127}
128
129static int
130ed_pccard_attach(device_t dev)
131{
132	struct ed_softc *sc = device_get_softc(dev);
133	int flags = device_get_flags(dev);
134	int error;
135
136	if (sc->port_used > 0)
137		ed_alloc_port(dev, sc->port_rid, sc->port_used);
138	if (sc->mem_used)
139		ed_alloc_memory(dev, sc->mem_rid, sc->mem_used);
140	ed_alloc_irq(dev, sc->irq_rid, 0);
141
142	error = bus_setup_intr(dev, sc->irq_res, INTR_TYPE_NET,
143			       edintr, sc, &sc->irq_handle);
144	if (error) {
145		printf("setup intr failed %d \n", error);
146		ed_release_resources(dev);
147		return (error);
148	}
149
150	error = ed_attach(sc, device_get_unit(dev), flags);
151	return (error);
152}
153