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