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