1/*- 2 * Copyright (c) 1998, 2001 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 *
| 1/*- 2 * Copyright (c) 1998, 2001 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 *
|
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#include <dev/iicbus/iiconf.h> 39#include <dev/iicbus/iicbus.h> 40#include <dev/iicbus/iic.h> 41 42#include "iicbus_if.h" 43 44#define BUFSIZE 1024 45 46struct iic_softc { 47 48 u_char sc_addr; /* 7 bit address on iicbus */ 49 int sc_count; /* >0 if device opened */ 50 51 char sc_buffer[BUFSIZE]; /* output buffer */ 52 char sc_inbuf[BUFSIZE]; /* input buffer */ 53 54 dev_t sc_devnode; 55}; 56 57#define IIC_SOFTC(unit) \ 58 ((struct iic_softc *)devclass_get_softc(iic_devclass, (unit))) 59 60#define IIC_DEVICE(unit) \ 61 (devclass_get_device(iic_devclass, (unit))) 62 63static int iic_probe(device_t); 64static int iic_attach(device_t); 65static int iic_detach(device_t); 66static void iic_identify(driver_t *driver, device_t parent); 67 68static devclass_t iic_devclass; 69 70static device_method_t iic_methods[] = { 71 /* device interface */ 72 DEVMETHOD(device_identify, iic_identify), 73 DEVMETHOD(device_probe, iic_probe), 74 DEVMETHOD(device_attach, iic_attach), 75 DEVMETHOD(device_detach, iic_detach), 76 77 /* iicbus interface */ 78 DEVMETHOD(iicbus_intr, iicbus_generic_intr), 79 80 { 0, 0 } 81}; 82 83static driver_t iic_driver = { 84 "iic", 85 iic_methods, 86 sizeof(struct iic_softc), 87}; 88 89static d_open_t iicopen; 90static d_close_t iicclose; 91static d_write_t iicwrite; 92static d_read_t iicread; 93static d_ioctl_t iicioctl; 94 95#define CDEV_MAJOR 105 96static struct cdevsw iic_cdevsw = {
| 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#include <dev/iicbus/iiconf.h> 39#include <dev/iicbus/iicbus.h> 40#include <dev/iicbus/iic.h> 41 42#include "iicbus_if.h" 43 44#define BUFSIZE 1024 45 46struct iic_softc { 47 48 u_char sc_addr; /* 7 bit address on iicbus */ 49 int sc_count; /* >0 if device opened */ 50 51 char sc_buffer[BUFSIZE]; /* output buffer */ 52 char sc_inbuf[BUFSIZE]; /* input buffer */ 53 54 dev_t sc_devnode; 55}; 56 57#define IIC_SOFTC(unit) \ 58 ((struct iic_softc *)devclass_get_softc(iic_devclass, (unit))) 59 60#define IIC_DEVICE(unit) \ 61 (devclass_get_device(iic_devclass, (unit))) 62 63static int iic_probe(device_t); 64static int iic_attach(device_t); 65static int iic_detach(device_t); 66static void iic_identify(driver_t *driver, device_t parent); 67 68static devclass_t iic_devclass; 69 70static device_method_t iic_methods[] = { 71 /* device interface */ 72 DEVMETHOD(device_identify, iic_identify), 73 DEVMETHOD(device_probe, iic_probe), 74 DEVMETHOD(device_attach, iic_attach), 75 DEVMETHOD(device_detach, iic_detach), 76 77 /* iicbus interface */ 78 DEVMETHOD(iicbus_intr, iicbus_generic_intr), 79 80 { 0, 0 } 81}; 82 83static driver_t iic_driver = { 84 "iic", 85 iic_methods, 86 sizeof(struct iic_softc), 87}; 88 89static d_open_t iicopen; 90static d_close_t iicclose; 91static d_write_t iicwrite; 92static d_read_t iicread; 93static d_ioctl_t iicioctl; 94 95#define CDEV_MAJOR 105 96static struct cdevsw iic_cdevsw = {
|
110}; 111 112static void 113iic_identify(driver_t *driver, device_t parent) 114{ 115 BUS_ADD_CHILD(parent, 0, "iic", 0); 116} 117 118static int 119iic_probe(device_t dev) 120{ 121 device_set_desc(dev, "I2C generic I/O"); 122 return (0); 123} 124 125static int 126iic_attach(device_t dev) 127{ 128 struct iic_softc *sc = (struct iic_softc *)device_get_softc(dev); 129 130 if (!sc) 131 return (ENOMEM); 132 133 bzero(sc, sizeof(struct iic_softc)); 134 135 sc->sc_devnode = make_dev(&iic_cdevsw, device_get_unit(dev), 136 UID_ROOT, GID_WHEEL, 137 0600, "iic%d", device_get_unit(dev)); 138 139 return (0); 140} 141 142static int 143iic_detach(device_t dev) 144{ 145 struct iic_softc *sc = (struct iic_softc *)device_get_softc(dev); 146 147 if (sc->sc_devnode) 148 destroy_dev(sc->sc_devnode); 149 150 return (0); 151} 152 153static int 154iicopen (dev_t dev, int flags, int fmt, struct thread *td) 155{ 156 struct iic_softc *sc = IIC_SOFTC(minor(dev)); 157 158 if (!sc) 159 return (EINVAL); 160 161 if (sc->sc_count > 0) 162 return (EBUSY); 163 164 sc->sc_count++; 165 166 return (0); 167} 168 169static int 170iicclose(dev_t dev, int flags, int fmt, struct thread *td) 171{ 172 struct iic_softc *sc = IIC_SOFTC(minor(dev)); 173 174 if (!sc) 175 return (EINVAL); 176 177 if (!sc->sc_count) 178 return (EINVAL); 179 180 sc->sc_count--; 181 182 if (sc->sc_count < 0) 183 panic("%s: iic_count < 0!", __func__); 184 185 return (0); 186} 187 188static int 189iicwrite(dev_t dev, struct uio * uio, int ioflag) 190{ 191 device_t iicdev = IIC_DEVICE(minor(dev)); 192 struct iic_softc *sc = IIC_SOFTC(minor(dev)); 193 int sent, error, count; 194 195 if (!sc || !iicdev || !sc->sc_addr) 196 return (EINVAL); 197 198 if (sc->sc_count == 0) 199 return (EINVAL); 200 201 if ((error = iicbus_request_bus(device_get_parent(iicdev), iicdev, IIC_DONTWAIT))) 202 return (error); 203 204 count = min(uio->uio_resid, BUFSIZE); 205 uiomove(sc->sc_buffer, count, uio); 206 207 error = iicbus_block_write(device_get_parent(iicdev), sc->sc_addr, 208 sc->sc_buffer, count, &sent); 209 210 iicbus_release_bus(device_get_parent(iicdev), iicdev); 211 212 return(error); 213} 214 215static int 216iicread(dev_t dev, struct uio * uio, int ioflag) 217{ 218 device_t iicdev = IIC_DEVICE(minor(dev)); 219 struct iic_softc *sc = IIC_SOFTC(minor(dev)); 220 int len, error = 0; 221 int bufsize; 222 223 if (!sc || !iicdev || !sc->sc_addr) 224 return (EINVAL); 225 226 if (sc->sc_count == 0) 227 return (EINVAL); 228 229 if ((error = iicbus_request_bus(device_get_parent(iicdev), iicdev, IIC_DONTWAIT))) 230 return (error); 231 232 /* max amount of data to read */ 233 len = min(uio->uio_resid, BUFSIZE); 234 235 if ((error = iicbus_block_read(device_get_parent(iicdev), sc->sc_addr, 236 sc->sc_inbuf, len, &bufsize))) 237 return (error); 238 239 if (bufsize > uio->uio_resid) 240 panic("%s: too much data read!", __func__); 241 242 iicbus_release_bus(device_get_parent(iicdev), iicdev); 243 244 return (uiomove(sc->sc_inbuf, bufsize, uio)); 245} 246 247static int 248iicioctl(dev_t dev, u_long cmd, caddr_t data, int flags, struct thread *td) 249{ 250 device_t iicdev = IIC_DEVICE(minor(dev)); 251 struct iic_softc *sc = IIC_SOFTC(minor(dev)); 252 device_t parent = device_get_parent(iicdev); 253 struct iiccmd *s = (struct iiccmd *)data; 254 int error, count; 255 256 if (!sc) 257 return (EINVAL); 258 259 if ((error = iicbus_request_bus(device_get_parent(iicdev), iicdev, 260 (flags & O_NONBLOCK) ? IIC_DONTWAIT : 261 (IIC_WAIT | IIC_INTR)))) 262 return (error); 263 264 switch (cmd) { 265 case I2CSTART: 266 error = iicbus_start(parent, s->slave, 0); 267 268 /* 269 * Implicitly set the chip addr to the slave addr passed as 270 * parameter. Consequently, start/stop shall be called before 271 * the read or the write of a block. 272 */ 273 if (!error) 274 sc->sc_addr = s->slave; 275 276 break; 277 278 case I2CSTOP: 279 error = iicbus_stop(parent); 280 break; 281 282 case I2CRSTCARD: 283 error = iicbus_reset(parent, 0, 0, NULL); 284 break; 285 286 case I2CWRITE: 287 error = iicbus_write(parent, s->buf, s->count, &count, 10); 288 break; 289 290 case I2CREAD: 291 error = iicbus_read(parent, s->buf, s->count, &count, s->last, 10); 292 break; 293 294 default: 295 error = ENODEV; 296 } 297 298 iicbus_release_bus(device_get_parent(iicdev), iicdev); 299 300 return (error); 301} 302 303DRIVER_MODULE(iic, iicbus, iic_driver, iic_devclass, 0, 0); 304MODULE_DEPEND(iic, iicbus, IICBUS_MINVER, IICBUS_PREFVER, IICBUS_MAXVER); 305MODULE_VERSION(iic, 1);
| 104}; 105 106static void 107iic_identify(driver_t *driver, device_t parent) 108{ 109 BUS_ADD_CHILD(parent, 0, "iic", 0); 110} 111 112static int 113iic_probe(device_t dev) 114{ 115 device_set_desc(dev, "I2C generic I/O"); 116 return (0); 117} 118 119static int 120iic_attach(device_t dev) 121{ 122 struct iic_softc *sc = (struct iic_softc *)device_get_softc(dev); 123 124 if (!sc) 125 return (ENOMEM); 126 127 bzero(sc, sizeof(struct iic_softc)); 128 129 sc->sc_devnode = make_dev(&iic_cdevsw, device_get_unit(dev), 130 UID_ROOT, GID_WHEEL, 131 0600, "iic%d", device_get_unit(dev)); 132 133 return (0); 134} 135 136static int 137iic_detach(device_t dev) 138{ 139 struct iic_softc *sc = (struct iic_softc *)device_get_softc(dev); 140 141 if (sc->sc_devnode) 142 destroy_dev(sc->sc_devnode); 143 144 return (0); 145} 146 147static int 148iicopen (dev_t dev, int flags, int fmt, struct thread *td) 149{ 150 struct iic_softc *sc = IIC_SOFTC(minor(dev)); 151 152 if (!sc) 153 return (EINVAL); 154 155 if (sc->sc_count > 0) 156 return (EBUSY); 157 158 sc->sc_count++; 159 160 return (0); 161} 162 163static int 164iicclose(dev_t dev, int flags, int fmt, struct thread *td) 165{ 166 struct iic_softc *sc = IIC_SOFTC(minor(dev)); 167 168 if (!sc) 169 return (EINVAL); 170 171 if (!sc->sc_count) 172 return (EINVAL); 173 174 sc->sc_count--; 175 176 if (sc->sc_count < 0) 177 panic("%s: iic_count < 0!", __func__); 178 179 return (0); 180} 181 182static int 183iicwrite(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 sent, error, count; 188 189 if (!sc || !iicdev || !sc->sc_addr) 190 return (EINVAL); 191 192 if (sc->sc_count == 0) 193 return (EINVAL); 194 195 if ((error = iicbus_request_bus(device_get_parent(iicdev), iicdev, IIC_DONTWAIT))) 196 return (error); 197 198 count = min(uio->uio_resid, BUFSIZE); 199 uiomove(sc->sc_buffer, count, uio); 200 201 error = iicbus_block_write(device_get_parent(iicdev), sc->sc_addr, 202 sc->sc_buffer, count, &sent); 203 204 iicbus_release_bus(device_get_parent(iicdev), iicdev); 205 206 return(error); 207} 208 209static int 210iicread(dev_t dev, struct uio * uio, int ioflag) 211{ 212 device_t iicdev = IIC_DEVICE(minor(dev)); 213 struct iic_softc *sc = IIC_SOFTC(minor(dev)); 214 int len, error = 0; 215 int bufsize; 216 217 if (!sc || !iicdev || !sc->sc_addr) 218 return (EINVAL); 219 220 if (sc->sc_count == 0) 221 return (EINVAL); 222 223 if ((error = iicbus_request_bus(device_get_parent(iicdev), iicdev, IIC_DONTWAIT))) 224 return (error); 225 226 /* max amount of data to read */ 227 len = min(uio->uio_resid, BUFSIZE); 228 229 if ((error = iicbus_block_read(device_get_parent(iicdev), sc->sc_addr, 230 sc->sc_inbuf, len, &bufsize))) 231 return (error); 232 233 if (bufsize > uio->uio_resid) 234 panic("%s: too much data read!", __func__); 235 236 iicbus_release_bus(device_get_parent(iicdev), iicdev); 237 238 return (uiomove(sc->sc_inbuf, bufsize, uio)); 239} 240 241static int 242iicioctl(dev_t dev, u_long cmd, caddr_t data, int flags, struct thread *td) 243{ 244 device_t iicdev = IIC_DEVICE(minor(dev)); 245 struct iic_softc *sc = IIC_SOFTC(minor(dev)); 246 device_t parent = device_get_parent(iicdev); 247 struct iiccmd *s = (struct iiccmd *)data; 248 int error, count; 249 250 if (!sc) 251 return (EINVAL); 252 253 if ((error = iicbus_request_bus(device_get_parent(iicdev), iicdev, 254 (flags & O_NONBLOCK) ? IIC_DONTWAIT : 255 (IIC_WAIT | IIC_INTR)))) 256 return (error); 257 258 switch (cmd) { 259 case I2CSTART: 260 error = iicbus_start(parent, s->slave, 0); 261 262 /* 263 * Implicitly set the chip addr to the slave addr passed as 264 * parameter. Consequently, start/stop shall be called before 265 * the read or the write of a block. 266 */ 267 if (!error) 268 sc->sc_addr = s->slave; 269 270 break; 271 272 case I2CSTOP: 273 error = iicbus_stop(parent); 274 break; 275 276 case I2CRSTCARD: 277 error = iicbus_reset(parent, 0, 0, NULL); 278 break; 279 280 case I2CWRITE: 281 error = iicbus_write(parent, s->buf, s->count, &count, 10); 282 break; 283 284 case I2CREAD: 285 error = iicbus_read(parent, s->buf, s->count, &count, s->last, 10); 286 break; 287 288 default: 289 error = ENODEV; 290 } 291 292 iicbus_release_bus(device_get_parent(iicdev), iicdev); 293 294 return (error); 295} 296 297DRIVER_MODULE(iic, iicbus, iic_driver, iic_devclass, 0, 0); 298MODULE_DEPEND(iic, iicbus, IICBUS_MINVER, IICBUS_PREFVER, IICBUS_MAXVER); 299MODULE_VERSION(iic, 1);
|