pcmcia: convert pcmcia_request_configuration to pcmcia_enable_device
[linux-2.6.git] / drivers / char / pcmcia / ipwireless / main.c
1 /*
2  * IPWireless 3G PCMCIA Network Driver
3  *
4  * Original code
5  *   by Stephen Blackheath <stephen@blacksapphire.com>,
6  *      Ben Martel <benm@symmetric.co.nz>
7  *
8  * Copyrighted as follows:
9  *   Copyright (C) 2004 by Symmetric Systems Ltd (NZ)
10  *
11  * Various driver changes and rewrites, port to new kernels
12  *   Copyright (C) 2006-2007 Jiri Kosina
13  *
14  * Misc code cleanups and updates
15  *   Copyright (C) 2007 David Sterba
16  */
17
18 #include "hardware.h"
19 #include "network.h"
20 #include "main.h"
21 #include "tty.h"
22
23 #include <linux/delay.h>
24 #include <linux/init.h>
25 #include <linux/io.h>
26 #include <linux/kernel.h>
27 #include <linux/module.h>
28 #include <linux/sched.h>
29 #include <linux/slab.h>
30
31 #include <pcmcia/cisreg.h>
32 #include <pcmcia/device_id.h>
33 #include <pcmcia/ss.h>
34 #include <pcmcia/ds.h>
35
36 static struct pcmcia_device_id ipw_ids[] = {
37         PCMCIA_DEVICE_MANF_CARD(0x02f2, 0x0100),
38         PCMCIA_DEVICE_MANF_CARD(0x02f2, 0x0200),
39         PCMCIA_DEVICE_NULL
40 };
41 MODULE_DEVICE_TABLE(pcmcia, ipw_ids);
42
43 static void ipwireless_detach(struct pcmcia_device *link);
44
45 /*
46  * Module params
47  */
48 /* Debug mode: more verbose, print sent/recv bytes */
49 int ipwireless_debug;
50 int ipwireless_loopback;
51 int ipwireless_out_queue = 10;
52
53 module_param_named(debug, ipwireless_debug, int, 0);
54 module_param_named(loopback, ipwireless_loopback, int, 0);
55 module_param_named(out_queue, ipwireless_out_queue, int, 0);
56 MODULE_PARM_DESC(debug, "switch on debug messages [0]");
57 MODULE_PARM_DESC(loopback,
58                 "debug: enable ras_raw channel [0]");
59 MODULE_PARM_DESC(out_queue, "debug: set size of outgoing PPP queue [10]");
60
61 /* Executes in process context. */
62 static void signalled_reboot_work(struct work_struct *work_reboot)
63 {
64         struct ipw_dev *ipw = container_of(work_reboot, struct ipw_dev,
65                         work_reboot);
66         struct pcmcia_device *link = ipw->link;
67         pcmcia_reset_card(link->socket);
68 }
69
70 static void signalled_reboot_callback(void *callback_data)
71 {
72         struct ipw_dev *ipw = (struct ipw_dev *) callback_data;
73
74         /* Delegate to process context. */
75         schedule_work(&ipw->work_reboot);
76 }
77
78 static int ipwireless_probe(struct pcmcia_device *p_dev,
79                             cistpl_cftable_entry_t *cfg,
80                             cistpl_cftable_entry_t *dflt,
81                             unsigned int vcc,
82                             void *priv_data)
83 {
84         struct ipw_dev *ipw = priv_data;
85         struct resource *io_resource;
86         int ret;
87
88         p_dev->resource[0]->flags |= IO_DATA_PATH_WIDTH_AUTO;
89         p_dev->resource[0]->start = cfg->io.win[0].base;
90         p_dev->resource[0]->end = cfg->io.win[0].len;
91
92         /* 0x40 causes it to generate level mode interrupts. */
93         /* 0x04 enables IREQ pin. */
94         p_dev->config_index = cfg->index | 0x44;
95         p_dev->io_lines = 16;
96         ret = pcmcia_request_io(p_dev);
97         if (ret)
98                 return ret;
99
100         io_resource = request_region(p_dev->resource[0]->start,
101                                 resource_size(p_dev->resource[0]),
102                                 IPWIRELESS_PCCARD_NAME);
103
104         if (cfg->mem.nwin == 0)
105                 return 0;
106
107         p_dev->resource[2]->flags |=
108                 WIN_DATA_WIDTH_16 | WIN_MEMORY_TYPE_CM | WIN_ENABLE;
109         p_dev->resource[2]->start = cfg->mem.win[0].host_addr;
110         p_dev->resource[2]->end = cfg->mem.win[0].len;
111         if (p_dev->resource[2]->end < 0x1000)
112                 p_dev->resource[2]->end = 0x1000;
113
114         ret = pcmcia_request_window(p_dev, p_dev->resource[2], 0);
115         if (ret != 0)
116                 goto exit1;
117
118         ret = pcmcia_map_mem_page(p_dev, p_dev->resource[2],
119                                 cfg->mem.win[0].card_addr);
120         if (ret != 0)
121                 goto exit2;
122
123         ipw->is_v2_card = cfg->mem.win[0].len == 0x100;
124
125         ipw->attr_memory = ioremap(p_dev->resource[2]->start,
126                                 resource_size(p_dev->resource[2]));
127         request_mem_region(p_dev->resource[2]->start,
128                         resource_size(p_dev->resource[2]),
129                         IPWIRELESS_PCCARD_NAME);
130
131         p_dev->resource[3]->flags |= WIN_DATA_WIDTH_16 | WIN_MEMORY_TYPE_AM |
132                                         WIN_ENABLE;
133         p_dev->resource[3]->end = 0; /* this used to be 0x1000 */
134         ret = pcmcia_request_window(p_dev, p_dev->resource[3], 0);
135         if (ret != 0)
136                 goto exit2;
137
138         ret = pcmcia_map_mem_page(p_dev, p_dev->resource[3], 0);
139         if (ret != 0)
140                 goto exit3;
141
142         ipw->attr_memory = ioremap(p_dev->resource[3]->start,
143                                 resource_size(p_dev->resource[3]));
144         request_mem_region(p_dev->resource[3]->start,
145                         resource_size(p_dev->resource[3]),
146                         IPWIRELESS_PCCARD_NAME);
147
148         return 0;
149
150 exit3:
151 exit2:
152         if (ipw->common_memory) {
153                 release_mem_region(p_dev->resource[2]->start,
154                                 resource_size(p_dev->resource[2]));
155                 iounmap(ipw->common_memory);
156         }
157 exit1:
158         release_resource(io_resource);
159         pcmcia_disable_device(p_dev);
160         return -1;
161 }
162
163 static int config_ipwireless(struct ipw_dev *ipw)
164 {
165         struct pcmcia_device *link = ipw->link;
166         int ret = 0;
167
168         ipw->is_v2_card = 0;
169
170         ret = pcmcia_loop_config(link, ipwireless_probe, ipw);
171         if (ret != 0)
172                 return ret;
173
174         link->config_flags |= CONF_ENABLE_IRQ;
175
176         INIT_WORK(&ipw->work_reboot, signalled_reboot_work);
177
178         ipwireless_init_hardware_v1(ipw->hardware, link->resource[0]->start,
179                                     ipw->attr_memory, ipw->common_memory,
180                                     ipw->is_v2_card, signalled_reboot_callback,
181                                     ipw);
182
183         ret = pcmcia_request_irq(link, ipwireless_interrupt);
184         if (ret != 0)
185                 goto exit;
186
187         printk(KERN_INFO IPWIRELESS_PCCARD_NAME ": Card type %s\n",
188                         ipw->is_v2_card ? "V2/V3" : "V1");
189         printk(KERN_INFO IPWIRELESS_PCCARD_NAME
190                 ": I/O ports %pR, irq %d\n", link->resource[0],
191                         (unsigned int) link->irq);
192         if (ipw->attr_memory && ipw->common_memory)
193                 printk(KERN_INFO IPWIRELESS_PCCARD_NAME
194                         ": attr memory %pR, common memory %pR\n",
195                         link->resource[3],
196                         link->resource[2]);
197
198         ipw->network = ipwireless_network_create(ipw->hardware);
199         if (!ipw->network)
200                 goto exit;
201
202         ipw->tty = ipwireless_tty_create(ipw->hardware, ipw->network);
203         if (!ipw->tty)
204                 goto exit;
205
206         ipwireless_init_hardware_v2_v3(ipw->hardware);
207
208         /*
209          * Do the RequestConfiguration last, because it enables interrupts.
210          * Then we don't get any interrupts before we're ready for them.
211          */
212         ret = pcmcia_enable_device(link);
213         if (ret != 0)
214                 goto exit;
215
216         return 0;
217
218 exit:
219         if (ipw->common_memory) {
220                 release_mem_region(link->resource[2]->start,
221                                 resource_size(link->resource[2]));
222                 iounmap(ipw->common_memory);
223         }
224         if (ipw->attr_memory) {
225                 release_mem_region(link->resource[3]->start,
226                                 resource_size(link->resource[3]));
227                 iounmap(ipw->attr_memory);
228         }
229         pcmcia_disable_device(link);
230         return -1;
231 }
232
233 static void release_ipwireless(struct ipw_dev *ipw)
234 {
235         if (ipw->common_memory) {
236                 release_mem_region(ipw->link->resource[2]->start,
237                                 resource_size(ipw->link->resource[2]));
238                 iounmap(ipw->common_memory);
239         }
240         if (ipw->attr_memory) {
241                 release_mem_region(ipw->link->resource[3]->start,
242                                 resource_size(ipw->link->resource[3]));
243                 iounmap(ipw->attr_memory);
244         }
245         pcmcia_disable_device(ipw->link);
246 }
247
248 /*
249  * ipwireless_attach() creates an "instance" of the driver, allocating
250  * local data structures for one device (one interface).  The device
251  * is registered with Card Services.
252  *
253  * The pcmcia_device structure is initialized, but we don't actually
254  * configure the card at this point -- we wait until we receive a
255  * card insertion event.
256  */
257 static int ipwireless_attach(struct pcmcia_device *link)
258 {
259         struct ipw_dev *ipw;
260         int ret;
261
262         ipw = kzalloc(sizeof(struct ipw_dev), GFP_KERNEL);
263         if (!ipw)
264                 return -ENOMEM;
265
266         ipw->link = link;
267         link->priv = ipw;
268
269         ipw->hardware = ipwireless_hardware_create();
270         if (!ipw->hardware) {
271                 kfree(ipw);
272                 return -ENOMEM;
273         }
274         /* RegisterClient will call config_ipwireless */
275
276         ret = config_ipwireless(ipw);
277
278         if (ret != 0) {
279                 ipwireless_detach(link);
280                 return ret;
281         }
282
283         return 0;
284 }
285
286 /*
287  * This deletes a driver "instance".  The device is de-registered with
288  * Card Services.  If it has been released, all local data structures
289  * are freed.  Otherwise, the structures will be freed when the device
290  * is released.
291  */
292 static void ipwireless_detach(struct pcmcia_device *link)
293 {
294         struct ipw_dev *ipw = link->priv;
295
296         release_ipwireless(ipw);
297
298         if (ipw->tty != NULL)
299                 ipwireless_tty_free(ipw->tty);
300         if (ipw->network != NULL)
301                 ipwireless_network_free(ipw->network);
302         if (ipw->hardware != NULL)
303                 ipwireless_hardware_free(ipw->hardware);
304         kfree(ipw);
305 }
306
307 static struct pcmcia_driver me = {
308         .owner          = THIS_MODULE,
309         .probe          = ipwireless_attach,
310         .remove         = ipwireless_detach,
311         .drv = { .name  = IPWIRELESS_PCCARD_NAME },
312         .id_table       = ipw_ids
313 };
314
315 /*
316  * Module insertion : initialisation of the module.
317  * Register the card with cardmgr...
318  */
319 static int __init init_ipwireless(void)
320 {
321         int ret;
322
323         printk(KERN_INFO IPWIRELESS_PCCARD_NAME " "
324                IPWIRELESS_PCMCIA_VERSION " by " IPWIRELESS_PCMCIA_AUTHOR "\n");
325
326         ret = ipwireless_tty_init();
327         if (ret != 0)
328                 return ret;
329
330         ret = pcmcia_register_driver(&me);
331         if (ret != 0)
332                 ipwireless_tty_release();
333
334         return ret;
335 }
336
337 /*
338  * Module removal
339  */
340 static void __exit exit_ipwireless(void)
341 {
342         printk(KERN_INFO IPWIRELESS_PCCARD_NAME " "
343                         IPWIRELESS_PCMCIA_VERSION " removed\n");
344
345         pcmcia_unregister_driver(&me);
346         ipwireless_tty_release();
347 }
348
349 module_init(init_ipwireless);
350 module_exit(exit_ipwireless);
351
352 MODULE_AUTHOR(IPWIRELESS_PCMCIA_AUTHOR);
353 MODULE_DESCRIPTION(IPWIRELESS_PCCARD_NAME " " IPWIRELESS_PCMCIA_VERSION);
354 MODULE_LICENSE("GPL");