if_ed_pccard.c revision 56361
1121054Semax/*
2121054Semax * Copyright (c) 1995, David Greenman
3121054Semax * All rights reserved.
4121054Semax *
5121054Semax * Redistribution and use in source and binary forms, with or without
6121054Semax * modification, are permitted provided that the following conditions
7121054Semax * are met:
8121054Semax * 1. Redistributions of source code must retain the above copyright
9121054Semax *    notice unmodified, this list of conditions, and the following
10121054Semax *    disclaimer.
11121054Semax * 2. Redistributions in binary form must reproduce the above copyright
12121054Semax *    notice, this list of conditions and the following disclaimer in the
13121054Semax *    documentation and/or other materials provided with the distribution.
14121054Semax *
15121054Semax * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16121054Semax * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17121054Semax * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18121054Semax * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19121054Semax * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20121054Semax * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21121054Semax * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22121054Semax * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23121054Semax * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24121054Semax * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25121054Semax * SUCH DAMAGE.
26121054Semax *
27121054Semax * $FreeBSD: head/sys/dev/ed/if_ed_pccard.c 56361 2000-01-21 03:08:46Z hosokawa $
28121054Semax */
29121054Semax
30121054Semax#include <sys/param.h>
31121054Semax#include <sys/systm.h>
32121054Semax#include <sys/socket.h>
33121054Semax#include <sys/kernel.h>
34121054Semax
35121054Semax#include <sys/module.h>
36121054Semax#include <sys/bus.h>
37121054Semax#include <machine/bus.h>
38121054Semax
39121054Semax#include <net/ethernet.h>
40121054Semax#include <net/if.h>
41121054Semax#include <net/if_arp.h>
42121054Semax#include <net/if_mib.h>
43121054Semax
44121054Semax#include <dev/ed/if_edvar.h>
45121054Semax#include <dev/pccard/pccardvar.h>
46121054Semax
47121054Semax/*
48121054Semax *      PC-Card (PCMCIA) specific code.
49121054Semax */
50121054Semaxstatic int	ed_pccard_probe(device_t);
51121054Semaxstatic int	ed_pccard_attach(device_t);
52121054Semaxstatic int	ed_pccard_detach(device_t);
53121054Semax
54121054Semaxstatic device_method_t ed_pccard_methods[] = {
55121054Semax	/* Device interface */
56121054Semax	DEVMETHOD(device_probe,		ed_pccard_probe),
57121054Semax	DEVMETHOD(device_attach,	ed_pccard_attach),
58121054Semax	DEVMETHOD(device_detach,	ed_pccard_detach),
59121054Semax
60121054Semax	{ 0, 0 }
61121054Semax};
62121054Semax
63121054Semaxstatic driver_t ed_pccard_driver = {
64121054Semax	"ed",
65121054Semax	ed_pccard_methods,
66121054Semax	sizeof(struct ed_softc)
67121054Semax};
68121054Semax
69121054Semaxstatic devclass_t ed_pccard_devclass;
70121054Semax
71121054SemaxDRIVER_MODULE(ed, pccard, ed_pccard_driver, ed_pccard_devclass, 0, 0);
72121054Semax
73121054Semax/*
74121054Semax *      ed_pccard_detach - unload the driver and clear the table.
75121054Semax *      XXX TODO:
76121054Semax *      This is usually called when the card is ejected, but
77121054Semax *      can be caused by a modunload of a controller driver.
78121054Semax *      The idea is to reset the driver's view of the device
79121054Semax *      and ensure that any driver entry points such as
80121054Semax *      read and write do not hang.
81121054Semax */
82121054Semaxstatic int
83121054Semaxed_pccard_detach(device_t dev)
84121054Semax{
85121054Semax	struct ed_softc *sc = device_get_softc(dev);
86121054Semax	struct ifnet *ifp = &sc->arpcom.ac_if;
87121054Semax
88121054Semax	if (sc->gone) {
89121054Semax		device_printf(dev, "already unloaded\n");
90121054Semax		return (0);
91121054Semax	}
92121054Semax	ed_stop(sc);
93121054Semax	ifp->if_flags &= ~IFF_RUNNING;
94161662Smarkus	if_detach(ifp);
95121054Semax	sc->gone = 1;
96121054Semax	bus_teardown_intr(dev, sc->irq_res, sc->irq_handle);
97121054Semax	ed_release_resources(dev);
98121054Semax	device_printf(dev, "unload\n");
99121054Semax	return (0);
100121054Semax}
101121054Semax
102121054Semax/*
103121054Semax * Probe framework for pccards.  Replicates the standard framework,
104121054Semax * minus the pccard driver registration and ignores the ether address
105121054Semax * supplied (from the CIS), relying on the probe to find it instead.
106121054Semax */
107121054Semaxstatic int
108121054Semaxed_pccard_probe(device_t dev)
109121054Semax{
110121054Semax	int     error;
111121054Semax
112121054Semax	error = ed_probe_WD80x3(dev);
113121054Semax	if (error == 0)
114121054Semax		goto end;
115121054Semax	ed_release_resources(dev);
116121054Semax
117121054Semax	error = ed_probe_Novell(dev);
118121054Semax	if (error == 0)
119121054Semax		goto end;
120121054Semax	ed_release_resources(dev);
121121054Semax
122161662Smarkusend:
123121054Semax	if (error == 0)
124121054Semax		error = ed_alloc_irq(dev, 0, 0);
125121054Semax
126121054Semax	ed_release_resources(dev);
127121054Semax	return (error);
128121054Semax}
129121054Semax
130121054Semaxstatic int
131121054Semaxed_pccard_attach(device_t dev)
132121054Semax{
133121054Semax	struct ed_softc *sc = device_get_softc(dev);
134121054Semax	int flags = device_get_flags(dev);
135121054Semax	int error;
136121054Semax	int i;
137121054Semax	u_char sum;
138121054Semax	u_char ether_addr[ETHER_ADDR_LEN];
139121054Semax
140121054Semax	if (sc->port_used > 0)
141121054Semax		ed_alloc_port(dev, sc->port_rid, sc->port_used);
142121054Semax	if (sc->mem_used)
143121054Semax		ed_alloc_memory(dev, sc->mem_rid, sc->mem_used);
144121054Semax	ed_alloc_irq(dev, sc->irq_rid, 0);
145121054Semax
146121054Semax	error = bus_setup_intr(dev, sc->irq_res, INTR_TYPE_NET,
147121054Semax			       edintr, sc, &sc->irq_handle);
148121054Semax	if (error) {
149121054Semax		printf("setup intr failed %d \n", error);
150121054Semax		ed_release_resources(dev);
151121054Semax		return (error);
152121054Semax	}
153121054Semax
154121054Semax	pccard_get_ether(dev, ether_addr);
155121054Semax	for (i = 0, sum = 0; i < ETHER_ADDR_LEN; i++)
156121054Semax		sum |= ether_addr[i];
157121054Semax	if (sum)
158121054Semax		bcopy(ether_addr, sc->arpcom.ac_enaddr, ETHER_ADDR_LEN);
159121054Semax
160121054Semax	error = ed_attach(sc, device_get_unit(dev), flags);
161121054Semax	return (error);
162121054Semax}
163121054Semax