/***************************************************************************
 *   Copyright (C) 2008 Timo Juhani Lindfors <timo.lindfors@iki.fi>        *
 *   Copyright (C) 2005 by Alejandro Vidal Mata & Javier Vidal Mata        *
 *                                                                         *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 *                                                                         *
 *   The full GNU General Public License is included in this distribution  *
 *   in the file called COPYING.                                           *
 *                                                                         *
 *   This project haven't a warranty. The project developers haven't       *
 *   responsible for whatever happens from using.                          *
 *                                                                         *
 *   Author (userland port):                                               *
 *    Timo Juhani Lindfors <timo.lindfors@iki.fi>                          *
 *                                                                         *
 *   Based on kernel space version by:                                     *
 *    Martin Vecera <ja@marvec.org>                                        *
 *                                                                         *
 *   Based on fsam7440 by:                                                 *
 *    Alejandro Vidal Mata <alex14@gmail.com>                              *
 *    Javier Vidal Mata <javitu@gmail.com>                                 *
 *                                                                         *
 *   Based on:                                                             *
 *    pbe5.c by Pedro Ramalhais <pmr09313@students.fct.unl.pt>             *
 ***************************************************************************/

#include <stdlib.h>
#include <assert.h>
#include <sys/io.h>

/*
 * These values were obtained from disassembling and debugging the PM.exe program
 * installed in the Fujitsu-Siemens AMILO A1655G
 */
#define KBD_STATE_PORT  	0x64
#define KBD_COMMAND_PORT        0x64
#define KBD_DATA_PORT           0x60

#define WIFI_COMMAND            0xC5
#define WIFI_ON                 0x25
#define WIFI_OFF                0x45

static void amiloa1655g_set_radio(int enable) {
  unsigned char val = 0;

  if (enable) {
      do { val = inb(KBD_STATE_PORT); } while ((val & 2) == 2);
      outb(WIFI_COMMAND, KBD_COMMAND_PORT);
      do { val = inb(KBD_STATE_PORT); } while ((val & 2) == 2);
      outb(WIFI_ON, KBD_DATA_PORT);
  } else {
      do { val = inb(KBD_STATE_PORT); } while ((val & 2) == 2);
      outb(WIFI_COMMAND, KBD_COMMAND_PORT);
      do { val = inb(KBD_STATE_PORT); } while ((val & 2) == 2);
      outb(WIFI_OFF, KBD_DATA_PORT);
  }
}

int main(int argc, char *argv[]) {
    int ret;
    
    assert(argc == 2);
    ret = ioperm(KBD_STATE_PORT, 1, 1);
    assert(ret == 0);
    ret = ioperm(KBD_DATA_PORT, 1, 1);
    assert(ret == 0);

    amiloa1655g_set_radio(atoi(argv[1]));
    return 0;
}
