summaryrefslogtreecommitdiffstats
path: root/package/d80211/src/ieee80211_sysfs.c
blob: b9d0973d1bb0fc4dd9025b1ab722c482c41244e0 (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
/*
 * Copyright (c) 2006 Jiri Benc <jbenc@suse.cz>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 */

#include <linux/kernel.h>
#include <linux/device.h>
#include <linux/if.h>
#include <linux/interrupt.h>
#include <linux/netdevice.h>
#include <linux/rtnetlink.h>
#include <net/d80211.h>
#include "ieee80211_i.h"
#include "ieee80211_rate.h"

#define to_ieee80211_local(class) \
	container_of(class, struct ieee80211_local, class_dev)
#define to_net_dev(class) \
	container_of(class, struct net_device, class_dev)

static inline int rtnl_lock_local(struct ieee80211_local *local)
{
	rtnl_lock();
	if (unlikely(local->reg_state != IEEE80211_DEV_REGISTERED)) {
		rtnl_unlock();
		return -ENODEV;
	}
	return 0;
}

static const char *ieee80211_mode_str_short(int mode)
{
	switch (mode) {
	case MODE_IEEE80211A:
		return "802.11a";
	case MODE_IEEE80211B:
		return "802.11b";
	case MODE_IEEE80211G:
		return "802.11g";
	case MODE_ATHEROS_TURBO:
		return "AtherosTurbo";
	default:
		return "UNKNOWN";
	}
}

static const char *ieee80211_mode_str(int mode)
{
	switch (mode) {
	case MODE_IEEE80211A:
		return "IEEE 802.11a";
	case MODE_IEEE80211B:
		return "IEEE 802.11b";
	case MODE_IEEE80211G:
		return "IEEE 802.11g";
	case MODE_ATHEROS_TURBO:
		return "Atheros Turbo (5 GHz)";
	default:
		return "UNKNOWN";
	}
}

/* attributes in /sys/class/ieee80211/phyX/ */

static ssize_t store_add_iface(struct class_device *dev,
			       const char *buf, size_t len)
{
	struct ieee80211_local *local = to_ieee80211_local(dev);
	struct net_device *new_dev;
	int res;

	if (!capable(CAP_NET_ADMIN))
		return -EPERM;
	if (len > IFNAMSIZ)
		return -EINVAL;
	res = rtnl_lock_local(local);
	if (res)
		return res;
	res = ieee80211_if_add(local->mdev, buf, 0, &new_dev);
	if (res == 0)
		ieee80211_if_set_type(new_dev, IEEE80211_IF_TYPE_STA);
	rtnl_unlock();
	return res < 0 ? res : len;
}

static ssize_t store_remove_iface(struct class_device *dev,
				  const char *buf, size_t len)
{
	struct ieee80211_local *local = to_ieee80211_local(dev);
	int res;

	if (!capable(CAP_NET_ADMIN))
		return -EPERM;
	if (len > IFNAMSIZ)
		return -EINVAL;
	res = rtnl_lock_local(local);
	if (res)
		return res;
	res = ieee80211_if_remove(local->mdev, buf, -1);
	rtnl_unlock();
	return res < 0 ? res : len;
}

static ssize_t store_rate_ctrl_alg(struct class_device *dev,
				   const char *buf, size_t len)
{
	struct ieee80211_local *local = to_ieee80211_local(dev);
	int res;

	if (!capable(CAP_NET_ADMIN))
		return -EPERM;
	res = rtnl_lock_local(local);
	if (res)
		return res;
	res = ieee80211_init_rate_ctrl_alg(local, buf);
	rtnl_unlock();
	return res < 0 ? res : len;
}

static ssize_t ieee80211_local_show(struct class_device *dev, char *buf,
			ssize_t (*format)(struct ieee80211_local *, char *))
{
	struct ieee80211_local *local = to_ieee80211_local(dev);
	ssize_t ret = -EINVAL;

	if (local->reg_state == IEEE80211_DEV_REGISTERED)
		ret = (*format)(local, buf);
	return ret;
}

#define IEEE80211_LOCAL_FMT(name, field, format_string)			\
static ssize_t ieee80211_local_fmt_##name(struct ieee80211_local *local,\
					  char *buf)			\
{									\
	return sprintf(buf, format_string, local->field);		\
}

#define __IEEE80211_LOCAL_SHOW(name)					\
static ssize_t ieee80211_local_show_##name(struct class_device *cd,	\
					   char *buf)			\
{									\
	return ieee80211_local_show(cd, buf,				\
				    ieee80211_local_fmt_##name);	\
}

#define IEEE80211_LOCAL_SHOW(name, field, format)			\
		IEEE80211_LOCAL_FMT(name, field, format "\n")		\
		__IEEE80211_LOCAL_SHOW(name)

IEEE80211_LOCAL_SHOW(channel, hw.conf.channel, "%d");
IEEE80211_LOCAL_SHOW(frequency, hw.conf.freq, "%d");
IEEE80211_LOCAL_SHOW(radar_detect, hw.conf.radar_detect, "%d");
IEEE80211_LOCAL_SHOW(antenna_sel, hw.conf.antenna_sel, "%d");
IEEE80211_LOCAL_SHOW(bridge_packets, bridge_packets, "%d");
IEEE80211_LOCAL_SHOW(key_tx_rx_threshold, key_tx_rx_threshold, "%d");
IEEE80211_LOCAL_SHOW(rts_threshold, rts_threshold, "%d");
IEEE80211_LOCAL_SHOW(fragmentation_threshold, fragmentation_threshold, "%d");
IEEE80211_LOCAL_SHOW(short_retry_limit, short_retry_limit, "%d");
IEEE80211_LOCAL_SHOW(long_retry_limit, long_retry_limit, "%d");
IEEE80211_LOCAL_SHOW(total_ps_buffered, total_ps_buffered, "%d");

static ssize_t ieee80211_local_fmt_mode(struct ieee80211_local *local,
					char *buf)
{
	return sprintf(buf, "%s\n", ieee80211_mode_str(local->hw.conf.phymode));
}
__IEEE80211_LOCAL_SHOW(mode);

static ssize_t ieee80211_local_fmt_wep_iv(struct ieee80211_local *local,
					  char *buf)
{
	return sprintf(buf, "%#06x\n", local->wep_iv & 0xffffff);
}
__IEEE80211_LOCAL_SHOW(wep_iv);

static ssize_t ieee80211_local_fmt_tx_power_reduction(struct ieee80211_local
						      *local, char *buf)
{
	short tx_power_reduction = local->hw.conf.tx_power_reduction;

	return sprintf(buf, "%d.%d dBm\n", tx_power_reduction / 10,
		       tx_power_reduction % 10);
}
__IEEE80211_LOCAL_SHOW(tx_power_reduction);

static ssize_t ieee80211_local_fmt_modes(struct ieee80211_local *local,
					 char *buf)
{
	struct ieee80211_hw_mode *mode;
	char *p = buf;

	/* FIXME: Locking? Could register a mode in the meantime. */
	list_for_each_entry(mode, &local->modes_list, list)
		p += sprintf(p, "%s\n", ieee80211_mode_str_short(mode->mode));

	return (p - buf);
}
__IEEE80211_LOCAL_SHOW(modes);

static ssize_t ieee80211_local_fmt_rate_ctrl_alg(struct ieee80211_local *local,
						 char *buf)
{
	struct rate_control_ref *ref = local->rate_ctrl;
	if (ref)
		return sprintf(buf, "%s\n", ref->ops->name);
	return 0;
}
__IEEE80211_LOCAL_SHOW(rate_ctrl_alg);

static struct class_device_attribute ieee80211_class_dev_attrs[] = {
	__ATTR(add_iface, S_IWUGO, NULL, store_add_iface),
	__ATTR(remove_iface, S_IWUGO, NULL, store_remove_iface),
	__ATTR(channel, S_IRUGO, ieee80211_local_show_channel, NULL),
	__ATTR(frequency, S_IRUGO, ieee80211_local_show_frequency, NULL),
	__ATTR(radar_detect, S_IRUGO, ieee80211_local_show_radar_detect, NULL),
	__ATTR(antenna_sel, S_IRUGO, ieee80211_local_show_antenna_sel, NULL),
	__ATTR(bridge_packets, S_IRUGO, ieee80211_local_show_bridge_packets, NULL),
	__ATTR(key_tx_rx_threshold, S_IRUGO, ieee80211_local_show_key_tx_rx_threshold, NULL),
	__ATTR(rts_threshold, S_IRUGO, ieee80211_local_show_rts_threshold, NULL),
	__ATTR(fragmentation_threshold, S_IRUGO, ieee80211_local_show_fragmentation_threshold, NULL),
	__ATTR(short_retry_limit, S_IRUGO, ieee80211_local_show_short_retry_limit, NULL),
	__ATTR(long_retry_limit, S_IRUGO, ieee80211_local_show_long_retry_limit, NULL),
	__ATTR(total_ps_buffered, S_IRUGO, ieee80211_local_show_total_ps_buffered, NULL),
	__ATTR(mode, S_IRUGO, ieee80211_local_show_mode, NULL),
	__ATTR(wep_iv, S_IRUGO, ieee80211_local_show_wep_iv, NULL),
	__ATTR(tx_power_reduction, S_IRUGO, ieee80211_local_show_tx_power_reduction, NULL),
	__ATTR(modes, S_IRUGO, ieee80211_local_show_modes, NULL),
	__ATTR(rate_ctrl_alg, S_IRUGO | S_IWUGO, ieee80211_local_show_rate_ctrl_alg, store_rate_ctrl_alg),
	{}
};

/* attributes in /sys/class/ieee80211/phyX/statistics/ */

#define IEEE80211_LOCAL_ATTR(name, field, format)			\
IEEE80211_LOCAL_SHOW(name, field, format)				\
static CLASS_DEVICE_ATTR(name, S_IRUGO, ieee80211_local_show_##name, NULL);

IEEE80211_LOCAL_ATTR(transmitted_fragment_count, dot11TransmittedFragmentCount, "%u");
IEEE80211_LOCAL_ATTR(multicast_transmitted_frame_count, dot11MulticastTransmittedFrameCount, "%u");
IEEE80211_LOCAL_ATTR(failed_count, dot11FailedCount, "%u");
IEEE80211_LOCAL_ATTR(retry_count, dot11RetryCount, "%u");
IEEE80211_LOCAL_ATTR(multiple_retry_count, dot11MultipleRetryCount, "%u");
IEEE80211_LOCAL_ATTR(frame_duplicate_count, dot11FrameDuplicateCount, "%u");
IEEE80211_LOCAL_ATTR(received_fragment_count, dot11ReceivedFragmentCount, "%u");
IEEE80211_LOCAL_ATTR(multicast_received_frame_count, dot11MulticastReceivedFrameCount, "%u");
IEEE80211_LOCAL_ATTR(transmitted_frame_count, dot11TransmittedFrameCount, "%u");
IEEE80211_LOCAL_ATTR(wep_undecryptable_count, dot11WEPUndecryptableCount, "%u");
IEEE80211_LOCAL_ATTR(num_scans, scan.num_scans, "%u");

#ifdef CONFIG_D80211_DEBUG_COUNTERS
IEEE80211_LOCAL_ATTR(tx_handlers_drop, tx_handlers_drop, "%u");
IEEE80211_LOCAL_ATTR(tx_handlers_queued, tx_handlers_queued, "%u");
IEEE80211_LOCAL_ATTR(tx_handlers_drop_unencrypted, tx_handlers_drop_unencrypted, "%u");
IEEE80211_LOCAL_ATTR(tx_handlers_drop_fragment, tx_handlers_drop_fragment, "%u");
IEEE80211_LOCAL_ATTR(tx_handlers_drop_wep, tx_handlers_drop_wep, "%u");
IEEE80211_LOCAL_ATTR(tx_handlers_drop_not_assoc, tx_handlers_drop_not_assoc, "%u");
IEEE80211_LOCAL_ATTR(tx_handlers_drop_unauth_port, tx_handlers_drop_unauth_port, "%u");
IEEE80211_LOCAL_ATTR(rx_handlers_drop, rx_handlers_drop, "%u");
IEEE80211_LOCAL_ATTR(rx_handlers_queued, rx_handlers_queued, "%u");
IEEE80211_LOCAL_ATTR(rx_handlers_drop_nullfunc, rx_handlers_drop_nullfunc, "%u");
IEEE80211_LOCAL_ATTR(rx_handlers_drop_defrag, rx_handlers_drop_defrag, "%u");
IEEE80211_LOCAL_ATTR(rx_handlers_drop_short, rx_handlers_drop_short, "%u");
IEEE80211_LOCAL_ATTR(rx_handlers_drop_passive_scan, rx_handlers_drop_passive_scan, "%u");
IEEE80211_LOCAL_ATTR(tx_expand_skb_head, tx_expand_skb_head, "%u");
IEEE80211_LOCAL_ATTR(tx_expand_skb_head_cloned, tx_expand_skb_head_cloned, "%u");
IEEE80211_LOCAL_ATTR(rx_expand_skb_head, rx_expand_skb_head, "%u");
IEEE80211_LOCAL_ATTR(rx_expand_skb_head2, rx_expand_skb_head2, "%u");
IEEE80211_LOCAL_ATTR(rx_handlers_fragments, rx_handlers_fragments, "%u");
IEEE80211_LOCAL_ATTR(tx_status_drop, tx_status_drop, "%u");

static ssize_t ieee80211_local_fmt_wme_rx_queue(struct ieee80211_local *local,
						char *buf)
{
	int i;
	char *p = buf;

	for (i = 0; i < NUM_RX_DATA_QUEUES; i++)
		p += sprintf(p, "%u\n", local->wme_rx_queue[i]);
	return (p - buf);
}
__IEEE80211_LOCAL_SHOW(wme_rx_queue);
static CLASS_DEVICE_ATTR(wme_rx_queue, S_IRUGO,
			 ieee80211_local_show_wme_rx_queue, NULL);

static ssize_t ieee80211_local_fmt_wme_tx_queue(struct ieee80211_local *local,
						char *buf)
{
	int i;
	char *p = buf;

	for (i = 0; i < NUM_RX_DATA_QUEUES; i++)
		p += sprintf(p, "%u\n", local->wme_tx_queue[i]);
	return (p - buf);
}
__IEEE80211_LOCAL_SHOW(wme_tx_queue);
static CLASS_DEVICE_ATTR(wme_tx_queue, S_IRUGO,
			 ieee80211_local_show_wme_tx_queue, NULL);
#endif

static ssize_t ieee80211_stats_show(struct class_device *dev, char *buf,
		ssize_t (*format)(struct ieee80211_low_level_stats *, char *))
{
	struct ieee80211_local *local = to_ieee80211_local(dev);
	struct ieee80211_low_level_stats stats;
	ssize_t ret = -EINVAL;

	if (!local->ops->get_stats)
		return -EOPNOTSUPP;
	ret = rtnl_lock_local(local);
	if (ret)
		return ret;
	ret = local->ops->get_stats(local_to_hw(local), &stats);
	rtnl_unlock();
	if (!ret)
		ret = (*format)(&stats, buf);
	return ret;
}

#define IEEE80211_STATS_FMT(name, field, format_string)			\
static ssize_t ieee80211_stats_fmt_##name(struct ieee80211_low_level_stats \
					  *stats, char *buf)		\
{									\
	return sprintf(buf, format_string, stats->field);		\
}

#define __IEEE80211_STATS_SHOW(name)					\
static ssize_t ieee80211_stats_show_##name(struct class_device *cd,	\
					   char *buf)			\
{									\
	return ieee80211_stats_show(cd, buf,				\
				    ieee80211_stats_fmt_##name);	\
}

#define IEEE80211_STATS_ATTR(name, field, format)			\
IEEE80211_STATS_FMT(name, field, format "\n")				\
__IEEE80211_STATS_SHOW(name)						\
static CLASS_DEVICE_ATTR(name, S_IRUGO, ieee80211_stats_show_##name, NULL);

IEEE80211_STATS_ATTR(ack_failure_count, dot11ACKFailureCount, "%u");
IEEE80211_STATS_ATTR(rts_failure_count, dot11RTSFailureCount, "%u");
IEEE80211_STATS_ATTR(fcs_error_count, dot11FCSErrorCount, "%u");
IEEE80211_STATS_ATTR(rts_success_count, dot11RTSSuccessCount, "%u");

static struct attribute *ieee80211_stats_attrs[] = {
	&class_device_attr_transmitted_fragment_count.attr,
	&class_device_attr_multicast_transmitted_frame_count.attr,
	&class_device_attr_failed_count.attr,
	&class_device_attr_retry_count.attr,
	&class_device_attr_multiple_retry_count.attr,
	&class_device_attr_frame_duplicate_count.attr,
	&class_device_attr_received_fragment_count.attr,
	&class_device_attr_multicast_received_frame_count.attr,
	&class_device_attr_transmitted_frame_count.attr,
	&class_device_attr_wep_undecryptable_count.attr,
	&class_device_attr_ack_failure_count.attr,
	&class_device_attr_rts_failure_count.attr,
	&class_device_attr_fcs_error_count.attr,
	&class_device_attr_rts_success_count.attr,
	&class_device_attr_num_scans.attr,
#ifdef CONFIG_D80211_DEBUG_COUNTERS
	&class_device_attr_tx_handlers_drop.attr,
	&class_device_attr_tx_handlers_queued.attr,
	&class_device_attr_tx_handlers_drop_unencrypted.attr,
	&class_device_attr_tx_handlers_drop_fragment.attr,
	&class_device_attr_tx_handlers_drop_wep.attr,
	&class_device_attr_tx_handlers_drop_not_assoc.attr,
	&class_device_attr_tx_handlers_drop_unauth_port.attr,
	&class_device_attr_rx_handlers_drop.attr,
	&class_device_attr_rx_handlers_queued.attr,
	&class_device_attr_rx_handlers_drop_nullfunc.attr,
	&class_device_attr_rx_handlers_drop_defrag.attr,
	&class_device_attr_rx_handlers_drop_short.attr,
	&class_device_attr_rx_handlers_drop_passive_scan.attr,
	&class_device_attr_tx_expand_skb_head.attr,
	&class_device_attr_tx_expand_skb_head_cloned.attr,
	&class_device_attr_rx_expand_skb_head.attr,
	&class_device_attr_rx_expand_skb_head2.attr,
	&class_device_attr_rx_handlers_fragments.attr,
	&class_device_attr_tx_status_drop.attr,
	&class_device_attr_wme_rx_queue.attr,
	&class_device_attr_wme_tx_queue.attr,
#endif
	NULL,
};

static struct attribute_group ieee80211_stats_group = {
	.name = "statistics",
	.attrs = ieee80211_stats_attrs,
};

/* attributes in /sys/class/net/X/ */

static ssize_t ieee80211_if_show(struct class_device *cd, char *buf,
			ssize_t (*format)(const struct ieee80211_sub_if_data *,
					  char *))
{
	struct net_device *dev = to_net_dev(cd);
	struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
	ssize_t ret = -EINVAL;

	read_lock(&dev_base_lock);
	if (dev->reg_state == NETREG_REGISTERED) {
		ret = (*format)(sdata, buf);
	}
	read_unlock(&dev_base_lock);
	return ret;
}

#define IEEE80211_IF_FMT(name, field, format_string)			\
static ssize_t ieee80211_if_fmt_##name(const struct			\
			ieee80211_sub_if_data *sdata, char *buf)	\
{									\
	return sprintf(buf, format_string, sdata->field);		\
}
#define IEEE80211_IF_FMT_DEC(name, field)				\
		IEEE80211_IF_FMT(name, field, "%d\n")
#define IEEE80211_IF_FMT_HEX(name, field)				\
		IEEE80211_IF_FMT(name, field, "%#x\n")
#define IEEE80211_IF_FMT_SIZE(name, field)				\
		IEEE80211_IF_FMT(name, field, "%zd\n")

#define IEEE80211_IF_FMT_ATOMIC(name, field)				\
static ssize_t ieee80211_if_fmt_##name(const struct			\
			ieee80211_sub_if_data *sdata, char *buf)	\
{									\
	return sprintf(buf, "%d\n", atomic_read(&sdata->field));	\
}

#define IEEE80211_IF_FMT_MAC(name, field)				\
static ssize_t ieee80211_if_fmt_##name(const struct			\
			ieee80211_sub_if_data *sdata, char *buf)	\
{									\
	return sprintf(buf, MAC_FMT "\n", MAC_ARG(sdata->field));	\
}

#define __IEEE80211_IF_SHOW(name)					\
static ssize_t ieee80211_if_show_##name(struct class_device *cd,	\
					char *buf)			\
{									\
	return ieee80211_if_show(cd, buf, ieee80211_if_fmt_##name);	\
}									\
static CLASS_DEVICE_ATTR(name, S_IRUGO, ieee80211_if_show_##name, NULL);

#define IEEE80211_IF_SHOW(name, field, format)				\
		IEEE80211_IF_FMT_##format(name, field)			\
		__IEEE80211_IF_SHOW(name)

/* common attributes */
IEEE80211_IF_SHOW(channel_use, channel_use, DEC);
IEEE80211_IF_SHOW(drop_unencrypted, drop_unencrypted, DEC);
IEEE80211_IF_SHOW(eapol, eapol, DEC);
IEEE80211_IF_SHOW(ieee8021_x, ieee802_1x, DEC);

/* STA/IBSS attributes */
IEEE80211_IF_SHOW(state, u.sta.state, DEC);
IEEE80211_IF_SHOW(bssid, u.sta.bssid, MAC);
IEEE80211_IF_SHOW(prev_bssid, u.sta.prev_bssid, MAC);
IEEE80211_IF_SHOW(ssid_len, u.sta.ssid_len, SIZE);
IEEE80211_IF_SHOW(aid, u.sta.aid, DEC);
IEEE80211_IF_SHOW(ap_capab, u.sta.ap_capab, HEX);
IEEE80211_IF_SHOW(capab, u.sta.capab, HEX);
IEEE80211_IF_SHOW(extra_ie_len, u.sta.extra_ie_len, SIZE);
IEEE80211_IF_SHOW(auth_tries, u.sta.auth_tries, DEC);
IEEE80211_IF_SHOW(assoc_tries, u.sta.assoc_tries, DEC);
IEEE80211_IF_SHOW(auth_algs, u.sta.auth_algs, HEX);
IEEE80211_IF_SHOW(auth_alg, u.sta.auth_alg, DEC);
IEEE80211_IF_SHOW(auth_transaction, u.sta.auth_transaction, DEC);

static ssize_t ieee80211_if_fmt_flags(const struct
			ieee80211_sub_if_data *sdata, char *buf)
{
	return sprintf(buf, "%s%s%s%s%s%s%s\n",
		       sdata->u.sta.ssid_set ? "SSID\n" : "",
		       sdata->u.sta.bssid_set ? "BSSID\n" : "",
		       sdata->u.sta.prev_bssid_set ? "prev BSSID\n" : "",
		       sdata->u.sta.authenticated ? "AUTH\n" : "",
		       sdata->u.sta.associated ? "ASSOC\n" : "",
		       sdata->u.sta.probereq_poll ? "PROBEREQ POLL\n" : "",
		       sdata->u.sta.use_protection ? "CTS prot\n" : "");
}
__IEEE80211_IF_SHOW(flags);

/* AP attributes */
IEEE80211_IF_SHOW(num_sta_ps, u.ap.num_sta_ps, ATOMIC);
IEEE80211_IF_SHOW(dtim_period, u.ap.dtim_period, DEC);
IEEE80211_IF_SHOW(dtim_count, u.ap.dtim_count, DEC);
IEEE80211_IF_SHOW(num_beacons, u.ap.num_beacons, DEC);
IEEE80211_IF_SHOW(force_unicast_rateidx, u.ap.force_unicast_rateidx, DEC);
IEEE80211_IF_SHOW(max_ratectrl_rateidx, u.ap.max_ratectrl_rateidx, DEC);

static ssize_t ieee80211_if_fmt_num_buffered_multicast(const struct
			ieee80211_sub_if_data *sdata, char *buf)
{
	return sprintf(buf, "%u\n", skb_queue_len(&sdata->u.ap.ps_bc_buf));
}
__IEEE80211_IF_SHOW(num_buffered_multicast);

static ssize_t ieee80211_if_fmt_beacon_head_len(const struct
			ieee80211_sub_if_data *sdata, char *buf)
{
	if (sdata->u.ap.beacon_head)
		return sprintf(buf, "%d\n", sdata->u.ap.beacon_head_len);
	return sprintf(buf, "\n");
}
__IEEE80211_IF_SHOW(beacon_head_len);

static ssize_t ieee80211_if_fmt_beacon_tail_len(const struct
			ieee80211_sub_if_data *sdata, char *buf)
{
	if (sdata->u.ap.beacon_tail)
		return sprintf(buf, "%d\n", sdata->u.ap.beacon_tail_len);
	return sprintf(buf, "\n");
}
__IEEE80211_IF_SHOW(beacon_tail_len);

/* WDS attributes */
IEEE80211_IF_SHOW(peer, u.wds.remote_addr, MAC);

/* VLAN attributes */
IEEE80211_IF_SHOW(vlan_id, u.vlan.id, DEC);

/* MONITOR attributes */
static ssize_t ieee80211_if_fmt_mode(const struct
			ieee80211_sub_if_data *sdata, char *buf)
{
	struct ieee80211_local *local = sdata->local;

	return sprintf(buf, "%s\n",
		       ((local->hw.flags & IEEE80211_HW_MONITOR_DURING_OPER) ||
			local->open_count == local->monitors) ?
		       "hard" : "soft");
}
__IEEE80211_IF_SHOW(mode);

static struct attribute *ieee80211_sta_attrs[] = {
	&class_device_attr_channel_use.attr,
	&class_device_attr_drop_unencrypted.attr,
	&class_device_attr_eapol.attr,
	&class_device_attr_ieee8021_x.attr,
	&class_device_attr_state.attr,
	&class_device_attr_bssid.attr,
	&class_device_attr_prev_bssid.attr,
	&class_device_attr_ssid_len.attr,
	&class_device_attr_aid.attr,
	&class_device_attr_ap_capab.attr,
	&class_device_attr_capab.attr,
	&class_device_attr_extra_ie_len.attr,
	&class_device_attr_auth_tries.attr,
	&class_device_attr_assoc_tries.attr,
	&class_device_attr_auth_algs.attr,
	&class_device_attr_auth_alg.attr,
	&class_device_attr_auth_transaction.attr,
	&class_device_attr_flags.attr,
	NULL
};

static struct attribute *ieee80211_ap_attrs[] = {
	&class_device_attr_channel_use.attr,
	&class_device_attr_drop_unencrypted.attr,
	&class_device_attr_eapol.attr,
	&class_device_attr_ieee8021_x.attr,
	&class_device_attr_num_sta_ps.attr,
	&class_device_attr_dtim_period.attr,
	&class_device_attr_dtim_count.attr,
	&class_device_attr_num_beacons.attr,
	&class_device_attr_force_unicast_rateidx.attr,
	&class_device_attr_max_ratectrl_rateidx.attr,
	&class_device_attr_num_buffered_multicast.attr,
	&class_device_attr_beacon_head_len.attr,
	&class_device_attr_beacon_tail_len.attr,
	NULL
};

static struct attribute *ieee80211_wds_attrs[] = {
	&class_device_attr_channel_use.attr,
	&class_device_attr_drop_unencrypted.attr,
	&class_device_attr_eapol.attr,
	&class_device_attr_ieee8021_x.attr,
	&class_device_attr_peer.attr,
	NULL
};

static struct attribute *ieee80211_vlan_attrs[] = {
	&class_device_attr_channel_use.attr,
	&class_device_attr_drop_unencrypted.attr,
	&class_device_attr_eapol.attr,
	&class_device_attr_ieee8021_x.attr,
	&class_device_attr_vlan_id.attr,
	NULL
};

static struct attribute *ieee80211_monitor_attrs[] = {
	&class_device_attr_mode.attr,
	NULL
};

static struct attribute_group ieee80211_sta_group = {
	.name = "sta",
	.attrs = ieee80211_sta_attrs,
};

static struct attribute_group ieee80211_ap_group = {
	.name = "ap",
	.attrs = ieee80211_ap_attrs,
};

static struct attribute_group ieee80211_wds_group = {
	.name = "wds",
	.attrs = ieee80211_wds_attrs,
};

static struct attribute_group ieee80211_vlan_group = {
	.name = "vlan",
	.attrs = ieee80211_vlan_attrs,
};

static struct attribute_group ieee80211_monitor_group = {
	.name = "monitor",
	.attrs = ieee80211_monitor_attrs,
};

/* /sys/class/ieee80211/phyX functions */

static void ieee80211_class_dev_release(struct class_device *dev)
{
	ieee80211_release_hw(to_ieee80211_local(dev));
}

#ifdef CONFIG_HOTPLUG
static int ieee80211_uevent(struct class_device *cd, char **envp,
			    int num_envp, char *buf, int size)
{
	struct ieee80211_local *local = to_ieee80211_local(cd);

	if (num_envp < 2)
		return -ENOMEM;
	envp[0] = buf;
	if (snprintf(buf, size, "IEEE80211_DEV=phy%d",
		     local->hw.index) + 1 >= size)
		return -ENOMEM;
	envp[1] = NULL;
	return 0;
}
#endif

static struct class ieee80211_class = {
	.name = "ieee80211",
	.class_dev_attrs = ieee80211_class_dev_attrs,
	.release = ieee80211_class_dev_release,
#ifdef CONFIG_HOTPLUG
	.uevent = ieee80211_uevent,
#endif
};

void ieee80211_dev_sysfs_init(struct ieee80211_local *local)
{
	local->class_dev.class = &ieee80211_class;
	local->class_dev.class_data = local;
	class_device_initialize(&local->class_dev);
}

void ieee80211_dev_sysfs_put(struct ieee80211_local *local)
{
	class_device_put(&local->class_dev);
}

int ieee80211_dev_sysfs_add(struct ieee80211_local *local)
{
	int res;

	snprintf(local->class_dev.class_id, BUS_ID_SIZE,
		 "phy%d", local->hw.index);
	res = class_device_add(&local->class_dev);
	if (res)
		return res;
	res = sysfs_create_group(&local->class_dev.kobj,
				 &ieee80211_stats_group);
	if (res)
		class_device_del(&local->class_dev);
	return res;
}

void ieee80211_dev_sysfs_del(struct ieee80211_local *local)
{
	sysfs_remove_group(&local->class_dev.kobj, &ieee80211_stats_group);
	class_device_del(&local->class_dev);
}

/* /sys/class/net/X functions */

static void __ieee80211_remove_if_group(struct kobject *kobj,
					struct ieee80211_sub_if_data *sdata)
{
	if (sdata->sysfs_group) {
		sysfs_remove_group(kobj, sdata->sysfs_group);
		sdata->sysfs_group = NULL;
	}
}

static inline void ieee80211_remove_if_group(struct kobject *kobj,
					     struct net_device *dev)
{
	__ieee80211_remove_if_group(kobj, IEEE80211_DEV_TO_SUB_IF(dev));
}

static int ieee80211_add_if_group(struct kobject *kobj,
				  struct net_device *dev)
{
	struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
	int res = 0;

	__ieee80211_remove_if_group(kobj, sdata);
	switch (sdata->type) {
	case IEEE80211_IF_TYPE_STA:
		sdata->sysfs_group = &ieee80211_sta_group;
		break;
	case IEEE80211_IF_TYPE_AP:
		sdata->sysfs_group = &ieee80211_ap_group;
		break;
	case IEEE80211_IF_TYPE_WDS:
		sdata->sysfs_group = &ieee80211_wds_group;
		break;
	case IEEE80211_IF_TYPE_VLAN:
		sdata->sysfs_group = &ieee80211_vlan_group;
		break;
	case IEEE80211_IF_TYPE_MNTR:
		sdata->sysfs_group = &ieee80211_monitor_group;
		break;
	default:
		goto out;
	}
	res = sysfs_create_group(kobj, sdata->sysfs_group);
	if (res)
		sdata->sysfs_group = NULL;
out:
	return res;
}

int ieee80211_sysfs_change_if_type(struct net_device *dev)
{
	return ieee80211_add_if_group(&dev->class_dev.kobj, dev);
}

int ieee80211_sysfs_add_netdevice(struct net_device *dev)
{
	struct ieee80211_local *local = dev->ieee80211_ptr;
	int res;

	res = sysfs_create_link(&dev->class_dev.kobj, &local->class_dev.kobj,
				"hw");
	if (res)
		goto err_out;
	res = ieee80211_add_if_group(&dev->class_dev.kobj, dev);
	if (res)
		goto err_link;
	res = ieee80211_key_kset_sysfs_register(IEEE80211_DEV_TO_SUB_IF(dev));
	return res;

err_link:
	sysfs_remove_link(&dev->class_dev.kobj, "hw");
err_out:
	return res;
}

void ieee80211_sysfs_remove_netdevice(struct net_device *dev)
{
	ieee80211_key_kset_sysfs_unregister(IEEE80211_DEV_TO_SUB_IF(dev));
	ieee80211_remove_if_group(&dev->class_dev.kobj, dev);
	sysfs_remove_link(&dev->class_dev.kobj, "hw");
}

/* general module functions */

int ieee80211_sysfs_init(void)
{
	return class_register(&ieee80211_class);
}

void ieee80211_sysfs_deinit(void)
{
	class_unregister(&ieee80211_class);
}