smb.c revision 41591
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 *	$Id: smb.c,v 1.3 1998/09/09 18:57:38 nsouch Exp $
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/buf.h>
36#include <sys/uio.h>
37#include <sys/malloc.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	DRIVER_TYPE_MISC,
86	sizeof(struct smb_softc),
87};
88
89static	d_open_t	smbopen;
90static	d_close_t	smbclose;
91static	d_write_t	smbwrite;
92static	d_read_t	smbread;
93static	d_ioctl_t	smbioctl;
94
95#define CDEV_MAJOR 106
96static struct cdevsw smb_cdevsw =
97	{ smbopen,	smbclose,	smbread,	smbwrite,	/*106*/
98	  smbioctl,	nullstop,	nullreset,	nodevtotty,	/*smb*/
99	  seltrue,	nommap,		nostrat,	"smb",	NULL,	-1 };
100
101/*
102 * smbprobe()
103 */
104static int
105smb_probe(device_t dev)
106{
107	struct smb_softc *sc = (struct smb_softc *)device_get_softc(dev);
108
109	sc->sc_addr = smbus_get_addr(dev);
110
111	/* XXX detect chip with start/stop conditions */
112
113	return (0);
114}
115
116/*
117 * smbattach()
118 */
119static int
120smb_attach(device_t dev)
121{
122	return (0);
123}
124
125static int
126smbopen (dev_t dev, int flags, int fmt, struct proc *p)
127{
128	struct smb_softc *sc = IIC_SOFTC(minor(dev));
129
130	if (!sc)
131		return (EINVAL);
132
133	if (sc->sc_count)
134		return (EBUSY);
135
136	sc->sc_count++;
137
138	return (0);
139}
140
141static int
142smbclose(dev_t dev, int flags, int fmt, struct proc *p)
143{
144	struct smb_softc *sc = IIC_SOFTC(minor(dev));
145
146	if (!sc)
147		return (EINVAL);
148
149	if (!sc->sc_count)
150		return (EINVAL);
151
152	sc->sc_count--;
153
154	return (0);
155}
156
157static int
158smbwrite(dev_t dev, struct uio * uio, int ioflag)
159{
160	device_t smbdev = IIC_DEVICE(minor(dev));
161	struct smb_softc *sc = IIC_SOFTC(minor(dev));
162	int count;
163
164	if (!sc || !smbdev)
165		return (EINVAL);
166
167	if (sc->sc_count == 0)
168		return (EINVAL);
169
170	count = min(uio->uio_resid, BUFSIZE);
171	uiomove(sc->sc_buffer, count, uio);
172
173	/* we consider the command char as the first character to send */
174	smbus_bwrite(device_get_parent(smbdev), sc->sc_addr,
175				sc->sc_buffer[0], count-1, sc->sc_buffer+1);
176
177	return (0);
178}
179
180static int
181smbread(dev_t dev, struct uio * uio, int ioflag)
182{
183	/* not supported */
184
185	return (EINVAL);
186}
187
188static int
189smbioctl(dev_t dev, u_long cmd, caddr_t data, int flags, struct proc *p)
190{
191	device_t smbdev = IIC_DEVICE(minor(dev));
192	struct smb_softc *sc = IIC_SOFTC(minor(dev));
193	device_t parent = device_get_parent(smbdev);
194
195	int error = 0;
196	struct smbcmd *s = (struct smbcmd *)data;
197
198	if (!sc)
199		return (EINVAL);
200
201	switch (cmd) {
202	case SMB_QUICK_WRITE:
203		smbus_quick(parent, sc->sc_addr, SMB_QWRITE);
204		goto end;
205
206	case SMB_QUICK_READ:
207		smbus_quick(parent, sc->sc_addr, SMB_QREAD);
208		goto end;
209	};
210
211	if (!s)
212		return (EINVAL);
213
214	switch (cmd) {
215	case SMB_SENDB:
216		smbus_sendb(parent, sc->sc_addr, s->cmd);
217		break;
218
219	case SMB_RECVB:
220		smbus_recvb(parent, sc->sc_addr, &s->cmd);
221		break;
222
223	case SMB_WRITEB:
224		smbus_writeb(parent, sc->sc_addr, s->cmd, s->data.byte);
225		break;
226
227	case SMB_WRITEW:
228		smbus_writew(parent, sc->sc_addr, s->cmd, s->data.word);
229		break;
230
231	case SMB_READB:
232		if (s->data.byte_ptr)
233			smbus_readb(parent, sc->sc_addr, s->cmd,
234							s->data.byte_ptr);
235		break;
236
237	case SMB_READW:
238		if (s->data.word_ptr)
239			smbus_readw(parent, sc->sc_addr, s->cmd, s->data.word_ptr);
240		break;
241
242	case SMB_PCALL:
243		if (s->data.process.rdata)
244			smbus_pcall(parent, sc->sc_addr, s->cmd,
245				s->data.process.sdata, s->data.process.rdata);
246		break;
247
248	case SMB_BWRITE:
249		if (s->count && s->data.byte_ptr)
250			smbus_bwrite(parent, sc->sc_addr, s->cmd, s->count,
251							s->data.byte_ptr);
252		break;
253
254	case SMB_BREAD:
255		if (s->count && s->data.byte_ptr)
256			smbus_bread(parent, sc->sc_addr, s->cmd, s->count,
257							s->data.byte_ptr);
258		break;
259
260	default:
261		error = ENODEV;
262	}
263
264end:
265	return (error);
266}
267
268static int smb_devsw_installed = 0;
269
270static void
271smb_drvinit(void *unused)
272{
273        dev_t dev;
274
275        if( ! smb_devsw_installed ) {
276                dev = makedev(CDEV_MAJOR,0);
277                cdevsw_add(&dev,&smb_cdevsw,NULL);
278                smb_devsw_installed = 1;
279        }
280}
281
282CDEV_DRIVER_MODULE(smb, smbus, smb_driver, smb_devclass, CDEV_MAJOR,
283			smb_cdevsw, 0, 0);
284
285SYSINIT(smbdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR,smb_drvinit,NULL)
286