1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
44
45#include <linux/stddef.h>
46#include <linux/errno.h>
47#include <linux/string.h>
48#include <linux/slab.h>
49#include <linux/kernel.h>
50#include <linux/module.h>
51#include <linux/ioport.h>
52#include <linux/wanrouter.h>
53#include <linux/cyclomx.h>
54#include <linux/init.h>
55#include <linux/interrupt.h>
56
57unsigned int cycx_debug;
58
59MODULE_AUTHOR("Arnaldo Carvalho de Melo");
60MODULE_DESCRIPTION("Cyclom 2X Sync Card Driver.");
61MODULE_LICENSE("GPL");
62module_param(cycx_debug, int, 0);
63MODULE_PARM_DESC(cycx_debug, "cyclomx debug level");
64
65
66
67#define CYCX_DRV_VERSION 0
68#define CYCX_DRV_RELEASE 11
69#define CYCX_MAX_CARDS 1
70
71#define CONFIG_CYCX_CARDS 1
72
73
74
75
76static int cycx_wan_setup(struct wan_device *wandev, wandev_conf_t *conf);
77static int cycx_wan_shutdown(struct wan_device *wandev);
78
79
80static irqreturn_t cycx_isr(int irq, void *dev_id);
81
82
83
84
85
86
87static const char cycx_drvname[] = "cyclomx";
88static const char cycx_fullname[] = "CYCLOM 2X(tm) Sync Card Driver";
89static const char cycx_copyright[] = "(c) 1998-2003 Arnaldo Carvalho de Melo "
90 "<acme@conectiva.com.br>";
91static int cycx_ncards = CONFIG_CYCX_CARDS;
92static struct cycx_device *cycx_card_array;
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108static 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
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
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;
151out: return err;
152}
153
154
155
156
157
158
159static 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
172
173
174
175
176
177
178
179
180
181
182
183
184static 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
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
213 irq = conf->irq == 2 ? 9 : conf->irq;
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
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
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
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;
258out:
259 return rc;
260out_irq:
261 free_irq(irq, card);
262 goto out;
263}
264
265
266
267
268
269
270
271
272
273static int cycx_wan_shutdown(struct wan_device *wandev)
274{
275 int ret = -EFAULT;
276 struct cycx_device *card;
277
278
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);
291out: return ret;
292}
293
294
295
296
297
298
299
300static 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;
316out:
317 return IRQ_NONE;
318}
319
320
321void 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
345module_init(cycx_init);
346module_exit(cycx_exit);
347