smb.c revision 42442
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: smb.c,v 1.4 1998/12/07 21:58:17 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/smbus/smbconf.h> 42#include <dev/smbus/smbus.h> 43#include <machine/smb.h> 44 45#include "smbus_if.h" 46 47#define BUFSIZE 1024 48 49struct smb_softc { 50 51 int sc_addr; /* address on smbus */ 52 int sc_count; /* >0 if device opened */ 53 54 char *sc_cp; /* output buffer pointer */ 55 56 char sc_buffer[BUFSIZE]; /* output buffer */ 57 char sc_inbuf[BUFSIZE]; /* input buffer */ 58}; 59 60#define IIC_SOFTC(unit) \ 61 ((struct smb_softc *)devclass_get_softc(smb_devclass, (unit))) 62 63#define IIC_DEVICE(unit) \ 64 (devclass_get_device(smb_devclass, (unit))) 65 66static int smb_probe(device_t); 67static int smb_attach(device_t); 68 69static devclass_t smb_devclass; 70 71static device_method_t smb_methods[] = { 72 /* device interface */ 73 DEVMETHOD(device_probe, smb_probe), 74 DEVMETHOD(device_attach, smb_attach), 75 76 /* smbus interface */ 77 DEVMETHOD(smbus_intr, smbus_generic_intr), 78 79 { 0, 0 } 80}; 81 82static driver_t smb_driver = { 83 "smb", 84 smb_methods, 85 DRIVER_TYPE_MISC, 86 sizeof(struct smb_softc), 87}; 88 89static d_open_t smbopen; 90static d_close_t smbclose; 91static d_write_t smbwrite; 92static d_read_t smbread; 93static d_ioctl_t smbioctl; 94 95#define CDEV_MAJOR 106 96static struct cdevsw smb_cdevsw = 97 { smbopen, smbclose, smbread, smbwrite, /*106*/ 98 smbioctl, nullstop, nullreset, nodevtotty, /*smb*/ 99 seltrue, nommap, nostrat, "smb", NULL, -1 }; 100 101/* 102 * smbprobe() 103 */ 104static int 105smb_probe(device_t dev) 106{ 107 struct smb_softc *sc = (struct smb_softc *)device_get_softc(dev); 108 109 sc->sc_addr = smbus_get_addr(dev); 110 111 /* XXX detect chip with start/stop conditions */ 112 113 return (0); 114} 115 116/* 117 * smbattach() 118 */ 119static int 120smb_attach(device_t dev) 121{ 122 return (0); 123} 124 125static int 126smbopen (dev_t dev, int flags, int fmt, struct proc *p) 127{ 128 struct smb_softc *sc = IIC_SOFTC(minor(dev)); 129 130 if (!sc) 131 return (EINVAL); 132 133 if (sc->sc_count) 134 return (EBUSY); 135 136 sc->sc_count++; 137 138 return (0); 139} 140 141static int 142smbclose(dev_t dev, int flags, int fmt, struct proc *p) 143{ 144 struct smb_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 return (0); 155} 156 157static int 158smbwrite(dev_t dev, struct uio * uio, int ioflag) 159{ 160 /* not supported */ 161 162 return (EINVAL); 163} 164 165static int 166smbread(dev_t dev, struct uio * uio, int ioflag) 167{ 168 /* not supported */ 169 170 return (EINVAL); 171} 172 173static int 174smbioctl(dev_t dev, u_long cmd, caddr_t data, int flags, struct proc *p) 175{ 176 device_t smbdev = IIC_DEVICE(minor(dev)); 177 struct smb_softc *sc = IIC_SOFTC(minor(dev)); 178 device_t parent = device_get_parent(smbdev); 179 180 int error = 0; 181 struct smbcmd *s = (struct smbcmd *)data; 182 183 if (!sc || !s) 184 return (EINVAL); 185 186 switch (cmd) { 187 case SMB_QUICK_WRITE: 188 smbus_quick(parent, s->slave, SMB_QWRITE); 189 goto end; 190 191 case SMB_QUICK_READ: 192 smbus_quick(parent, s->slave, SMB_QREAD); 193 goto end; 194 }; 195 196 switch (cmd) { 197 case SMB_SENDB: 198 smbus_sendb(parent, s->slave, s->cmd); 199 break; 200 201 case SMB_RECVB: 202 smbus_recvb(parent, s->slave, &s->cmd); 203 break; 204 205 case SMB_WRITEB: 206 smbus_writeb(parent, s->slave, s->cmd, s->data.byte); 207 break; 208 209 case SMB_WRITEW: 210 smbus_writew(parent, s->slave, s->cmd, s->data.word); 211 break; 212 213 case SMB_READB: 214 if (s->data.byte_ptr) 215 smbus_readb(parent, s->slave, s->cmd, 216 s->data.byte_ptr); 217 break; 218 219 case SMB_READW: 220 if (s->data.word_ptr) 221 smbus_readw(parent, s->slave, s->cmd, s->data.word_ptr); 222 break; 223 224 case SMB_PCALL: 225 if (s->data.process.rdata) 226 smbus_pcall(parent, s->slave, s->cmd, 227 s->data.process.sdata, s->data.process.rdata); 228 break; 229 230 case SMB_BWRITE: 231 if (s->count && s->data.byte_ptr) 232 smbus_bwrite(parent, s->slave, s->cmd, s->count, 233 s->data.byte_ptr); 234 break; 235 236 case SMB_BREAD: 237 if (s->count && s->data.byte_ptr) 238 smbus_bread(parent, s->slave, s->cmd, s->count, 239 s->data.byte_ptr); 240 break; 241 242 default: 243 error = ENODEV; 244 } 245 246end: 247 return (error); 248} 249 250static int smb_devsw_installed = 0; 251 252static void 253smb_drvinit(void *unused) 254{ 255 dev_t dev; 256 257 if( ! smb_devsw_installed ) { 258 dev = makedev(CDEV_MAJOR,0); 259 cdevsw_add(&dev,&smb_cdevsw,NULL); 260 smb_devsw_installed = 1; 261 } 262} 263 264CDEV_DRIVER_MODULE(smb, smbus, smb_driver, smb_devclass, CDEV_MAJOR, 265 smb_cdevsw, 0, 0); 266 267SYSINIT(smbdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR,smb_drvinit,NULL) 268