ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-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/config.h>       /* OS configuration options */
44 #include <linux/stddef.h>       /* offsetof(), etc. */
45 #include <linux/errno.h>        /* return codes */
46 #include <linux/string.h>       /* inline memset(), etc. */
47 #include <linux/slab.h>         /* kmalloc(), kfree() */
48 #include <linux/kernel.h>       /* printk(), and other useful stuff */
49 #include <linux/module.h>       /* support for loadable modules */
50 #include <linux/ioport.h>       /* request_region(), release_region() */
51 #include <linux/wanrouter.h>    /* WAN router definitions */
52 #include <linux/cyclomx.h>      /* cyclomx common user API definitions */
53 #include <linux/init.h>         /* __init (when not using as a module) */
54
55 unsigned int cycx_debug;
56
57 MODULE_AUTHOR("Arnaldo Carvalho de Melo");
58 MODULE_DESCRIPTION("Cyclom 2X Sync Card Driver.");
59 MODULE_LICENSE("GPL");
60 MODULE_PARM(cycx_debug, "i");
61 MODULE_PARM_DESC(cycx_debug, "cyclomx debug level");
62
63 /* Defines & Macros */
64
65 #define CYCX_DRV_VERSION        0       /* version number */
66 #define CYCX_DRV_RELEASE        11      /* release (minor version) number */
67 #define CYCX_MAX_CARDS          1       /* max number of adapters */
68
69 #define CONFIG_CYCX_CARDS 1
70
71 /* Function Prototypes */
72
73 /* WAN link driver entry points */
74 static int cycx_wan_setup(struct wan_device *wandev, wandev_conf_t *conf);
75 static int cycx_wan_shutdown(struct wan_device *wandev);
76
77 /* Miscellaneous functions */
78 static irqreturn_t cycx_isr(int irq, void *dev_id, struct pt_regs *regs);
79
80 /* Global Data
81  * Note: All data must be explicitly initialized!!!
82  */
83
84 /* private data */
85 static char cycx_drvname[] = "cyclomx";
86 static char cycx_fullname[] = "CYCLOM 2X(tm) Sync Card Driver";
87 static char cycx_copyright[] = "(c) 1998-2003 Arnaldo Carvalho de Melo "
88                           "<acme@conectiva.com.br>";
89 static int cycx_ncards = CONFIG_CYCX_CARDS;
90 static struct cycx_device *cycx_card_array;     /* adapter data space */
91
92 /* Kernel Loadable Module Entry Points */
93
94 /*
95  * Module 'insert' entry point.
96  * o print announcement
97  * o allocate adapter data space
98  * o initialize static data
99  * o register all cards with WAN router
100  * o calibrate Cyclom 2X shared memory access delay.
101  *
102  * Return:      0       Ok
103  *              < 0     error.
104  * Context:     process
105  */
106 int __init cycx_init(void)
107 {
108         int cnt, err = -ENOMEM;
109
110         printk(KERN_INFO "%s v%u.%u %s\n",
111                 cycx_fullname, CYCX_DRV_VERSION, CYCX_DRV_RELEASE,
112                 cycx_copyright);
113
114         /* Verify number of cards and allocate adapter data space */
115         cycx_ncards = min_t(int, cycx_ncards, CYCX_MAX_CARDS);
116         cycx_ncards = max_t(int, cycx_ncards, 1);
117         cycx_card_array = kmalloc(sizeof(struct cycx_device) * cycx_ncards,
118                                   GFP_KERNEL);
119         if (!cycx_card_array)
120                 goto out;
121
122         memset(cycx_card_array, 0, sizeof(struct cycx_device) * cycx_ncards);
123
124         /* Register adapters with WAN router */
125         for (cnt = 0; cnt < cycx_ncards; ++cnt) {
126                 struct cycx_device *card = &cycx_card_array[cnt];
127                 struct wan_device *wandev = &card->wandev;
128
129                 sprintf(card->devname, "%s%d", cycx_drvname, cnt + 1);
130                 wandev->magic    = ROUTER_MAGIC;
131                 wandev->name     = card->devname;
132                 wandev->private  = card;
133                 wandev->setup    = cycx_wan_setup;
134                 wandev->shutdown = cycx_wan_shutdown;
135                 err = register_wan_device(wandev);
136
137                 if (err) {
138                         printk(KERN_ERR "%s: %s registration failed with "
139                                         "error %d!\n",
140                                         cycx_drvname, card->devname, err);
141                         break;
142                 }
143         }
144
145         err = -ENODEV;
146         if (!cnt) {
147                 kfree(cycx_card_array);
148                 goto out;
149         }
150         err = 0;
151         cycx_ncards = cnt;      /* adjust actual number of cards */
152 out:    return err;
153 }
154
155 /*
156  * Module 'remove' entry point.
157  * o unregister all adapters from the WAN router
158  * o release all remaining system resources
159  */
160 static void __exit cycx_exit(void)
161 {
162         int i = 0;
163
164         for (; i < cycx_ncards; ++i) {
165                 struct cycx_device *card = &cycx_card_array[i];
166                 unregister_wan_device(card->devname);
167         }
168
169         kfree(cycx_card_array);
170 }
171
172 /* WAN Device Driver Entry Points */
173 /*
174  * Setup/configure WAN link driver.
175  * o check adapter state
176  * o make sure firmware is present in configuration
177  * o allocate interrupt vector
178  * o setup Cyclom 2X hardware
179  * o call appropriate routine to perform protocol-specific initialization
180  *
181  * This function is called when router handles ROUTER_SETUP IOCTL. The
182  * configuration structure is in kernel memory (including extended data, if
183  * any).
184  */
185 static int cycx_wan_setup(struct wan_device *wandev, wandev_conf_t *conf)
186 {
187         int rc = -EFAULT;
188         struct cycx_device *card;
189         int irq;
190
191         /* Sanity checks */
192
193         if (!wandev || !wandev->private || !conf)
194                 goto out;
195
196         card = wandev->private;
197         rc = -EBUSY;
198         if (wandev->state != WAN_UNCONFIGURED)
199                 goto out;
200
201         rc = -EINVAL;
202         if (!conf->data_size || !conf->data) {
203                 printk(KERN_ERR "%s: firmware not found in configuration "
204                                 "data!\n", wandev->name);
205                 goto out;
206         }
207
208         if (conf->irq <= 0) {
209                 printk(KERN_ERR "%s: can't configure without IRQ!\n",
210                                 wandev->name);
211                 goto out;
212         }
213
214         /* Allocate IRQ */
215         irq = conf->irq == 2 ? 9 : conf->irq;   /* IRQ2 -> IRQ9 */
216
217         if (request_irq(irq, cycx_isr, 0, wandev->name, card)) {
218                 printk(KERN_ERR "%s: can't reserve IRQ %d!\n",
219                                 wandev->name, irq);
220                 goto out;
221         }
222
223         /* Configure hardware, load firmware, etc. */
224         memset(&card->hw, 0, sizeof(card->hw));
225         card->hw.irq     = irq;
226         card->hw.dpmbase = (void *)conf->maddr;
227         card->hw.dpmsize = CYCX_WINDOWSIZE;
228         card->hw.fwid    = CFID_X25_2X;
229         card->lock       = SPIN_LOCK_UNLOCKED;
230         init_waitqueue_head(&card->wait_stats);
231
232         rc = cycx_setup(&card->hw, conf->data, conf->data_size);
233         if (rc)
234                 goto out_irq;
235
236         /* Initialize WAN device data space */
237         wandev->irq       = irq;
238         wandev->dma       = wandev->ioport = 0;
239         wandev->maddr     = (unsigned long)card->hw.dpmbase;
240         wandev->msize     = card->hw.dpmsize;
241         wandev->hw_opt[2] = 0;
242         wandev->hw_opt[3] = card->hw.fwid;
243
244         /* Protocol-specific initialization */
245         switch (card->hw.fwid) {
246 #ifdef CONFIG_CYCLOMX_X25
247         case CFID_X25_2X:
248                 rc = cycx_x25_wan_init(card, conf);
249                 break;
250 #endif
251         default:
252                 printk(KERN_ERR "%s: this firmware is not supported!\n",
253                                 wandev->name);
254                 rc = -EINVAL;
255         }
256
257         if (rc) {
258                 cycx_down(&card->hw);
259                 goto out_irq;
260         }
261
262         rc = 0;
263 out:
264         return rc;
265 out_irq:
266         free_irq(irq, card);
267         goto out;
268 }
269
270 /*
271  * Shut down WAN link driver.
272  * o shut down adapter hardware
273  * o release system resources.
274  *
275  * This function is called by the router when device is being unregistered or
276  * when it handles ROUTER_DOWN IOCTL.
277  */
278 static int cycx_wan_shutdown(struct wan_device *wandev)
279 {
280         int ret = -EFAULT;
281         struct cycx_device *card;
282
283         /* sanity checks */
284         if (!wandev || !wandev->private)
285                 goto out;
286
287         ret = 0;
288         if (wandev->state == WAN_UNCONFIGURED)
289                 goto out;
290
291         card = wandev->private;
292         wandev->state = WAN_UNCONFIGURED;
293         cycx_down(&card->hw);
294         printk(KERN_INFO "%s: irq %d being freed!\n", wandev->name,
295                         wandev->irq);
296         free_irq(wandev->irq, card);
297 out:    return ret;
298 }
299
300 /* Miscellaneous */
301 /*
302  * Cyclom 2X Interrupt Service Routine.
303  * o acknowledge Cyclom 2X hardware interrupt.
304  * o call protocol-specific interrupt service routine, if any.
305  */
306 static irqreturn_t cycx_isr(int irq, void *dev_id, struct pt_regs *regs)
307 {
308         struct cycx_device *card = (struct cycx_device *)dev_id;
309
310         if (!card || card->wandev.state == WAN_UNCONFIGURED)
311                 goto out;
312
313         if (card->in_isr) {
314                 printk(KERN_WARNING "%s: interrupt re-entrancy on IRQ %d!\n",
315                                     card->devname, card->wandev.irq);
316                 goto out;
317         }
318
319         if (card->isr)
320                 card->isr(card);
321         return IRQ_HANDLED;
322 out:
323         return IRQ_NONE;
324 }
325
326 /* Set WAN device state.  */
327 void cycx_set_state(struct cycx_device *card, int state)
328 {
329         unsigned long flags;
330         char *string_state = NULL;
331
332         spin_lock_irqsave(&card->lock, flags);
333
334         if (card->wandev.state != state) {
335                 switch (state) {
336                 case WAN_CONNECTED:
337                         string_state = "connected!";
338                         break;
339                 case WAN_DISCONNECTED:
340                         string_state = "disconnected!";
341                         break;
342                 }
343                 printk(KERN_INFO "%s: link %s\n", card->devname, string_state);
344                 card->wandev.state = state;
345         }
346
347         card->state_tick = jiffies;
348         spin_unlock_irqrestore(&card->lock, flags);
349 }
350
351 module_init(cycx_init);
352 module_exit(cycx_exit);