Linux Kernel  3.7.1
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
cycx_main.c
Go to the documentation of this file.
1 /*
2 * cycx_main.c Cyclades Cyclom 2X WAN Link Driver. Main module.
3 *
4 * Author: Arnaldo Carvalho de Melo <[email protected]>
5 *
6 * Copyright: (c) 1998-2003 Arnaldo Carvalho de Melo
7 *
8 * Based on sdlamain.c by Gene Kozin <[email protected]> &
9 * Jaspreet Singh <[email protected]>
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 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
44 
45 #include <linux/stddef.h> /* offsetof(), etc. */
46 #include <linux/errno.h> /* return codes */
47 #include <linux/string.h> /* inline memset(), etc. */
48 #include <linux/slab.h> /* kmalloc(), kfree() */
49 #include <linux/kernel.h> /* printk(), and other useful stuff */
50 #include <linux/module.h> /* support for loadable modules */
51 #include <linux/ioport.h> /* request_region(), release_region() */
52 #include <linux/wanrouter.h> /* WAN router definitions */
53 #include <linux/cyclomx.h> /* cyclomx common user API definitions */
54 #include <linux/init.h> /* __init (when not using as a module) */
55 #include <linux/interrupt.h>
56 
57 unsigned int cycx_debug;
58 
59 MODULE_AUTHOR("Arnaldo Carvalho de Melo");
60 MODULE_DESCRIPTION("Cyclom 2X Sync Card Driver.");
61 MODULE_LICENSE("GPL");
62 module_param(cycx_debug, int, 0);
63 MODULE_PARM_DESC(cycx_debug, "cyclomx debug level");
64 
65 /* Defines & Macros */
66 
67 #define CYCX_DRV_VERSION 0 /* version number */
68 #define CYCX_DRV_RELEASE 11 /* release (minor version) number */
69 #define CYCX_MAX_CARDS 1 /* max number of adapters */
70 
71 #define CONFIG_CYCX_CARDS 1
72 
73 /* Function Prototypes */
74 
75 /* WAN link driver entry points */
76 static int cycx_wan_setup(struct wan_device *wandev, wandev_conf_t *conf);
77 static int cycx_wan_shutdown(struct wan_device *wandev);
78 
79 /* Miscellaneous functions */
80 static irqreturn_t cycx_isr(int irq, void *dev_id);
81 
82 /* Global Data
83  * Note: All data must be explicitly initialized!!!
84  */
85 
86 /* private data */
87 static const char cycx_drvname[] = "cyclomx";
88 static const char cycx_fullname[] = "CYCLOM 2X(tm) Sync Card Driver";
89 static const char cycx_copyright[] = "(c) 1998-2003 Arnaldo Carvalho de Melo "
91 static int cycx_ncards = CONFIG_CYCX_CARDS;
92 static struct cycx_device *cycx_card_array; /* adapter data space */
93 
94 /* Kernel Loadable Module Entry Points */
95 
96 /*
97  * Module 'insert' entry point.
98  * o print announcement
99  * o allocate adapter data space
100  * o initialize static data
101  * o register all cards with WAN router
102  * o calibrate Cyclom 2X shared memory access delay.
103  *
104  * Return: 0 Ok
105  * < 0 error.
106  * Context: process
107  */
108 static int __init cycx_init(void)
109 {
110  int cnt, err = -ENOMEM;
111 
112  pr_info("%s v%u.%u %s\n",
113  cycx_fullname, CYCX_DRV_VERSION, CYCX_DRV_RELEASE,
114  cycx_copyright);
115 
116  /* Verify number of cards and allocate adapter data space */
117  cycx_ncards = min_t(int, cycx_ncards, CYCX_MAX_CARDS);
118  cycx_ncards = max_t(int, cycx_ncards, 1);
119  cycx_card_array = kcalloc(cycx_ncards, sizeof(struct cycx_device), GFP_KERNEL);
120  if (!cycx_card_array)
121  goto out;
122 
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  pr_err("%s registration failed with error %d!\n",
139  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  pr_err("%s: firmware not found in configuration data!\n",
203  wandev->name);
204  goto out;
205  }
206 
207  if (conf->irq <= 0) {
208  pr_err("%s: can't configure without IRQ!\n", 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  pr_err("%s: can't reserve IRQ %d!\n", 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  pr_err("%s: this firmware is not supported!\n", wandev->name);
249  rc = -EINVAL;
250  }
251 
252  if (rc) {
253  cycx_down(&card->hw);
254  goto out_irq;
255  }
256 
257  rc = 0;
258 out:
259  return rc;
260 out_irq:
261  free_irq(irq, card);
262  goto out;
263 }
264 
265 /*
266  * Shut down WAN link driver.
267  * o shut down adapter hardware
268  * o release system resources.
269  *
270  * This function is called by the router when device is being unregistered or
271  * when it handles ROUTER_DOWN IOCTL.
272  */
273 static int cycx_wan_shutdown(struct wan_device *wandev)
274 {
275  int ret = -EFAULT;
276  struct cycx_device *card;
277 
278  /* sanity checks */
279  if (!wandev || !wandev->private)
280  goto out;
281 
282  ret = 0;
283  if (wandev->state == WAN_UNCONFIGURED)
284  goto out;
285 
286  card = wandev->private;
287  wandev->state = WAN_UNCONFIGURED;
288  cycx_down(&card->hw);
289  pr_info("%s: irq %d being freed!\n", wandev->name, wandev->irq);
290  free_irq(wandev->irq, card);
291 out: return ret;
292 }
293 
294 /* Miscellaneous */
295 /*
296  * Cyclom 2X Interrupt Service Routine.
297  * o acknowledge Cyclom 2X hardware interrupt.
298  * o call protocol-specific interrupt service routine, if any.
299  */
300 static irqreturn_t cycx_isr(int irq, void *dev_id)
301 {
302  struct cycx_device *card = dev_id;
303 
304  if (card->wandev.state == WAN_UNCONFIGURED)
305  goto out;
306 
307  if (card->in_isr) {
308  pr_warn("%s: interrupt re-entrancy on IRQ %d!\n",
309  card->devname, card->wandev.irq);
310  goto out;
311  }
312 
313  if (card->isr)
314  card->isr(card);
315  return IRQ_HANDLED;
316 out:
317  return IRQ_NONE;
318 }
319 
320 /* Set WAN device state. */
321 void cycx_set_state(struct cycx_device *card, int state)
322 {
323  unsigned long flags;
324  char *string_state = NULL;
325 
326  spin_lock_irqsave(&card->lock, flags);
327 
328  if (card->wandev.state != state) {
329  switch (state) {
330  case WAN_CONNECTED:
331  string_state = "connected!";
332  break;
333  case WAN_DISCONNECTED:
334  string_state = "disconnected!";
335  break;
336  }
337  pr_info("%s: link %s\n", card->devname, string_state);
338  card->wandev.state = state;
339  }
340 
341  card->state_tick = jiffies;
342  spin_unlock_irqrestore(&card->lock, flags);
343 }
344 
345 module_init(cycx_init);
346 module_exit(cycx_exit);