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