hostap_pci.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. #define PRISM2_PCI
  2. /* Host AP driver's support for Intersil Prism2.5 PCI cards is based on
  3. * driver patches from Reyk Floeter <reyk@vantronix.net> and
  4. * Andy Warner <andyw@pobox.com> */
  5. #include <linux/module.h>
  6. #include <linux/if.h>
  7. #include <linux/skbuff.h>
  8. #include <linux/netdevice.h>
  9. #include <linux/slab.h>
  10. #include <linux/workqueue.h>
  11. #include <linux/wireless.h>
  12. #include <net/iw_handler.h>
  13. #include <linux/ioport.h>
  14. #include <linux/pci.h>
  15. #include <asm/io.h>
  16. #include "hostap_wlan.h"
  17. static char *dev_info = "hostap_pci";
  18. MODULE_AUTHOR("Jouni Malinen");
  19. MODULE_DESCRIPTION("Support for Intersil Prism2.5-based 802.11 wireless LAN "
  20. "PCI cards.");
  21. MODULE_SUPPORTED_DEVICE("Intersil Prism2.5-based WLAN PCI cards");
  22. MODULE_LICENSE("GPL");
  23. /* struct local_info::hw_priv */
  24. struct hostap_pci_priv {
  25. void __iomem *mem_start;
  26. };
  27. /* FIX: do we need mb/wmb/rmb with memory operations? */
  28. static const struct pci_device_id prism2_pci_id_table[] = {
  29. /* Intersil Prism3 ISL3872 11Mb/s WLAN Controller */
  30. { 0x1260, 0x3872, PCI_ANY_ID, PCI_ANY_ID },
  31. /* Intersil Prism2.5 ISL3874 11Mb/s WLAN Controller */
  32. { 0x1260, 0x3873, PCI_ANY_ID, PCI_ANY_ID },
  33. /* Samsung MagicLAN SWL-2210P */
  34. { 0x167d, 0xa000, PCI_ANY_ID, PCI_ANY_ID },
  35. { 0 }
  36. };
  37. #ifdef PRISM2_IO_DEBUG
  38. static inline void hfa384x_outb_debug(struct net_device *dev, int a, u8 v)
  39. {
  40. struct hostap_interface *iface;
  41. struct hostap_pci_priv *hw_priv;
  42. local_info_t *local;
  43. unsigned long flags;
  44. iface = netdev_priv(dev);
  45. local = iface->local;
  46. hw_priv = local->hw_priv;
  47. spin_lock_irqsave(&local->lock, flags);
  48. prism2_io_debug_add(dev, PRISM2_IO_DEBUG_CMD_OUTB, a, v);
  49. writeb(v, hw_priv->mem_start + a);
  50. spin_unlock_irqrestore(&local->lock, flags);
  51. }
  52. static inline u8 hfa384x_inb_debug(struct net_device *dev, int a)
  53. {
  54. struct hostap_interface *iface;
  55. struct hostap_pci_priv *hw_priv;
  56. local_info_t *local;
  57. unsigned long flags;
  58. u8 v;
  59. iface = netdev_priv(dev);
  60. local = iface->local;
  61. hw_priv = local->hw_priv;
  62. spin_lock_irqsave(&local->lock, flags);
  63. v = readb(hw_priv->mem_start + a);
  64. prism2_io_debug_add(dev, PRISM2_IO_DEBUG_CMD_INB, a, v);
  65. spin_unlock_irqrestore(&local->lock, flags);
  66. return v;
  67. }
  68. static inline void hfa384x_outw_debug(struct net_device *dev, int a, u16 v)
  69. {
  70. struct hostap_interface *iface;
  71. struct hostap_pci_priv *hw_priv;
  72. local_info_t *local;
  73. unsigned long flags;
  74. iface = netdev_priv(dev);
  75. local = iface->local;
  76. hw_priv = local->hw_priv;
  77. spin_lock_irqsave(&local->lock, flags);
  78. prism2_io_debug_add(dev, PRISM2_IO_DEBUG_CMD_OUTW, a, v);
  79. writew(v, hw_priv->mem_start + a);
  80. spin_unlock_irqrestore(&local->lock, flags);
  81. }
  82. static inline u16 hfa384x_inw_debug(struct net_device *dev, int a)
  83. {
  84. struct hostap_interface *iface;
  85. struct hostap_pci_priv *hw_priv;
  86. local_info_t *local;
  87. unsigned long flags;
  88. u16 v;
  89. iface = netdev_priv(dev);
  90. local = iface->local;
  91. hw_priv = local->hw_priv;
  92. spin_lock_irqsave(&local->lock, flags);
  93. v = readw(hw_priv->mem_start + a);
  94. prism2_io_debug_add(dev, PRISM2_IO_DEBUG_CMD_INW, a, v);
  95. spin_unlock_irqrestore(&local->lock, flags);
  96. return v;
  97. }
  98. #define HFA384X_OUTB(v,a) hfa384x_outb_debug(dev, (a), (v))
  99. #define HFA384X_INB(a) hfa384x_inb_debug(dev, (a))
  100. #define HFA384X_OUTW(v,a) hfa384x_outw_debug(dev, (a), (v))
  101. #define HFA384X_INW(a) hfa384x_inw_debug(dev, (a))
  102. #define HFA384X_OUTW_DATA(v,a) hfa384x_outw_debug(dev, (a), le16_to_cpu((v)))
  103. #define HFA384X_INW_DATA(a) cpu_to_le16(hfa384x_inw_debug(dev, (a)))
  104. #else /* PRISM2_IO_DEBUG */
  105. static inline void hfa384x_outb(struct net_device *dev, int a, u8 v)
  106. {
  107. struct hostap_interface *iface;
  108. struct hostap_pci_priv *hw_priv;
  109. iface = netdev_priv(dev);
  110. hw_priv = iface->local->hw_priv;
  111. writeb(v, hw_priv->mem_start + a);
  112. }
  113. static inline u8 hfa384x_inb(struct net_device *dev, int a)
  114. {
  115. struct hostap_interface *iface;
  116. struct hostap_pci_priv *hw_priv;
  117. iface = netdev_priv(dev);
  118. hw_priv = iface->local->hw_priv;
  119. return readb(hw_priv->mem_start + a);
  120. }
  121. static inline void hfa384x_outw(struct net_device *dev, int a, u16 v)
  122. {
  123. struct hostap_interface *iface;
  124. struct hostap_pci_priv *hw_priv;
  125. iface = netdev_priv(dev);
  126. hw_priv = iface->local->hw_priv;
  127. writew(v, hw_priv->mem_start + a);
  128. }
  129. static inline u16 hfa384x_inw(struct net_device *dev, int a)
  130. {
  131. struct hostap_interface *iface;
  132. struct hostap_pci_priv *hw_priv;
  133. iface = netdev_priv(dev);
  134. hw_priv = iface->local->hw_priv;
  135. return readw(hw_priv->mem_start + a);
  136. }
  137. #define HFA384X_OUTB(v,a) hfa384x_outb(dev, (a), (v))
  138. #define HFA384X_INB(a) hfa384x_inb(dev, (a))
  139. #define HFA384X_OUTW(v,a) hfa384x_outw(dev, (a), (v))
  140. #define HFA384X_INW(a) hfa384x_inw(dev, (a))
  141. #define HFA384X_OUTW_DATA(v,a) hfa384x_outw(dev, (a), le16_to_cpu((v)))
  142. #define HFA384X_INW_DATA(a) cpu_to_le16(hfa384x_inw(dev, (a)))
  143. #endif /* PRISM2_IO_DEBUG */
  144. static int hfa384x_from_bap(struct net_device *dev, u16 bap, void *buf,
  145. int len)
  146. {
  147. u16 d_off;
  148. __le16 *pos;
  149. d_off = (bap == 1) ? HFA384X_DATA1_OFF : HFA384X_DATA0_OFF;
  150. pos = (__le16 *) buf;
  151. for ( ; len > 1; len -= 2)
  152. *pos++ = HFA384X_INW_DATA(d_off);
  153. if (len & 1)
  154. *((char *) pos) = HFA384X_INB(d_off);
  155. return 0;
  156. }
  157. static int hfa384x_to_bap(struct net_device *dev, u16 bap, void *buf, int len)
  158. {
  159. u16 d_off;
  160. __le16 *pos;
  161. d_off = (bap == 1) ? HFA384X_DATA1_OFF : HFA384X_DATA0_OFF;
  162. pos = (__le16 *) buf;
  163. for ( ; len > 1; len -= 2)
  164. HFA384X_OUTW_DATA(*pos++, d_off);
  165. if (len & 1)
  166. HFA384X_OUTB(*((char *) pos), d_off);
  167. return 0;
  168. }
  169. /* FIX: This might change at some point.. */
  170. #include "hostap_hw.c"
  171. static void prism2_pci_cor_sreset(local_info_t *local)
  172. {
  173. struct net_device *dev = local->dev;
  174. u16 reg;
  175. reg = HFA384X_INB(HFA384X_PCICOR_OFF);
  176. printk(KERN_DEBUG "%s: Original COR value: 0x%0x\n", dev->name, reg);
  177. /* linux-wlan-ng uses extremely long hold and settle times for
  178. * COR sreset. A comment in the driver code mentions that the long
  179. * delays appear to be necessary. However, at least IBM 22P6901 seems
  180. * to work fine with shorter delays.
  181. *
  182. * Longer delays can be configured by uncommenting following line: */
  183. /* #define PRISM2_PCI_USE_LONG_DELAYS */
  184. #ifdef PRISM2_PCI_USE_LONG_DELAYS
  185. int i;
  186. HFA384X_OUTW(reg | 0x0080, HFA384X_PCICOR_OFF);
  187. mdelay(250);
  188. HFA384X_OUTW(reg & ~0x0080, HFA384X_PCICOR_OFF);
  189. mdelay(500);
  190. /* Wait for f/w to complete initialization (CMD:BUSY == 0) */
  191. i = 2000000 / 10;
  192. while ((HFA384X_INW(HFA384X_CMD_OFF) & HFA384X_CMD_BUSY) && --i)
  193. udelay(10);
  194. #else /* PRISM2_PCI_USE_LONG_DELAYS */
  195. HFA384X_OUTW(reg | 0x0080, HFA384X_PCICOR_OFF);
  196. mdelay(2);
  197. HFA384X_OUTW(reg & ~0x0080, HFA384X_PCICOR_OFF);
  198. mdelay(2);
  199. #endif /* PRISM2_PCI_USE_LONG_DELAYS */
  200. if (HFA384X_INW(HFA384X_CMD_OFF) & HFA384X_CMD_BUSY) {
  201. printk(KERN_DEBUG "%s: COR sreset timeout\n", dev->name);
  202. }
  203. }
  204. static void prism2_pci_genesis_reset(local_info_t *local, int hcr)
  205. {
  206. struct net_device *dev = local->dev;
  207. HFA384X_OUTW(0x00C5, HFA384X_PCICOR_OFF);
  208. mdelay(10);
  209. HFA384X_OUTW(hcr, HFA384X_PCIHCR_OFF);
  210. mdelay(10);
  211. HFA384X_OUTW(0x0045, HFA384X_PCICOR_OFF);
  212. mdelay(10);
  213. }
  214. static struct prism2_helper_functions prism2_pci_funcs =
  215. {
  216. .card_present = NULL,
  217. .cor_sreset = prism2_pci_cor_sreset,
  218. .genesis_reset = prism2_pci_genesis_reset,
  219. .hw_type = HOSTAP_HW_PCI,
  220. };
  221. static int prism2_pci_probe(struct pci_dev *pdev,
  222. const struct pci_device_id *id)
  223. {
  224. unsigned long phymem;
  225. void __iomem *mem = NULL;
  226. local_info_t *local = NULL;
  227. struct net_device *dev = NULL;
  228. static int cards_found /* = 0 */;
  229. int irq_registered = 0;
  230. struct hostap_interface *iface;
  231. struct hostap_pci_priv *hw_priv;
  232. hw_priv = kzalloc(sizeof(*hw_priv), GFP_KERNEL);
  233. if (hw_priv == NULL)
  234. return -ENOMEM;
  235. if (pci_enable_device(pdev))
  236. goto err_out_free;
  237. phymem = pci_resource_start(pdev, 0);
  238. if (!request_mem_region(phymem, pci_resource_len(pdev, 0), "Prism2")) {
  239. printk(KERN_ERR "prism2: Cannot reserve PCI memory region\n");
  240. goto err_out_disable;
  241. }
  242. mem = pci_ioremap_bar(pdev, 0);
  243. if (mem == NULL) {
  244. printk(KERN_ERR "prism2: Cannot remap PCI memory region\n") ;
  245. goto fail;
  246. }
  247. dev = prism2_init_local_data(&prism2_pci_funcs, cards_found,
  248. &pdev->dev);
  249. if (dev == NULL)
  250. goto fail;
  251. iface = netdev_priv(dev);
  252. local = iface->local;
  253. local->hw_priv = hw_priv;
  254. cards_found++;
  255. dev->irq = pdev->irq;
  256. hw_priv->mem_start = mem;
  257. dev->base_addr = (unsigned long) mem;
  258. prism2_pci_cor_sreset(local);
  259. pci_set_drvdata(pdev, dev);
  260. if (request_irq(dev->irq, prism2_interrupt, IRQF_SHARED, dev->name,
  261. dev)) {
  262. printk(KERN_WARNING "%s: request_irq failed\n", dev->name);
  263. goto fail;
  264. } else
  265. irq_registered = 1;
  266. if (!local->pri_only && prism2_hw_config(dev, 1)) {
  267. printk(KERN_DEBUG "%s: hardware initialization failed\n",
  268. dev_info);
  269. goto fail;
  270. }
  271. printk(KERN_INFO "%s: Intersil Prism2.5 PCI: "
  272. "mem=0x%lx, irq=%d\n", dev->name, phymem, dev->irq);
  273. return hostap_hw_ready(dev);
  274. fail:
  275. if (irq_registered && dev)
  276. free_irq(dev->irq, dev);
  277. if (mem)
  278. iounmap(mem);
  279. release_mem_region(phymem, pci_resource_len(pdev, 0));
  280. err_out_disable:
  281. pci_disable_device(pdev);
  282. prism2_free_local_data(dev);
  283. err_out_free:
  284. kfree(hw_priv);
  285. return -ENODEV;
  286. }
  287. static void prism2_pci_remove(struct pci_dev *pdev)
  288. {
  289. struct net_device *dev;
  290. struct hostap_interface *iface;
  291. void __iomem *mem_start;
  292. struct hostap_pci_priv *hw_priv;
  293. dev = pci_get_drvdata(pdev);
  294. iface = netdev_priv(dev);
  295. hw_priv = iface->local->hw_priv;
  296. /* Reset the hardware, and ensure interrupts are disabled. */
  297. prism2_pci_cor_sreset(iface->local);
  298. hfa384x_disable_interrupts(dev);
  299. if (dev->irq)
  300. free_irq(dev->irq, dev);
  301. mem_start = hw_priv->mem_start;
  302. prism2_free_local_data(dev);
  303. kfree(hw_priv);
  304. iounmap(mem_start);
  305. release_mem_region(pci_resource_start(pdev, 0),
  306. pci_resource_len(pdev, 0));
  307. pci_disable_device(pdev);
  308. }
  309. #ifdef CONFIG_PM
  310. static int prism2_pci_suspend(struct pci_dev *pdev, pm_message_t state)
  311. {
  312. struct net_device *dev = pci_get_drvdata(pdev);
  313. if (netif_running(dev)) {
  314. netif_stop_queue(dev);
  315. netif_device_detach(dev);
  316. }
  317. prism2_suspend(dev);
  318. pci_save_state(pdev);
  319. pci_disable_device(pdev);
  320. pci_set_power_state(pdev, PCI_D3hot);
  321. return 0;
  322. }
  323. static int prism2_pci_resume(struct pci_dev *pdev)
  324. {
  325. struct net_device *dev = pci_get_drvdata(pdev);
  326. int err;
  327. err = pci_enable_device(pdev);
  328. if (err) {
  329. printk(KERN_ERR "%s: pci_enable_device failed on resume\n",
  330. dev->name);
  331. return err;
  332. }
  333. pci_restore_state(pdev);
  334. prism2_hw_config(dev, 0);
  335. if (netif_running(dev)) {
  336. netif_device_attach(dev);
  337. netif_start_queue(dev);
  338. }
  339. return 0;
  340. }
  341. #endif /* CONFIG_PM */
  342. MODULE_DEVICE_TABLE(pci, prism2_pci_id_table);
  343. static struct pci_driver prism2_pci_driver = {
  344. .name = "hostap_pci",
  345. .id_table = prism2_pci_id_table,
  346. .probe = prism2_pci_probe,
  347. .remove = prism2_pci_remove,
  348. #ifdef CONFIG_PM
  349. .suspend = prism2_pci_suspend,
  350. .resume = prism2_pci_resume,
  351. #endif /* CONFIG_PM */
  352. };
  353. module_pci_driver(prism2_pci_driver);