Merge branch 'for-linus' of git://git.kernel.dk/linux-2.6-block
[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/version.h>
32 #include <pcmcia/cisreg.h>
33 #include <pcmcia/device_id.h>
34 #include <pcmcia/ss.h>
35 #include <pcmcia/ds.h>
36 #include <pcmcia/cs.h>
37
38 static struct pcmcia_device_id ipw_ids[] = {
39         PCMCIA_DEVICE_MANF_CARD(0x02f2, 0x0100),
40         PCMCIA_DEVICE_MANF_CARD(0x02f2, 0x0200),
41         PCMCIA_DEVICE_NULL
42 };
43 MODULE_DEVICE_TABLE(pcmcia, ipw_ids);
44
45 static void ipwireless_detach(struct pcmcia_device *link);
46
47 /*
48  * Module params
49  */
50 /* Debug mode: more verbose, print sent/recv bytes */
51 int ipwireless_debug;
52 int ipwireless_loopback;
53 int ipwireless_out_queue = 1;
54
55 module_param_named(debug, ipwireless_debug, int, 0);
56 module_param_named(loopback, ipwireless_loopback, int, 0);
57 module_param_named(out_queue, ipwireless_out_queue, int, 0);
58 MODULE_PARM_DESC(debug, "switch on debug messages [0]");
59 MODULE_PARM_DESC(loopback,
60                 "debug: enable ras_raw channel [0]");
61 MODULE_PARM_DESC(out_queue, "debug: set size of outgoing queue [1]");
62
63 /* Executes in process context. */
64 static void signalled_reboot_work(struct work_struct *work_reboot)
65 {
66         struct ipw_dev *ipw = container_of(work_reboot, struct ipw_dev,
67                         work_reboot);
68         struct pcmcia_device *link = ipw->link;
69         int ret = pccard_reset_card(link->socket);
70
71         if (ret != CS_SUCCESS)
72                 cs_error(link, ResetCard, ret);
73 }
74
75 static void signalled_reboot_callback(void *callback_data)
76 {
77         struct ipw_dev *ipw = (struct ipw_dev *) callback_data;
78
79         /* Delegate to process context. */
80         schedule_work(&ipw->work_reboot);
81 }
82
83 static int config_ipwireless(struct ipw_dev *ipw)
84 {
85         struct pcmcia_device *link = ipw->link;
86         int ret;
87         config_info_t conf;
88         tuple_t tuple;
89         unsigned short buf[64];
90         cisparse_t parse;
91         unsigned short cor_value;
92         win_req_t request_attr_memory;
93         win_req_t request_common_memory;
94         memreq_t memreq_attr_memory;
95         memreq_t memreq_common_memory;
96
97         ipw->is_v2_card = 0;
98
99         tuple.Attributes = 0;
100         tuple.TupleData = (cisdata_t *) buf;
101         tuple.TupleDataMax = sizeof(buf);
102         tuple.TupleOffset = 0;
103
104         tuple.DesiredTuple = RETURN_FIRST_TUPLE;
105
106         ret = pcmcia_get_first_tuple(link, &tuple);
107
108         while (ret == 0) {
109                 ret = pcmcia_get_tuple_data(link, &tuple);
110
111                 if (ret != CS_SUCCESS) {
112                         cs_error(link, GetTupleData, ret);
113                         goto exit0;
114                 }
115                 ret = pcmcia_get_next_tuple(link, &tuple);
116         }
117
118         tuple.DesiredTuple = CISTPL_CFTABLE_ENTRY;
119
120         ret = pcmcia_get_first_tuple(link, &tuple);
121
122         if (ret != CS_SUCCESS) {
123                 cs_error(link, GetFirstTuple, ret);
124                 goto exit0;
125         }
126
127         ret = pcmcia_get_tuple_data(link, &tuple);
128
129         if (ret != CS_SUCCESS) {
130                 cs_error(link, GetTupleData, ret);
131                 goto exit0;
132         }
133
134         ret = pcmcia_parse_tuple(link, &tuple, &parse);
135
136         if (ret != CS_SUCCESS) {
137                 cs_error(link, ParseTuple, ret);
138                 goto exit0;
139         }
140
141         link->io.Attributes1 = IO_DATA_PATH_WIDTH_AUTO;
142         link->io.BasePort1 = parse.cftable_entry.io.win[0].base;
143         link->io.NumPorts1 = parse.cftable_entry.io.win[0].len;
144         link->io.IOAddrLines = 16;
145
146         link->irq.IRQInfo1 = parse.cftable_entry.irq.IRQInfo1;
147
148         /* 0x40 causes it to generate level mode interrupts. */
149         /* 0x04 enables IREQ pin. */
150         cor_value = parse.cftable_entry.index | 0x44;
151         link->conf.ConfigIndex = cor_value;
152
153         /* IRQ and I/O settings */
154         tuple.DesiredTuple = CISTPL_CONFIG;
155
156         ret = pcmcia_get_first_tuple(link, &tuple);
157
158         if (ret != CS_SUCCESS) {
159                 cs_error(link, GetFirstTuple, ret);
160                 goto exit0;
161         }
162
163         ret = pcmcia_get_tuple_data(link, &tuple);
164
165         if (ret != CS_SUCCESS) {
166                 cs_error(link, GetTupleData, ret);
167                 goto exit0;
168         }
169
170         ret = pcmcia_parse_tuple(link, &tuple, &parse);
171
172         if (ret != CS_SUCCESS) {
173                 cs_error(link, GetTupleData, ret);
174                 goto exit0;
175         }
176         link->conf.Attributes = CONF_ENABLE_IRQ;
177         link->conf.ConfigBase = parse.config.base;
178         link->conf.Present = parse.config.rmask[0];
179         link->conf.IntType = INT_MEMORY_AND_IO;
180
181         link->irq.Attributes = IRQ_TYPE_DYNAMIC_SHARING | IRQ_HANDLE_PRESENT;
182         link->irq.Handler = ipwireless_interrupt;
183         link->irq.Instance = ipw->hardware;
184
185         ret = pcmcia_request_io(link, &link->io);
186
187         if (ret != CS_SUCCESS) {
188                 cs_error(link, RequestIO, ret);
189                 goto exit0;
190         }
191
192         /* memory settings */
193
194         tuple.DesiredTuple = CISTPL_CFTABLE_ENTRY;
195
196         ret = pcmcia_get_first_tuple(link, &tuple);
197
198         if (ret != CS_SUCCESS) {
199                 cs_error(link, GetFirstTuple, ret);
200                 goto exit1;
201         }
202
203         ret = pcmcia_get_tuple_data(link, &tuple);
204
205         if (ret != CS_SUCCESS) {
206                 cs_error(link, GetTupleData, ret);
207                 goto exit1;
208         }
209
210         ret = pcmcia_parse_tuple(link, &tuple, &parse);
211
212         if (ret != CS_SUCCESS) {
213                 cs_error(link, ParseTuple, ret);
214                 goto exit1;
215         }
216
217         if (parse.cftable_entry.mem.nwin > 0) {
218                 request_common_memory.Attributes =
219                         WIN_DATA_WIDTH_16 | WIN_MEMORY_TYPE_CM | WIN_ENABLE;
220                 request_common_memory.Base =
221                         parse.cftable_entry.mem.win[0].host_addr;
222                 request_common_memory.Size = parse.cftable_entry.mem.win[0].len;
223                 if (request_common_memory.Size < 0x1000)
224                         request_common_memory.Size = 0x1000;
225                 request_common_memory.AccessSpeed = 0;
226
227                 ret = pcmcia_request_window(&link, &request_common_memory,
228                                 &ipw->handle_common_memory);
229
230                 if (ret != CS_SUCCESS) {
231                         cs_error(link, RequestWindow, ret);
232                         goto exit1;
233                 }
234
235                 memreq_common_memory.CardOffset =
236                         parse.cftable_entry.mem.win[0].card_addr;
237                 memreq_common_memory.Page = 0;
238
239                 ret = pcmcia_map_mem_page(ipw->handle_common_memory,
240                                 &memreq_common_memory);
241
242                 if (ret != CS_SUCCESS) {
243                         cs_error(link, MapMemPage, ret);
244                         goto exit1;
245                 }
246
247                 ipw->is_v2_card =
248                         parse.cftable_entry.mem.win[0].len == 0x100;
249
250                 ipw->common_memory = ioremap(request_common_memory.Base,
251                                 request_common_memory.Size);
252
253                 request_attr_memory.Attributes =
254                         WIN_DATA_WIDTH_16 | WIN_MEMORY_TYPE_AM | WIN_ENABLE;
255                 request_attr_memory.Base = 0;
256                 request_attr_memory.Size = 0;   /* this used to be 0x1000 */
257                 request_attr_memory.AccessSpeed = 0;
258
259                 ret = pcmcia_request_window(&link, &request_attr_memory,
260                                 &ipw->handle_attr_memory);
261
262                 if (ret != CS_SUCCESS) {
263                         cs_error(link, RequestWindow, ret);
264                         goto exit2;
265                 }
266
267                 memreq_attr_memory.CardOffset = 0;
268                 memreq_attr_memory.Page = 0;
269
270                 ret = pcmcia_map_mem_page(ipw->handle_attr_memory,
271                                 &memreq_attr_memory);
272
273                 if (ret != CS_SUCCESS) {
274                         cs_error(link, MapMemPage, ret);
275                         goto exit2;
276                 }
277
278                 ipw->attr_memory = ioremap(request_attr_memory.Base,
279                                 request_attr_memory.Size);
280         }
281
282         INIT_WORK(&ipw->work_reboot, signalled_reboot_work);
283
284         ipwireless_init_hardware_v1(ipw->hardware, link->io.BasePort1,
285                                     ipw->attr_memory, ipw->common_memory,
286                                     ipw->is_v2_card, signalled_reboot_callback,
287                                     ipw);
288
289         ret = pcmcia_request_irq(link, &link->irq);
290
291         if (ret != CS_SUCCESS) {
292                 cs_error(link, RequestIRQ, ret);
293                 goto exit3;
294         }
295
296         /* Look up current Vcc */
297
298         ret = pcmcia_get_configuration_info(link, &conf);
299
300         if (ret != CS_SUCCESS) {
301                 cs_error(link, GetConfigurationInfo, ret);
302                 goto exit4;
303         }
304
305         printk(KERN_INFO IPWIRELESS_PCCARD_NAME ": Card type %s\n",
306                         ipw->is_v2_card ? "V2/V3" : "V1");
307         printk(KERN_INFO IPWIRELESS_PCCARD_NAME
308                         ": I/O ports 0x%04x-0x%04x, irq %d\n",
309                         (unsigned int) link->io.BasePort1,
310                         (unsigned int) (link->io.BasePort1 +
311                                 link->io.NumPorts1 - 1),
312                         (unsigned int) link->irq.AssignedIRQ);
313         if (ipw->attr_memory && ipw->common_memory)
314                 printk(KERN_INFO IPWIRELESS_PCCARD_NAME
315                                 ": attr memory 0x%08lx-0x%08lx, "
316                                 "common memory 0x%08lx-0x%08lx\n",
317                                 request_attr_memory.Base,
318                                 request_attr_memory.Base
319                                 + request_attr_memory.Size - 1,
320                                 request_common_memory.Base,
321                                 request_common_memory.Base
322                                 + request_common_memory.Size - 1);
323
324         ipw->network = ipwireless_network_create(ipw->hardware);
325         if (!ipw->network)
326                 goto exit3;
327
328         ipw->tty = ipwireless_tty_create(ipw->hardware, ipw->network,
329                         ipw->nodes);
330         if (!ipw->tty)
331                 goto exit3;
332
333         ipwireless_init_hardware_v2_v3(ipw->hardware);
334
335         /*
336          * Do the RequestConfiguration last, because it enables interrupts.
337          * Then we don't get any interrupts before we're ready for them.
338          */
339         ret = pcmcia_request_configuration(link, &link->conf);
340
341         if (ret != CS_SUCCESS) {
342                 cs_error(link, RequestConfiguration, ret);
343                 goto exit4;
344         }
345
346         link->dev_node = &ipw->nodes[0];
347
348         return 0;
349
350 exit4:
351         pcmcia_disable_device(link);
352 exit3:
353         if (ipw->attr_memory) {
354                 iounmap(ipw->attr_memory);
355                 pcmcia_release_window(ipw->handle_attr_memory);
356                 pcmcia_disable_device(link);
357         }
358 exit2:
359         if (ipw->common_memory) {
360                 iounmap(ipw->common_memory);
361                 pcmcia_release_window(ipw->handle_common_memory);
362         }
363 exit1:
364         pcmcia_disable_device(link);
365 exit0:
366         return -1;
367 }
368
369 static void release_ipwireless(struct ipw_dev *ipw)
370 {
371         struct pcmcia_device *link = ipw->link;
372
373         pcmcia_disable_device(link);
374
375         if (ipw->common_memory)
376                 iounmap(ipw->common_memory);
377         if (ipw->attr_memory)
378                 iounmap(ipw->attr_memory);
379         if (ipw->common_memory)
380                 pcmcia_release_window(ipw->handle_common_memory);
381         if (ipw->attr_memory)
382                 pcmcia_release_window(ipw->handle_attr_memory);
383         pcmcia_disable_device(link);
384 }
385
386 /*
387  * ipwireless_attach() creates an "instance" of the driver, allocating
388  * local data structures for one device (one interface).  The device
389  * is registered with Card Services.
390  *
391  * The pcmcia_device structure is initialized, but we don't actually
392  * configure the card at this point -- we wait until we receive a
393  * card insertion event.
394  */
395 static int ipwireless_attach(struct pcmcia_device *link)
396 {
397         struct ipw_dev *ipw;
398         int ret;
399
400         ipw = kzalloc(sizeof(struct ipw_dev), GFP_KERNEL);
401         if (!ipw)
402                 return -ENOMEM;
403
404         ipw->link = link;
405         link->priv = ipw;
406         link->irq.Instance = ipw;
407
408         /* Link this device into our device list. */
409         link->dev_node = &ipw->nodes[0];
410
411         ipw->hardware = ipwireless_hardware_create();
412         if (!ipw->hardware) {
413                 kfree(ipw);
414                 return -ENOMEM;
415         }
416         /* RegisterClient will call config_ipwireless */
417
418         ret = config_ipwireless(ipw);
419
420         if (ret != 0) {
421                 cs_error(link, RegisterClient, ret);
422                 ipwireless_detach(link);
423                 return ret;
424         }
425
426         return 0;
427 }
428
429 /*
430  * This deletes a driver "instance".  The device is de-registered with
431  * Card Services.  If it has been released, all local data structures
432  * are freed.  Otherwise, the structures will be freed when the device
433  * is released.
434  */
435 static void ipwireless_detach(struct pcmcia_device *link)
436 {
437         struct ipw_dev *ipw = link->priv;
438
439         release_ipwireless(ipw);
440
441         /* Break the link with Card Services */
442         if (link)
443                 pcmcia_disable_device(link);
444
445         if (ipw->tty != NULL)
446                 ipwireless_tty_free(ipw->tty);
447         if (ipw->network != NULL)
448                 ipwireless_network_free(ipw->network);
449         if (ipw->hardware != NULL)
450                 ipwireless_hardware_free(ipw->hardware);
451         kfree(ipw);
452 }
453
454 static struct pcmcia_driver me = {
455         .owner          = THIS_MODULE,
456         .probe          = ipwireless_attach,
457         .remove         = ipwireless_detach,
458         .drv = { .name  = IPWIRELESS_PCCARD_NAME },
459         .id_table       = ipw_ids
460 };
461
462 /*
463  * Module insertion : initialisation of the module.
464  * Register the card with cardmgr...
465  */
466 static int __init init_ipwireless(void)
467 {
468         int ret;
469
470         printk(KERN_INFO IPWIRELESS_PCCARD_NAME " "
471                IPWIRELESS_PCMCIA_VERSION " by " IPWIRELESS_PCMCIA_AUTHOR "\n");
472
473         ret = ipwireless_tty_init();
474         if (ret != 0)
475                 return ret;
476
477         ret = pcmcia_register_driver(&me);
478         if (ret != 0)
479                 ipwireless_tty_release();
480
481         return ret;
482 }
483
484 /*
485  * Module removal
486  */
487 static void __exit exit_ipwireless(void)
488 {
489         printk(KERN_INFO IPWIRELESS_PCCARD_NAME " "
490                         IPWIRELESS_PCMCIA_VERSION " removed\n");
491
492         pcmcia_unregister_driver(&me);
493         ipwireless_tty_release();
494 }
495
496 module_init(init_ipwireless);
497 module_exit(exit_ipwireless);
498
499 MODULE_AUTHOR(IPWIRELESS_PCMCIA_AUTHOR);
500 MODULE_DESCRIPTION(IPWIRELESS_PCCARD_NAME " " IPWIRELESS_PCMCIA_VERSION);
501 MODULE_LICENSE("GPL");