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: stable/10/sys/dev/pccard/pccard_device.c 150432 2005-09-21 20:08:24Z imp $");
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 <dev/pccard/pccardreg.h>
43 #include <dev/pccard/pccardvar.h>
44 #include <dev/pccard/pccardvarp.h>
45 #include <dev/pccard/pccard_cis.h>
46 
47 static	d_open_t	pccard_open;
48 static	d_close_t	pccard_close;
49 static	d_read_t	pccard_read;
50 static	d_ioctl_t	pccard_ioctl;
51 
52 static struct cdevsw pccard_cdevsw = {
53 	.d_version =	D_VERSION,
54 	.d_open =	pccard_open,
55 	.d_close =	pccard_close,
56 	.d_read =	pccard_read,
57 	.d_ioctl =	pccard_ioctl,
58 	.d_name =	"pccard"
59 };
60 
61 int
pccard_device_create(struct pccard_softc * sc)62 pccard_device_create(struct pccard_softc *sc)
63 {
64 	uint32_t minor;
65 
66 	minor = device_get_unit(sc->dev) << 16;
67 	sc->cisdev = make_dev(&pccard_cdevsw, minor, 0, 0, 0666,
68 	    "pccard%u.cis", device_get_unit(sc->dev));
69 	sc->cisdev->si_drv1 = sc;
70 	return (0);
71 }
72 
73 int
pccard_device_destroy(struct pccard_softc * sc)74 pccard_device_destroy(struct pccard_softc *sc)
75 {
76 	if (sc->cisdev)
77 		destroy_dev(sc->cisdev);
78 	return (0);
79 }
80 
81 static int
pccard_build_cis(const struct pccard_tuple * tuple,void * argp)82 pccard_build_cis(const struct pccard_tuple *tuple, void *argp)
83 {
84 	struct cis_buffer *cis;
85 	int i;
86 	uint8_t ch;
87 
88 	cis = (struct cis_buffer *)argp;
89 	/*
90 	 * CISTPL_END is a special case, it has no length field.
91 	 */
92 	if (tuple->code == CISTPL_END) {
93 		if (cis->len + 1 > sizeof(cis->buffer))
94 			return (ENOSPC);
95 		cis->buffer[cis->len++] = tuple->code;
96 		return (0);
97 	}
98 	if (cis->len + 2 + tuple->length > sizeof(cis->buffer))
99 		return (ENOSPC);
100 	cis->buffer[cis->len++] = tuple->code;
101 	cis->buffer[cis->len++] = tuple->length;
102 	for (i = 0; i < tuple->length; i++) {
103 		ch = pccard_tuple_read_1(tuple, i);
104 		cis->buffer[cis->len++] = ch;
105 	}
106 	return (0);
107 }
108 
109 static	int
pccard_open(struct cdev * dev,int oflags,int devtype,struct thread * td)110 pccard_open(struct cdev *dev, int oflags, int devtype, struct thread *td)
111 {
112 	device_t parent, child;
113 	device_t *kids;
114 	int cnt, err;
115 	struct pccard_softc *sc;
116 
117 	sc = dev->si_drv1;
118 	if (sc->cis_open)
119 		return (EBUSY);
120 	parent = sc->dev;
121 	err = device_get_children(parent, &kids, &cnt);
122 	if (err)
123 		return err;
124 	if (cnt == 0) {
125 		free(kids, M_TEMP);
126 		sc->cis_open++;
127 		sc->cis = NULL;
128 		return (0);
129 	}
130 	child = kids[0];
131 	free(kids, M_TEMP);
132 	sc->cis = malloc(sizeof(*sc->cis), M_TEMP, M_ZERO | M_WAITOK);
133 	err = pccard_scan_cis(parent, child, pccard_build_cis, sc->cis);
134 	if (err) {
135 		free(sc->cis, M_TEMP);
136 		sc->cis = NULL;
137 		return (err);
138 	}
139 	sc->cis_open++;
140 	return (0);
141 }
142 
143 static	int
pccard_close(struct cdev * dev,int fflags,int devtype,struct thread * td)144 pccard_close(struct cdev *dev, int fflags, int devtype, struct thread *td)
145 {
146 	struct pccard_softc *sc;
147 
148 	sc = dev->si_drv1;
149 	free(sc->cis, M_TEMP);
150 	sc->cis = NULL;
151 	sc->cis_open = 0;
152 	return (0);
153 }
154 
155 static	int
pccard_ioctl(struct cdev * dev,u_long cmd,caddr_t data,int fflag,struct thread * td)156 pccard_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag,
157     struct thread *td)
158 {
159 	return (ENOTTY);
160 }
161 
162 static	int
pccard_read(struct cdev * dev,struct uio * uio,int ioflag)163 pccard_read(struct cdev *dev, struct uio *uio, int ioflag)
164 {
165 	struct pccard_softc *sc;
166 
167 	sc = dev->si_drv1;
168 	/* EOF */
169 	if (sc->cis == NULL || uio->uio_offset > sc->cis->len)
170 		return (0);
171 	return (uiomove(sc->cis->buffer + uio->uio_offset,
172 	  MIN(uio->uio_resid, sc->cis->len - uio->uio_offset), uio));
173 }
174