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