10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
57656SSherry.Moore@Sun.COM * Common Development and Distribution License (the "License").
67656SSherry.Moore@Sun.COM * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate * See the License for the specific language governing permissions
110Sstevel@tonic-gate * and limitations under the License.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate *
190Sstevel@tonic-gate * CDDL HEADER END
200Sstevel@tonic-gate */
210Sstevel@tonic-gate /*
227656SSherry.Moore@Sun.COM * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
230Sstevel@tonic-gate * Use is subject to license terms.
240Sstevel@tonic-gate */
250Sstevel@tonic-gate
260Sstevel@tonic-gate #include <sys/stat.h> /* ddi_create_minor_node S_IFCHR */
270Sstevel@tonic-gate #include <sys/modctl.h> /* for modldrv */
280Sstevel@tonic-gate #include <sys/open.h> /* for open params. */
290Sstevel@tonic-gate #include <sys/types.h>
300Sstevel@tonic-gate #include <sys/kmem.h>
310Sstevel@tonic-gate #include <sys/sunddi.h>
320Sstevel@tonic-gate #include <sys/conf.h> /* req. by dev_ops flags MTSAFE etc. */
330Sstevel@tonic-gate #include <sys/ddi.h>
340Sstevel@tonic-gate #include <sys/file.h>
350Sstevel@tonic-gate #include <sys/note.h>
360Sstevel@tonic-gate
370Sstevel@tonic-gate #include <sys/i2c/clients/ssc100_impl.h>
380Sstevel@tonic-gate
390Sstevel@tonic-gate static void *ssc100soft_statep;
400Sstevel@tonic-gate
410Sstevel@tonic-gate static int ssc100_do_attach(dev_info_t *);
420Sstevel@tonic-gate static int ssc100_do_detach(dev_info_t *);
430Sstevel@tonic-gate static int ssc100_do_resume(void);
440Sstevel@tonic-gate static int ssc100_do_suspend(void);
450Sstevel@tonic-gate static int ssc100_get(struct ssc100_unit *, uchar_t *);
460Sstevel@tonic-gate static int ssc100_set(struct ssc100_unit *, uchar_t);
470Sstevel@tonic-gate static int ssc100_get_reg(struct ssc100_unit *, uchar_t *, uchar_t);
480Sstevel@tonic-gate static int ssc100_common(struct ssc100_unit *, uchar_t *, uchar_t, int8_t);
490Sstevel@tonic-gate static int ssc100_read(dev_t, struct uio *, cred_t *);
500Sstevel@tonic-gate static int ssc100_write(dev_t, struct uio *, cred_t *);
510Sstevel@tonic-gate static int ssc100_io(dev_t, struct uio *, int);
520Sstevel@tonic-gate
530Sstevel@tonic-gate /*
540Sstevel@tonic-gate * cb ops (only need ioctl)
550Sstevel@tonic-gate */
560Sstevel@tonic-gate static int ssc100_open(dev_t *, int, int, cred_t *);
570Sstevel@tonic-gate static int ssc100_close(dev_t, int, int, cred_t *);
580Sstevel@tonic-gate static int ssc100_ioctl(dev_t, int, intptr_t, int, cred_t *, int *);
590Sstevel@tonic-gate
600Sstevel@tonic-gate static struct cb_ops ssc100_cbops = {
610Sstevel@tonic-gate ssc100_open, /* open */
620Sstevel@tonic-gate ssc100_close, /* close */
630Sstevel@tonic-gate nodev, /* strategy */
640Sstevel@tonic-gate nodev, /* print */
650Sstevel@tonic-gate nodev, /* dump */
660Sstevel@tonic-gate ssc100_read, /* read */
670Sstevel@tonic-gate ssc100_write, /* write */
680Sstevel@tonic-gate ssc100_ioctl, /* ioctl */
690Sstevel@tonic-gate nodev, /* devmap */
700Sstevel@tonic-gate nodev, /* mmap */
710Sstevel@tonic-gate nodev, /* segmap */
720Sstevel@tonic-gate nochpoll, /* poll */
730Sstevel@tonic-gate ddi_prop_op, /* cb_prop_op */
740Sstevel@tonic-gate NULL, /* streamtab */
750Sstevel@tonic-gate D_NEW | D_MP | D_HOTPLUG, /* Driver compatibility flag */
760Sstevel@tonic-gate CB_REV, /* rev */
770Sstevel@tonic-gate nodev, /* int (*cb_aread)() */
780Sstevel@tonic-gate nodev /* int (*cb_awrite)() */
790Sstevel@tonic-gate };
800Sstevel@tonic-gate
810Sstevel@tonic-gate /*
820Sstevel@tonic-gate * dev ops
830Sstevel@tonic-gate */
840Sstevel@tonic-gate static int ssc100_attach(dev_info_t *dip, ddi_attach_cmd_t cmd);
850Sstevel@tonic-gate static int ssc100_detach(dev_info_t *dip, ddi_detach_cmd_t cmd);
860Sstevel@tonic-gate
870Sstevel@tonic-gate static struct dev_ops ssc100_ops = {
880Sstevel@tonic-gate DEVO_REV,
890Sstevel@tonic-gate 0,
900Sstevel@tonic-gate ddi_getinfo_1to1,
910Sstevel@tonic-gate nulldev,
920Sstevel@tonic-gate nulldev,
930Sstevel@tonic-gate ssc100_attach,
940Sstevel@tonic-gate ssc100_detach,
950Sstevel@tonic-gate nodev,
960Sstevel@tonic-gate &ssc100_cbops,
977656SSherry.Moore@Sun.COM NULL,
987656SSherry.Moore@Sun.COM NULL,
997656SSherry.Moore@Sun.COM ddi_quiesce_not_needed, /* quiesce */
1000Sstevel@tonic-gate };
1010Sstevel@tonic-gate
1020Sstevel@tonic-gate extern struct mod_ops mod_driverops;
1030Sstevel@tonic-gate
1040Sstevel@tonic-gate static struct modldrv ssc100_modldrv = {
1050Sstevel@tonic-gate &mod_driverops, /* type of module - driver */
106*7696SRichard.Bean@Sun.COM "SSC100 i2c device driver",
1070Sstevel@tonic-gate &ssc100_ops
1080Sstevel@tonic-gate };
1090Sstevel@tonic-gate
1100Sstevel@tonic-gate static struct modlinkage ssc100_modlinkage = {
1110Sstevel@tonic-gate MODREV_1,
1120Sstevel@tonic-gate &ssc100_modldrv,
1130Sstevel@tonic-gate 0
1140Sstevel@tonic-gate };
1150Sstevel@tonic-gate
1160Sstevel@tonic-gate
1170Sstevel@tonic-gate int
_init(void)1180Sstevel@tonic-gate _init(void)
1190Sstevel@tonic-gate {
1200Sstevel@tonic-gate int error;
1210Sstevel@tonic-gate
1220Sstevel@tonic-gate error = mod_install(&ssc100_modlinkage);
1230Sstevel@tonic-gate
1240Sstevel@tonic-gate if (!error)
1250Sstevel@tonic-gate (void) ddi_soft_state_init(&ssc100soft_statep,
1267656SSherry.Moore@Sun.COM sizeof (struct ssc100_unit), 1);
1270Sstevel@tonic-gate return (error);
1280Sstevel@tonic-gate }
1290Sstevel@tonic-gate
1300Sstevel@tonic-gate int
_fini(void)1310Sstevel@tonic-gate _fini(void)
1320Sstevel@tonic-gate {
1330Sstevel@tonic-gate int error;
1340Sstevel@tonic-gate
1350Sstevel@tonic-gate error = mod_remove(&ssc100_modlinkage);
1360Sstevel@tonic-gate if (!error)
1370Sstevel@tonic-gate ddi_soft_state_fini(&ssc100soft_statep);
1380Sstevel@tonic-gate
1390Sstevel@tonic-gate return (error);
1400Sstevel@tonic-gate }
1410Sstevel@tonic-gate
1420Sstevel@tonic-gate int
_info(struct modinfo * modinfop)1430Sstevel@tonic-gate _info(struct modinfo *modinfop)
1440Sstevel@tonic-gate {
1450Sstevel@tonic-gate return (mod_info(&ssc100_modlinkage, modinfop));
1460Sstevel@tonic-gate }
1470Sstevel@tonic-gate
1480Sstevel@tonic-gate static int
ssc100_open(dev_t * devp,int flags,int otyp,cred_t * credp)1490Sstevel@tonic-gate ssc100_open(dev_t *devp, int flags, int otyp, cred_t *credp)
1500Sstevel@tonic-gate {
1510Sstevel@tonic-gate _NOTE(ARGUNUSED(credp))
1520Sstevel@tonic-gate
1530Sstevel@tonic-gate struct ssc100_unit *unitp;
1540Sstevel@tonic-gate int instance;
1550Sstevel@tonic-gate int error = 0;
1560Sstevel@tonic-gate
1570Sstevel@tonic-gate instance = getminor(*devp);
1580Sstevel@tonic-gate
1590Sstevel@tonic-gate if (instance < 0) {
1600Sstevel@tonic-gate return (ENXIO);
1610Sstevel@tonic-gate }
1620Sstevel@tonic-gate
1630Sstevel@tonic-gate unitp = (struct ssc100_unit *)
1647656SSherry.Moore@Sun.COM ddi_get_soft_state(ssc100soft_statep, instance);
1650Sstevel@tonic-gate
1660Sstevel@tonic-gate if (unitp == NULL) {
1670Sstevel@tonic-gate return (ENXIO);
1680Sstevel@tonic-gate }
1690Sstevel@tonic-gate
1700Sstevel@tonic-gate if (otyp != OTYP_CHR) {
1710Sstevel@tonic-gate return (EINVAL);
1720Sstevel@tonic-gate }
1730Sstevel@tonic-gate
1740Sstevel@tonic-gate mutex_enter(&unitp->ssc100_mutex);
1750Sstevel@tonic-gate
1760Sstevel@tonic-gate if (flags & FEXCL) {
1770Sstevel@tonic-gate if (unitp->ssc100_oflag != 0) {
1780Sstevel@tonic-gate error = EBUSY;
1790Sstevel@tonic-gate } else {
1800Sstevel@tonic-gate unitp->ssc100_oflag = FEXCL;
1810Sstevel@tonic-gate }
1820Sstevel@tonic-gate } else {
1830Sstevel@tonic-gate if (unitp->ssc100_oflag == FEXCL) {
1840Sstevel@tonic-gate error = EBUSY;
1850Sstevel@tonic-gate } else {
1860Sstevel@tonic-gate unitp->ssc100_oflag = FOPEN;
1870Sstevel@tonic-gate }
1880Sstevel@tonic-gate }
1890Sstevel@tonic-gate
1900Sstevel@tonic-gate mutex_exit(&unitp->ssc100_mutex);
1910Sstevel@tonic-gate
1920Sstevel@tonic-gate return (error);
1930Sstevel@tonic-gate }
1940Sstevel@tonic-gate
1950Sstevel@tonic-gate static int
ssc100_close(dev_t dev,int flags,int otyp,cred_t * credp)1960Sstevel@tonic-gate ssc100_close(dev_t dev, int flags, int otyp, cred_t *credp)
1970Sstevel@tonic-gate {
1980Sstevel@tonic-gate _NOTE(ARGUNUSED(flags, otyp, credp))
1990Sstevel@tonic-gate
2000Sstevel@tonic-gate struct ssc100_unit *unitp;
2010Sstevel@tonic-gate int instance;
2020Sstevel@tonic-gate
2030Sstevel@tonic-gate instance = getminor(dev);
2040Sstevel@tonic-gate
2050Sstevel@tonic-gate if (instance < 0) {
2060Sstevel@tonic-gate return (ENXIO);
2070Sstevel@tonic-gate }
2080Sstevel@tonic-gate unitp = (struct ssc100_unit *)
2097656SSherry.Moore@Sun.COM ddi_get_soft_state(ssc100soft_statep, instance);
2100Sstevel@tonic-gate
2110Sstevel@tonic-gate if (unitp == NULL) {
2120Sstevel@tonic-gate return (ENXIO);
2130Sstevel@tonic-gate }
2140Sstevel@tonic-gate
2150Sstevel@tonic-gate mutex_enter(&unitp->ssc100_mutex);
2160Sstevel@tonic-gate
2170Sstevel@tonic-gate unitp->ssc100_oflag = 0;
2180Sstevel@tonic-gate
2190Sstevel@tonic-gate mutex_exit(&unitp->ssc100_mutex);
2200Sstevel@tonic-gate return (DDI_SUCCESS);
2210Sstevel@tonic-gate }
2220Sstevel@tonic-gate
2230Sstevel@tonic-gate static int
ssc100_common(struct ssc100_unit * unitp,uchar_t * byte,uchar_t input,int8_t flag)2240Sstevel@tonic-gate ssc100_common(struct ssc100_unit *unitp, uchar_t *byte, uchar_t input,
2250Sstevel@tonic-gate int8_t flag)
2260Sstevel@tonic-gate {
2270Sstevel@tonic-gate i2c_transfer_t *i2c_tran_pointer;
2280Sstevel@tonic-gate int err = I2C_SUCCESS;
2290Sstevel@tonic-gate
2300Sstevel@tonic-gate (void) i2c_transfer_alloc(unitp->ssc100_hdl, &i2c_tran_pointer,
2310Sstevel@tonic-gate 1, 1, I2C_SLEEP);
2320Sstevel@tonic-gate if (i2c_tran_pointer == NULL) {
2330Sstevel@tonic-gate D2CMN_ERR((CE_WARN, "%s: Failed in SSC100_COMMON "
2347656SSherry.Moore@Sun.COM "i2c_tran_pointer not allocated",
2357656SSherry.Moore@Sun.COM unitp->ssc100_name));
2360Sstevel@tonic-gate return (ENOMEM);
2370Sstevel@tonic-gate }
2380Sstevel@tonic-gate
2390Sstevel@tonic-gate i2c_tran_pointer->i2c_flags = flag;
2400Sstevel@tonic-gate if (flag != I2C_RD) {
2410Sstevel@tonic-gate i2c_tran_pointer->i2c_wbuf[0] = input;
2420Sstevel@tonic-gate }
2430Sstevel@tonic-gate
2440Sstevel@tonic-gate err = i2c_transfer(unitp->ssc100_hdl, i2c_tran_pointer);
2450Sstevel@tonic-gate if (err) {
2460Sstevel@tonic-gate D2CMN_ERR((CE_WARN, "%s: Failed in SSC100_COMMON "
2477656SSherry.Moore@Sun.COM "i2c_transfer routine", unitp->ssc100_name));
2480Sstevel@tonic-gate } else if (flag != I2C_WR) {
2490Sstevel@tonic-gate *byte = i2c_tran_pointer->i2c_rbuf[0];
2500Sstevel@tonic-gate }
2510Sstevel@tonic-gate
2520Sstevel@tonic-gate i2c_transfer_free(unitp->ssc100_hdl, i2c_tran_pointer);
2530Sstevel@tonic-gate return (err);
2540Sstevel@tonic-gate }
2550Sstevel@tonic-gate
2560Sstevel@tonic-gate static int
ssc100_get_reg(struct ssc100_unit * unitp,uchar_t * byte,uchar_t reg)2570Sstevel@tonic-gate ssc100_get_reg(struct ssc100_unit *unitp, uchar_t *byte, uchar_t reg)
2580Sstevel@tonic-gate {
2590Sstevel@tonic-gate int err = I2C_SUCCESS;
2600Sstevel@tonic-gate
2610Sstevel@tonic-gate err = ssc100_common(unitp, byte, reg, I2C_WR_RD);
2620Sstevel@tonic-gate if (err) {
2630Sstevel@tonic-gate D2CMN_ERR((CE_WARN, "%s: Failed in SSC100_GET_REG "
2647656SSherry.Moore@Sun.COM "i2c_common routine", unitp->ssc100_name));
2650Sstevel@tonic-gate }
2660Sstevel@tonic-gate return (err);
2670Sstevel@tonic-gate }
2680Sstevel@tonic-gate
2690Sstevel@tonic-gate static int
ssc100_get(struct ssc100_unit * unitp,uchar_t * byte)2700Sstevel@tonic-gate ssc100_get(struct ssc100_unit *unitp, uchar_t *byte)
2710Sstevel@tonic-gate {
2720Sstevel@tonic-gate int err = I2C_SUCCESS;
2730Sstevel@tonic-gate
2740Sstevel@tonic-gate err = ssc100_common(unitp, byte, 0, I2C_RD);
2750Sstevel@tonic-gate if (err) {
2760Sstevel@tonic-gate D2CMN_ERR((CE_WARN, "%s: Failed in SSC100_GET "
2777656SSherry.Moore@Sun.COM "i2c_common routine", unitp->ssc100_name));
2780Sstevel@tonic-gate }
2790Sstevel@tonic-gate return (err);
2800Sstevel@tonic-gate }
2810Sstevel@tonic-gate
2820Sstevel@tonic-gate static int
ssc100_set(struct ssc100_unit * unitp,uchar_t byte)2830Sstevel@tonic-gate ssc100_set(struct ssc100_unit *unitp, uchar_t byte)
2840Sstevel@tonic-gate {
2850Sstevel@tonic-gate int err = I2C_SUCCESS;
2860Sstevel@tonic-gate
2870Sstevel@tonic-gate err = ssc100_common(unitp, NULL, byte, I2C_WR);
2880Sstevel@tonic-gate if (err) {
2890Sstevel@tonic-gate D2CMN_ERR((CE_WARN, "%s: Failed in SSC100_SET "
2907656SSherry.Moore@Sun.COM "i2c_common routine", unitp->ssc100_name));
2910Sstevel@tonic-gate }
2920Sstevel@tonic-gate return (err);
2930Sstevel@tonic-gate }
2940Sstevel@tonic-gate
2950Sstevel@tonic-gate static int
ssc100_ioctl(dev_t dev,int cmd,intptr_t arg,int mode,cred_t * credp,int * rvalp)2960Sstevel@tonic-gate ssc100_ioctl(dev_t dev, int cmd, intptr_t arg, int mode,
2970Sstevel@tonic-gate cred_t *credp, int *rvalp)
2980Sstevel@tonic-gate {
2990Sstevel@tonic-gate _NOTE(ARGUNUSED(credp, rvalp))
3000Sstevel@tonic-gate
3010Sstevel@tonic-gate struct ssc100_unit *unitp;
3020Sstevel@tonic-gate int instance;
3030Sstevel@tonic-gate int err = 0;
3040Sstevel@tonic-gate i2c_bit_t ioctl_bit;
3050Sstevel@tonic-gate i2c_port_t ioctl_port;
3060Sstevel@tonic-gate i2c_reg_t ioctl_reg;
3070Sstevel@tonic-gate uchar_t byte;
3080Sstevel@tonic-gate
3090Sstevel@tonic-gate if (arg == NULL) {
3100Sstevel@tonic-gate D2CMN_ERR((CE_WARN, "SSC100: ioctl: arg passed in to ioctl "
3117656SSherry.Moore@Sun.COM "= NULL"));
3120Sstevel@tonic-gate err = EINVAL;
3130Sstevel@tonic-gate return (err);
3140Sstevel@tonic-gate }
3150Sstevel@tonic-gate
3160Sstevel@tonic-gate instance = getminor(dev);
3170Sstevel@tonic-gate unitp = (struct ssc100_unit *)
3187656SSherry.Moore@Sun.COM ddi_get_soft_state(ssc100soft_statep, instance);
3190Sstevel@tonic-gate if (unitp == NULL) {
3200Sstevel@tonic-gate cmn_err(CE_WARN, "SSC100: ioctl: unitp not filled");
3210Sstevel@tonic-gate return (ENOMEM);
3220Sstevel@tonic-gate }
3230Sstevel@tonic-gate
3240Sstevel@tonic-gate mutex_enter(&unitp->ssc100_mutex);
3250Sstevel@tonic-gate
3260Sstevel@tonic-gate switch (cmd) {
3270Sstevel@tonic-gate case I2C_GET_PORT:
3280Sstevel@tonic-gate if (ddi_copyin((caddr_t)arg, (caddr_t)&ioctl_port,
3297656SSherry.Moore@Sun.COM sizeof (i2c_port_t), mode) != DDI_SUCCESS) {
3300Sstevel@tonic-gate D2CMN_ERR((CE_WARN, "%s: Failed in the I2C_GET_PORT"
3317656SSherry.Moore@Sun.COM " ddi_copyin routine", unitp->ssc100_name));
3320Sstevel@tonic-gate err = EFAULT;
3330Sstevel@tonic-gate break;
3340Sstevel@tonic-gate }
3350Sstevel@tonic-gate
3360Sstevel@tonic-gate err = ssc100_get(unitp, &byte);
3370Sstevel@tonic-gate if (err != I2C_SUCCESS) {
3380Sstevel@tonic-gate D2CMN_ERR((CE_WARN, "%s: Failed in the I2C_GET_PORT"
3397656SSherry.Moore@Sun.COM " ssc100_get routine", unitp->ssc100_name));
3400Sstevel@tonic-gate break;
3410Sstevel@tonic-gate }
3420Sstevel@tonic-gate
3430Sstevel@tonic-gate ioctl_port.value = byte;
3440Sstevel@tonic-gate if (ddi_copyout((caddr_t)&ioctl_port, (caddr_t)arg,
3457656SSherry.Moore@Sun.COM sizeof (i2c_port_t), mode) != DDI_SUCCESS) {
3460Sstevel@tonic-gate D2CMN_ERR((CE_WARN, "%s: Failed in I2C_GET_PORT "
3477656SSherry.Moore@Sun.COM "ddi_copyout routine", unitp->ssc100_name));
3480Sstevel@tonic-gate err = EFAULT;
3490Sstevel@tonic-gate }
3500Sstevel@tonic-gate
3510Sstevel@tonic-gate D1CMN_ERR((CE_NOTE, "%s: contains %x", unitp->ssc100_name,
3527656SSherry.Moore@Sun.COM byte));
3530Sstevel@tonic-gate break;
3540Sstevel@tonic-gate
3550Sstevel@tonic-gate case I2C_SET_PORT:
3560Sstevel@tonic-gate if (ddi_copyin((caddr_t)arg, (caddr_t)&ioctl_port,
3577656SSherry.Moore@Sun.COM sizeof (uint8_t), mode) != DDI_SUCCESS) {
3580Sstevel@tonic-gate D2CMN_ERR((CE_WARN, "%s: Failed in the I2C_SET_PORT"
3597656SSherry.Moore@Sun.COM "ddi_cpoyin routine", unitp->ssc100_name));
3600Sstevel@tonic-gate err = EFAULT;
3610Sstevel@tonic-gate break;
3620Sstevel@tonic-gate }
3630Sstevel@tonic-gate
3640Sstevel@tonic-gate err = ssc100_set(unitp, ioctl_port.value);
3650Sstevel@tonic-gate if (err != I2C_SUCCESS) {
3660Sstevel@tonic-gate D2CMN_ERR((CE_WARN, "%s: Failed in the I2C_SET_PORT"
3677656SSherry.Moore@Sun.COM " ssc100_set routine", unitp->ssc100_name));
3680Sstevel@tonic-gate break;
3690Sstevel@tonic-gate }
3700Sstevel@tonic-gate break;
3710Sstevel@tonic-gate
3720Sstevel@tonic-gate case I2C_GET_BIT:
3730Sstevel@tonic-gate if (ddi_copyin((caddr_t)arg, (caddr_t)&ioctl_bit,
3747656SSherry.Moore@Sun.COM sizeof (i2c_bit_t), mode) != DDI_SUCCESS) {
3750Sstevel@tonic-gate D2CMN_ERR((CE_WARN, "%s: Failed in the I2C_GET_BIT"
3767656SSherry.Moore@Sun.COM " ddi_copyin routine", unitp->ssc100_name));
3770Sstevel@tonic-gate err = EFAULT;
3780Sstevel@tonic-gate break;
3790Sstevel@tonic-gate }
3800Sstevel@tonic-gate
381946Smathue if (ioctl_bit.bit_num > 7) {
3820Sstevel@tonic-gate D2CMN_ERR((CE_WARN, "%s: In I2C_GET_BIT bit num"
3837656SSherry.Moore@Sun.COM " was not between 0 and 7",
3847656SSherry.Moore@Sun.COM unitp->ssc100_name));
3850Sstevel@tonic-gate err = EIO;
3860Sstevel@tonic-gate break;
3870Sstevel@tonic-gate }
3880Sstevel@tonic-gate
3890Sstevel@tonic-gate err = ssc100_get(unitp, &byte);
3900Sstevel@tonic-gate if (err != I2C_SUCCESS) {
3910Sstevel@tonic-gate D2CMN_ERR((CE_WARN, "%s: Failed in the I2C_GET_BIT"
3927656SSherry.Moore@Sun.COM " ssc100_get routine", unitp->ssc100_name));
3930Sstevel@tonic-gate break;
3940Sstevel@tonic-gate }
3950Sstevel@tonic-gate
3960Sstevel@tonic-gate D1CMN_ERR((CE_NOTE, "%s: byte returned from device is %x",
3977656SSherry.Moore@Sun.COM unitp->ssc100_name, byte));
3980Sstevel@tonic-gate ioctl_bit.bit_value = (boolean_t)SSC100_BIT_READ_MASK(byte,
3990Sstevel@tonic-gate ioctl_bit.bit_num);
4000Sstevel@tonic-gate D1CMN_ERR((CE_NOTE, "%s: byte now contains %x",
4017656SSherry.Moore@Sun.COM unitp->ssc100_name, byte));
4020Sstevel@tonic-gate
4030Sstevel@tonic-gate if (ddi_copyout((caddr_t)&ioctl_bit, (caddr_t)arg,
4047656SSherry.Moore@Sun.COM sizeof (i2c_bit_t), mode) != DDI_SUCCESS) {
4050Sstevel@tonic-gate D2CMN_ERR((CE_WARN, "%s: Failed in I2C_GET_BIT"
4067656SSherry.Moore@Sun.COM " ddi_copyout routine", unitp->ssc100_name));
4070Sstevel@tonic-gate err = EFAULT;
4080Sstevel@tonic-gate }
4090Sstevel@tonic-gate break;
4100Sstevel@tonic-gate
4110Sstevel@tonic-gate case I2C_SET_BIT:
4120Sstevel@tonic-gate if (ddi_copyin((caddr_t)arg, (caddr_t)&ioctl_bit,
4137656SSherry.Moore@Sun.COM sizeof (i2c_bit_t), mode) != DDI_SUCCESS) {
4140Sstevel@tonic-gate D2CMN_ERR((CE_WARN, "%s: Failed in I2C_SET_BIT"
4157656SSherry.Moore@Sun.COM " ddi_copyin routine", unitp->ssc100_name));
4160Sstevel@tonic-gate err = EFAULT;
4170Sstevel@tonic-gate break;
4180Sstevel@tonic-gate }
4190Sstevel@tonic-gate
420946Smathue if (ioctl_bit.bit_num > 7) {
4210Sstevel@tonic-gate D2CMN_ERR((CE_WARN, "%s: I2C_SET_BIT: bit_num sent"
4227656SSherry.Moore@Sun.COM " in was not between 0 and 7",
4237656SSherry.Moore@Sun.COM unitp->ssc100_name));
4240Sstevel@tonic-gate err = EIO;
4250Sstevel@tonic-gate break;
4260Sstevel@tonic-gate }
4270Sstevel@tonic-gate
4280Sstevel@tonic-gate err = ssc100_get(unitp, &byte);
4290Sstevel@tonic-gate if (err != I2C_SUCCESS) {
4300Sstevel@tonic-gate D2CMN_ERR((CE_WARN, "%s: Failed in the I2C_SET_BIT"
4317656SSherry.Moore@Sun.COM " ssc100_get routine", unitp->ssc100_name));
4320Sstevel@tonic-gate break;
4330Sstevel@tonic-gate }
4340Sstevel@tonic-gate
4350Sstevel@tonic-gate D1CMN_ERR((CE_NOTE, "%s: byte returned from device is %x",
4367656SSherry.Moore@Sun.COM unitp->ssc100_name, byte));
4370Sstevel@tonic-gate byte = SSC100_BIT_WRITE_MASK(byte, ioctl_bit.bit_num,
4380Sstevel@tonic-gate ioctl_bit.bit_value);
4390Sstevel@tonic-gate D1CMN_ERR((CE_NOTE, "%s: byte after shifting is %x",
4407656SSherry.Moore@Sun.COM unitp->ssc100_name, byte));
4410Sstevel@tonic-gate
4420Sstevel@tonic-gate err = ssc100_set(unitp, byte);
4430Sstevel@tonic-gate if (err != I2C_SUCCESS) {
4440Sstevel@tonic-gate D2CMN_ERR((CE_WARN, "%s: Failed in the I2C_SET_BIT"
4457656SSherry.Moore@Sun.COM " ssc100_set routine", unitp->ssc100_name));
4460Sstevel@tonic-gate break;
4470Sstevel@tonic-gate }
4480Sstevel@tonic-gate break;
4490Sstevel@tonic-gate
4500Sstevel@tonic-gate case I2C_GET_REG:
4510Sstevel@tonic-gate if (ddi_copyin((caddr_t)arg, (caddr_t)&ioctl_reg,
4527656SSherry.Moore@Sun.COM sizeof (i2c_reg_t), mode) != DDI_SUCCESS) {
4530Sstevel@tonic-gate D2CMN_ERR((CE_WARN, "%s: Failed in I2C_GET_REG "
4540Sstevel@tonic-gate "ddi_copyin routine", unitp->ssc100_name));
4550Sstevel@tonic-gate err = EFAULT;
4560Sstevel@tonic-gate break;
4570Sstevel@tonic-gate }
4580Sstevel@tonic-gate
4590Sstevel@tonic-gate err = ssc100_get_reg(unitp, &byte, ioctl_reg.reg_num);
4600Sstevel@tonic-gate
4610Sstevel@tonic-gate ioctl_reg.reg_value = byte;
4620Sstevel@tonic-gate if (ddi_copyout((caddr_t)&ioctl_reg, (caddr_t)arg,
4637656SSherry.Moore@Sun.COM sizeof (i2c_reg_t), mode) != DDI_SUCCESS) {
4640Sstevel@tonic-gate D2CMN_ERR((CE_WARN, "%s: Failed in I2C_GET_REG "
4650Sstevel@tonic-gate "ddi_copyout routine", unitp->ssc100_name));
4660Sstevel@tonic-gate err = EFAULT;
4670Sstevel@tonic-gate }
4680Sstevel@tonic-gate break;
4690Sstevel@tonic-gate
4700Sstevel@tonic-gate default:
4710Sstevel@tonic-gate D2CMN_ERR((CE_WARN, "%s: Invalid IOCTL cmd: %x",
4727656SSherry.Moore@Sun.COM unitp->ssc100_name, cmd));
4730Sstevel@tonic-gate err = EINVAL;
4740Sstevel@tonic-gate }
4750Sstevel@tonic-gate
4760Sstevel@tonic-gate mutex_exit(&unitp->ssc100_mutex);
4770Sstevel@tonic-gate return (err);
4780Sstevel@tonic-gate }
4790Sstevel@tonic-gate
4800Sstevel@tonic-gate static int
ssc100_attach(dev_info_t * dip,ddi_attach_cmd_t cmd)4810Sstevel@tonic-gate ssc100_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
4820Sstevel@tonic-gate {
4830Sstevel@tonic-gate switch (cmd) {
4840Sstevel@tonic-gate case DDI_ATTACH:
4850Sstevel@tonic-gate return (ssc100_do_attach(dip));
4860Sstevel@tonic-gate case DDI_RESUME:
4870Sstevel@tonic-gate return (ssc100_do_resume());
4880Sstevel@tonic-gate default:
4890Sstevel@tonic-gate return (DDI_FAILURE);
4900Sstevel@tonic-gate }
4910Sstevel@tonic-gate }
4920Sstevel@tonic-gate
4930Sstevel@tonic-gate static int
ssc100_detach(dev_info_t * dip,ddi_detach_cmd_t cmd)4940Sstevel@tonic-gate ssc100_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
4950Sstevel@tonic-gate {
4960Sstevel@tonic-gate switch (cmd) {
4970Sstevel@tonic-gate case DDI_DETACH:
4980Sstevel@tonic-gate return (ssc100_do_detach(dip));
4990Sstevel@tonic-gate case DDI_SUSPEND:
5000Sstevel@tonic-gate return (ssc100_do_suspend());
5010Sstevel@tonic-gate default:
5020Sstevel@tonic-gate return (DDI_FAILURE);
5030Sstevel@tonic-gate }
5040Sstevel@tonic-gate }
5050Sstevel@tonic-gate
5060Sstevel@tonic-gate static int
ssc100_do_attach(dev_info_t * dip)5070Sstevel@tonic-gate ssc100_do_attach(dev_info_t *dip)
5080Sstevel@tonic-gate {
5090Sstevel@tonic-gate struct ssc100_unit *unitp;
5100Sstevel@tonic-gate int instance;
5110Sstevel@tonic-gate
5120Sstevel@tonic-gate instance = ddi_get_instance(dip);
5130Sstevel@tonic-gate
5140Sstevel@tonic-gate if (ddi_soft_state_zalloc(ssc100soft_statep, instance) != 0) {
5150Sstevel@tonic-gate cmn_err(CE_WARN, "%s%d: failed to zalloc softstate",
5167656SSherry.Moore@Sun.COM ddi_get_name(dip), instance);
5170Sstevel@tonic-gate return (DDI_FAILURE);
5180Sstevel@tonic-gate }
5190Sstevel@tonic-gate
5200Sstevel@tonic-gate unitp = ddi_get_soft_state(ssc100soft_statep, instance);
5210Sstevel@tonic-gate
5220Sstevel@tonic-gate if (unitp == NULL) {
5230Sstevel@tonic-gate cmn_err(CE_WARN, "%s%d: unitp not filled",
5247656SSherry.Moore@Sun.COM ddi_get_name(dip), instance);
5250Sstevel@tonic-gate return (ENOMEM);
5260Sstevel@tonic-gate }
5270Sstevel@tonic-gate
5280Sstevel@tonic-gate (void) snprintf(unitp->ssc100_name, sizeof (unitp->ssc100_name),
5297656SSherry.Moore@Sun.COM "%s%d", ddi_node_name(dip), instance);
5300Sstevel@tonic-gate
5310Sstevel@tonic-gate if (ddi_create_minor_node(dip, "ssc100", S_IFCHR, instance,
5327656SSherry.Moore@Sun.COM "ddi_i2c:ioexp", NULL) == DDI_FAILURE) {
5330Sstevel@tonic-gate cmn_err(CE_WARN, "%s ddi_create_minor_node failed for "
5347656SSherry.Moore@Sun.COM "%s", unitp->ssc100_name, "ssc100");
5350Sstevel@tonic-gate ddi_soft_state_free(ssc100soft_statep, instance);
5360Sstevel@tonic-gate
5370Sstevel@tonic-gate return (DDI_FAILURE);
5380Sstevel@tonic-gate }
5390Sstevel@tonic-gate
5400Sstevel@tonic-gate /*
5410Sstevel@tonic-gate * If we had different sizes in the future, this could be read
5420Sstevel@tonic-gate * from a property.
5430Sstevel@tonic-gate */
5440Sstevel@tonic-gate unitp->ssc100_size = SSC100_SIZE;
5450Sstevel@tonic-gate
5460Sstevel@tonic-gate (void) ddi_prop_create(DDI_DEV_T_NONE, dip,
5477656SSherry.Moore@Sun.COM DDI_PROP_CANSLEEP, "size",
5487656SSherry.Moore@Sun.COM (caddr_t)&unitp->ssc100_size, sizeof (unitp->ssc100_size));
5490Sstevel@tonic-gate
5500Sstevel@tonic-gate if (i2c_client_register(dip, &unitp->ssc100_hdl) != I2C_SUCCESS) {
5510Sstevel@tonic-gate ddi_remove_minor_node(dip, NULL);
5520Sstevel@tonic-gate ddi_soft_state_free(ssc100soft_statep, instance);
5530Sstevel@tonic-gate
5540Sstevel@tonic-gate return (DDI_FAILURE);
5550Sstevel@tonic-gate }
5560Sstevel@tonic-gate
5570Sstevel@tonic-gate mutex_init(&unitp->ssc100_mutex, NULL, MUTEX_DRIVER, NULL);
5580Sstevel@tonic-gate
5590Sstevel@tonic-gate return (DDI_SUCCESS);
5600Sstevel@tonic-gate }
5610Sstevel@tonic-gate
5620Sstevel@tonic-gate static int
ssc100_do_resume()5630Sstevel@tonic-gate ssc100_do_resume()
5640Sstevel@tonic-gate {
5650Sstevel@tonic-gate int ret = DDI_SUCCESS;
5660Sstevel@tonic-gate
5670Sstevel@tonic-gate return (ret);
5680Sstevel@tonic-gate }
5690Sstevel@tonic-gate
5700Sstevel@tonic-gate static int
ssc100_do_suspend()5710Sstevel@tonic-gate ssc100_do_suspend()
5720Sstevel@tonic-gate {
5730Sstevel@tonic-gate int ret = DDI_SUCCESS;
5740Sstevel@tonic-gate
5750Sstevel@tonic-gate return (ret);
5760Sstevel@tonic-gate }
5770Sstevel@tonic-gate
5780Sstevel@tonic-gate static int
ssc100_do_detach(dev_info_t * dip)5790Sstevel@tonic-gate ssc100_do_detach(dev_info_t *dip)
5800Sstevel@tonic-gate {
5810Sstevel@tonic-gate struct ssc100_unit *unitp;
5820Sstevel@tonic-gate int instance;
5830Sstevel@tonic-gate
5840Sstevel@tonic-gate instance = ddi_get_instance(dip);
5850Sstevel@tonic-gate
5860Sstevel@tonic-gate unitp = ddi_get_soft_state(ssc100soft_statep, instance);
5870Sstevel@tonic-gate
5880Sstevel@tonic-gate i2c_client_unregister(unitp->ssc100_hdl);
5890Sstevel@tonic-gate
5900Sstevel@tonic-gate ddi_remove_minor_node(dip, NULL);
5910Sstevel@tonic-gate
5920Sstevel@tonic-gate mutex_destroy(&unitp->ssc100_mutex);
5930Sstevel@tonic-gate
5940Sstevel@tonic-gate ddi_soft_state_free(ssc100soft_statep, instance);
5950Sstevel@tonic-gate
5960Sstevel@tonic-gate return (DDI_SUCCESS);
5970Sstevel@tonic-gate
5980Sstevel@tonic-gate }
5990Sstevel@tonic-gate
6000Sstevel@tonic-gate static int
ssc100_read(dev_t dev,struct uio * uiop,cred_t * cred_p)6010Sstevel@tonic-gate ssc100_read(dev_t dev, struct uio *uiop, cred_t *cred_p)
6020Sstevel@tonic-gate {
6030Sstevel@tonic-gate _NOTE(ARGUNUSED(cred_p))
6040Sstevel@tonic-gate
6050Sstevel@tonic-gate return (ssc100_io(dev, uiop, B_READ));
6060Sstevel@tonic-gate }
6070Sstevel@tonic-gate
6080Sstevel@tonic-gate static int
ssc100_write(dev_t dev,struct uio * uiop,cred_t * cred_p)6090Sstevel@tonic-gate ssc100_write(dev_t dev, struct uio *uiop, cred_t *cred_p)
6100Sstevel@tonic-gate {
6110Sstevel@tonic-gate _NOTE(ARGUNUSED(cred_p))
6120Sstevel@tonic-gate
6130Sstevel@tonic-gate return (ssc100_io(dev, uiop, B_WRITE));
6140Sstevel@tonic-gate }
6150Sstevel@tonic-gate
6160Sstevel@tonic-gate static int
ssc100_io(dev_t dev,struct uio * uiop,int rw)6170Sstevel@tonic-gate ssc100_io(dev_t dev, struct uio *uiop, int rw)
6180Sstevel@tonic-gate {
6190Sstevel@tonic-gate struct ssc100_unit *unitp;
6200Sstevel@tonic-gate int instance = getminor(dev);
6210Sstevel@tonic-gate int ssc100_addr;
6220Sstevel@tonic-gate int bytes_to_rw;
6230Sstevel@tonic-gate int err = 0;
6240Sstevel@tonic-gate int current_xfer_len;
6250Sstevel@tonic-gate i2c_transfer_t *i2ctp = NULL;
6260Sstevel@tonic-gate
6270Sstevel@tonic-gate if (instance < 0) {
6280Sstevel@tonic-gate return (ENXIO);
6290Sstevel@tonic-gate }
6300Sstevel@tonic-gate
6310Sstevel@tonic-gate unitp = (struct ssc100_unit *)
6327656SSherry.Moore@Sun.COM ddi_get_soft_state(ssc100soft_statep, instance);
6330Sstevel@tonic-gate
6340Sstevel@tonic-gate
6350Sstevel@tonic-gate if (unitp == NULL) {
6360Sstevel@tonic-gate return (ENXIO);
6370Sstevel@tonic-gate }
6380Sstevel@tonic-gate
6390Sstevel@tonic-gate if (uiop->uio_offset >= unitp->ssc100_size) {
6400Sstevel@tonic-gate /*
6410Sstevel@tonic-gate * Exceeded ssc100 size.
6420Sstevel@tonic-gate */
6430Sstevel@tonic-gate if (rw == B_WRITE) {
6440Sstevel@tonic-gate
6450Sstevel@tonic-gate return (ENOSPC);
6460Sstevel@tonic-gate }
6470Sstevel@tonic-gate return (0);
6480Sstevel@tonic-gate }
6490Sstevel@tonic-gate
6500Sstevel@tonic-gate ssc100_addr = uiop->uio_offset;
6510Sstevel@tonic-gate
6520Sstevel@tonic-gate if (uiop->uio_resid == 0) {
6530Sstevel@tonic-gate return (0);
6540Sstevel@tonic-gate }
6550Sstevel@tonic-gate
6560Sstevel@tonic-gate bytes_to_rw = min(uiop->uio_resid,
6577656SSherry.Moore@Sun.COM unitp->ssc100_size - uiop->uio_offset);
6580Sstevel@tonic-gate current_xfer_len = bytes_to_rw;
6590Sstevel@tonic-gate
6600Sstevel@tonic-gate if (rw == B_WRITE) {
6610Sstevel@tonic-gate (void) i2c_transfer_alloc(unitp->ssc100_hdl, &i2ctp,
6627656SSherry.Moore@Sun.COM current_xfer_len+1, 0, I2C_SLEEP);
6630Sstevel@tonic-gate if (i2ctp == NULL) {
6640Sstevel@tonic-gate D2CMN_ERR((CE_WARN, "%s: Failed in ssc100_io WRITE "
6657656SSherry.Moore@Sun.COM "i2c_tran_pointer not allocated",
6667656SSherry.Moore@Sun.COM unitp->ssc100_name));
6670Sstevel@tonic-gate return (ENOMEM);
6680Sstevel@tonic-gate }
6690Sstevel@tonic-gate i2ctp->i2c_version = I2C_XFER_REV;
6700Sstevel@tonic-gate i2ctp->i2c_flags = I2C_WR;
6710Sstevel@tonic-gate i2ctp->i2c_wbuf[0] = (uchar_t)ssc100_addr;
6720Sstevel@tonic-gate if ((err = uiomove(&i2ctp->i2c_wbuf[1], current_xfer_len,
6737656SSherry.Moore@Sun.COM UIO_WRITE, uiop)) != 0) {
6740Sstevel@tonic-gate D2CMN_ERR((CE_WARN, "%s: Failed in ssc100_io WRITE "
6757656SSherry.Moore@Sun.COM "uiomove failed", unitp->ssc100_name));
6760Sstevel@tonic-gate goto end;
6770Sstevel@tonic-gate }
6780Sstevel@tonic-gate
6790Sstevel@tonic-gate if ((err = i2c_transfer(unitp->ssc100_hdl, i2ctp)) !=
6800Sstevel@tonic-gate I2C_SUCCESS) {
6810Sstevel@tonic-gate D2CMN_ERR((CE_WARN, "%s: Failed in ssc100_io WRITE "
6827656SSherry.Moore@Sun.COM "i2c_transfer failed", unitp->ssc100_name));
6830Sstevel@tonic-gate goto end;
6840Sstevel@tonic-gate }
6850Sstevel@tonic-gate } else {
6860Sstevel@tonic-gate /*
6870Sstevel@tonic-gate * SSC100 read. We need to first write out the address
6880Sstevel@tonic-gate * that we wish to read.
6890Sstevel@tonic-gate */
6900Sstevel@tonic-gate (void) i2c_transfer_alloc(unitp->ssc100_hdl, &i2ctp, 1,
6910Sstevel@tonic-gate current_xfer_len, I2C_SLEEP);
6920Sstevel@tonic-gate if (i2ctp == NULL) {
6930Sstevel@tonic-gate D2CMN_ERR((CE_WARN, "%s: Failed in ssc100_io READ "
6947656SSherry.Moore@Sun.COM "i2c_tran_pointer not allocated",
6957656SSherry.Moore@Sun.COM unitp->ssc100_name));
6960Sstevel@tonic-gate return (ENOMEM);
6970Sstevel@tonic-gate }
6980Sstevel@tonic-gate i2ctp->i2c_version = I2C_XFER_REV;
6990Sstevel@tonic-gate i2ctp->i2c_wbuf[0] = (uchar_t)ssc100_addr;
7000Sstevel@tonic-gate i2ctp->i2c_flags = I2C_WR_RD;
7010Sstevel@tonic-gate
7020Sstevel@tonic-gate if ((err = i2c_transfer(unitp->ssc100_hdl, i2ctp)) !=
7030Sstevel@tonic-gate I2C_SUCCESS) {
7040Sstevel@tonic-gate D2CMN_ERR((CE_WARN, "%s: Failed in ssc100_io READ "
7057656SSherry.Moore@Sun.COM "i2c_transfer failed", unitp->ssc100_name));
7060Sstevel@tonic-gate goto end;
7070Sstevel@tonic-gate }
7080Sstevel@tonic-gate
7090Sstevel@tonic-gate if ((err = uiomove(i2ctp->i2c_rbuf, current_xfer_len,
7107656SSherry.Moore@Sun.COM UIO_READ, uiop)) != 0) {
7110Sstevel@tonic-gate D2CMN_ERR((CE_WARN, "%s: Failed in ssc100_io READ "
7127656SSherry.Moore@Sun.COM "uiomove failed", unitp->ssc100_name));
7130Sstevel@tonic-gate goto end;
7140Sstevel@tonic-gate }
7150Sstevel@tonic-gate }
7160Sstevel@tonic-gate
7170Sstevel@tonic-gate end:
7180Sstevel@tonic-gate i2c_transfer_free(unitp->ssc100_hdl, i2ctp);
7190Sstevel@tonic-gate return (err);
7200Sstevel@tonic-gate }
721