hostap_pci.c 11.0 KB
Newer Older
J
Jouni Malinen 已提交
1 2 3 4 5 6 7 8 9 10 11
#define PRISM2_PCI

/* Host AP driver's support for Intersil Prism2.5 PCI cards is based on
 * driver patches from Reyk Floeter <reyk@vantronix.net> and
 * Andy Warner <andyw@pobox.com> */

#include <linux/module.h>
#include <linux/init.h>
#include <linux/if.h>
#include <linux/skbuff.h>
#include <linux/netdevice.h>
12
#include <linux/slab.h>
J
Jouni Malinen 已提交
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
#include <linux/workqueue.h>
#include <linux/wireless.h>
#include <net/iw_handler.h>

#include <linux/ioport.h>
#include <linux/pci.h>
#include <asm/io.h>

#include "hostap_wlan.h"


static char *dev_info = "hostap_pci";


MODULE_AUTHOR("Jouni Malinen");
MODULE_DESCRIPTION("Support for Intersil Prism2.5-based 802.11 wireless LAN "
		   "PCI cards.");
MODULE_SUPPORTED_DEVICE("Intersil Prism2.5-based WLAN PCI cards");
MODULE_LICENSE("GPL");


34 35 36 37 38 39
/* struct local_info::hw_priv */
struct hostap_pci_priv {
	void __iomem *mem_start;
};


J
Jouni Malinen 已提交
40 41 42
/* FIX: do we need mb/wmb/rmb with memory operations? */


43
static DEFINE_PCI_DEVICE_TABLE(prism2_pci_id_table) = {
J
Jouni Malinen 已提交
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
	/* Intersil Prism3 ISL3872 11Mb/s WLAN Controller */
	{ 0x1260, 0x3872, PCI_ANY_ID, PCI_ANY_ID },
	/* Intersil Prism2.5 ISL3874 11Mb/s WLAN Controller */
	{ 0x1260, 0x3873, PCI_ANY_ID, PCI_ANY_ID },
	/* Samsung MagicLAN SWL-2210P */
	{ 0x167d, 0xa000, PCI_ANY_ID, PCI_ANY_ID },
	{ 0 }
};


#ifdef PRISM2_IO_DEBUG

static inline void hfa384x_outb_debug(struct net_device *dev, int a, u8 v)
{
	struct hostap_interface *iface;
59
	struct hostap_pci_priv *hw_priv;
J
Jouni Malinen 已提交
60 61 62 63 64
	local_info_t *local;
	unsigned long flags;

	iface = netdev_priv(dev);
	local = iface->local;
65
	hw_priv = local->hw_priv;
J
Jouni Malinen 已提交
66 67 68

	spin_lock_irqsave(&local->lock, flags);
	prism2_io_debug_add(dev, PRISM2_IO_DEBUG_CMD_OUTB, a, v);
69
	writeb(v, hw_priv->mem_start + a);
J
Jouni Malinen 已提交
70 71 72 73 74 75
	spin_unlock_irqrestore(&local->lock, flags);
}

static inline u8 hfa384x_inb_debug(struct net_device *dev, int a)
{
	struct hostap_interface *iface;
76
	struct hostap_pci_priv *hw_priv;
J
Jouni Malinen 已提交
77 78 79 80 81 82
	local_info_t *local;
	unsigned long flags;
	u8 v;

	iface = netdev_priv(dev);
	local = iface->local;
83
	hw_priv = local->hw_priv;
J
Jouni Malinen 已提交
84 85

	spin_lock_irqsave(&local->lock, flags);
86
	v = readb(hw_priv->mem_start + a);
J
Jouni Malinen 已提交
87 88 89 90 91 92 93 94
	prism2_io_debug_add(dev, PRISM2_IO_DEBUG_CMD_INB, a, v);
	spin_unlock_irqrestore(&local->lock, flags);
	return v;
}

static inline void hfa384x_outw_debug(struct net_device *dev, int a, u16 v)
{
	struct hostap_interface *iface;
95
	struct hostap_pci_priv *hw_priv;
J
Jouni Malinen 已提交
96 97 98 99 100
	local_info_t *local;
	unsigned long flags;

	iface = netdev_priv(dev);
	local = iface->local;
101
	hw_priv = local->hw_priv;
J
Jouni Malinen 已提交
102 103 104

	spin_lock_irqsave(&local->lock, flags);
	prism2_io_debug_add(dev, PRISM2_IO_DEBUG_CMD_OUTW, a, v);
105
	writew(v, hw_priv->mem_start + a);
J
Jouni Malinen 已提交
106 107 108 109 110 111
	spin_unlock_irqrestore(&local->lock, flags);
}

static inline u16 hfa384x_inw_debug(struct net_device *dev, int a)
{
	struct hostap_interface *iface;
112
	struct hostap_pci_priv *hw_priv;
J
Jouni Malinen 已提交
113 114 115 116 117 118
	local_info_t *local;
	unsigned long flags;
	u16 v;

	iface = netdev_priv(dev);
	local = iface->local;
119
	hw_priv = local->hw_priv;
J
Jouni Malinen 已提交
120 121

	spin_lock_irqsave(&local->lock, flags);
122
	v = readw(hw_priv->mem_start + a);
J
Jouni Malinen 已提交
123 124 125 126 127 128 129 130 131
	prism2_io_debug_add(dev, PRISM2_IO_DEBUG_CMD_INW, a, v);
	spin_unlock_irqrestore(&local->lock, flags);
	return v;
}

#define HFA384X_OUTB(v,a) hfa384x_outb_debug(dev, (a), (v))
#define HFA384X_INB(a) hfa384x_inb_debug(dev, (a))
#define HFA384X_OUTW(v,a) hfa384x_outw_debug(dev, (a), (v))
#define HFA384X_INW(a) hfa384x_inw_debug(dev, (a))
A
Al Viro 已提交
132 133
#define HFA384X_OUTW_DATA(v,a) hfa384x_outw_debug(dev, (a), le16_to_cpu((v)))
#define HFA384X_INW_DATA(a) cpu_to_le16(hfa384x_inw_debug(dev, (a)))
J
Jouni Malinen 已提交
134 135 136 137 138 139

#else /* PRISM2_IO_DEBUG */

static inline void hfa384x_outb(struct net_device *dev, int a, u8 v)
{
	struct hostap_interface *iface;
140
	struct hostap_pci_priv *hw_priv;
J
Jouni Malinen 已提交
141
	iface = netdev_priv(dev);
142 143
	hw_priv = iface->local->hw_priv;
	writeb(v, hw_priv->mem_start + a);
J
Jouni Malinen 已提交
144 145 146 147 148
}

static inline u8 hfa384x_inb(struct net_device *dev, int a)
{
	struct hostap_interface *iface;
149
	struct hostap_pci_priv *hw_priv;
J
Jouni Malinen 已提交
150
	iface = netdev_priv(dev);
151 152
	hw_priv = iface->local->hw_priv;
	return readb(hw_priv->mem_start + a);
J
Jouni Malinen 已提交
153 154 155 156 157
}

static inline void hfa384x_outw(struct net_device *dev, int a, u16 v)
{
	struct hostap_interface *iface;
158
	struct hostap_pci_priv *hw_priv;
J
Jouni Malinen 已提交
159
	iface = netdev_priv(dev);
160 161
	hw_priv = iface->local->hw_priv;
	writew(v, hw_priv->mem_start + a);
J
Jouni Malinen 已提交
162 163 164 165 166
}

static inline u16 hfa384x_inw(struct net_device *dev, int a)
{
	struct hostap_interface *iface;
167
	struct hostap_pci_priv *hw_priv;
J
Jouni Malinen 已提交
168
	iface = netdev_priv(dev);
169 170
	hw_priv = iface->local->hw_priv;
	return readw(hw_priv->mem_start + a);
J
Jouni Malinen 已提交
171 172 173 174 175 176
}

#define HFA384X_OUTB(v,a) hfa384x_outb(dev, (a), (v))
#define HFA384X_INB(a) hfa384x_inb(dev, (a))
#define HFA384X_OUTW(v,a) hfa384x_outw(dev, (a), (v))
#define HFA384X_INW(a) hfa384x_inw(dev, (a))
A
Al Viro 已提交
177 178
#define HFA384X_OUTW_DATA(v,a) hfa384x_outw(dev, (a), le16_to_cpu((v)))
#define HFA384X_INW_DATA(a) cpu_to_le16(hfa384x_inw(dev, (a)))
J
Jouni Malinen 已提交
179 180 181 182 183 184 185 186

#endif /* PRISM2_IO_DEBUG */


static int hfa384x_from_bap(struct net_device *dev, u16 bap, void *buf,
			    int len)
{
	u16 d_off;
A
Al Viro 已提交
187
	__le16 *pos;
J
Jouni Malinen 已提交
188 189

	d_off = (bap == 1) ? HFA384X_DATA1_OFF : HFA384X_DATA0_OFF;
A
Al Viro 已提交
190
	pos = (__le16 *) buf;
J
Jouni Malinen 已提交
191 192 193 194 195 196 197 198 199 200 201 202 203 204

	for ( ; len > 1; len -= 2)
		*pos++ = HFA384X_INW_DATA(d_off);

	if (len & 1)
		*((char *) pos) = HFA384X_INB(d_off);

	return 0;
}


static int hfa384x_to_bap(struct net_device *dev, u16 bap, void *buf, int len)
{
	u16 d_off;
A
Al Viro 已提交
205
	__le16 *pos;
J
Jouni Malinen 已提交
206 207

	d_off = (bap == 1) ? HFA384X_DATA1_OFF : HFA384X_DATA0_OFF;
A
Al Viro 已提交
208
	pos = (__le16 *) buf;
J
Jouni Malinen 已提交
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299

	for ( ; len > 1; len -= 2)
		HFA384X_OUTW_DATA(*pos++, d_off);

	if (len & 1)
		HFA384X_OUTB(*((char *) pos), d_off);

	return 0;
}


/* FIX: This might change at some point.. */
#include "hostap_hw.c"

static void prism2_pci_cor_sreset(local_info_t *local)
{
	struct net_device *dev = local->dev;
	u16 reg;

	reg = HFA384X_INB(HFA384X_PCICOR_OFF);
	printk(KERN_DEBUG "%s: Original COR value: 0x%0x\n", dev->name, reg);

	/* linux-wlan-ng uses extremely long hold and settle times for
	 * COR sreset. A comment in the driver code mentions that the long
	 * delays appear to be necessary. However, at least IBM 22P6901 seems
	 * to work fine with shorter delays.
	 *
	 * Longer delays can be configured by uncommenting following line: */
/* #define PRISM2_PCI_USE_LONG_DELAYS */

#ifdef PRISM2_PCI_USE_LONG_DELAYS
	int i;

	HFA384X_OUTW(reg | 0x0080, HFA384X_PCICOR_OFF);
	mdelay(250);

	HFA384X_OUTW(reg & ~0x0080, HFA384X_PCICOR_OFF);
	mdelay(500);

	/* Wait for f/w to complete initialization (CMD:BUSY == 0) */
	i = 2000000 / 10;
	while ((HFA384X_INW(HFA384X_CMD_OFF) & HFA384X_CMD_BUSY) && --i)
		udelay(10);

#else /* PRISM2_PCI_USE_LONG_DELAYS */

	HFA384X_OUTW(reg | 0x0080, HFA384X_PCICOR_OFF);
	mdelay(2);
	HFA384X_OUTW(reg & ~0x0080, HFA384X_PCICOR_OFF);
	mdelay(2);

#endif /* PRISM2_PCI_USE_LONG_DELAYS */

	if (HFA384X_INW(HFA384X_CMD_OFF) & HFA384X_CMD_BUSY) {
		printk(KERN_DEBUG "%s: COR sreset timeout\n", dev->name);
	}
}


static void prism2_pci_genesis_reset(local_info_t *local, int hcr)
{
	struct net_device *dev = local->dev;

	HFA384X_OUTW(0x00C5, HFA384X_PCICOR_OFF);
	mdelay(10);
	HFA384X_OUTW(hcr, HFA384X_PCIHCR_OFF);
	mdelay(10);
	HFA384X_OUTW(0x0045, HFA384X_PCICOR_OFF);
	mdelay(10);
}


static struct prism2_helper_functions prism2_pci_funcs =
{
	.card_present	= NULL,
	.cor_sreset	= prism2_pci_cor_sreset,
	.genesis_reset	= prism2_pci_genesis_reset,
	.hw_type	= HOSTAP_HW_PCI,
};


static int prism2_pci_probe(struct pci_dev *pdev,
			    const struct pci_device_id *id)
{
	unsigned long phymem;
	void __iomem *mem = NULL;
	local_info_t *local = NULL;
	struct net_device *dev = NULL;
	static int cards_found /* = 0 */;
	int irq_registered = 0;
	struct hostap_interface *iface;
300 301
	struct hostap_pci_priv *hw_priv;

302
	hw_priv = kzalloc(sizeof(*hw_priv), GFP_KERNEL);
303 304
	if (hw_priv == NULL)
		return -ENOMEM;
J
Jouni Malinen 已提交
305 306

	if (pci_enable_device(pdev))
307
		goto err_out_free;
J
Jouni Malinen 已提交
308 309 310 311 312 313 314 315

	phymem = pci_resource_start(pdev, 0);

	if (!request_mem_region(phymem, pci_resource_len(pdev, 0), "Prism2")) {
		printk(KERN_ERR "prism2: Cannot reserve PCI memory region\n");
		goto err_out_disable;
	}

316
	mem = pci_ioremap_bar(pdev, 0);
J
Jouni Malinen 已提交
317 318 319 320 321
	if (mem == NULL) {
		printk(KERN_ERR "prism2: Cannot remap PCI memory region\n") ;
		goto fail;
	}

D
Dave Hansen 已提交
322 323
	dev = prism2_init_local_data(&prism2_pci_funcs, cards_found,
				     &pdev->dev);
J
Jouni Malinen 已提交
324 325 326 327
	if (dev == NULL)
		goto fail;
	iface = netdev_priv(dev);
	local = iface->local;
328
	local->hw_priv = hw_priv;
J
Jouni Malinen 已提交
329 330 331
	cards_found++;

        dev->irq = pdev->irq;
332
        hw_priv->mem_start = mem;
333
	dev->base_addr = (unsigned long) mem;
J
Jouni Malinen 已提交
334 335 336 337 338

	prism2_pci_cor_sreset(local);

	pci_set_drvdata(pdev, dev);

339
	if (request_irq(dev->irq, prism2_interrupt, IRQF_SHARED, dev->name,
J
Jouni Malinen 已提交
340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368
			dev)) {
		printk(KERN_WARNING "%s: request_irq failed\n", dev->name);
		goto fail;
	} else
		irq_registered = 1;

	if (!local->pri_only && prism2_hw_config(dev, 1)) {
		printk(KERN_DEBUG "%s: hardware initialization failed\n",
		       dev_info);
		goto fail;
	}

	printk(KERN_INFO "%s: Intersil Prism2.5 PCI: "
	       "mem=0x%lx, irq=%d\n", dev->name, phymem, dev->irq);

	return hostap_hw_ready(dev);

 fail:
	if (irq_registered && dev)
		free_irq(dev->irq, dev);

	if (mem)
		iounmap(mem);

	release_mem_region(phymem, pci_resource_len(pdev, 0));

 err_out_disable:
	pci_disable_device(pdev);
	prism2_free_local_data(dev);
369 370

 err_out_free:
371
	kfree(hw_priv);
J
Jouni Malinen 已提交
372 373 374 375 376 377 378 379 380 381

	return -ENODEV;
}


static void prism2_pci_remove(struct pci_dev *pdev)
{
	struct net_device *dev;
	struct hostap_interface *iface;
	void __iomem *mem_start;
382
	struct hostap_pci_priv *hw_priv;
J
Jouni Malinen 已提交
383 384 385

	dev = pci_get_drvdata(pdev);
	iface = netdev_priv(dev);
386
	hw_priv = iface->local->hw_priv;
J
Jouni Malinen 已提交
387 388 389 390 391 392 393 394

	/* Reset the hardware, and ensure interrupts are disabled. */
	prism2_pci_cor_sreset(iface->local);
	hfa384x_disable_interrupts(dev);

	if (dev->irq)
		free_irq(dev->irq, dev);

395
	mem_start = hw_priv->mem_start;
J
Jouni Malinen 已提交
396
	prism2_free_local_data(dev);
397
	kfree(hw_priv);
J
Jouni Malinen 已提交
398 399 400 401 402 403 404 405 406 407

	iounmap(mem_start);

	release_mem_region(pci_resource_start(pdev, 0),
			   pci_resource_len(pdev, 0));
	pci_disable_device(pdev);
}


#ifdef CONFIG_PM
408
static int prism2_pci_suspend(struct pci_dev *pdev, pm_message_t state)
J
Jouni Malinen 已提交
409 410 411 412 413 414 415 416 417 418
{
	struct net_device *dev = pci_get_drvdata(pdev);

	if (netif_running(dev)) {
		netif_stop_queue(dev);
		netif_device_detach(dev);
	}
	prism2_suspend(dev);
	pci_save_state(pdev);
	pci_disable_device(pdev);
419
	pci_set_power_state(pdev, PCI_D3hot);
J
Jouni Malinen 已提交
420 421 422 423 424 425 426

	return 0;
}

static int prism2_pci_resume(struct pci_dev *pdev)
{
	struct net_device *dev = pci_get_drvdata(pdev);
427
	int err;
J
Jouni Malinen 已提交
428

429 430 431 432 433 434
	err = pci_enable_device(pdev);
	if (err) {
		printk(KERN_ERR "%s: pci_enable_device failed on resume\n",
		       dev->name);
		return err;
	}
J
Jouni Malinen 已提交
435 436 437 438 439 440 441 442 443 444 445 446 447 448
	pci_restore_state(pdev);
	prism2_hw_config(dev, 0);
	if (netif_running(dev)) {
		netif_device_attach(dev);
		netif_start_queue(dev);
	}

	return 0;
}
#endif /* CONFIG_PM */


MODULE_DEVICE_TABLE(pci, prism2_pci_id_table);

449
static struct pci_driver prism2_pci_driver = {
450
	.name		= "hostap_pci",
J
Jouni Malinen 已提交
451 452 453 454 455 456 457 458 459 460 461 462
	.id_table	= prism2_pci_id_table,
	.probe		= prism2_pci_probe,
	.remove		= prism2_pci_remove,
#ifdef CONFIG_PM
	.suspend	= prism2_pci_suspend,
	.resume		= prism2_pci_resume,
#endif /* CONFIG_PM */
};


static int __init init_prism2_pci(void)
{
463
	return pci_register_driver(&prism2_pci_driver);
J
Jouni Malinen 已提交
464 465 466 467 468
}


static void __exit exit_prism2_pci(void)
{
469
	pci_unregister_driver(&prism2_pci_driver);
J
Jouni Malinen 已提交
470 471 472 473 474
}


module_init(init_prism2_pci);
module_exit(exit_prism2_pci);