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