Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | /* |
| 45 | * Theory of operation... |
| 46 | * ------------------- |
| 47 | * Maybe you had a look in orinoco_plx. Well, this is totally different... |
| 48 | * |
| 49 | * The card contains only one PCI region, which contains all the usual |
| 50 | * hermes registers. |
| 51 | * |
| 52 | * The driver will memory map this region in normal memory. Because |
| 53 | * the hermes registers are mapped in normal memory and not in ISA I/O |
| 54 | * post space, we can't use the usual inw/outw macros and we need to |
| 55 | * use readw/writew. |
| 56 | * This slight difference force us to compile our own version of |
| 57 | * hermes.c with the register access macro changed. That's a bit |
| 58 | * hackish but works fine. |
| 59 | * |
| 60 | * Note that the PCI region is pretty big (4K). That's much more than |
| 61 | * the usual set of hermes register (0x0 -> 0x3E). I've got a strong |
| 62 | * suspicion that the whole memory space of the adapter is in fact in |
| 63 | * this region. Accessing directly the adapter memory instead of going |
| 64 | * through the usual register would speed up significantely the |
| 65 | * operations... |
| 66 | * |
| 67 | * Finally, the card looks like this : |
| 68 | ----------------------- |
| 69 | Bus 0, device 14, function 0: |
| 70 | Network controller: PCI device 1260:3873 (Harris Semiconductor) (rev 1). |
| 71 | IRQ 11. |
| 72 | Master Capable. Latency=248. |
| 73 | Prefetchable 32 bit memory at 0xffbcc000 [0xffbccfff]. |
| 74 | ----------------------- |
| 75 | 00:0e.0 Network controller: Harris Semiconductor: Unknown device 3873 (rev 01) |
| 76 | Subsystem: Unknown device 1737:3874 |
| 77 | Control: I/O+ Mem+ BusMaster+ SpecCycle- MemWINV- VGASnoop- ParErr- Stepping- SERR- FastB2B- |
| 78 | Status: Cap+ 66Mhz- UDF- FastB2B+ ParErr- DEVSEL=medium >TAbort- <TAbort- <MAbort- >SERR- <PERR- |
| 79 | Latency: 248 set, cache line size 08 |
| 80 | Interrupt: pin A routed to IRQ 11 |
| 81 | Region 0: Memory at ffbcc000 (32-bit, prefetchable) [size=4K] |
| 82 | Capabilities: [dc] Power Management version 2 |
| 83 | Flags: PMEClk- AuxPwr- DSI- D1+ D2+ PME+ |
| 84 | Status: D0 PME-Enable- DSel=0 DScale=0 PME- |
| 85 | ----------------------- |
| 86 | * |
| 87 | * That's all.. |
| 88 | * |
| 89 | * Jean II |
| 90 | */ |
| 91 | |
| 92 | #define DRIVER_NAME "orinoco_pci" |
| 93 | #define PFX DRIVER_NAME ": " |
| 94 | |
| 95 | #include <linux/config.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | #include <linux/module.h> |
| 97 | #include <linux/kernel.h> |
| 98 | #include <linux/init.h> |
Pavel Roskin | ef846bf | 2005-09-23 04:18:07 -0400 | [diff] [blame] | 99 | #include <linux/delay.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | #include <linux/pci.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | #include "orinoco.h" |
| 103 | |
| 104 | /* All the magic there is from wlan-ng */ |
| 105 | /* Magic offset of the reset register of the PCI card */ |
| 106 | #define HERMES_PCI_COR (0x26) |
| 107 | /* Magic bitmask to reset the card */ |
| 108 | #define HERMES_PCI_COR_MASK (0x0080) |
| 109 | /* Magic timeouts for doing the reset. |
| 110 | * Those times are straight from wlan-ng, and it is claimed that they |
| 111 | * are necessary. Alan will kill me. Take your time and grab a coffee. */ |
| 112 | #define HERMES_PCI_COR_ONT (250) /* ms */ |
| 113 | #define HERMES_PCI_COR_OFFT (500) /* ms */ |
| 114 | #define HERMES_PCI_COR_BUSYT (500) /* ms */ |
| 115 | |
| 116 | /* Orinoco PCI specific data */ |
| 117 | struct orinoco_pci_card { |
| 118 | void __iomem *pci_ioaddr; |
| 119 | }; |
| 120 | |
| 121 | /* |
| 122 | * Do a soft reset of the PCI card using the Configuration Option Register |
| 123 | * We need this to get going... |
| 124 | * This is the part of the code that is strongly inspired from wlan-ng |
| 125 | * |
| 126 | * Note : This code is done with irq enabled. This mean that many |
| 127 | * interrupts will occur while we are there. This is why we use the |
| 128 | * jiffies to regulate time instead of a straight mdelay(). Usually we |
| 129 | * need only around 245 iteration of the loop to do 250 ms delay. |
| 130 | * |
| 131 | * Note bis : Don't try to access HERMES_CMD during the reset phase. |
| 132 | * It just won't work ! |
| 133 | */ |
| 134 | static int |
| 135 | orinoco_pci_cor_reset(struct orinoco_private *priv) |
| 136 | { |
| 137 | hermes_t *hw = &priv->hw; |
| 138 | unsigned long timeout; |
| 139 | u16 reg; |
| 140 | |
| 141 | /* Assert the reset until the card notice */ |
| 142 | hermes_write_regn(hw, PCI_COR, HERMES_PCI_COR_MASK); |
| 143 | mdelay(HERMES_PCI_COR_ONT); |
| 144 | |
| 145 | /* Give time for the card to recover from this hard effort */ |
| 146 | hermes_write_regn(hw, PCI_COR, 0x0000); |
| 147 | mdelay(HERMES_PCI_COR_OFFT); |
| 148 | |
| 149 | /* The card is ready when it's no longer busy */ |
| 150 | timeout = jiffies + (HERMES_PCI_COR_BUSYT * HZ / 1000); |
| 151 | reg = hermes_read_regn(hw, CMD); |
| 152 | while (time_before(jiffies, timeout) && (reg & HERMES_CMD_BUSY)) { |
| 153 | mdelay(1); |
| 154 | reg = hermes_read_regn(hw, CMD); |
| 155 | } |
| 156 | |
| 157 | /* Still busy? */ |
| 158 | if (reg & HERMES_CMD_BUSY) { |
| 159 | printk(KERN_ERR PFX "Busy timeout\n"); |
| 160 | return -ETIMEDOUT; |
| 161 | } |
| 162 | |
| 163 | return 0; |
| 164 | } |
| 165 | |
| 166 | /* |
| 167 | * Initialise a card. Mostly similar to PLX code. |
| 168 | */ |
| 169 | static int orinoco_pci_init_one(struct pci_dev *pdev, |
| 170 | const struct pci_device_id *ent) |
| 171 | { |
| 172 | int err = 0; |
Pavel Roskin | 3d52996 | 2006-04-07 04:10:53 -0400 | [diff] [blame^] | 173 | void __iomem *pci_ioaddr = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 | struct orinoco_private *priv = NULL; |
| 175 | struct orinoco_pci_card *card; |
| 176 | struct net_device *dev = NULL; |
| 177 | |
| 178 | err = pci_enable_device(pdev); |
| 179 | if (err) { |
| 180 | printk(KERN_ERR PFX "Cannot enable PCI device\n"); |
| 181 | return err; |
| 182 | } |
| 183 | |
| 184 | err = pci_request_regions(pdev, DRIVER_NAME); |
| 185 | if (err != 0) { |
| 186 | printk(KERN_ERR PFX "Cannot obtain PCI resources\n"); |
| 187 | goto fail_resources; |
| 188 | } |
| 189 | |
| 190 | /* Resource 0 is mapped to the hermes registers */ |
Pavel Roskin | 3d52996 | 2006-04-07 04:10:53 -0400 | [diff] [blame^] | 191 | pci_ioaddr = pci_iomap(pdev, 0, 0); |
| 192 | if (!pci_ioaddr) { |
| 193 | err = -EIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 194 | printk(KERN_ERR PFX "Cannot remap hardware registers\n"); |
| 195 | goto fail_map; |
| 196 | } |
| 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 | |
| 205 | priv = netdev_priv(dev); |
| 206 | card = priv->card; |
| 207 | card->pci_ioaddr = pci_ioaddr; |
Pavel Roskin | 3d52996 | 2006-04-07 04:10:53 -0400 | [diff] [blame^] | 208 | dev->mem_start = pci_resource_start(pdev, 0); |
| 209 | dev->mem_end = dev->mem_start + pci_resource_len(pdev, 0) - 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 210 | SET_MODULE_OWNER(dev); |
| 211 | SET_NETDEV_DEV(dev, &pdev->dev); |
| 212 | |
| 213 | hermes_struct_init(&priv->hw, pci_ioaddr, HERMES_32BIT_REGSPACING); |
| 214 | |
| 215 | printk(KERN_DEBUG PFX "Detected device %s, mem:0x%lx-0x%lx, irq %d\n", |
| 216 | pci_name(pdev), dev->mem_start, dev->mem_end, pdev->irq); |
| 217 | |
| 218 | err = request_irq(pdev->irq, orinoco_interrupt, SA_SHIRQ, |
| 219 | dev->name, dev); |
| 220 | if (err) { |
| 221 | printk(KERN_ERR PFX "Cannot allocate IRQ %d\n", pdev->irq); |
| 222 | err = -EBUSY; |
| 223 | goto fail_irq; |
| 224 | } |
| 225 | dev->irq = pdev->irq; |
| 226 | |
| 227 | /* Perform a COR reset to start the card */ |
| 228 | err = orinoco_pci_cor_reset(priv); |
| 229 | if (err) { |
| 230 | printk(KERN_ERR PFX "Initial reset failed\n"); |
| 231 | goto fail; |
| 232 | } |
| 233 | |
| 234 | err = register_netdev(dev); |
| 235 | if (err) { |
| 236 | printk(KERN_ERR PFX "Failed to register net device\n"); |
| 237 | goto fail; |
| 238 | } |
| 239 | |
| 240 | pci_set_drvdata(pdev, dev); |
| 241 | |
| 242 | return 0; |
| 243 | |
| 244 | fail: |
| 245 | free_irq(pdev->irq, dev); |
| 246 | |
| 247 | fail_irq: |
| 248 | pci_set_drvdata(pdev, NULL); |
| 249 | free_orinocodev(dev); |
| 250 | |
| 251 | fail_alloc: |
| 252 | iounmap(pci_ioaddr); |
| 253 | |
| 254 | fail_map: |
| 255 | pci_release_regions(pdev); |
| 256 | |
| 257 | fail_resources: |
| 258 | pci_disable_device(pdev); |
| 259 | |
| 260 | return err; |
| 261 | } |
| 262 | |
| 263 | static void __devexit orinoco_pci_remove_one(struct pci_dev *pdev) |
| 264 | { |
| 265 | struct net_device *dev = pci_get_drvdata(pdev); |
| 266 | struct orinoco_private *priv = netdev_priv(dev); |
| 267 | struct orinoco_pci_card *card = priv->card; |
| 268 | |
| 269 | unregister_netdev(dev); |
| 270 | free_irq(dev->irq, dev); |
| 271 | pci_set_drvdata(pdev, NULL); |
| 272 | free_orinocodev(dev); |
| 273 | iounmap(card->pci_ioaddr); |
| 274 | pci_release_regions(pdev); |
| 275 | pci_disable_device(pdev); |
| 276 | } |
| 277 | |
Pavel Machek | 05adc3b | 2005-04-16 15:25:25 -0700 | [diff] [blame] | 278 | static int orinoco_pci_suspend(struct pci_dev *pdev, pm_message_t state) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 279 | { |
| 280 | struct net_device *dev = pci_get_drvdata(pdev); |
| 281 | struct orinoco_private *priv = netdev_priv(dev); |
| 282 | unsigned long flags; |
| 283 | int err; |
| 284 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 285 | |
| 286 | err = orinoco_lock(priv, &flags); |
| 287 | if (err) { |
| 288 | printk(KERN_ERR "%s: hw_unavailable on orinoco_pci_suspend\n", |
| 289 | dev->name); |
| 290 | return err; |
| 291 | } |
| 292 | |
| 293 | err = __orinoco_down(dev); |
| 294 | if (err) |
| 295 | printk(KERN_WARNING "%s: orinoco_pci_suspend(): Error %d downing interface\n", |
| 296 | dev->name, err); |
| 297 | |
| 298 | netif_device_detach(dev); |
| 299 | |
| 300 | priv->hw_unavailable++; |
| 301 | |
| 302 | orinoco_unlock(priv, &flags); |
| 303 | |
Pavel Roskin | d62274b | 2006-04-07 04:10:51 -0400 | [diff] [blame] | 304 | free_irq(pdev->irq, dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 305 | pci_save_state(pdev); |
Pavel Roskin | d62274b | 2006-04-07 04:10:51 -0400 | [diff] [blame] | 306 | pci_disable_device(pdev); |
Pavel Machek | 05adc3b | 2005-04-16 15:25:25 -0700 | [diff] [blame] | 307 | pci_set_power_state(pdev, PCI_D3hot); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 308 | |
| 309 | return 0; |
| 310 | } |
| 311 | |
| 312 | static int orinoco_pci_resume(struct pci_dev *pdev) |
| 313 | { |
| 314 | struct net_device *dev = pci_get_drvdata(pdev); |
| 315 | struct orinoco_private *priv = netdev_priv(dev); |
| 316 | unsigned long flags; |
| 317 | int err; |
| 318 | |
| 319 | printk(KERN_DEBUG "%s: Orinoco-PCI waking up\n", dev->name); |
| 320 | |
| 321 | pci_set_power_state(pdev, 0); |
Pavel Roskin | d62274b | 2006-04-07 04:10:51 -0400 | [diff] [blame] | 322 | pci_enable_device(pdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 323 | pci_restore_state(pdev); |
| 324 | |
Pavel Roskin | d62274b | 2006-04-07 04:10:51 -0400 | [diff] [blame] | 325 | err = request_irq(pdev->irq, orinoco_interrupt, SA_SHIRQ, |
| 326 | dev->name, dev); |
| 327 | if (err) { |
| 328 | printk(KERN_ERR "%s: Cannot re-allocate IRQ\n", dev->name); |
| 329 | pci_disable_device(pdev); |
| 330 | return -EBUSY; |
| 331 | } |
| 332 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 333 | err = orinoco_reinit_firmware(dev); |
| 334 | if (err) { |
| 335 | printk(KERN_ERR "%s: Error %d re-initializing firmware on orinoco_pci_resume()\n", |
| 336 | dev->name, err); |
| 337 | return err; |
| 338 | } |
| 339 | |
| 340 | spin_lock_irqsave(&priv->lock, flags); |
| 341 | |
| 342 | netif_device_attach(dev); |
| 343 | |
| 344 | priv->hw_unavailable--; |
| 345 | |
| 346 | if (priv->open && (! priv->hw_unavailable)) { |
| 347 | err = __orinoco_up(dev); |
| 348 | if (err) |
| 349 | printk(KERN_ERR "%s: Error %d restarting card on orinoco_pci_resume()\n", |
| 350 | dev->name, err); |
| 351 | } |
| 352 | |
| 353 | spin_unlock_irqrestore(&priv->lock, flags); |
| 354 | |
| 355 | return 0; |
| 356 | } |
| 357 | |
| 358 | static struct pci_device_id orinoco_pci_pci_id_table[] = { |
| 359 | /* Intersil Prism 3 */ |
| 360 | {0x1260, 0x3872, PCI_ANY_ID, PCI_ANY_ID,}, |
| 361 | /* Intersil Prism 2.5 */ |
| 362 | {0x1260, 0x3873, PCI_ANY_ID, PCI_ANY_ID,}, |
| 363 | /* Samsung MagicLAN SWL-2210P */ |
| 364 | {0x167d, 0xa000, PCI_ANY_ID, PCI_ANY_ID,}, |
| 365 | {0,}, |
| 366 | }; |
| 367 | |
| 368 | MODULE_DEVICE_TABLE(pci, orinoco_pci_pci_id_table); |
| 369 | |
| 370 | static struct pci_driver orinoco_pci_driver = { |
| 371 | .name = DRIVER_NAME, |
| 372 | .id_table = orinoco_pci_pci_id_table, |
| 373 | .probe = orinoco_pci_init_one, |
| 374 | .remove = __devexit_p(orinoco_pci_remove_one), |
| 375 | .suspend = orinoco_pci_suspend, |
| 376 | .resume = orinoco_pci_resume, |
| 377 | }; |
| 378 | |
| 379 | static char version[] __initdata = DRIVER_NAME " " DRIVER_VERSION |
| 380 | " (Pavel Roskin <proski@gnu.org>," |
| 381 | " David Gibson <hermes@gibson.dropbear.id.au> &" |
| 382 | " Jean Tourrilhes <jt@hpl.hp.com>)"; |
| 383 | MODULE_AUTHOR("Pavel Roskin <proski@gnu.org> & David Gibson <hermes@gibson.dropbear.id.au>"); |
| 384 | MODULE_DESCRIPTION("Driver for wireless LAN cards using direct PCI interface"); |
| 385 | MODULE_LICENSE("Dual MPL/GPL"); |
| 386 | |
| 387 | static int __init orinoco_pci_init(void) |
| 388 | { |
| 389 | printk(KERN_DEBUG "%s\n", version); |
| 390 | return pci_module_init(&orinoco_pci_driver); |
| 391 | } |
| 392 | |
| 393 | static void __exit orinoco_pci_exit(void) |
| 394 | { |
| 395 | pci_unregister_driver(&orinoco_pci_driver); |
| 396 | } |
| 397 | |
| 398 | module_init(orinoco_pci_init); |
| 399 | module_exit(orinoco_pci_exit); |
| 400 | |
| 401 | /* |
| 402 | * Local variables: |
| 403 | * c-indent-level: 8 |
| 404 | * c-basic-offset: 8 |
| 405 | * tab-width: 8 |
| 406 | * End: |
| 407 | */ |