1*b6faf3cfSNathan Whitehorn /*- 2*b6faf3cfSNathan Whitehorn * Copyright (c) 2011, Justin Hibbits. 3*b6faf3cfSNathan Whitehorn * Copyright (c) 2002, Miodrag Vallat. 4*b6faf3cfSNathan Whitehorn * Copyright (C) 1999 Tsubai Masanari. All rights reserved. 5*b6faf3cfSNathan Whitehorn * 6*b6faf3cfSNathan Whitehorn * Redistribution and use in source and binary forms, with or without 7*b6faf3cfSNathan Whitehorn * modification, are permitted provided that the following conditions 8*b6faf3cfSNathan Whitehorn * are met: 9*b6faf3cfSNathan Whitehorn * 1. Redistributions of source code must retain the above copyright 10*b6faf3cfSNathan Whitehorn * notice, this list of conditions and the following disclaimer. 11*b6faf3cfSNathan Whitehorn * 2. Redistributions in binary form must reproduce the above copyright 12*b6faf3cfSNathan Whitehorn * notice, this list of conditions and the following disclaimer in the 13*b6faf3cfSNathan Whitehorn * documentation and/or other materials provided with the distribution. 14*b6faf3cfSNathan Whitehorn * 3. The name of the author may not be used to endorse or promote products 15*b6faf3cfSNathan Whitehorn * derived from this software without specific prior written permission. 16*b6faf3cfSNathan Whitehorn * 17*b6faf3cfSNathan Whitehorn * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 18*b6faf3cfSNathan Whitehorn * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 19*b6faf3cfSNathan Whitehorn * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20*b6faf3cfSNathan Whitehorn * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 21*b6faf3cfSNathan Whitehorn * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22*b6faf3cfSNathan Whitehorn * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23*b6faf3cfSNathan Whitehorn * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24*b6faf3cfSNathan Whitehorn * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25*b6faf3cfSNathan Whitehorn * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 26*b6faf3cfSNathan Whitehorn * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27*b6faf3cfSNathan Whitehorn * 28*b6faf3cfSNathan Whitehorn * OpenBSD: abtn.c,v 1.12 2009/01/10 18:00:59 robert Exp 29*b6faf3cfSNathan Whitehorn * NetBSD: abtn.c,v 1.1 1999/07/12 17:48:26 tsubai Exp 30*b6faf3cfSNathan Whitehorn * 31*b6faf3cfSNathan Whitehorn * $FreeBSD$ 32*b6faf3cfSNathan Whitehorn */ 33*b6faf3cfSNathan Whitehorn 34*b6faf3cfSNathan Whitehorn #include <sys/param.h> 35*b6faf3cfSNathan Whitehorn #include <sys/systm.h> 36*b6faf3cfSNathan Whitehorn #include <sys/module.h> 37*b6faf3cfSNathan Whitehorn #include <sys/kernel.h> 38*b6faf3cfSNathan Whitehorn #include <sys/bus.h> 39*b6faf3cfSNathan Whitehorn 40*b6faf3cfSNathan Whitehorn #include <machine/bus.h> 41*b6faf3cfSNathan Whitehorn 42*b6faf3cfSNathan Whitehorn #include <dev/ofw/openfirm.h> 43*b6faf3cfSNathan Whitehorn #include <machine/ofw_machdep.h> 44*b6faf3cfSNathan Whitehorn 45*b6faf3cfSNathan Whitehorn #include <dev/adb/adb.h> 46*b6faf3cfSNathan Whitehorn 47*b6faf3cfSNathan Whitehorn #define ABTN_HANDLER_ID 31 48*b6faf3cfSNathan Whitehorn 49*b6faf3cfSNathan Whitehorn struct abtn_softc { 50*b6faf3cfSNathan Whitehorn device_t sc_dev; 51*b6faf3cfSNathan Whitehorn 52*b6faf3cfSNathan Whitehorn int handler_id; 53*b6faf3cfSNathan Whitehorn }; 54*b6faf3cfSNathan Whitehorn 55*b6faf3cfSNathan Whitehorn static int abtn_probe(device_t dev); 56*b6faf3cfSNathan Whitehorn static int abtn_attach(device_t dev); 57*b6faf3cfSNathan Whitehorn static u_int abtn_receive_packet(device_t dev, u_char status, 58*b6faf3cfSNathan Whitehorn u_char command, u_char reg, int len, u_char *data); 59*b6faf3cfSNathan Whitehorn 60*b6faf3cfSNathan Whitehorn static device_method_t abtn_methods[] = { 61*b6faf3cfSNathan Whitehorn /* Device interface */ 62*b6faf3cfSNathan Whitehorn DEVMETHOD(device_probe, abtn_probe), 63*b6faf3cfSNathan Whitehorn DEVMETHOD(device_attach, abtn_attach), 64*b6faf3cfSNathan Whitehorn DEVMETHOD(device_shutdown, bus_generic_shutdown), 65*b6faf3cfSNathan Whitehorn DEVMETHOD(device_suspend, bus_generic_suspend), 66*b6faf3cfSNathan Whitehorn DEVMETHOD(device_resume, bus_generic_resume), 67*b6faf3cfSNathan Whitehorn 68*b6faf3cfSNathan Whitehorn /* ADB interface */ 69*b6faf3cfSNathan Whitehorn DEVMETHOD(adb_receive_packet, abtn_receive_packet), 70*b6faf3cfSNathan Whitehorn 71*b6faf3cfSNathan Whitehorn { 0, 0 } 72*b6faf3cfSNathan Whitehorn }; 73*b6faf3cfSNathan Whitehorn 74*b6faf3cfSNathan Whitehorn static driver_t abtn_driver = { 75*b6faf3cfSNathan Whitehorn "abtn", 76*b6faf3cfSNathan Whitehorn abtn_methods, 77*b6faf3cfSNathan Whitehorn sizeof(struct abtn_softc), 78*b6faf3cfSNathan Whitehorn }; 79*b6faf3cfSNathan Whitehorn 80*b6faf3cfSNathan Whitehorn static devclass_t abtn_devclass; 81*b6faf3cfSNathan Whitehorn 82*b6faf3cfSNathan Whitehorn DRIVER_MODULE(abtn, adb, abtn_driver, abtn_devclass, 0, 0); 83*b6faf3cfSNathan Whitehorn 84*b6faf3cfSNathan Whitehorn static int 85*b6faf3cfSNathan Whitehorn abtn_probe(device_t dev) 86*b6faf3cfSNathan Whitehorn { 87*b6faf3cfSNathan Whitehorn uint8_t type; 88*b6faf3cfSNathan Whitehorn 89*b6faf3cfSNathan Whitehorn type = adb_get_device_type(dev); 90*b6faf3cfSNathan Whitehorn 91*b6faf3cfSNathan Whitehorn if (type != ADB_DEVICE_MISC) 92*b6faf3cfSNathan Whitehorn return (ENXIO); 93*b6faf3cfSNathan Whitehorn 94*b6faf3cfSNathan Whitehorn device_set_desc(dev, "ADB Brightness/Volume/Eject Buttons"); 95*b6faf3cfSNathan Whitehorn return (0); 96*b6faf3cfSNathan Whitehorn } 97*b6faf3cfSNathan Whitehorn 98*b6faf3cfSNathan Whitehorn static int 99*b6faf3cfSNathan Whitehorn abtn_attach(device_t dev) 100*b6faf3cfSNathan Whitehorn { 101*b6faf3cfSNathan Whitehorn struct abtn_softc *sc; 102*b6faf3cfSNathan Whitehorn 103*b6faf3cfSNathan Whitehorn sc = device_get_softc(dev); 104*b6faf3cfSNathan Whitehorn sc->sc_dev = dev; 105*b6faf3cfSNathan Whitehorn 106*b6faf3cfSNathan Whitehorn sc->handler_id = adb_get_device_handler(dev); 107*b6faf3cfSNathan Whitehorn 108*b6faf3cfSNathan Whitehorn return 0; 109*b6faf3cfSNathan Whitehorn } 110*b6faf3cfSNathan Whitehorn 111*b6faf3cfSNathan Whitehorn static u_int 112*b6faf3cfSNathan Whitehorn abtn_receive_packet(device_t dev, u_char status, 113*b6faf3cfSNathan Whitehorn u_char command, u_char reg, int len, u_char *data) 114*b6faf3cfSNathan Whitehorn { 115*b6faf3cfSNathan Whitehorn u_int cmd; 116*b6faf3cfSNathan Whitehorn 117*b6faf3cfSNathan Whitehorn cmd = data[0]; 118*b6faf3cfSNathan Whitehorn 119*b6faf3cfSNathan Whitehorn switch (cmd) { 120*b6faf3cfSNathan Whitehorn case 0x0a: /* decrease brightness */ 121*b6faf3cfSNathan Whitehorn if (devctl_process_running()) 122*b6faf3cfSNathan Whitehorn devctl_notify("PMU", "keys", "brightness", 123*b6faf3cfSNathan Whitehorn "notify=down"); 124*b6faf3cfSNathan Whitehorn break; 125*b6faf3cfSNathan Whitehorn 126*b6faf3cfSNathan Whitehorn case 0x09: /* increase brightness */ 127*b6faf3cfSNathan Whitehorn if (devctl_process_running()) 128*b6faf3cfSNathan Whitehorn devctl_notify("PMU", "keys", "brightness", "notify=up"); 129*b6faf3cfSNathan Whitehorn break; 130*b6faf3cfSNathan Whitehorn 131*b6faf3cfSNathan Whitehorn case 0x08: /* mute */ 132*b6faf3cfSNathan Whitehorn case 0x01: /* mute, AV hardware */ 133*b6faf3cfSNathan Whitehorn if (devctl_process_running()) 134*b6faf3cfSNathan Whitehorn devctl_notify("PMU", "keys", "mute", NULL); 135*b6faf3cfSNathan Whitehorn break; 136*b6faf3cfSNathan Whitehorn case 0x07: /* decrease volume */ 137*b6faf3cfSNathan Whitehorn case 0x02: /* decrease volume, AV hardware */ 138*b6faf3cfSNathan Whitehorn if (devctl_process_running()) 139*b6faf3cfSNathan Whitehorn devctl_notify("PMU", "keys", "volume", "notify=down"); 140*b6faf3cfSNathan Whitehorn break; 141*b6faf3cfSNathan Whitehorn case 0x06: /* increase volume */ 142*b6faf3cfSNathan Whitehorn case 0x03: /* increase volume, AV hardware */ 143*b6faf3cfSNathan Whitehorn if (devctl_process_running()) 144*b6faf3cfSNathan Whitehorn devctl_notify("PMU", "keys", "volume", "notify=up"); 145*b6faf3cfSNathan Whitehorn break; 146*b6faf3cfSNathan Whitehorn case 0x0c: /* mirror display key */ 147*b6faf3cfSNathan Whitehorn /* Need callback to do something with this */ 148*b6faf3cfSNathan Whitehorn break; 149*b6faf3cfSNathan Whitehorn case 0x0b: /* eject tray */ 150*b6faf3cfSNathan Whitehorn if (devctl_process_running()) 151*b6faf3cfSNathan Whitehorn devctl_notify("PMU", "keys", "eject", NULL); 152*b6faf3cfSNathan Whitehorn case 0x7f: /* numlock */ 153*b6faf3cfSNathan Whitehorn /* Need callback to do something with this */ 154*b6faf3cfSNathan Whitehorn break; 155*b6faf3cfSNathan Whitehorn 156*b6faf3cfSNathan Whitehorn default: 157*b6faf3cfSNathan Whitehorn #ifdef DEBUG 158*b6faf3cfSNathan Whitehorn if ((cmd & ~0x7f) == 0) 159*b6faf3cfSNathan Whitehorn device_printf(dev, "unknown ADB button 0x%x\n", cmd); 160*b6faf3cfSNathan Whitehorn #endif 161*b6faf3cfSNathan Whitehorn break; 162*b6faf3cfSNathan Whitehorn } 163*b6faf3cfSNathan Whitehorn return 0; 164*b6faf3cfSNathan Whitehorn } 165*b6faf3cfSNathan Whitehorn 166