smb.c revision 129290
1/*-
2 * Copyright (c) 1998, 2001 Nicolas Souchu
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: head/sys/dev/smbus/smb.c 129290 2004-05-16 21:18:45Z joerg $
27 *
28 */
29#include <sys/param.h>
30#include <sys/kernel.h>
31#include <sys/systm.h>
32#include <sys/module.h>
33#include <sys/bus.h>
34#include <sys/conf.h>
35#include <sys/uio.h>
36#include <sys/fcntl.h>
37
38#include <dev/smbus/smbconf.h>
39#include <dev/smbus/smbus.h>
40#include <dev/smbus/smb.h>
41
42#include "smbus_if.h"
43
44#define BUFSIZE 1024
45
46struct smb_softc {
47
48	int sc_count;			/* >0 if device opened */
49	dev_t sc_devnode;
50};
51
52#define IIC_SOFTC(unit) \
53	((struct smb_softc *)devclass_get_softc(smb_devclass, (unit)))
54
55#define IIC_DEVICE(unit) \
56	(devclass_get_device(smb_devclass, (unit)))
57
58static int smb_probe(device_t);
59static int smb_attach(device_t);
60static int smb_detach(device_t);
61static void smb_identify(driver_t *driver, device_t parent);
62
63static devclass_t smb_devclass;
64
65static device_method_t smb_methods[] = {
66	/* device interface */
67	DEVMETHOD(device_identify,	smb_identify),
68	DEVMETHOD(device_probe,		smb_probe),
69	DEVMETHOD(device_attach,	smb_attach),
70	DEVMETHOD(device_detach,	smb_detach),
71
72	/* smbus interface */
73	DEVMETHOD(smbus_intr,		smbus_generic_intr),
74
75	{ 0, 0 }
76};
77
78static driver_t smb_driver = {
79	"smb",
80	smb_methods,
81	sizeof(struct smb_softc),
82};
83
84static	d_open_t	smbopen;
85static	d_close_t	smbclose;
86static	d_write_t	smbwrite;
87static	d_read_t	smbread;
88static	d_ioctl_t	smbioctl;
89
90static struct cdevsw smb_cdevsw = {
91	.d_version =	D_VERSION,
92	.d_flags =	D_NEEDGIANT,
93	.d_open =	smbopen,
94	.d_close =	smbclose,
95	.d_read =	smbread,
96	.d_write =	smbwrite,
97	.d_ioctl =	smbioctl,
98	.d_name =	"smb",
99};
100
101static void
102smb_identify(driver_t *driver, device_t parent)
103{
104	BUS_ADD_CHILD(parent, 0, "smb", -1);
105}
106
107static int
108smb_probe(device_t dev)
109{
110	device_set_desc(dev, "SMBus generic I/O");
111
112	return (0);
113}
114
115static int
116smb_attach(device_t dev)
117{
118	struct smb_softc *sc = (struct smb_softc *)device_get_softc(dev);
119
120	if (!sc)
121		return (ENOMEM);
122
123	bzero(sc, sizeof(struct smb_softc *));
124
125	sc->sc_devnode = make_dev(&smb_cdevsw, device_get_unit(dev),
126			UID_ROOT, GID_WHEEL,
127			0600, "smb%d", device_get_unit(dev));
128
129	return (0);
130}
131
132static int
133smb_detach(device_t dev)
134{
135	struct smb_softc *sc = (struct smb_softc *)device_get_softc(dev);
136
137	if (sc->sc_devnode)
138		destroy_dev(sc->sc_devnode);
139
140	return (0);
141}
142
143static int
144smbopen (dev_t dev, int flags, int fmt, struct thread *td)
145{
146	struct smb_softc *sc = IIC_SOFTC(minor(dev));
147
148	if (!sc)
149		return (EINVAL);
150
151	if (sc->sc_count)
152		return (EBUSY);
153
154	sc->sc_count++;
155
156	return (0);
157}
158
159static int
160smbclose(dev_t dev, int flags, int fmt, struct thread *td)
161{
162	struct smb_softc *sc = IIC_SOFTC(minor(dev));
163
164	if (!sc)
165		return (EINVAL);
166
167	if (!sc->sc_count)
168		return (EINVAL);
169
170	sc->sc_count--;
171
172	return (0);
173}
174
175static int
176smbwrite(dev_t dev, struct uio * uio, int ioflag)
177{
178	/* not supported */
179
180	return (EINVAL);
181}
182
183static int
184smbread(dev_t dev, struct uio * uio, int ioflag)
185{
186	/* not supported */
187
188	return (EINVAL);
189}
190
191static int
192smbioctl(dev_t dev, u_long cmd, caddr_t data, int flags, struct thread *td)
193{
194	device_t smbdev = IIC_DEVICE(minor(dev));
195	struct smb_softc *sc = IIC_SOFTC(minor(dev));
196	device_t parent = device_get_parent(smbdev);
197	char buf[SMB_MAXBLOCKSIZE];
198	char c;
199	short w;
200
201	int error = 0;
202	struct smbcmd *s = (struct smbcmd *)data;
203
204	if (!sc || !s)
205		return (EINVAL);
206
207	/* allocate the bus */
208	if ((error = smbus_request_bus(parent, smbdev,
209			(flags & O_NONBLOCK) ? SMB_DONTWAIT : (SMB_WAIT | SMB_INTR))))
210		return (error);
211
212	switch (cmd) {
213	case SMB_QUICK_WRITE:
214		error = smbus_error(smbus_quick(parent, s->slave, SMB_QWRITE));
215		break;
216
217	case SMB_QUICK_READ:
218		error = smbus_error(smbus_quick(parent, s->slave, SMB_QREAD));
219		break;
220
221	case SMB_SENDB:
222		error = smbus_error(smbus_sendb(parent, s->slave, s->cmd));
223		break;
224
225	case SMB_RECVB:
226		error = smbus_error(smbus_recvb(parent, s->slave, &s->cmd));
227		break;
228
229	case SMB_WRITEB:
230		error = smbus_error(smbus_writeb(parent, s->slave, s->cmd,
231						s->data.byte));
232		break;
233
234	case SMB_WRITEW:
235		error = smbus_error(smbus_writew(parent, s->slave,
236						s->cmd, s->data.word));
237		break;
238
239	case SMB_READB:
240		if (s->data.byte_ptr) {
241			error = smbus_error(smbus_readb(parent, s->slave,
242						s->cmd, &c));
243			if (error)
244				break;
245			error = copyout(&c, s->data.byte_ptr,
246					sizeof(*(s->data.byte_ptr)));
247		}
248		break;
249
250	case SMB_READW:
251		if (s->data.word_ptr) {
252			error = smbus_error(smbus_readw(parent, s->slave,
253						s->cmd, &w));
254			if (error == 0) {
255				error = copyout(&w, s->data.word_ptr,
256						sizeof(*(s->data.word_ptr)));
257			}
258		}
259		break;
260
261	case SMB_PCALL:
262		if (s->data.process.rdata) {
263
264			error = smbus_error(smbus_pcall(parent, s->slave, s->cmd,
265				s->data.process.sdata, &w));
266			if (error)
267				break;
268			error = copyout(&w, s->data.process.rdata,
269					sizeof(*(s->data.process.rdata)));
270		}
271
272		break;
273
274	case SMB_BWRITE:
275		if (s->count && s->data.byte_ptr) {
276			if (s->count > SMB_MAXBLOCKSIZE)
277				s->count = SMB_MAXBLOCKSIZE;
278			error = copyin(s->data.byte_ptr, buf, s->count);
279			if (error)
280				break;
281			error = smbus_error(smbus_bwrite(parent, s->slave,
282						s->cmd, s->count, buf));
283		}
284		break;
285
286	case SMB_BREAD:
287		if (s->count && s->data.byte_ptr) {
288			if (s->count > SMB_MAXBLOCKSIZE)
289				s->count = SMB_MAXBLOCKSIZE;
290			error = smbus_error(smbus_bread(parent, s->slave,
291						s->cmd, s->count, buf));
292			if (error)
293				break;
294			error = copyout(buf, s->data.byte_ptr, s->count);
295		}
296		break;
297
298	default:
299		error = ENOTTY;
300	}
301
302	/* release the bus */
303	smbus_release_bus(parent, smbdev);
304
305	return (error);
306}
307
308DRIVER_MODULE(smb, smbus, smb_driver, smb_devclass, 0, 0);
309MODULE_DEPEND(smb, smbus, SMBUS_MINVER, SMBUS_PREFVER, SMBUS_MAXVER);
310MODULE_VERSION(smb, 1);
311