1/*-
2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3 *
4 * Copyright (c) 2006-2007 Daniel Roethlisberger <daniel@roe.ch>
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, this list of conditions and the following 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 * $FreeBSD$
29 */
30
31/*#define	CMX_DEBUG*/
32/*#define	CMX_INTR*/
33
34#define	CMX_MIN_RDLEN	10		/* min read length */
35#define	CMX_MIN_WRLEN	5		/* min write length */
36#define	CMX_BUFSZ	512		/* I/O block size */
37
38struct cmx_softc {
39	device_t dev;			/* pccard device */
40	struct cdev *cdev;		/* character device */
41
42	struct resource *ioport;	/* io port resource descriptor */
43	int ioport_rid;			/* io port resource identification */
44
45	bus_space_tag_t bst;		/* bus space tag */
46	bus_space_handle_t bsh;		/* bus space handle */
47
48#ifdef CMX_INTR
49	struct resource* irq;		/* irq resource descriptor */
50	int irq_rid;			/* irq resource identification */
51	void *ih;			/* intr handle */
52#endif
53
54	struct mtx mtx;			/* per-unit lock */
55	struct callout ch;		/* callout handle */
56	struct selinfo sel;		/* select/poll queue handle */
57
58	int open;			/* is chardev open? */
59	int polling;			/* are we polling? */
60	int dying;			/* are we detaching? */
61
62	unsigned long timeout;		/* response timeout */
63
64	uint8_t buf[CMX_BUFSZ];		/* read/write buffer */
65};
66
67extern devclass_t cmx_devclass;
68
69void	cmx_init_softc(device_t);
70int	cmx_alloc_resources(device_t);
71void	cmx_release_resources(device_t);
72int	cmx_attach(device_t);
73int	cmx_detach(device_t);
74
75#define	CMX_READ_1(sc, off)						\
76	(bus_space_read_1((sc)->bst, (sc)->bsh, off))
77#define	CMX_WRITE_1(sc, off, val)					\
78	(bus_space_write_1((sc)->bst, (sc)->bsh, off, val))
79
80#define	cmx_read_BSR(sc)						\
81	CMX_READ_1(sc, REG_OFFSET_BSR)
82#define	cmx_write_BSR(sc, val)						\
83	CMX_WRITE_1(sc, REG_OFFSET_BSR, val)
84#define	cmx_read_SCR(sc)						\
85	CMX_READ_1(sc, REG_OFFSET_SCR)
86#define	cmx_write_SCR(sc, val)						\
87	CMX_WRITE_1(sc, REG_OFFSET_SCR, val)
88#define	cmx_read_DTR(sc)						\
89	CMX_READ_1(sc, REG_OFFSET_DTR)
90#define	cmx_write_DTR(sc, val)						\
91	CMX_WRITE_1(sc, REG_OFFSET_DTR, val)
92
93#define	cmx_test(byte, flags, test)					\
94	(((byte) & (flags)) == ((test) ? (flags) : 0))
95
96#define	cmx_test_BSR(sc, flags, test)					\
97	cmx_test(cmx_read_BSR(sc), flags, test)
98
99#define	CMX_LOCK(sc)			mtx_lock(&(sc)->mtx)
100#define	CMX_UNLOCK(sc)			mtx_unlock(&(sc)->mtx)
101#define	CMX_LOCK_ASSERT(sc, what)	mtx_assert(&(sc)->mtx, (what))
102