| 1 | /******************************************************************************
|
|---|
| 2 | * apm.c - Functions to interface with the APM driver.
|
|---|
| 3 | *
|
|---|
| 4 | * Copyright (c) 2011 thi.guten Software Development
|
|---|
| 5 | * Copyright (c) 2011 Mensys B.V.
|
|---|
| 6 | *
|
|---|
| 7 | * Authors: Christian Mueller, Markus Thielen
|
|---|
| 8 | *
|
|---|
| 9 | * Parts copied from/inspired by the Linux AHCI driver;
|
|---|
| 10 | * those parts are (c) Linux AHCI/ATA maintainers
|
|---|
| 11 | *
|
|---|
| 12 | * This program is free software; you can redistribute it and/or modify
|
|---|
| 13 | * it under the terms of the GNU General Public License as published by
|
|---|
| 14 | * the Free Software Foundation; either version 2 of the License, or
|
|---|
| 15 | * (at your option) any later version.
|
|---|
| 16 | *
|
|---|
| 17 | * This program is distributed in the hope that it will be useful,
|
|---|
| 18 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|---|
| 19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|---|
| 20 | * GNU General Public License for more details.
|
|---|
| 21 | *
|
|---|
| 22 | * You should have received a copy of the GNU General Public License
|
|---|
| 23 | * along with this program; if not, write to the Free Software
|
|---|
| 24 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|---|
| 25 | */
|
|---|
| 26 |
|
|---|
| 27 | #include "os2ahci.h"
|
|---|
| 28 |
|
|---|
| 29 | #include <apmcalls.h>
|
|---|
| 30 |
|
|---|
| 31 | /* -------------------------- macros and constants ------------------------- */
|
|---|
| 32 |
|
|---|
| 33 | /* ------------------------ typedefs and structures ------------------------ */
|
|---|
| 34 |
|
|---|
| 35 | /* -------------------------- function prototypes -------------------------- */
|
|---|
| 36 |
|
|---|
| 37 | /* ------------------------ global/static variables ------------------------ */
|
|---|
| 38 |
|
|---|
| 39 | USHORT _far _cdecl apm_event (APMEVENT _far *evt);
|
|---|
| 40 |
|
|---|
| 41 | /* ----------------------------- start of code ----------------------------- */
|
|---|
| 42 |
|
|---|
| 43 | /******************************************************************************
|
|---|
| 44 | * Connect to APM driver and register for power state change events.
|
|---|
| 45 | */
|
|---|
| 46 | void apm_init(void)
|
|---|
| 47 | {
|
|---|
| 48 | USHORT rc;
|
|---|
| 49 |
|
|---|
| 50 | /* connect to APM driver */
|
|---|
| 51 | if ((rc = APMAttach()) != 0) {
|
|---|
| 52 | dprintf("couldn't connect to APM driver (rc = %d)\n", rc);
|
|---|
| 53 | return;
|
|---|
| 54 | }
|
|---|
| 55 |
|
|---|
| 56 | /* register for suspend/resume events */
|
|---|
| 57 | if ((rc = APMRegister(apm_event, APM_NOTIFYSETPWR |
|
|---|
| 58 | APM_NOTIFYNORMRESUME |
|
|---|
| 59 | APM_NOTIFYCRITRESUME, 0)) != 0) {
|
|---|
| 60 | dprintf("couldn't register for power event notificatins (rc = %d)\n", rc);
|
|---|
| 61 | return;
|
|---|
| 62 | }
|
|---|
| 63 | }
|
|---|
| 64 |
|
|---|
| 65 | /******************************************************************************
|
|---|
| 66 | * APM event handler
|
|---|
| 67 | */
|
|---|
| 68 | USHORT _far _cdecl apm_event(APMEVENT _far *evt)
|
|---|
| 69 | {
|
|---|
| 70 | USHORT msg = (USHORT) evt->ulParm1;
|
|---|
| 71 |
|
|---|
| 72 | dprintf("received APM event: 0x%lx/0x%lx\n");
|
|---|
| 73 |
|
|---|
| 74 | switch (msg) {
|
|---|
| 75 |
|
|---|
| 76 | case APM_SETPWRSTATE:
|
|---|
| 77 | if (evt->ulParm2 >> 16 != APM_PWRSTATEREADY) {
|
|---|
| 78 | /* we're suspending */
|
|---|
| 79 | apm_suspend();
|
|---|
| 80 | }
|
|---|
| 81 | break;
|
|---|
| 82 |
|
|---|
| 83 | case APM_NORMRESUMEEVENT:
|
|---|
| 84 | case APM_CRITRESUMEEVENT:
|
|---|
| 85 | /* we're resuming */
|
|---|
| 86 | apm_resume();
|
|---|
| 87 | break;
|
|---|
| 88 |
|
|---|
| 89 | default:
|
|---|
| 90 | dprintf("unknown APM event; ignoring...\n");
|
|---|
| 91 | break;
|
|---|
| 92 | }
|
|---|
| 93 |
|
|---|
| 94 | return(0);
|
|---|
| 95 | }
|
|---|
| 96 |
|
|---|
| 97 | /******************************************************************************
|
|---|
| 98 | * APM suspend handler. In a nutshell, it'll turn of interrupts and flush all
|
|---|
| 99 | * write caches.
|
|---|
| 100 | */
|
|---|
| 101 | void apm_suspend(void)
|
|---|
| 102 | {
|
|---|
| 103 | int a;
|
|---|
| 104 | int p;
|
|---|
| 105 | int d;
|
|---|
| 106 |
|
|---|
| 107 | dprintf("apm_suspend()\n");
|
|---|
| 108 |
|
|---|
| 109 | /* restart all ports with interrupts disabled */
|
|---|
| 110 | for (a = 0; a < ad_info_cnt; a++) {
|
|---|
| 111 | AD_INFO *ai = ad_infos + a;
|
|---|
| 112 |
|
|---|
| 113 | lock_adapter(ai);
|
|---|
| 114 | for (p = 0; p <= ai->port_max; p++) {
|
|---|
| 115 | /* wait until all active commands have completed on this port */
|
|---|
| 116 | while (ahci_port_busy(ai, p)) {
|
|---|
| 117 | msleep(250);
|
|---|
| 118 | }
|
|---|
| 119 |
|
|---|
| 120 | /* restart port with interrupts disabled */
|
|---|
| 121 | ahci_stop_port(ai, p);
|
|---|
| 122 | ahci_start_port(ai, p, 0);
|
|---|
| 123 |
|
|---|
| 124 | /* flush cache on all attached devices */
|
|---|
| 125 | for (d = 0; d <= ai->ports[p].dev_max; d++) {
|
|---|
| 126 | if (ai->ports[p].devs[d].present) {
|
|---|
| 127 | ahci_flush_cache(ai, p, d);
|
|---|
| 128 | }
|
|---|
| 129 | }
|
|---|
| 130 | }
|
|---|
| 131 | }
|
|---|
| 132 |
|
|---|
| 133 | /* reset init_complete so that we can process IORBs without interrupts */
|
|---|
| 134 | init_complete = 0;
|
|---|
| 135 |
|
|---|
| 136 | /* restore BIOS configuration for each adapter and release the adapter */
|
|---|
| 137 | for (a = 0; a < ad_info_cnt; a++) {
|
|---|
| 138 | ahci_restore_bios_config(ad_infos + a);
|
|---|
| 139 | unlock_adapter(ad_infos + a);
|
|---|
| 140 | }
|
|---|
| 141 |
|
|---|
| 142 | dprintf("apm_suspend() finished\n");
|
|---|
| 143 | }
|
|---|
| 144 |
|
|---|
| 145 | /******************************************************************************
|
|---|
| 146 | * APM resume handler. All ports are restarted with interrupts enabled using
|
|---|
| 147 | * the same function as the IOCM_COMPLETE_INIT handler does.
|
|---|
| 148 | */
|
|---|
| 149 | void apm_resume(void)
|
|---|
| 150 | {
|
|---|
| 151 | int a;
|
|---|
| 152 |
|
|---|
| 153 | dprintf("apm_resume()\n");
|
|---|
| 154 |
|
|---|
| 155 | for (a = 0; a < ad_info_cnt; a++) {
|
|---|
| 156 | AD_INFO *ai = ad_infos + a;
|
|---|
| 157 |
|
|---|
| 158 | /* Complete initialization of this adapter; this will restart the ports
|
|---|
| 159 | * with interrupts enabled and take care of whatever else needs to be
|
|---|
| 160 | * done to get the adapter and its ports up and running.
|
|---|
| 161 | */
|
|---|
| 162 | lock_adapter(ai);
|
|---|
| 163 | ahci_complete_init(ai);
|
|---|
| 164 | }
|
|---|
| 165 |
|
|---|
| 166 | /* tell the driver we're again fully operational */
|
|---|
| 167 | init_complete = 1;
|
|---|
| 168 |
|
|---|
| 169 | /* unlock all adapters now that we have set the init_complete flag */
|
|---|
| 170 | for (a = 0; a < ad_info_cnt; a++) {
|
|---|
| 171 | unlock_adapter(ad_infos + a);
|
|---|
| 172 | }
|
|---|
| 173 |
|
|---|
| 174 | dprintf("apm_resume() finished\n");
|
|---|
| 175 | }
|
|---|