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