xref: /freebsd/sys/dev/cardbus/cardbus_device.c (revision a9e8641da961bcf3d24afc85fd657f2083a872a2)
1 /*-
2  * Copyright (c) 2005-2008, 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 static int
67 cardbus_build_cis(device_t cbdev, device_t child, int id,
68     int len, uint8_t *tupledata, uint32_t start, uint32_t *off,
69     struct tuple_callbacks *info, void *argp)
70 {
71 	struct cis_buffer *cis;
72 	int i;
73 
74 	cis = (struct cis_buffer *)argp;
75 	/*
76 	 * CISTPL_END is a special case, it has no length field.
77 	 */
78 	if (id == CISTPL_END) {
79 		if (cis->len + 1 > sizeof(cis->buffer)) {
80 			cis->len = 0;
81 			return (ENOSPC);
82 		}
83 		cis->buffer[cis->len++] = id;
84 		return (0);
85 	}
86 	if (cis->len + 2 + len > sizeof(cis->buffer)) {
87 		cis->len = 0;
88 		return (ENOSPC);
89 	}
90 	cis->buffer[cis->len++] = id;
91 	cis->buffer[cis->len++] = len;
92 	for (i = 0; i < len; i++)
93 		cis->buffer[cis->len++] = tupledata[i];
94 	return (0);
95 }
96 
97 static int
98 cardbus_device_buffer_cis(device_t parent, device_t child,
99     struct cis_buffer *cbp)
100 {
101 	struct tuple_callbacks cb[] = {
102 		{CISTPL_GENERIC, "GENERIC", cardbus_build_cis}
103 	};
104 
105 	return (cardbus_parse_cis(parent, child, cb, cbp));
106 }
107 
108 int
109 cardbus_device_create(struct cardbus_softc *sc, struct cardbus_devinfo *devi,
110     device_t parent, device_t child)
111 {
112 	uint32_t minor;
113 	int unit;
114 
115 	cardbus_device_buffer_cis(parent, child, &devi->sc_cis);
116 	minor = (device_get_unit(sc->sc_dev) << 8) + devi->pci.cfg.func;
117 	unit = device_get_unit(sc->sc_dev);
118 	devi->sc_cisdev = make_dev(&cardbus_cdevsw, minor, 0, 0, 0666,
119 	    "cardbus%d.%d.cis", unit, devi->pci.cfg.func);
120 	if (devi->pci.cfg.func == 0)
121 		make_dev_alias(devi->sc_cisdev, "cardbus%d.cis", unit);
122 	devi->sc_cisdev->si_drv1 = devi;
123 	return (0);
124 }
125 
126 int
127 cardbus_device_destroy(struct cardbus_devinfo *devi)
128 {
129 	if (devi->sc_cisdev)
130 		destroy_dev(devi->sc_cisdev);
131 	return (0);
132 }
133 
134 static	int
135 cardbus_open(struct cdev *dev, int oflags, int devtype, struct thread *td)
136 {
137 
138 	return (0);
139 }
140 
141 static	int
142 cardbus_close(struct cdev *dev, int fflags, int devtype, struct thread *td)
143 {
144 
145 	return (0);
146 }
147 
148 static	int
149 cardbus_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag,
150     struct thread *td)
151 {
152 	return (ENOTTY);
153 }
154 
155 static	int
156 cardbus_read(struct cdev *dev, struct uio *uio, int ioflag)
157 {
158 	struct cardbus_devinfo *devi;
159 
160 	devi = dev->si_drv1;
161 	/* EOF */
162 	if (uio->uio_offset >= devi->sc_cis.len)
163 		return (0);
164 	return (uiomove(devi->sc_cis.buffer + uio->uio_offset,
165 	  MIN(uio->uio_resid, devi->sc_cis.len - uio->uio_offset), uio));
166 }
167