iic.c revision 38813
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.1.1.1 1998/09/03 20:51:50 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 49struct 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 64static int iic_probe(device_t); 65static int iic_attach(device_t); 66 67static devclass_t iic_devclass; 68 69static 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 80static driver_t iic_driver = { 81 "iic", 82 iic_methods, 83 DRIVER_TYPE_MISC, 84 sizeof(struct iic_softc), 85}; 86 87static d_open_t iicopen; 88static d_close_t iicclose; 89static d_write_t iicwrite; 90static d_read_t iicread; 91static d_ioctl_t iicioctl; 92 93#define CDEV_MAJOR 25 94static struct cdevsw iic_cdevsw = 95 { iicopen, iicclose, iicread, iicwrite, /*25*/ 96 iicioctl, nullstop, nullreset, nodevtotty, /*iic*/ 97 seltrue, nommap, nostrat, "iic", NULL, -1 }; 98 99/* 100 * iicprobe() 101 */ 102static int 103iic_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 */ 117static int 118iic_attach(device_t dev) 119{ 120 struct iic_softc *sc = (struct iic_softc *)device_get_softc(dev); 121 122 return (0); 123} 124 125static int 126iicopen (dev_t dev, int flags, int fmt, struct proc *p) 127{ 128 struct iic_softc *sc = IIC_SOFTC(minor(dev)); 129 130 if (!sc) 131 return (EINVAL); 132 133 if (sc->sc_count > 0) 134 return (EBUSY); 135 136 sc->sc_count++; 137 138 return (0); 139} 140 141static int 142iicclose(dev_t dev, int flags, int fmt, struct proc *p) 143{ 144 struct iic_softc *sc = IIC_SOFTC(minor(dev)); 145 146 if (!sc) 147 return (EINVAL); 148 149 if (!sc->sc_count) 150 return (EINVAL); 151 152 sc->sc_count--; 153 154 if (sc->sc_count < 0) 155 panic("%s: iic_count < 0!", __FUNCTION__); 156 157 return (0); 158} 159 160static int 161iicwrite(dev_t dev, struct uio * uio, int ioflag) 162{ 163 device_t iicdev = IIC_DEVICE(minor(dev)); 164 struct iic_softc *sc = IIC_SOFTC(minor(dev)); 165 int sent, error, count; 166 167 if (!sc || !iicdev) 168 return (EINVAL); 169 170 if (sc->sc_count == 0) 171 return (EINVAL); 172 173 count = min(uio->uio_resid, BUFSIZE); 174 uiomove(sc->sc_buffer, count, uio); 175 176 error = iicbus_block_write(device_get_parent(iicdev), sc->sc_addr, 177 sc->sc_buffer, count, &sent); 178 179 return(error); 180} 181 182static int 183iicread(dev_t dev, struct uio * uio, int ioflag) 184{ 185 device_t iicdev = IIC_DEVICE(minor(dev)); 186 struct iic_softc *sc = IIC_SOFTC(minor(dev)); 187 int len, error = 0; 188 int bufsize; 189 190 if (!sc || !iicdev) 191 return (EINVAL); 192 193 if (sc->sc_count == 0) 194 return (EINVAL); 195 196 /* max amount of data to read */ 197 len = min(uio->uio_resid, BUFSIZE); 198 199 if ((error = iicbus_block_read(device_get_parent(iicdev), sc->sc_addr, 200 sc->sc_inbuf, len, &bufsize))) 201 return (error); 202 203 if (bufsize > uio->uio_resid) 204 panic("%s: too much data read!", __FUNCTION__); 205 206 return (uiomove(sc->sc_inbuf, bufsize, uio)); 207} 208 209static int 210iicioctl(dev_t dev, u_long cmd, caddr_t data, int flags, struct proc *p) 211{ 212 device_t iicdev = IIC_DEVICE(minor(dev)); 213 struct iic_softc *sc = IIC_SOFTC(minor(dev)); 214 int error; 215 device_t parent = device_get_parent(iicdev); 216 217 if (!sc) 218 return (EINVAL); 219 220 switch (cmd) { 221 case I2CSTART: 222 error = iicbus_start(parent, sc->sc_addr); 223 break; 224 225 case I2CSTOP: 226 error = iicbus_stop(parent); 227 break; 228 229 case I2CRSTCARD: 230 error = iicbus_reset(parent, 0); 231 break; 232 233 default: 234 error = ENODEV; 235 } 236 237 return (error); 238} 239 240static int iic_devsw_installed = 0; 241 242static void 243iic_drvinit(void *unused) 244{ 245 dev_t dev; 246 247 if( ! iic_devsw_installed ) { 248 dev = makedev(CDEV_MAJOR,0); 249 cdevsw_add(&dev,&iic_cdevsw,NULL); 250 iic_devsw_installed = 1; 251 } 252} 253 254CDEV_DRIVER_MODULE(iic, iicbus, iic_driver, iic_devclass, CDEV_MAJOR, 255 iic_cdevsw, 0, 0); 256#if 0 257SYSINIT(iicdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR,iic_drvinit,NULL) 258#endif 259