Linux Audio

Check our new training course

Embedded Linux Audio

Check our new training course
with Creative Commons CC-BY-SA
lecture materials

Bootlin logo

Elixir Cross Referencer

Loading...
  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
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
/* $Id: tpam_commands.c,v 1.1.2.4 2001/11/06 20:58:30 kai Exp $
 *
 * Turbo PAM ISDN driver for Linux. (Kernel Driver - ISDN commands)
 *
 * Copyright 2001 Stelian Pop <stelian.pop@fr.alcove.com>, Alcôve
 *
 * This software may be used and distributed according to the terms
 * of the GNU General Public License, incorporated herein by reference.
 *
 * For all support questions please contact: <support@auvertech.fr>
 *
 */

#include <linux/module.h>
#include <linux/pci.h>
#include <linux/sched.h>
#include <linux/interrupt.h>
#include <asm/io.h>

#include <linux/isdn/tpam.h>
#include "tpam.h"

/* Local functions prototypes */
static int tpam_command_ioctl_dspload(tpam_card *, u32);
static int tpam_command_ioctl_dspsave(tpam_card *, u32);
static int tpam_command_ioctl_dsprun(tpam_card *);
static int tpam_command_ioctl_loopmode(tpam_card *, u8);
static int tpam_command_dial(tpam_card *, u32, u8 *);
static int tpam_command_setl2(tpam_card *, u32, u8);
static int tpam_command_acceptd(tpam_card *, u32);
static int tpam_command_acceptb(tpam_card *, u32);
static int tpam_command_hangup(tpam_card *, u32);
static int tpam_command_proceed(tpam_card *, u32);
static void tpam_statcallb_run(unsigned long);
static void tpam_statcallb(tpam_card *, isdn_ctrl);

/*
 * Function called when the ISDN link level send a command to the driver.
 *
 * 	c: ISDN command.
 *
 * Return: 0 if OK, <0 on errors.
 */
int tpam_command(isdn_ctrl *c) {
	tpam_card *card;
	unsigned long argp;

	dprintk("TurboPAM(tpam_command) card=%d, command=%d\n", 
		c->driver, c->command);	

	/* search for the board */
	if (!(card = tpam_findcard(c->driver))) {
		printk(KERN_ERR "TurboPAM(tpam_command): invalid driverId %d\n",
		       c->driver);	
		return -ENODEV;
	}

	/* dispatch the command */
	switch (c->command) {
		case ISDN_CMD_IOCTL:
			argp = c->parm.userdata;
			switch (c->arg) {
				case TPAM_CMD_DSPLOAD:
					return tpam_command_ioctl_dspload(card,
									  argp);
				case TPAM_CMD_DSPSAVE:
					return tpam_command_ioctl_dspsave(card,
									  argp);
				case TPAM_CMD_DSPRUN:
					return tpam_command_ioctl_dsprun(card);
				case TPAM_CMD_LOOPMODEON:
					return tpam_command_ioctl_loopmode(card,
									   1);
				case TPAM_CMD_LOOPMODEOFF:
					return tpam_command_ioctl_loopmode(card,
									   0);
				default:
					dprintk("TurboPAM(tpam_command): "
						"invalid tpam ioctl %ld\n", 
						c->arg);	
					return -EINVAL;
			}
		case ISDN_CMD_DIAL:
			return tpam_command_dial(card, c->arg, 
						 c->parm.setup.phone);
		case ISDN_CMD_ACCEPTD:
			return tpam_command_acceptd(card, c->arg);
		case ISDN_CMD_ACCEPTB:
			return tpam_command_acceptb(card, c->arg);
		case ISDN_CMD_HANGUP:
			return tpam_command_hangup(card, c->arg);
		case ISDN_CMD_SETL2:
			return tpam_command_setl2(card, c->arg & 0xff, 
						  c->arg >> 8);
		case ISDN_CMD_PROCEED:
			return tpam_command_proceed(card, c->arg);
		default:
			dprintk("TurboPAM(tpam_command): "
				"unknown or unused isdn ioctl %d\n", 
				c->command);	
			return -EINVAL;
	}

	/* not reached */
	return -EINVAL;
}

/*
 * Load some data into the board's memory.
 *
 * 	card: the board
 * 	arg: IOCTL argument containing the user space address of 
 * 		the tpam_dsp_ioctl structure describing the IOCTL.
 *
 * Return: 0 if OK, <0 on errors.
 */
static int tpam_command_ioctl_dspload(tpam_card *card, u32 arg) {
	tpam_dsp_ioctl tdl;

	dprintk("TurboPAM(tpam_command_ioctl_dspload): card=%d\n", card->id);

	/* get the IOCTL parameter from userspace */
	if (copy_from_user(&tdl, (void *)arg, sizeof(tpam_dsp_ioctl)))
		return -EFAULT;

	/* if the board's firmware was started, protect against writes
	 * to unallowed memory areas. If the board's firmware wasn't started,
	 * all is allowed. */
	if (card->running && tpam_verify_area(tdl.address, tdl.data_len)) 
		return -EPERM;

	/* write the data in the board's memory */
	return copy_from_user_to_pam(card, (void *)tdl.address, 
				     (void *)arg + sizeof(tpam_dsp_ioctl), 
				     tdl.data_len);
}

/*
 * Extract some data from the board's memory.
 *
 * 	card: the board
 * 	arg: IOCTL argument containing the user space address of 
 * 		the tpam_dsp_ioctl structure describing the IOCTL.
 *
 * Return: 0 if OK, <0 on errors.
 */
static int tpam_command_ioctl_dspsave(tpam_card *card, u32 arg) {
	tpam_dsp_ioctl tdl;

	dprintk("TurboPAM(tpam_command_ioctl_dspsave): card=%d\n", card->id);

	/* get the IOCTL parameter from userspace */
	if (copy_from_user(&tdl, (void *)arg, sizeof(tpam_dsp_ioctl)))
		return -EFAULT;

	/* protect against read from unallowed memory areas */
	if (tpam_verify_area(tdl.address, tdl.data_len)) 
		return -EPERM;

	/* read the data from the board's memory */
	return copy_from_pam_to_user(card, (void *)arg + sizeof(tpam_dsp_ioctl),
				     (void *)tdl.address, tdl.data_len);
}

/*
 * Launch the board's firmware. This function must be called after the 
 * firmware was loaded into the board's memory using TPAM_CMD_DSPLOAD 
 * IOCTL commands. After launching the firmware, this function creates
 * the NCOs and waits for their creation.
 *
 * 	card: the board
 *
 * Return: 0 if OK, <0 on errors.
 */
static int tpam_command_ioctl_dsprun(tpam_card *card) {
	u32 signature = 0, i;
	unsigned long timeout;
	isdn_ctrl ctrl;
	struct sk_buff *skb;

	dprintk("TurboPAM(tpam_command_ioctl_dsprun): card=%d\n", card->id);

	/* board must _not_ be running */
	if (card->running)
		return -EBUSY;

	/* reset the board */
	spin_lock_irq(&card->lock);
	copy_to_pam_dword(card, (void *)TPAM_MAGICNUMBER_REGISTER, 0xdeadface);
	readl(card->bar0 + TPAM_DSPINT_REGISTER);
	readl(card->bar0 + TPAM_HINTACK_REGISTER);
	spin_unlock_irq(&card->lock);
	
	/* wait for the board signature */
	timeout = jiffies + SIGNATURE_TIMEOUT;
	while (time_before(jiffies, timeout)) {
		spin_lock_irq(&card->lock);
		signature = copy_from_pam_dword(card, 
						(void *)TPAM_MAGICNUMBER_REGISTER);
		spin_unlock_irq(&card->lock);
		if (signature == TPAM_MAGICNUMBER)
			break;
		set_current_state(TASK_UNINTERRUPTIBLE);
		schedule_timeout(2);
	}

	/* signature not present -> board not started */
	if (signature != TPAM_MAGICNUMBER) {
		printk(KERN_ERR "TurboPAM(tpam_command_ioctl_dsprun): "
		       "card=%d, signature 0x%lx, expected 0x%lx\n", 
		       card->id, (unsigned long)signature, 
		       (unsigned long)TPAM_MAGICNUMBER);
		printk(KERN_ERR "TurboPAM(tpam_command_ioctl_dsprun): "
		       "card=%d, firmware not started\n", card->id);
		return -EIO;
	}

	/* the firmware is started */
	printk(KERN_INFO "TurboPAM: card=%d, firmware started\n", card->id);

	/* init the CRC routines */
	init_CRC();

	/* create all the NCOs */
	for (i = 0; i < TPAM_NBCHANNEL; ++i)
		if ((skb = build_ACreateNCOReq("")))
			tpam_enqueue(card, skb);

	/* wait for NCO creation confirmation */
	timeout = jiffies + NCOCREATE_TIMEOUT;
	while (time_before(jiffies, timeout)) {
		if (card->channels_tested == TPAM_NBCHANNEL)
			break;
		set_current_state(TASK_UNINTERRUPTIBLE);
		schedule_timeout(2);
	}

	card->running = 1;

	if (card->channels_tested != TPAM_NBCHANNEL)
		printk(KERN_ERR "TurboPAM(tpam_command_ioctl_dsprun): "
		       "card=%d, tried to init %d channels, "
		       "got reply from only %d channels\n", card->id, 
		       TPAM_NBCHANNEL, card->channels_tested);

	/* if all the channels were not initialized, signal to the ISDN
	 * link layer that fact that some channels are not usable */
	if (card->channels_used != TPAM_NBCHANNEL)
		for (i = card->channels_used; i < TPAM_NBCHANNEL; ++i) {
			ctrl.driver = card->id;
			ctrl.command = ISDN_STAT_DISCH;
			ctrl.arg = i;
			ctrl.parm.num[0] = 0;
			(* card->interface.statcallb)(&ctrl);
		}

	printk(KERN_INFO "TurboPAM: card=%d, ready, %d channels available\n", 
	       card->id, card->channels_used);

	/* let's rock ! */
	ctrl.driver = card->id;
	ctrl.command = ISDN_STAT_RUN;
	ctrl.arg = 0;
	tpam_statcallb(card, ctrl);

	return 0;
}

/* 
 * Set/reset the board's looptest mode.
 *
 * 	card: the board
 * 	mode: if 1, sets the board's looptest mode, if 0 resets it.
 *
 * Return: 0 if OK, <0 if error.
 */
static int tpam_command_ioctl_loopmode(tpam_card *card, u8 mode) {

	/* board must be running */
	if (!card->running)
		return -ENODEV;

	card->loopmode = mode;
	return 0;
}

/*
 * Issue a dial command. This function builds and sends a CConnectReq.
 * 
 * 	card: the board
 * 	channel: the channel number
 * 	phone: the remote phone number (EAZ)
 *
 * Return: 0 if OK, <0 if error.
 */
static int tpam_command_dial(tpam_card *card, u32 channel, u8 *phone) {
	struct sk_buff *skb;
	isdn_ctrl ctrl;

	dprintk("TurboPAM(tpam_command_dial): card=%d, channel=%lu, phone=%s\n",
		card->id, (unsigned long)channel, phone);

	/* board must be running */
	if (!card->running)
		return -ENODEV;

	/* initialize channel parameters */
	card->channels[channel].realhdlc = card->channels[channel].hdlc;
	card->channels[channel].hdlcshift = 0;
	card->channels[channel].readytoreceive = 0;

	/* build and send a CConnectReq */
	skb = build_CConnectReq(card->channels[channel].ncoid, phone, 
				card->channels[channel].realhdlc);
	if (!skb)
		return -ENOMEM;
	tpam_enqueue(card, skb);

	/* making a connection in modem mode is slow and causes the ISDN
	 * link layer to hangup the connection before even it gets a chance
	 * to establish... All we can do is simulate a successful connection
	 * for now, and send a DHUP later if the connection fails */
	if (!card->channels[channel].realhdlc) {
		ctrl.driver = card->id;
		ctrl.command = ISDN_STAT_DCONN;
		ctrl.arg = channel;
		tpam_statcallb(card, ctrl);
	}
	
	return 0;
}

/*
 * Set the level2 protocol (modem or HDLC).
 *
 * 	card: the board
 * 	channel: the channel number
 * 	proto: the level2 protocol (one of ISDN_PROTO_L2*)
 *
 * Return: 0 if OK, <0 if error.
 */
static int tpam_command_setl2(tpam_card *card, u32 channel, u8 proto) {

	dprintk("TurboPAM(tpam_command_setl2): card=%d, channel=%lu, proto=%d\n",
		card->id, (unsigned long)channel, proto);

	/* board must be running */
	if (!card->running)
		return -ENODEV;

	/* set the hdlc/modem mode */
	switch (proto) {
		case ISDN_PROTO_L2_HDLC:
			card->channels[channel].hdlc = 1;
			break;
		case ISDN_PROTO_L2_MODEM:
			card->channels[channel].hdlc = 0;
			break;
		default:
			return -EINVAL;
	}
	return 0;
}

/*
 * Accept a D-channel connection (incoming connection). This function
 * builds and sends a CConnectRsp message and signals DCONN to the ISDN
 * link level.
 *
 * 	card: the board
 * 	channel: the channel number
 *
 * Return: 0 if OK, <0 if error.
 */
static int tpam_command_acceptd(tpam_card *card, u32 channel) {
	isdn_ctrl ctrl;
	struct sk_buff *skb;

	dprintk("TurboPAM(tpam_command_acceptd): card=%d, channel=%lu\n",
		card->id, (unsigned long)channel);

	/* board must be running */
	if (!card->running)
		return -ENODEV;

	/* build and send a CConnectRsp */
	skb = build_CConnectRsp(card->channels[channel].ncoid);
	if (!skb)
		return -ENOMEM;
	tpam_enqueue(card, skb);

	/* issue DCONN to the ISDN link level */
	ctrl.driver = card->id;
	ctrl.command = ISDN_STAT_DCONN;
	ctrl.arg = channel;
	tpam_statcallb(card, ctrl);
	return 0;
}

/*
 * Accepts a B-channel connection. This is not used by the driver, 
 * since the TurboPAM is an active card hiding its B-channels from
 * us. We just signal BCONN to the ISDN link layer.
 *
 * 	card: the board
 * 	channel: the channel number
 *
 * Return: 0 if OK, <0 if error.
 */
static int tpam_command_acceptb(tpam_card *card, u32 channel) {
	isdn_ctrl ctrl;

	dprintk("TurboPAM(tpam_command_acceptb): card=%d, channel=%lu\n",
		card->id, (unsigned long)channel);

	/* board must be running */
	if (!card->running)
		return -ENODEV;

	/* issue BCONN to the ISDN link level */
	ctrl.driver = card->id;
	ctrl.command = ISDN_STAT_BCONN;
	ctrl.arg = channel;
	ctrl.parm.num[0] = '\0';
	tpam_statcallb(card, ctrl);
	return 0;
}

/*
 * Hang up a connection. This function builds and sends a CDisconnectReq.
 *
 * 	card: the board
 * 	channel: the channel number.
 *
 * Return: 0 if OK, <0 if error.
 */
static int tpam_command_hangup(tpam_card *card, u32 channel) {
	struct sk_buff *skb;

	dprintk("TurboPAM(tpam_command_hangup): card=%d, channel=%lu\n",
		card->id, (unsigned long)channel);

	/* board must be running */
	if (!card->running)
		return -ENODEV;

	/* build and send a CDisconnectReq */
	skb = build_CDisconnectReq(card->channels[channel].ncoid);
	if (!skb)
		return -ENOMEM;
	tpam_enqueue(card, skb);
	return 0;
}

/*
 * Proceed with an incoming connection. This function builds and sends a 
 * CConnectRsp.
 *
 * 	card: the board
 * 	channel: the channel number.
 *
 * Return: 0 if OK, <0 if error.
 */
static int tpam_command_proceed(tpam_card *card, u32 channel) {
	struct sk_buff *skb;

	dprintk("TurboPAM(tpam_command_proceed): card=%d, channel=%lu\n",
		card->id, (unsigned long)channel);

	/* board must be running */
	if (!card->running)
		return -ENODEV;

	/* build and send a CConnectRsp */
	skb = build_CConnectRsp(card->channels[channel].ncoid);
	if (!skb)
		return -ENOMEM;
	tpam_enqueue(card, skb);
	return 0;
}

/*
 * Send data through the board. This function encodes the data depending
 * on the connection type (modem or HDLC), then builds and sends a U3DataReq.
 *
 * 	driverId: the driver id (really meaning here the board)
 * 	channel: the channel number
 * 	ack: data needs to be acknowledged upon send
 * 	skb: sk_buff containing the data
 *
 * Return: size of data send if OK, <0 if error.
 */
int tpam_writebuf_skb(int driverId, int channel, int ack, struct sk_buff *skb) {
	tpam_card *card;
	int orig_size = skb->len;
	void *finaldata;
	u32 finallen;

	dprintk("TurboPAM(tpam_writebuf_skb): "
		"card=%d, channel=%ld, ack=%d, data size=%d\n", 
		driverId, (unsigned long)channel, ack, skb->len);

	/* find the board based on its driver ID */
	if (!(card = tpam_findcard(driverId))) {
		printk(KERN_ERR "TurboPAM(tpam_writebuf_skb): "
		       "invalid driverId %d\n", driverId);	
		return -ENODEV;
	}

	/* board must be running */
	if (!card->running)
		return -ENODEV;

	/* allocate some temporary memory */
	if (!(finaldata = (void *)__get_free_page(GFP_ATOMIC))) {
		printk(KERN_ERR "TurboPAM(tpam_writebuf_skb): "
		       "get_free_page failed\n");
		return -ENOMEM;
	}

	/* encode the data */
	if (!card->channels[channel].realhdlc) {
		/* modem mode */
		hdlc_encode_modem(skb->data, skb->len, finaldata, &finallen);
	}
	else {
		/* HDLC mode */
		void *tempdata;
		u32 templen;

		if (!(tempdata = (void *)__get_free_page(GFP_ATOMIC))) {
			printk(KERN_ERR "TurboPAM(tpam_writebuf_skb): "
			       "get_free_page failed\n");
			free_page((u32)finaldata);
			return -ENOMEM;
		}
		hdlc_no_accm_encode(skb->data, skb->len, tempdata, &templen);
		finallen = tpam_hdlc_encode(tempdata, finaldata, 
				       &card->channels[channel].hdlcshift, 
				       templen);
		free_page((u32)tempdata);
	}

	/* free the old sk_buff */
	kfree_skb(skb);

	/* build and send a U3DataReq */
	skb = build_U3DataReq(card->channels[channel].ncoid, finaldata, 
			      finallen, ack, orig_size);
	if (!skb) {
		free_page((u32)finaldata);
		return -ENOMEM;
	}
	tpam_enqueue_data(&card->channels[channel], skb);

	/* free the temporary memory */
	free_page((u32)finaldata);
	return orig_size;
}

/*
 * Treat a received ACreateNCOCnf message.
 *
 * 	card: the board
 * 	skb: the received message
 */
void tpam_recv_ACreateNCOCnf(tpam_card *card, struct sk_buff *skb) {
	u32 ncoid;
	u8 status;
	u32 channel;

	dprintk("TurboPAM(tpam_recv_ACreateNCOCnf): card=%d\n", card->id);

	/* parse the message contents */
	if (parse_ACreateNCOCnf(skb, &status, &ncoid))
		return;

	/* if the card is alreay running, it means that this message
	 * arrives too late... */
	if (card->running) {
		printk(KERN_ERR "TurboPAM(tpam_recv_ACreateNCOCnf): "
		       "ACreateNCOCnf received too late, status=%d\n", status);
		return;
	}

	/* the NCO creation failed, the corresponding channel will
	 * be unused */
	if (status) {
		printk(KERN_ERR "TurboPAM(tpam_recv_ACreateNCOCnf): "
		       "ACreateNCO failed, status=%d\n", status);
		card->channels_tested++;
		return;
	}

	/* find the first free channel and assign the nco ID to it */
	if ((channel = tpam_findchannel(card, TPAM_NCOID_INVALID)) == TPAM_CHANNEL_INVALID) {
		printk(KERN_ERR "TurboPAM(tpam_recv_ACreateNCOCnf): "
		       "All channels are assigned\n");
		return;
	}
	card->channels[channel].ncoid = ncoid;
	card->channels_tested++;
	card->channels_used++;
}

/*
 * Treat a received ADestroyNCOCnf message. Not used by the driver.
 *
 * 	card: the board
 * 	skb: the received message
 */
void tpam_recv_ADestroyNCOCnf(tpam_card *card, struct sk_buff *skb) {
	u32 ncoid;
	u8 status;
	u32 channel;

	dprintk("TurboPAM(tpam_recv_ADestroyNCOCnf): card=%d\n", card->id);

	/* parse the message contents */
	if (parse_ADestroyNCOCnf(skb, &status, &ncoid))
		return;
	
	if (status) {
		printk(KERN_ERR "TurboPAM(tpam_recv_ADestroyNCOCnf): "
		       "ADestroyNCO failed, status=%d\n", status);
		return;
	}

	/* clears the channel's nco ID */
	if ((channel = tpam_findchannel(card, ncoid)) == TPAM_CHANNEL_INVALID) {
		printk(KERN_ERR "TurboPAM(tpam_recv_ADestroyNCOCnf): "
		       "ncoid invalid %lu\n", (unsigned long)ncoid);
		return;
	}

	card->channels[channel].ncoid = TPAM_NCOID_INVALID;
}

/*
 * Treat a received CConnectCnf message.
 *
 * 	card: the board
 * 	skb: the received message
 */
void tpam_recv_CConnectCnf(tpam_card *card, struct sk_buff *skb) {
	u32 ncoid;
	u32 channel;
	isdn_ctrl ctrl;

	dprintk("TurboPAM(tpam_recv_CConnectCnf): card=%d\n", card->id);

	/* parse the message contents */
	if (parse_CConnectCnf(skb, &ncoid))
		return;

	/* find the channel by its nco ID */
	if ((channel = tpam_findchannel(card, ncoid)) == TPAM_CHANNEL_INVALID) {
		printk(KERN_ERR "TurboPAM(tpam_recv_CConnectCnf): "
		       "ncoid invalid %lu\n", (unsigned long)ncoid);
		return;
	}

	/* issue a DCONN command to the ISDN link layer if we are in HDLC mode.
	 * In modem mode, we alreay did it - the ISDN timer kludge */
	if (card->channels[channel].realhdlc) {
		ctrl.driver = card->id;
		ctrl.command = ISDN_STAT_DCONN;
		ctrl.arg = channel;
		(* card->interface.statcallb)(&ctrl);
	}
}

/*
 * Treat a received CConnectInd message. This function signals a ICALL
 * to the ISDN link layer.
 *
 * 	card: the board
 * 	skb: the received message
 */
void tpam_recv_CConnectInd(tpam_card *card, struct sk_buff *skb) {
	u32 ncoid;
	u32 channel;
	u8 hdlc, plan, screen;
	u8 calling[PHONE_MAXIMUMSIZE], called[PHONE_MAXIMUMSIZE];
	isdn_ctrl ctrl;
	int status;

	dprintk("TurboPAM(tpam_recv_CConnectInd): card=%d\n", card->id);

	/* parse the message contents */
	if (parse_CConnectInd(skb, &ncoid, &hdlc, calling, called, &plan, &screen))
		return;

	/* find the channel by its nco ID */
	if ((channel = tpam_findchannel(card, ncoid)) == TPAM_CHANNEL_INVALID) {
		printk(KERN_ERR "TurboPAM(tpam_recv_CConnectInd): "
		       "ncoid invalid %lu\n", (unsigned long)ncoid);
		return;
	}

	/* initialize the channel parameters */
	card->channels[channel].realhdlc = hdlc;
	card->channels[channel].hdlcshift = 0;
	card->channels[channel].readytoreceive = 0;

	/* issue a ICALL command to the ISDN link layer */
	ctrl.driver = card->id;
	ctrl.command = ISDN_STAT_ICALL;
	ctrl.arg = channel;
	memcpy(ctrl.parm.setup.phone, calling, 32);
	memcpy(ctrl.parm.setup.eazmsn, called, 32);
	ctrl.parm.setup.si1 = 7;	/* data capability */
	ctrl.parm.setup.si2 = 0;
	ctrl.parm.setup.plan = plan;
	ctrl.parm.setup.screen = screen;

	status = (* card->interface.statcallb)(&ctrl);
	switch (status) {
		case 1:
		case 4:
			/* call accepted, link layer will send us a ACCEPTD 
			 * command later */
			dprintk("TurboPAM(tpam_recv_CConnectInd): "
				"card=%d, channel=%d, icall waiting, status=%d\n", 
				card->id, channel, status);
			break;
		default:
			/* call denied, we build and send a CDisconnectReq */
			dprintk("TurboPAM(tpam_recv_CConnectInd): "
				"card=%d, channel=%d, icall denied, status=%d\n", 
				card->id, channel, status);
			skb = build_CDisconnectReq(ncoid);
			if (!skb)
				return;
			tpam_enqueue(card, skb);
	}
}

/*
 * Treat a received CDisconnectInd message. This function signals a DHUP and
 * a BHUP to the ISDN link layer.
 *
 * 	card: the board
 * 	skb: the received message
 */
void tpam_recv_CDisconnectInd(tpam_card *card, struct sk_buff *skb) {
	u32 ncoid;
	u32 channel;
	u32 cause;
	isdn_ctrl ctrl;

	dprintk("TurboPAM(tpam_recv_CDisconnectInd): card=%d\n", card->id);

	/* parse the message contents */
	if (parse_CDisconnectInd(skb, &ncoid, &cause))
		return;

	/* find the channel by its nco ID */
	if ((channel = tpam_findchannel(card, ncoid)) == TPAM_CHANNEL_INVALID) {
		printk(KERN_ERR "TurboPAM(tpam_recv_CDisconnectInd): "
		       "ncoid invalid %lu\n", (unsigned long)ncoid);
		return;
	}

	/* build and send a CDisconnectRsp */
	skb = build_CDisconnectRsp(ncoid);
	if (!skb)
		return;
	tpam_enqueue(card, skb);

	/* issue a DHUP to the ISDN link layer */
	ctrl.driver = card->id;
	ctrl.command = ISDN_STAT_DHUP;
	ctrl.arg = channel;
	(* card->interface.statcallb)(&ctrl);

	/* issue a BHUP to the ISDN link layer */
	ctrl.driver = card->id;
	ctrl.command = ISDN_STAT_BHUP;
	ctrl.arg = channel;
	(* card->interface.statcallb)(&ctrl);
}

/*
 * Treat a received CDisconnectCnf message. This function signals a DHUP and
 * a BHUP to the ISDN link layer.
 *
 * 	card: the board
 * 	skb: the received message
 */
void tpam_recv_CDisconnectCnf(tpam_card *card, struct sk_buff *skb) {
	u32 ncoid;
	u32 channel;
	u32 cause;
	isdn_ctrl ctrl;

	dprintk("TurboPAM(tpam_recv_CDisconnectCnf): card=%d\n", card->id);

	/* parse the message contents */
	if (parse_CDisconnectCnf(skb, &ncoid, &cause))
		return;

	/* find the channel by its nco ID */
	if ((channel = tpam_findchannel(card, ncoid)) == TPAM_CHANNEL_INVALID) {
		printk(KERN_ERR "TurboPAM(tpam_recv_CDisconnectCnf): "
		       "ncoid invalid %lu\n", (unsigned long)ncoid);
		return;
	}

	/* issue a DHUP to the ISDN link layer */
	ctrl.driver = card->id;
	ctrl.command = ISDN_STAT_DHUP;
	ctrl.arg = channel;
	(* card->interface.statcallb)(&ctrl);

	/* issue a BHUP to the ISDN link layer */
	ctrl.driver = card->id;
	ctrl.command = ISDN_STAT_BHUP;
	ctrl.arg = channel;
	(* card->interface.statcallb)(&ctrl);
}

/*
 * Treat a received U3DataInd message. This function decodes the data
 * depending on the connection type (modem or HDLC) and passes it to the
 * ISDN link layer by using rcvcallb_skb.
 *
 * 	card: the board
 * 	skb: the received message + data
 */
void tpam_recv_U3DataInd(tpam_card *card, struct sk_buff *skb) {
	u32 ncoid;
	u32 channel;
	u8 *data;
	u16 len;
	struct sk_buff *result;

	dprintk("TurboPAM(tpam_recv_U3DataInd): card=%d, datalen=%d\n", 
		card->id, skb->len);

	/* parse the message contents */
	if (parse_U3DataInd(skb, &ncoid, &data, &len))
		return;

	/* find the channel by its nco ID */
	if ((channel = tpam_findchannel(card, ncoid)) == TPAM_CHANNEL_INVALID) {
		printk(KERN_ERR "TurboPAM(tpam_recv_U3DataInd): "
		       "ncoid invalid %lu\n", (unsigned long)ncoid);
		return;
	}

	/* decode the data */
	if (card->channels[ncoid].realhdlc) {
		/* HDLC mode */
		u8 *tempdata;
		u32 templen;

		if (!(tempdata = (void *)__get_free_page(GFP_ATOMIC))) {
			printk(KERN_ERR "TurboPAM(tpam_recv_U3DataInd): "
			       "get_free_page failed\n");
			return;
		}
		templen = tpam_hdlc_decode(data, tempdata, len);
		templen = hdlc_no_accm_decode(tempdata, templen);
		if (!(result = alloc_skb(templen, GFP_ATOMIC))) {
			printk(KERN_ERR "TurboPAM(tpam_recv_U3DataInd): "
			       "alloc_skb failed\n");
			free_page((u32)tempdata);
			return;
		}
		memcpy(skb_put(result, templen), tempdata, templen);
		free_page((u32)tempdata);
	}
	else {
		/* modem mode */
		if (!(result = alloc_skb(len, GFP_ATOMIC))) {
			printk(KERN_ERR "TurboPAM(tpam_recv_U3DataInd): "
			       "alloc_skb failed\n");
			return;
		}
		memcpy(skb_put(result, len), data, len);
	}

	/* In loop mode, resend the data immediately */
	if (card->loopmode) {
		struct sk_buff *loopskb;

		if (!(loopskb = alloc_skb(skb->len, GFP_ATOMIC))) {
			printk(KERN_ERR "TurboPAM(tpam_recv_U3DataInd): "
			       "alloc_skb failed\n");
			kfree_skb(result);
			return;
		}
		memcpy(skb_put(loopskb, result->len), result->data, 
		       result->len);
		if (tpam_writebuf_skb(card->id, channel, 0, loopskb) < 0)
			kfree_skb(loopskb);
	}

	/* pass the data to the ISDN link layer */
	(* card->interface.rcvcallb_skb)(card->id, channel, result);
}

/*
 * Treat a received U3ReadyToReceiveInd message. This function sets the
 * channel ready flag and triggers the send of data if the channel becomed
 * ready.
 *
 * 	card: the board
 * 	skb: the received message + data
 */
void tpam_recv_U3ReadyToReceiveInd(tpam_card *card, struct sk_buff *skb) {
	u32 ncoid;
	u32 channel;
	u8 ready;

	dprintk("TurboPAM(tpam_recv_U3ReadyToReceiveInd): card=%d\n", card->id);

	/* parse the message contents */
	if (parse_U3ReadyToReceiveInd(skb, &ncoid, &ready))
		return;

	/* find the channel by its nco ID */
	if ((channel = tpam_findchannel(card, ncoid)) == TPAM_CHANNEL_INVALID) {
		printk(KERN_ERR "TurboPAM(tpam_recv_U3ReadyToReceiveInd): "
		       "ncoid invalid %lu\n", (unsigned long)ncoid);
		return;
	}

	/* set the readytoreceive flag */
	card->channels[channel].readytoreceive = ready;

	/* if the channel just becomed ready, trigger the send of queued data */
	if (ready)
		tpam_enqueue_data(&card->channels[channel], NULL);
}

/*
 * Runs the delayed statcallb when its timer expires.
 *
 * 	parm: pointer to the tpam_statcallb_data statcallb argument.
 */
static void tpam_statcallb_run(unsigned long parm) {
	tpam_statcallb_data *ds = (tpam_statcallb_data *)parm;

	dprintk("TurboPAM(tpam_statcallb_run)\n");

	(* ds->card->interface.statcallb)(&ds->ctrl);

	kfree(ds->timer);
	kfree(ds);
}

/*
 * Queues a statcallb call for delayed invocation.
 *
 * 	card: the board
 * 	ctrl: the statcallb argument
 */
static void tpam_statcallb(tpam_card *card, isdn_ctrl ctrl) {
	struct timer_list *timer;
	tpam_statcallb_data *ds;

	dprintk("TurboPAM(tpam_statcallb): card=%d\n", card->id);

	if (!(timer = (struct timer_list *) kmalloc(sizeof(struct timer_list), 
						    GFP_ATOMIC))) {
		printk(KERN_ERR "TurboPAM: tpam_statcallb: kmalloc failed!\n");
		return;
	}

	if (!(ds = (tpam_statcallb_data *) kmalloc(sizeof(tpam_statcallb_data),
						   GFP_ATOMIC))) {
		printk(KERN_ERR "TurboPAM: tpam_statcallb: kmalloc failed!\n");
		kfree(timer);
		return;
	}
	ds->card = card;
	ds->timer = timer;
	memcpy(&ds->ctrl, &ctrl, sizeof(isdn_ctrl));

	init_timer(timer);
	timer->function = tpam_statcallb_run;
	timer->data = (unsigned long)ds;
	timer->expires = jiffies + HZ / 10;   /* 0.1 second */
	add_timer(timer);
}