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