Merge from vendor branch CVS:
[dragonfly.git] / sys / dev / serial / sio / sio_pccard.c
1 /*-
2  * Copyright (c) 1991 The Regents of the University of California.
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, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *      This product includes software developed by the University of
16  *      California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  * $DragonFly: src/sys/dev/serial/sio/sio_pccard.c,v 1.3 2004/05/31 20:48:41 eirikn Exp $
34  */
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/tty.h>
38 #include <sys/proc.h>
39 #include <sys/module.h>
40 #include <sys/kernel.h>
41 #include <sys/bus.h>
42 #include <machine/bus.h>
43 #include <sys/rman.h>
44 #include <sys/timepps.h>
45
46 #include <machine/resource.h>
47
48 #include "sioreg.h"
49 #include "sio_private.h"
50
51 #include <bus/pccard/pccarddevs.h>
52 #include <bus/pccard/pccardreg.h>
53 #include <bus/pccard/pccardvar.h>
54
55 static  int     sio_pccard_attach(device_t dev);
56 static  int     sio_pccard_match(device_t self);
57 static  int     sio_pccard_detach(device_t dev);
58 static  int     sio_pccard_probe(device_t dev);
59
60 static device_method_t sio_pccard_methods[] = {
61         /* Device interface */
62         DEVMETHOD(device_probe,         pccard_compat_probe),
63         DEVMETHOD(device_attach,        pccard_compat_attach),
64         DEVMETHOD(device_detach,        sio_pccard_detach),
65
66         /* Card interface */
67         DEVMETHOD(card_compat_match,    sio_pccard_match),
68         DEVMETHOD(card_compat_probe,    sio_pccard_probe),
69         DEVMETHOD(card_compat_attach,   sio_pccard_attach),
70
71         { 0, 0 }
72 };
73
74 static driver_t sio_pccard_driver = {
75         "sio",
76         sio_pccard_methods,
77         sizeof(struct com_s),
78 };
79
80 static int
81 sio_pccard_match(device_t dev)
82 {
83         u_int32_t       fcn = PCCARD_FUNCTION_UNSPEC;
84
85         fcn = pccard_get_function(dev);
86         /*
87          * If a serial card, we are likely the right driver.  However,
88          * some serial cards are better servered by other drivers, so
89          * allow other drivers to claim it, if they want.
90          */
91         if (fcn == PCCARD_FUNCTION_SERIAL)
92                 return (-100);
93
94         return(ENXIO);
95 }
96
97 static int
98 sio_pccard_probe(dev)
99         device_t        dev;
100 {
101         /* Do not probe IRQ - pccard doesn't turn on the interrupt line */
102         /* until bus_setup_intr */
103         SET_FLAG(dev, COM_C_NOPROBE);
104
105         return (sioprobe(dev, 0, 0UL));
106 }
107
108 static int
109 sio_pccard_attach(dev)
110         device_t        dev;
111 {
112         return (sioattach(dev, 0, 0UL));
113 }
114
115 /*
116  *      sio_detach - unload the driver and clear the table.
117  *      XXX TODO:
118  *      This is usually called when the card is ejected, but
119  *      can be caused by a modunload of a controller driver.
120  *      The idea is to reset the driver's view of the device
121  *      and ensure that any driver entry points such as
122  *      read and write do not hang.
123  */
124 static int
125 sio_pccard_detach(dev)
126         device_t        dev;
127 {
128         struct com_s    *com;
129
130         com = (struct com_s *) device_get_softc(dev);
131         if (com == NULL) {
132                 device_printf(dev, "NULL com in siounload\n");
133                 return (0);
134         }
135         com->gone = 1;
136         if (com->irqres) {
137                 bus_teardown_intr(dev, com->irqres, com->cookie);
138                 bus_release_resource(dev, SYS_RES_IRQ, 0, com->irqres);
139         }
140         if (com->ioportres)
141                 bus_release_resource(dev, SYS_RES_IOPORT, 0, com->ioportres);
142         if (com->tp && (com->tp->t_state & TS_ISOPEN)) {
143                 device_printf(dev, "still open, forcing close\n");
144                 com->tp->t_gen++;
145                 ttyclose(com->tp);
146                 ttwakeup(com->tp);
147                 ttwwakeup(com->tp);
148         } else {
149                 if (com->ibuf != NULL)
150                         free(com->ibuf, M_DEVBUF);
151         }
152         device_printf(dev, "unloaded\n");
153         return (0);
154 }
155
156 DRIVER_MODULE(sio, pccard, sio_pccard_driver, sio_devclass, 0, 0);