fedora core 6 1.2949 + vserver 2.2.0
[linux-2.6.git] / drivers / net / wireless / prism54 / islpci_hotplug.c
1 /*
2  *  Copyright (C) 2002 Intersil Americas Inc.
3  *  Copyright (C) 2003 Herbert Valerio Riedel <hvr@gnu.org>
4  *
5  *  This program is free software; you can redistribute it and/or modify
6  *  it under the terms of the GNU General Public License as published by
7  *  the Free Software Foundation; either version 2 of the License
8  *
9  *  This program is distributed in the hope that it will be useful,
10  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *  GNU General Public License for more details.
13  *
14  *  You should have received a copy of the GNU General Public License
15  *  along with this program; if not, write to the Free Software
16  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17  *
18  */
19
20 #include <linux/module.h>
21 #include <linux/pci.h>
22 #include <linux/delay.h>
23 #include <linux/init.h> /* For __init, __exit */
24 #include <linux/dma-mapping.h>
25
26 #include "prismcompat.h"
27 #include "islpci_dev.h"
28 #include "islpci_mgt.h"         /* for pc_debug */
29 #include "isl_oid.h"
30
31 #define DRV_NAME        "prism54"
32 #define DRV_VERSION     "1.2"
33
34 MODULE_AUTHOR("[Intersil] R.Bastings and W.Termorshuizen, The prism54.org Development Team <prism54-devel@prism54.org>");
35 MODULE_DESCRIPTION("The Prism54 802.11 Wireless LAN adapter");
36 MODULE_LICENSE("GPL");
37
38 static int      init_pcitm = 0;
39 module_param(init_pcitm, int, 0);
40
41 /* In this order: vendor, device, subvendor, subdevice, class, class_mask,
42  * driver_data
43  * If you have an update for this please contact prism54-devel@prism54.org
44  * The latest list can be found at http://prism54.org/supported_cards.php */
45 static const struct pci_device_id prism54_id_tbl[] = {
46         /* Intersil PRISM Duette/Prism GT Wireless LAN adapter */
47         {
48          0x1260, 0x3890,
49          PCI_ANY_ID, PCI_ANY_ID,
50          0, 0, 0
51         },
52
53         /* 3COM 3CRWE154G72 Wireless LAN adapter */
54         {
55          0x10b7, 0x6001,
56          PCI_ANY_ID, PCI_ANY_ID,
57          0, 0, 0
58         },
59
60         /* Intersil PRISM Indigo Wireless LAN adapter */
61         {
62          0x1260, 0x3877,
63          PCI_ANY_ID, PCI_ANY_ID,
64          0, 0, 0
65         },
66
67         /* Intersil PRISM Javelin/Xbow Wireless LAN adapter */
68         {
69          0x1260, 0x3886,
70          PCI_ANY_ID, PCI_ANY_ID,
71          0, 0, 0
72         },
73
74         /* End of list */
75         {0,0,0,0,0,0,0}
76 };
77
78 /* register the device with the Hotplug facilities of the kernel */
79 MODULE_DEVICE_TABLE(pci, prism54_id_tbl);
80
81 static int prism54_probe(struct pci_dev *, const struct pci_device_id *);
82 static void prism54_remove(struct pci_dev *);
83 static int prism54_suspend(struct pci_dev *, pm_message_t state);
84 static int prism54_resume(struct pci_dev *);
85
86 static struct pci_driver prism54_driver = {
87         .name = DRV_NAME,
88         .id_table = prism54_id_tbl,
89         .probe = prism54_probe,
90         .remove = prism54_remove,
91         .suspend = prism54_suspend,
92         .resume = prism54_resume,
93         /* .enable_wake ; we don't support this yet */
94 };
95
96 /******************************************************************************
97     Module initialization functions
98 ******************************************************************************/
99
100 int
101 prism54_probe(struct pci_dev *pdev, const struct pci_device_id *id)
102 {
103         struct net_device *ndev;
104         u8 latency_tmr;
105         u32 mem_addr;
106         islpci_private *priv;
107         int rvalue;
108
109         /* Enable the pci device */
110         if (pci_enable_device(pdev)) {
111                 printk(KERN_ERR "%s: pci_enable_device() failed.\n", DRV_NAME);
112                 return -ENODEV;
113         }
114
115         /* check whether the latency timer is set correctly */
116         pci_read_config_byte(pdev, PCI_LATENCY_TIMER, &latency_tmr);
117 #if VERBOSE > SHOW_ERROR_MESSAGES
118         DEBUG(SHOW_TRACING, "latency timer: %x\n", latency_tmr);
119 #endif
120         if (latency_tmr < PCIDEVICE_LATENCY_TIMER_MIN) {
121                 /* set the latency timer */
122                 pci_write_config_byte(pdev, PCI_LATENCY_TIMER,
123                                       PCIDEVICE_LATENCY_TIMER_VAL);
124         }
125
126         /* enable PCI DMA */
127         if (pci_set_dma_mask(pdev, DMA_32BIT_MASK)) {
128                 printk(KERN_ERR "%s: 32-bit PCI DMA not supported", DRV_NAME);
129                 goto do_pci_disable_device;
130         }
131
132         /* 0x40 is the programmable timer to configure the response timeout (TRDY_TIMEOUT)
133          * 0x41 is the programmable timer to configure the retry timeout (RETRY_TIMEOUT)
134          *      The RETRY_TIMEOUT is used to set the number of retries that the core, as a
135          *      Master, will perform before abandoning a cycle. The default value for
136          *      RETRY_TIMEOUT is 0x80, which far exceeds the PCI 2.1 requirement for new
137          *      devices. A write of zero to the RETRY_TIMEOUT register disables this
138          *      function to allow use with any non-compliant legacy devices that may
139          *      execute more retries.
140          *
141          *      Writing zero to both these two registers will disable both timeouts and
142          *      *can* solve problems caused by devices that are slow to respond.
143          *      Make this configurable - MSW
144          */
145         if ( init_pcitm >= 0 ) {
146                 pci_write_config_byte(pdev, 0x40, (u8)init_pcitm);
147                 pci_write_config_byte(pdev, 0x41, (u8)init_pcitm);
148         } else {
149                 printk(KERN_INFO "PCI TRDY/RETRY unchanged\n");
150         }
151
152         /* request the pci device I/O regions */
153         rvalue = pci_request_regions(pdev, DRV_NAME);
154         if (rvalue) {
155                 printk(KERN_ERR "%s: pci_request_regions failure (rc=%d)\n",
156                        DRV_NAME, rvalue);
157                 goto do_pci_disable_device;
158         }
159
160         /* check if the memory window is indeed set */
161         rvalue = pci_read_config_dword(pdev, PCI_BASE_ADDRESS_0, &mem_addr);
162         if (rvalue || !mem_addr) {
163                 printk(KERN_ERR "%s: PCI device memory region not configured; fix your BIOS or CardBus bridge/drivers\n",
164                        DRV_NAME);
165                 goto do_pci_release_regions;
166         }
167
168         /* enable PCI bus-mastering */
169         DEBUG(SHOW_TRACING, "%s: pci_set_master(pdev)\n", DRV_NAME);
170         pci_set_master(pdev);
171
172         /* enable MWI */
173         if (!pci_set_mwi(pdev))
174                 printk(KERN_INFO "%s: pci_set_mwi(pdev) succeeded\n", DRV_NAME);
175
176         /* setup the network device interface and its structure */
177         if (!(ndev = islpci_setup(pdev))) {
178                 /* error configuring the driver as a network device */
179                 printk(KERN_ERR "%s: could not configure network device\n",
180                        DRV_NAME);
181                 goto do_pci_clear_mwi;
182         }
183
184         priv = netdev_priv(ndev);
185         islpci_set_state(priv, PRV_STATE_PREBOOT); /* we are attempting to boot */
186
187         /* card is in unknown state yet, might have some interrupts pending */
188         isl38xx_disable_interrupts(priv->device_base);
189
190         /* request for the interrupt before uploading the firmware */
191         rvalue = request_irq(pdev->irq, &islpci_interrupt,
192                              IRQF_SHARED, ndev->name, priv);
193
194         if (rvalue) {
195                 /* error, could not hook the handler to the irq */
196                 printk(KERN_ERR "%s: could not install IRQ handler\n",
197                        ndev->name);
198                 goto do_unregister_netdev;
199         }
200
201         /* firmware upload is triggered in islpci_open */
202
203         return 0;
204
205       do_unregister_netdev:
206         unregister_netdev(ndev);
207         islpci_free_memory(priv);
208         pci_set_drvdata(pdev, NULL);
209         free_netdev(ndev);
210         priv = NULL;
211       do_pci_clear_mwi:
212         pci_clear_mwi(pdev);
213       do_pci_release_regions:
214         pci_release_regions(pdev);
215       do_pci_disable_device:
216         pci_disable_device(pdev);
217         return -EIO;
218 }
219
220 /* set by cleanup_module */
221 static volatile int __in_cleanup_module = 0;
222
223 /* this one removes one(!!) instance only */
224 void
225 prism54_remove(struct pci_dev *pdev)
226 {
227         struct net_device *ndev = pci_get_drvdata(pdev);
228         islpci_private *priv = ndev ? netdev_priv(ndev) : NULL;
229         BUG_ON(!priv);
230
231         if (!__in_cleanup_module) {
232                 printk(KERN_DEBUG "%s: hot unplug detected\n", ndev->name);
233                 islpci_set_state(priv, PRV_STATE_OFF);
234         }
235
236         printk(KERN_DEBUG "%s: removing device\n", ndev->name);
237
238         unregister_netdev(ndev);
239
240         /* free the interrupt request */
241
242         if (islpci_get_state(priv) != PRV_STATE_OFF) {
243                 isl38xx_disable_interrupts(priv->device_base);
244                 islpci_set_state(priv, PRV_STATE_OFF);
245                 /* This bellow causes a lockup at rmmod time. It might be
246                  * because some interrupts still linger after rmmod time,
247                  * see bug #17 */
248                 /* pci_set_power_state(pdev, 3);*/      /* try to power-off */
249         }
250
251         free_irq(pdev->irq, priv);
252
253         /* free the PCI memory and unmap the remapped page */
254         islpci_free_memory(priv);
255
256         pci_set_drvdata(pdev, NULL);
257         free_netdev(ndev);
258         priv = NULL;
259
260         pci_clear_mwi(pdev);
261
262         pci_release_regions(pdev);
263
264         pci_disable_device(pdev);
265 }
266
267 int
268 prism54_suspend(struct pci_dev *pdev, pm_message_t state)
269 {
270         struct net_device *ndev = pci_get_drvdata(pdev);
271         islpci_private *priv = ndev ? netdev_priv(ndev) : NULL;
272         BUG_ON(!priv);
273
274
275         pci_save_state(pdev);
276
277         /* tell the device not to trigger interrupts for now... */
278         isl38xx_disable_interrupts(priv->device_base);
279
280         /* from now on assume the hardware was already powered down
281            and don't touch it anymore */
282         islpci_set_state(priv, PRV_STATE_OFF);
283
284         netif_stop_queue(ndev);
285         netif_device_detach(ndev);
286
287         return 0;
288 }
289
290 int
291 prism54_resume(struct pci_dev *pdev)
292 {
293         struct net_device *ndev = pci_get_drvdata(pdev);
294         islpci_private *priv = ndev ? netdev_priv(ndev) : NULL;
295         int err;
296
297         BUG_ON(!priv);
298
299         printk(KERN_NOTICE "%s: got resume request\n", ndev->name);
300
301         err = pci_enable_device(pdev);
302         if (err) {
303                 printk(KERN_ERR "%s: pci_enable_device failed on resume\n",
304                        ndev->name);
305                 return err;
306         }
307
308         pci_restore_state(pdev);
309
310         /* alright let's go into the PREBOOT state */
311         islpci_reset(priv, 1);
312
313         netif_device_attach(ndev);
314         netif_start_queue(ndev);
315
316         return 0;
317 }
318
319 static int __init
320 prism54_module_init(void)
321 {
322         printk(KERN_INFO "Loaded %s driver, version %s\n",
323                DRV_NAME, DRV_VERSION);
324
325         __bug_on_wrong_struct_sizes ();
326
327         return pci_register_driver(&prism54_driver);
328 }
329
330 /* by the time prism54_module_exit() terminates, as a postcondition
331  * all instances will have been destroyed by calls to
332  * prism54_remove() */
333 static void __exit
334 prism54_module_exit(void)
335 {
336         __in_cleanup_module = 1;
337
338         pci_unregister_driver(&prism54_driver);
339
340         printk(KERN_INFO "Unloaded %s driver\n", DRV_NAME);
341
342         __in_cleanup_module = 0;
343 }
344
345 /* register entry points */
346 module_init(prism54_module_init);
347 module_exit(prism54_module_exit);
348 /* EOF */