17c478bd9Sstevel@tonic-gate /* 27c478bd9Sstevel@tonic-gate * CDDL HEADER START 37c478bd9Sstevel@tonic-gate * 47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 57c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 67c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 77c478bd9Sstevel@tonic-gate * with the License. 87c478bd9Sstevel@tonic-gate * 97c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 107c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 117c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 127c478bd9Sstevel@tonic-gate * and limitations under the License. 137c478bd9Sstevel@tonic-gate * 147c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 157c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 167c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 177c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 187c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 197c478bd9Sstevel@tonic-gate * 207c478bd9Sstevel@tonic-gate * CDDL HEADER END 217c478bd9Sstevel@tonic-gate */ 227c478bd9Sstevel@tonic-gate /* 23*f47a9c50Smathue * Copyright 2005 Sun Microsystems, Inc. All rights reserved. 24*f47a9c50Smathue * Use is subject to license terms. 257c478bd9Sstevel@tonic-gate */ 267c478bd9Sstevel@tonic-gate 277c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 287c478bd9Sstevel@tonic-gate 297c478bd9Sstevel@tonic-gate 307c478bd9Sstevel@tonic-gate #include <sys/stat.h> /* ddi_create_minor_node S_IFCHR */ 317c478bd9Sstevel@tonic-gate #include <sys/modctl.h> /* for modldrv */ 327c478bd9Sstevel@tonic-gate #include <sys/open.h> /* for open params. */ 337c478bd9Sstevel@tonic-gate #include <sys/types.h> 347c478bd9Sstevel@tonic-gate #include <sys/kmem.h> 357c478bd9Sstevel@tonic-gate #include <sys/sunddi.h> 367c478bd9Sstevel@tonic-gate #include <sys/conf.h> /* req. by dev_ops flags MTSAFE etc. */ 377c478bd9Sstevel@tonic-gate #include <sys/ddi.h> 387c478bd9Sstevel@tonic-gate #include <sys/file.h> 397c478bd9Sstevel@tonic-gate #include <sys/note.h> 407c478bd9Sstevel@tonic-gate 417c478bd9Sstevel@tonic-gate #include <sys/i2c/clients/pcf8574_impl.h> 427c478bd9Sstevel@tonic-gate 437c478bd9Sstevel@tonic-gate static void *pcf8574soft_statep; 447c478bd9Sstevel@tonic-gate 457c478bd9Sstevel@tonic-gate static int pcf8574_do_attach(dev_info_t *); 467c478bd9Sstevel@tonic-gate static int pcf8574_do_detach(dev_info_t *); 477c478bd9Sstevel@tonic-gate static int pcf8574_do_resume(void); 487c478bd9Sstevel@tonic-gate static int pcf8574_do_suspend(void); 497c478bd9Sstevel@tonic-gate static int pcf8574_get(struct pcf8574_unit *, uchar_t *); 507c478bd9Sstevel@tonic-gate static int pcf8574_set(struct pcf8574_unit *, uchar_t); 517c478bd9Sstevel@tonic-gate 527c478bd9Sstevel@tonic-gate /* 537c478bd9Sstevel@tonic-gate * cb ops (only need ioctl) 547c478bd9Sstevel@tonic-gate */ 557c478bd9Sstevel@tonic-gate static int pcf8574_open(dev_t *, int, int, cred_t *); 567c478bd9Sstevel@tonic-gate static int pcf8574_close(dev_t, int, int, cred_t *); 577c478bd9Sstevel@tonic-gate static int pcf8574_ioctl(dev_t, int, intptr_t, int, cred_t *, int *); 587c478bd9Sstevel@tonic-gate 597c478bd9Sstevel@tonic-gate static struct cb_ops pcf8574_cbops = { 607c478bd9Sstevel@tonic-gate pcf8574_open, /* open */ 617c478bd9Sstevel@tonic-gate pcf8574_close, /* close */ 627c478bd9Sstevel@tonic-gate nodev, /* strategy */ 637c478bd9Sstevel@tonic-gate nodev, /* print */ 647c478bd9Sstevel@tonic-gate nodev, /* dump */ 657c478bd9Sstevel@tonic-gate nodev, /* read */ 667c478bd9Sstevel@tonic-gate nodev, /* write */ 677c478bd9Sstevel@tonic-gate pcf8574_ioctl, /* ioctl */ 687c478bd9Sstevel@tonic-gate nodev, /* devmap */ 697c478bd9Sstevel@tonic-gate nodev, /* mmap */ 707c478bd9Sstevel@tonic-gate nodev, /* segmap */ 717c478bd9Sstevel@tonic-gate nochpoll, /* poll */ 727c478bd9Sstevel@tonic-gate ddi_prop_op, /* cb_prop_op */ 737c478bd9Sstevel@tonic-gate NULL, /* streamtab */ 747c478bd9Sstevel@tonic-gate D_NEW | D_MP | D_HOTPLUG, /* Driver compatibility flag */ 757c478bd9Sstevel@tonic-gate CB_REV, /* rev */ 767c478bd9Sstevel@tonic-gate nodev, /* int (*cb_aread)() */ 777c478bd9Sstevel@tonic-gate nodev /* int (*cb_awrite)() */ 787c478bd9Sstevel@tonic-gate }; 797c478bd9Sstevel@tonic-gate 807c478bd9Sstevel@tonic-gate /* 817c478bd9Sstevel@tonic-gate * dev ops 827c478bd9Sstevel@tonic-gate */ 837c478bd9Sstevel@tonic-gate static int pcf8574_attach(dev_info_t *dip, ddi_attach_cmd_t cmd); 847c478bd9Sstevel@tonic-gate static int pcf8574_detach(dev_info_t *dip, ddi_detach_cmd_t cmd); 857c478bd9Sstevel@tonic-gate 867c478bd9Sstevel@tonic-gate static struct dev_ops pcf8574_ops = { 877c478bd9Sstevel@tonic-gate DEVO_REV, 887c478bd9Sstevel@tonic-gate 0, 897c478bd9Sstevel@tonic-gate ddi_getinfo_1to1, 907c478bd9Sstevel@tonic-gate nulldev, 917c478bd9Sstevel@tonic-gate nulldev, 927c478bd9Sstevel@tonic-gate pcf8574_attach, 937c478bd9Sstevel@tonic-gate pcf8574_detach, 947c478bd9Sstevel@tonic-gate nodev, 957c478bd9Sstevel@tonic-gate &pcf8574_cbops, 967c478bd9Sstevel@tonic-gate NULL 977c478bd9Sstevel@tonic-gate }; 987c478bd9Sstevel@tonic-gate 997c478bd9Sstevel@tonic-gate extern struct mod_ops mod_driverops; 1007c478bd9Sstevel@tonic-gate 1017c478bd9Sstevel@tonic-gate static struct modldrv pcf8574_modldrv = { 1027c478bd9Sstevel@tonic-gate &mod_driverops, /* type of module - driver */ 1037c478bd9Sstevel@tonic-gate "PCF8574 i2c device driver: v%I%", 1047c478bd9Sstevel@tonic-gate &pcf8574_ops 1057c478bd9Sstevel@tonic-gate }; 1067c478bd9Sstevel@tonic-gate 1077c478bd9Sstevel@tonic-gate static struct modlinkage pcf8574_modlinkage = { 1087c478bd9Sstevel@tonic-gate MODREV_1, 1097c478bd9Sstevel@tonic-gate &pcf8574_modldrv, 1107c478bd9Sstevel@tonic-gate 0 1117c478bd9Sstevel@tonic-gate }; 1127c478bd9Sstevel@tonic-gate 1137c478bd9Sstevel@tonic-gate 1147c478bd9Sstevel@tonic-gate int 1157c478bd9Sstevel@tonic-gate _init(void) 1167c478bd9Sstevel@tonic-gate { 1177c478bd9Sstevel@tonic-gate int error; 1187c478bd9Sstevel@tonic-gate 1197c478bd9Sstevel@tonic-gate error = mod_install(&pcf8574_modlinkage); 1207c478bd9Sstevel@tonic-gate 1217c478bd9Sstevel@tonic-gate if (!error) 1227c478bd9Sstevel@tonic-gate (void) ddi_soft_state_init(&pcf8574soft_statep, 1237c478bd9Sstevel@tonic-gate sizeof (struct pcf8574_unit), 1); 1247c478bd9Sstevel@tonic-gate return (error); 1257c478bd9Sstevel@tonic-gate } 1267c478bd9Sstevel@tonic-gate 1277c478bd9Sstevel@tonic-gate int 1287c478bd9Sstevel@tonic-gate _fini(void) 1297c478bd9Sstevel@tonic-gate { 1307c478bd9Sstevel@tonic-gate int error; 1317c478bd9Sstevel@tonic-gate 1327c478bd9Sstevel@tonic-gate error = mod_remove(&pcf8574_modlinkage); 1337c478bd9Sstevel@tonic-gate if (!error) 1347c478bd9Sstevel@tonic-gate ddi_soft_state_fini(&pcf8574soft_statep); 1357c478bd9Sstevel@tonic-gate 1367c478bd9Sstevel@tonic-gate return (error); 1377c478bd9Sstevel@tonic-gate } 1387c478bd9Sstevel@tonic-gate 1397c478bd9Sstevel@tonic-gate int 1407c478bd9Sstevel@tonic-gate _info(struct modinfo *modinfop) 1417c478bd9Sstevel@tonic-gate { 1427c478bd9Sstevel@tonic-gate return (mod_info(&pcf8574_modlinkage, modinfop)); 1437c478bd9Sstevel@tonic-gate } 1447c478bd9Sstevel@tonic-gate 1457c478bd9Sstevel@tonic-gate static int 1467c478bd9Sstevel@tonic-gate pcf8574_open(dev_t *devp, int flags, int otyp, cred_t *credp) 1477c478bd9Sstevel@tonic-gate { 1487c478bd9Sstevel@tonic-gate _NOTE(ARGUNUSED(credp)) 1497c478bd9Sstevel@tonic-gate 1507c478bd9Sstevel@tonic-gate struct pcf8574_unit *unitp; 1517c478bd9Sstevel@tonic-gate int instance; 1527c478bd9Sstevel@tonic-gate int error = 0; 1537c478bd9Sstevel@tonic-gate 1547c478bd9Sstevel@tonic-gate D1CMN_ERR((CE_WARN, "Opening the PCF8574 device\n")); 1557c478bd9Sstevel@tonic-gate 1567c478bd9Sstevel@tonic-gate instance = getminor(*devp); 1577c478bd9Sstevel@tonic-gate 1587c478bd9Sstevel@tonic-gate if (instance < 0) { 1597c478bd9Sstevel@tonic-gate return (ENXIO); 1607c478bd9Sstevel@tonic-gate } 1617c478bd9Sstevel@tonic-gate 1627c478bd9Sstevel@tonic-gate unitp = (struct pcf8574_unit *) 1637c478bd9Sstevel@tonic-gate ddi_get_soft_state(pcf8574soft_statep, instance); 1647c478bd9Sstevel@tonic-gate 1657c478bd9Sstevel@tonic-gate if (unitp == NULL) { 1667c478bd9Sstevel@tonic-gate return (ENXIO); 1677c478bd9Sstevel@tonic-gate } 1687c478bd9Sstevel@tonic-gate 1697c478bd9Sstevel@tonic-gate if (otyp != OTYP_CHR) { 1707c478bd9Sstevel@tonic-gate return (EINVAL); 1717c478bd9Sstevel@tonic-gate } 1727c478bd9Sstevel@tonic-gate 1737c478bd9Sstevel@tonic-gate mutex_enter(&unitp->pcf8574_mutex); 1747c478bd9Sstevel@tonic-gate 1757c478bd9Sstevel@tonic-gate if (flags & FEXCL) { 1767c478bd9Sstevel@tonic-gate if (unitp->pcf8574_oflag != 0) { 1777c478bd9Sstevel@tonic-gate error = EBUSY; 1787c478bd9Sstevel@tonic-gate } else { 1797c478bd9Sstevel@tonic-gate unitp->pcf8574_oflag = FEXCL; 1807c478bd9Sstevel@tonic-gate } 1817c478bd9Sstevel@tonic-gate } else { 1827c478bd9Sstevel@tonic-gate if (unitp->pcf8574_oflag == FEXCL) { 1837c478bd9Sstevel@tonic-gate error = EBUSY; 1847c478bd9Sstevel@tonic-gate } else { 1857c478bd9Sstevel@tonic-gate unitp->pcf8574_oflag = FOPEN; 1867c478bd9Sstevel@tonic-gate } 1877c478bd9Sstevel@tonic-gate } 1887c478bd9Sstevel@tonic-gate 1897c478bd9Sstevel@tonic-gate mutex_exit(&unitp->pcf8574_mutex); 1907c478bd9Sstevel@tonic-gate 1917c478bd9Sstevel@tonic-gate return (error); 1927c478bd9Sstevel@tonic-gate } 1937c478bd9Sstevel@tonic-gate 1947c478bd9Sstevel@tonic-gate static int 1957c478bd9Sstevel@tonic-gate pcf8574_close(dev_t dev, int flags, int otyp, cred_t *credp) 1967c478bd9Sstevel@tonic-gate { 1977c478bd9Sstevel@tonic-gate _NOTE(ARGUNUSED(flags, otyp, credp)) 1987c478bd9Sstevel@tonic-gate 1997c478bd9Sstevel@tonic-gate struct pcf8574_unit *unitp; 2007c478bd9Sstevel@tonic-gate int instance; 2017c478bd9Sstevel@tonic-gate 2027c478bd9Sstevel@tonic-gate instance = getminor(dev); 2037c478bd9Sstevel@tonic-gate 2047c478bd9Sstevel@tonic-gate if (instance < 0) { 2057c478bd9Sstevel@tonic-gate return (ENXIO); 2067c478bd9Sstevel@tonic-gate } 2077c478bd9Sstevel@tonic-gate unitp = (struct pcf8574_unit *) 2087c478bd9Sstevel@tonic-gate ddi_get_soft_state(pcf8574soft_statep, instance); 2097c478bd9Sstevel@tonic-gate 2107c478bd9Sstevel@tonic-gate if (unitp == NULL) { 2117c478bd9Sstevel@tonic-gate return (ENXIO); 2127c478bd9Sstevel@tonic-gate } 2137c478bd9Sstevel@tonic-gate 2147c478bd9Sstevel@tonic-gate mutex_enter(&unitp->pcf8574_mutex); 2157c478bd9Sstevel@tonic-gate 2167c478bd9Sstevel@tonic-gate unitp->pcf8574_oflag = 0; 2177c478bd9Sstevel@tonic-gate 2187c478bd9Sstevel@tonic-gate mutex_exit(&unitp->pcf8574_mutex); 2197c478bd9Sstevel@tonic-gate return (DDI_SUCCESS); 2207c478bd9Sstevel@tonic-gate } 2217c478bd9Sstevel@tonic-gate 2227c478bd9Sstevel@tonic-gate static int 2237c478bd9Sstevel@tonic-gate pcf8574_get(struct pcf8574_unit *unitp, uchar_t *byte) { 2247c478bd9Sstevel@tonic-gate i2c_transfer_t *i2c_tran_pointer; 2257c478bd9Sstevel@tonic-gate int err = I2C_SUCCESS; 2267c478bd9Sstevel@tonic-gate 2277c478bd9Sstevel@tonic-gate D1CMN_ERR((CE_WARN, "Entered the pcf8574_get routine\n")); 2287c478bd9Sstevel@tonic-gate 2297c478bd9Sstevel@tonic-gate (void) i2c_transfer_alloc(unitp->pcf8574_hdl, &i2c_tran_pointer, 2307c478bd9Sstevel@tonic-gate 0, 1, I2C_SLEEP); 2317c478bd9Sstevel@tonic-gate if (i2c_tran_pointer == NULL) { 2327c478bd9Sstevel@tonic-gate D2CMN_ERR((CE_WARN, "%s: Failed in pcf8574_get " 2337c478bd9Sstevel@tonic-gate "i2c_tran_pointer not allocated\n", 2347c478bd9Sstevel@tonic-gate unitp->pcf8574_name)); 2357c478bd9Sstevel@tonic-gate return (ENOMEM); 2367c478bd9Sstevel@tonic-gate } 2377c478bd9Sstevel@tonic-gate 2387c478bd9Sstevel@tonic-gate i2c_tran_pointer->i2c_flags = I2C_RD; 2397c478bd9Sstevel@tonic-gate err = i2c_transfer(unitp->pcf8574_hdl, i2c_tran_pointer); 2407c478bd9Sstevel@tonic-gate if (err) { 2417c478bd9Sstevel@tonic-gate D2CMN_ERR((CE_WARN, "%s: Failed in the i2c_transfer routine\n", 2427c478bd9Sstevel@tonic-gate unitp->pcf8574_name)); 2437c478bd9Sstevel@tonic-gate i2c_transfer_free(unitp->pcf8574_hdl, i2c_tran_pointer); 2447c478bd9Sstevel@tonic-gate return (err); 2457c478bd9Sstevel@tonic-gate } 2467c478bd9Sstevel@tonic-gate 2477c478bd9Sstevel@tonic-gate D1CMN_ERR((CE_WARN, "Back from a transfer value is %x\n", 2487c478bd9Sstevel@tonic-gate i2c_tran_pointer->i2c_rbuf[0])); 2497c478bd9Sstevel@tonic-gate *byte = i2c_tran_pointer->i2c_rbuf[0]; 2507c478bd9Sstevel@tonic-gate 2517c478bd9Sstevel@tonic-gate i2c_transfer_free(unitp->pcf8574_hdl, i2c_tran_pointer); 2527c478bd9Sstevel@tonic-gate return (err); 2537c478bd9Sstevel@tonic-gate } 2547c478bd9Sstevel@tonic-gate 2557c478bd9Sstevel@tonic-gate static int 2567c478bd9Sstevel@tonic-gate pcf8574_set(struct pcf8574_unit *unitp, uchar_t byte) { 2577c478bd9Sstevel@tonic-gate i2c_transfer_t *i2c_tran_pointer; 2587c478bd9Sstevel@tonic-gate int err = I2C_SUCCESS; 2597c478bd9Sstevel@tonic-gate 2607c478bd9Sstevel@tonic-gate (void) i2c_transfer_alloc(unitp->pcf8574_hdl, &i2c_tran_pointer, 2617c478bd9Sstevel@tonic-gate 1, 0, I2C_SLEEP); 2627c478bd9Sstevel@tonic-gate if (i2c_tran_pointer == NULL) { 2637c478bd9Sstevel@tonic-gate D2CMN_ERR((CE_WARN, "%s: Failed in pcf8574_set " 2647c478bd9Sstevel@tonic-gate "i2c_tran_pointer not allocated\n", 2657c478bd9Sstevel@tonic-gate unitp->pcf8574_name)); 2667c478bd9Sstevel@tonic-gate return (ENOMEM); 2677c478bd9Sstevel@tonic-gate } 2687c478bd9Sstevel@tonic-gate 2697c478bd9Sstevel@tonic-gate i2c_tran_pointer->i2c_flags = I2C_WR; 2707c478bd9Sstevel@tonic-gate i2c_tran_pointer->i2c_wbuf[0] = byte; 2717c478bd9Sstevel@tonic-gate D1CMN_ERR((CE_NOTE, "%s: contains %x\n", unitp->pcf8574_name, 2727c478bd9Sstevel@tonic-gate i2c_tran_pointer->i2c_wbuf[0])); 2737c478bd9Sstevel@tonic-gate 2747c478bd9Sstevel@tonic-gate err = i2c_transfer(unitp->pcf8574_hdl, i2c_tran_pointer); 2757c478bd9Sstevel@tonic-gate if (err) { 2767c478bd9Sstevel@tonic-gate D2CMN_ERR((CE_WARN, "%s: Failed in the pcf8574_set" 2777c478bd9Sstevel@tonic-gate " i2c_transfer routine\n", 2787c478bd9Sstevel@tonic-gate unitp->pcf8574_name)); 2797c478bd9Sstevel@tonic-gate i2c_transfer_free(unitp->pcf8574_hdl, i2c_tran_pointer); 2807c478bd9Sstevel@tonic-gate return (err); 2817c478bd9Sstevel@tonic-gate } 2827c478bd9Sstevel@tonic-gate i2c_transfer_free(unitp->pcf8574_hdl, i2c_tran_pointer); 2837c478bd9Sstevel@tonic-gate return (err); 2847c478bd9Sstevel@tonic-gate } 2857c478bd9Sstevel@tonic-gate 2867c478bd9Sstevel@tonic-gate static int 2877c478bd9Sstevel@tonic-gate pcf8574_ioctl(dev_t dev, int cmd, intptr_t arg, int mode, 2887c478bd9Sstevel@tonic-gate cred_t *credp, int *rvalp) 2897c478bd9Sstevel@tonic-gate { 2907c478bd9Sstevel@tonic-gate _NOTE(ARGUNUSED(credp, rvalp)) 2917c478bd9Sstevel@tonic-gate 2927c478bd9Sstevel@tonic-gate struct pcf8574_unit *unitp; 2937c478bd9Sstevel@tonic-gate int instance; 2947c478bd9Sstevel@tonic-gate int err = 0; 2957c478bd9Sstevel@tonic-gate i2c_bit_t ioctl_bit; 2967c478bd9Sstevel@tonic-gate i2c_port_t ioctl_port; 2977c478bd9Sstevel@tonic-gate uchar_t byte; 2987c478bd9Sstevel@tonic-gate 2997c478bd9Sstevel@tonic-gate if (arg == NULL) { 3007c478bd9Sstevel@tonic-gate D2CMN_ERR((CE_WARN, "PCF8574: ioctl: arg passed in to ioctl " 3017c478bd9Sstevel@tonic-gate "= NULL\n")); 3027c478bd9Sstevel@tonic-gate err = EINVAL; 3037c478bd9Sstevel@tonic-gate return (err); 3047c478bd9Sstevel@tonic-gate } 3057c478bd9Sstevel@tonic-gate 3067c478bd9Sstevel@tonic-gate instance = getminor(dev); 3077c478bd9Sstevel@tonic-gate unitp = (struct pcf8574_unit *) 3087c478bd9Sstevel@tonic-gate ddi_get_soft_state(pcf8574soft_statep, instance); 3097c478bd9Sstevel@tonic-gate if (unitp == NULL) { 3107c478bd9Sstevel@tonic-gate cmn_err(CE_WARN, "PCF8574: ioctl: unitp not filled\n"); 3117c478bd9Sstevel@tonic-gate return (ENOMEM); 3127c478bd9Sstevel@tonic-gate } 3137c478bd9Sstevel@tonic-gate 3147c478bd9Sstevel@tonic-gate mutex_enter(&unitp->pcf8574_mutex); 3157c478bd9Sstevel@tonic-gate 3167c478bd9Sstevel@tonic-gate switch (cmd) { 3177c478bd9Sstevel@tonic-gate case I2C_GET_PORT: 3187c478bd9Sstevel@tonic-gate if (ddi_copyin((caddr_t)arg, (caddr_t)&ioctl_port, 3197c478bd9Sstevel@tonic-gate sizeof (i2c_port_t), mode) != DDI_SUCCESS) { 3207c478bd9Sstevel@tonic-gate D2CMN_ERR((CE_WARN, "%s: Failed in the I2C_GET_PORT" 3217c478bd9Sstevel@tonic-gate " ddi_copyin routine\n", 3227c478bd9Sstevel@tonic-gate unitp->pcf8574_name)); 3237c478bd9Sstevel@tonic-gate err = EFAULT; 3247c478bd9Sstevel@tonic-gate break; 3257c478bd9Sstevel@tonic-gate } 3267c478bd9Sstevel@tonic-gate 3277c478bd9Sstevel@tonic-gate err = pcf8574_get(unitp, &byte); 3287c478bd9Sstevel@tonic-gate if (err != I2C_SUCCESS) { 3297c478bd9Sstevel@tonic-gate D2CMN_ERR((CE_WARN, "%s: Failed in the I2C_GET_PORT" 3307c478bd9Sstevel@tonic-gate " pcf8574_get routine\n", 3317c478bd9Sstevel@tonic-gate unitp->pcf8574_name)); 3327c478bd9Sstevel@tonic-gate break; 3337c478bd9Sstevel@tonic-gate } 3347c478bd9Sstevel@tonic-gate 3357c478bd9Sstevel@tonic-gate ioctl_port.value = byte; 3367c478bd9Sstevel@tonic-gate if (ddi_copyout((caddr_t)&ioctl_port, (caddr_t)arg, 3377c478bd9Sstevel@tonic-gate sizeof (i2c_port_t), mode) != DDI_SUCCESS) { 3387c478bd9Sstevel@tonic-gate D2CMN_ERR((CE_WARN, "%s: Failed in I2C_GET_PORT " 3397c478bd9Sstevel@tonic-gate "ddi_copyout routine\n", 3407c478bd9Sstevel@tonic-gate unitp->pcf8574_name)); 3417c478bd9Sstevel@tonic-gate err = EFAULT; 3427c478bd9Sstevel@tonic-gate } 3437c478bd9Sstevel@tonic-gate 3447c478bd9Sstevel@tonic-gate D1CMN_ERR((CE_NOTE, "%s: contains %x\n", unitp->pcf8574_name, 3457c478bd9Sstevel@tonic-gate byte)); 3467c478bd9Sstevel@tonic-gate break; 3477c478bd9Sstevel@tonic-gate 3487c478bd9Sstevel@tonic-gate case I2C_SET_PORT: 3497c478bd9Sstevel@tonic-gate if (ddi_copyin((caddr_t)arg, (caddr_t)&ioctl_port, 3507c478bd9Sstevel@tonic-gate sizeof (uint8_t), mode) != DDI_SUCCESS) { 3517c478bd9Sstevel@tonic-gate D2CMN_ERR((CE_WARN, "%s: Failed in the I2C_SET_PORT" 3527c478bd9Sstevel@tonic-gate "ddi_cpoyin routine\n", 3537c478bd9Sstevel@tonic-gate unitp->pcf8574_name)); 3547c478bd9Sstevel@tonic-gate err = EFAULT; 3557c478bd9Sstevel@tonic-gate break; 3567c478bd9Sstevel@tonic-gate } 3577c478bd9Sstevel@tonic-gate 3587c478bd9Sstevel@tonic-gate err = pcf8574_set(unitp, ioctl_port.value); 3597c478bd9Sstevel@tonic-gate if (err != I2C_SUCCESS) { 3607c478bd9Sstevel@tonic-gate D2CMN_ERR((CE_WARN, "%s: Failed in the I2C_SET_PORT" 3617c478bd9Sstevel@tonic-gate " pcf8574_set routine\n", 3627c478bd9Sstevel@tonic-gate unitp->pcf8574_name)); 3637c478bd9Sstevel@tonic-gate break; 3647c478bd9Sstevel@tonic-gate } 3657c478bd9Sstevel@tonic-gate break; 3667c478bd9Sstevel@tonic-gate 3677c478bd9Sstevel@tonic-gate case I2C_GET_BIT: 3687c478bd9Sstevel@tonic-gate if (ddi_copyin((caddr_t)arg, (caddr_t)&ioctl_bit, 3697c478bd9Sstevel@tonic-gate sizeof (i2c_bit_t), mode) != DDI_SUCCESS) { 3707c478bd9Sstevel@tonic-gate D2CMN_ERR((CE_WARN, "%s: Failed in the I2C_GET_BIT" 3717c478bd9Sstevel@tonic-gate " ddi_copyin routine\n", 3727c478bd9Sstevel@tonic-gate unitp->pcf8574_name)); 3737c478bd9Sstevel@tonic-gate err = EFAULT; 3747c478bd9Sstevel@tonic-gate break; 3757c478bd9Sstevel@tonic-gate } 3767c478bd9Sstevel@tonic-gate 377*f47a9c50Smathue if (ioctl_bit.bit_num > 7) { 3787c478bd9Sstevel@tonic-gate D2CMN_ERR((CE_WARN, "%s: In I2C_GET_BIT bit num" 3797c478bd9Sstevel@tonic-gate " was not between 0 and 7\n", 3807c478bd9Sstevel@tonic-gate unitp->pcf8574_name)); 3817c478bd9Sstevel@tonic-gate err = EIO; 3827c478bd9Sstevel@tonic-gate break; 3837c478bd9Sstevel@tonic-gate } 3847c478bd9Sstevel@tonic-gate 3857c478bd9Sstevel@tonic-gate err = pcf8574_get(unitp, &byte); 3867c478bd9Sstevel@tonic-gate if (err != I2C_SUCCESS) { 3877c478bd9Sstevel@tonic-gate D2CMN_ERR((CE_WARN, "%s: Failed in the I2C_GET_BIT" 3887c478bd9Sstevel@tonic-gate " pcf8574_get routine\n", 3897c478bd9Sstevel@tonic-gate unitp->pcf8574_name)); 3907c478bd9Sstevel@tonic-gate break; 3917c478bd9Sstevel@tonic-gate } 3927c478bd9Sstevel@tonic-gate 3937c478bd9Sstevel@tonic-gate D1CMN_ERR((CE_NOTE, "%s: byte returned from device is %x\n", 3947c478bd9Sstevel@tonic-gate unitp->pcf8574_name, byte)); 3957c478bd9Sstevel@tonic-gate ioctl_bit.bit_value = (boolean_t)PCF8574_BIT_READ_MASK(byte, 3967c478bd9Sstevel@tonic-gate ioctl_bit.bit_num); 3977c478bd9Sstevel@tonic-gate D1CMN_ERR((CE_NOTE, "%s: byte now contains %x\n", 3987c478bd9Sstevel@tonic-gate unitp->pcf8574_name, byte)); 3997c478bd9Sstevel@tonic-gate 4007c478bd9Sstevel@tonic-gate if (ddi_copyout((caddr_t)&ioctl_bit, (caddr_t)arg, 4017c478bd9Sstevel@tonic-gate sizeof (i2c_bit_t), mode) != DDI_SUCCESS) { 4027c478bd9Sstevel@tonic-gate D2CMN_ERR((CE_WARN, "%s: Failed in I2C_GET_BIT" 4037c478bd9Sstevel@tonic-gate " ddi_copyout routine\n", 4047c478bd9Sstevel@tonic-gate unitp->pcf8574_name)); 4057c478bd9Sstevel@tonic-gate err = EFAULT; 4067c478bd9Sstevel@tonic-gate } 4077c478bd9Sstevel@tonic-gate break; 4087c478bd9Sstevel@tonic-gate 4097c478bd9Sstevel@tonic-gate case I2C_SET_BIT: 4107c478bd9Sstevel@tonic-gate if (ddi_copyin((caddr_t)arg, (caddr_t)&ioctl_bit, 4117c478bd9Sstevel@tonic-gate sizeof (i2c_bit_t), mode) != DDI_SUCCESS) { 4127c478bd9Sstevel@tonic-gate D2CMN_ERR((CE_WARN, "%s: Failed in I2C_SET_BIT" 4137c478bd9Sstevel@tonic-gate " ddi_copyin routine\n", 4147c478bd9Sstevel@tonic-gate unitp->pcf8574_name)); 4157c478bd9Sstevel@tonic-gate err = EFAULT; 4167c478bd9Sstevel@tonic-gate break; 4177c478bd9Sstevel@tonic-gate } 4187c478bd9Sstevel@tonic-gate 419*f47a9c50Smathue if (ioctl_bit.bit_num > 7) { 4207c478bd9Sstevel@tonic-gate D2CMN_ERR((CE_WARN, "%s: I2C_SET_BIT: bit_num sent" 4217c478bd9Sstevel@tonic-gate " in was not between 0 and 7", 4227c478bd9Sstevel@tonic-gate unitp->pcf8574_name)); 4237c478bd9Sstevel@tonic-gate err = EIO; 4247c478bd9Sstevel@tonic-gate break; 4257c478bd9Sstevel@tonic-gate } 4267c478bd9Sstevel@tonic-gate 4277c478bd9Sstevel@tonic-gate err = pcf8574_get(unitp, &byte); 4287c478bd9Sstevel@tonic-gate if (err != I2C_SUCCESS) { 4297c478bd9Sstevel@tonic-gate D2CMN_ERR((CE_WARN, "%s: Failed in the I2C_SET_BIT" 4307c478bd9Sstevel@tonic-gate " pcf8574_get routine\n", 4317c478bd9Sstevel@tonic-gate unitp->pcf8574_name)); 4327c478bd9Sstevel@tonic-gate break; 4337c478bd9Sstevel@tonic-gate } 4347c478bd9Sstevel@tonic-gate 4357c478bd9Sstevel@tonic-gate D1CMN_ERR((CE_NOTE, "%s: byte returned from device is %x\n", 4367c478bd9Sstevel@tonic-gate unitp->pcf8574_name, byte)); 4377c478bd9Sstevel@tonic-gate byte = PCF8574_BIT_WRITE_MASK(byte, ioctl_bit.bit_num, 4387c478bd9Sstevel@tonic-gate ioctl_bit.bit_value); 4397c478bd9Sstevel@tonic-gate D1CMN_ERR((CE_NOTE, "%s: byte after shifting is %x\n", 4407c478bd9Sstevel@tonic-gate unitp->pcf8574_name, byte)); 4417c478bd9Sstevel@tonic-gate 4427c478bd9Sstevel@tonic-gate err = pcf8574_set(unitp, byte); 4437c478bd9Sstevel@tonic-gate if (err != I2C_SUCCESS) { 4447c478bd9Sstevel@tonic-gate D2CMN_ERR((CE_WARN, "%s: Failed in the I2C_SET_BIT" 4457c478bd9Sstevel@tonic-gate " pcf8574_set routine\n", 4467c478bd9Sstevel@tonic-gate unitp->pcf8574_name)); 4477c478bd9Sstevel@tonic-gate break; 4487c478bd9Sstevel@tonic-gate } 4497c478bd9Sstevel@tonic-gate break; 4507c478bd9Sstevel@tonic-gate 4517c478bd9Sstevel@tonic-gate default: 4527c478bd9Sstevel@tonic-gate D2CMN_ERR((CE_WARN, "%s: Invalid IOCTL cmd: %x\n", 4537c478bd9Sstevel@tonic-gate unitp->pcf8574_name, cmd)); 4547c478bd9Sstevel@tonic-gate err = EINVAL; 4557c478bd9Sstevel@tonic-gate } 4567c478bd9Sstevel@tonic-gate 4577c478bd9Sstevel@tonic-gate mutex_exit(&unitp->pcf8574_mutex); 4587c478bd9Sstevel@tonic-gate return (err); 4597c478bd9Sstevel@tonic-gate } 4607c478bd9Sstevel@tonic-gate 4617c478bd9Sstevel@tonic-gate static int 4627c478bd9Sstevel@tonic-gate pcf8574_attach(dev_info_t *dip, ddi_attach_cmd_t cmd) 4637c478bd9Sstevel@tonic-gate { 4647c478bd9Sstevel@tonic-gate switch (cmd) { 4657c478bd9Sstevel@tonic-gate case DDI_ATTACH: 4667c478bd9Sstevel@tonic-gate return (pcf8574_do_attach(dip)); 4677c478bd9Sstevel@tonic-gate case DDI_RESUME: 4687c478bd9Sstevel@tonic-gate return (pcf8574_do_resume()); 4697c478bd9Sstevel@tonic-gate default: 4707c478bd9Sstevel@tonic-gate return (DDI_FAILURE); 4717c478bd9Sstevel@tonic-gate } 4727c478bd9Sstevel@tonic-gate } 4737c478bd9Sstevel@tonic-gate 4747c478bd9Sstevel@tonic-gate static int 4757c478bd9Sstevel@tonic-gate pcf8574_detach(dev_info_t *dip, ddi_detach_cmd_t cmd) 4767c478bd9Sstevel@tonic-gate { 4777c478bd9Sstevel@tonic-gate switch (cmd) { 4787c478bd9Sstevel@tonic-gate case DDI_DETACH: 4797c478bd9Sstevel@tonic-gate return (pcf8574_do_detach(dip)); 4807c478bd9Sstevel@tonic-gate case DDI_SUSPEND: 4817c478bd9Sstevel@tonic-gate return (pcf8574_do_suspend()); 4827c478bd9Sstevel@tonic-gate default: 4837c478bd9Sstevel@tonic-gate return (DDI_FAILURE); 4847c478bd9Sstevel@tonic-gate } 4857c478bd9Sstevel@tonic-gate } 4867c478bd9Sstevel@tonic-gate 4877c478bd9Sstevel@tonic-gate static int 4887c478bd9Sstevel@tonic-gate pcf8574_do_attach(dev_info_t *dip) 4897c478bd9Sstevel@tonic-gate { 4907c478bd9Sstevel@tonic-gate struct pcf8574_unit *unitp; 4917c478bd9Sstevel@tonic-gate int instance; 4927c478bd9Sstevel@tonic-gate 4937c478bd9Sstevel@tonic-gate instance = ddi_get_instance(dip); 4947c478bd9Sstevel@tonic-gate 4957c478bd9Sstevel@tonic-gate if (ddi_soft_state_zalloc(pcf8574soft_statep, instance) != 0) { 4967c478bd9Sstevel@tonic-gate cmn_err(CE_WARN, "%s%d: failed to zalloc softstate\n", 4977c478bd9Sstevel@tonic-gate ddi_get_name(dip), instance); 4987c478bd9Sstevel@tonic-gate return (DDI_FAILURE); 4997c478bd9Sstevel@tonic-gate } 5007c478bd9Sstevel@tonic-gate 5017c478bd9Sstevel@tonic-gate unitp = ddi_get_soft_state(pcf8574soft_statep, instance); 5027c478bd9Sstevel@tonic-gate 5037c478bd9Sstevel@tonic-gate if (unitp == NULL) { 5047c478bd9Sstevel@tonic-gate cmn_err(CE_WARN, "%s%d: unitp not filled\n", 5057c478bd9Sstevel@tonic-gate ddi_get_name(dip), instance); 5067c478bd9Sstevel@tonic-gate return (ENOMEM); 5077c478bd9Sstevel@tonic-gate } 5087c478bd9Sstevel@tonic-gate 5097c478bd9Sstevel@tonic-gate (void) snprintf(unitp->pcf8574_name, sizeof (unitp->pcf8574_name), 510*f47a9c50Smathue "%s%d", ddi_node_name(dip), instance); 5117c478bd9Sstevel@tonic-gate 5127c478bd9Sstevel@tonic-gate 5137c478bd9Sstevel@tonic-gate if (ddi_create_minor_node(dip, "pcf8574", S_IFCHR, instance, 5147c478bd9Sstevel@tonic-gate "ddi_i2c:ioexp", NULL) == DDI_FAILURE) { 5157c478bd9Sstevel@tonic-gate cmn_err(CE_WARN, "%s ddi_create_minor_node failed for " 5167c478bd9Sstevel@tonic-gate "%s\n", unitp->pcf8574_name, "pcf8574"); 5177c478bd9Sstevel@tonic-gate ddi_soft_state_free(pcf8574soft_statep, instance); 5187c478bd9Sstevel@tonic-gate 5197c478bd9Sstevel@tonic-gate return (DDI_FAILURE); 5207c478bd9Sstevel@tonic-gate } 5217c478bd9Sstevel@tonic-gate 5227c478bd9Sstevel@tonic-gate if (i2c_client_register(dip, &unitp->pcf8574_hdl) != I2C_SUCCESS) { 5237c478bd9Sstevel@tonic-gate ddi_remove_minor_node(dip, NULL); 5247c478bd9Sstevel@tonic-gate ddi_soft_state_free(pcf8574soft_statep, instance); 5257c478bd9Sstevel@tonic-gate 5267c478bd9Sstevel@tonic-gate return (DDI_FAILURE); 5277c478bd9Sstevel@tonic-gate } 5287c478bd9Sstevel@tonic-gate 5297c478bd9Sstevel@tonic-gate mutex_init(&unitp->pcf8574_mutex, NULL, MUTEX_DRIVER, NULL); 5307c478bd9Sstevel@tonic-gate 5317c478bd9Sstevel@tonic-gate return (DDI_SUCCESS); 5327c478bd9Sstevel@tonic-gate } 5337c478bd9Sstevel@tonic-gate 5347c478bd9Sstevel@tonic-gate static int 5357c478bd9Sstevel@tonic-gate pcf8574_do_resume() 5367c478bd9Sstevel@tonic-gate { 5377c478bd9Sstevel@tonic-gate int ret = DDI_SUCCESS; 5387c478bd9Sstevel@tonic-gate 5397c478bd9Sstevel@tonic-gate return (ret); 5407c478bd9Sstevel@tonic-gate } 5417c478bd9Sstevel@tonic-gate 5427c478bd9Sstevel@tonic-gate static int 5437c478bd9Sstevel@tonic-gate pcf8574_do_suspend() 5447c478bd9Sstevel@tonic-gate { 5457c478bd9Sstevel@tonic-gate int ret = DDI_SUCCESS; 5467c478bd9Sstevel@tonic-gate 5477c478bd9Sstevel@tonic-gate return (ret); 5487c478bd9Sstevel@tonic-gate } 5497c478bd9Sstevel@tonic-gate 5507c478bd9Sstevel@tonic-gate static int 5517c478bd9Sstevel@tonic-gate pcf8574_do_detach(dev_info_t *dip) 5527c478bd9Sstevel@tonic-gate { 5537c478bd9Sstevel@tonic-gate struct pcf8574_unit *unitp; 5547c478bd9Sstevel@tonic-gate int instance; 5557c478bd9Sstevel@tonic-gate 5567c478bd9Sstevel@tonic-gate instance = ddi_get_instance(dip); 5577c478bd9Sstevel@tonic-gate 5587c478bd9Sstevel@tonic-gate unitp = ddi_get_soft_state(pcf8574soft_statep, instance); 5597c478bd9Sstevel@tonic-gate 5607c478bd9Sstevel@tonic-gate if (unitp == NULL) { 5617c478bd9Sstevel@tonic-gate cmn_err(CE_WARN, "%s%d: unitp not filled\n", 5627c478bd9Sstevel@tonic-gate ddi_get_name(dip), instance); 5637c478bd9Sstevel@tonic-gate return (ENOMEM); 5647c478bd9Sstevel@tonic-gate } 5657c478bd9Sstevel@tonic-gate 5667c478bd9Sstevel@tonic-gate i2c_client_unregister(unitp->pcf8574_hdl); 5677c478bd9Sstevel@tonic-gate 5687c478bd9Sstevel@tonic-gate ddi_remove_minor_node(dip, NULL); 5697c478bd9Sstevel@tonic-gate 5707c478bd9Sstevel@tonic-gate mutex_destroy(&unitp->pcf8574_mutex); 5717c478bd9Sstevel@tonic-gate 5727c478bd9Sstevel@tonic-gate ddi_soft_state_free(pcf8574soft_statep, instance); 5737c478bd9Sstevel@tonic-gate 5747c478bd9Sstevel@tonic-gate return (DDI_SUCCESS); 5757c478bd9Sstevel@tonic-gate 5767c478bd9Sstevel@tonic-gate } 577