1/*-
2 * Copyright (c) 2006-2007 Daniel Roethlisberger <daniel@roe.ch>
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, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 *
26 * $FreeBSD$
27 */
28
29/*#define	CMX_DEBUG*/
30/*#define	CMX_INTR*/
31
32#define	CMX_MIN_RDLEN	10		/* min read length */
33#define	CMX_MIN_WRLEN	5		/* min write length */
34#define	CMX_BUFSZ	512		/* I/O block size */
35
36struct cmx_softc {
37	device_t dev;			/* pccard device */
38	struct cdev *cdev;		/* character device */
39
40	struct resource *ioport;	/* io port resource descriptor */
41	int ioport_rid;			/* io port resource identification */
42
43	bus_space_tag_t bst;		/* bus space tag */
44	bus_space_handle_t bsh;		/* bus space handle */
45
46#ifdef CMX_INTR
47	struct resource* irq;		/* irq resource descriptor */
48	int irq_rid;			/* irq resource identification */
49	void *ih;			/* intr handle */
50#endif
51
52	struct mtx mtx;			/* per-unit lock */
53	struct callout ch;		/* callout handle */
54	struct selinfo sel;		/* select/poll queue handle */
55
56	int open;			/* is chardev open? */
57	int polling;			/* are we polling? */
58	int dying;			/* are we detaching? */
59
60	unsigned long timeout;		/* response timeout */
61
62	uint8_t buf[CMX_BUFSZ];		/* read/write buffer */
63};
64
65extern devclass_t cmx_devclass;
66
67void	cmx_init_softc(device_t);
68int	cmx_alloc_resources(device_t);
69void	cmx_release_resources(device_t);
70int	cmx_attach(device_t);
71int	cmx_detach(device_t);
72
73#define	CMX_READ_1(sc, off)						\
74	(bus_space_read_1((sc)->bst, (sc)->bsh, off))
75#define	CMX_WRITE_1(sc, off, val)					\
76	(bus_space_write_1((sc)->bst, (sc)->bsh, off, val))
77
78#define	cmx_read_BSR(sc)						\
79	CMX_READ_1(sc, REG_OFFSET_BSR)
80#define	cmx_write_BSR(sc, val)						\
81	CMX_WRITE_1(sc, REG_OFFSET_BSR, val)
82#define	cmx_read_SCR(sc)						\
83	CMX_READ_1(sc, REG_OFFSET_SCR)
84#define	cmx_write_SCR(sc, val)						\
85	CMX_WRITE_1(sc, REG_OFFSET_SCR, val)
86#define	cmx_read_DTR(sc)						\
87	CMX_READ_1(sc, REG_OFFSET_DTR)
88#define	cmx_write_DTR(sc, val)						\
89	CMX_WRITE_1(sc, REG_OFFSET_DTR, val)
90
91#define	cmx_test(byte, flags, test)					\
92	(((byte) & (flags)) == ((test) ? (flags) : 0))
93
94#define	cmx_test_BSR(sc, flags, test)					\
95	cmx_test(cmx_read_BSR(sc), flags, test)
96
97#define	CMX_LOCK(sc)			mtx_lock(&(sc)->mtx)
98#define	CMX_UNLOCK(sc)			mtx_unlock(&(sc)->mtx)
99#define	CMX_LOCK_ASSERT(sc, what)	mtx_assert(&(sc)->mtx, (what))
100