ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / drivers / net / wireless / orinoco_plx.c
1 /* orinoco_plx.c 0.13e
2  * 
3  * Driver for Prism II devices which would usually be driven by orinoco_cs,
4  * but are connected to the PCI bus by a PLX9052. 
5  *
6  * Copyright (C) 2001 Daniel Barlow <dan@telent.net>
7  *
8  * The contents of this file are subject to the Mozilla Public License
9  * Version 1.1 (the "License"); you may not use this file except in
10  * compliance with the License. You may obtain a copy of the License
11  * at http://www.mozilla.org/MPL/
12  *
13  * Software distributed under the License is distributed on an "AS IS"
14  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
15  * the License for the specific language governing rights and
16  * limitations under the License.
17  *
18  * Alternatively, the contents of this file may be used under the
19  * terms of the GNU General Public License version 2 (the "GPL"), in
20  * which case the provisions of the GPL are applicable instead of the
21  * above.  If you wish to allow the use of your version of this file
22  * only under the terms of the GPL and not to allow others to use your
23  * version of this file under the MPL, indicate your decision by
24  * deleting the provisions above and replace them with the notice and
25  * other provisions required by the GPL.  If you do not delete the
26  * provisions above, a recipient may use your version of this file
27  * under either the MPL or the GPL.
28
29  * Caution: this is experimental and probably buggy.  For success and
30  * failure reports for different cards and adaptors, see
31  * orinoco_plx_pci_id_table near the end of the file.  If you have a
32  * card we don't have the PCI id for, and looks like it should work,
33  * drop me mail with the id and "it works"/"it doesn't work".
34  *
35  * Note: if everything gets detected fine but it doesn't actually send
36  * or receive packets, your first port of call should probably be to   
37  * try newer firmware in the card.  Especially if you're doing Ad-Hoc
38  * modes
39  *
40  * The actual driving is done by orinoco.c, this is just resource
41  * allocation stuff.  The explanation below is courtesy of Ryan Niemi
42  * on the linux-wlan-ng list at
43  * http://archives.neohapsis.com/archives/dev/linux-wlan/2001-q1/0026.html
44
45 The PLX9052-based cards (WL11000 and several others) are a different
46 beast than the usual PCMCIA-based PRISM2 configuration expected by
47 wlan-ng. Here's the general details on how the WL11000 PCI adapter
48 works:
49
50  - Two PCI I/O address spaces, one 0x80 long which contains the PLX9052
51    registers, and one that's 0x40 long mapped to the PCMCIA slot I/O
52    address space.
53
54  - One PCI memory address space, mapped to the PCMCIA memory space
55    (containing the CIS).
56
57 After identifying the I/O and memory space, you can read through the
58 memory space to confirm the CIS's device ID or manufacturer ID to make
59 sure it's the expected card. Keep in mind that the PCMCIA spec specifies
60 the CIS as the lower 8 bits of each word read from the CIS, so to read the
61 bytes of the CIS, read every other byte (0,2,4,...). Passing that test,
62 you need to enable the I/O address space on the PCMCIA card via the PCMCIA
63 COR register. This is the first byte following the CIS. In my case
64 (which may not have any relation to what's on the PRISM2 cards), COR was
65 at offset 0x800 within the PCI memory space. Write 0x41 to the COR
66 register to enable I/O mode and to select level triggered interrupts. To
67 confirm you actually succeeded, read the COR register back and make sure
68 it actually got set to 0x41, incase you have an unexpected card inserted.
69
70 Following that, you can treat the second PCI I/O address space (the one
71 that's not 0x80 in length) as the PCMCIA I/O space.
72
73 Note that in the Eumitcom's source for their drivers, they register the
74 interrupt as edge triggered when registering it with the Windows kernel. I
75 don't recall how to register edge triggered on Linux (if it can be done at
76 all). But in some experimentation, I don't see much operational
77 difference between using either interrupt mode. Don't mess with the
78 interrupt mode in the COR register though, as the PLX9052 wants level
79 triggers with the way the serial EEPROM configures it on the WL11000.
80
81 There's some other little quirks related to timing that I bumped into, but
82 I don't recall right now. Also, there's two variants of the WL11000 I've
83 seen, revision A1 and T2. These seem to differ slightly in the timings
84 configured in the wait-state generator in the PLX9052. There have also
85 been some comments from Eumitcom that cards shouldn't be hot swapped,
86 apparently due to risk of cooking the PLX9052. I'm unsure why they
87 believe this, as I can't see anything in the design that would really
88 cause a problem, except for crashing drivers not written to expect it. And
89 having developed drivers for the WL11000, I'd say it's quite tricky to
90 write code that will successfully deal with a hot unplug. Very odd things
91 happen on the I/O side of things. But anyway, be warned. Despite that,
92 I've hot-swapped a number of times during debugging and driver development
93 for various reasons (stuck WAIT# line after the radio card's firmware
94 locks up).
95
96 Hope this is enough info for someone to add PLX9052 support to the wlan-ng
97 card. In the case of the WL11000, the PCI ID's are 0x1639/0x0200, with
98 matching subsystem ID's. Other PLX9052-based manufacturers other than
99 Eumitcom (or on cards other than the WL11000) may have different PCI ID's.
100
101 If anyone needs any more specific info, let me know. I haven't had time
102 to implement support myself yet, and with the way things are going, might
103 not have time for a while..
104
105 ---end of mail---
106 */
107
108 #include <linux/config.h>
109
110 #include <linux/module.h>
111 #include <linux/kernel.h>
112 #include <linux/init.h>
113 #include <linux/sched.h>
114 #include <linux/ptrace.h>
115 #include <linux/slab.h>
116 #include <linux/string.h>
117 #include <linux/timer.h>
118 #include <linux/ioport.h>
119 #include <asm/uaccess.h>
120 #include <asm/io.h>
121 #include <asm/system.h>
122 #include <linux/netdevice.h>
123 #include <linux/if_arp.h>
124 #include <linux/etherdevice.h>
125 #include <linux/wireless.h>
126 #include <linux/list.h>
127 #include <linux/pci.h>
128 #include <linux/wireless.h>
129 #include <linux/fcntl.h>
130
131 #include <pcmcia/cisreg.h>
132
133 #include "hermes.h"
134 #include "orinoco.h"
135
136 static char dev_info[] = "orinoco_plx";
137
138 #define COR_OFFSET    (0x3e0 / 2)       /* COR attribute offset of Prism2 PC card */
139 #define COR_VALUE     (COR_LEVEL_REQ | COR_FUNC_ENA) /* Enable PC card with interrupt in level trigger */
140
141 #define PLX_INTCSR       0x4c /* Interrupt Control and Status Register */
142 #define PLX_INTCSR_INTEN (1<<6) /* Interrupt Enable bit */
143
144 static const u16 cis_magic[] = {
145         0x0001, 0x0003, 0x0000, 0x0000, 0x00ff, 0x0017, 0x0004, 0x0067
146 };
147
148 static int orinoco_plx_init_one(struct pci_dev *pdev,
149                                 const struct pci_device_id *ent)
150 {
151         int err = 0;
152         u16 *attr_mem = NULL;
153         u32 reg, addr;
154         struct orinoco_private *priv = NULL;
155         unsigned long pccard_ioaddr = 0;
156         unsigned long pccard_iolen = 0;
157         struct net_device *dev = NULL;
158         int i;
159
160         err = pci_enable_device(pdev);
161         if (err)
162                 return -EIO;
163
164         /* Resource 2 is mapped to the PCMCIA space */
165         attr_mem = ioremap(pci_resource_start(pdev, 2), PAGE_SIZE);
166         if (! attr_mem)
167                 goto fail;
168
169         printk(KERN_DEBUG "orinoco_plx: CIS: ");
170         for (i = 0; i < 16; i++) {
171                 printk("%02X:", (int)attr_mem[i]);
172         }
173         printk("\n");
174
175         /* Verify whether PC card is present */
176         /* FIXME: we probably need to be smarted about this */
177         if (memcmp(attr_mem, cis_magic, sizeof(cis_magic)) != 0) {
178                 printk(KERN_ERR "orinoco_plx: The CIS value of Prism2 PC card is invalid.\n");
179                 err = -EIO;
180                 goto fail;
181         }
182
183         /* PCMCIA COR is the first byte following CIS: this write should
184          * enable I/O mode and select level-triggered interrupts */
185         attr_mem[COR_OFFSET] = COR_VALUE;
186         mdelay(1);
187         reg = attr_mem[COR_OFFSET];
188         if (reg != COR_VALUE) {
189                 printk(KERN_ERR "orinoco_plx: Error setting COR value (reg=%x)\n", reg);
190                 goto fail;
191         }                       
192
193         iounmap(attr_mem);
194         attr_mem = NULL; /* done with this now, it seems */
195
196         /* bjoern: We need to tell the card to enable interrupts, in
197            case the serial eprom didn't do this already. See the
198            PLX9052 data book, p8-1 and 8-24 for reference. */
199         addr = pci_resource_start(pdev, 1);
200         reg = 0;
201         reg = inl(addr+PLX_INTCSR);
202         if (reg & PLX_INTCSR_INTEN)
203                 printk(KERN_DEBUG "orinoco_plx: "
204                        "Local Interrupt already enabled\n");
205         else {
206                 reg |= PLX_INTCSR_INTEN;
207                 outl(reg, addr+PLX_INTCSR);
208                 reg = inl(addr+PLX_INTCSR);
209                 if(!(reg & PLX_INTCSR_INTEN)) {
210                         printk(KERN_ERR "orinoco_plx: "
211                                "Couldn't enable Local Interrupts\n");
212                         goto fail;
213                 }
214         }
215
216         /* and 3 to the PCMCIA slot I/O address space */
217         pccard_ioaddr = pci_resource_start(pdev, 3);
218         pccard_iolen = pci_resource_len(pdev, 3);
219         if (! request_region(pccard_ioaddr, pccard_iolen, dev_info)) {
220                 printk(KERN_ERR "orinoco_plx: I/O resource 0x%lx @ 0x%lx busy\n",
221                        pccard_iolen, pccard_ioaddr);
222                 pccard_ioaddr = 0;
223                 err = -EBUSY;
224                 goto fail;
225         }
226
227         dev = alloc_orinocodev(0, NULL);
228         if (! dev) {
229                 err = -ENOMEM;
230                 goto fail;
231         }
232
233         priv = dev->priv;
234         dev->base_addr = pccard_ioaddr;
235         SET_MODULE_OWNER(dev);
236         SET_NETDEV_DEV(dev, &pdev->dev);
237
238         printk(KERN_DEBUG
239                "Detected Orinoco/Prism2 PLX device at %s irq:%d, io addr:0x%lx\n",
240                pci_name(pdev), pdev->irq, pccard_ioaddr);
241
242         hermes_struct_init(&(priv->hw), dev->base_addr,
243                         HERMES_IO, HERMES_16BIT_REGSPACING);
244         pci_set_drvdata(pdev, dev);
245
246         err = request_irq(pdev->irq, orinoco_interrupt, SA_SHIRQ, dev->name, dev);
247         if (err) {
248                 printk(KERN_ERR "orinoco_plx: Error allocating IRQ %d.\n", pdev->irq);
249                 err = -EBUSY;
250                 goto fail;
251         }
252         dev->irq = pdev->irq;
253
254         err = register_netdev(dev);
255         if (err)
256                 goto fail;
257
258         return 0;               /* succeeded */
259
260  fail:  
261         printk(KERN_DEBUG "orinoco_plx: init_one(), FAIL!\n");
262
263         if (dev) {
264                 if (dev->irq)
265                         free_irq(dev->irq, dev);
266                 
267                 free_netdev(dev);
268         }
269
270         if (pccard_ioaddr)
271                 release_region(pccard_ioaddr, pccard_iolen);
272
273         if (attr_mem)
274                 iounmap(attr_mem);
275
276         pci_disable_device(pdev);
277
278         return err;
279 }
280
281 static void __devexit orinoco_plx_remove_one(struct pci_dev *pdev)
282 {
283         struct net_device *dev = pci_get_drvdata(pdev);
284
285         if (! dev)
286                 BUG();
287
288         unregister_netdev(dev);
289                 
290         if (dev->irq)
291                 free_irq(dev->irq, dev);
292                 
293         pci_set_drvdata(pdev, NULL);
294
295         free_netdev(dev);
296
297         release_region(pci_resource_start(pdev, 3), pci_resource_len(pdev, 3));
298
299         pci_disable_device(pdev);
300 }
301
302
303 static struct pci_device_id orinoco_plx_pci_id_table[] = {
304         {0x111a, 0x1023, PCI_ANY_ID, PCI_ANY_ID,},      /* Siemens SpeedStream SS1023 */
305         {0x1385, 0x4100, PCI_ANY_ID, PCI_ANY_ID,},      /* Netgear MA301 */
306         {0x15e8, 0x0130, PCI_ANY_ID, PCI_ANY_ID,},      /* Correga  - does this work? */
307         {0x1638, 0x1100, PCI_ANY_ID, PCI_ANY_ID,},      /* SMC EZConnect SMC2602W,
308                                                            Eumitcom PCI WL11000,
309                                                            Addtron AWA-100*/
310         {0x16ab, 0x1100, PCI_ANY_ID, PCI_ANY_ID,},      /* Global Sun Tech GL24110P */
311         {0x16ab, 0x1101, PCI_ANY_ID, PCI_ANY_ID,},      /* Reported working, but unknown */
312         {0x16ab, 0x1102, PCI_ANY_ID, PCI_ANY_ID,},      /* Linksys WDT11 */
313         {0x16ec, 0x3685, PCI_ANY_ID, PCI_ANY_ID,},      /* USR 2415 */
314         {0xec80, 0xec00, PCI_ANY_ID, PCI_ANY_ID,},      /* Belkin F5D6000 tested by
315                                                            Brendan W. McAdams <rit@jacked-in.org> */
316         {0x10b7, 0x7770, PCI_ANY_ID, PCI_ANY_ID,},      /* 3Com AirConnect PCI tested by
317                                                            Damien Persohn <damien@persohn.net> */
318         {0,},
319 };
320
321 MODULE_DEVICE_TABLE(pci, orinoco_plx_pci_id_table);
322
323 static struct pci_driver orinoco_plx_driver = {
324         .name           = "orinoco_plx",
325         .id_table       = orinoco_plx_pci_id_table,
326         .probe          = orinoco_plx_init_one,
327         .remove         = __devexit_p(orinoco_plx_remove_one),
328         .suspend        = 0,
329         .resume         = 0,
330 };
331
332 static char version[] __initdata = "orinoco_plx.c 0.13e (Daniel Barlow <dan@telent.net>, David Gibson <hermes@gibson.dropbear.id.au>)";
333 MODULE_AUTHOR("Daniel Barlow <dan@telent.net>");
334 MODULE_DESCRIPTION("Driver for wireless LAN cards using the PLX9052 PCI bridge");
335 #ifdef MODULE_LICENSE
336 MODULE_LICENSE("Dual MPL/GPL");
337 #endif
338
339 static int __init orinoco_plx_init(void)
340 {
341         printk(KERN_DEBUG "%s\n", version);
342         return pci_module_init(&orinoco_plx_driver);
343 }
344
345 extern void __exit orinoco_plx_exit(void)
346 {
347         pci_unregister_driver(&orinoco_plx_driver);
348         current->state = TASK_UNINTERRUPTIBLE;
349         schedule_timeout(HZ);
350 }
351
352 module_init(orinoco_plx_init);
353 module_exit(orinoco_plx_exit);
354
355 /*
356  * Local variables:
357  *  c-indent-level: 8
358  *  c-basic-offset: 8
359  *  tab-width: 8
360  * End:
361  */