smb.c revision 162234
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 * 26 * $FreeBSD: head/sys/dev/smbus/smb.c 162234 2006-09-11 20:52:41Z jhb $ 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/smbus/smbconf.h> 39#include <dev/smbus/smbus.h> 40#include <dev/smbus/smb.h> 41 42#include "smbus_if.h" 43 44#define BUFSIZE 1024 45 46struct smb_softc { 47 48 int sc_count; /* >0 if device opened */ 49 struct cdev *sc_devnode; 50}; 51 52#define IIC_SOFTC(unit) \ 53 ((struct smb_softc *)devclass_get_softc(smb_devclass, (unit))) 54 55#define IIC_DEVICE(unit) \ 56 (devclass_get_device(smb_devclass, (unit))) 57 58static void smb_identify(driver_t *driver, device_t parent); 59static int smb_probe(device_t); 60static int smb_attach(device_t); 61static int smb_detach(device_t); 62 63static devclass_t smb_devclass; 64 65static device_method_t smb_methods[] = { 66 /* device interface */ 67 DEVMETHOD(device_identify, smb_identify), 68 DEVMETHOD(device_probe, smb_probe), 69 DEVMETHOD(device_attach, smb_attach), 70 DEVMETHOD(device_detach, smb_detach), 71 72 /* smbus interface */ 73 DEVMETHOD(smbus_intr, smbus_generic_intr), 74 75 { 0, 0 } 76}; 77 78static driver_t smb_driver = { 79 "smb", 80 smb_methods, 81 sizeof(struct smb_softc), 82}; 83 84static d_open_t smbopen; 85static d_close_t smbclose; 86static d_ioctl_t smbioctl; 87 88static struct cdevsw smb_cdevsw = { 89 .d_version = D_VERSION, 90 .d_flags = D_NEEDGIANT, 91 .d_open = smbopen, 92 .d_close = smbclose, 93 .d_ioctl = smbioctl, 94 .d_name = "smb", 95}; 96 97static void 98smb_identify(driver_t *driver, device_t parent) 99{ 100 101 if (device_find_child(parent, "smb", -1) == NULL) 102 BUS_ADD_CHILD(parent, 0, "smb", -1); 103} 104 105static int 106smb_probe(device_t dev) 107{ 108 device_set_desc(dev, "SMBus generic I/O"); 109 110 return (0); 111} 112 113static int 114smb_attach(device_t dev) 115{ 116 struct smb_softc *sc = (struct smb_softc *)device_get_softc(dev); 117 118 if (!sc) 119 return (ENOMEM); 120 121 bzero(sc, sizeof(struct smb_softc *)); 122 123 sc->sc_devnode = make_dev(&smb_cdevsw, device_get_unit(dev), 124 UID_ROOT, GID_WHEEL, 125 0600, "smb%d", device_get_unit(dev)); 126 127 return (0); 128} 129 130static int 131smb_detach(device_t dev) 132{ 133 struct smb_softc *sc = (struct smb_softc *)device_get_softc(dev); 134 135 if (sc->sc_devnode) 136 destroy_dev(sc->sc_devnode); 137 138 return (0); 139} 140 141static int 142smbopen (struct cdev *dev, int flags, int fmt, struct thread *td) 143{ 144 struct smb_softc *sc = IIC_SOFTC(minor(dev)); 145 146 if (sc == NULL) 147 return (ENXIO); 148 149 if (sc->sc_count != 0) 150 return (EBUSY); 151 152 sc->sc_count++; 153 154 return (0); 155} 156 157static int 158smbclose(struct cdev *dev, int flags, int fmt, struct thread *td) 159{ 160 struct smb_softc *sc = IIC_SOFTC(minor(dev)); 161 162 if (sc == NULL) 163 return (ENXIO); 164 165 if (sc->sc_count == 0) 166 /* This is not supposed to happen. */ 167 return (0); 168 169 sc->sc_count--; 170 171 return (0); 172} 173 174static int 175smbioctl(struct cdev *dev, u_long cmd, caddr_t data, int flags, struct thread *td) 176{ 177 char buf[SMB_MAXBLOCKSIZE]; 178 device_t parent; 179 struct smbcmd *s = (struct smbcmd *)data; 180 struct smb_softc *sc = IIC_SOFTC(minor(dev)); 181 device_t smbdev = IIC_DEVICE(minor(dev)); 182 int error; 183 short w; 184 u_char count; 185 char c; 186 187 if (sc == NULL) 188 return (ENXIO); 189 if (s == NULL) 190 return (EINVAL); 191 192 parent = device_get_parent(smbdev); 193 194 /* Allocate the bus. */ 195 if ((error = smbus_request_bus(parent, smbdev, 196 (flags & O_NONBLOCK) ? SMB_DONTWAIT : (SMB_WAIT | SMB_INTR)))) 197 return (error); 198 199 switch (cmd) { 200 case SMB_QUICK_WRITE: 201 error = smbus_error(smbus_quick(parent, s->slave, SMB_QWRITE)); 202 break; 203 204 case SMB_QUICK_READ: 205 error = smbus_error(smbus_quick(parent, s->slave, SMB_QREAD)); 206 break; 207 208 case SMB_SENDB: 209 error = smbus_error(smbus_sendb(parent, s->slave, s->cmd)); 210 break; 211 212 case SMB_RECVB: 213 error = smbus_error(smbus_recvb(parent, s->slave, &s->cmd)); 214 break; 215 216 case SMB_WRITEB: 217 error = smbus_error(smbus_writeb(parent, s->slave, s->cmd, 218 s->data.byte)); 219 break; 220 221 case SMB_WRITEW: 222 error = smbus_error(smbus_writew(parent, s->slave, 223 s->cmd, s->data.word)); 224 break; 225 226 case SMB_READB: 227 if (s->data.byte_ptr) { 228 error = smbus_error(smbus_readb(parent, s->slave, 229 s->cmd, &c)); 230 if (error) 231 break; 232 error = copyout(&c, s->data.byte_ptr, 233 sizeof(*(s->data.byte_ptr))); 234 } 235 break; 236 237 case SMB_READW: 238 if (s->data.word_ptr) { 239 error = smbus_error(smbus_readw(parent, s->slave, 240 s->cmd, &w)); 241 if (error == 0) { 242 error = copyout(&w, s->data.word_ptr, 243 sizeof(*(s->data.word_ptr))); 244 } 245 } 246 break; 247 248 case SMB_PCALL: 249 if (s->data.process.rdata) { 250 251 error = smbus_error(smbus_pcall(parent, s->slave, s->cmd, 252 s->data.process.sdata, &w)); 253 if (error) 254 break; 255 error = copyout(&w, s->data.process.rdata, 256 sizeof(*(s->data.process.rdata))); 257 } 258 259 break; 260 261 case SMB_BWRITE: 262 if (s->count && s->data.byte_ptr) { 263 if (s->count > SMB_MAXBLOCKSIZE) 264 s->count = SMB_MAXBLOCKSIZE; 265 error = copyin(s->data.byte_ptr, buf, s->count); 266 if (error) 267 break; 268 error = smbus_error(smbus_bwrite(parent, s->slave, 269 s->cmd, s->count, buf)); 270 } 271 break; 272 273 case SMB_OLD_BREAD: 274 case SMB_BREAD: 275 if (s->count && s->data.byte_ptr) { 276 count = min(s->count, SMB_MAXBLOCKSIZE); 277 error = smbus_error(smbus_bread(parent, s->slave, 278 s->cmd, &count, buf)); 279 if (error) 280 break; 281 error = copyout(buf, s->data.byte_ptr, 282 min(count, s->count)); 283 s->count = count; 284 } 285 break; 286 287 default: 288 error = ENOTTY; 289 } 290 291 smbus_release_bus(parent, smbdev); 292 293 return (error); 294} 295 296DRIVER_MODULE(smb, smbus, smb_driver, smb_devclass, 0, 0); 297MODULE_DEPEND(smb, smbus, SMBUS_MINVER, SMBUS_PREFVER, SMBUS_MAXVER); 298MODULE_VERSION(smb, 1); 299