orinoco_pci.c
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:11k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

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