]> CyberLeo.Net >> Repos - FreeBSD/releng/7.2.git/blob - sys/dev/cardbus/cardbus_device.c
Create releng/7.2 from stable/7 in preparation for 7.2-RELEASE.
[FreeBSD/releng/7.2.git] / sys / dev / cardbus / cardbus_device.c
1 /*-
2  * Copyright (c) 2005, M. Warner Losh
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  */
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
30
31 #include <sys/param.h>
32 #include <sys/conf.h>
33 #include <sys/malloc.h>
34 #include <sys/systm.h>
35 #include <sys/uio.h>
36
37 #include <sys/bus.h>
38 #include <machine/bus.h>
39 #include <sys/rman.h>
40 #include <machine/resource.h>
41
42 #include <sys/pciio.h>
43 #include <dev/pci/pcivar.h>
44 #include <dev/pci/pcireg.h>
45 #include <dev/pci/pci_private.h>
46
47 #include <dev/cardbus/cardbusreg.h>
48 #include <dev/cardbus/cardbusvar.h>
49 #include <dev/cardbus/cardbus_cis.h>
50 #include <dev/pccard/pccard_cis.h>
51
52 static  d_open_t        cardbus_open;
53 static  d_close_t       cardbus_close;
54 static  d_read_t        cardbus_read;
55 static  d_ioctl_t       cardbus_ioctl;
56
57 static struct cdevsw cardbus_cdevsw = {
58         .d_version =    D_VERSION,
59         .d_open =       cardbus_open,
60         .d_close =      cardbus_close,
61         .d_read =       cardbus_read,
62         .d_ioctl =      cardbus_ioctl,
63         .d_name =       "cardbus"
64 };
65
66 int
67 cardbus_device_create(struct cardbus_softc *sc)
68 {
69         uint32_t minor;
70
71         minor = device_get_unit(sc->sc_dev) << 16;
72         sc->sc_cisdev = make_dev(&cardbus_cdevsw, minor, 0, 0, 0666,
73             "cardbus%u.cis", device_get_unit(sc->sc_dev));
74         sc->sc_cisdev->si_drv1 = sc;
75         return (0);
76 }
77
78 int
79 cardbus_device_destroy(struct cardbus_softc *sc)
80 {
81         if (sc->sc_cisdev)
82                 destroy_dev(sc->sc_cisdev);
83         return (0);
84 }
85
86 static int
87 cardbus_build_cis(device_t cbdev, device_t child, int id,
88     int len, uint8_t *tupledata, uint32_t start, uint32_t *off,
89     struct tuple_callbacks *info, void *argp)
90 {
91         struct cis_buffer *cis;
92         int i;
93
94         cis = (struct cis_buffer *)argp;
95         /*
96          * CISTPL_END is a special case, it has no length field.
97          */
98         if (id == CISTPL_END) {
99                 if (cis->len + 1 > sizeof(cis->buffer))
100                         return (ENOSPC);
101                 cis->buffer[cis->len++] = id;
102                 return (0);
103         }
104         if (cis->len + 2 + len > sizeof(cis->buffer))
105                 return (ENOSPC);
106         cis->buffer[cis->len++] = id;
107         cis->buffer[cis->len++] = len;
108         for (i = 0; i < len; i++)
109                 cis->buffer[cis->len++] = tupledata[i];
110         return (0);
111 }
112
113 static  int
114 cardbus_open(struct cdev *dev, int oflags, int devtype, struct thread *td)
115 {
116         device_t parent, child;
117         device_t *kids;
118         int cnt, err;
119         struct cardbus_softc *sc;
120         struct tuple_callbacks cb[] = {
121                 {CISTPL_GENERIC, "GENERIC", cardbus_build_cis}
122         };
123
124         sc = dev->si_drv1;
125         if (sc->sc_cis_open)
126                 return (EBUSY);
127         parent = sc->sc_dev;
128         err = device_get_children(parent, &kids, &cnt);
129         if (err)
130                 return err;
131         if (cnt == 0) {
132                 free(kids, M_TEMP);
133                 sc->sc_cis_open++;
134                 sc->sc_cis = NULL;
135                 return (0);
136         }
137         child = kids[0];
138         free(kids, M_TEMP);
139         sc->sc_cis = malloc(sizeof(*sc->sc_cis), M_TEMP, M_ZERO | M_WAITOK);
140         err = cardbus_parse_cis(parent, child, cb, sc->sc_cis);
141         if (err) {
142                 free(sc->sc_cis, M_TEMP);
143                 sc->sc_cis = NULL;
144                 return (err);
145         }
146         sc->sc_cis_open++;
147         return (0);
148 }
149
150 static  int
151 cardbus_close(struct cdev *dev, int fflags, int devtype, struct thread *td)
152 {
153         struct cardbus_softc *sc;
154
155         sc = dev->si_drv1;
156         free(sc->sc_cis, M_TEMP);
157         sc->sc_cis = NULL;
158         sc->sc_cis_open = 0;
159         return (0);
160 }
161
162 static  int
163 cardbus_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag,
164     struct thread *td)
165 {
166         return (ENOTTY);
167 }
168
169 static  int
170 cardbus_read(struct cdev *dev, struct uio *uio, int ioflag)
171 {
172         struct cardbus_softc *sc;
173
174         sc = dev->si_drv1;
175         /* EOF */
176         if (sc->sc_cis == NULL || uio->uio_offset > sc->sc_cis->len)
177                 return (0);
178         return (uiomove(sc->sc_cis->buffer + uio->uio_offset,
179           MIN(uio->uio_resid, sc->sc_cis->len - uio->uio_offset), uio));
180 }