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