1 /*- 2 * Copyright (c) 1998, 2001 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$ 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/fcntl.h> 37 38 #include <dev/smbus/smbconf.h> 39 #include <dev/smbus/smbus.h> 40 #include <dev/smbus/smb.h> 41 42 #include "smbus_if.h" 43 44 #define BUFSIZE 1024 45 46 struct smb_softc { 47 48 int sc_count; /* >0 if device opened */ 49 struct cdev *sc_devnode; 50 }; 51 52 #define IIC_SOFTC(unit) \ 53 ((struct smb_softc *)devclass_get_softc(smb_devclass, (unit))) 54 55 #define IIC_DEVICE(unit) \ 56 (devclass_get_device(smb_devclass, (unit))) 57 58 static int smb_probe(device_t); 59 static int smb_attach(device_t); 60 static int smb_detach(device_t); 61 62 static devclass_t smb_devclass; 63 64 static device_method_t smb_methods[] = { 65 /* device interface */ 66 DEVMETHOD(device_probe, smb_probe), 67 DEVMETHOD(device_attach, smb_attach), 68 DEVMETHOD(device_detach, smb_detach), 69 70 /* smbus interface */ 71 DEVMETHOD(smbus_intr, smbus_generic_intr), 72 73 { 0, 0 } 74 }; 75 76 static driver_t smb_driver = { 77 "smb", 78 smb_methods, 79 sizeof(struct smb_softc), 80 }; 81 82 static d_open_t smbopen; 83 static d_close_t smbclose; 84 static d_ioctl_t smbioctl; 85 86 static struct cdevsw smb_cdevsw = { 87 .d_version = D_VERSION, 88 .d_flags = D_NEEDGIANT, 89 .d_open = smbopen, 90 .d_close = smbclose, 91 .d_ioctl = smbioctl, 92 .d_name = "smb", 93 }; 94 95 static int 96 smb_probe(device_t dev) 97 { 98 device_set_desc(dev, "SMBus generic I/O"); 99 100 return (0); 101 } 102 103 static int 104 smb_attach(device_t dev) 105 { 106 struct smb_softc *sc = (struct smb_softc *)device_get_softc(dev); 107 108 if (!sc) 109 return (ENOMEM); 110 111 bzero(sc, sizeof(struct smb_softc *)); 112 113 sc->sc_devnode = make_dev(&smb_cdevsw, device_get_unit(dev), 114 UID_ROOT, GID_WHEEL, 115 0600, "smb%d", device_get_unit(dev)); 116 117 return (0); 118 } 119 120 static int 121 smb_detach(device_t dev) 122 { 123 struct smb_softc *sc = (struct smb_softc *)device_get_softc(dev); 124 125 if (sc->sc_devnode) 126 destroy_dev(sc->sc_devnode); 127 128 return (0); 129 } 130 131 static int 132 smbopen (struct cdev *dev, int flags, int fmt, struct thread *td) 133 { 134 struct smb_softc *sc = IIC_SOFTC(minor(dev)); 135 136 if (sc == NULL) 137 return (ENXIO); 138 139 if (sc->sc_count != 0) 140 return (EBUSY); 141 142 sc->sc_count++; 143 144 return (0); 145 } 146 147 static int 148 smbclose(struct cdev *dev, int flags, int fmt, struct thread *td) 149 { 150 struct smb_softc *sc = IIC_SOFTC(minor(dev)); 151 152 if (sc == NULL) 153 return (ENXIO); 154 155 if (sc->sc_count == 0) 156 /* This is not supposed to happen. */ 157 return (0); 158 159 sc->sc_count--; 160 161 return (0); 162 } 163 164 static int 165 smbioctl(struct cdev *dev, u_long cmd, caddr_t data, int flags, struct thread *td) 166 { 167 char buf[SMB_MAXBLOCKSIZE]; 168 device_t parent; 169 struct smbcmd *s = (struct smbcmd *)data; 170 struct smb_softc *sc = IIC_SOFTC(minor(dev)); 171 device_t smbdev = IIC_DEVICE(minor(dev)); 172 int error; 173 short w; 174 char c; 175 176 if (sc == NULL) 177 return (ENXIO); 178 if (s == NULL) 179 return (EINVAL); 180 181 parent = device_get_parent(smbdev); 182 183 /* Allocate the bus. */ 184 if ((error = smbus_request_bus(parent, smbdev, 185 (flags & O_NONBLOCK) ? SMB_DONTWAIT : (SMB_WAIT | SMB_INTR)))) 186 return (error); 187 188 switch (cmd) { 189 case SMB_QUICK_WRITE: 190 error = smbus_error(smbus_quick(parent, s->slave, SMB_QWRITE)); 191 break; 192 193 case SMB_QUICK_READ: 194 error = smbus_error(smbus_quick(parent, s->slave, SMB_QREAD)); 195 break; 196 197 case SMB_SENDB: 198 error = smbus_error(smbus_sendb(parent, s->slave, s->cmd)); 199 break; 200 201 case SMB_RECVB: 202 error = smbus_error(smbus_recvb(parent, s->slave, &s->cmd)); 203 break; 204 205 case SMB_WRITEB: 206 error = smbus_error(smbus_writeb(parent, s->slave, s->cmd, 207 s->data.byte)); 208 break; 209 210 case SMB_WRITEW: 211 error = smbus_error(smbus_writew(parent, s->slave, 212 s->cmd, s->data.word)); 213 break; 214 215 case SMB_READB: 216 if (s->data.byte_ptr) { 217 error = smbus_error(smbus_readb(parent, s->slave, 218 s->cmd, &c)); 219 if (error) 220 break; 221 error = copyout(&c, s->data.byte_ptr, 222 sizeof(*(s->data.byte_ptr))); 223 } 224 break; 225 226 case SMB_READW: 227 if (s->data.word_ptr) { 228 error = smbus_error(smbus_readw(parent, s->slave, 229 s->cmd, &w)); 230 if (error == 0) { 231 error = copyout(&w, s->data.word_ptr, 232 sizeof(*(s->data.word_ptr))); 233 } 234 } 235 break; 236 237 case SMB_PCALL: 238 if (s->data.process.rdata) { 239 240 error = smbus_error(smbus_pcall(parent, s->slave, s->cmd, 241 s->data.process.sdata, &w)); 242 if (error) 243 break; 244 error = copyout(&w, s->data.process.rdata, 245 sizeof(*(s->data.process.rdata))); 246 } 247 248 break; 249 250 case SMB_BWRITE: 251 if (s->count && s->data.byte_ptr) { 252 if (s->count > SMB_MAXBLOCKSIZE) 253 s->count = SMB_MAXBLOCKSIZE; 254 error = copyin(s->data.byte_ptr, buf, s->count); 255 if (error) 256 break; 257 error = smbus_error(smbus_bwrite(parent, s->slave, 258 s->cmd, s->count, buf)); 259 } 260 break; 261 262 case SMB_BREAD: 263 if (s->count && s->data.byte_ptr) { 264 if (s->count > SMB_MAXBLOCKSIZE) 265 s->count = SMB_MAXBLOCKSIZE; 266 error = smbus_error(smbus_bread(parent, s->slave, 267 s->cmd, s->count, buf)); 268 if (error) 269 break; 270 error = copyout(buf, s->data.byte_ptr, s->count); 271 } 272 break; 273 274 default: 275 error = ENOTTY; 276 } 277 278 smbus_release_bus(parent, smbdev); 279 280 return (error); 281 } 282 283 DRIVER_MODULE(smb, smbus, smb_driver, smb_devclass, 0, 0); 284 MODULE_DEPEND(smb, smbus, SMBUS_MINVER, SMBUS_PREFVER, SMBUS_MAXVER); 285 MODULE_VERSION(smb, 1); 286