summaryrefslogtreecommitdiff
path: root/hardsid.c
blob: 6a71bca314eeb9a2d4a83061dfd4ce6a0027b14a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
/* Copyright 2018 Ian Cowburn

   Redistribution and use in source and binary forms, with or without
   modification, are permitted provided that the following conditions are met:

    1. Redistributions of source code must retain the above copyright notice,
       this list of conditions and the following disclaimer.

    2. Redistributions in binary form must reproduce the above copyright notice,
       this list of conditions and the following disclaimer in the documentation
       and/or other materials provided with the distribution.

   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
   AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
   LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
   CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
   SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
   INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
   CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
   ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
   POSSIBILITY OF SUCH DAMAGE.
*/
#include <sys/param.h>
#include <sys/module.h>
#include <sys/systm.h>
#include <sys/errno.h>
#include <sys/kernel.h>
#include <sys/conf.h>
#include <sys/uio.h>
#include <sys/malloc.h>
#include <sys/bus.h>
#include <sys/param.h>
#include <sys/lock.h>
#include <sys/mutex.h>

#include <machine/bus.h>
#include <sys/rman.h>
#include <machine/resource.h>

#include <dev/pci/pcivar.h>
#include <dev/pci/pcireg.h>

/* The maximum amount we support in a write call
*/
#define MAXWRITEBUFF	256

/* The softc holds our per-instance data.
*/
struct hardsid_softc
{
    device_t		dev;
    struct cdev		*cdev;
    int			rid;
    struct mtx		lock;
    struct resource	*res;
    bus_space_tag_t	res_bt;
    bus_space_handle_t	res_bh;
    int			opened;
    int			reg;
};

/* Function prototypes
*/
static d_open_t		hardsid_open;
static d_close_t	hardsid_close;
static d_read_t		hardsid_read;
static d_write_t	hardsid_write;

/* Character device entry points
*/
static struct cdevsw hardsid_cdevsw =
{
    .d_version =	D_VERSION,
    .d_open =		hardsid_open,
    .d_close =		hardsid_close,
    .d_read =		hardsid_read,
    .d_write =		hardsid_write,
    .d_name =		"hardsid",
};

/* Set a SID register
*/
static void SetSIDReg(struct hardsid_softc *sc, int reg, int value)
{
    //bus_space_write_2(sc->res_bt, sc->res_bh, 2, reg << 8 | value);
    bus_space_write_1(sc->res_bt, sc->res_bh, 2, value);
    bus_space_write_1(sc->res_bt, sc->res_bh, 3, reg);
    tsleep(sc, 0, "sleep", hz/200);
}

int hardsid_open(struct cdev *dev, int oflags, int devtype, struct thread *td)
{
    struct hardsid_softc *sc;
    int ret;

    sc = dev->si_drv1;

    mtx_lock(&sc->lock);

    if (sc->opened)
    {
    	ret = EACCES;
    }
    else
    {
	ret = 0;
	sc->opened = 1;
	sc->reg = -1;
    }

    mtx_unlock(&sc->lock);

    return ret;
}

int hardsid_close(struct cdev *dev, int fflag, int devtype, struct thread *td)
{
    struct hardsid_softc *sc;

    sc = dev->si_drv1;

    mtx_lock(&sc->lock);
    sc->opened = 0;
    mtx_unlock(&sc->lock);

    return 0;
}

int hardsid_read(struct cdev *dev, struct uio *uio, int ioflag)
{
    struct hardsid_softc *sc;

    sc = dev->si_drv1;

    return 0;
}

int hardsid_write(struct cdev *dev, struct uio *uio, int ioflag)
{
    struct hardsid_softc *sc;
    unsigned char buff[MAXWRITEBUFF];
    int len;
    int f;

    if (uio->uio_resid > MAXWRITEBUFF)
    {
    	return EINVAL;
    }

    sc = dev->si_drv1;

    len = uio->uio_resid;

    uiomove(buff, len, uio);

    for(f = 0; f < len; f++)
    {
	if (sc->reg == -1)
	{
	    sc->reg = buff[f];
	}
	else
	{
	    SetSIDReg(sc, sc->reg, buff[f]);
	    sc->reg = -1;
	}
    }

    return 0;
}

/* PCI Support Functions */

static int hardsid_probe(device_t dev)
{
    if ((pci_get_vendor(dev) == 0x6581) && (pci_get_device(dev) == 0x8580))
    {
	device_set_desc(dev, "hardsid");
	return BUS_PROBE_DEFAULT;
    }

    return ENXIO;
}

/* Attach function is only called if the probe is successful.
*/
static int hardsid_attach(device_t dev)
{
    struct hardsid_softc *sc;

    /* Look up our softc and initialize its fields.
    */
    sc = device_get_softc(dev);
    sc->dev = dev;
    sc->opened = 0;
    sc->rid = PCIR_BAR(0);
    sc->res = bus_alloc_resource_any(dev, SYS_RES_IOPORT, &sc->rid, RF_ACTIVE);
    mtx_init(&sc->lock, "hardsid", NULL, MTX_DEF);

    if (sc->res == NULL)
    {
    	device_printf(dev, "couldn't configure PCI IO resources\n");
	return ENXIO;
    }

    sc->res_bt = rman_get_bustag(sc->res);
    sc->res_bh = rman_get_bushandle(sc->res);

    /*Create a /dev entry for this device.
    */
    sc->cdev = make_dev(&hardsid_cdevsw,
			device_get_unit(dev),
			UID_ROOT, GID_WHEEL, 0666,
			"hardsid%u", device_get_unit(dev));

    sc->cdev->si_drv1 = sc;

    return 0;
}

/* Detach device.
*/
static int hardsid_detach(device_t dev)
{
    struct hardsid_softc *sc;

    /* Teardown the state in our softc created in our attach routine.
    */
    sc = device_get_softc(dev);
    bus_release_resource(dev, SYS_RES_IOPORT, sc->rid, sc->res);
    destroy_dev(sc->cdev);

    return 0;
}

/* Called during system shutdown after sync.
*/
static int hardsid_shutdown(device_t dev)
{
    return 0;
}

/* Device suspend routine.
*/
static int hardsid_suspend(device_t dev)
{
    return 0;
}

/* Device resume routine.
*/
static int hardsid_resume(device_t dev)
{
    return 0;
}

static device_method_t hardsid_methods[] =
{
    /* Device interface
    */
    DEVMETHOD(device_probe,	hardsid_probe),
    DEVMETHOD(device_attach,	hardsid_attach),
    DEVMETHOD(device_detach,	hardsid_detach),
    DEVMETHOD(device_shutdown,	hardsid_shutdown),
    DEVMETHOD(device_suspend,	hardsid_suspend),
    DEVMETHOD(device_resume,	hardsid_resume),

    DEVMETHOD_END
};

static devclass_t hardsid_devclass;

DEFINE_CLASS_0(hardsid, hardsid_driver, hardsid_methods,
		sizeof(struct hardsid_softc));
DRIVER_MODULE(hardsid, pci, hardsid_driver, hardsid_devclass, 0, 0);