1*0Sstevel@tonic-gate /* 2*0Sstevel@tonic-gate * CDDL HEADER START 3*0Sstevel@tonic-gate * 4*0Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*0Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 6*0Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 7*0Sstevel@tonic-gate * with the License. 8*0Sstevel@tonic-gate * 9*0Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10*0Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 11*0Sstevel@tonic-gate * See the License for the specific language governing permissions 12*0Sstevel@tonic-gate * and limitations under the License. 13*0Sstevel@tonic-gate * 14*0Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 15*0Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16*0Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 17*0Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 18*0Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 19*0Sstevel@tonic-gate * 20*0Sstevel@tonic-gate * CDDL HEADER END 21*0Sstevel@tonic-gate */ 22*0Sstevel@tonic-gate /* 23*0Sstevel@tonic-gate * Copyright 2004 Sun Microsystems, Inc. All rights reserved. 24*0Sstevel@tonic-gate * Use is subject to license terms. 25*0Sstevel@tonic-gate */ 26*0Sstevel@tonic-gate 27*0Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 28*0Sstevel@tonic-gate 29*0Sstevel@tonic-gate #include <sys/stat.h> 30*0Sstevel@tonic-gate #include <sys/file.h> 31*0Sstevel@tonic-gate #include <sys/uio.h> 32*0Sstevel@tonic-gate #include <sys/modctl.h> 33*0Sstevel@tonic-gate #include <sys/open.h> 34*0Sstevel@tonic-gate #include <sys/types.h> 35*0Sstevel@tonic-gate #include <sys/kmem.h> 36*0Sstevel@tonic-gate #include <sys/systm.h> 37*0Sstevel@tonic-gate #include <sys/ddi.h> 38*0Sstevel@tonic-gate #include <sys/sunddi.h> 39*0Sstevel@tonic-gate #include <sys/conf.h> 40*0Sstevel@tonic-gate #include <sys/mode.h> 41*0Sstevel@tonic-gate #include <sys/note.h> 42*0Sstevel@tonic-gate #include <sys/i2c/misc/i2c_svc.h> 43*0Sstevel@tonic-gate #include <sys/i2c/clients/tda8444_impl.h> 44*0Sstevel@tonic-gate 45*0Sstevel@tonic-gate /* 46*0Sstevel@tonic-gate * cb ops 47*0Sstevel@tonic-gate */ 48*0Sstevel@tonic-gate static int tda8444_open(dev_t *, int, int, cred_t *); 49*0Sstevel@tonic-gate static int tda8444_close(dev_t, int, int, cred_t *); 50*0Sstevel@tonic-gate static int tda8444_read(dev_t dev, struct uio *uiop, cred_t *cred_p); 51*0Sstevel@tonic-gate static int tda8444_write(dev_t dev, struct uio *uiop, cred_t *cred_p); 52*0Sstevel@tonic-gate static int tda8444_io(dev_t dev, struct uio *uiop, int rw); 53*0Sstevel@tonic-gate /* 54*0Sstevel@tonic-gate * dev ops 55*0Sstevel@tonic-gate */ 56*0Sstevel@tonic-gate static int tda8444_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, 57*0Sstevel@tonic-gate void **result); 58*0Sstevel@tonic-gate static int tda8444_attach(dev_info_t *dip, ddi_attach_cmd_t cmd); 59*0Sstevel@tonic-gate static int tda8444_detach(dev_info_t *dip, ddi_detach_cmd_t cmd); 60*0Sstevel@tonic-gate 61*0Sstevel@tonic-gate static struct cb_ops tda8444_cbops = { 62*0Sstevel@tonic-gate tda8444_open, /* open */ 63*0Sstevel@tonic-gate tda8444_close, /* close */ 64*0Sstevel@tonic-gate nodev, /* strategy */ 65*0Sstevel@tonic-gate nodev, /* print */ 66*0Sstevel@tonic-gate nodev, /* dump */ 67*0Sstevel@tonic-gate tda8444_read, /* read */ 68*0Sstevel@tonic-gate tda8444_write, /* write */ 69*0Sstevel@tonic-gate nodev, /* ioctl */ 70*0Sstevel@tonic-gate nodev, /* devmap */ 71*0Sstevel@tonic-gate nodev, /* mmap */ 72*0Sstevel@tonic-gate nodev, /* segmap */ 73*0Sstevel@tonic-gate nochpoll, /* poll */ 74*0Sstevel@tonic-gate ddi_prop_op, /* cb_prop_op */ 75*0Sstevel@tonic-gate NULL, /* streamtab */ 76*0Sstevel@tonic-gate D_NEW | D_MP | D_HOTPLUG, /* Driver compatibility flag */ 77*0Sstevel@tonic-gate CB_REV, /* rev */ 78*0Sstevel@tonic-gate nodev, /* int (*cb_aread)() */ 79*0Sstevel@tonic-gate nodev /* int (*cb_awrite)() */ 80*0Sstevel@tonic-gate }; 81*0Sstevel@tonic-gate 82*0Sstevel@tonic-gate static struct dev_ops tda8444_ops = { 83*0Sstevel@tonic-gate DEVO_REV, 84*0Sstevel@tonic-gate 0, 85*0Sstevel@tonic-gate tda8444_info, 86*0Sstevel@tonic-gate nulldev, 87*0Sstevel@tonic-gate nulldev, 88*0Sstevel@tonic-gate tda8444_attach, 89*0Sstevel@tonic-gate tda8444_detach, 90*0Sstevel@tonic-gate nodev, 91*0Sstevel@tonic-gate &tda8444_cbops, 92*0Sstevel@tonic-gate NULL 93*0Sstevel@tonic-gate }; 94*0Sstevel@tonic-gate 95*0Sstevel@tonic-gate static struct modldrv tda8444_modldrv = { 96*0Sstevel@tonic-gate &mod_driverops, /* type of module - driver */ 97*0Sstevel@tonic-gate "tda8444 device driver v%I%", 98*0Sstevel@tonic-gate &tda8444_ops, 99*0Sstevel@tonic-gate }; 100*0Sstevel@tonic-gate 101*0Sstevel@tonic-gate static struct modlinkage tda8444_modlinkage = { 102*0Sstevel@tonic-gate MODREV_1, 103*0Sstevel@tonic-gate &tda8444_modldrv, 104*0Sstevel@tonic-gate 0 105*0Sstevel@tonic-gate }; 106*0Sstevel@tonic-gate 107*0Sstevel@tonic-gate static void *tda8444_soft_statep; 108*0Sstevel@tonic-gate static int tda8444_debug = 0; 109*0Sstevel@tonic-gate 110*0Sstevel@tonic-gate int 111*0Sstevel@tonic-gate _init(void) 112*0Sstevel@tonic-gate { 113*0Sstevel@tonic-gate int error; 114*0Sstevel@tonic-gate 115*0Sstevel@tonic-gate error = mod_install(&tda8444_modlinkage); 116*0Sstevel@tonic-gate if (error == 0) { 117*0Sstevel@tonic-gate (void) ddi_soft_state_init(&tda8444_soft_statep, 118*0Sstevel@tonic-gate sizeof (struct tda8444_unit), TDA8444_MAX_DACS); 119*0Sstevel@tonic-gate } 120*0Sstevel@tonic-gate 121*0Sstevel@tonic-gate return (error); 122*0Sstevel@tonic-gate } 123*0Sstevel@tonic-gate 124*0Sstevel@tonic-gate int 125*0Sstevel@tonic-gate _fini(void) 126*0Sstevel@tonic-gate { 127*0Sstevel@tonic-gate int error; 128*0Sstevel@tonic-gate 129*0Sstevel@tonic-gate error = mod_remove(&tda8444_modlinkage); 130*0Sstevel@tonic-gate if (error == 0) { 131*0Sstevel@tonic-gate ddi_soft_state_fini(&tda8444_soft_statep); 132*0Sstevel@tonic-gate } 133*0Sstevel@tonic-gate 134*0Sstevel@tonic-gate return (error); 135*0Sstevel@tonic-gate } 136*0Sstevel@tonic-gate 137*0Sstevel@tonic-gate int 138*0Sstevel@tonic-gate _info(struct modinfo *modinfop) 139*0Sstevel@tonic-gate { 140*0Sstevel@tonic-gate return (mod_info(&tda8444_modlinkage, modinfop)); 141*0Sstevel@tonic-gate } 142*0Sstevel@tonic-gate 143*0Sstevel@tonic-gate /* ARGSUSED */ 144*0Sstevel@tonic-gate static int 145*0Sstevel@tonic-gate tda8444_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result) 146*0Sstevel@tonic-gate { 147*0Sstevel@tonic-gate dev_t dev; 148*0Sstevel@tonic-gate int instance; 149*0Sstevel@tonic-gate 150*0Sstevel@tonic-gate if (infocmd == DDI_INFO_DEVT2INSTANCE) { 151*0Sstevel@tonic-gate dev = (dev_t)arg; 152*0Sstevel@tonic-gate instance = TDA8444_MINOR_TO_DEVINST(dev); 153*0Sstevel@tonic-gate *result = (void *)(uintptr_t)instance; 154*0Sstevel@tonic-gate return (DDI_SUCCESS); 155*0Sstevel@tonic-gate } 156*0Sstevel@tonic-gate return (DDI_FAILURE); 157*0Sstevel@tonic-gate } 158*0Sstevel@tonic-gate 159*0Sstevel@tonic-gate static int 160*0Sstevel@tonic-gate tda8444_do_resume(dev_info_t *dip) 161*0Sstevel@tonic-gate { 162*0Sstevel@tonic-gate int instance = ddi_get_instance(dip); 163*0Sstevel@tonic-gate struct tda8444_unit *unitp; 164*0Sstevel@tonic-gate int channel; 165*0Sstevel@tonic-gate int ret = DDI_SUCCESS; 166*0Sstevel@tonic-gate 167*0Sstevel@tonic-gate unitp = (struct tda8444_unit *) 168*0Sstevel@tonic-gate ddi_get_soft_state(tda8444_soft_statep, instance); 169*0Sstevel@tonic-gate 170*0Sstevel@tonic-gate if (unitp == NULL) { 171*0Sstevel@tonic-gate 172*0Sstevel@tonic-gate return (ENXIO); 173*0Sstevel@tonic-gate } 174*0Sstevel@tonic-gate 175*0Sstevel@tonic-gate for (channel = 0; channel < TDA8444_CHANS; channel++) { 176*0Sstevel@tonic-gate unitp->tda8444_transfer->i2c_wbuf[0] = TDA8444_REGBASE | 177*0Sstevel@tonic-gate channel; 178*0Sstevel@tonic-gate unitp->tda8444_transfer->i2c_wbuf[1] = 179*0Sstevel@tonic-gate unitp->tda8444_output[channel]; 180*0Sstevel@tonic-gate DPRINTF(RESUME, ("tda8444_resume: setting channel %d to %d", 181*0Sstevel@tonic-gate channel, unitp->tda8444_output[channel])); 182*0Sstevel@tonic-gate if (i2c_transfer(unitp->tda8444_hdl, 183*0Sstevel@tonic-gate unitp->tda8444_transfer) != I2C_SUCCESS) { 184*0Sstevel@tonic-gate ret = DDI_FAILURE; 185*0Sstevel@tonic-gate } 186*0Sstevel@tonic-gate } 187*0Sstevel@tonic-gate 188*0Sstevel@tonic-gate mutex_enter(&unitp->tda8444_mutex); 189*0Sstevel@tonic-gate unitp->tda8444_flags = 0; 190*0Sstevel@tonic-gate cv_signal(&unitp->tda8444_cv); 191*0Sstevel@tonic-gate mutex_exit(&unitp->tda8444_mutex); 192*0Sstevel@tonic-gate 193*0Sstevel@tonic-gate return (ret); 194*0Sstevel@tonic-gate } 195*0Sstevel@tonic-gate 196*0Sstevel@tonic-gate static int 197*0Sstevel@tonic-gate tda8444_do_attach(dev_info_t *dip) 198*0Sstevel@tonic-gate { 199*0Sstevel@tonic-gate struct tda8444_unit *unitp; 200*0Sstevel@tonic-gate char name[MAXNAMELEN]; 201*0Sstevel@tonic-gate int instance; 202*0Sstevel@tonic-gate minor_t minor; 203*0Sstevel@tonic-gate int i; 204*0Sstevel@tonic-gate 205*0Sstevel@tonic-gate instance = ddi_get_instance(dip); 206*0Sstevel@tonic-gate 207*0Sstevel@tonic-gate if (ddi_soft_state_zalloc(tda8444_soft_statep, instance) != 0) { 208*0Sstevel@tonic-gate cmn_err(CE_WARN, "%s%d failed to zalloc softstate", 209*0Sstevel@tonic-gate ddi_get_name(dip), instance); 210*0Sstevel@tonic-gate 211*0Sstevel@tonic-gate return (DDI_FAILURE); 212*0Sstevel@tonic-gate } 213*0Sstevel@tonic-gate 214*0Sstevel@tonic-gate unitp = ddi_get_soft_state(tda8444_soft_statep, instance); 215*0Sstevel@tonic-gate 216*0Sstevel@tonic-gate if (unitp == NULL) { 217*0Sstevel@tonic-gate return (DDI_FAILURE); 218*0Sstevel@tonic-gate } 219*0Sstevel@tonic-gate 220*0Sstevel@tonic-gate (void) snprintf(unitp->tda8444_name, sizeof (unitp->tda8444_name), 221*0Sstevel@tonic-gate "%s%d", ddi_driver_name(dip), instance); 222*0Sstevel@tonic-gate 223*0Sstevel@tonic-gate for (i = 0; i < TDA8444_CHANS; i++) { 224*0Sstevel@tonic-gate (void) sprintf(name, "%d", i); 225*0Sstevel@tonic-gate minor = TDA8444_CHANNEL_TO_MINOR(i) | 226*0Sstevel@tonic-gate TDA8444_DEVINST_TO_MINOR(instance); 227*0Sstevel@tonic-gate if (ddi_create_minor_node(dip, name, S_IFCHR, minor, 228*0Sstevel@tonic-gate TDA8444_NODE_TYPE, NULL) == DDI_FAILURE) { 229*0Sstevel@tonic-gate cmn_err(CE_WARN, "%s ddi_create_minor_node failed", 230*0Sstevel@tonic-gate unitp->tda8444_name); 231*0Sstevel@tonic-gate ddi_soft_state_free(tda8444_soft_statep, instance); 232*0Sstevel@tonic-gate ddi_remove_minor_node(dip, NULL); 233*0Sstevel@tonic-gate 234*0Sstevel@tonic-gate return (DDI_FAILURE); 235*0Sstevel@tonic-gate } 236*0Sstevel@tonic-gate unitp->tda8444_output[i] = TDA8444_UNKNOWN_OUT; 237*0Sstevel@tonic-gate } 238*0Sstevel@tonic-gate 239*0Sstevel@tonic-gate /* 240*0Sstevel@tonic-gate * preallocate a single buffer for all writes 241*0Sstevel@tonic-gate */ 242*0Sstevel@tonic-gate if (i2c_transfer_alloc(unitp->tda8444_hdl, &unitp->tda8444_transfer, 243*0Sstevel@tonic-gate 2, 0, I2C_SLEEP) != I2C_SUCCESS) { 244*0Sstevel@tonic-gate cmn_err(CE_WARN, "i2c_transfer_alloc failed"); 245*0Sstevel@tonic-gate ddi_remove_minor_node(dip, NULL); 246*0Sstevel@tonic-gate ddi_soft_state_free(tda8444_soft_statep, instance); 247*0Sstevel@tonic-gate 248*0Sstevel@tonic-gate return (DDI_FAILURE); 249*0Sstevel@tonic-gate } 250*0Sstevel@tonic-gate unitp->tda8444_transfer->i2c_flags = I2C_WR; 251*0Sstevel@tonic-gate unitp->tda8444_transfer->i2c_version = I2C_XFER_REV; 252*0Sstevel@tonic-gate 253*0Sstevel@tonic-gate if (i2c_client_register(dip, &unitp->tda8444_hdl) != I2C_SUCCESS) { 254*0Sstevel@tonic-gate ddi_remove_minor_node(dip, NULL); 255*0Sstevel@tonic-gate cmn_err(CE_WARN, "i2c_client_register failed"); 256*0Sstevel@tonic-gate ddi_soft_state_free(tda8444_soft_statep, instance); 257*0Sstevel@tonic-gate i2c_transfer_free(unitp->tda8444_hdl, unitp->tda8444_transfer); 258*0Sstevel@tonic-gate 259*0Sstevel@tonic-gate return (DDI_FAILURE); 260*0Sstevel@tonic-gate } 261*0Sstevel@tonic-gate 262*0Sstevel@tonic-gate mutex_init(&unitp->tda8444_mutex, NULL, MUTEX_DRIVER, NULL); 263*0Sstevel@tonic-gate cv_init(&unitp->tda8444_cv, NULL, CV_DRIVER, NULL); 264*0Sstevel@tonic-gate 265*0Sstevel@tonic-gate return (DDI_SUCCESS); 266*0Sstevel@tonic-gate } 267*0Sstevel@tonic-gate 268*0Sstevel@tonic-gate static int 269*0Sstevel@tonic-gate tda8444_attach(dev_info_t *dip, ddi_attach_cmd_t cmd) 270*0Sstevel@tonic-gate { 271*0Sstevel@tonic-gate switch (cmd) { 272*0Sstevel@tonic-gate case DDI_ATTACH: 273*0Sstevel@tonic-gate 274*0Sstevel@tonic-gate return (tda8444_do_attach(dip)); 275*0Sstevel@tonic-gate case DDI_RESUME: 276*0Sstevel@tonic-gate 277*0Sstevel@tonic-gate return (tda8444_do_resume(dip)); 278*0Sstevel@tonic-gate default: 279*0Sstevel@tonic-gate 280*0Sstevel@tonic-gate return (DDI_FAILURE); 281*0Sstevel@tonic-gate } 282*0Sstevel@tonic-gate } 283*0Sstevel@tonic-gate 284*0Sstevel@tonic-gate static int 285*0Sstevel@tonic-gate tda8444_do_detach(dev_info_t *dip) 286*0Sstevel@tonic-gate { 287*0Sstevel@tonic-gate struct tda8444_unit *unitp; 288*0Sstevel@tonic-gate int instance; 289*0Sstevel@tonic-gate 290*0Sstevel@tonic-gate instance = ddi_get_instance(dip); 291*0Sstevel@tonic-gate unitp = ddi_get_soft_state(tda8444_soft_statep, instance); 292*0Sstevel@tonic-gate 293*0Sstevel@tonic-gate i2c_transfer_free(unitp->tda8444_hdl, unitp->tda8444_transfer); 294*0Sstevel@tonic-gate i2c_client_unregister(unitp->tda8444_hdl); 295*0Sstevel@tonic-gate ddi_remove_minor_node(dip, NULL); 296*0Sstevel@tonic-gate mutex_destroy(&unitp->tda8444_mutex); 297*0Sstevel@tonic-gate cv_destroy(&unitp->tda8444_cv); 298*0Sstevel@tonic-gate ddi_soft_state_free(tda8444_soft_statep, instance); 299*0Sstevel@tonic-gate 300*0Sstevel@tonic-gate return (DDI_SUCCESS); 301*0Sstevel@tonic-gate } 302*0Sstevel@tonic-gate 303*0Sstevel@tonic-gate static int 304*0Sstevel@tonic-gate tda8444_do_suspend(dev_info_t *dip) 305*0Sstevel@tonic-gate { 306*0Sstevel@tonic-gate struct tda8444_unit *unitp; 307*0Sstevel@tonic-gate int instance; 308*0Sstevel@tonic-gate 309*0Sstevel@tonic-gate instance = ddi_get_instance(dip); 310*0Sstevel@tonic-gate unitp = ddi_get_soft_state(tda8444_soft_statep, instance); 311*0Sstevel@tonic-gate 312*0Sstevel@tonic-gate /* 313*0Sstevel@tonic-gate * Set the busy flag so that future transactions block 314*0Sstevel@tonic-gate * until resume. 315*0Sstevel@tonic-gate */ 316*0Sstevel@tonic-gate mutex_enter(&unitp->tda8444_mutex); 317*0Sstevel@tonic-gate while (unitp->tda8444_flags == TDA8444_BUSY) { 318*0Sstevel@tonic-gate if (cv_wait_sig(&unitp->tda8444_cv, 319*0Sstevel@tonic-gate &unitp->tda8444_mutex) <= 0) { 320*0Sstevel@tonic-gate mutex_exit(&unitp->tda8444_mutex); 321*0Sstevel@tonic-gate 322*0Sstevel@tonic-gate return (DDI_FAILURE); 323*0Sstevel@tonic-gate } 324*0Sstevel@tonic-gate } 325*0Sstevel@tonic-gate unitp->tda8444_flags = TDA8444_SUSPENDED; 326*0Sstevel@tonic-gate mutex_exit(&unitp->tda8444_mutex); 327*0Sstevel@tonic-gate return (DDI_SUCCESS); 328*0Sstevel@tonic-gate } 329*0Sstevel@tonic-gate 330*0Sstevel@tonic-gate static int 331*0Sstevel@tonic-gate tda8444_detach(dev_info_t *dip, ddi_detach_cmd_t cmd) 332*0Sstevel@tonic-gate { 333*0Sstevel@tonic-gate switch (cmd) { 334*0Sstevel@tonic-gate case DDI_DETACH: 335*0Sstevel@tonic-gate 336*0Sstevel@tonic-gate return (tda8444_do_detach(dip)); 337*0Sstevel@tonic-gate case DDI_SUSPEND: 338*0Sstevel@tonic-gate 339*0Sstevel@tonic-gate return (tda8444_do_suspend(dip)); 340*0Sstevel@tonic-gate default: 341*0Sstevel@tonic-gate 342*0Sstevel@tonic-gate return (DDI_FAILURE); 343*0Sstevel@tonic-gate } 344*0Sstevel@tonic-gate } 345*0Sstevel@tonic-gate 346*0Sstevel@tonic-gate static int 347*0Sstevel@tonic-gate tda8444_open(dev_t *devp, int flags, int otyp, cred_t *credp) 348*0Sstevel@tonic-gate { 349*0Sstevel@tonic-gate _NOTE(ARGUNUSED(credp)) 350*0Sstevel@tonic-gate struct tda8444_unit *unitp; 351*0Sstevel@tonic-gate int err = 0; 352*0Sstevel@tonic-gate int instance = TDA8444_MINOR_TO_DEVINST(*devp); 353*0Sstevel@tonic-gate int channel = TDA8444_MINOR_TO_CHANNEL(*devp); 354*0Sstevel@tonic-gate 355*0Sstevel@tonic-gate if (instance < 0) { 356*0Sstevel@tonic-gate 357*0Sstevel@tonic-gate return (ENXIO); 358*0Sstevel@tonic-gate } 359*0Sstevel@tonic-gate 360*0Sstevel@tonic-gate unitp = (struct tda8444_unit *) 361*0Sstevel@tonic-gate ddi_get_soft_state(tda8444_soft_statep, instance); 362*0Sstevel@tonic-gate 363*0Sstevel@tonic-gate if (unitp == NULL) { 364*0Sstevel@tonic-gate 365*0Sstevel@tonic-gate return (ENXIO); 366*0Sstevel@tonic-gate } 367*0Sstevel@tonic-gate 368*0Sstevel@tonic-gate if (otyp != OTYP_CHR) { 369*0Sstevel@tonic-gate 370*0Sstevel@tonic-gate return (EINVAL); 371*0Sstevel@tonic-gate } 372*0Sstevel@tonic-gate 373*0Sstevel@tonic-gate mutex_enter(&unitp->tda8444_mutex); 374*0Sstevel@tonic-gate 375*0Sstevel@tonic-gate if (flags & FEXCL) { 376*0Sstevel@tonic-gate if (unitp->tda8444_oflag[channel] != 0) { 377*0Sstevel@tonic-gate err = EBUSY; 378*0Sstevel@tonic-gate } else { 379*0Sstevel@tonic-gate unitp->tda8444_oflag[channel] = FEXCL; 380*0Sstevel@tonic-gate } 381*0Sstevel@tonic-gate } else { 382*0Sstevel@tonic-gate if (unitp->tda8444_oflag[channel] == FEXCL) { 383*0Sstevel@tonic-gate err = EBUSY; 384*0Sstevel@tonic-gate } else { 385*0Sstevel@tonic-gate unitp->tda8444_oflag[channel] = FOPEN; 386*0Sstevel@tonic-gate } 387*0Sstevel@tonic-gate } 388*0Sstevel@tonic-gate 389*0Sstevel@tonic-gate mutex_exit(&unitp->tda8444_mutex); 390*0Sstevel@tonic-gate 391*0Sstevel@tonic-gate return (err); 392*0Sstevel@tonic-gate } 393*0Sstevel@tonic-gate 394*0Sstevel@tonic-gate static int 395*0Sstevel@tonic-gate tda8444_close(dev_t dev, int flags, int otyp, cred_t *credp) 396*0Sstevel@tonic-gate { 397*0Sstevel@tonic-gate _NOTE(ARGUNUSED(flags, otyp, credp)) 398*0Sstevel@tonic-gate struct tda8444_unit *unitp; 399*0Sstevel@tonic-gate int instance = TDA8444_MINOR_TO_DEVINST(dev); 400*0Sstevel@tonic-gate int channel = TDA8444_MINOR_TO_CHANNEL(dev); 401*0Sstevel@tonic-gate 402*0Sstevel@tonic-gate if (instance < 0) { 403*0Sstevel@tonic-gate 404*0Sstevel@tonic-gate return (ENXIO); 405*0Sstevel@tonic-gate } 406*0Sstevel@tonic-gate 407*0Sstevel@tonic-gate unitp = (struct tda8444_unit *) 408*0Sstevel@tonic-gate ddi_get_soft_state(tda8444_soft_statep, instance); 409*0Sstevel@tonic-gate 410*0Sstevel@tonic-gate if (unitp == NULL) { 411*0Sstevel@tonic-gate 412*0Sstevel@tonic-gate return (ENXIO); 413*0Sstevel@tonic-gate } 414*0Sstevel@tonic-gate 415*0Sstevel@tonic-gate mutex_enter(&unitp->tda8444_mutex); 416*0Sstevel@tonic-gate 417*0Sstevel@tonic-gate unitp->tda8444_oflag[channel] = 0; 418*0Sstevel@tonic-gate 419*0Sstevel@tonic-gate mutex_exit(&unitp->tda8444_mutex); 420*0Sstevel@tonic-gate 421*0Sstevel@tonic-gate return (DDI_SUCCESS); 422*0Sstevel@tonic-gate } 423*0Sstevel@tonic-gate 424*0Sstevel@tonic-gate static int 425*0Sstevel@tonic-gate tda8444_read(dev_t dev, struct uio *uiop, cred_t *cred_p) 426*0Sstevel@tonic-gate { 427*0Sstevel@tonic-gate _NOTE(ARGUNUSED(cred_p)) 428*0Sstevel@tonic-gate return (tda8444_io(dev, uiop, B_READ)); 429*0Sstevel@tonic-gate } 430*0Sstevel@tonic-gate 431*0Sstevel@tonic-gate static int 432*0Sstevel@tonic-gate tda8444_write(dev_t dev, struct uio *uiop, cred_t *cred_p) 433*0Sstevel@tonic-gate { 434*0Sstevel@tonic-gate _NOTE(ARGUNUSED(cred_p)) 435*0Sstevel@tonic-gate return (tda8444_io(dev, uiop, B_WRITE)); 436*0Sstevel@tonic-gate } 437*0Sstevel@tonic-gate 438*0Sstevel@tonic-gate static int 439*0Sstevel@tonic-gate tda8444_io(dev_t dev, struct uio *uiop, int rw) 440*0Sstevel@tonic-gate { 441*0Sstevel@tonic-gate struct tda8444_unit *unitp; 442*0Sstevel@tonic-gate int instance = TDA8444_MINOR_TO_DEVINST(getminor(dev)); 443*0Sstevel@tonic-gate int channel = TDA8444_MINOR_TO_CHANNEL(getminor(dev)); 444*0Sstevel@tonic-gate int ret = 0; 445*0Sstevel@tonic-gate size_t len = uiop->uio_resid; 446*0Sstevel@tonic-gate int8_t out_value; 447*0Sstevel@tonic-gate 448*0Sstevel@tonic-gate if (instance < 0) { 449*0Sstevel@tonic-gate 450*0Sstevel@tonic-gate return (ENXIO); 451*0Sstevel@tonic-gate } 452*0Sstevel@tonic-gate 453*0Sstevel@tonic-gate if (len == 0) { 454*0Sstevel@tonic-gate return (0); 455*0Sstevel@tonic-gate } 456*0Sstevel@tonic-gate 457*0Sstevel@tonic-gate unitp = (struct tda8444_unit *) 458*0Sstevel@tonic-gate ddi_get_soft_state(tda8444_soft_statep, instance); 459*0Sstevel@tonic-gate 460*0Sstevel@tonic-gate if (unitp == NULL) { 461*0Sstevel@tonic-gate 462*0Sstevel@tonic-gate return (ENXIO); 463*0Sstevel@tonic-gate } 464*0Sstevel@tonic-gate 465*0Sstevel@tonic-gate if (rw == B_READ) { 466*0Sstevel@tonic-gate if (unitp->tda8444_output[channel] != TDA8444_UNKNOWN_OUT) { 467*0Sstevel@tonic-gate return (uiomove(&unitp->tda8444_output[channel], 1, 468*0Sstevel@tonic-gate UIO_READ, uiop)); 469*0Sstevel@tonic-gate } else { 470*0Sstevel@tonic-gate return (EIO); 471*0Sstevel@tonic-gate } 472*0Sstevel@tonic-gate } 473*0Sstevel@tonic-gate 474*0Sstevel@tonic-gate /* 475*0Sstevel@tonic-gate * rw == B_WRITE. Make sure each write to a device is single 476*0Sstevel@tonic-gate * threaded since we pre-allocate a single write buffer. This is not a 477*0Sstevel@tonic-gate * bottleneck since concurrent writes would serialize at the 478*0Sstevel@tonic-gate * transport level anyway. 479*0Sstevel@tonic-gate */ 480*0Sstevel@tonic-gate mutex_enter(&unitp->tda8444_mutex); 481*0Sstevel@tonic-gate if (unitp->tda8444_flags == TDA8444_SUSPENDED) { 482*0Sstevel@tonic-gate mutex_exit(&unitp->tda8444_mutex); 483*0Sstevel@tonic-gate 484*0Sstevel@tonic-gate return (EAGAIN); 485*0Sstevel@tonic-gate } 486*0Sstevel@tonic-gate 487*0Sstevel@tonic-gate while (unitp->tda8444_flags == TDA8444_BUSY) { 488*0Sstevel@tonic-gate if (cv_wait_sig(&unitp->tda8444_cv, 489*0Sstevel@tonic-gate &unitp->tda8444_mutex) <= 0) { 490*0Sstevel@tonic-gate mutex_exit(&unitp->tda8444_mutex); 491*0Sstevel@tonic-gate 492*0Sstevel@tonic-gate return (EINTR); 493*0Sstevel@tonic-gate } 494*0Sstevel@tonic-gate } 495*0Sstevel@tonic-gate unitp->tda8444_flags = TDA8444_BUSY; 496*0Sstevel@tonic-gate mutex_exit(&unitp->tda8444_mutex); 497*0Sstevel@tonic-gate 498*0Sstevel@tonic-gate unitp->tda8444_transfer->i2c_wbuf[0] = (TDA8444_REGBASE | channel); 499*0Sstevel@tonic-gate if ((ret = uiomove(&out_value, sizeof (out_value), UIO_WRITE, 500*0Sstevel@tonic-gate uiop)) == 0) { 501*0Sstevel@tonic-gate 502*0Sstevel@tonic-gate /* 503*0Sstevel@tonic-gate * Check bounds 504*0Sstevel@tonic-gate */ 505*0Sstevel@tonic-gate if ((out_value > TDA8444_MAX_OUT) || 506*0Sstevel@tonic-gate (out_value < TDA8444_MIN_OUT)) { 507*0Sstevel@tonic-gate ret = EINVAL; 508*0Sstevel@tonic-gate } else { 509*0Sstevel@tonic-gate unitp->tda8444_transfer->i2c_wbuf[1] = 510*0Sstevel@tonic-gate (uchar_t)out_value; 511*0Sstevel@tonic-gate DPRINTF(IO, ("setting channel %d to %d", channel, 512*0Sstevel@tonic-gate unitp->tda8444_transfer->i2c_wbuf[1])); 513*0Sstevel@tonic-gate 514*0Sstevel@tonic-gate if (i2c_transfer(unitp->tda8444_hdl, 515*0Sstevel@tonic-gate unitp->tda8444_transfer) != I2C_SUCCESS) { 516*0Sstevel@tonic-gate ret = EIO; 517*0Sstevel@tonic-gate } else { 518*0Sstevel@tonic-gate unitp->tda8444_output[channel] = out_value; 519*0Sstevel@tonic-gate } 520*0Sstevel@tonic-gate } 521*0Sstevel@tonic-gate } else { 522*0Sstevel@tonic-gate ret = EFAULT; 523*0Sstevel@tonic-gate } 524*0Sstevel@tonic-gate 525*0Sstevel@tonic-gate mutex_enter(&unitp->tda8444_mutex); 526*0Sstevel@tonic-gate unitp->tda8444_flags = 0; 527*0Sstevel@tonic-gate cv_signal(&unitp->tda8444_cv); 528*0Sstevel@tonic-gate mutex_exit(&unitp->tda8444_mutex); 529*0Sstevel@tonic-gate 530*0Sstevel@tonic-gate return (ret); 531*0Sstevel@tonic-gate } 532