/*
 *  Copyright (C) 2002 Intersil Americas Inc.
 *  Copyright (C) 2003 Herbert Valerio Riedel <hvr@gnu.org>
 *  Copyright (C) 2005 Jean-Baptiste Note <jean-baptiste.note@m4x.org>
 *
 *  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
 *
 *  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
 *
 */

#include <linux/version.h>
#include <linux/module.h>
#include <linux/pci.h>
#include <linux/delay.h>
#include <linux/init.h>		/* For __init, __exit */
#include <linux/netdevice.h>

#include "islpci_dev.h"
#include "isl_sm.h"
#include "islsm_log.h"

MODULE_AUTHOR
    ("[Intersil] R.Bastings and W.Termorshuizen, The prism54.org Development Team <prism54-devel@prism54.org>, Jean-Baptiste Note <jean-baptiste.note@m4x.org>");
MODULE_DESCRIPTION("Prism54 softmac 802.11 Wireless LAN adapter");
MODULE_LICENSE("GPL");

static int init_pcitm = 0;
module_param(init_pcitm, int, 0);

/* In this order: vendor, device, subvendor, subdevice, class, class_mask,
 * driver_data
 * If you have an update for this please contact prism54-devel@prism54.org
 * The latest list can be found at http://prism54.org/supported_cards.php */

static const struct pci_device_id islpci_id_tbl[] = {
	/* Intersil PRISM Duette/Prism GT Wireless LAN adapter */
	{
	 0x1260, 0x3890,
	 PCI_ANY_ID, PCI_ANY_ID,
	 0, 0, 0},

	/* 3COM 3CRWE154G72 Wireless LAN adapter */
	{
	 0x10b7, 0x6001,
	 PCI_ANY_ID, PCI_ANY_ID,
	 0, 0, 0},

	/* Intersil PRISM Indigo Wireless LAN adapter */
	{
	 0x1260, 0x3877,
	 PCI_ANY_ID, PCI_ANY_ID,
	 0, 0, 0},

	/* Intersil PRISM Javelin/Xbow Wireless LAN adapter */
	{
	 0x1260, 0x3886,
	 PCI_ANY_ID, PCI_ANY_ID,
	 0, 0, 0},

	/* End of list */
	{0, 0, 0, 0, 0, 0, 0}
};

/* register the device with the Hotplug facilities of the kernel */
MODULE_DEVICE_TABLE(pci, islpci_id_tbl);

static __devinit int prism54_probe(struct pci_dev *, const struct pci_device_id *);
static __devexit void prism54_remove(struct pci_dev *);
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,14)
static int prism54_suspend(struct pci_dev *, u32 state);
#else
static int prism54_suspend(struct pci_dev *, pm_message_t state);
#endif
static int prism54_resume(struct pci_dev *);

static struct pci_driver islpci_driver = {
	.name = DRV_NAME,
	.id_table = islpci_id_tbl,
	.probe = prism54_probe,
	.remove = prism54_remove,
	.suspend = prism54_suspend,
	.resume = prism54_resume,
	/* .enable_wake ; we don't support this yet */
};

/******************************************************************************
    Module initialization functions
******************************************************************************/

static __devinit int
prism54_probe(struct pci_dev *pdev, const struct pci_device_id *id)
{
	struct net_device *ndev;
	u8 latency_tmr;
	u32 mem_addr;
	islpci_private *priv;
	struct islsm *islsm;
	int err;

	/* Enable the pci device */
	err = pci_enable_device(pdev);
	if (err) {
		printk(KERN_ERR "%s: pci_enable_device() failed.\n", DRV_NAME);
		return -ENODEV;
	}

	/* check whether the latency timer is set correctly */
	pci_read_config_byte(pdev, PCI_LATENCY_TIMER, &latency_tmr);
	islog(L_DEBUG, "latency timer: %x\n", latency_tmr);

	if (latency_tmr < PCIDEVICE_LATENCY_TIMER_MIN) {
		/* set the latency timer */
		pci_write_config_byte(pdev, PCI_LATENCY_TIMER,
				      PCIDEVICE_LATENCY_TIMER_VAL);
	}

	/* enable PCI DMA */
	err = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
	if (err) {
		printk(KERN_ERR "%s: 32-bit PCI DMA not supported\n", DRV_NAME);
		goto do_pci_disable_device;
	}

	/* 0x40 is the programmable timer to configure the response timeout (TRDY_TIMEOUT)
	 * 0x41 is the programmable timer to configure the retry timeout (RETRY_TIMEOUT)
	 *      The RETRY_TIMEOUT is used to set the number of retries that the core, as a
	 *      Master, will perform before abandoning a cycle. The default value for
	 *      RETRY_TIMEOUT is 0x80, which far exceeds the PCI 2.1 requirement for new
	 *      devices. A write of zero to the RETRY_TIMEOUT register disables this
	 *      function to allow use with any non-compliant legacy devices that may
	 *      execute more retries.
	 *
	 *      Writing zero to both these two registers will disable both timeouts and
	 *      *can* solve problems caused by devices that are slow to respond.
	 *      Make this configurable - MSW
	 */
	if (init_pcitm >= 0) {
		pci_write_config_byte(pdev, PCI_TRDY_TIMEOUT, (u8) init_pcitm);
		pci_write_config_byte(pdev, PCI_RETRY_TIMEOUT, (u8) init_pcitm);
	} else {
		printk(KERN_INFO "PCI TRDY/RETRY unchanged\n");
	}

	/* request the pci device I/O regions */
	err = pci_request_regions(pdev, DRV_NAME);
	if (err) {
		printk(KERN_ERR
		       "%s: pci_request_regions failure (rc=%d)\n",
		       DRV_NAME, err);
		goto do_pci_disable_device;
	}

	/* check if the memory window is indeed set */
	err = pci_read_config_dword(pdev, PCI_BASE_ADDRESS_0, &mem_addr);
	if (err || !mem_addr) {
		printk(KERN_ERR
		       "%s: PCI device memory region not configured; fix your BIOS or CardBus bridge/drivers\n",
		       DRV_NAME);
		goto do_pci_disable_device;
	}

	/* enable PCI bus-mastering */
//      DEBUG(SHOW_TRACING, "%s: pci_set_master(pdev)\n", DRV_NAME);
	pci_set_master(pdev);

	/* enable MWI */
	err = pci_set_mwi(pdev);
	if (err) {
		printk(KERN_ERR
		       "%s: PCI set_mwi failed\n",
		       DRV_NAME);
		goto do_pci_disable_device;
	}

	/* setup the network device interface and its structure */
	ndev = islpci_alloc(pdev);

	if (!ndev) {
		/* error configuring the driver as a network device */
		printk(KERN_ERR "%s: could not configure network device\n",
		       DRV_NAME);
		goto do_pci_release_regions;
	}

	err = register_islsm(ndev);
	if (err) {
		printk(KERN_ERR "%s: could not register islsm device\n",
		       DRV_NAME);
		goto do_free_islsm;
	}

	islsm = ISLSM_OF_NETDEV(ndev);
	priv = ISLPCI_OF_NETDEV(ndev);

	islpci_set_state(priv, PRV_STATE_PREBOOT);	/* we are attempting to boot */

	/* card is in unknown state yet, might have some interrupts pending */
	isl38xx_disable_interrupts(priv->device_base);

	/* request for the interrupt before uploading the firmware */
	err = request_irq(pdev->irq, &islpci_interrupt,
			  SA_SHIRQ, ndev->name, priv);

	if (err) {
		/* error, could not hook the handler to the irq */
		printk(KERN_ERR "%s: could not install IRQ handler\n",
		       ndev->name);
		goto do_unregister_islsm;
	}

	/* firmware upload is triggered in islpci_open */
	/* meanwhile, reboot the device,
	   so that we go into a known state (rom) */
	/* this will raise CTS flag */
	islpci_romboot(priv);

	/* we should now be in a known state */

	return 0;

 do_unregister_islsm:
	unregister_islsm(ndev);
 do_free_islsm:
	islpci_free(ndev);
 do_pci_release_regions:
	pci_release_regions(pdev);
 do_pci_disable_device:
	pci_disable_device(pdev);
	return err;
}

/* set by cleanup_module */
static volatile int __in_cleanup_module = 0;

/* this one removes one(!!) instance only */
static __devexit void
prism54_remove(struct pci_dev *pdev)
{
	struct net_device *ndev = pci_get_drvdata(pdev);
	islpci_private *priv = ISLPCI_OF_NETDEV(ndev);

	if (!__in_cleanup_module) {
		printk(KERN_DEBUG "%s: hot unplug detected\n", ndev->name);
		islpci_set_state(priv, PRV_STATE_OFF);
	}

	printk(KERN_DEBUG "%s: removing device\n", ndev->name);

	/* free the interrupt request */

	if (islpci_get_state(priv) != PRV_STATE_OFF) {
		islpci_romboot(priv);

		isl38xx_disable_interrupts(priv->device_base);
		islpci_set_state(priv, PRV_STATE_OFF);
		/* This bellow causes a lockup at rmmod time. It might be
		 * because some interrupts still linger after rmmod time,
		 * see bug #17 */
		/* pci_set_power_state(pdev, 3); *//* try to power-off */
	}
	tasklet_disable(&priv->rx_task);

	/* see above allocation */
	free_irq(pdev->irq, priv);
	unregister_islsm(ndev);
	islpci_free(ndev);
	pci_release_regions(pdev);
	pci_disable_device(pdev);
	/* Documentation/power/pci.txt mentions a reference implementation which
	 * makes the call to set_power_state after disable_device, as the last
	 * operation of the .suspend callback example. Try here: */
	pci_set_power_state(pdev, PCI_D3cold);
}

int
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,14)
prism54_suspend(struct pci_dev *pdev, u32 state)
#else
prism54_suspend(struct pci_dev *pdev, pm_message_t state)
#endif
{
	// will have to be modified for madwifi i guess
	struct net_device *ndev = pci_get_drvdata(pdev);
	islpci_private *priv = ISLPCI_OF_NETDEV(ndev);
	BUG_ON(!priv);

	printk(KERN_NOTICE "%s: got suspend request (state %d)\n",
	       ndev->name,
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,14)
	       state
#else
	       state.event
#endif
		);

	pci_save_state(pdev);

	/* tell the device not to trigger interrupts for now... */
	isl38xx_disable_interrupts(priv->device_base);

	/* from now on assume the hardware was already powered down
	   and don't touch it anymore */
	islpci_set_state(priv, PRV_STATE_OFF);

	netif_stop_queue(ndev);
	netif_device_detach(ndev);

	return 0;
}

int
prism54_resume(struct pci_dev *pdev)
{
	int err = 0;
	struct net_device *ndev = pci_get_drvdata(pdev);
	islpci_private *priv = ISLPCI_OF_NETDEV(ndev);
	BUG_ON(!priv);

	err = pci_enable_device(pdev);
	if (err) {
		/* FIXME : what to do exactly in this case ? */
		goto out;
	}

	printk(KERN_NOTICE "%s: got resume request\n", ndev->name);

	pci_restore_state(pdev);

	/* alright let's go into the PREBOOT state */
	islpci_reset(priv, 1);

	netif_device_attach(ndev);
	netif_start_queue(ndev);

 out:
	return 0;
}

static int __init
prism54_module_init(void)
{
	printk(KERN_INFO "Loaded %s driver, version %s\n",
	       DRV_NAME, DRV_VERSION);

	return pci_module_init(&islpci_driver);
}

/* by the time prism54_module_exit() terminates, as a postcondition
 * all instances will have been destroyed by calls to
 * prism54_remove() */
static void __exit
prism54_module_exit(void)
{
	__in_cleanup_module = 1;

	pci_unregister_driver(&islpci_driver);

	printk(KERN_INFO "Unloaded %s driver\n", DRV_NAME);

	__in_cleanup_module = 0;
}

/* register entry points */
module_init(prism54_module_init);
module_exit(prism54_module_exit);
/* EOF */
