Merge git://git.kernel.org/pub/scm/linux/kernel/git/hskinnemoen/avr32-2.6
[pandora-kernel.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 #include <pcmcia/cs.h>
36
37 static struct pcmcia_device_id ipw_ids[] = {
38         PCMCIA_DEVICE_MANF_CARD(0x02f2, 0x0100),
39         PCMCIA_DEVICE_MANF_CARD(0x02f2, 0x0200),
40         PCMCIA_DEVICE_NULL
41 };
42 MODULE_DEVICE_TABLE(pcmcia, ipw_ids);
43
44 static void ipwireless_detach(struct pcmcia_device *link);
45
46 /*
47  * Module params
48  */
49 /* Debug mode: more verbose, print sent/recv bytes */
50 int ipwireless_debug;
51 int ipwireless_loopback;
52 int ipwireless_out_queue = 10;
53
54 module_param_named(debug, ipwireless_debug, int, 0);
55 module_param_named(loopback, ipwireless_loopback, int, 0);
56 module_param_named(out_queue, ipwireless_out_queue, int, 0);
57 MODULE_PARM_DESC(debug, "switch on debug messages [0]");
58 MODULE_PARM_DESC(loopback,
59                 "debug: enable ras_raw channel [0]");
60 MODULE_PARM_DESC(out_queue, "debug: set size of outgoing PPP queue [10]");
61
62 /* Executes in process context. */
63 static void signalled_reboot_work(struct work_struct *work_reboot)
64 {
65         struct ipw_dev *ipw = container_of(work_reboot, struct ipw_dev,
66                         work_reboot);
67         struct pcmcia_device *link = ipw->link;
68         pcmcia_reset_card(link->socket);
69 }
70
71 static void signalled_reboot_callback(void *callback_data)
72 {
73         struct ipw_dev *ipw = (struct ipw_dev *) callback_data;
74
75         /* Delegate to process context. */
76         schedule_work(&ipw->work_reboot);
77 }
78
79 static int ipwireless_probe(struct pcmcia_device *p_dev,
80                             cistpl_cftable_entry_t *cfg,
81                             cistpl_cftable_entry_t *dflt,
82                             unsigned int vcc,
83                             void *priv_data)
84 {
85         struct ipw_dev *ipw = priv_data;
86         struct resource *io_resource;
87         memreq_t memreq_attr_memory;
88         memreq_t memreq_common_memory;
89         int ret;
90
91         p_dev->io.Attributes1 = IO_DATA_PATH_WIDTH_AUTO;
92         p_dev->io.BasePort1 = cfg->io.win[0].base;
93         p_dev->io.NumPorts1 = cfg->io.win[0].len;
94         p_dev->io.IOAddrLines = 16;
95
96         /* 0x40 causes it to generate level mode interrupts. */
97         /* 0x04 enables IREQ pin. */
98         p_dev->conf.ConfigIndex = cfg->index | 0x44;
99         ret = pcmcia_request_io(p_dev, &p_dev->io);
100         if (ret)
101                 return ret;
102
103         io_resource = request_region(p_dev->io.BasePort1, p_dev->io.NumPorts1,
104                                 IPWIRELESS_PCCARD_NAME);
105
106         if (cfg->mem.nwin == 0)
107                 return 0;
108
109         ipw->request_common_memory.Attributes =
110                 WIN_DATA_WIDTH_16 | WIN_MEMORY_TYPE_CM | WIN_ENABLE;
111         ipw->request_common_memory.Base = cfg->mem.win[0].host_addr;
112         ipw->request_common_memory.Size = cfg->mem.win[0].len;
113         if (ipw->request_common_memory.Size < 0x1000)
114                 ipw->request_common_memory.Size = 0x1000;
115         ipw->request_common_memory.AccessSpeed = 0;
116
117         ret = pcmcia_request_window(p_dev, &ipw->request_common_memory,
118                                 &ipw->handle_common_memory);
119
120         if (ret != 0)
121                 goto exit1;
122
123         memreq_common_memory.CardOffset = cfg->mem.win[0].card_addr;
124         memreq_common_memory.Page = 0;
125
126         ret = pcmcia_map_mem_page(p_dev, ipw->handle_common_memory,
127                                 &memreq_common_memory);
128
129         if (ret != 0)
130                 goto exit2;
131
132         ipw->is_v2_card = cfg->mem.win[0].len == 0x100;
133
134         ipw->common_memory = ioremap(ipw->request_common_memory.Base,
135                                 ipw->request_common_memory.Size);
136         request_mem_region(ipw->request_common_memory.Base,
137                         ipw->request_common_memory.Size,
138                         IPWIRELESS_PCCARD_NAME);
139
140         ipw->request_attr_memory.Attributes =
141                 WIN_DATA_WIDTH_16 | WIN_MEMORY_TYPE_AM | WIN_ENABLE;
142         ipw->request_attr_memory.Base = 0;
143         ipw->request_attr_memory.Size = 0;      /* this used to be 0x1000 */
144         ipw->request_attr_memory.AccessSpeed = 0;
145
146         ret = pcmcia_request_window(p_dev, &ipw->request_attr_memory,
147                                 &ipw->handle_attr_memory);
148
149         if (ret != 0)
150                 goto exit2;
151
152         memreq_attr_memory.CardOffset = 0;
153         memreq_attr_memory.Page = 0;
154
155         ret = pcmcia_map_mem_page(p_dev, ipw->handle_attr_memory,
156                                 &memreq_attr_memory);
157
158         if (ret != 0)
159                 goto exit3;
160
161         ipw->attr_memory = ioremap(ipw->request_attr_memory.Base,
162                                 ipw->request_attr_memory.Size);
163         request_mem_region(ipw->request_attr_memory.Base,
164                         ipw->request_attr_memory.Size, IPWIRELESS_PCCARD_NAME);
165
166         return 0;
167
168 exit3:
169         pcmcia_release_window(p_dev, ipw->handle_attr_memory);
170 exit2:
171         if (ipw->common_memory) {
172                 release_mem_region(ipw->request_common_memory.Base,
173                                 ipw->request_common_memory.Size);
174                 iounmap(ipw->common_memory);
175                 pcmcia_release_window(p_dev, ipw->handle_common_memory);
176         } else
177                 pcmcia_release_window(p_dev, ipw->handle_common_memory);
178 exit1:
179         release_resource(io_resource);
180         pcmcia_disable_device(p_dev);
181         return -1;
182 }
183
184 static int config_ipwireless(struct ipw_dev *ipw)
185 {
186         struct pcmcia_device *link = ipw->link;
187         int ret = 0;
188
189         ipw->is_v2_card = 0;
190
191         ret = pcmcia_loop_config(link, ipwireless_probe, ipw);
192         if (ret != 0)
193                 return ret;
194
195         link->conf.Attributes = CONF_ENABLE_IRQ;
196         link->conf.IntType = INT_MEMORY_AND_IO;
197
198         link->irq.Attributes = IRQ_TYPE_DYNAMIC_SHARING;
199         link->irq.Handler = ipwireless_interrupt;
200
201         INIT_WORK(&ipw->work_reboot, signalled_reboot_work);
202
203         ipwireless_init_hardware_v1(ipw->hardware, link->io.BasePort1,
204                                     ipw->attr_memory, ipw->common_memory,
205                                     ipw->is_v2_card, signalled_reboot_callback,
206                                     ipw);
207
208         ret = pcmcia_request_irq(link, &link->irq);
209
210         if (ret != 0)
211                 goto exit;
212
213         printk(KERN_INFO IPWIRELESS_PCCARD_NAME ": Card type %s\n",
214                         ipw->is_v2_card ? "V2/V3" : "V1");
215         printk(KERN_INFO IPWIRELESS_PCCARD_NAME
216                         ": I/O ports 0x%04x-0x%04x, irq %d\n",
217                         (unsigned int) link->io.BasePort1,
218                         (unsigned int) (link->io.BasePort1 +
219                                 link->io.NumPorts1 - 1),
220                         (unsigned int) link->irq.AssignedIRQ);
221         if (ipw->attr_memory && ipw->common_memory)
222                 printk(KERN_INFO IPWIRELESS_PCCARD_NAME
223                         ": attr memory 0x%08lx-0x%08lx, common memory 0x%08lx-0x%08lx\n",
224                         ipw->request_attr_memory.Base,
225                         ipw->request_attr_memory.Base
226                         + ipw->request_attr_memory.Size - 1,
227                         ipw->request_common_memory.Base,
228                         ipw->request_common_memory.Base
229                         + ipw->request_common_memory.Size - 1);
230
231         ipw->network = ipwireless_network_create(ipw->hardware);
232         if (!ipw->network)
233                 goto exit;
234
235         ipw->tty = ipwireless_tty_create(ipw->hardware, ipw->network,
236                         ipw->nodes);
237         if (!ipw->tty)
238                 goto exit;
239
240         ipwireless_init_hardware_v2_v3(ipw->hardware);
241
242         /*
243          * Do the RequestConfiguration last, because it enables interrupts.
244          * Then we don't get any interrupts before we're ready for them.
245          */
246         ret = pcmcia_request_configuration(link, &link->conf);
247
248         if (ret != 0)
249                 goto exit;
250
251         link->dev_node = &ipw->nodes[0];
252
253         return 0;
254
255 exit:
256         if (ipw->attr_memory) {
257                 release_mem_region(ipw->request_attr_memory.Base,
258                                 ipw->request_attr_memory.Size);
259                 iounmap(ipw->attr_memory);
260                 pcmcia_release_window(link, ipw->handle_attr_memory);
261         }
262         if (ipw->common_memory) {
263                 release_mem_region(ipw->request_common_memory.Base,
264                                 ipw->request_common_memory.Size);
265                 iounmap(ipw->common_memory);
266                 pcmcia_release_window(link, ipw->handle_common_memory);
267         }
268         pcmcia_disable_device(link);
269         return -1;
270 }
271
272 static void release_ipwireless(struct ipw_dev *ipw)
273 {
274         pcmcia_disable_device(ipw->link);
275
276         if (ipw->common_memory) {
277                 release_mem_region(ipw->request_common_memory.Base,
278                                 ipw->request_common_memory.Size);
279                 iounmap(ipw->common_memory);
280         }
281         if (ipw->attr_memory) {
282                 release_mem_region(ipw->request_attr_memory.Base,
283                                 ipw->request_attr_memory.Size);
284                 iounmap(ipw->attr_memory);
285         }
286         if (ipw->common_memory)
287                 pcmcia_release_window(ipw->link, ipw->handle_common_memory);
288         if (ipw->attr_memory)
289                 pcmcia_release_window(ipw->link, ipw->handle_attr_memory);
290
291         /* Break the link with Card Services */
292         pcmcia_disable_device(ipw->link);
293 }
294
295 /*
296  * ipwireless_attach() creates an "instance" of the driver, allocating
297  * local data structures for one device (one interface).  The device
298  * is registered with Card Services.
299  *
300  * The pcmcia_device structure is initialized, but we don't actually
301  * configure the card at this point -- we wait until we receive a
302  * card insertion event.
303  */
304 static int ipwireless_attach(struct pcmcia_device *link)
305 {
306         struct ipw_dev *ipw;
307         int ret;
308
309         ipw = kzalloc(sizeof(struct ipw_dev), GFP_KERNEL);
310         if (!ipw)
311                 return -ENOMEM;
312
313         ipw->link = link;
314         link->priv = ipw;
315
316         /* Link this device into our device list. */
317         link->dev_node = &ipw->nodes[0];
318
319         ipw->hardware = ipwireless_hardware_create();
320         if (!ipw->hardware) {
321                 kfree(ipw);
322                 return -ENOMEM;
323         }
324         /* RegisterClient will call config_ipwireless */
325
326         ret = config_ipwireless(ipw);
327
328         if (ret != 0) {
329                 ipwireless_detach(link);
330                 return ret;
331         }
332
333         return 0;
334 }
335
336 /*
337  * This deletes a driver "instance".  The device is de-registered with
338  * Card Services.  If it has been released, all local data structures
339  * are freed.  Otherwise, the structures will be freed when the device
340  * is released.
341  */
342 static void ipwireless_detach(struct pcmcia_device *link)
343 {
344         struct ipw_dev *ipw = link->priv;
345
346         release_ipwireless(ipw);
347
348         if (ipw->tty != NULL)
349                 ipwireless_tty_free(ipw->tty);
350         if (ipw->network != NULL)
351                 ipwireless_network_free(ipw->network);
352         if (ipw->hardware != NULL)
353                 ipwireless_hardware_free(ipw->hardware);
354         kfree(ipw);
355 }
356
357 static struct pcmcia_driver me = {
358         .owner          = THIS_MODULE,
359         .probe          = ipwireless_attach,
360         .remove         = ipwireless_detach,
361         .drv = { .name  = IPWIRELESS_PCCARD_NAME },
362         .id_table       = ipw_ids
363 };
364
365 /*
366  * Module insertion : initialisation of the module.
367  * Register the card with cardmgr...
368  */
369 static int __init init_ipwireless(void)
370 {
371         int ret;
372
373         printk(KERN_INFO IPWIRELESS_PCCARD_NAME " "
374                IPWIRELESS_PCMCIA_VERSION " by " IPWIRELESS_PCMCIA_AUTHOR "\n");
375
376         ret = ipwireless_tty_init();
377         if (ret != 0)
378                 return ret;
379
380         ret = pcmcia_register_driver(&me);
381         if (ret != 0)
382                 ipwireless_tty_release();
383
384         return ret;
385 }
386
387 /*
388  * Module removal
389  */
390 static void __exit exit_ipwireless(void)
391 {
392         printk(KERN_INFO IPWIRELESS_PCCARD_NAME " "
393                         IPWIRELESS_PCMCIA_VERSION " removed\n");
394
395         pcmcia_unregister_driver(&me);
396         ipwireless_tty_release();
397 }
398
399 module_init(init_ipwireless);
400 module_exit(exit_ipwireless);
401
402 MODULE_AUTHOR(IPWIRELESS_PCMCIA_AUTHOR);
403 MODULE_DESCRIPTION(IPWIRELESS_PCCARD_NAME " " IPWIRELESS_PCMCIA_VERSION);
404 MODULE_LICENSE("GPL");