orinoco_pci.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. /* orinoco_pci.c
  2. *
  3. * Driver for Prism II devices that have a direct PCI interface
  4. * (i.e., not in a Pcmcia or PLX bridge)
  5. *
  6. * Specifically here we're talking about the Linksys WMP11
  7. *
  8. * Current maintainers (as of 29 September 2003) are:
  9. * Pavel Roskin <proski AT gnu.org>
  10. * and David Gibson <hermes AT gibson.dropbear.id.au>
  11. *
  12. * Some of this code is borrowed from orinoco_plx.c
  13. * Copyright (C) 2001 Daniel Barlow <dan AT telent.net>
  14. * Some of this code is "inspired" by linux-wlan-ng-0.1.10, but nothing
  15. * has been copied from it. linux-wlan-ng-0.1.10 is originally :
  16. * Copyright (C) 1999 AbsoluteValue Systems, Inc. All Rights Reserved.
  17. * This file originally written by:
  18. * Copyright (C) 2001 Jean Tourrilhes <jt AT hpl.hp.com>
  19. * And is now maintained by:
  20. * (C) Copyright David Gibson, IBM Corp. 2002-2003.
  21. *
  22. * The contents of this file are subject to the Mozilla Public License
  23. * Version 1.1 (the "License"); you may not use this file except in
  24. * compliance with the License. You may obtain a copy of the License
  25. * at http://www.mozilla.org/MPL/
  26. *
  27. * Software distributed under the License is distributed on an "AS IS"
  28. * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
  29. * the License for the specific language governing rights and
  30. * limitations under the License.
  31. *
  32. * Alternatively, the contents of this file may be used under the
  33. * terms of the GNU General Public License version 2 (the "GPL"), in
  34. * which case the provisions of the GPL are applicable instead of the
  35. * above. If you wish to allow the use of your version of this file
  36. * only under the terms of the GPL and not to allow others to use your
  37. * version of this file under the MPL, indicate your decision by
  38. * deleting the provisions above and replace them with the notice and
  39. * other provisions required by the GPL. If you do not delete the
  40. * provisions above, a recipient may use your version of this file
  41. * under either the MPL or the GPL.
  42. */
  43. /*
  44. * Theory of operation...
  45. * -------------------
  46. * Maybe you had a look in orinoco_plx. Well, this is totally different...
  47. *
  48. * The card contains only one PCI region, which contains all the usual
  49. * hermes registers.
  50. *
  51. * The driver will memory map this region in normal memory. Because
  52. * the hermes registers are mapped in normal memory and not in ISA I/O
  53. * post space, we can't use the usual inw/outw macros and we need to
  54. * use readw/writew.
  55. * This slight difference force us to compile our own version of
  56. * hermes.c with the register access macro changed. That's a bit
  57. * hackish but works fine.
  58. *
  59. * Note that the PCI region is pretty big (4K). That's much more than
  60. * the usual set of hermes register (0x0 -> 0x3E). I've got a strong
  61. * suspicion that the whole memory space of the adapter is in fact in
  62. * this region. Accessing directly the adapter memory instead of going
  63. * through the usual register would speed up significantely the
  64. * operations...
  65. *
  66. * Finally, the card looks like this :
  67. -----------------------
  68. Bus 0, device 14, function 0:
  69. Network controller: PCI device 1260:3873 (Harris Semiconductor) (rev 1).
  70. IRQ 11.
  71. Master Capable. Latency=248.
  72. Prefetchable 32 bit memory at 0xffbcc000 [0xffbccfff].
  73. -----------------------
  74. 00:0e.0 Network controller: Harris Semiconductor: Unknown device 3873 (rev 01)
  75. Subsystem: Unknown device 1737:3874
  76. Control: I/O+ Mem+ BusMaster+ SpecCycle- MemWINV- VGASnoop- ParErr- Stepping- SERR- FastB2B-
  77. Status: Cap+ 66Mhz- UDF- FastB2B+ ParErr- DEVSEL=medium >TAbort- <TAbort- <MAbort- >SERR- <PERR-
  78. Latency: 248 set, cache line size 08
  79. Interrupt: pin A routed to IRQ 11
  80. Region 0: Memory at ffbcc000 (32-bit, prefetchable) [size=4K]
  81. Capabilities: [dc] Power Management version 2
  82. Flags: PMEClk- AuxPwr- DSI- D1+ D2+ PME+
  83. Status: D0 PME-Enable- DSel=0 DScale=0 PME-
  84. -----------------------
  85. *
  86. * That's all..
  87. *
  88. * Jean II
  89. */
  90. #define DRIVER_NAME "orinoco_pci"
  91. #define PFX DRIVER_NAME ": "
  92. #include <linux/config.h>
  93. #include <linux/module.h>
  94. #include <linux/kernel.h>
  95. #include <linux/init.h>
  96. #include <linux/sched.h>
  97. #include <linux/ptrace.h>
  98. #include <linux/slab.h>
  99. #include <linux/string.h>
  100. #include <linux/timer.h>
  101. #include <linux/ioport.h>
  102. #include <linux/netdevice.h>
  103. #include <linux/if_arp.h>
  104. #include <linux/etherdevice.h>
  105. #include <linux/list.h>
  106. #include <linux/pci.h>
  107. #include <linux/fcntl.h>
  108. #include <asm/uaccess.h>
  109. #include <asm/io.h>
  110. #include <asm/system.h>
  111. #include "hermes.h"
  112. #include "orinoco.h"
  113. /* All the magic there is from wlan-ng */
  114. /* Magic offset of the reset register of the PCI card */
  115. #define HERMES_PCI_COR (0x26)
  116. /* Magic bitmask to reset the card */
  117. #define HERMES_PCI_COR_MASK (0x0080)
  118. /* Magic timeouts for doing the reset.
  119. * Those times are straight from wlan-ng, and it is claimed that they
  120. * are necessary. Alan will kill me. Take your time and grab a coffee. */
  121. #define HERMES_PCI_COR_ONT (250) /* ms */
  122. #define HERMES_PCI_COR_OFFT (500) /* ms */
  123. #define HERMES_PCI_COR_BUSYT (500) /* ms */
  124. /* Orinoco PCI specific data */
  125. struct orinoco_pci_card {
  126. void __iomem *pci_ioaddr;
  127. };
  128. /*
  129. * Do a soft reset of the PCI card using the Configuration Option Register
  130. * We need this to get going...
  131. * This is the part of the code that is strongly inspired from wlan-ng
  132. *
  133. * Note : This code is done with irq enabled. This mean that many
  134. * interrupts will occur while we are there. This is why we use the
  135. * jiffies to regulate time instead of a straight mdelay(). Usually we
  136. * need only around 245 iteration of the loop to do 250 ms delay.
  137. *
  138. * Note bis : Don't try to access HERMES_CMD during the reset phase.
  139. * It just won't work !
  140. */
  141. static int
  142. orinoco_pci_cor_reset(struct orinoco_private *priv)
  143. {
  144. hermes_t *hw = &priv->hw;
  145. unsigned long timeout;
  146. u16 reg;
  147. /* Assert the reset until the card notice */
  148. hermes_write_regn(hw, PCI_COR, HERMES_PCI_COR_MASK);
  149. mdelay(HERMES_PCI_COR_ONT);
  150. /* Give time for the card to recover from this hard effort */
  151. hermes_write_regn(hw, PCI_COR, 0x0000);
  152. mdelay(HERMES_PCI_COR_OFFT);
  153. /* The card is ready when it's no longer busy */
  154. timeout = jiffies + (HERMES_PCI_COR_BUSYT * HZ / 1000);
  155. reg = hermes_read_regn(hw, CMD);
  156. while (time_before(jiffies, timeout) && (reg & HERMES_CMD_BUSY)) {
  157. mdelay(1);
  158. reg = hermes_read_regn(hw, CMD);
  159. }
  160. /* Still busy? */
  161. if (reg & HERMES_CMD_BUSY) {
  162. printk(KERN_ERR PFX "Busy timeout\n");
  163. return -ETIMEDOUT;
  164. }
  165. return 0;
  166. }
  167. /*
  168. * Initialise a card. Mostly similar to PLX code.
  169. */
  170. static int orinoco_pci_init_one(struct pci_dev *pdev,
  171. const struct pci_device_id *ent)
  172. {
  173. int err = 0;
  174. unsigned long pci_iorange;
  175. u16 __iomem *pci_ioaddr = NULL;
  176. unsigned long pci_iolen;
  177. struct orinoco_private *priv = NULL;
  178. struct orinoco_pci_card *card;
  179. struct net_device *dev = NULL;
  180. err = pci_enable_device(pdev);
  181. if (err) {
  182. printk(KERN_ERR PFX "Cannot enable PCI device\n");
  183. return err;
  184. }
  185. err = pci_request_regions(pdev, DRIVER_NAME);
  186. if (err != 0) {
  187. printk(KERN_ERR PFX "Cannot obtain PCI resources\n");
  188. goto fail_resources;
  189. }
  190. /* Resource 0 is mapped to the hermes registers */
  191. pci_iorange = pci_resource_start(pdev, 0);
  192. pci_iolen = pci_resource_len(pdev, 0);
  193. pci_ioaddr = ioremap(pci_iorange, pci_iolen);
  194. if (!pci_iorange) {
  195. printk(KERN_ERR PFX "Cannot remap hardware registers\n");
  196. goto fail_map;
  197. }
  198. /* Allocate network device */
  199. dev = alloc_orinocodev(sizeof(*card), orinoco_pci_cor_reset);
  200. if (! dev) {
  201. err = -ENOMEM;
  202. goto fail_alloc;
  203. }
  204. priv = netdev_priv(dev);
  205. card = priv->card;
  206. card->pci_ioaddr = pci_ioaddr;
  207. dev->mem_start = pci_iorange;
  208. dev->mem_end = pci_iorange + pci_iolen - 1;
  209. SET_MODULE_OWNER(dev);
  210. SET_NETDEV_DEV(dev, &pdev->dev);
  211. hermes_struct_init(&priv->hw, pci_ioaddr, HERMES_32BIT_REGSPACING);
  212. printk(KERN_DEBUG PFX "Detected device %s, mem:0x%lx-0x%lx, irq %d\n",
  213. pci_name(pdev), dev->mem_start, dev->mem_end, pdev->irq);
  214. err = request_irq(pdev->irq, orinoco_interrupt, SA_SHIRQ,
  215. dev->name, dev);
  216. if (err) {
  217. printk(KERN_ERR PFX "Cannot allocate IRQ %d\n", pdev->irq);
  218. err = -EBUSY;
  219. goto fail_irq;
  220. }
  221. dev->irq = pdev->irq;
  222. /* Perform a COR reset to start the card */
  223. err = orinoco_pci_cor_reset(priv);
  224. if (err) {
  225. printk(KERN_ERR PFX "Initial reset failed\n");
  226. goto fail;
  227. }
  228. err = register_netdev(dev);
  229. if (err) {
  230. printk(KERN_ERR PFX "Failed to register net device\n");
  231. goto fail;
  232. }
  233. pci_set_drvdata(pdev, dev);
  234. return 0;
  235. fail:
  236. free_irq(pdev->irq, dev);
  237. fail_irq:
  238. pci_set_drvdata(pdev, NULL);
  239. free_orinocodev(dev);
  240. fail_alloc:
  241. iounmap(pci_ioaddr);
  242. fail_map:
  243. pci_release_regions(pdev);
  244. fail_resources:
  245. pci_disable_device(pdev);
  246. return err;
  247. }
  248. static void __devexit orinoco_pci_remove_one(struct pci_dev *pdev)
  249. {
  250. struct net_device *dev = pci_get_drvdata(pdev);
  251. struct orinoco_private *priv = netdev_priv(dev);
  252. struct orinoco_pci_card *card = priv->card;
  253. unregister_netdev(dev);
  254. free_irq(dev->irq, dev);
  255. pci_set_drvdata(pdev, NULL);
  256. free_orinocodev(dev);
  257. iounmap(card->pci_ioaddr);
  258. pci_release_regions(pdev);
  259. pci_disable_device(pdev);
  260. }
  261. static int orinoco_pci_suspend(struct pci_dev *pdev, pm_message_t state)
  262. {
  263. struct net_device *dev = pci_get_drvdata(pdev);
  264. struct orinoco_private *priv = netdev_priv(dev);
  265. unsigned long flags;
  266. int err;
  267. err = orinoco_lock(priv, &flags);
  268. if (err) {
  269. printk(KERN_ERR "%s: hw_unavailable on orinoco_pci_suspend\n",
  270. dev->name);
  271. return err;
  272. }
  273. err = __orinoco_down(dev);
  274. if (err)
  275. printk(KERN_WARNING "%s: orinoco_pci_suspend(): Error %d downing interface\n",
  276. dev->name, err);
  277. netif_device_detach(dev);
  278. priv->hw_unavailable++;
  279. orinoco_unlock(priv, &flags);
  280. pci_save_state(pdev);
  281. pci_set_power_state(pdev, PCI_D3hot);
  282. return 0;
  283. }
  284. static int orinoco_pci_resume(struct pci_dev *pdev)
  285. {
  286. struct net_device *dev = pci_get_drvdata(pdev);
  287. struct orinoco_private *priv = netdev_priv(dev);
  288. unsigned long flags;
  289. int err;
  290. printk(KERN_DEBUG "%s: Orinoco-PCI waking up\n", dev->name);
  291. pci_set_power_state(pdev, 0);
  292. pci_restore_state(pdev);
  293. err = orinoco_reinit_firmware(dev);
  294. if (err) {
  295. printk(KERN_ERR "%s: Error %d re-initializing firmware on orinoco_pci_resume()\n",
  296. dev->name, err);
  297. return err;
  298. }
  299. spin_lock_irqsave(&priv->lock, flags);
  300. netif_device_attach(dev);
  301. priv->hw_unavailable--;
  302. if (priv->open && (! priv->hw_unavailable)) {
  303. err = __orinoco_up(dev);
  304. if (err)
  305. printk(KERN_ERR "%s: Error %d restarting card on orinoco_pci_resume()\n",
  306. dev->name, err);
  307. }
  308. spin_unlock_irqrestore(&priv->lock, flags);
  309. return 0;
  310. }
  311. static struct pci_device_id orinoco_pci_pci_id_table[] = {
  312. /* Intersil Prism 3 */
  313. {0x1260, 0x3872, PCI_ANY_ID, PCI_ANY_ID,},
  314. /* Intersil Prism 2.5 */
  315. {0x1260, 0x3873, PCI_ANY_ID, PCI_ANY_ID,},
  316. /* Samsung MagicLAN SWL-2210P */
  317. {0x167d, 0xa000, PCI_ANY_ID, PCI_ANY_ID,},
  318. {0,},
  319. };
  320. MODULE_DEVICE_TABLE(pci, orinoco_pci_pci_id_table);
  321. static struct pci_driver orinoco_pci_driver = {
  322. .name = DRIVER_NAME,
  323. .id_table = orinoco_pci_pci_id_table,
  324. .probe = orinoco_pci_init_one,
  325. .remove = __devexit_p(orinoco_pci_remove_one),
  326. .suspend = orinoco_pci_suspend,
  327. .resume = orinoco_pci_resume,
  328. };
  329. static char version[] __initdata = DRIVER_NAME " " DRIVER_VERSION
  330. " (Pavel Roskin <proski@gnu.org>,"
  331. " David Gibson <hermes@gibson.dropbear.id.au> &"
  332. " Jean Tourrilhes <jt@hpl.hp.com>)";
  333. MODULE_AUTHOR("Pavel Roskin <proski@gnu.org> & David Gibson <hermes@gibson.dropbear.id.au>");
  334. MODULE_DESCRIPTION("Driver for wireless LAN cards using direct PCI interface");
  335. MODULE_LICENSE("Dual MPL/GPL");
  336. static int __init orinoco_pci_init(void)
  337. {
  338. printk(KERN_DEBUG "%s\n", version);
  339. return pci_module_init(&orinoco_pci_driver);
  340. }
  341. static void __exit orinoco_pci_exit(void)
  342. {
  343. pci_unregister_driver(&orinoco_pci_driver);
  344. }
  345. module_init(orinoco_pci_init);
  346. module_exit(orinoco_pci_exit);
  347. /*
  348. * Local variables:
  349. * c-indent-level: 8
  350. * c-basic-offset: 8
  351. * tab-width: 8
  352. * End:
  353. */