Merge branch 'master' into upstream-fixes
[sfrench/cifs-2.6.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 char cycx_drvname[] = "cyclomx";
85 static char cycx_fullname[] = "CYCLOM 2X(tm) Sync Card Driver";
86 static 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 = kmalloc(sizeof(struct cycx_device) * cycx_ncards,
117                                   GFP_KERNEL);
118         if (!cycx_card_array)
119                 goto out;
120
121         memset(cycx_card_array, 0, sizeof(struct cycx_device) * cycx_ncards);
122
123         /* Register adapters with WAN router */
124         for (cnt = 0; cnt < cycx_ncards; ++cnt) {
125                 struct cycx_device *card = &cycx_card_array[cnt];
126                 struct wan_device *wandev = &card->wandev;
127
128                 sprintf(card->devname, "%s%d", cycx_drvname, cnt + 1);
129                 wandev->magic    = ROUTER_MAGIC;
130                 wandev->name     = card->devname;
131                 wandev->private  = card;
132                 wandev->setup    = cycx_wan_setup;
133                 wandev->shutdown = cycx_wan_shutdown;
134                 err = register_wan_device(wandev);
135
136                 if (err) {
137                         printk(KERN_ERR "%s: %s registration failed with "
138                                         "error %d!\n",
139                                         cycx_drvname, card->devname, err);
140                         break;
141                 }
142         }
143
144         err = -ENODEV;
145         if (!cnt) {
146                 kfree(cycx_card_array);
147                 goto out;
148         }
149         err = 0;
150         cycx_ncards = cnt;      /* adjust actual number of cards */
151 out:    return err;
152 }
153
154 /*
155  * Module 'remove' entry point.
156  * o unregister all adapters from the WAN router
157  * o release all remaining system resources
158  */
159 static void __exit cycx_exit(void)
160 {
161         int i = 0;
162
163         for (; i < cycx_ncards; ++i) {
164                 struct cycx_device *card = &cycx_card_array[i];
165                 unregister_wan_device(card->devname);
166         }
167
168         kfree(cycx_card_array);
169 }
170
171 /* WAN Device Driver Entry Points */
172 /*
173  * Setup/configure WAN link driver.
174  * o check adapter state
175  * o make sure firmware is present in configuration
176  * o allocate interrupt vector
177  * o setup Cyclom 2X hardware
178  * o call appropriate routine to perform protocol-specific initialization
179  *
180  * This function is called when router handles ROUTER_SETUP IOCTL. The
181  * configuration structure is in kernel memory (including extended data, if
182  * any).
183  */
184 static int cycx_wan_setup(struct wan_device *wandev, wandev_conf_t *conf)
185 {
186         int rc = -EFAULT;
187         struct cycx_device *card;
188         int irq;
189
190         /* Sanity checks */
191
192         if (!wandev || !wandev->private || !conf)
193                 goto out;
194
195         card = wandev->private;
196         rc = -EBUSY;
197         if (wandev->state != WAN_UNCONFIGURED)
198                 goto out;
199
200         rc = -EINVAL;
201         if (!conf->data_size || !conf->data) {
202                 printk(KERN_ERR "%s: firmware not found in configuration "
203                                 "data!\n", wandev->name);
204                 goto out;
205         }
206
207         if (conf->irq <= 0) {
208                 printk(KERN_ERR "%s: can't configure without IRQ!\n",
209                                 wandev->name);
210                 goto out;
211         }
212
213         /* Allocate IRQ */
214         irq = conf->irq == 2 ? 9 : conf->irq;   /* IRQ2 -> IRQ9 */
215
216         if (request_irq(irq, cycx_isr, 0, wandev->name, card)) {
217                 printk(KERN_ERR "%s: can't reserve IRQ %d!\n",
218                                 wandev->name, irq);
219                 goto out;
220         }
221
222         /* Configure hardware, load firmware, etc. */
223         memset(&card->hw, 0, sizeof(card->hw));
224         card->hw.irq     = irq;
225         card->hw.dpmsize = CYCX_WINDOWSIZE;
226         card->hw.fwid    = CFID_X25_2X;
227         spin_lock_init(&card->lock);
228         init_waitqueue_head(&card->wait_stats);
229
230         rc = cycx_setup(&card->hw, conf->data, conf->data_size, conf->maddr);
231         if (rc)
232                 goto out_irq;
233
234         /* Initialize WAN device data space */
235         wandev->irq       = irq;
236         wandev->dma       = wandev->ioport = 0;
237         wandev->maddr     = (unsigned long)card->hw.dpmbase;
238         wandev->msize     = card->hw.dpmsize;
239         wandev->hw_opt[2] = 0;
240         wandev->hw_opt[3] = card->hw.fwid;
241
242         /* Protocol-specific initialization */
243         switch (card->hw.fwid) {
244 #ifdef CONFIG_CYCLOMX_X25
245         case CFID_X25_2X:
246                 rc = cycx_x25_wan_init(card, conf);
247                 break;
248 #endif
249         default:
250                 printk(KERN_ERR "%s: this firmware is not supported!\n",
251                                 wandev->name);
252                 rc = -EINVAL;
253         }
254
255         if (rc) {
256                 cycx_down(&card->hw);
257                 goto out_irq;
258         }
259
260         rc = 0;
261 out:
262         return rc;
263 out_irq:
264         free_irq(irq, card);
265         goto out;
266 }
267
268 /*
269  * Shut down WAN link driver.
270  * o shut down adapter hardware
271  * o release system resources.
272  *
273  * This function is called by the router when device is being unregistered or
274  * when it handles ROUTER_DOWN IOCTL.
275  */
276 static int cycx_wan_shutdown(struct wan_device *wandev)
277 {
278         int ret = -EFAULT;
279         struct cycx_device *card;
280
281         /* sanity checks */
282         if (!wandev || !wandev->private)
283                 goto out;
284
285         ret = 0;
286         if (wandev->state == WAN_UNCONFIGURED)
287                 goto out;
288
289         card = wandev->private;
290         wandev->state = WAN_UNCONFIGURED;
291         cycx_down(&card->hw);
292         printk(KERN_INFO "%s: irq %d being freed!\n", wandev->name,
293                         wandev->irq);
294         free_irq(wandev->irq, card);
295 out:    return ret;
296 }
297
298 /* Miscellaneous */
299 /*
300  * Cyclom 2X Interrupt Service Routine.
301  * o acknowledge Cyclom 2X hardware interrupt.
302  * o call protocol-specific interrupt service routine, if any.
303  */
304 static irqreturn_t cycx_isr(int irq, void *dev_id)
305 {
306         struct cycx_device *card = dev_id;
307
308         if (card->wandev.state == WAN_UNCONFIGURED)
309                 goto out;
310
311         if (card->in_isr) {
312                 printk(KERN_WARNING "%s: interrupt re-entrancy on IRQ %d!\n",
313                                     card->devname, card->wandev.irq);
314                 goto out;
315         }
316
317         if (card->isr)
318                 card->isr(card);
319         return IRQ_HANDLED;
320 out:
321         return IRQ_NONE;
322 }
323
324 /* Set WAN device state.  */
325 void cycx_set_state(struct cycx_device *card, int state)
326 {
327         unsigned long flags;
328         char *string_state = NULL;
329
330         spin_lock_irqsave(&card->lock, flags);
331
332         if (card->wandev.state != state) {
333                 switch (state) {
334                 case WAN_CONNECTED:
335                         string_state = "connected!";
336                         break;
337                 case WAN_DISCONNECTED:
338                         string_state = "disconnected!";
339                         break;
340                 }
341                 printk(KERN_INFO "%s: link %s\n", card->devname, string_state);
342                 card->wandev.state = state;
343         }
344
345         card->state_tick = jiffies;
346         spin_unlock_irqrestore(&card->lock, flags);
347 }
348
349 module_init(cycx_init);
350 module_exit(cycx_exit);