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