summaryrefslogtreecommitdiff
path: root/drivers/dahdi/dahdi_dynamic_ethmf.c
blob: 126001f5a6283b6e7f1891e7a8fea2029389b9f6 (plain)
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
/*
 * Dynamic Span Interface for DAHDI (Multi-Span Ethernet Interface)
 *
 * Written by Joseph Benden <joe@thrallingpenguin.com>
 *
 * Copyright (C) 2007-2010, Thralling Penguin LLC.
 *
 * All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 */

#include <linux/kernel.h>
#include <linux/errno.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/spinlock.h>
#include <linux/slab.h>
#include <linux/kmod.h>
#include <linux/sched.h>
#include <linux/interrupt.h>
#include <linux/moduleparam.h>
#include <linux/netdevice.h>
#include <linux/notifier.h>
#include <linux/crc32.h>

/**
 * Undefine USE_PROC_FS, if you do not want the /proc/dahdi/dynamic-ethmf
 * support. Undefining this would give a slight performance increase.
 */
#define USE_PROC_FS

#ifdef USE_PROC_FS
# include <linux/proc_fs.h>
# include <asm/atomic.h>
#endif

#ifdef CONFIG_DEVFS_FS
# include <linux/devfs_fs_kernel.h>
#endif

#include <dahdi/kernel.h>
#include <dahdi/user.h>

#define ETH_P_ZTDETH			0xd00d
#define ETHMF_MAX_PER_SPAN_GROUP	8
#define ETHMF_MAX_GROUPS		16
#define ETHMF_FLAG_IGNORE_CHAN0	(1 << 3)
#define ETHMF_MAX_SPANS			4

struct ztdeth_header {
	unsigned short subaddr;
};

/* Timer for enabling spans - used to combat a lock problem */
static struct timer_list timer;

/* Whether or not the timer has been deleted */
static atomic_t timer_deleted = ATOMIC_INIT(0);

/* Global error counter */
static atomic_t errcount = ATOMIC_INIT(0);

/* Whether or not we are in shutdown */
static atomic_t shutdown = ATOMIC_INIT(0);

static struct sk_buff_head skbs;

#ifdef USE_PROC_FS
struct ethmf_group {
	unsigned int hash_addr;
	atomic_t spans;
	atomic_t rxframecount;
	atomic_t txframecount;
	atomic_t rxbytecount;
	atomic_t txbytecount;
	atomic_t devupcount;
	atomic_t devdowncount;
};
static struct ethmf_group ethmf_groups[ETHMF_MAX_GROUPS];
#endif

struct ztdeth {
	/* Destination MAC address */
	unsigned char addr[ETH_ALEN];
	/* Destination MAC address hash value */
	unsigned int addr_hash;
	/* span sub-address, in network byte order */
	unsigned short subaddr;
	/* DAHDI span associated with this TDMoE-mf span */
	struct dahdi_span *span;
	/* Ethernet interface name */
	char ethdev[IFNAMSIZ];
	/* Ethernet device reference */
	struct net_device *dev;
	/* trx buffer */
	unsigned char *msgbuf;
	/* trx buffer length */
	int msgbuf_len;
	/* wether or not this frame is ready for trx */
	atomic_t ready;
	/* delay counter, to ensure all spans are added, prior to usage */
	atomic_t delay;
	/* rvc buffer */
	unsigned char *rcvbuf;
	/* the number of channels in this span */
	int real_channels;
	/* use padding if 1, else no padding */
	atomic_t no_front_padding;
	/* counter to pseudo lock the rcvbuf */
	atomic_t refcnt;

	struct list_head list;
};

/**
 * Lock for adding and removing items in ethmf_list
 */
static DEFINE_SPINLOCK(ethmf_lock);

/**
 * The active list of all running spans
 */
static LIST_HEAD(ethmf_list);

static inline void ethmf_errors_inc(void)
{
#ifdef USE_PROC_FS
	atomic_inc(&errcount);
#endif
}

#ifdef USE_PROC_FS
static inline int hashaddr_to_index(unsigned int hash_addr)
{
	int i, z = -1;
	for (i = 0; i < ETHMF_MAX_GROUPS; ++i) {
		if (z == -1 && ethmf_groups[i].hash_addr == 0)
			z = i;
		if (ethmf_groups[i].hash_addr == hash_addr)
			return i;
	}
	if (z != -1) {
		ethmf_groups[z].hash_addr = hash_addr;
	}
	return z;
}
#endif

/**
 * Find the Ztdeth Struct and DAHDI span for a given MAC address and subaddr.
 *
 * NOTE: RCU read lock must already be held.
 */
static inline void find_ethmf(const unsigned char *addr,
	const unsigned short subaddr, struct ztdeth **ze,
	struct dahdi_span **span)
{
	struct ztdeth *z;

	list_for_each_entry_rcu(z, &ethmf_list, list) {
		if (!atomic_read(&z->delay)) {
			if (!memcmp(addr, z->addr, ETH_ALEN)
					&& z->subaddr == subaddr) {
				*ze = z;
				*span = z->span;
				return;
			}
		}
	}

	/* no results */
	*ze = NULL;
	*span = NULL;
}

/**
 * Determines if all spans are ready for transmit. If all spans are ready,
 * we return the number of spans which indeed are ready and populate the
 * array of pointers to those spans..
 *
 * NOTE: RCU read lock must already be held.
 */
static inline int ethmf_trx_spans_ready(unsigned int addr_hash, struct ztdeth *(*ready_spans)[ETHMF_MAX_PER_SPAN_GROUP])
{
	struct ztdeth *t;
	int span_count = 0, spans_ready = 0;

	list_for_each_entry_rcu(t, &ethmf_list, list) {
		if (!atomic_read(&t->delay) && t->addr_hash == addr_hash) {
			++span_count;
			if (atomic_read(&t->ready)) {
				short subaddr = ntohs(t->subaddr);
				if (subaddr < ETHMF_MAX_PER_SPAN_GROUP) {
					(*ready_spans)[subaddr] = t;
					++spans_ready;
				} else {
					printk(KERN_ERR "More than %d spans per multi-frame group are not currently supported.",
						ETHMF_MAX_PER_SPAN_GROUP);
				}
			}
		}
	}

	if (span_count && spans_ready && span_count == spans_ready) {
		return spans_ready;
	}
	return 0;
}

/**
 * Ethernet receiving side processing function.
 */
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 14)
static int ztdethmf_rcv(struct sk_buff *skb, struct net_device *dev,
		struct packet_type *pt, struct net_device *orig_dev)
#else
static int ztdethmf_rcv(struct sk_buff *skb, struct net_device *dev,
		struct packet_type *pt)
#endif
{
	int num_spans = 0, span_index = 0;
	unsigned char *data;
	struct dahdi_span *span;
	struct ztdeth *z = NULL;
	struct ztdeth_header *zh;
	unsigned int samples, channels, rbslen, flags;
	unsigned int skip = 0;

#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 22)
	zh = (struct ztdeth_header *) skb_network_header(skb);
#else
	zh = (struct ztdeth_header *) skb->nh.raw;
#endif
	if (ntohs(zh->subaddr) & 0x8000) {
		/* got a multi-span frame */
		num_spans = ntohs(zh->subaddr) & 0xFF;

		/* Currently max of 4 spans supported */
		if (unlikely(num_spans > ETHMF_MAX_SPANS)) {
			kfree_skb(skb);
			return 0;
		}

		skb_pull(skb, sizeof(struct ztdeth_header));
#ifdef NEW_SKB_LINEARIZE
		if (skb_is_nonlinear(skb))
			skb_linearize(skb);
#else
		if (skb_is_nonlinear(skb))
			skb_linearize(skb, GFP_KERNEL);
#endif
		data = (unsigned char *) skb->data;

		rcu_read_lock();
		do {
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 9)
			find_ethmf(eth_hdr(skb)->h_source,
				htons(span_index), &z, &span);
#else
			find_ethmf(skb->mac.ethernet->h_source,
				htons(span_index), &z, &span);
#endif
			if (unlikely(!z || !span)) {
				/* The recv'd span does not belong to us */
				/* ethmf_errors_inc(); */
				++span_index;
				continue;
			}

			samples = data[(span_index * 6)] & 0xFF;
			flags = data[((span_index * 6) + 1)] & 0xFF;
			channels = data[((span_index * 6) + 5)] & 0xFF;

			/* Precomputed defaults for most typical values */
			if (channels == 24)
				rbslen = 12;
			else if (channels == 31)
				rbslen = 16;
			else
				rbslen = ((channels + 3) / 4) * 2;

			if (unlikely(samples != 8 || channels >= 32 || channels == 0)) {
				ethmf_errors_inc();
				++span_index;
				continue;
			}

			if (atomic_dec_and_test(&z->refcnt) == 0) {
				memcpy(z->rcvbuf, data + 6*span_index, 6); /* TDM Header */
				/*
				 * If we ignore channel zero we must skip the first eight bytes and
				 * ensure that ztdynamic doesn't get confused by this new flag
				 */
				if (flags & ETHMF_FLAG_IGNORE_CHAN0) {
					skip = 8;

					/* Remove this flag since ztdynamic may not understand it */
					z->rcvbuf[1] = flags & ~(ETHMF_FLAG_IGNORE_CHAN0);

					/* Additionally, now we will transmit with front padding */
					atomic_set(&z->no_front_padding, 0);
				} else {
					/* Disable front padding if we recv'd a packet without it */
					atomic_set(&z->no_front_padding, 1);
				}
				memcpy(z->rcvbuf + 6, data + 6*num_spans + 16
					*span_index, rbslen); /* RBS Header */

				/* 256 == 32*8; if padding lengths change, this must be modified */
				memcpy(z->rcvbuf + 6 + rbslen, data + 6*num_spans + 16
					*num_spans + (256)*span_index + skip, channels
					* 8); /* Payload */

				dahdi_dynamic_receive(span, z->rcvbuf, 6 + rbslen
					+ channels*8);
			} else {
				ethmf_errors_inc();
				printk(KERN_INFO "TDMoE span overflow detected. Span %d was dropped.", span_index);
			}
			atomic_inc(&z->refcnt);

#ifdef USE_PROC_FS
			if (span_index == 0) {
				atomic_inc(&(ethmf_groups[hashaddr_to_index(z->addr_hash)].rxframecount));
				atomic_add(skb->len + z->dev->hard_header_len +
					sizeof(struct ztdeth_header),
					&(ethmf_groups[hashaddr_to_index(z->addr_hash)].rxbytecount));
			}
#endif
			++span_index;
		} while (!atomic_read(&shutdown) && span_index < num_spans);
		rcu_read_unlock();
	}

	kfree_skb(skb);
	return 0;
}

static int ztdethmf_notifier(struct notifier_block *block, unsigned long event,
		void *ptr)
{
	struct net_device *dev = ptr;
	struct ztdeth *z;

	switch (event) {
	case NETDEV_GOING_DOWN:
	case NETDEV_DOWN:
		rcu_read_lock();
		list_for_each_entry_rcu(z, &ethmf_list, list) {
			/* Note that the device no longer exists */
			if (z->dev == dev) {
				z->dev = NULL;
#ifdef USE_PROC_FS
				atomic_inc(&(ethmf_groups[hashaddr_to_index(z->addr_hash)].devdowncount));
#endif
			}
		}
		rcu_read_unlock();
		break;
	case NETDEV_UP:
		rcu_read_lock();
		list_for_each_entry_rcu(z, &ethmf_list, list) {
			/* Now that the device exists again, use it */
			if (!strcmp(z->ethdev, dev->name)) {
				z->dev = dev;
#ifdef USE_PROC_FS
				atomic_inc(&(ethmf_groups[hashaddr_to_index(z->addr_hash)].devupcount));
#endif
			}
		}
		rcu_read_unlock();
		break;
	}
	return 0;
}

static int ztdethmf_transmit(void *pvt, unsigned char *msg, int msglen)
{
	struct ztdeth *z = pvt, *ready_spans[ETHMF_MAX_PER_SPAN_GROUP];
	struct sk_buff *skb;
	struct ztdeth_header *zh;
	struct net_device *dev;
	unsigned char addr[ETH_ALEN];
	int spans_ready = 0, index = 0;
#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 10)
	static DEFINE_SPINLOCK(lock);
	unsigned long flags;
#endif

	if (atomic_read(&shutdown))
		return 0;

	rcu_read_lock();

	if (unlikely(!z || !z->dev)) {
		rcu_read_unlock();
		return 0;
	}

#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 10)
	if (!atomic_read(&z->ready)) {
		spin_lock_irqsave(&lock, flags);
		atomic_inc(&z->ready);
		if (1 == atomic_read(&z->ready)) {
			memcpy(z->msgbuf, msg, msglen);
			z->msgbuf_len = msglen;
		}
		spin_unlock_irqrestore(&lock, flags);
	}
#else
	if (!atomic_read(&z->ready)) {
		if (atomic_inc_return(&z->ready) == 1) {
			memcpy(z->msgbuf, msg, msglen);
			z->msgbuf_len = msglen;
		}
	}
#endif

	spans_ready = ethmf_trx_spans_ready(z->addr_hash, &ready_spans);
	if (spans_ready) {
		int pad[ETHMF_MAX_SPANS], rbs[ETHMF_MAX_SPANS];

		dev = z->dev;
		memcpy(addr, z->addr, sizeof(z->addr));

		for (index = 0; index < spans_ready; index++) {
			int chan = ready_spans[index]->real_channels;
			/* By default we pad to 32 channels, but if
			 * no_front_padding is false then we have a pad
			 * in the front of 8 bytes, so this implies one
			 * less channel
			 */
			if (atomic_read(&(ready_spans[index]->no_front_padding)))
				pad[index] = (32 - chan)*8;
			else
				pad[index] = (31 - chan)*8;

			if (chan == 24)
				rbs[index] = 12;
			else if (chan == 31)
				rbs[index] = 16;
			else
				/* Shouldn't this be index, not spans_ready? */
				rbs[spans_ready] = ((chan + 3) / 4) * 2;
		}

		/* Allocate the standard size for a 32-chan frame */
		skb = dev_alloc_skb(1112 + dev->hard_header_len
			+ sizeof(struct ztdeth_header) + 32);
		if (unlikely(!skb)) {
			rcu_read_unlock();
			ethmf_errors_inc();
			return 0;
		}

		/* Reserve header space */
		skb_reserve(skb, dev->hard_header_len
				+ sizeof(struct ztdeth_header));
		/* copy each spans header */
		for (index = 0; index < spans_ready; index++) {
			if (!atomic_read(&(ready_spans[index]->no_front_padding)))
				ready_spans[index]->msgbuf[1]
					|= ETHMF_FLAG_IGNORE_CHAN0;

			memcpy(skb_put(skb, 6), ready_spans[index]->msgbuf, 6);
		}

		/* copy each spans RBS payload */
		for (index = 0; index < spans_ready; index++) {
			memcpy(skb_put(skb, 16), ready_spans[index]->msgbuf + 6,
				rbs[index]);
		}

		/* copy each spans data/voice payload */
		for (index = 0; index < spans_ready; index++) {
			int chan = ready_spans[index]->real_channels;
			if (!atomic_read(&(ready_spans[index]->no_front_padding))) {
				/* This adds an additional (padded) channel to our total */
				memset(skb_put(skb, 8), 0xA5, 8); /* ETHMF_IGNORE_CHAN0 */
			}
			memcpy(skb_put(skb, chan*8), ready_spans[index]->msgbuf
					+ (6 + rbs[index]), chan*8);
			if (pad[index] > 0) {
				memset(skb_put(skb, pad[index]), 0xDD, pad[index]);
			}

			/* mark span as ready for new data/voice */
			atomic_set(&(ready_spans[index]->ready), 0);
		}

		/* Throw on header */
		zh = (struct ztdeth_header *)skb_push(skb,
				sizeof(struct ztdeth_header));
		zh->subaddr = htons((unsigned short)(0x8000 | (unsigned char)(spans_ready & 0xFF)));

		/* Setup protocol type */
		skb->protocol = __constant_htons(ETH_P_ZTDETH);
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 22)
		skb_set_network_header(skb, 0);
#else
		skb->nh.raw = skb->data;
#endif
		skb->dev = dev;
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 24)
		dev_hard_header(skb, dev, ETH_P_ZTDETH, addr, dev->dev_addr, skb->len);
#else
		if (dev->hard_header)
			dev->hard_header(skb, dev, ETH_P_ZTDETH, addr,
					dev->dev_addr, skb->len);
#endif
		/* queue frame for delivery */
		if (dev) {
			skb_queue_tail(&skbs, skb);
		}
#ifdef USE_PROC_FS
		atomic_inc(&(ethmf_groups[hashaddr_to_index(z->addr_hash)].txframecount));
		atomic_add(skb->len, &(ethmf_groups[hashaddr_to_index(z->addr_hash)].txbytecount));
#endif
	}

	rcu_read_unlock();

	return 0;
}

static int ztdethmf_flush(void)
{
	struct sk_buff *skb;

	/* Handle all transmissions now */
	while ((skb = skb_dequeue(&skbs))) {
		dev_queue_xmit(skb);
	}
	return 0;
}

static struct packet_type ztdethmf_ptype = {
	.type = __constant_htons(ETH_P_ZTDETH),	/* Protocol */
	.dev  = NULL,				/* Device (NULL = wildcard) */
	.func = ztdethmf_rcv,			/* Receiver */
};

static void ztdethmf_destroy(void *pvt)
{
	struct ztdeth *z = pvt;
	unsigned long flags;

	atomic_set(&shutdown, 1);
	synchronize_rcu();

	spin_lock_irqsave(&ethmf_lock, flags);
	list_del_rcu(&z->list);
	spin_unlock_irqrestore(&ethmf_lock, flags);
	synchronize_rcu();
	atomic_dec(&(ethmf_groups[hashaddr_to_index(z->addr_hash)].spans));

	if (z) { /* Successfully removed */
		printk(KERN_INFO "Removed interface for %s\n",
			z->span->name);
		kfree(z->msgbuf);
		kfree(z);
		module_put(THIS_MODULE);
	} else {
		if (z && z->span && z->span->name) {
			printk(KERN_ERR "Cannot find interface for %s\n",
				z->span->name);
		}
	}
}

static void *ztdethmf_create(struct dahdi_span *span, char *addr)
{
	struct ztdeth *z;
	char src[256];
	char *src_ptr;
	int x, bufsize, num_matched;
	unsigned long flags;

	BUG_ON(!span);
	BUG_ON(!addr);

	z = kmalloc(sizeof(struct ztdeth), GFP_KERNEL);
	if (!z)
		return NULL;

	/* Zero it out */
	memset(z, 0, sizeof(struct ztdeth));

	/* set a delay for xmit/recv to workaround Zaptel problems */
	atomic_set(&z->delay, 4);

	/* create a msg buffer. MAX OF 31 CHANNELS!!!! */
	bufsize = 31 * DAHDI_CHUNKSIZE + 31 / 4 + 48;
	z->msgbuf = kmalloc(bufsize, GFP_KERNEL);
	z->rcvbuf = kmalloc(bufsize, GFP_KERNEL);

	/* Address should be <dev>/<macaddr>/subaddr */
	dahdi_copy_string(src, addr, sizeof(src));
	/* replace all / with space; otherwise kernel sscanf does not work */
	src_ptr = src;
	while (*src_ptr) {
		if (*src_ptr == '/')
			*src_ptr = ' ';
		++src_ptr;
	}
	num_matched = sscanf(src,
			"%16s %hhx:%hhx:%hhx:%hhx:%hhx:%hhx %hu",
			z->ethdev, &z->addr[0], &z->addr[1],
			&z->addr[2], &z->addr[3], &z->addr[4],
			&z->addr[5], &z->subaddr);
	if (8 != num_matched) {
		printk(KERN_ERR "Only matched %d entries in '%s'\n", num_matched, src);
		printk(KERN_ERR "Invalid TDMoE Multiframe address: %s\n", addr);
		kfree(z);
		return NULL;
	}
	z->dev = dev_get_by_name(
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 24)
		&init_net,
#endif
		z->ethdev);
	if (!z->dev) {
		printk(KERN_ERR "TDMoE Multiframe: Invalid device '%s'\n", z->ethdev);
		kfree(z);
		return NULL;
	}
	z->span = span;
	z->subaddr = htons(z->subaddr);
	z->addr_hash = crc32_le(0, z->addr, ETH_ALEN);
	z->real_channels = span->channels;

	src[0] = '\0';
	for (x = 0; x < 5; x++)
		sprintf(src + strlen(src), "%02x:", z->dev->dev_addr[x]);
	sprintf(src + strlen(src), "%02x", z->dev->dev_addr[5]);

	printk(KERN_INFO "TDMoEmf: Added new interface for %s at %s "
		"(addr=%s, src=%s, subaddr=%d)\n", span->name, z->dev->name,
		addr, src, ntohs(z->subaddr));

	atomic_set(&z->ready, 0);
	atomic_set(&z->refcnt, 0);

	spin_lock_irqsave(&ethmf_lock, flags);
	list_add_rcu(&z->list, &ethmf_list);
	spin_unlock_irqrestore(&ethmf_lock, flags);
	atomic_inc(&(ethmf_groups[hashaddr_to_index(z->addr_hash)].spans));

	if (!try_module_get(THIS_MODULE))
		printk(KERN_ERR "TDMoEmf: Unable to increment module use count\n");

	/* enable the timer for enabling the spans */
	mod_timer(&timer, jiffies + HZ);
	atomic_set(&shutdown, 0);
	return z;
}

static struct dahdi_dynamic_driver ztd_ethmf = {
	"ethmf",
	"Ethernet",
	ztdethmf_create,
	ztdethmf_destroy,
	ztdethmf_transmit,
	ztdethmf_flush
};

static struct notifier_block ztdethmf_nblock = {
	.notifier_call = ztdethmf_notifier,
};

/**
 * Decrements each delay counter in the ethmf_list and returns the number of
 * delay counters that are not equal to zero.
 */
static int ethmf_delay_dec(void)
{
	struct ztdeth *z;
	int count_nonzero = 0;

	rcu_read_lock();
	list_for_each_entry_rcu(z, &ethmf_list, list) {
		if (atomic_read(&z->delay)) {
			atomic_dec(&z->delay);
			++count_nonzero;
		} else
			atomic_set(&z->delay, 0);
	}
	rcu_read_unlock();
	return count_nonzero;
}

/**
 * Timer callback function to allow all spans to be added, prior to any of
 * them being used.
 */
static void timer_callback(unsigned long param)
{
	if (ethmf_delay_dec()) {
		if (!atomic_read(&timer_deleted)) {
			timer.expires = jiffies + HZ;
			add_timer(&timer);
		}
	} else {
		printk(KERN_INFO "All TDMoE multiframe span groups are active.\n");
		del_timer(&timer);
	}
}

#ifdef USE_PROC_FS
static struct proc_dir_entry *proc_entry;
static const char *ztdethmf_procname = "dahdi/dynamic-ethmf";
static int ztdethmf_proc_read(char *page, char **start, off_t off, int count,
		int *eof, void *data)
{
	struct ztdeth *z = NULL;
	int len = 0, i = 0;
	unsigned int group = 0, c = 0;

	rcu_read_lock();

	len += sprintf(page + len, "Errors: %d\n\n", atomic_read(&errcount));

	for (group = 0; group < ETHMF_MAX_GROUPS; ++group) {
		if (atomic_read(&(ethmf_groups[group].spans))) {
			len += sprintf(page + len, "Group #%d (0x%x)\n", i++, ethmf_groups[group].hash_addr);
			len += sprintf(page + len, "  Spans: %d\n",
				atomic_read(&(ethmf_groups[group].spans)));

			c = 1;
			list_for_each_entry_rcu(z, &ethmf_list, list) {
				if (z->addr_hash == ethmf_groups[group].hash_addr) {
					if (c == 1) {
						len += sprintf(page + len,
							"  Device: %s (MAC: %02x:%02x:%02x:%02x:%02x:%02x)\n",
							z->ethdev,
							z->addr[0], z->addr[1], z->addr[2],
							z->addr[3], z->addr[4], z->addr[5]);
					}
					len += sprintf(page + len, "    Span %d: subaddr=%u ready=%d delay=%d real_channels=%d no_front_padding=%d\n",
						c++, ntohs(z->subaddr),
						atomic_read(&z->ready), atomic_read(&z->delay),
						z->real_channels, atomic_read(&z->no_front_padding));
				}
			}
			len += sprintf(page + len, "  Device UPs: %u\n",
				atomic_read(&(ethmf_groups[group].devupcount)));
			len += sprintf(page + len, "  Device DOWNs: %u\n",
				atomic_read(&(ethmf_groups[group].devdowncount)));
			len += sprintf(page + len, "  Rx Frames: %u\n",
				atomic_read(&(ethmf_groups[group].rxframecount)));
			len += sprintf(page + len, "  Tx Frames: %u\n",
				atomic_read(&(ethmf_groups[group].txframecount)));
			len += sprintf(page + len, "  Rx Bytes: %u\n",
				atomic_read(&(ethmf_groups[group].rxbytecount)));
			len += sprintf(page + len, "  Tx Bytes: %u\n",
				atomic_read(&(ethmf_groups[group].txbytecount)));
			if (len <= off) {
				off -= len;
				len = 0;
			}
			if (len > off+count)
				break;
		}
	}
	rcu_read_unlock();

	if (len <= off) {
		off -= len;
		len = 0;
	}
	*start = page + off;
	len -= off;
	if (len > count)
		len = count;
	return len;
}
#endif

static int __init ztdethmf_init(void)
{
	init_timer(&timer);
	timer.expires = jiffies + HZ;
	timer.function = &timer_callback;
	if (!timer_pending(&timer))
		add_timer(&timer);

	dev_add_pack(&ztdethmf_ptype);
	register_netdevice_notifier(&ztdethmf_nblock);
	dahdi_dynamic_register(&ztd_ethmf);

	skb_queue_head_init(&skbs);

#ifdef USE_PROC_FS
	proc_entry = create_proc_read_entry(ztdethmf_procname, 0444, NULL,
		ztdethmf_proc_read, NULL);
	if (!proc_entry) {
		printk(KERN_ALERT "create_proc_read_entry failed.\n");
	}
#endif

	return 0;
}

static void __exit ztdethmf_exit(void)
{
	atomic_set(&timer_deleted, 1);
	del_timer_sync(&timer);

	dev_remove_pack(&ztdethmf_ptype);
	unregister_netdevice_notifier(&ztdethmf_nblock);
	dahdi_dynamic_unregister(&ztd_ethmf);

#ifdef USE_PROC_FS
	if (proc_entry)
		remove_proc_entry(ztdethmf_procname, NULL);
#endif
}

MODULE_DESCRIPTION("DAHDI Dynamic TDMoEmf Support");
MODULE_AUTHOR("Joseph Benden <joe@thrallingpenguin.com>");
#ifdef MODULE_LICENSE
MODULE_LICENSE("GPL");
#endif

module_init(ztdethmf_init);
module_exit(ztdethmf_exit);