Linux-libre 4.4.228-gnu
[librecmc/linux-libre.git] / drivers / tty / 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 const 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, void *priv_data)
79 {
80         struct ipw_dev *ipw = priv_data;
81         int ret;
82
83         p_dev->resource[0]->flags &= ~IO_DATA_PATH_WIDTH;
84         p_dev->resource[0]->flags |= IO_DATA_PATH_WIDTH_AUTO;
85
86         /* 0x40 causes it to generate level mode interrupts. */
87         /* 0x04 enables IREQ pin. */
88         p_dev->config_index |= 0x44;
89         p_dev->io_lines = 16;
90         ret = pcmcia_request_io(p_dev);
91         if (ret)
92                 return ret;
93
94         if (!request_region(p_dev->resource[0]->start,
95                             resource_size(p_dev->resource[0]),
96                             IPWIRELESS_PCCARD_NAME)) {
97                 ret = -EBUSY;
98                 goto exit;
99         }
100
101         p_dev->resource[2]->flags |=
102                 WIN_DATA_WIDTH_16 | WIN_MEMORY_TYPE_CM | WIN_ENABLE;
103
104         ret = pcmcia_request_window(p_dev, p_dev->resource[2], 0);
105         if (ret != 0)
106                 goto exit1;
107
108         ret = pcmcia_map_mem_page(p_dev, p_dev->resource[2], p_dev->card_addr);
109         if (ret != 0)
110                 goto exit1;
111
112         ipw->is_v2_card = resource_size(p_dev->resource[2]) == 0x100;
113
114         ipw->common_memory = ioremap(p_dev->resource[2]->start,
115                                 resource_size(p_dev->resource[2]));
116         if (!ipw->common_memory) {
117                 ret = -ENOMEM;
118                 goto exit1;
119         }
120         if (!request_mem_region(p_dev->resource[2]->start,
121                                 resource_size(p_dev->resource[2]),
122                                 IPWIRELESS_PCCARD_NAME)) {
123                 ret = -EBUSY;
124                 goto exit2;
125         }
126
127         p_dev->resource[3]->flags |= WIN_DATA_WIDTH_16 | WIN_MEMORY_TYPE_AM |
128                                         WIN_ENABLE;
129         p_dev->resource[3]->end = 0; /* this used to be 0x1000 */
130         ret = pcmcia_request_window(p_dev, p_dev->resource[3], 0);
131         if (ret != 0)
132                 goto exit3;
133
134         ret = pcmcia_map_mem_page(p_dev, p_dev->resource[3], 0);
135         if (ret != 0)
136                 goto exit3;
137
138         ipw->attr_memory = ioremap(p_dev->resource[3]->start,
139                                 resource_size(p_dev->resource[3]));
140         if (!ipw->attr_memory) {
141                 ret = -ENOMEM;
142                 goto exit3;
143         }
144         if (!request_mem_region(p_dev->resource[3]->start,
145                                 resource_size(p_dev->resource[3]),
146                                 IPWIRELESS_PCCARD_NAME)) {
147                 ret = -EBUSY;
148                 goto exit4;
149         }
150
151         return 0;
152
153 exit4:
154         iounmap(ipw->attr_memory);
155 exit3:
156         release_mem_region(p_dev->resource[2]->start,
157                         resource_size(p_dev->resource[2]));
158 exit2:
159         iounmap(ipw->common_memory);
160 exit1:
161         release_region(p_dev->resource[0]->start,
162                        resource_size(p_dev->resource[0]));
163 exit:
164         pcmcia_disable_device(p_dev);
165         return ret;
166 }
167
168 static int config_ipwireless(struct ipw_dev *ipw)
169 {
170         struct pcmcia_device *link = ipw->link;
171         int ret = 0;
172
173         ipw->is_v2_card = 0;
174         link->config_flags |= CONF_AUTO_SET_IO | CONF_AUTO_SET_IOMEM |
175                 CONF_ENABLE_IRQ;
176
177         ret = pcmcia_loop_config(link, ipwireless_probe, ipw);
178         if (ret != 0)
179                 return ret;
180
181         INIT_WORK(&ipw->work_reboot, signalled_reboot_work);
182
183         ipwireless_init_hardware_v1(ipw->hardware, link->resource[0]->start,
184                                     ipw->attr_memory, ipw->common_memory,
185                                     ipw->is_v2_card, signalled_reboot_callback,
186                                     ipw);
187
188         ret = pcmcia_request_irq(link, ipwireless_interrupt);
189         if (ret != 0)
190                 goto exit;
191
192         printk(KERN_INFO IPWIRELESS_PCCARD_NAME ": Card type %s\n",
193                         ipw->is_v2_card ? "V2/V3" : "V1");
194         printk(KERN_INFO IPWIRELESS_PCCARD_NAME
195                 ": I/O ports %pR, irq %d\n", link->resource[0],
196                         (unsigned int) link->irq);
197         if (ipw->attr_memory && ipw->common_memory)
198                 printk(KERN_INFO IPWIRELESS_PCCARD_NAME
199                         ": attr memory %pR, common memory %pR\n",
200                         link->resource[3],
201                         link->resource[2]);
202
203         ipw->network = ipwireless_network_create(ipw->hardware);
204         if (!ipw->network)
205                 goto exit;
206
207         ipw->tty = ipwireless_tty_create(ipw->hardware, ipw->network);
208         if (!ipw->tty)
209                 goto exit;
210
211         ipwireless_init_hardware_v2_v3(ipw->hardware);
212
213         /*
214          * Do the RequestConfiguration last, because it enables interrupts.
215          * Then we don't get any interrupts before we're ready for them.
216          */
217         ret = pcmcia_enable_device(link);
218         if (ret != 0)
219                 goto exit;
220
221         return 0;
222
223 exit:
224         if (ipw->common_memory) {
225                 release_mem_region(link->resource[2]->start,
226                                 resource_size(link->resource[2]));
227                 iounmap(ipw->common_memory);
228         }
229         if (ipw->attr_memory) {
230                 release_mem_region(link->resource[3]->start,
231                                 resource_size(link->resource[3]));
232                 iounmap(ipw->attr_memory);
233         }
234         pcmcia_disable_device(link);
235         return -1;
236 }
237
238 static void release_ipwireless(struct ipw_dev *ipw)
239 {
240         release_region(ipw->link->resource[0]->start,
241                        resource_size(ipw->link->resource[0]));
242         if (ipw->common_memory) {
243                 release_mem_region(ipw->link->resource[2]->start,
244                                 resource_size(ipw->link->resource[2]));
245                 iounmap(ipw->common_memory);
246         }
247         if (ipw->attr_memory) {
248                 release_mem_region(ipw->link->resource[3]->start,
249                                 resource_size(ipw->link->resource[3]));
250                 iounmap(ipw->attr_memory);
251         }
252         pcmcia_disable_device(ipw->link);
253 }
254
255 /*
256  * ipwireless_attach() creates an "instance" of the driver, allocating
257  * local data structures for one device (one interface).  The device
258  * is registered with Card Services.
259  *
260  * The pcmcia_device structure is initialized, but we don't actually
261  * configure the card at this point -- we wait until we receive a
262  * card insertion event.
263  */
264 static int ipwireless_attach(struct pcmcia_device *link)
265 {
266         struct ipw_dev *ipw;
267         int ret;
268
269         ipw = kzalloc(sizeof(struct ipw_dev), GFP_KERNEL);
270         if (!ipw)
271                 return -ENOMEM;
272
273         ipw->link = link;
274         link->priv = ipw;
275
276         ipw->hardware = ipwireless_hardware_create();
277         if (!ipw->hardware) {
278                 kfree(ipw);
279                 return -ENOMEM;
280         }
281         /* RegisterClient will call config_ipwireless */
282
283         ret = config_ipwireless(ipw);
284
285         if (ret != 0) {
286                 ipwireless_detach(link);
287                 return ret;
288         }
289
290         return 0;
291 }
292
293 /*
294  * This deletes a driver "instance".  The device is de-registered with
295  * Card Services.  If it has been released, all local data structures
296  * are freed.  Otherwise, the structures will be freed when the device
297  * is released.
298  */
299 static void ipwireless_detach(struct pcmcia_device *link)
300 {
301         struct ipw_dev *ipw = link->priv;
302
303         release_ipwireless(ipw);
304
305         if (ipw->tty != NULL)
306                 ipwireless_tty_free(ipw->tty);
307         if (ipw->network != NULL)
308                 ipwireless_network_free(ipw->network);
309         if (ipw->hardware != NULL)
310                 ipwireless_hardware_free(ipw->hardware);
311         kfree(ipw);
312 }
313
314 static struct pcmcia_driver me = {
315         .owner          = THIS_MODULE,
316         .probe          = ipwireless_attach,
317         .remove         = ipwireless_detach,
318         .name           = IPWIRELESS_PCCARD_NAME,
319         .id_table       = ipw_ids
320 };
321
322 /*
323  * Module insertion : initialisation of the module.
324  * Register the card with cardmgr...
325  */
326 static int __init init_ipwireless(void)
327 {
328         int ret;
329
330         ret = ipwireless_tty_init();
331         if (ret != 0)
332                 return ret;
333
334         ret = pcmcia_register_driver(&me);
335         if (ret != 0)
336                 ipwireless_tty_release();
337
338         return ret;
339 }
340
341 /*
342  * Module removal
343  */
344 static void __exit exit_ipwireless(void)
345 {
346         pcmcia_unregister_driver(&me);
347         ipwireless_tty_release();
348 }
349
350 module_init(init_ipwireless);
351 module_exit(exit_ipwireless);
352
353 MODULE_AUTHOR(IPWIRELESS_PCMCIA_AUTHOR);
354 MODULE_DESCRIPTION(IPWIRELESS_PCCARD_NAME " " IPWIRELESS_PCMCIA_VERSION);
355 MODULE_LICENSE("GPL");