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