Merge branches 'core-urgent-for-linus', 'perf-urgent-for-linus' and 'sched-urgent...
[pandora-kernel.git] / drivers / net / wan / cycx_main.c
1 /*
2 * cycx_main.c   Cyclades Cyclom 2X WAN Link Driver. Main module.
3 *
4 * Author:       Arnaldo Carvalho de Melo <acme@conectiva.com.br>
5 *
6 * Copyright:    (c) 1998-2003 Arnaldo Carvalho de Melo
7 *
8 * Based on sdlamain.c by Gene Kozin <genek@compuserve.com> &
9 *                        Jaspreet Singh <jaspreet@sangoma.com>
10 *
11 *               This program is free software; you can redistribute it and/or
12 *               modify it under the terms of the GNU General Public License
13 *               as published by the Free Software Foundation; either version
14 *               2 of the License, or (at your option) any later version.
15 * ============================================================================
16 * Please look at the bitkeeper changelog (or any other scm tool that ends up
17 * importing bitkeeper changelog or that replaces bitkeeper in the future as
18 * main tool for linux development).
19
20 * 2001/05/09    acme            Fix MODULE_DESC for debug, .bss nitpicks,
21 *                               some cleanups
22 * 2000/07/13    acme            remove useless #ifdef MODULE and crap
23 *                               #if KERNEL_VERSION > blah
24 * 2000/07/06    acme            __exit at cyclomx_cleanup
25 * 2000/04/02    acme            dprintk and cycx_debug
26 *                               module_init/module_exit
27 * 2000/01/21    acme            rename cyclomx_open to cyclomx_mod_inc_use_count
28 *                               and cyclomx_close to cyclomx_mod_dec_use_count
29 * 2000/01/08    acme            cleanup
30 * 1999/11/06    acme            cycx_down back to life (it needs to be
31 *                               called to iounmap the dpmbase)
32 * 1999/08/09    acme            removed references to enable_tx_int
33 *                               use spinlocks instead of cli/sti in
34 *                               cyclomx_set_state
35 * 1999/05/19    acme            works directly linked into the kernel
36 *                               init_waitqueue_head for 2.3.* kernel
37 * 1999/05/18    acme            major cleanup (polling not needed), etc
38 * 1998/08/28    acme            minor cleanup (ioctls for firmware deleted)
39 *                               queue_task activated
40 * 1998/08/08    acme            Initial version.
41 */
42
43 #include <linux/stddef.h>       /* offsetof(), etc. */
44 #include <linux/errno.h>        /* return codes */
45 #include <linux/string.h>       /* inline memset(), etc. */
46 #include <linux/slab.h>         /* kmalloc(), kfree() */
47 #include <linux/kernel.h>       /* printk(), and other useful stuff */
48 #include <linux/module.h>       /* support for loadable modules */
49 #include <linux/ioport.h>       /* request_region(), release_region() */
50 #include <linux/wanrouter.h>    /* WAN router definitions */
51 #include <linux/cyclomx.h>      /* cyclomx common user API definitions */
52 #include <linux/init.h>         /* __init (when not using as a module) */
53
54 unsigned int cycx_debug;
55
56 MODULE_AUTHOR("Arnaldo Carvalho de Melo");
57 MODULE_DESCRIPTION("Cyclom 2X Sync Card Driver.");
58 MODULE_LICENSE("GPL");
59 module_param(cycx_debug, int, 0);
60 MODULE_PARM_DESC(cycx_debug, "cyclomx debug level");
61
62 /* Defines & Macros */
63
64 #define CYCX_DRV_VERSION        0       /* version number */
65 #define CYCX_DRV_RELEASE        11      /* release (minor version) number */
66 #define CYCX_MAX_CARDS          1       /* max number of adapters */
67
68 #define CONFIG_CYCX_CARDS 1
69
70 /* Function Prototypes */
71
72 /* WAN link driver entry points */
73 static int cycx_wan_setup(struct wan_device *wandev, wandev_conf_t *conf);
74 static int cycx_wan_shutdown(struct wan_device *wandev);
75
76 /* Miscellaneous functions */
77 static irqreturn_t cycx_isr(int irq, void *dev_id);
78
79 /* Global Data
80  * Note: All data must be explicitly initialized!!!
81  */
82
83 /* private data */
84 static const char cycx_drvname[] = "cyclomx";
85 static const char cycx_fullname[] = "CYCLOM 2X(tm) Sync Card Driver";
86 static const char cycx_copyright[] = "(c) 1998-2003 Arnaldo Carvalho de Melo "
87                           "<acme@conectiva.com.br>";
88 static int cycx_ncards = CONFIG_CYCX_CARDS;
89 static struct cycx_device *cycx_card_array;     /* adapter data space */
90
91 /* Kernel Loadable Module Entry Points */
92
93 /*
94  * Module 'insert' entry point.
95  * o print announcement
96  * o allocate adapter data space
97  * o initialize static data
98  * o register all cards with WAN router
99  * o calibrate Cyclom 2X shared memory access delay.
100  *
101  * Return:      0       Ok
102  *              < 0     error.
103  * Context:     process
104  */
105 static int __init cycx_init(void)
106 {
107         int cnt, err = -ENOMEM;
108
109         printk(KERN_INFO "%s v%u.%u %s\n",
110                 cycx_fullname, CYCX_DRV_VERSION, CYCX_DRV_RELEASE,
111                 cycx_copyright);
112
113         /* Verify number of cards and allocate adapter data space */
114         cycx_ncards = min_t(int, cycx_ncards, CYCX_MAX_CARDS);
115         cycx_ncards = max_t(int, cycx_ncards, 1);
116         cycx_card_array = kcalloc(cycx_ncards, sizeof(struct cycx_device), GFP_KERNEL);
117         if (!cycx_card_array)
118                 goto out;
119
120
121         /* Register adapters with WAN router */
122         for (cnt = 0; cnt < cycx_ncards; ++cnt) {
123                 struct cycx_device *card = &cycx_card_array[cnt];
124                 struct wan_device *wandev = &card->wandev;
125
126                 sprintf(card->devname, "%s%d", cycx_drvname, cnt + 1);
127                 wandev->magic    = ROUTER_MAGIC;
128                 wandev->name     = card->devname;
129                 wandev->private  = card;
130                 wandev->setup    = cycx_wan_setup;
131                 wandev->shutdown = cycx_wan_shutdown;
132                 err = register_wan_device(wandev);
133
134                 if (err) {
135                         printk(KERN_ERR "%s: %s registration failed with "
136                                         "error %d!\n",
137                                         cycx_drvname, card->devname, err);
138                         break;
139                 }
140         }
141
142         err = -ENODEV;
143         if (!cnt) {
144                 kfree(cycx_card_array);
145                 goto out;
146         }
147         err = 0;
148         cycx_ncards = cnt;      /* adjust actual number of cards */
149 out:    return err;
150 }
151
152 /*
153  * Module 'remove' entry point.
154  * o unregister all adapters from the WAN router
155  * o release all remaining system resources
156  */
157 static void __exit cycx_exit(void)
158 {
159         int i = 0;
160
161         for (; i < cycx_ncards; ++i) {
162                 struct cycx_device *card = &cycx_card_array[i];
163                 unregister_wan_device(card->devname);
164         }
165
166         kfree(cycx_card_array);
167 }
168
169 /* WAN Device Driver Entry Points */
170 /*
171  * Setup/configure WAN link driver.
172  * o check adapter state
173  * o make sure firmware is present in configuration
174  * o allocate interrupt vector
175  * o setup Cyclom 2X hardware
176  * o call appropriate routine to perform protocol-specific initialization
177  *
178  * This function is called when router handles ROUTER_SETUP IOCTL. The
179  * configuration structure is in kernel memory (including extended data, if
180  * any).
181  */
182 static int cycx_wan_setup(struct wan_device *wandev, wandev_conf_t *conf)
183 {
184         int rc = -EFAULT;
185         struct cycx_device *card;
186         int irq;
187
188         /* Sanity checks */
189
190         if (!wandev || !wandev->private || !conf)
191                 goto out;
192
193         card = wandev->private;
194         rc = -EBUSY;
195         if (wandev->state != WAN_UNCONFIGURED)
196                 goto out;
197
198         rc = -EINVAL;
199         if (!conf->data_size || !conf->data) {
200                 printk(KERN_ERR "%s: firmware not found in configuration "
201                                 "data!\n", wandev->name);
202                 goto out;
203         }
204
205         if (conf->irq <= 0) {
206                 printk(KERN_ERR "%s: can't configure without IRQ!\n",
207                                 wandev->name);
208                 goto out;
209         }
210
211         /* Allocate IRQ */
212         irq = conf->irq == 2 ? 9 : conf->irq;   /* IRQ2 -> IRQ9 */
213
214         if (request_irq(irq, cycx_isr, 0, wandev->name, card)) {
215                 printk(KERN_ERR "%s: can't reserve IRQ %d!\n",
216                                 wandev->name, irq);
217                 goto out;
218         }
219
220         /* Configure hardware, load firmware, etc. */
221         memset(&card->hw, 0, sizeof(card->hw));
222         card->hw.irq     = irq;
223         card->hw.dpmsize = CYCX_WINDOWSIZE;
224         card->hw.fwid    = CFID_X25_2X;
225         spin_lock_init(&card->lock);
226         init_waitqueue_head(&card->wait_stats);
227
228         rc = cycx_setup(&card->hw, conf->data, conf->data_size, conf->maddr);
229         if (rc)
230                 goto out_irq;
231
232         /* Initialize WAN device data space */
233         wandev->irq       = irq;
234         wandev->dma       = wandev->ioport = 0;
235         wandev->maddr     = (unsigned long)card->hw.dpmbase;
236         wandev->msize     = card->hw.dpmsize;
237         wandev->hw_opt[2] = 0;
238         wandev->hw_opt[3] = card->hw.fwid;
239
240         /* Protocol-specific initialization */
241         switch (card->hw.fwid) {
242 #ifdef CONFIG_CYCLOMX_X25
243         case CFID_X25_2X:
244                 rc = cycx_x25_wan_init(card, conf);
245                 break;
246 #endif
247         default:
248                 printk(KERN_ERR "%s: this firmware is not supported!\n",
249                                 wandev->name);
250                 rc = -EINVAL;
251         }
252
253         if (rc) {
254                 cycx_down(&card->hw);
255                 goto out_irq;
256         }
257
258         rc = 0;
259 out:
260         return rc;
261 out_irq:
262         free_irq(irq, card);
263         goto out;
264 }
265
266 /*
267  * Shut down WAN link driver.
268  * o shut down adapter hardware
269  * o release system resources.
270  *
271  * This function is called by the router when device is being unregistered or
272  * when it handles ROUTER_DOWN IOCTL.
273  */
274 static int cycx_wan_shutdown(struct wan_device *wandev)
275 {
276         int ret = -EFAULT;
277         struct cycx_device *card;
278
279         /* sanity checks */
280         if (!wandev || !wandev->private)
281                 goto out;
282
283         ret = 0;
284         if (wandev->state == WAN_UNCONFIGURED)
285                 goto out;
286
287         card = wandev->private;
288         wandev->state = WAN_UNCONFIGURED;
289         cycx_down(&card->hw);
290         printk(KERN_INFO "%s: irq %d being freed!\n", wandev->name,
291                         wandev->irq);
292         free_irq(wandev->irq, card);
293 out:    return ret;
294 }
295
296 /* Miscellaneous */
297 /*
298  * Cyclom 2X Interrupt Service Routine.
299  * o acknowledge Cyclom 2X hardware interrupt.
300  * o call protocol-specific interrupt service routine, if any.
301  */
302 static irqreturn_t cycx_isr(int irq, void *dev_id)
303 {
304         struct cycx_device *card = dev_id;
305
306         if (card->wandev.state == WAN_UNCONFIGURED)
307                 goto out;
308
309         if (card->in_isr) {
310                 printk(KERN_WARNING "%s: interrupt re-entrancy on IRQ %d!\n",
311                                     card->devname, card->wandev.irq);
312                 goto out;
313         }
314
315         if (card->isr)
316                 card->isr(card);
317         return IRQ_HANDLED;
318 out:
319         return IRQ_NONE;
320 }
321
322 /* Set WAN device state.  */
323 void cycx_set_state(struct cycx_device *card, int state)
324 {
325         unsigned long flags;
326         char *string_state = NULL;
327
328         spin_lock_irqsave(&card->lock, flags);
329
330         if (card->wandev.state != state) {
331                 switch (state) {
332                 case WAN_CONNECTED:
333                         string_state = "connected!";
334                         break;
335                 case WAN_DISCONNECTED:
336                         string_state = "disconnected!";
337                         break;
338                 }
339                 printk(KERN_INFO "%s: link %s\n", card->devname, string_state);
340                 card->wandev.state = state;
341         }
342
343         card->state_tick = jiffies;
344         spin_unlock_irqrestore(&card->lock, flags);
345 }
346
347 module_init(cycx_init);
348 module_exit(cycx_exit);