iPXE
apm.c
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2013 Marin Hannache <ipxe@mareo.fr>.
00003  *
00004  * This program is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU General Public License as
00006  * published by the Free Software Foundation; either version 2 of the
00007  * License, or any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful, but
00010  * WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  * General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software
00016  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
00017  * 02110-1301, USA.
00018  *
00019  * You can also choose to distribute this program under the terms of
00020  * the Unmodified Binary Distribution Licence (as given in the file
00021  * COPYING.UBDL), provided that you have satisfied its requirements.
00022  */
00023 
00024 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
00025 
00026 /**
00027  * @file
00028  *
00029  * Advanced Power Management
00030  *
00031  */
00032 
00033 #include <errno.h>
00034 #include <realmode.h>
00035 #include <ipxe/apm.h>
00036 
00037 /**
00038  * Power off the computer using APM
00039  *
00040  * @ret rc              Return status code
00041  */
00042 int apm_poweroff ( void ) {
00043         uint16_t apm_version;
00044         uint16_t apm_signature;
00045         uint16_t apm_flags;
00046         uint16_t carry;
00047 
00048         /* APM check */
00049         __asm__ __volatile__ ( REAL_CODE ( "int $0x15\n\t"
00050                                            "adc %%edx,0\n\t" )
00051                                : "=a" ( apm_version ), "=b" ( apm_signature ),
00052                                  "=c" ( apm_flags ), "=d" ( carry )
00053                                : "a" ( 0x5300 ), "b" ( 0x0000 ),
00054                                  "d" ( 0x0000 ) );
00055         if ( carry ) {
00056                 DBG ( "APM not present\n" );
00057                 return -ENOTSUP;
00058         }
00059         if ( apm_signature != 0x504d ) { /* signature 'PM' */
00060                 DBG ( "APM not present\n" );
00061                 return -ENOTSUP;
00062         }
00063         if ( apm_version < 0x0101 ) { /* Need version 1.1+ */
00064                 DBG ( "APM 1.1+ not supported\n" );
00065                 return -ENOTSUP;
00066         }
00067         if ( ( apm_flags & 0x8 ) == 0x8 ) {
00068                 DBG ( "APM power management disabled\n" );
00069                 return -EPERM;
00070         }
00071         DBG2 ( "APM check completed\n" );
00072 
00073         /* APM initialisation */
00074         __asm__ __volatile__ ( REAL_CODE ( "int $0x15\n\t"
00075                                            "adc %%edx,0\n\t" )
00076                                : "=d" ( carry )
00077                                : "a" ( 0x5301 ), "b" ( 0x0000 ),
00078                                  "d" ( 0x0000 ) );
00079         if ( carry ) {
00080                 DBG ( "APM initialisation failed\n" );
00081                 return -EIO;
00082         }
00083         DBG2 ( "APM initialisation completed\n" );
00084 
00085         /* Set APM driver version */
00086         __asm__ __volatile__ ( REAL_CODE ( "int $0x15\n\t"
00087                                            "adc %%edx,0\n\t" )
00088                                : "=d" ( carry )
00089                                : "a" ( 0x530e ), "b" ( 0x0000 ),
00090                                  "c" ( 0x0101 ), "d" ( 0x0000 ) );
00091         if ( carry ) {
00092                 DBG ( "APM setting driver version failed\n" );
00093                 return -EIO;
00094         }
00095         DBG2 ( "APM driver version set\n" );
00096 
00097         /* Setting power state to off */
00098         __asm__ __volatile__ ( REAL_CODE ( "int $0x15\n\t"
00099                                            "adc %%edx,0\n\t" )
00100                                : "=d" ( carry )
00101                                : "a" ( 0x5307 ), "b" ( 0x0001 ),
00102                                  "c" ( 0x0003 ), "d" ( 0x0000) );
00103         if ( carry ) {
00104                 DBG ( "APM setting power state failed\n" );
00105                 return -ENOTTY;
00106         }
00107 
00108         /* Should never happen */
00109         return -ECANCELED;
00110 }