17c478bd9Sstevel@tonic-gate /* 29db67a32Sanbui * CDDL HEADER START 39db67a32Sanbui * 49db67a32Sanbui * The contents of this file are subject to the terms of the 59db67a32Sanbui * Common Development and Distribution License (the "License"). 69db67a32Sanbui * You may not use this file except in compliance with the License. 79db67a32Sanbui * 89db67a32Sanbui * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 99db67a32Sanbui * or http://www.opensolaris.org/os/licensing. 109db67a32Sanbui * See the License for the specific language governing permissions 119db67a32Sanbui * and limitations under the License. 129db67a32Sanbui * 139db67a32Sanbui * When distributing Covered Code, include this CDDL HEADER in each 149db67a32Sanbui * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 159db67a32Sanbui * If applicable, add the following below this CDDL HEADER, with the 169db67a32Sanbui * fields enclosed by brackets "[]" replaced with your own identifying 179db67a32Sanbui * information: Portions Copyright [yyyy] [name of copyright owner] 189db67a32Sanbui * 199db67a32Sanbui * CDDL HEADER END 209db67a32Sanbui */ 219db67a32Sanbui /* 22*07d06da5SSurya Prakki * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 237c478bd9Sstevel@tonic-gate * Use is subject to license terms. 247c478bd9Sstevel@tonic-gate */ 257c478bd9Sstevel@tonic-gate 267c478bd9Sstevel@tonic-gate 277c478bd9Sstevel@tonic-gate #include <sys/stat.h> 287c478bd9Sstevel@tonic-gate #include <sys/file.h> 297c478bd9Sstevel@tonic-gate #include <sys/uio.h> 307c478bd9Sstevel@tonic-gate #include <sys/modctl.h> 317c478bd9Sstevel@tonic-gate #include <sys/open.h> 327c478bd9Sstevel@tonic-gate #include <sys/types.h> 337c478bd9Sstevel@tonic-gate #include <sys/kmem.h> 347c478bd9Sstevel@tonic-gate #include <sys/systm.h> 357c478bd9Sstevel@tonic-gate #include <sys/ddi.h> 367c478bd9Sstevel@tonic-gate #include <sys/sunddi.h> 377c478bd9Sstevel@tonic-gate #include <sys/conf.h> 387c478bd9Sstevel@tonic-gate #include <sys/mode.h> 397c478bd9Sstevel@tonic-gate #include <sys/policy.h> 407c478bd9Sstevel@tonic-gate 417c478bd9Sstevel@tonic-gate #include <sys/grfans.h> 427c478bd9Sstevel@tonic-gate 437c478bd9Sstevel@tonic-gate /* 447c478bd9Sstevel@tonic-gate * cb ops 457c478bd9Sstevel@tonic-gate */ 467c478bd9Sstevel@tonic-gate static int grfans_open(dev_t *, int, int, cred_t *); 477c478bd9Sstevel@tonic-gate static int grfans_close(dev_t, int, int, cred_t *); 487c478bd9Sstevel@tonic-gate static int grfans_read(dev_t dev, struct uio *uiop, cred_t *cred_p); 497c478bd9Sstevel@tonic-gate static int grfans_write(dev_t dev, struct uio *uiop, cred_t *cred_p); 507c478bd9Sstevel@tonic-gate static int grfans_io(dev_t dev, struct uio *uiop, int rw); 517c478bd9Sstevel@tonic-gate /* 527c478bd9Sstevel@tonic-gate * dev ops 537c478bd9Sstevel@tonic-gate */ 547c478bd9Sstevel@tonic-gate static int grfans_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, 557c478bd9Sstevel@tonic-gate void **result); 567c478bd9Sstevel@tonic-gate static int grfans_attach(dev_info_t *dip, ddi_attach_cmd_t cmd); 577c478bd9Sstevel@tonic-gate static int grfans_detach(dev_info_t *dip, ddi_detach_cmd_t cmd); 587c478bd9Sstevel@tonic-gate 597c478bd9Sstevel@tonic-gate static struct cb_ops grfans_cbops = { 607c478bd9Sstevel@tonic-gate grfans_open, /* open */ 617c478bd9Sstevel@tonic-gate grfans_close, /* close */ 627c478bd9Sstevel@tonic-gate nodev, /* strategy */ 637c478bd9Sstevel@tonic-gate nodev, /* print */ 647c478bd9Sstevel@tonic-gate nodev, /* dump */ 657c478bd9Sstevel@tonic-gate grfans_read, /* read */ 667c478bd9Sstevel@tonic-gate grfans_write, /* write */ 677c478bd9Sstevel@tonic-gate nodev, /* 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 static struct dev_ops grfans_ops = { 817c478bd9Sstevel@tonic-gate DEVO_REV, 827c478bd9Sstevel@tonic-gate 0, 837c478bd9Sstevel@tonic-gate grfans_info, 847c478bd9Sstevel@tonic-gate nulldev, 857c478bd9Sstevel@tonic-gate nulldev, 867c478bd9Sstevel@tonic-gate grfans_attach, 877c478bd9Sstevel@tonic-gate grfans_detach, 887c478bd9Sstevel@tonic-gate nodev, 897c478bd9Sstevel@tonic-gate &grfans_cbops, 9019397407SSherry Moore NULL, /* bus_ops */ 9119397407SSherry Moore NULL, /* power */ 9219397407SSherry Moore ddi_quiesce_not_needed, /* quiesce */ 937c478bd9Sstevel@tonic-gate }; 947c478bd9Sstevel@tonic-gate 957c478bd9Sstevel@tonic-gate static struct modldrv grfans_modldrv = { 967c478bd9Sstevel@tonic-gate &mod_driverops, /* type of module - driver */ 979db67a32Sanbui "grfans device driver", 987c478bd9Sstevel@tonic-gate &grfans_ops, 997c478bd9Sstevel@tonic-gate }; 1007c478bd9Sstevel@tonic-gate 1017c478bd9Sstevel@tonic-gate static struct modlinkage grfans_modlinkage = { 1027c478bd9Sstevel@tonic-gate MODREV_1, 1037c478bd9Sstevel@tonic-gate &grfans_modldrv, 1047c478bd9Sstevel@tonic-gate 0 1057c478bd9Sstevel@tonic-gate }; 1067c478bd9Sstevel@tonic-gate 1077c478bd9Sstevel@tonic-gate static void *grfans_soft_statep; 1087c478bd9Sstevel@tonic-gate static int grfans_debug = 0; 1097c478bd9Sstevel@tonic-gate 1107c478bd9Sstevel@tonic-gate int 1117c478bd9Sstevel@tonic-gate _init(void) 1127c478bd9Sstevel@tonic-gate { 1137c478bd9Sstevel@tonic-gate int error; 1147c478bd9Sstevel@tonic-gate 1157c478bd9Sstevel@tonic-gate error = mod_install(&grfans_modlinkage); 1167c478bd9Sstevel@tonic-gate if (error == 0) { 1177c478bd9Sstevel@tonic-gate (void) ddi_soft_state_init(&grfans_soft_statep, 1187c478bd9Sstevel@tonic-gate sizeof (struct grfans_unit), 1); 1197c478bd9Sstevel@tonic-gate } 1207c478bd9Sstevel@tonic-gate 1217c478bd9Sstevel@tonic-gate return (error); 1227c478bd9Sstevel@tonic-gate } 1237c478bd9Sstevel@tonic-gate 1247c478bd9Sstevel@tonic-gate int 1257c478bd9Sstevel@tonic-gate _fini(void) 1267c478bd9Sstevel@tonic-gate { 1277c478bd9Sstevel@tonic-gate int error; 1287c478bd9Sstevel@tonic-gate 1297c478bd9Sstevel@tonic-gate error = mod_remove(&grfans_modlinkage); 1307c478bd9Sstevel@tonic-gate if (error == 0) { 1317c478bd9Sstevel@tonic-gate ddi_soft_state_fini(&grfans_soft_statep); 1327c478bd9Sstevel@tonic-gate } 1337c478bd9Sstevel@tonic-gate 1347c478bd9Sstevel@tonic-gate return (error); 1357c478bd9Sstevel@tonic-gate } 1367c478bd9Sstevel@tonic-gate 1377c478bd9Sstevel@tonic-gate int 1387c478bd9Sstevel@tonic-gate _info(struct modinfo *modinfop) 1397c478bd9Sstevel@tonic-gate { 1407c478bd9Sstevel@tonic-gate return (mod_info(&grfans_modlinkage, modinfop)); 1417c478bd9Sstevel@tonic-gate } 1427c478bd9Sstevel@tonic-gate 1437c478bd9Sstevel@tonic-gate /* ARGSUSED */ 1447c478bd9Sstevel@tonic-gate static int 1457c478bd9Sstevel@tonic-gate grfans_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result) 1467c478bd9Sstevel@tonic-gate { 1477c478bd9Sstevel@tonic-gate dev_t dev; 1487c478bd9Sstevel@tonic-gate int instance; 1497c478bd9Sstevel@tonic-gate 1507c478bd9Sstevel@tonic-gate if (infocmd == DDI_INFO_DEVT2INSTANCE) { 1517c478bd9Sstevel@tonic-gate dev = (dev_t)arg; 1527c478bd9Sstevel@tonic-gate instance = MINOR_TO_DEVINST(dev); 1537c478bd9Sstevel@tonic-gate *result = (void *)(uintptr_t)instance; 1547c478bd9Sstevel@tonic-gate return (DDI_SUCCESS); 1557c478bd9Sstevel@tonic-gate } 1567c478bd9Sstevel@tonic-gate return (DDI_FAILURE); 1577c478bd9Sstevel@tonic-gate } 1587c478bd9Sstevel@tonic-gate 1597c478bd9Sstevel@tonic-gate static int 1607c478bd9Sstevel@tonic-gate grfans_do_attach(dev_info_t *dip) 1617c478bd9Sstevel@tonic-gate { 1627c478bd9Sstevel@tonic-gate struct grfans_unit *unitp; 1637c478bd9Sstevel@tonic-gate int instance; 1647c478bd9Sstevel@tonic-gate ddi_device_acc_attr_t attr; 1657c478bd9Sstevel@tonic-gate int nregs; 1667c478bd9Sstevel@tonic-gate char name[32]; 1677c478bd9Sstevel@tonic-gate 1687c478bd9Sstevel@tonic-gate instance = ddi_get_instance(dip); 1697c478bd9Sstevel@tonic-gate 1707c478bd9Sstevel@tonic-gate if (ddi_soft_state_zalloc(grfans_soft_statep, instance) != 0) { 1717c478bd9Sstevel@tonic-gate cmn_err(CE_WARN, "%s%d failed to zalloc softstate", 1727c478bd9Sstevel@tonic-gate ddi_get_name(dip), instance); 1737c478bd9Sstevel@tonic-gate 1747c478bd9Sstevel@tonic-gate return (DDI_FAILURE); 1757c478bd9Sstevel@tonic-gate } 1767c478bd9Sstevel@tonic-gate 1777c478bd9Sstevel@tonic-gate if (grfans_debug) { 1787c478bd9Sstevel@tonic-gate printf("attached instance number %d\n", instance); 1797c478bd9Sstevel@tonic-gate } 1807c478bd9Sstevel@tonic-gate 1817c478bd9Sstevel@tonic-gate unitp = ddi_get_soft_state(grfans_soft_statep, instance); 1827c478bd9Sstevel@tonic-gate if (unitp == NULL) 1837c478bd9Sstevel@tonic-gate return (DDI_FAILURE); 1847c478bd9Sstevel@tonic-gate 1857c478bd9Sstevel@tonic-gate (void) snprintf(name, sizeof (name), "%s%d", ddi_driver_name(dip), 1867c478bd9Sstevel@tonic-gate instance); 1877c478bd9Sstevel@tonic-gate 1887c478bd9Sstevel@tonic-gate attr.devacc_attr_version = DDI_DEVICE_ATTR_V0; 1897c478bd9Sstevel@tonic-gate attr.devacc_attr_endian_flags = DDI_STRUCTURE_LE_ACC; 1907c478bd9Sstevel@tonic-gate attr.devacc_attr_dataorder = DDI_STRICTORDER_ACC; 1917c478bd9Sstevel@tonic-gate 1927c478bd9Sstevel@tonic-gate if (grfans_debug) { 1937c478bd9Sstevel@tonic-gate printf("number of registers is %d\n", 1947c478bd9Sstevel@tonic-gate ddi_dev_nregs(dip, &nregs)); 1957c478bd9Sstevel@tonic-gate } 1967c478bd9Sstevel@tonic-gate 1977c478bd9Sstevel@tonic-gate if (ddi_regs_map_setup(dip, 0, 1987c478bd9Sstevel@tonic-gate (caddr_t *)&unitp->cpufan_reg, 1997c478bd9Sstevel@tonic-gate 3, 1, &attr, &unitp->cpufan_rhandle) != DDI_SUCCESS) { 2007c478bd9Sstevel@tonic-gate cmn_err(CE_WARN, "%s ddi_regs_map_setup failed for regset " 2017c478bd9Sstevel@tonic-gate "0", name); 2027c478bd9Sstevel@tonic-gate ddi_soft_state_free(grfans_soft_statep, instance); 2037c478bd9Sstevel@tonic-gate return (DDI_FAILURE); 2047c478bd9Sstevel@tonic-gate } 2057c478bd9Sstevel@tonic-gate 2067c478bd9Sstevel@tonic-gate if (ddi_regs_map_setup(dip, 1, 2077c478bd9Sstevel@tonic-gate (caddr_t *)&unitp->sysfan_reg, 2087c478bd9Sstevel@tonic-gate 0, 1, &attr, &unitp->sysfan_rhandle) != DDI_SUCCESS) { 2097c478bd9Sstevel@tonic-gate cmn_err(CE_WARN, "%s ddi_regs_map_setup failed for regset " 2107c478bd9Sstevel@tonic-gate "1", name); 2117c478bd9Sstevel@tonic-gate ddi_regs_map_free(&unitp->cpufan_rhandle); 2127c478bd9Sstevel@tonic-gate ddi_soft_state_free(grfans_soft_statep, instance); 2137c478bd9Sstevel@tonic-gate return (DDI_FAILURE); 2147c478bd9Sstevel@tonic-gate } 2157c478bd9Sstevel@tonic-gate 2167c478bd9Sstevel@tonic-gate if (ddi_create_minor_node(dip, "cpu_fan", S_IFCHR, 2177c478bd9Sstevel@tonic-gate DEVINST_TO_MINOR(instance) | CHANNEL_TO_MINOR(CPU_FAN_CHANNEL), 2187c478bd9Sstevel@tonic-gate FANS_NODE_TYPE, NULL) == DDI_FAILURE) { 2197c478bd9Sstevel@tonic-gate cmn_err(CE_WARN, "%s ddi_create_minor_node failed" 2207c478bd9Sstevel@tonic-gate " for cpu fan", name); 2217c478bd9Sstevel@tonic-gate ddi_regs_map_free(&unitp->cpufan_rhandle); 2227c478bd9Sstevel@tonic-gate ddi_regs_map_free(&unitp->sysfan_rhandle); 2237c478bd9Sstevel@tonic-gate ddi_soft_state_free(grfans_soft_statep, instance); 2247c478bd9Sstevel@tonic-gate ddi_remove_minor_node(dip, NULL); 2257c478bd9Sstevel@tonic-gate 2267c478bd9Sstevel@tonic-gate return (DDI_FAILURE); 2277c478bd9Sstevel@tonic-gate } 2287c478bd9Sstevel@tonic-gate 2297c478bd9Sstevel@tonic-gate if (ddi_create_minor_node(dip, "sys_fan", S_IFCHR, 2307c478bd9Sstevel@tonic-gate DEVINST_TO_MINOR(instance) | CHANNEL_TO_MINOR(SYSTEM_FAN_CHANNEL), 2317c478bd9Sstevel@tonic-gate FANS_NODE_TYPE, NULL) == DDI_FAILURE) { 2327c478bd9Sstevel@tonic-gate cmn_err(CE_WARN, "%s ddi_create_minor_node failed" 2337c478bd9Sstevel@tonic-gate " for system fan", name); 2347c478bd9Sstevel@tonic-gate ddi_regs_map_free(&unitp->cpufan_rhandle); 2357c478bd9Sstevel@tonic-gate ddi_regs_map_free(&unitp->sysfan_rhandle); 2367c478bd9Sstevel@tonic-gate ddi_soft_state_free(grfans_soft_statep, instance); 2377c478bd9Sstevel@tonic-gate ddi_remove_minor_node(dip, NULL); 2387c478bd9Sstevel@tonic-gate 2397c478bd9Sstevel@tonic-gate return (DDI_FAILURE); 2407c478bd9Sstevel@tonic-gate } 2417c478bd9Sstevel@tonic-gate 2427c478bd9Sstevel@tonic-gate mutex_init(&unitp->mutex, NULL, MUTEX_DRIVER, NULL); 2437c478bd9Sstevel@tonic-gate 2447c478bd9Sstevel@tonic-gate return (DDI_SUCCESS); 2457c478bd9Sstevel@tonic-gate } 2467c478bd9Sstevel@tonic-gate 2477c478bd9Sstevel@tonic-gate static int 2487c478bd9Sstevel@tonic-gate grfans_attach(dev_info_t *dip, ddi_attach_cmd_t cmd) 2497c478bd9Sstevel@tonic-gate { 2507c478bd9Sstevel@tonic-gate switch (cmd) { 2517c478bd9Sstevel@tonic-gate case DDI_ATTACH: 2527c478bd9Sstevel@tonic-gate return (grfans_do_attach(dip)); 2537c478bd9Sstevel@tonic-gate 2547c478bd9Sstevel@tonic-gate case DDI_RESUME: 2557c478bd9Sstevel@tonic-gate return (DDI_SUCCESS); 2567c478bd9Sstevel@tonic-gate 2577c478bd9Sstevel@tonic-gate default: 2587c478bd9Sstevel@tonic-gate return (DDI_FAILURE); 2597c478bd9Sstevel@tonic-gate } 2607c478bd9Sstevel@tonic-gate } 2617c478bd9Sstevel@tonic-gate 2627c478bd9Sstevel@tonic-gate static int 2637c478bd9Sstevel@tonic-gate grfans_do_detach(dev_info_t *dip) 2647c478bd9Sstevel@tonic-gate { 2657c478bd9Sstevel@tonic-gate struct grfans_unit *unitp; 2667c478bd9Sstevel@tonic-gate int instance; 2677c478bd9Sstevel@tonic-gate 2687c478bd9Sstevel@tonic-gate instance = ddi_get_instance(dip); 2697c478bd9Sstevel@tonic-gate unitp = ddi_get_soft_state(grfans_soft_statep, instance); 2707c478bd9Sstevel@tonic-gate ddi_remove_minor_node(dip, NULL); 2717c478bd9Sstevel@tonic-gate 2727c478bd9Sstevel@tonic-gate ddi_regs_map_free(&unitp->cpufan_rhandle); 2737c478bd9Sstevel@tonic-gate ddi_regs_map_free(&unitp->sysfan_rhandle); 2747c478bd9Sstevel@tonic-gate 2757c478bd9Sstevel@tonic-gate mutex_destroy(&unitp->mutex); 2767c478bd9Sstevel@tonic-gate 2777c478bd9Sstevel@tonic-gate ddi_soft_state_free(grfans_soft_statep, instance); 2787c478bd9Sstevel@tonic-gate 2797c478bd9Sstevel@tonic-gate return (DDI_SUCCESS); 2807c478bd9Sstevel@tonic-gate } 2817c478bd9Sstevel@tonic-gate 2827c478bd9Sstevel@tonic-gate static int 2837c478bd9Sstevel@tonic-gate grfans_detach(dev_info_t *dip, ddi_detach_cmd_t cmd) 2847c478bd9Sstevel@tonic-gate { 2857c478bd9Sstevel@tonic-gate switch (cmd) { 2867c478bd9Sstevel@tonic-gate case DDI_DETACH: 2877c478bd9Sstevel@tonic-gate return (grfans_do_detach(dip)); 2887c478bd9Sstevel@tonic-gate 2897c478bd9Sstevel@tonic-gate case DDI_SUSPEND: 2907c478bd9Sstevel@tonic-gate return (DDI_SUCCESS); 2917c478bd9Sstevel@tonic-gate 2927c478bd9Sstevel@tonic-gate default: 2937c478bd9Sstevel@tonic-gate return (DDI_FAILURE); 2947c478bd9Sstevel@tonic-gate } 2957c478bd9Sstevel@tonic-gate } 2967c478bd9Sstevel@tonic-gate 2977c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 2987c478bd9Sstevel@tonic-gate static int 2997c478bd9Sstevel@tonic-gate grfans_open(dev_t *devp, int flags, int otyp, cred_t *credp) 3007c478bd9Sstevel@tonic-gate { 3017c478bd9Sstevel@tonic-gate struct grfans_unit *unitp; 3027c478bd9Sstevel@tonic-gate int err = 0; 3037c478bd9Sstevel@tonic-gate int instance = MINOR_TO_DEVINST(*devp); 3047c478bd9Sstevel@tonic-gate int channel; 3057c478bd9Sstevel@tonic-gate 3067c478bd9Sstevel@tonic-gate /* 3077c478bd9Sstevel@tonic-gate * must be privileged to access this device 3087c478bd9Sstevel@tonic-gate */ 3097c478bd9Sstevel@tonic-gate if (secpolicy_sys_config(credp, B_FALSE) != 0) 3107c478bd9Sstevel@tonic-gate return (EPERM); 3117c478bd9Sstevel@tonic-gate 3127c478bd9Sstevel@tonic-gate if (instance < 0) { 3137c478bd9Sstevel@tonic-gate cmn_err(CE_WARN, "grfan: instance less than 0: %d\n", 3147c478bd9Sstevel@tonic-gate instance); 3157c478bd9Sstevel@tonic-gate 3167c478bd9Sstevel@tonic-gate return (ENXIO); 3177c478bd9Sstevel@tonic-gate } 3187c478bd9Sstevel@tonic-gate 3197c478bd9Sstevel@tonic-gate unitp = ddi_get_soft_state(grfans_soft_statep, instance); 3207c478bd9Sstevel@tonic-gate if (unitp == NULL) { 3217c478bd9Sstevel@tonic-gate cmn_err(CE_WARN, "grfan: no soft state for instance %d\n", 3227c478bd9Sstevel@tonic-gate instance); 3237c478bd9Sstevel@tonic-gate 3247c478bd9Sstevel@tonic-gate return (ENXIO); 3257c478bd9Sstevel@tonic-gate } 3267c478bd9Sstevel@tonic-gate 3277c478bd9Sstevel@tonic-gate if (otyp != OTYP_CHR) 3287c478bd9Sstevel@tonic-gate return (EINVAL); 3297c478bd9Sstevel@tonic-gate 3307c478bd9Sstevel@tonic-gate channel = MINOR_TO_CHANNEL(getminor(*devp)); 3317c478bd9Sstevel@tonic-gate 3327c478bd9Sstevel@tonic-gate mutex_enter(&unitp->mutex); 3337c478bd9Sstevel@tonic-gate 3347c478bd9Sstevel@tonic-gate if (flags & FEXCL) { 3357c478bd9Sstevel@tonic-gate if (unitp->oflag[channel] != 0) 3367c478bd9Sstevel@tonic-gate err = EBUSY; 3377c478bd9Sstevel@tonic-gate else 3387c478bd9Sstevel@tonic-gate unitp->oflag[channel] = FEXCL; 3397c478bd9Sstevel@tonic-gate } else { 3407c478bd9Sstevel@tonic-gate if (unitp->oflag[channel] == FEXCL) 3417c478bd9Sstevel@tonic-gate err = EBUSY; 3427c478bd9Sstevel@tonic-gate else 343f47a9c50Smathue unitp->oflag[channel] = (uint16_t)FOPEN; 3447c478bd9Sstevel@tonic-gate } 3457c478bd9Sstevel@tonic-gate 3467c478bd9Sstevel@tonic-gate mutex_exit(&unitp->mutex); 3477c478bd9Sstevel@tonic-gate 3487c478bd9Sstevel@tonic-gate return (err); 3497c478bd9Sstevel@tonic-gate } 3507c478bd9Sstevel@tonic-gate 3517c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 3527c478bd9Sstevel@tonic-gate static int 3537c478bd9Sstevel@tonic-gate grfans_close(dev_t dev, int flags, int otyp, cred_t *credp) 3547c478bd9Sstevel@tonic-gate { 3557c478bd9Sstevel@tonic-gate struct grfans_unit *unitp; 3567c478bd9Sstevel@tonic-gate int instance = MINOR_TO_DEVINST(dev); 3577c478bd9Sstevel@tonic-gate int channel; 3587c478bd9Sstevel@tonic-gate 3597c478bd9Sstevel@tonic-gate if (instance < 0) 3607c478bd9Sstevel@tonic-gate return (ENXIO); 3617c478bd9Sstevel@tonic-gate 3627c478bd9Sstevel@tonic-gate unitp = ddi_get_soft_state(grfans_soft_statep, instance); 3637c478bd9Sstevel@tonic-gate if (unitp == NULL) 3647c478bd9Sstevel@tonic-gate return (ENXIO); 3657c478bd9Sstevel@tonic-gate 3667c478bd9Sstevel@tonic-gate channel = MINOR_TO_CHANNEL(getminor(dev)); 3677c478bd9Sstevel@tonic-gate 3687c478bd9Sstevel@tonic-gate unitp->oflag[channel] = 0; 3697c478bd9Sstevel@tonic-gate 3707c478bd9Sstevel@tonic-gate return (DDI_SUCCESS); 3717c478bd9Sstevel@tonic-gate } 3727c478bd9Sstevel@tonic-gate 3737c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 3747c478bd9Sstevel@tonic-gate static int 3757c478bd9Sstevel@tonic-gate grfans_read(dev_t dev, struct uio *uiop, cred_t *cred_p) 3767c478bd9Sstevel@tonic-gate { 3777c478bd9Sstevel@tonic-gate return (grfans_io(dev, uiop, B_READ)); 3787c478bd9Sstevel@tonic-gate } 3797c478bd9Sstevel@tonic-gate 3807c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 3817c478bd9Sstevel@tonic-gate static int 3827c478bd9Sstevel@tonic-gate grfans_write(dev_t dev, struct uio *uiop, cred_t *cred_p) 3837c478bd9Sstevel@tonic-gate { 3847c478bd9Sstevel@tonic-gate return (grfans_io(dev, uiop, B_WRITE)); 3857c478bd9Sstevel@tonic-gate } 3867c478bd9Sstevel@tonic-gate 3877c478bd9Sstevel@tonic-gate static int 3887c478bd9Sstevel@tonic-gate grfans_io(dev_t dev, struct uio *uiop, int rw) 3897c478bd9Sstevel@tonic-gate { 3907c478bd9Sstevel@tonic-gate struct grfans_unit *unitp; 3917c478bd9Sstevel@tonic-gate int instance = MINOR_TO_DEVINST(getminor(dev)); 3927c478bd9Sstevel@tonic-gate int ret = 0; 3937c478bd9Sstevel@tonic-gate size_t len = uiop->uio_resid; 3947c478bd9Sstevel@tonic-gate int8_t out_value, req_value, reg_value; 3957c478bd9Sstevel@tonic-gate caddr_t outputaddr; 3967c478bd9Sstevel@tonic-gate 3977c478bd9Sstevel@tonic-gate if (instance < 0) 3987c478bd9Sstevel@tonic-gate return (ENXIO); 3997c478bd9Sstevel@tonic-gate 4007c478bd9Sstevel@tonic-gate if (len == 0) 4017c478bd9Sstevel@tonic-gate return (0); 4027c478bd9Sstevel@tonic-gate 4037c478bd9Sstevel@tonic-gate unitp = ddi_get_soft_state(grfans_soft_statep, instance); 4047c478bd9Sstevel@tonic-gate 4057c478bd9Sstevel@tonic-gate if (unitp == NULL) 4067c478bd9Sstevel@tonic-gate return (ENXIO); 4077c478bd9Sstevel@tonic-gate 4087c478bd9Sstevel@tonic-gate if (MINOR_TO_CHANNEL(getminor(dev)) == CPU_FAN_CHANNEL) 4097c478bd9Sstevel@tonic-gate outputaddr = &unitp->cpufan_output; 4107c478bd9Sstevel@tonic-gate else 4117c478bd9Sstevel@tonic-gate outputaddr = &unitp->sysfan_output; 4127c478bd9Sstevel@tonic-gate 4137c478bd9Sstevel@tonic-gate if (rw == B_READ) { 4147c478bd9Sstevel@tonic-gate if (*outputaddr == UNKNOWN_OUT) 4157c478bd9Sstevel@tonic-gate return (EIO); 4167c478bd9Sstevel@tonic-gate return (uiomove(outputaddr, 1, UIO_READ, uiop)); 4177c478bd9Sstevel@tonic-gate } 4187c478bd9Sstevel@tonic-gate 4197c478bd9Sstevel@tonic-gate /* 4207c478bd9Sstevel@tonic-gate * rw == B_WRITE. 4217c478bd9Sstevel@tonic-gate */ 4227c478bd9Sstevel@tonic-gate if ((ret = uiomove(&req_value, sizeof (req_value), UIO_WRITE, 4237c478bd9Sstevel@tonic-gate uiop)) == 0) { 4247c478bd9Sstevel@tonic-gate if (MINOR_TO_CHANNEL(dev) == CPU_FAN_CHANNEL) { 4257c478bd9Sstevel@tonic-gate /* 4267c478bd9Sstevel@tonic-gate * Check bounds for cpu fan 4277c478bd9Sstevel@tonic-gate */ 4287c478bd9Sstevel@tonic-gate if (req_value == 0) { 4297c478bd9Sstevel@tonic-gate reg_value = CPU_FAN_0; 4307c478bd9Sstevel@tonic-gate out_value = 0; 4317c478bd9Sstevel@tonic-gate } else if (req_value <= 25) { 4327c478bd9Sstevel@tonic-gate reg_value = CPU_FAN_25; 4337c478bd9Sstevel@tonic-gate out_value = 25; 4347c478bd9Sstevel@tonic-gate } else if (req_value <= 50) { 4357c478bd9Sstevel@tonic-gate reg_value = CPU_FAN_50; 4367c478bd9Sstevel@tonic-gate out_value = 50; 4377c478bd9Sstevel@tonic-gate } else if (req_value <= 75) { 4387c478bd9Sstevel@tonic-gate reg_value = CPU_FAN_75; 4397c478bd9Sstevel@tonic-gate out_value = 75; 4407c478bd9Sstevel@tonic-gate } else if (req_value <= 100) { 4417c478bd9Sstevel@tonic-gate reg_value = CPU_FAN_100; 4427c478bd9Sstevel@tonic-gate out_value = 100; 4437c478bd9Sstevel@tonic-gate } else 4447c478bd9Sstevel@tonic-gate ret = EINVAL; 4457c478bd9Sstevel@tonic-gate 4467c478bd9Sstevel@tonic-gate if (ret != EINVAL) { 4477c478bd9Sstevel@tonic-gate uint8_t reg; 4487c478bd9Sstevel@tonic-gate 4497c478bd9Sstevel@tonic-gate *outputaddr = out_value; 4507c478bd9Sstevel@tonic-gate 4517c478bd9Sstevel@tonic-gate reg = ddi_get8(unitp->cpufan_rhandle, 4527c478bd9Sstevel@tonic-gate unitp->cpufan_reg); 4537c478bd9Sstevel@tonic-gate reg = (reg & ~CPU_FAN_MASK) | reg_value; 4547c478bd9Sstevel@tonic-gate ddi_put8(unitp->cpufan_rhandle, 4557c478bd9Sstevel@tonic-gate unitp->cpufan_reg, reg); 4567c478bd9Sstevel@tonic-gate (void) ddi_get8(unitp->cpufan_rhandle, 4577c478bd9Sstevel@tonic-gate unitp->cpufan_reg); 4587c478bd9Sstevel@tonic-gate 4597c478bd9Sstevel@tonic-gate if (grfans_debug) { 4607c478bd9Sstevel@tonic-gate printf("set output to %d at addr %p\n", 4617c478bd9Sstevel@tonic-gate out_value, 462*07d06da5SSurya Prakki (void *)unitp->cpufan_reg); 4637c478bd9Sstevel@tonic-gate } 4647c478bd9Sstevel@tonic-gate } 4657c478bd9Sstevel@tonic-gate } else { 4667c478bd9Sstevel@tonic-gate if (req_value == 0) { 4677c478bd9Sstevel@tonic-gate reg_value = SYS_FAN_OFF; 4687c478bd9Sstevel@tonic-gate out_value = 0; 4697c478bd9Sstevel@tonic-gate } else if (req_value > 0) { 4707c478bd9Sstevel@tonic-gate reg_value = SYS_FAN_ON; 4717c478bd9Sstevel@tonic-gate out_value = 100; 4727c478bd9Sstevel@tonic-gate } else { 4737c478bd9Sstevel@tonic-gate ret = EINVAL; 4747c478bd9Sstevel@tonic-gate } 4757c478bd9Sstevel@tonic-gate 4767c478bd9Sstevel@tonic-gate if (ret != EINVAL) { 4777c478bd9Sstevel@tonic-gate *outputaddr = out_value; 4787c478bd9Sstevel@tonic-gate 4797c478bd9Sstevel@tonic-gate ddi_put8(unitp->sysfan_rhandle, 4807c478bd9Sstevel@tonic-gate unitp->sysfan_reg, 4817c478bd9Sstevel@tonic-gate reg_value); 4827c478bd9Sstevel@tonic-gate (void) ddi_get8(unitp->sysfan_rhandle, 4837c478bd9Sstevel@tonic-gate unitp->sysfan_reg); 4847c478bd9Sstevel@tonic-gate if (grfans_debug) { 4857c478bd9Sstevel@tonic-gate printf("set SYSFAN output to %d at " 4867c478bd9Sstevel@tonic-gate "addr %p\n", out_value, 487*07d06da5SSurya Prakki (void *)unitp->sysfan_reg); 4887c478bd9Sstevel@tonic-gate } 4897c478bd9Sstevel@tonic-gate } 4907c478bd9Sstevel@tonic-gate } 4917c478bd9Sstevel@tonic-gate } else { 4927c478bd9Sstevel@tonic-gate ret = EFAULT; 4937c478bd9Sstevel@tonic-gate } 4947c478bd9Sstevel@tonic-gate 4957c478bd9Sstevel@tonic-gate return (ret); 4967c478bd9Sstevel@tonic-gate } 497