smb.c revision 59368
1/*-
2 * Copyright (c) 1998 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 59368 2000-04-18 15:15:39Z phk $
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/malloc.h>
37#include <sys/fcntl.h>
38
39#include <machine/clock.h>
40
41#include <dev/smbus/smbconf.h>
42#include <dev/smbus/smbus.h>
43#include <machine/smb.h>
44
45#include "smbus_if.h"
46
47#define BUFSIZE 1024
48
49struct smb_softc {
50
51	int sc_addr;			/* address on smbus */
52	int sc_count;			/* >0 if device opened */
53
54	char *sc_cp;			/* output buffer pointer */
55
56	char sc_buffer[BUFSIZE];	/* output buffer */
57	char sc_inbuf[BUFSIZE];		/* input buffer */
58};
59
60#define IIC_SOFTC(unit) \
61	((struct smb_softc *)devclass_get_softc(smb_devclass, (unit)))
62
63#define IIC_DEVICE(unit) \
64	(devclass_get_device(smb_devclass, (unit)))
65
66static int smb_probe(device_t);
67static int smb_attach(device_t);
68
69static devclass_t smb_devclass;
70
71static device_method_t smb_methods[] = {
72	/* device interface */
73	DEVMETHOD(device_probe,		smb_probe),
74	DEVMETHOD(device_attach,	smb_attach),
75
76	/* smbus interface */
77	DEVMETHOD(smbus_intr,		smbus_generic_intr),
78
79	{ 0, 0 }
80};
81
82static driver_t smb_driver = {
83	"smb",
84	smb_methods,
85	sizeof(struct smb_softc),
86};
87
88static	d_open_t	smbopen;
89static	d_close_t	smbclose;
90static	d_write_t	smbwrite;
91static	d_read_t	smbread;
92static	d_ioctl_t	smbioctl;
93
94#define CDEV_MAJOR 106
95static struct cdevsw smb_cdevsw = {
96	/* open */	smbopen,
97	/* close */	smbclose,
98	/* read */	smbread,
99	/* write */	smbwrite,
100	/* ioctl */	smbioctl,
101	/* poll */	nopoll,
102	/* mmap */	nommap,
103	/* strategy */	nostrategy,
104	/* name */	"smb",
105	/* maj */	CDEV_MAJOR,
106	/* dump */	nodump,
107	/* psize */	nopsize,
108	/* flags */	0,
109	/* bmaj */	-1
110};
111
112/*
113 * smbprobe()
114 */
115static int
116smb_probe(device_t dev)
117{
118	struct smb_softc *sc = (struct smb_softc *)device_get_softc(dev);
119
120	sc->sc_addr = smbus_get_addr(dev);
121
122	/* XXX detect chip with start/stop conditions */
123
124	return (0);
125}
126
127/*
128 * smbattach()
129 */
130static int
131smb_attach(device_t dev)
132{
133	make_dev(&smb_cdevsw, device_get_unit(dev),	/* XXX cleanup */
134			UID_ROOT, GID_WHEEL,
135			0600, "smb%d", device_get_unit(dev));
136	return (0);
137}
138
139static int
140smbopen (dev_t dev, int flags, int fmt, struct proc *p)
141{
142	struct smb_softc *sc = IIC_SOFTC(minor(dev));
143
144	if (!sc)
145		return (EINVAL);
146
147	if (sc->sc_count)
148		return (EBUSY);
149
150	sc->sc_count++;
151
152	return (0);
153}
154
155static int
156smbclose(dev_t dev, int flags, int fmt, struct proc *p)
157{
158	struct smb_softc *sc = IIC_SOFTC(minor(dev));
159
160	if (!sc)
161		return (EINVAL);
162
163	if (!sc->sc_count)
164		return (EINVAL);
165
166	sc->sc_count--;
167
168	return (0);
169}
170
171static int
172smbwrite(dev_t dev, struct uio * uio, int ioflag)
173{
174	/* not supported */
175
176	return (EINVAL);
177}
178
179static int
180smbread(dev_t dev, struct uio * uio, int ioflag)
181{
182	/* not supported */
183
184	return (EINVAL);
185}
186
187static int
188smbioctl(dev_t dev, u_long cmd, caddr_t data, int flags, struct proc *p)
189{
190	device_t smbdev = IIC_DEVICE(minor(dev));
191	struct smb_softc *sc = IIC_SOFTC(minor(dev));
192	device_t parent = device_get_parent(smbdev);
193
194	int error = 0;
195	struct smbcmd *s = (struct smbcmd *)data;
196
197	if (!sc || !s)
198		return (EINVAL);
199
200	/* allocate the bus */
201	if ((error = smbus_request_bus(parent, smbdev,
202			(flags & O_NONBLOCK) ? SMB_DONTWAIT : (SMB_WAIT | SMB_INTR))))
203		return (error);
204
205	switch (cmd) {
206	case SMB_QUICK_WRITE:
207		error = smbus_error(smbus_quick(parent, s->slave, SMB_QWRITE));
208		break;
209
210	case SMB_QUICK_READ:
211		error = smbus_error(smbus_quick(parent, s->slave, SMB_QREAD));
212		break;
213
214	case SMB_SENDB:
215		error = smbus_error(smbus_sendb(parent, s->slave, s->cmd));
216		break;
217
218	case SMB_RECVB:
219		error = smbus_error(smbus_recvb(parent, s->slave, &s->cmd));
220		break;
221
222	case SMB_WRITEB:
223		error = smbus_error(smbus_writeb(parent, s->slave, s->cmd,
224						s->data.byte));
225		break;
226
227	case SMB_WRITEW:
228		error = smbus_error(smbus_writew(parent, s->slave,
229						s->cmd, s->data.word));
230		break;
231
232	case SMB_READB:
233		if (s->data.byte_ptr)
234			error = smbus_error(smbus_readb(parent, s->slave,
235						s->cmd, s->data.byte_ptr));
236		break;
237
238	case SMB_READW:
239		if (s->data.word_ptr)
240			error = smbus_error(smbus_readw(parent, s->slave,
241						s->cmd, s->data.word_ptr));
242		break;
243
244	case SMB_PCALL:
245		if (s->data.process.rdata)
246			error = smbus_error(smbus_pcall(parent, s->slave, s->cmd,
247				s->data.process.sdata, s->data.process.rdata));
248		break;
249
250	case SMB_BWRITE:
251		if (s->count && s->data.byte_ptr)
252			error = smbus_error(smbus_bwrite(parent, s->slave,
253						s->cmd, s->count, s->data.byte_ptr));
254		break;
255
256	case SMB_BREAD:
257		if (s->count && s->data.byte_ptr)
258			error = smbus_error(smbus_bread(parent, s->slave,
259						s->cmd, s->count, s->data.byte_ptr));
260		break;
261
262	default:
263		error = ENODEV;
264	}
265
266	/* release the bus */
267	smbus_release_bus(parent, smbdev);
268
269	return (error);
270}
271
272DRIVER_MODULE(smb, smbus, smb_driver, smb_devclass, 0, 0);
273