Home | History | Annotate | Download | only in Kernel
      1 /** @addtogroup MCD_MCDIMPL_DAEMON_KERNEL
      2  * @{
      3  * @file
      4  *
      5  * Kernel Module Interface.
      6  *
      7  * <!-- Copyright Giesecke & Devrient GmbH 2009 - 2012 -->
      8  *
      9  * Redistribution and use in source and binary forms, with or without
     10  * modification, are permitted provided that the following conditions
     11  * are met:
     12  * 1. Redistributions of source code must retain the above copyright
     13  *    notice, this list of conditions and the following disclaimer.
     14  * 2. Redistributions in binary form must reproduce the above copyright
     15  *    notice, this list of conditions and the following disclaimer in the
     16  *    documentation and/or other materials provided with the distribution.
     17  * 3. The name of the author may not be used to endorse or promote
     18  *    products derived from this software without specific prior
     19  *    written permission.
     20  *
     21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
     22  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     23  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
     25  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
     27  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
     29  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
     30  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
     31  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     32  */
     33 #include <cstdlib>
     34 
     35 #include <fcntl.h>
     36 #include <sys/mman.h>
     37 #include <errno.h>
     38 
     39 #include "CKMod.h"
     40 
     41 #include "log.h"
     42 
     43 #define INVALID_FILE_DESCRIPTOR          ((int)(-1))
     44 
     45 //------------------------------------------------------------------------------
     46 CKMod::CKMod(void)
     47 {
     48     fdKMod = INVALID_FILE_DESCRIPTOR;
     49 }
     50 
     51 
     52 //------------------------------------------------------------------------------
     53 CKMod::~CKMod(void)
     54 {
     55     close();
     56 }
     57 
     58 
     59 //------------------------------------------------------------------------------
     60 bool CKMod::isOpen(void)
     61 {
     62     return (INVALID_FILE_DESCRIPTOR == fdKMod) ? false : true;
     63 }
     64 
     65 
     66 //------------------------------------------------------------------------------
     67 mcResult_t CKMod::open(const char *deviceName)
     68 {
     69     if (isOpen()) {
     70         LOG_W("already open");
     71         return MC_DRV_ERR_DEVICE_ALREADY_OPEN;
     72     }
     73 
     74     LOG_I(" Opening kernel module at %s.", deviceName);
     75 
     76     // open return -1 on error, "errno" is set with details
     77     int openRet = ::open(deviceName, O_RDWR);
     78     if (openRet == -1) {
     79         LOG_ERRNO("open");
     80         return MAKE_MC_DRV_KMOD_WITH_ERRNO(errno);
     81     }
     82 
     83     fdKMod = openRet;
     84     return MC_DRV_OK;
     85 }
     86 
     87 
     88 //------------------------------------------------------------------------------
     89 void CKMod::close(
     90     void
     91 )
     92 {
     93     if (isOpen()) {
     94         if (::close(fdKMod) != 0) {
     95             LOG_ERRNO("close");
     96         } else {
     97             fdKMod = INVALID_FILE_DESCRIPTOR;
     98         }
     99     } else {
    100         LOG_W(" Kernel module device not open");
    101     }
    102 }
    103 
    104 /** @} */
    105