1124771Sgrehan/*-
2124771Sgrehan * SPDX-License-Identifier: BSD-2-Clause
3124771Sgrehan *
4124771Sgrehan * Copyright (c) 2005-2008 M. Warner Losh <imp@FreeBSD.org>
5124771Sgrehan *
6124771Sgrehan * Redistribution and use in source and binary forms, with or without
7124771Sgrehan * modification, are permitted provided that the following conditions
8124771Sgrehan * are met:
9124771Sgrehan * 1. Redistributions of source code must retain the above copyright
10124771Sgrehan *    notice unmodified, this list of conditions, and the following
11124771Sgrehan *    disclaimer.
12124771Sgrehan * 2. Redistributions in binary form must reproduce the above copyright
13124771Sgrehan *    notice, this list of conditions and the following disclaimer in the
14124771Sgrehan *    documentation and/or other materials provided with the distribution.
15124771Sgrehan *
16124771Sgrehan * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17124771Sgrehan * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18124771Sgrehan * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19124771Sgrehan * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20124771Sgrehan * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21124771Sgrehan * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22124771Sgrehan * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23124771Sgrehan * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24124771Sgrehan * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25124771Sgrehan * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26124771Sgrehan * SUCH DAMAGE.
27124771Sgrehan *
28124771Sgrehan */
29124771Sgrehan
30124771Sgrehan#include <sys/param.h>
31124771Sgrehan#include <sys/conf.h>
32124771Sgrehan#include <sys/malloc.h>
33124771Sgrehan#include <sys/systm.h>
34130585Sphk#include <sys/uio.h>
35255904Snwhitehorn
36124771Sgrehan#include <sys/bus.h>
37124771Sgrehan#include <machine/bus.h>
38124771Sgrehan#include <sys/rman.h>
39124771Sgrehan#include <machine/resource.h>
40131671Sgrehan
41124771Sgrehan#include <sys/pciio.h>
42124771Sgrehan#include <dev/pci/pcivar.h>
43124771Sgrehan#include <dev/pci/pcireg.h>
44124771Sgrehan#include <dev/pci/pci_private.h>
45124771Sgrehan
46124771Sgrehan#include <dev/cardbus/cardbusreg.h>
47124771Sgrehan#include <dev/cardbus/cardbusvar.h>
48124771Sgrehan#include <dev/cardbus/cardbus_cis.h>
49124771Sgrehan#include <dev/pccard/pccard_cis.h>
50124771Sgrehan
51124771Sgrehanstatic	d_open_t	cardbus_open;
52131671Sgrehanstatic	d_close_t	cardbus_close;
53131671Sgrehanstatic	d_read_t	cardbus_read;
54131671Sgrehanstatic	d_ioctl_t	cardbus_ioctl;
55186050Snwhitehorn
56142414Sgrehanstatic struct cdevsw cardbus_cdevsw = {
57142414Sgrehan	.d_version =	D_VERSION,
58142414Sgrehan	.d_open =	cardbus_open,
59142414Sgrehan	.d_close =	cardbus_close,
60142414Sgrehan	.d_read =	cardbus_read,
61124771Sgrehan	.d_ioctl =	cardbus_ioctl,
62124771Sgrehan	.d_name =	"cardbus"
63124771Sgrehan};
64
65static int
66cardbus_build_cis(device_t cbdev, device_t child, int id,
67    int len, uint8_t *tupledata, uint32_t start, uint32_t *off,
68    struct tuple_callbacks *info, void *argp)
69{
70	struct cis_buffer *cis;
71	int i;
72
73	cis = (struct cis_buffer *)argp;
74	/*
75	 * CISTPL_END is a special case, it has no length field.
76	 */
77	if (id == CISTPL_END) {
78		if (cis->len + 1 > sizeof(cis->buffer)) {
79			cis->len = 0;
80			return (ENOSPC);
81		}
82		cis->buffer[cis->len++] = id;
83		return (0);
84	}
85	if (cis->len + 2 + len > sizeof(cis->buffer)) {
86		cis->len = 0;
87		return (ENOSPC);
88	}
89	cis->buffer[cis->len++] = id;
90	cis->buffer[cis->len++] = len;
91	for (i = 0; i < len; i++)
92		cis->buffer[cis->len++] = tupledata[i];
93	return (0);
94}
95
96static int
97cardbus_device_buffer_cis(device_t parent, device_t child,
98    struct cis_buffer *cbp)
99{
100	struct tuple_callbacks cb[] = {
101		{CISTPL_GENERIC, "GENERIC", cardbus_build_cis}
102	};
103
104	return (cardbus_parse_cis(parent, child, cb, cbp));
105}
106
107int
108cardbus_device_create(struct cardbus_softc *sc, struct cardbus_devinfo *devi,
109    device_t parent, device_t child)
110{
111	uint32_t minor;
112	int unit;
113
114	cardbus_device_buffer_cis(parent, child, &devi->sc_cis);
115	minor = (device_get_unit(sc->sc_dev) << 8) + devi->pci.cfg.func;
116	unit = device_get_unit(sc->sc_dev);
117	devi->sc_cisdev = make_dev(&cardbus_cdevsw, minor, 0, 0, 0666,
118	    "cardbus%d.%d.cis", unit, devi->pci.cfg.func);
119	if (devi->pci.cfg.func == 0)
120		make_dev_alias(devi->sc_cisdev, "cardbus%d.cis", unit);
121	devi->sc_cisdev->si_drv1 = devi;
122	return (0);
123}
124
125int
126cardbus_device_destroy(struct cardbus_devinfo *devi)
127{
128	if (devi->sc_cisdev)
129		destroy_dev(devi->sc_cisdev);
130	return (0);
131}
132
133static	int
134cardbus_open(struct cdev *dev, int oflags, int devtype, struct thread *td)
135{
136
137	return (0);
138}
139
140static	int
141cardbus_close(struct cdev *dev, int fflags, int devtype, struct thread *td)
142{
143
144	return (0);
145}
146
147static	int
148cardbus_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag,
149    struct thread *td)
150{
151	return (ENOTTY);
152}
153
154static	int
155cardbus_read(struct cdev *dev, struct uio *uio, int ioflag)
156{
157	struct cardbus_devinfo *devi;
158
159	devi = dev->si_drv1;
160	/* EOF */
161	if (uio->uio_offset >= devi->sc_cis.len)
162		return (0);
163	return (uiomove(devi->sc_cis.buffer + uio->uio_offset,
164	  MIN(uio->uio_resid, devi->sc_cis.len - uio->uio_offset), uio));
165}
166