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.2 1998/09/04 17:53:42 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 49 struct 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 66 static int smb_probe(device_t); 67 static int smb_attach(device_t); 68 69 static devclass_t smb_devclass; 70 71 static 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 82 static driver_t smb_driver = { 83 "smb", 84 smb_methods, 85 DRIVER_TYPE_MISC, 86 sizeof(struct smb_softc), 87 }; 88 89 static d_open_t smbopen; 90 static d_close_t smbclose; 91 static d_write_t smbwrite; 92 static d_read_t smbread; 93 static d_ioctl_t smbioctl; 94 95 #define CDEV_MAJOR 106 96 static 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 */ 104 static int 105 smb_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 */ 119 static int 120 smb_attach(device_t dev) 121 { 122 struct smb_softc *sc = (struct smb_softc *)device_get_softc(dev); 123 124 return (0); 125 } 126 127 static int 128 smbopen (dev_t dev, int flags, int fmt, struct proc *p) 129 { 130 struct smb_softc *sc = IIC_SOFTC(minor(dev)); 131 132 if (!sc) 133 return (EINVAL); 134 135 if (sc->sc_count) 136 return (EBUSY); 137 138 sc->sc_count++; 139 140 return (0); 141 } 142 143 static int 144 smbclose(dev_t dev, int flags, int fmt, struct proc *p) 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 (EINVAL); 153 154 sc->sc_count--; 155 156 return (0); 157 } 158 159 static int 160 smbwrite(dev_t dev, struct uio * uio, int ioflag) 161 { 162 device_t smbdev = IIC_DEVICE(minor(dev)); 163 struct smb_softc *sc = IIC_SOFTC(minor(dev)); 164 int error, count; 165 166 if (!sc || !smbdev) 167 return (EINVAL); 168 169 if (sc->sc_count == 0) 170 return (EINVAL); 171 172 count = min(uio->uio_resid, BUFSIZE); 173 uiomove(sc->sc_buffer, count, uio); 174 175 /* we consider the command char as the first character to send */ 176 smbus_bwrite(device_get_parent(smbdev), sc->sc_addr, 177 sc->sc_buffer[0], count-1, sc->sc_buffer+1); 178 179 return (0); 180 } 181 182 static int 183 smbread(dev_t dev, struct uio * uio, int ioflag) 184 { 185 /* not supported */ 186 187 return (EINVAL); 188 } 189 190 static int 191 smbioctl(dev_t dev, u_long cmd, caddr_t data, int flags, struct proc *p) 192 { 193 device_t smbdev = IIC_DEVICE(minor(dev)); 194 struct smb_softc *sc = IIC_SOFTC(minor(dev)); 195 device_t parent = device_get_parent(smbdev); 196 197 int error = 0; 198 struct smbcmd *s = (struct smbcmd *)data; 199 200 if (!sc) 201 return (EINVAL); 202 203 switch (cmd) { 204 case SMB_QUICK_WRITE: 205 smbus_quick(parent, sc->sc_addr, SMB_QWRITE); 206 goto end; 207 208 case SMB_QUICK_READ: 209 smbus_quick(parent, sc->sc_addr, SMB_QREAD); 210 goto end; 211 }; 212 213 if (!s) 214 return (EINVAL); 215 216 switch (cmd) { 217 case SMB_SENDB: 218 smbus_sendb(parent, sc->sc_addr, s->cmd); 219 break; 220 221 case SMB_RECVB: 222 smbus_recvb(parent, sc->sc_addr, &s->cmd); 223 break; 224 225 case SMB_WRITEB: 226 smbus_writeb(parent, sc->sc_addr, s->cmd, s->data.byte); 227 break; 228 229 case SMB_WRITEW: 230 smbus_writew(parent, sc->sc_addr, s->cmd, s->data.word); 231 break; 232 233 case SMB_READB: 234 if (s->data.byte_ptr) 235 smbus_readb(parent, sc->sc_addr, s->cmd, 236 s->data.byte_ptr); 237 break; 238 239 case SMB_READW: 240 if (s->data.word_ptr) 241 smbus_readw(parent, sc->sc_addr, s->cmd, s->data.word_ptr); 242 break; 243 244 case SMB_PCALL: 245 if (s->data.process.rdata) 246 smbus_pcall(parent, sc->sc_addr, 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 smbus_bwrite(parent, sc->sc_addr, s->cmd, s->count, 253 s->data.byte_ptr); 254 break; 255 256 case SMB_BREAD: 257 if (s->count && s->data.byte_ptr) 258 smbus_bread(parent, sc->sc_addr, s->cmd, s->count, 259 s->data.byte_ptr); 260 break; 261 262 default: 263 error = ENODEV; 264 } 265 266 end: 267 return (error); 268 } 269 270 static int smb_devsw_installed = 0; 271 272 static void 273 smb_drvinit(void *unused) 274 { 275 dev_t dev; 276 277 if( ! smb_devsw_installed ) { 278 dev = makedev(CDEV_MAJOR,0); 279 cdevsw_add(&dev,&smb_cdevsw,NULL); 280 smb_devsw_installed = 1; 281 } 282 } 283 284 CDEV_DRIVER_MODULE(smb, smbus, smb_driver, smb_devclass, CDEV_MAJOR, 285 smb_cdevsw, 0, 0); 286 287 SYSINIT(smbdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR,smb_drvinit,NULL) 288