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