1 /* $Id: b1.c,v 1.1.2.2 2004/01/16 21:09:27 keil Exp $
2  *
3  * Common module for AVM B1 cards.
4  *
5  * Copyright 1999 by Carsten Paeth <calle@calle.de>
6  *
7  * This software may be used and distributed according to the terms
8  * of the GNU General Public License, incorporated herein by reference.
9  *
10  */
11 
12 #include <linux/module.h>
13 #include <linux/kernel.h>
14 #include <linux/pci.h>
15 #include <linux/proc_fs.h>
16 #include <linux/seq_file.h>
17 #include <linux/skbuff.h>
18 #include <linux/delay.h>
19 #include <linux/mm.h>
20 #include <linux/interrupt.h>
21 #include <linux/ioport.h>
22 #include <linux/capi.h>
23 #include <linux/kernelcapi.h>
24 #include <linux/slab.h>
25 #include <asm/io.h>
26 #include <linux/init.h>
27 #include <asm/uaccess.h>
28 #include <linux/netdevice.h>
29 #include <linux/isdn/capilli.h>
30 #include "avmcard.h"
31 #include <linux/isdn/capicmd.h>
32 #include <linux/isdn/capiutil.h>
33 
34 static char *revision = "$Revision: 1.1.2.2 $";
35 
36 /* ------------------------------------------------------------- */
37 
38 MODULE_DESCRIPTION("CAPI4Linux: Common support for active AVM cards");
39 MODULE_AUTHOR("Carsten Paeth");
40 MODULE_LICENSE("GPL");
41 
42 /* ------------------------------------------------------------- */
43 
44 int b1_irq_table[16] =
45 {0,
46  0,
47  0,
48  192,				/* irq 3 */
49  32,				/* irq 4 */
50  160,				/* irq 5 */
51  96,				/* irq 6 */
52  224,				/* irq 7 */
53  0,
54  64,				/* irq 9 */
55  80,				/* irq 10 */
56  208,				/* irq 11 */
57  48,				/* irq 12 */
58  0,
59  0,
60  112,				/* irq 15 */
61 };
62 
63 /* ------------------------------------------------------------- */
64 
b1_alloc_card(int nr_controllers)65 avmcard *b1_alloc_card(int nr_controllers)
66 {
67 	avmcard *card;
68 	avmctrl_info *cinfo;
69 	int i;
70 
71 	card = kzalloc(sizeof(*card), GFP_KERNEL);
72 	if (!card)
73 		return NULL;
74 
75 	cinfo = kzalloc(sizeof(*cinfo) * nr_controllers, GFP_KERNEL);
76 	if (!cinfo) {
77 		kfree(card);
78 		return NULL;
79 	}
80 
81 	card->ctrlinfo = cinfo;
82 	for (i = 0; i < nr_controllers; i++) {
83 		INIT_LIST_HEAD(&cinfo[i].ncci_head);
84 		cinfo[i].card = card;
85 	}
86 	spin_lock_init(&card->lock);
87 	card->nr_controllers = nr_controllers;
88 
89 	return card;
90 }
91 
92 /* ------------------------------------------------------------- */
93 
b1_free_card(avmcard * card)94 void b1_free_card(avmcard *card)
95 {
96 	kfree(card->ctrlinfo);
97 	kfree(card);
98 }
99 
100 /* ------------------------------------------------------------- */
101 
b1_detect(unsigned int base,enum avmcardtype cardtype)102 int b1_detect(unsigned int base, enum avmcardtype cardtype)
103 {
104 	int onoff, i;
105 
106 	/*
107 	 * Statusregister 0000 00xx
108 	 */
109 	if ((inb(base + B1_INSTAT) & 0xfc)
110 	    || (inb(base + B1_OUTSTAT) & 0xfc))
111 		return 1;
112 	/*
113 	 * Statusregister 0000 001x
114 	 */
115 	b1outp(base, B1_INSTAT, 0x2);	/* enable irq */
116 	/* b1outp(base, B1_OUTSTAT, 0x2); */
117 	if ((inb(base + B1_INSTAT) & 0xfe) != 0x2
118 	    /* || (inb(base + B1_OUTSTAT) & 0xfe) != 0x2 */)
119 		return 2;
120 	/*
121 	 * Statusregister 0000 000x
122 	 */
123 	b1outp(base, B1_INSTAT, 0x0);	/* disable irq */
124 	b1outp(base, B1_OUTSTAT, 0x0);
125 	if ((inb(base + B1_INSTAT) & 0xfe)
126 	    || (inb(base + B1_OUTSTAT) & 0xfe))
127 		return 3;
128 
129 	for (onoff = !0, i= 0; i < 10 ; i++) {
130 		b1_set_test_bit(base, cardtype, onoff);
131 		if (b1_get_test_bit(base, cardtype) != onoff)
132 		   return 4;
133 		onoff = !onoff;
134 	}
135 
136 	if (cardtype == avm_m1)
137 	   return 0;
138 
139         if ((b1_rd_reg(base, B1_STAT1(cardtype)) & 0x0f) != 0x01)
140 	   return 5;
141 
142 	return 0;
143 }
144 
b1_getrevision(avmcard * card)145 void b1_getrevision(avmcard *card)
146 {
147     card->class = inb(card->port + B1_ANALYSE);
148     card->revision = inb(card->port + B1_REVISION);
149 }
150 
151 #define FWBUF_SIZE	256
b1_load_t4file(avmcard * card,capiloaddatapart * t4file)152 int b1_load_t4file(avmcard *card, capiloaddatapart * t4file)
153 {
154 	unsigned char buf[FWBUF_SIZE];
155 	unsigned char *dp;
156 	int i, left;
157 	unsigned int base = card->port;
158 
159 	dp = t4file->data;
160 	left = t4file->len;
161 	while (left > FWBUF_SIZE) {
162 		if (t4file->user) {
163 			if (copy_from_user(buf, dp, FWBUF_SIZE))
164 				return -EFAULT;
165 		} else {
166 			memcpy(buf, dp, FWBUF_SIZE);
167 		}
168 		for (i = 0; i < FWBUF_SIZE; i++)
169 			if (b1_save_put_byte(base, buf[i]) < 0) {
170 				printk(KERN_ERR "%s: corrupted firmware file ?\n",
171 						card->name);
172 				return -EIO;
173 			}
174 		left -= FWBUF_SIZE;
175 		dp += FWBUF_SIZE;
176 	}
177 	if (left) {
178 		if (t4file->user) {
179 			if (copy_from_user(buf, dp, left))
180 				return -EFAULT;
181 		} else {
182 			memcpy(buf, dp, left);
183 		}
184 		for (i = 0; i < left; i++)
185 			if (b1_save_put_byte(base, buf[i]) < 0) {
186 				printk(KERN_ERR "%s: corrupted firmware file ?\n",
187 						card->name);
188 				return -EIO;
189 			}
190 	}
191 	return 0;
192 }
193 
b1_load_config(avmcard * card,capiloaddatapart * config)194 int b1_load_config(avmcard *card, capiloaddatapart * config)
195 {
196 	unsigned char buf[FWBUF_SIZE];
197 	unsigned char *dp;
198 	unsigned int base = card->port;
199 	int i, j, left;
200 
201 	dp = config->data;
202 	left = config->len;
203 	if (left) {
204 		b1_put_byte(base, SEND_CONFIG);
205         	b1_put_word(base, 1);
206 		b1_put_byte(base, SEND_CONFIG);
207         	b1_put_word(base, left);
208 	}
209 	while (left > FWBUF_SIZE) {
210 		if (config->user) {
211 			if (copy_from_user(buf, dp, FWBUF_SIZE))
212 				return -EFAULT;
213 		} else {
214 			memcpy(buf, dp, FWBUF_SIZE);
215 		}
216 		for (i = 0; i < FWBUF_SIZE; ) {
217 			b1_put_byte(base, SEND_CONFIG);
218 			for (j=0; j < 4; j++) {
219 				b1_put_byte(base, buf[i++]);
220 			}
221 		}
222 		left -= FWBUF_SIZE;
223 		dp += FWBUF_SIZE;
224 	}
225 	if (left) {
226 		if (config->user) {
227 			if (copy_from_user(buf, dp, left))
228 				return -EFAULT;
229 		} else {
230 			memcpy(buf, dp, left);
231 		}
232 		for (i = 0; i < left; ) {
233 			b1_put_byte(base, SEND_CONFIG);
234 			for (j=0; j < 4; j++) {
235 				if (i < left)
236 					b1_put_byte(base, buf[i++]);
237 				else
238 					b1_put_byte(base, 0);
239 			}
240 		}
241 	}
242 	return 0;
243 }
244 
b1_loaded(avmcard * card)245 int b1_loaded(avmcard *card)
246 {
247 	unsigned int base = card->port;
248 	unsigned long stop;
249 	unsigned char ans;
250 	unsigned long tout = 2;
251 
252 	for (stop = jiffies + tout * HZ; time_before(jiffies, stop);) {
253 		if (b1_tx_empty(base))
254 			break;
255 	}
256 	if (!b1_tx_empty(base)) {
257 		printk(KERN_ERR "%s: b1_loaded: tx err, corrupted t4 file ?\n",
258 				card->name);
259 		return 0;
260 	}
261 	b1_put_byte(base, SEND_POLL);
262 	for (stop = jiffies + tout * HZ; time_before(jiffies, stop);) {
263 		if (b1_rx_full(base)) {
264 			if ((ans = b1_get_byte(base)) == RECEIVE_POLL) {
265 				return 1;
266 			}
267 			printk(KERN_ERR "%s: b1_loaded: got 0x%x, firmware not running\n",
268 					card->name, ans);
269 			return 0;
270 		}
271 	}
272 	printk(KERN_ERR "%s: b1_loaded: firmware not running\n", card->name);
273 	return 0;
274 }
275 
276 /* ------------------------------------------------------------- */
277 
b1_load_firmware(struct capi_ctr * ctrl,capiloaddata * data)278 int b1_load_firmware(struct capi_ctr *ctrl, capiloaddata *data)
279 {
280 	avmctrl_info *cinfo = (avmctrl_info *)(ctrl->driverdata);
281 	avmcard *card = cinfo->card;
282 	unsigned int port = card->port;
283 	unsigned long flags;
284 	int retval;
285 
286 	b1_reset(port);
287 
288 	if ((retval = b1_load_t4file(card, &data->firmware))) {
289 		b1_reset(port);
290 		printk(KERN_ERR "%s: failed to load t4file!!\n",
291 					card->name);
292 		return retval;
293 	}
294 
295 	b1_disable_irq(port);
296 
297 	if (data->configuration.len > 0 && data->configuration.data) {
298 		if ((retval = b1_load_config(card, &data->configuration))) {
299 			b1_reset(port);
300 			printk(KERN_ERR "%s: failed to load config!!\n",
301 					card->name);
302 			return retval;
303 		}
304 	}
305 
306 	if (!b1_loaded(card)) {
307 		printk(KERN_ERR "%s: failed to load t4file.\n", card->name);
308 		return -EIO;
309 	}
310 
311 	spin_lock_irqsave(&card->lock, flags);
312 	b1_setinterrupt(port, card->irq, card->cardtype);
313 	b1_put_byte(port, SEND_INIT);
314 	b1_put_word(port, CAPI_MAXAPPL);
315 	b1_put_word(port, AVM_NCCI_PER_CHANNEL*2);
316 	b1_put_word(port, ctrl->cnr - 1);
317 	spin_unlock_irqrestore(&card->lock, flags);
318 
319 	return 0;
320 }
321 
b1_reset_ctr(struct capi_ctr * ctrl)322 void b1_reset_ctr(struct capi_ctr *ctrl)
323 {
324 	avmctrl_info *cinfo = (avmctrl_info *)(ctrl->driverdata);
325 	avmcard *card = cinfo->card;
326 	unsigned int port = card->port;
327 	unsigned long flags;
328 
329 	b1_reset(port);
330 	b1_reset(port);
331 
332 	memset(cinfo->version, 0, sizeof(cinfo->version));
333 	spin_lock_irqsave(&card->lock, flags);
334 	capilib_release(&cinfo->ncci_head);
335 	spin_unlock_irqrestore(&card->lock, flags);
336 	capi_ctr_down(ctrl);
337 }
338 
b1_register_appl(struct capi_ctr * ctrl,u16 appl,capi_register_params * rp)339 void b1_register_appl(struct capi_ctr *ctrl,
340 				u16 appl,
341 				capi_register_params *rp)
342 {
343 	avmctrl_info *cinfo = (avmctrl_info *)(ctrl->driverdata);
344 	avmcard *card = cinfo->card;
345 	unsigned int port = card->port;
346 	unsigned long flags;
347 	int nconn, want = rp->level3cnt;
348 
349 	if (want > 0) nconn = want;
350 	else nconn = ctrl->profile.nbchannel * -want;
351 	if (nconn == 0) nconn = ctrl->profile.nbchannel;
352 
353 	spin_lock_irqsave(&card->lock, flags);
354 	b1_put_byte(port, SEND_REGISTER);
355 	b1_put_word(port, appl);
356 	b1_put_word(port, 1024 * (nconn+1));
357 	b1_put_word(port, nconn);
358 	b1_put_word(port, rp->datablkcnt);
359 	b1_put_word(port, rp->datablklen);
360 	spin_unlock_irqrestore(&card->lock, flags);
361 }
362 
b1_release_appl(struct capi_ctr * ctrl,u16 appl)363 void b1_release_appl(struct capi_ctr *ctrl, u16 appl)
364 {
365 	avmctrl_info *cinfo = (avmctrl_info *)(ctrl->driverdata);
366 	avmcard *card = cinfo->card;
367 	unsigned int port = card->port;
368 	unsigned long flags;
369 
370 	spin_lock_irqsave(&card->lock, flags);
371 	capilib_release_appl(&cinfo->ncci_head, appl);
372 	b1_put_byte(port, SEND_RELEASE);
373 	b1_put_word(port, appl);
374 	spin_unlock_irqrestore(&card->lock, flags);
375 }
376 
b1_send_message(struct capi_ctr * ctrl,struct sk_buff * skb)377 u16 b1_send_message(struct capi_ctr *ctrl, struct sk_buff *skb)
378 {
379 	avmctrl_info *cinfo = (avmctrl_info *)(ctrl->driverdata);
380 	avmcard *card = cinfo->card;
381 	unsigned int port = card->port;
382 	unsigned long flags;
383 	u16 len = CAPIMSG_LEN(skb->data);
384 	u8 cmd = CAPIMSG_COMMAND(skb->data);
385 	u8 subcmd = CAPIMSG_SUBCOMMAND(skb->data);
386 	u16 dlen, retval;
387 
388 	spin_lock_irqsave(&card->lock, flags);
389 	if (CAPICMD(cmd, subcmd) == CAPI_DATA_B3_REQ) {
390 		retval = capilib_data_b3_req(&cinfo->ncci_head,
391 					     CAPIMSG_APPID(skb->data),
392 					     CAPIMSG_NCCI(skb->data),
393 					     CAPIMSG_MSGID(skb->data));
394 		if (retval != CAPI_NOERROR) {
395 			spin_unlock_irqrestore(&card->lock, flags);
396 			return retval;
397 		}
398 
399 		dlen = CAPIMSG_DATALEN(skb->data);
400 
401 		b1_put_byte(port, SEND_DATA_B3_REQ);
402 		b1_put_slice(port, skb->data, len);
403 		b1_put_slice(port, skb->data + len, dlen);
404 	} else {
405 		b1_put_byte(port, SEND_MESSAGE);
406 		b1_put_slice(port, skb->data, len);
407 	}
408 	spin_unlock_irqrestore(&card->lock, flags);
409 
410 	dev_kfree_skb_any(skb);
411 	return CAPI_NOERROR;
412 }
413 
414 /* ------------------------------------------------------------- */
415 
b1_parse_version(avmctrl_info * cinfo)416 void b1_parse_version(avmctrl_info *cinfo)
417 {
418 	struct capi_ctr *ctrl = &cinfo->capi_ctrl;
419 	avmcard *card = cinfo->card;
420 	capi_profile *profp;
421 	u8 *dversion;
422 	u8 flag;
423 	int i, j;
424 
425 	for (j = 0; j < AVM_MAXVERSION; j++)
426 		cinfo->version[j] = "\0\0" + 1;
427 	for (i = 0, j = 0;
428 	     j < AVM_MAXVERSION && i < cinfo->versionlen;
429 	     j++, i += cinfo->versionbuf[i] + 1)
430 		cinfo->version[j] = &cinfo->versionbuf[i + 1];
431 
432 	strlcpy(ctrl->serial, cinfo->version[VER_SERIAL], sizeof(ctrl->serial));
433 	memcpy(&ctrl->profile, cinfo->version[VER_PROFILE],sizeof(capi_profile));
434 	strlcpy(ctrl->manu, "AVM GmbH", sizeof(ctrl->manu));
435 	dversion = cinfo->version[VER_DRIVER];
436 	ctrl->version.majorversion = 2;
437 	ctrl->version.minorversion = 0;
438 	ctrl->version.majormanuversion = (((dversion[0] - '0') & 0xf) << 4);
439 	ctrl->version.majormanuversion |= ((dversion[2] - '0') & 0xf);
440 	ctrl->version.minormanuversion = (dversion[3] - '0') << 4;
441 	ctrl->version.minormanuversion |=
442 			(dversion[5] - '0') * 10 + ((dversion[6] - '0') & 0xf);
443 
444 	profp = &ctrl->profile;
445 
446 	flag = ((u8 *)(profp->manu))[1];
447 	switch (flag) {
448 	case 0: if (cinfo->version[VER_CARDTYPE])
449 	           strcpy(cinfo->cardname, cinfo->version[VER_CARDTYPE]);
450 	        else strcpy(cinfo->cardname, "B1");
451 		break;
452 	case 3: strcpy(cinfo->cardname,"PCMCIA B"); break;
453 	case 4: strcpy(cinfo->cardname,"PCMCIA M1"); break;
454 	case 5: strcpy(cinfo->cardname,"PCMCIA M2"); break;
455 	case 6: strcpy(cinfo->cardname,"B1 V3.0"); break;
456 	case 7: strcpy(cinfo->cardname,"B1 PCI"); break;
457 	default: sprintf(cinfo->cardname, "AVM?%u", (unsigned int)flag); break;
458         }
459         printk(KERN_NOTICE "%s: card %d \"%s\" ready.\n",
460 				card->name, ctrl->cnr, cinfo->cardname);
461 
462         flag = ((u8 *)(profp->manu))[3];
463         if (flag)
464 		printk(KERN_NOTICE "%s: card %d Protocol:%s%s%s%s%s%s%s\n",
465 			card->name,
466 			ctrl->cnr,
467 			(flag & 0x01) ? " DSS1" : "",
468 			(flag & 0x02) ? " CT1" : "",
469 			(flag & 0x04) ? " VN3" : "",
470 			(flag & 0x08) ? " NI1" : "",
471 			(flag & 0x10) ? " AUSTEL" : "",
472 			(flag & 0x20) ? " ESS" : "",
473 			(flag & 0x40) ? " 1TR6" : ""
474 			);
475 
476         flag = ((u8 *)(profp->manu))[5];
477 	if (flag)
478 		printk(KERN_NOTICE "%s: card %d Linetype:%s%s%s%s\n",
479 			card->name,
480 			ctrl->cnr,
481 			(flag & 0x01) ? " point to point" : "",
482 			(flag & 0x02) ? " point to multipoint" : "",
483 			(flag & 0x08) ? " leased line without D-channel" : "",
484 			(flag & 0x04) ? " leased line with D-channel" : ""
485 			);
486 }
487 
488 /* ------------------------------------------------------------- */
489 
b1_interrupt(int interrupt,void * devptr)490 irqreturn_t b1_interrupt(int interrupt, void *devptr)
491 {
492 	avmcard *card = devptr;
493 	avmctrl_info *cinfo = &card->ctrlinfo[0];
494 	struct capi_ctr *ctrl = &cinfo->capi_ctrl;
495 	unsigned char b1cmd;
496 	struct sk_buff *skb;
497 
498 	unsigned ApplId;
499 	unsigned MsgLen;
500 	unsigned DataB3Len;
501 	unsigned NCCI;
502 	unsigned WindowSize;
503 	unsigned long flags;
504 
505 	spin_lock_irqsave(&card->lock, flags);
506 
507 	if (!b1_rx_full(card->port)) {
508 		spin_unlock_irqrestore(&card->lock, flags);
509 		return IRQ_NONE;
510 	}
511 
512 	b1cmd = b1_get_byte(card->port);
513 
514 	switch (b1cmd) {
515 
516 	case RECEIVE_DATA_B3_IND:
517 
518 		ApplId = (unsigned) b1_get_word(card->port);
519 		MsgLen = b1_get_slice(card->port, card->msgbuf);
520 		DataB3Len = b1_get_slice(card->port, card->databuf);
521 		spin_unlock_irqrestore(&card->lock, flags);
522 
523 		if (MsgLen < 30) { /* not CAPI 64Bit */
524 			memset(card->msgbuf+MsgLen, 0, 30-MsgLen);
525 			MsgLen = 30;
526 			CAPIMSG_SETLEN(card->msgbuf, 30);
527 		}
528 		if (!(skb = alloc_skb(DataB3Len + MsgLen, GFP_ATOMIC))) {
529 			printk(KERN_ERR "%s: incoming packet dropped\n",
530 					card->name);
531 		} else {
532 			memcpy(skb_put(skb, MsgLen), card->msgbuf, MsgLen);
533 			memcpy(skb_put(skb, DataB3Len), card->databuf, DataB3Len);
534 			capi_ctr_handle_message(ctrl, ApplId, skb);
535 		}
536 		break;
537 
538 	case RECEIVE_MESSAGE:
539 
540 		ApplId = (unsigned) b1_get_word(card->port);
541 		MsgLen = b1_get_slice(card->port, card->msgbuf);
542 		if (!(skb = alloc_skb(MsgLen, GFP_ATOMIC))) {
543 			printk(KERN_ERR "%s: incoming packet dropped\n",
544 					card->name);
545 			spin_unlock_irqrestore(&card->lock, flags);
546 		} else {
547 			memcpy(skb_put(skb, MsgLen), card->msgbuf, MsgLen);
548 			if (CAPIMSG_CMD(skb->data) == CAPI_DATA_B3_CONF)
549 				capilib_data_b3_conf(&cinfo->ncci_head, ApplId,
550 						     CAPIMSG_NCCI(skb->data),
551 						     CAPIMSG_MSGID(skb->data));
552 			spin_unlock_irqrestore(&card->lock, flags);
553 			capi_ctr_handle_message(ctrl, ApplId, skb);
554 		}
555 		break;
556 
557 	case RECEIVE_NEW_NCCI:
558 
559 		ApplId = b1_get_word(card->port);
560 		NCCI = b1_get_word(card->port);
561 		WindowSize = b1_get_word(card->port);
562 		capilib_new_ncci(&cinfo->ncci_head, ApplId, NCCI, WindowSize);
563 		spin_unlock_irqrestore(&card->lock, flags);
564 		break;
565 
566 	case RECEIVE_FREE_NCCI:
567 
568 		ApplId = b1_get_word(card->port);
569 		NCCI = b1_get_word(card->port);
570 		if (NCCI != 0xffffffff)
571 			capilib_free_ncci(&cinfo->ncci_head, ApplId, NCCI);
572 		spin_unlock_irqrestore(&card->lock, flags);
573 		break;
574 
575 	case RECEIVE_START:
576 	   	/* b1_put_byte(card->port, SEND_POLLACK); */
577 		spin_unlock_irqrestore(&card->lock, flags);
578 		capi_ctr_resume_output(ctrl);
579 		break;
580 
581 	case RECEIVE_STOP:
582 		spin_unlock_irqrestore(&card->lock, flags);
583 		capi_ctr_suspend_output(ctrl);
584 		break;
585 
586 	case RECEIVE_INIT:
587 
588 		cinfo->versionlen = b1_get_slice(card->port, cinfo->versionbuf);
589 		spin_unlock_irqrestore(&card->lock, flags);
590 		b1_parse_version(cinfo);
591 		printk(KERN_INFO "%s: %s-card (%s) now active\n",
592 		       card->name,
593 		       cinfo->version[VER_CARDTYPE],
594 		       cinfo->version[VER_DRIVER]);
595 		capi_ctr_ready(ctrl);
596 		break;
597 
598 	case RECEIVE_TASK_READY:
599 		ApplId = (unsigned) b1_get_word(card->port);
600 		MsgLen = b1_get_slice(card->port, card->msgbuf);
601 		spin_unlock_irqrestore(&card->lock, flags);
602 		card->msgbuf[MsgLen] = 0;
603 		while (    MsgLen > 0
604 		       && (   card->msgbuf[MsgLen-1] == '\n'
605 			   || card->msgbuf[MsgLen-1] == '\r')) {
606 			card->msgbuf[MsgLen-1] = 0;
607 			MsgLen--;
608 		}
609 		printk(KERN_INFO "%s: task %d \"%s\" ready.\n",
610 				card->name, ApplId, card->msgbuf);
611 		break;
612 
613 	case RECEIVE_DEBUGMSG:
614 		MsgLen = b1_get_slice(card->port, card->msgbuf);
615 		spin_unlock_irqrestore(&card->lock, flags);
616 		card->msgbuf[MsgLen] = 0;
617 		while (    MsgLen > 0
618 		       && (   card->msgbuf[MsgLen-1] == '\n'
619 			   || card->msgbuf[MsgLen-1] == '\r')) {
620 			card->msgbuf[MsgLen-1] = 0;
621 			MsgLen--;
622 		}
623 		printk(KERN_INFO "%s: DEBUG: %s\n", card->name, card->msgbuf);
624 		break;
625 
626 	case 0xff:
627 		spin_unlock_irqrestore(&card->lock, flags);
628 		printk(KERN_ERR "%s: card removed ?\n", card->name);
629 		return IRQ_NONE;
630 	default:
631 		spin_unlock_irqrestore(&card->lock, flags);
632 		printk(KERN_ERR "%s: b1_interrupt: 0x%x ???\n",
633 				card->name, b1cmd);
634 		return IRQ_HANDLED;
635 	}
636 	return IRQ_HANDLED;
637 }
638 
639 /* ------------------------------------------------------------- */
b1ctl_proc_show(struct seq_file * m,void * v)640 static int b1ctl_proc_show(struct seq_file *m, void *v)
641 {
642 	struct capi_ctr *ctrl = m->private;
643 	avmctrl_info *cinfo = (avmctrl_info *)(ctrl->driverdata);
644 	avmcard *card = cinfo->card;
645 	u8 flag;
646 	char *s;
647 
648 	seq_printf(m, "%-16s %s\n", "name", card->name);
649 	seq_printf(m, "%-16s 0x%x\n", "io", card->port);
650 	seq_printf(m, "%-16s %d\n", "irq", card->irq);
651 	switch (card->cardtype) {
652 	case avm_b1isa: s = "B1 ISA"; break;
653 	case avm_b1pci: s = "B1 PCI"; break;
654 	case avm_b1pcmcia: s = "B1 PCMCIA"; break;
655 	case avm_m1: s = "M1"; break;
656 	case avm_m2: s = "M2"; break;
657 	case avm_t1isa: s = "T1 ISA (HEMA)"; break;
658 	case avm_t1pci: s = "T1 PCI"; break;
659 	case avm_c4: s = "C4"; break;
660 	case avm_c2: s = "C2"; break;
661 	default: s = "???"; break;
662 	}
663 	seq_printf(m, "%-16s %s\n", "type", s);
664 	if (card->cardtype == avm_t1isa)
665 		seq_printf(m, "%-16s %d\n", "cardnr", card->cardnr);
666 	if ((s = cinfo->version[VER_DRIVER]) != NULL)
667 		seq_printf(m, "%-16s %s\n", "ver_driver", s);
668 	if ((s = cinfo->version[VER_CARDTYPE]) != NULL)
669 		seq_printf(m, "%-16s %s\n", "ver_cardtype", s);
670 	if ((s = cinfo->version[VER_SERIAL]) != NULL)
671 		seq_printf(m, "%-16s %s\n", "ver_serial", s);
672 
673 	if (card->cardtype != avm_m1) {
674         	flag = ((u8 *)(ctrl->profile.manu))[3];
675         	if (flag)
676 			seq_printf(m, "%-16s%s%s%s%s%s%s%s\n",
677 			"protocol",
678 			(flag & 0x01) ? " DSS1" : "",
679 			(flag & 0x02) ? " CT1" : "",
680 			(flag & 0x04) ? " VN3" : "",
681 			(flag & 0x08) ? " NI1" : "",
682 			(flag & 0x10) ? " AUSTEL" : "",
683 			(flag & 0x20) ? " ESS" : "",
684 			(flag & 0x40) ? " 1TR6" : ""
685 			);
686 	}
687 	if (card->cardtype != avm_m1) {
688         	flag = ((u8 *)(ctrl->profile.manu))[5];
689 		if (flag)
690 			seq_printf(m, "%-16s%s%s%s%s\n",
691 			"linetype",
692 			(flag & 0x01) ? " point to point" : "",
693 			(flag & 0x02) ? " point to multipoint" : "",
694 			(flag & 0x08) ? " leased line without D-channel" : "",
695 			(flag & 0x04) ? " leased line with D-channel" : ""
696 			);
697 	}
698 	seq_printf(m, "%-16s %s\n", "cardname", cinfo->cardname);
699 
700 	return 0;
701 }
702 
b1ctl_proc_open(struct inode * inode,struct file * file)703 static int b1ctl_proc_open(struct inode *inode, struct file *file)
704 {
705 	return single_open(file, b1ctl_proc_show, PDE(inode)->data);
706 }
707 
708 const struct file_operations b1ctl_proc_fops = {
709 	.owner		= THIS_MODULE,
710 	.open		= b1ctl_proc_open,
711 	.read		= seq_read,
712 	.llseek		= seq_lseek,
713 	.release	= single_release,
714 };
715 EXPORT_SYMBOL(b1ctl_proc_fops);
716 
717 /* ------------------------------------------------------------- */
718 
719 #ifdef CONFIG_PCI
720 
721 avmcard_dmainfo *
avmcard_dma_alloc(char * name,struct pci_dev * pdev,long rsize,long ssize)722 avmcard_dma_alloc(char *name, struct pci_dev *pdev, long rsize, long ssize)
723 {
724 	avmcard_dmainfo *p;
725 	void *buf;
726 
727 	p = kzalloc(sizeof(avmcard_dmainfo), GFP_KERNEL);
728 	if (!p) {
729 		printk(KERN_WARNING "%s: no memory.\n", name);
730 		goto err;
731 	}
732 
733 	p->recvbuf.size = rsize;
734 	buf = pci_alloc_consistent(pdev, rsize, &p->recvbuf.dmaaddr);
735 	if (!buf) {
736 		printk(KERN_WARNING "%s: allocation of receive dma buffer failed.\n", name);
737 		goto err_kfree;
738 	}
739 	p->recvbuf.dmabuf = buf;
740 
741 	p->sendbuf.size = ssize;
742 	buf = pci_alloc_consistent(pdev, ssize, &p->sendbuf.dmaaddr);
743 	if (!buf) {
744 		printk(KERN_WARNING "%s: allocation of send dma buffer failed.\n", name);
745 		goto err_free_consistent;
746 	}
747 
748 	p->sendbuf.dmabuf = buf;
749 	skb_queue_head_init(&p->send_queue);
750 
751 	return p;
752 
753  err_free_consistent:
754 	pci_free_consistent(p->pcidev, p->recvbuf.size,
755 			    p->recvbuf.dmabuf, p->recvbuf.dmaaddr);
756  err_kfree:
757 	kfree(p);
758  err:
759 	return NULL;
760 }
761 
avmcard_dma_free(avmcard_dmainfo * p)762 void avmcard_dma_free(avmcard_dmainfo *p)
763 {
764 	pci_free_consistent(p->pcidev, p->recvbuf.size,
765 			    p->recvbuf.dmabuf, p->recvbuf.dmaaddr);
766 	pci_free_consistent(p->pcidev, p->sendbuf.size,
767 			    p->sendbuf.dmabuf, p->sendbuf.dmaaddr);
768 	skb_queue_purge(&p->send_queue);
769 	kfree(p);
770 }
771 
772 EXPORT_SYMBOL(avmcard_dma_alloc);
773 EXPORT_SYMBOL(avmcard_dma_free);
774 
775 #endif
776 
777 EXPORT_SYMBOL(b1_irq_table);
778 
779 EXPORT_SYMBOL(b1_alloc_card);
780 EXPORT_SYMBOL(b1_free_card);
781 EXPORT_SYMBOL(b1_detect);
782 EXPORT_SYMBOL(b1_getrevision);
783 EXPORT_SYMBOL(b1_load_t4file);
784 EXPORT_SYMBOL(b1_load_config);
785 EXPORT_SYMBOL(b1_loaded);
786 EXPORT_SYMBOL(b1_load_firmware);
787 EXPORT_SYMBOL(b1_reset_ctr);
788 EXPORT_SYMBOL(b1_register_appl);
789 EXPORT_SYMBOL(b1_release_appl);
790 EXPORT_SYMBOL(b1_send_message);
791 
792 EXPORT_SYMBOL(b1_parse_version);
793 EXPORT_SYMBOL(b1_interrupt);
794 
b1_init(void)795 static int __init b1_init(void)
796 {
797 	char *p;
798 	char rev[32];
799 
800 	if ((p = strchr(revision, ':')) != NULL && p[1]) {
801 		strlcpy(rev, p + 2, 32);
802 		if ((p = strchr(rev, '$')) != NULL && p > rev)
803 		   *(p-1) = 0;
804 	} else
805 		strcpy(rev, "1.0");
806 
807 	printk(KERN_INFO "b1: revision %s\n", rev);
808 
809 	return 0;
810 }
811 
b1_exit(void)812 static void __exit b1_exit(void)
813 {
814 }
815 
816 module_init(b1_init);
817 module_exit(b1_exit);
818