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: iic.c,v 1.5 1998/12/07 21:58:16 archie 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 39 #include <machine/clock.h> 40 41 #include <dev/iicbus/iiconf.h> 42 #include <dev/iicbus/iicbus.h> 43 44 #include <machine/iic.h> 45 46 #include "iicbus_if.h" 47 48 #define BUFSIZE 1024 49 50 struct iic_softc { 51 52 u_char sc_addr; /* address on iicbus */ 53 int sc_count; /* >0 if device opened */ 54 55 char sc_buffer[BUFSIZE]; /* output buffer */ 56 char sc_inbuf[BUFSIZE]; /* input buffer */ 57 }; 58 59 #define IIC_SOFTC(unit) \ 60 ((struct iic_softc *)devclass_get_softc(iic_devclass, (unit))) 61 62 #define IIC_DEVICE(unit) \ 63 (devclass_get_device(iic_devclass, (unit))) 64 65 static int iic_probe(device_t); 66 static int iic_attach(device_t); 67 68 static devclass_t iic_devclass; 69 70 static device_method_t iic_methods[] = { 71 /* device interface */ 72 DEVMETHOD(device_probe, iic_probe), 73 DEVMETHOD(device_attach, iic_attach), 74 75 /* iicbus interface */ 76 DEVMETHOD(iicbus_intr, iicbus_generic_intr), 77 78 { 0, 0 } 79 }; 80 81 static driver_t iic_driver = { 82 "iic", 83 iic_methods, 84 DRIVER_TYPE_MISC, 85 sizeof(struct iic_softc), 86 }; 87 88 static d_open_t iicopen; 89 static d_close_t iicclose; 90 static d_write_t iicwrite; 91 static d_read_t iicread; 92 static d_ioctl_t iicioctl; 93 94 #define CDEV_MAJOR 105 95 static struct cdevsw iic_cdevsw = 96 { iicopen, iicclose, iicread, iicwrite, /*105*/ 97 iicioctl, nullstop, nullreset, nodevtotty, /*iic*/ 98 seltrue, nommap, nostrat, "iic", NULL, -1 }; 99 100 /* 101 * iicprobe() 102 */ 103 static int 104 iic_probe(device_t dev) 105 { 106 struct iic_softc *sc = (struct iic_softc *)device_get_softc(dev); 107 108 sc->sc_addr = iicbus_get_addr(dev); 109 110 /* XXX detect chip with start/stop conditions */ 111 112 return (0); 113 } 114 115 /* 116 * iicattach() 117 */ 118 static int 119 iic_attach(device_t dev) 120 { 121 return (0); 122 } 123 124 static int 125 iicopen (dev_t dev, int flags, int fmt, struct proc *p) 126 { 127 struct iic_softc *sc = IIC_SOFTC(minor(dev)); 128 129 if (!sc) 130 return (EINVAL); 131 132 if (sc->sc_count > 0) 133 return (EBUSY); 134 135 sc->sc_count++; 136 137 return (0); 138 } 139 140 static int 141 iicclose(dev_t dev, int flags, int fmt, struct proc *p) 142 { 143 struct iic_softc *sc = IIC_SOFTC(minor(dev)); 144 145 if (!sc) 146 return (EINVAL); 147 148 if (!sc->sc_count) 149 return (EINVAL); 150 151 sc->sc_count--; 152 153 if (sc->sc_count < 0) 154 panic("%s: iic_count < 0!", __FUNCTION__); 155 156 return (0); 157 } 158 159 static int 160 iicwrite(dev_t dev, struct uio * uio, int ioflag) 161 { 162 device_t iicdev = IIC_DEVICE(minor(dev)); 163 struct iic_softc *sc = IIC_SOFTC(minor(dev)); 164 int sent, error, count; 165 166 if (!sc || !iicdev) 167 return (EINVAL); 168 169 if (sc->sc_count == 0) 170 return (EINVAL); 171 172 count = min(uio->uio_resid, BUFSIZE); 173 uiomove(sc->sc_buffer, count, uio); 174 175 error = iicbus_block_write(device_get_parent(iicdev), sc->sc_addr, 176 sc->sc_buffer, count, &sent); 177 178 return(error); 179 } 180 181 static int 182 iicread(dev_t dev, struct uio * uio, int ioflag) 183 { 184 device_t iicdev = IIC_DEVICE(minor(dev)); 185 struct iic_softc *sc = IIC_SOFTC(minor(dev)); 186 int len, error = 0; 187 int bufsize; 188 189 if (!sc || !iicdev) 190 return (EINVAL); 191 192 if (sc->sc_count == 0) 193 return (EINVAL); 194 195 /* max amount of data to read */ 196 len = min(uio->uio_resid, BUFSIZE); 197 198 if ((error = iicbus_block_read(device_get_parent(iicdev), sc->sc_addr, 199 sc->sc_inbuf, len, &bufsize))) 200 return (error); 201 202 if (bufsize > uio->uio_resid) 203 panic("%s: too much data read!", __FUNCTION__); 204 205 return (uiomove(sc->sc_inbuf, bufsize, uio)); 206 } 207 208 static int 209 iicioctl(dev_t dev, u_long cmd, caddr_t data, int flags, struct proc *p) 210 { 211 device_t iicdev = IIC_DEVICE(minor(dev)); 212 struct iic_softc *sc = IIC_SOFTC(minor(dev)); 213 device_t parent = device_get_parent(iicdev); 214 struct iiccmd *s = (struct iiccmd *)data; 215 int error, count; 216 217 if (!sc) 218 return (EINVAL); 219 220 switch (cmd) { 221 case I2CSTART: 222 error = iicbus_start(parent, s->slave, 0); 223 break; 224 225 case I2CSTOP: 226 error = iicbus_stop(parent); 227 break; 228 229 case I2CRSTCARD: 230 error = iicbus_reset(parent, 0, 0, NULL); 231 break; 232 233 case I2CWRITE: 234 error = iicbus_write(parent, s->buf, s->count, &count, 0); 235 break; 236 237 case I2CREAD: 238 error = iicbus_read(parent, s->buf, s->count, &count, s->last, 0); 239 break; 240 241 default: 242 error = ENODEV; 243 } 244 245 return (error); 246 } 247 248 static int iic_devsw_installed = 0; 249 250 static void 251 iic_drvinit(void *unused) 252 { 253 dev_t dev; 254 255 if( ! iic_devsw_installed ) { 256 dev = makedev(CDEV_MAJOR,0); 257 cdevsw_add(&dev,&iic_cdevsw,NULL); 258 iic_devsw_installed = 1; 259 } 260 } 261 262 CDEV_DRIVER_MODULE(iic, iicbus, iic_driver, iic_devclass, CDEV_MAJOR, 263 iic_cdevsw, 0, 0); 264 265 SYSINIT(iicdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR,iic_drvinit,NULL) 266