[PATCH] WAN: ioremap() failure checks in drivers
[linux-2.6/cjktty.git] / drivers / net / wan / cycx_main.c
blob430b1f630fb4a71147992411b3ad0eb71e997ad6
1 /*
2 * cycx_main.c Cyclades Cyclom 2X WAN Link Driver. Main module.
4 * Author: Arnaldo Carvalho de Melo <acme@conectiva.com.br>
6 * Copyright: (c) 1998-2003 Arnaldo Carvalho de Melo
8 * Based on sdlamain.c by Gene Kozin <genek@compuserve.com> &
9 * Jaspreet Singh <jaspreet@sangoma.com>
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).
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.
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) */
55 unsigned int cycx_debug;
57 MODULE_AUTHOR("Arnaldo Carvalho de Melo");
58 MODULE_DESCRIPTION("Cyclom 2X Sync Card Driver.");
59 MODULE_LICENSE("GPL");
60 module_param(cycx_debug, int, 0);
61 MODULE_PARM_DESC(cycx_debug, "cyclomx debug level");
63 /* Defines & Macros */
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 */
69 #define CONFIG_CYCX_CARDS 1
71 /* Function Prototypes */
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);
77 /* Miscellaneous functions */
78 static irqreturn_t cycx_isr(int irq, void *dev_id, struct pt_regs *regs);
80 /* Global Data
81 * Note: All data must be explicitly initialized!!!
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 */
92 /* Kernel Loadable Module Entry Points */
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.
102 * Return: 0 Ok
103 * < 0 error.
104 * Context: process
106 static int __init cycx_init(void)
108 int cnt, err = -ENOMEM;
110 printk(KERN_INFO "%s v%u.%u %s\n",
111 cycx_fullname, CYCX_DRV_VERSION, CYCX_DRV_RELEASE,
112 cycx_copyright);
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;
122 memset(cycx_card_array, 0, sizeof(struct cycx_device) * cycx_ncards);
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;
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);
137 if (err) {
138 printk(KERN_ERR "%s: %s registration failed with "
139 "error %d!\n",
140 cycx_drvname, card->devname, err);
141 break;
145 err = -ENODEV;
146 if (!cnt) {
147 kfree(cycx_card_array);
148 goto out;
150 err = 0;
151 cycx_ncards = cnt; /* adjust actual number of cards */
152 out: return err;
156 * Module 'remove' entry point.
157 * o unregister all adapters from the WAN router
158 * o release all remaining system resources
160 static void __exit cycx_exit(void)
162 int i = 0;
164 for (; i < cycx_ncards; ++i) {
165 struct cycx_device *card = &cycx_card_array[i];
166 unregister_wan_device(card->devname);
169 kfree(cycx_card_array);
172 /* WAN Device Driver Entry Points */
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
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).
185 static int cycx_wan_setup(struct wan_device *wandev, wandev_conf_t *conf)
187 int rc = -EFAULT;
188 struct cycx_device *card;
189 int irq;
191 /* Sanity checks */
193 if (!wandev || !wandev->private || !conf)
194 goto out;
196 card = wandev->private;
197 rc = -EBUSY;
198 if (wandev->state != WAN_UNCONFIGURED)
199 goto out;
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;
208 if (conf->irq <= 0) {
209 printk(KERN_ERR "%s: can't configure without IRQ!\n",
210 wandev->name);
211 goto out;
214 /* Allocate IRQ */
215 irq = conf->irq == 2 ? 9 : conf->irq; /* IRQ2 -> IRQ9 */
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;
223 /* Configure hardware, load firmware, etc. */
224 memset(&card->hw, 0, sizeof(card->hw));
225 card->hw.irq = irq;
226 card->hw.dpmsize = CYCX_WINDOWSIZE;
227 card->hw.fwid = CFID_X25_2X;
228 spin_lock_init(&card->lock);
229 init_waitqueue_head(&card->wait_stats);
231 rc = cycx_setup(&card->hw, conf->data, conf->data_size, conf->maddr);
232 if (rc)
233 goto out_irq;
235 /* Initialize WAN device data space */
236 wandev->irq = irq;
237 wandev->dma = wandev->ioport = 0;
238 wandev->maddr = (unsigned long)card->hw.dpmbase;
239 wandev->msize = card->hw.dpmsize;
240 wandev->hw_opt[2] = 0;
241 wandev->hw_opt[3] = card->hw.fwid;
243 /* Protocol-specific initialization */
244 switch (card->hw.fwid) {
245 #ifdef CONFIG_CYCLOMX_X25
246 case CFID_X25_2X:
247 rc = cycx_x25_wan_init(card, conf);
248 break;
249 #endif
250 default:
251 printk(KERN_ERR "%s: this firmware is not supported!\n",
252 wandev->name);
253 rc = -EINVAL;
256 if (rc) {
257 cycx_down(&card->hw);
258 goto out_irq;
261 rc = 0;
262 out:
263 return rc;
264 out_irq:
265 free_irq(irq, card);
266 goto out;
270 * Shut down WAN link driver.
271 * o shut down adapter hardware
272 * o release system resources.
274 * This function is called by the router when device is being unregistered or
275 * when it handles ROUTER_DOWN IOCTL.
277 static int cycx_wan_shutdown(struct wan_device *wandev)
279 int ret = -EFAULT;
280 struct cycx_device *card;
282 /* sanity checks */
283 if (!wandev || !wandev->private)
284 goto out;
286 ret = 0;
287 if (wandev->state == WAN_UNCONFIGURED)
288 goto out;
290 card = wandev->private;
291 wandev->state = WAN_UNCONFIGURED;
292 cycx_down(&card->hw);
293 printk(KERN_INFO "%s: irq %d being freed!\n", wandev->name,
294 wandev->irq);
295 free_irq(wandev->irq, card);
296 out: return ret;
299 /* Miscellaneous */
301 * Cyclom 2X Interrupt Service Routine.
302 * o acknowledge Cyclom 2X hardware interrupt.
303 * o call protocol-specific interrupt service routine, if any.
305 static irqreturn_t cycx_isr(int irq, void *dev_id, struct pt_regs *regs)
307 struct cycx_device *card = (struct cycx_device *)dev_id;
309 if (!card || card->wandev.state == WAN_UNCONFIGURED)
310 goto out;
312 if (card->in_isr) {
313 printk(KERN_WARNING "%s: interrupt re-entrancy on IRQ %d!\n",
314 card->devname, card->wandev.irq);
315 goto out;
318 if (card->isr)
319 card->isr(card);
320 return IRQ_HANDLED;
321 out:
322 return IRQ_NONE;
325 /* Set WAN device state. */
326 void cycx_set_state(struct cycx_device *card, int state)
328 unsigned long flags;
329 char *string_state = NULL;
331 spin_lock_irqsave(&card->lock, flags);
333 if (card->wandev.state != state) {
334 switch (state) {
335 case WAN_CONNECTED:
336 string_state = "connected!";
337 break;
338 case WAN_DISCONNECTED:
339 string_state = "disconnected!";
340 break;
342 printk(KERN_INFO "%s: link %s\n", card->devname, string_state);
343 card->wandev.state = state;
346 card->state_tick = jiffies;
347 spin_unlock_irqrestore(&card->lock, flags);
350 module_init(cycx_init);
351 module_exit(cycx_exit);