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 *
| 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 $
| 26 * $Id: smb.c,v 1.4 1998/12/07 21:58:17 archie 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{
| 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;
| 160 /* not supported */
|
163
| 161
|
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);
| 162 return (EINVAL);
|
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
| 163} 164 165static int 166smbread(dev_t dev, struct uio * uio, int ioflag) 167{ 168 /* not supported */ 169 170 return (EINVAL); 171} 172 173static int 174smbioctl(dev_t dev, u_long cmd, caddr_t data, int flags, struct proc *p) 175{ 176 device_t smbdev = IIC_DEVICE(minor(dev)); 177 struct smb_softc *sc = IIC_SOFTC(minor(dev)); 178 device_t parent = device_get_parent(smbdev); 179 180 int error = 0; 181 struct smbcmd *s = (struct smbcmd *)data; 182
|
198 if (!sc)
| 183 if (!sc || !s)
|
199 return (EINVAL); 200 201 switch (cmd) { 202 case SMB_QUICK_WRITE:
| 184 return (EINVAL); 185 186 switch (cmd) { 187 case SMB_QUICK_WRITE:
|
203 smbus_quick(parent, sc->sc_addr, SMB_QWRITE);
| 188 smbus_quick(parent, s->slave, SMB_QWRITE);
|
204 goto end; 205 206 case SMB_QUICK_READ:
| 189 goto end; 190 191 case SMB_QUICK_READ:
|
207 smbus_quick(parent, sc->sc_addr, SMB_QREAD);
| 192 smbus_quick(parent, s->slave, SMB_QREAD);
|
208 goto end; 209 }; 210
| 193 goto end; 194 }; 195
|
211 if (!s) 212 return (EINVAL); 213
| |
214 switch (cmd) { 215 case SMB_SENDB:
| 196 switch (cmd) { 197 case SMB_SENDB:
|
216 smbus_sendb(parent, sc->sc_addr, s->cmd);
| 198 smbus_sendb(parent, s->slave, s->cmd);
|
217 break; 218 219 case SMB_RECVB:
| 199 break; 200 201 case SMB_RECVB:
|
220 smbus_recvb(parent, sc->sc_addr, &s->cmd);
| 202 smbus_recvb(parent, s->slave, &s->cmd);
|
221 break; 222 223 case SMB_WRITEB:
| 203 break; 204 205 case SMB_WRITEB:
|
224 smbus_writeb(parent, sc->sc_addr, s->cmd, s->data.byte);
| 206 smbus_writeb(parent, s->slave, s->cmd, s->data.byte);
|
225 break; 226 227 case SMB_WRITEW:
| 207 break; 208 209 case SMB_WRITEW:
|
228 smbus_writew(parent, sc->sc_addr, s->cmd, s->data.word);
| 210 smbus_writew(parent, s->slave, s->cmd, s->data.word);
|
229 break; 230 231 case SMB_READB: 232 if (s->data.byte_ptr)
| 211 break; 212 213 case SMB_READB: 214 if (s->data.byte_ptr)
|
233 smbus_readb(parent, sc->sc_addr, s->cmd,
| 215 smbus_readb(parent, s->slave, s->cmd,
|
234 s->data.byte_ptr); 235 break; 236 237 case SMB_READW: 238 if (s->data.word_ptr)
| 216 s->data.byte_ptr); 217 break; 218 219 case SMB_READW: 220 if (s->data.word_ptr)
|
239 smbus_readw(parent, sc->sc_addr, s->cmd, s->data.word_ptr);
| 221 smbus_readw(parent, s->slave, s->cmd, s->data.word_ptr);
|
240 break; 241 242 case SMB_PCALL: 243 if (s->data.process.rdata)
| 222 break; 223 224 case SMB_PCALL: 225 if (s->data.process.rdata)
|
244 smbus_pcall(parent, sc->sc_addr, s->cmd,
| 226 smbus_pcall(parent, s->slave, 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)
| 227 s->data.process.sdata, s->data.process.rdata); 228 break; 229 230 case SMB_BWRITE: 231 if (s->count && s->data.byte_ptr)
|
250 smbus_bwrite(parent, sc->sc_addr, s->cmd, s->count,
| 232 smbus_bwrite(parent, s->slave, s->cmd, s->count,
|
251 s->data.byte_ptr); 252 break; 253 254 case SMB_BREAD: 255 if (s->count && s->data.byte_ptr)
| 233 s->data.byte_ptr); 234 break; 235 236 case SMB_BREAD: 237 if (s->count && s->data.byte_ptr)
|
256 smbus_bread(parent, sc->sc_addr, s->cmd, s->count,
| 238 smbus_bread(parent, s->slave, 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)
| 239 s->data.byte_ptr); 240 break; 241 242 default: 243 error = ENODEV; 244 } 245 246end: 247 return (error); 248} 249 250static int smb_devsw_installed = 0; 251 252static void 253smb_drvinit(void *unused) 254{ 255 dev_t dev; 256 257 if( ! smb_devsw_installed ) { 258 dev = makedev(CDEV_MAJOR,0); 259 cdevsw_add(&dev,&smb_cdevsw,NULL); 260 smb_devsw_installed = 1; 261 } 262} 263 264CDEV_DRIVER_MODULE(smb, smbus, smb_driver, smb_devclass, CDEV_MAJOR, 265 smb_cdevsw, 0, 0); 266 267SYSINIT(smbdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR,smb_drvinit,NULL)
|