xref: /freebsd/sys/dev/cardbus/cardbus_device.c (revision 8ddb146abcdf061be9f2c0db7e391697dafad85c)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2005-2008 M. Warner Losh <imp@FreeBSD.org>
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice unmodified, this list of conditions, and the following
11  *    disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  *
28  */
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31 
32 #include <sys/param.h>
33 #include <sys/conf.h>
34 #include <sys/malloc.h>
35 #include <sys/systm.h>
36 #include <sys/uio.h>
37 
38 #include <sys/bus.h>
39 #include <machine/bus.h>
40 #include <sys/rman.h>
41 #include <machine/resource.h>
42 
43 #include <sys/pciio.h>
44 #include <dev/pci/pcivar.h>
45 #include <dev/pci/pcireg.h>
46 #include <dev/pci/pci_private.h>
47 
48 #include <dev/cardbus/cardbusreg.h>
49 #include <dev/cardbus/cardbusvar.h>
50 #include <dev/cardbus/cardbus_cis.h>
51 #include <dev/pccard/pccard_cis.h>
52 
53 static	d_open_t	cardbus_open;
54 static	d_close_t	cardbus_close;
55 static	d_read_t	cardbus_read;
56 static	d_ioctl_t	cardbus_ioctl;
57 
58 static struct cdevsw cardbus_cdevsw = {
59 	.d_version =	D_VERSION,
60 	.d_open =	cardbus_open,
61 	.d_close =	cardbus_close,
62 	.d_read =	cardbus_read,
63 	.d_ioctl =	cardbus_ioctl,
64 	.d_name =	"cardbus"
65 };
66 
67 static int
68 cardbus_build_cis(device_t cbdev, device_t child, int id,
69     int len, uint8_t *tupledata, uint32_t start, uint32_t *off,
70     struct tuple_callbacks *info, void *argp)
71 {
72 	struct cis_buffer *cis;
73 	int i;
74 
75 	cis = (struct cis_buffer *)argp;
76 	/*
77 	 * CISTPL_END is a special case, it has no length field.
78 	 */
79 	if (id == CISTPL_END) {
80 		if (cis->len + 1 > sizeof(cis->buffer)) {
81 			cis->len = 0;
82 			return (ENOSPC);
83 		}
84 		cis->buffer[cis->len++] = id;
85 		return (0);
86 	}
87 	if (cis->len + 2 + len > sizeof(cis->buffer)) {
88 		cis->len = 0;
89 		return (ENOSPC);
90 	}
91 	cis->buffer[cis->len++] = id;
92 	cis->buffer[cis->len++] = len;
93 	for (i = 0; i < len; i++)
94 		cis->buffer[cis->len++] = tupledata[i];
95 	return (0);
96 }
97 
98 static int
99 cardbus_device_buffer_cis(device_t parent, device_t child,
100     struct cis_buffer *cbp)
101 {
102 	struct tuple_callbacks cb[] = {
103 		{CISTPL_GENERIC, "GENERIC", cardbus_build_cis}
104 	};
105 
106 	return (cardbus_parse_cis(parent, child, cb, cbp));
107 }
108 
109 int
110 cardbus_device_create(struct cardbus_softc *sc, struct cardbus_devinfo *devi,
111     device_t parent, device_t child)
112 {
113 	uint32_t minor;
114 	int unit;
115 
116 	cardbus_device_buffer_cis(parent, child, &devi->sc_cis);
117 	minor = (device_get_unit(sc->sc_dev) << 8) + devi->pci.cfg.func;
118 	unit = device_get_unit(sc->sc_dev);
119 	devi->sc_cisdev = make_dev(&cardbus_cdevsw, minor, 0, 0, 0666,
120 	    "cardbus%d.%d.cis", unit, devi->pci.cfg.func);
121 	if (devi->pci.cfg.func == 0)
122 		make_dev_alias(devi->sc_cisdev, "cardbus%d.cis", unit);
123 	devi->sc_cisdev->si_drv1 = devi;
124 	return (0);
125 }
126 
127 int
128 cardbus_device_destroy(struct cardbus_devinfo *devi)
129 {
130 	if (devi->sc_cisdev)
131 		destroy_dev(devi->sc_cisdev);
132 	return (0);
133 }
134 
135 static	int
136 cardbus_open(struct cdev *dev, int oflags, int devtype, struct thread *td)
137 {
138 
139 	return (0);
140 }
141 
142 static	int
143 cardbus_close(struct cdev *dev, int fflags, int devtype, struct thread *td)
144 {
145 
146 	return (0);
147 }
148 
149 static	int
150 cardbus_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag,
151     struct thread *td)
152 {
153 	return (ENOTTY);
154 }
155 
156 static	int
157 cardbus_read(struct cdev *dev, struct uio *uio, int ioflag)
158 {
159 	struct cardbus_devinfo *devi;
160 
161 	devi = dev->si_drv1;
162 	/* EOF */
163 	if (uio->uio_offset >= devi->sc_cis.len)
164 		return (0);
165 	return (uiomove(devi->sc_cis.buffer + uio->uio_offset,
166 	  MIN(uio->uio_resid, devi->sc_cis.len - uio->uio_offset), uio));
167 }
168