summaryrefslogtreecommitdiffstats
path: root/drivers/uwb/wlp/sysfs.c
blob: 1bb9b1f97d47402e304f54b5e53f635fa1a28f6f (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
/*
 * WiMedia Logical Link Control Protocol (WLP)
 * sysfs functions
 *
 * Copyright (C) 2007 Intel Corporation
 * Reinette Chatre <reinette.chatre@intel.com>
 *
 * 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.
 *
 * 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., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301, USA.
 *
 *
 * FIXME: Docs
 *
 */

#include <linux/wlp.h>
#include "wlp-internal.h"

static
size_t wlp_wss_wssid_e_print(char *buf, size_t bufsize,
			     struct wlp_wssid_e *wssid_e)
{
	size_t used = 0;
	used += scnprintf(buf, bufsize, " WSS: ");
	used += wlp_wss_uuid_print(buf + used, bufsize - used,
				   &wssid_e->wssid);

	if (wssid_e->info != NULL) {
		used += scnprintf(buf + used, bufsize - used, " ");
		used += uwb_mac_addr_print(buf + used, bufsize - used,
					   &wssid_e->info->bcast);
		used += scnprintf(buf + used, bufsize - used, " %u %u %s\n",
				  wssid_e->info->accept_enroll,
				  wssid_e->info->sec_status,
				  wssid_e->info->name);
	}
	return used;
}

/**
 * Print out information learned from neighbor discovery
 *
 * Some fields being printed may not be included in the device discovery
 * information (it is not mandatory). We are thus careful how the
 * information is printed to ensure it is clear to the user what field is
 * being referenced.
 * The information being printed is for one time use - temporary storage is
 * cleaned after it is printed.
 *
 * Ideally sysfs output should be on one line. The information printed here
 * contain a few strings so it will be hard to parse if they are all
 * printed on the same line - without agreeing on a standard field
 * separator.
 */
static
ssize_t wlp_wss_neighborhood_print_remove(struct wlp *wlp, char *buf,
				   size_t bufsize)
{
	size_t used = 0;
	struct wlp_neighbor_e *neighb;
	struct wlp_wssid_e *wssid_e;

	mutex_lock(&wlp->nbmutex);
	used = scnprintf(buf, bufsize, "#Neighbor information\n"
			 "#uuid dev_addr\n"
			 "# Device Name:\n# Model Name:\n# Manufacturer:\n"
			 "# Model Nr:\n# Serial:\n"
			 "# Pri Dev type: CategoryID OUI OUISubdiv "
			 "SubcategoryID\n"
			 "# WSS: WSSID WSS_name accept_enroll sec_status "
			 "bcast\n"
			 "# WSS: WSSID WSS_name accept_enroll sec_status "
			 "bcast\n\n");
	list_for_each_entry(neighb, &wlp->neighbors, node) {
		if (bufsize - used <= 0)
			goto out;
		used += wlp_wss_uuid_print(buf + used, bufsize - used,
					   &neighb->uuid);
		buf[used++] = ' ';
		used += uwb_dev_addr_print(buf + used, bufsize - used,
					   &neighb->uwb_dev->dev_addr);
		if (neighb->info != NULL)
			used += scnprintf(buf + used, bufsize - used,
					  "\n Device Name: %s\n"
					  " Model Name: %s\n"
					  " Manufacturer:%s \n"
					  " Model Nr: %s\n"
					  " Serial: %s\n"
					  " Pri Dev type: "
					  "%u %02x:%02x:%02x %u %u\n",
					  neighb->info->name,
					  neighb->info->model_name,
					  neighb->info->manufacturer,
					  neighb->info->model_nr,
					  neighb->info->serial,
					  neighb->info->prim_dev_type.category,
					  neighb->info->prim_dev_type.OUI[0],
					  neighb->info->prim_dev_type.OUI[1],
					  neighb->info->prim_dev_type.OUI[2],
					  neighb->info->prim_dev_type.OUIsubdiv,
					  neighb->info->prim_dev_type.subID);
		list_for_each_entry(wssid_e, &neighb->wssid, node) {
			used += wlp_wss_wssid_e_print(buf + used,
						      bufsize - used,
						      wssid_e);
		}
		buf[used++] = '\n';
		wlp_remove_neighbor_tmp_info(neighb);
	}


out:
	mutex_unlock(&wlp->nbmutex);
	return used;
}


/**
 * Show properties of all WSS in neighborhood.
 *
 * Will trigger a complete discovery of WSS activated by this device and
 * its neighbors.
 */
ssize_t wlp_neighborhood_show(struct wlp *wlp, char *buf)
{
	wlp_discover(wlp);
	return wlp_wss_neighborhood_print_remove(wlp, buf, PAGE_SIZE);
}
EXPORT_SYMBOL_GPL(wlp_neighborhood_show);

static
ssize_t __wlp_wss_properties_show(struct wlp_wss *wss, char *buf,
				  size_t bufsize)
{
	ssize_t result;

	result = wlp_wss_uuid_print(buf, bufsize, &wss->wssid);
	result += scnprintf(buf + result, bufsize - result, " ");
	result += uwb_mac_addr_print(buf + result, bufsize - result,
				     &wss->bcast);
	result += scnprintf(buf + result, bufsize - result,
			    " 0x%02x %u ", wss->hash, wss->secure_status);
	result += wlp_wss_key_print(buf + result, bufsize - result,
				    wss->master_key);
	result += scnprintf(buf + result, bufsize - result, " 0x%02x ",
			    wss->tag);
	result += uwb_mac_addr_print(buf + result, bufsize - result,
				     &wss->virtual_addr);
	result += scnprintf(buf + result, bufsize - result, " %s", wss->name);
	result += scnprintf(buf + result, bufsize - result,
			    "\n\n#WSSID\n#WSS broadcast address\n"
			    "#WSS hash\n#WSS secure status\n"
			    "#WSS master key\n#WSS local tag\n"
			    "#WSS local virtual EUI-48\n#WSS name\n");
	return result;
}

/**
 * Show which WSS is activated.
 */
ssize_t wlp_wss_activate_show(struct wlp_wss *wss, char *buf)
{
	int result = 0;

	if (mutex_lock_interruptible(&wss->mutex))
		goto out;
	if (wss->state >= WLP_WSS_STATE_ACTIVE)
		result = __wlp_wss_properties_show(wss, buf, PAGE_SIZE);
	else
		result = scnprintf(buf, PAGE_SIZE, "No local WSS active.\n");
	result += scnprintf(buf + result, PAGE_SIZE - result,
			"\n\n"
			"# echo WSSID SECURE_STATUS ACCEPT_ENROLLMENT "
			"NAME #create new WSS\n"
			"# echo WSSID [DEV ADDR] #enroll in and activate "
			"existing WSS, can request registrar\n"
			"#\n"
			"# WSSID is a 16 byte hex array. Eg. 12 A3 3B ... \n"
			"# SECURE_STATUS 0 - unsecure, 1 - secure (default)\n"
			"# ACCEPT_ENROLLMENT 0 - no, 1 - yes (default)\n"
			"# NAME is the text string identifying the WSS\n"
			"# DEV ADDR is the device address of neighbor "
			"that should be registrar. Eg. 32:AB\n");

	mutex_unlock(&wss->mutex);
out:
	return result;

}
EXPORT_SYMBOL_GPL(wlp_wss_activate_show);

/**
 * Create/activate a new WSS or enroll/activate in neighboring WSS
 *
 * The user can provide the WSSID of a WSS in which it wants to enroll.
 * Only the WSSID is necessary if the WSS have been discovered before. If
 * the WSS has not been discovered before, or the user wants to use a
 * particular neighbor as its registrar, then the user can also provide a
 * device address or the neighbor that will be used as registrar.
 *
 * A new WSS is created when the user provides a WSSID, secure status, and
 * WSS name.
 */
ssize_t wlp_wss_activate_store(struct wlp_wss *wss,
			       const char *buf, size_t size)
{
	ssize_t result = -EINVAL;
	struct wlp_uuid wssid;
	struct uwb_dev_addr dev;
	struct uwb_dev_addr bcast = {.data = {0xff, 0xff} };
	char name[65];
	unsigned sec_status, accept;
	memset(name, 0, sizeof(name));
	result = sscanf(buf, "%02hhx %02hhx %02hhx %02hhx "
			"%02hhx %02hhx %02hhx %02hhx "
			"%02hhx %02hhx %02hhx %02hhx "
			"%02hhx %02hhx %02hhx %02hhx "
			"%02hhx:%02hhx",
			&wssid.data[0] , &wssid.data[1],
			&wssid.data[2] , &wssid.data[3],
			&wssid.data[4] , &wssid.data[5],
			&wssid.data[6] , &wssid.data[7],
			&wssid.data[8] , &wssid.data[9],
			&wssid.data[10], &wssid.data[11],
			&wssid.data[12], &wssid.data[13],
			&wssid.data[14], &wssid.data[15],
			&dev.data[1], &dev.data[0]);
	if (result == 16 || result == 17) {
		result = sscanf(buf, "%02hhx %02hhx %02hhx %02hhx "
				"%02hhx %02hhx %02hhx %02hhx "
				"%02hhx %02hhx %02hhx %02hhx "
				"%02hhx %02hhx %02hhx %02hhx "
				"%u %u %64c",
				&wssid.data[0] , &wssid.data[1],
				&wssid.data[2] , &wssid.data[3],
				&wssid.data[4] , &wssid.data[5],
				&wssid.data[6] , &wssid.data[7],
				&wssid.data[8] , &wssid.data[9],
				&wssid.data[10], &wssid.data[11],
				&wssid.data[12], &wssid.data[13],
				&wssid.data[14], &wssid.data[15],
				&sec_status, &accept, name);
		if (result == 16)
			result = wlp_wss_enroll_activate(wss, &wssid, &bcast);
		else if (result == 19) {
			sec_status = sec_status == 0 ? 0 : 1;
			accept = accept == 0 ? 0 : 1;
			/* We read name using %c, so the newline needs to be
			 * removed */
			if (strlen(name) != sizeof(name) - 1)
				name[strlen(name) - 1] = '\0';
			result = wlp_wss_create_activate(wss, &wssid, name,
							 sec_status, accept);
		} else
			result = -EINVAL;
	} else if (result == 18)
		result = wlp_wss_enroll_activate(wss, &wssid, &dev);
	else
		result = -EINVAL;
	return result < 0 ? result : size;
}
EXPORT_SYMBOL_GPL(wlp_wss_activate_store);

/**
 * Show the UUID of this host
 */
ssize_t wlp_uuid_show(struct wlp *wlp, char *buf)
{
	ssize_t result = 0;

	mutex_lock(&wlp->mutex);
	result = wlp_wss_uuid_print(buf, PAGE_SIZE, &wlp->uuid);
	buf[result++] = '\n';
	mutex_unlock(&wlp->mutex);
	return result;
}
EXPORT_SYMBOL_GPL(wlp_uuid_show);

/**
 * Store a new UUID for this host
 *
 * According to the spec this should be encoded as an octet string in the
 * order the octets are shown in string representation in RFC 4122 (WLP
 * 0.99 [Table 6])
 *
 * We do not check value provided by user.
 */
ssize_t wlp_uuid_store(struct wlp *wlp, const char *buf, size_t size)
{
	ssize_t result;
	struct wlp_uuid uuid;

	mutex_lock(&wlp->mutex);
	result = sscanf(buf, "%02hhx %02hhx %02hhx %02hhx "
			"%02hhx %02hhx %02hhx %02hhx "
			"%02hhx %02hhx %02hhx %02hhx "
			"%02hhx %02hhx %02hhx %02hhx ",
			&uuid.data[0] , &uuid.data[1],
			&uuid.data[2] , &uuid.data[3],
			&uuid.data[4] , &uuid.data[5],
			&uuid.data[6] , &uuid.data[7],
			&uuid.data[8] , &uuid.data[9],
			&uuid.data[10], &uuid.data[11],
			&uuid.data[12], &uuid.data[13],
			&uuid.data[14], &uuid.data[15]);
	if (result != 16) {
		result = -EINVAL;
		goto error;
	}
	wlp->uuid = uuid;
error:
	mutex_unlock(&wlp->mutex);
	return result < 0 ? result : size;
}
EXPORT_SYMBOL_GPL(wlp_uuid_store);

/**
 * Show contents of members of device information structure
 */
#define wlp_dev_info_show(type)						\
ssize_t wlp_dev_##type##_show(struct wlp *wlp, char *buf)		\
{									\
	ssize_t result = 0;						\
	mutex_lock(&wlp->mutex);					\
	if (wlp->dev_info == NULL) {					\
		result = __wlp_setup_device_info(wlp);			\
		if (result < 0)						\
			goto out;					\
	}								\
	result = scnprintf(buf, PAGE_SIZE, "%s\n", wlp->dev_info->type);\
out:									\
	mutex_unlock(&wlp->mutex);					\
	return result;							\
}									\
EXPORT_SYMBOL_GPL(wlp_dev_##type##_show);

wlp_dev_info_show(name)
wlp_dev_info_show(model_name)
wlp_dev_info_show(model_nr)
wlp_dev_info_show(manufacturer)
wlp_dev_info_show(serial)

/**
 * Store contents of members of device information structure
 */
#define wlp_dev_info_store(type, len)					\
ssize_t wlp_dev_##type##_store(struct wlp *wlp, const char *buf, size_t size)\
{									\
	ssize_t result;							\
	char format[10];						\
	mutex_lock(&wlp->mutex);					\
	if (wlp->dev_info == NULL) {					\
		result = __wlp_alloc_device_info(wlp);			\
		if (result < 0)						\
			goto out;					\
	}								\
	memset(wlp->dev_info->type, 0, sizeof(wlp->dev_info->type));	\
	sprintf(format, "%%%uc", len);					\
	result = sscanf(buf, format, wlp->dev_info->type);		\
out:									\
	mutex_unlock(&wlp->mutex);					\
	return result < 0 ? result : size;				\
}									\
EXPORT_SYMBOL_GPL(wlp_dev_##type##_store);

wlp_dev_info_store(name, 32)
wlp_dev_info_store(manufacturer, 64)
wlp_dev_info_store(model_name, 32)
wlp_dev_info_store(model_nr, 32)
wlp_dev_info_store(serial, 32)

static
const char *__wlp_dev_category[] = {
	[WLP_DEV_CAT_COMPUTER] = "Computer",
	[WLP_DEV_CAT_INPUT] = "Input device",
	[WLP_DEV_CAT_PRINT_SCAN_FAX_COPIER] = "Printer, scanner, FAX, or "
					      "Copier",
	[WLP_DEV_CAT_CAMERA] = "Camera",
	[WLP_DEV_CAT_STORAGE] = "Storage Network",
	[WLP_DEV_CAT_INFRASTRUCTURE] = "Infrastructure",
	[WLP_DEV_CAT_DISPLAY] = "Display",
	[WLP_DEV_CAT_MULTIM] = "Multimedia device",
	[WLP_DEV_CAT_GAMING] = "Gaming device",
	[WLP_DEV_CAT_TELEPHONE] = "Telephone",
	[WLP_DEV_CAT_OTHER] = "Other",
};

static
const char *wlp_dev_category_str(unsigned cat)
{
	if ((cat >= WLP_DEV_CAT_COMPUTER && cat <= WLP_DEV_CAT_TELEPHONE)
	    || cat == WLP_DEV_CAT_OTHER)
		return __wlp_dev_category[cat];
	return "unknown category";
}

ssize_t wlp_dev_prim_category_show(struct wlp *wlp, char *buf)
{
	ssize_t result = 0;
	mutex_lock(&wlp->mutex);
	if (wlp->dev_info == NULL) {
		result = __wlp_setup_device_info(wlp);
		if (result < 0)
			goto out;
	}
	result = scnprintf(buf, PAGE_SIZE, "%s\n",
		  wlp_dev_category_str(wlp->dev_info->prim_dev_type.category));
out:
	mutex_unlock(&wlp->mutex);
	return result;
}
EXPORT_SYMBOL_GPL(wlp_dev_prim_category_show);

ssize_t wlp_dev_prim_category_store(struct wlp *wlp, const char *buf,
				    size_t size)
{
	ssize_t result;
	u16 cat;
	mutex_lock(&wlp->mutex);
	if (wlp->dev_info == NULL) {
		result = __wlp_alloc_device_info(wlp);
		if (result < 0)
			goto out;
	}
	result = sscanf(buf, "%hu", &cat);
	if ((cat >= WLP_DEV_CAT_COMPUTER && cat <= WLP_DEV_CAT_TELEPHONE)
	    || cat == WLP_DEV_CAT_OTHER)
		wlp->dev_info->prim_dev_type.category = cat;
	else
		result = -EINVAL;
out:
	mutex_unlock(&wlp->mutex);
	return result < 0 ? result : size;
}
EXPORT_SYMBOL_GPL(wlp_dev_prim_category_store);

ssize_t wlp_dev_prim_OUI_show(struct wlp *wlp, char *buf)
{
	ssize_t result = 0;
	mutex_lock(&wlp->mutex);
	if (wlp->dev_info == NULL) {
		result = __wlp_setup_device_info(wlp);
		if (result < 0)
			goto out;
	}
	result = scnprintf(buf, PAGE_SIZE, "%02x:%02x:%02x\n",
			   wlp->dev_info->prim_dev_type.OUI[0],
			   wlp->dev_info->prim_dev_type.OUI[1],
			   wlp->dev_info->prim_dev_type.OUI[2]);
out:
	mutex_unlock(&wlp->mutex);
	return result;
}
EXPORT_SYMBOL_GPL(wlp_dev_prim_OUI_show);

ssize_t wlp_dev_prim_OUI_store(struct wlp *wlp, const char *buf, size_t size)
{
	ssize_t result;
	u8 OUI[3];
	mutex_lock(&wlp->mutex);
	if (wlp->dev_info == NULL) {
		result = __wlp_alloc_device_info(wlp);
		if (result < 0)
			goto out;
	}
	result = sscanf(buf, "%hhx:%hhx:%hhx",
			&OUI[0], &OUI[1], &OUI[2]);
	if (result != 3) {
		result = -EINVAL;
		goto out;
	} else
		memcpy(wlp->dev_info->prim_dev_type.OUI, OUI, sizeof(OUI));
out:
	mutex_unlock(&wlp->mutex);
	return result < 0 ? result : size;
}
EXPORT_SYMBOL_GPL(wlp_dev_prim_OUI_store);


ssize_t wlp_dev_prim_OUI_sub_show(struct wlp *wlp, char *buf)
{
	ssize_t result = 0;
	mutex_lock(&wlp->mutex);
	if (wlp->dev_info == NULL) {
		result = __wlp_setup_device_info(wlp);
		if (result < 0)
			goto out;
	}
	result = scnprintf(buf, PAGE_SIZE, "%u\n",
			   wlp->dev_info->prim_dev_type.OUIsubdiv);
out:
	mutex_unlock(&wlp->mutex);
	return result;
}
EXPORT_SYMBOL_GPL(wlp_dev_prim_OUI_sub_show);

ssize_t wlp_dev_prim_OUI_sub_store(struct wlp *wlp, const char *buf,
				   size_t size)
{
	ssize_t result;
	unsigned sub;
	u8 max_sub = ~0;
	mutex_lock(&wlp->mutex);
	if (wlp->dev_info == NULL) {
		result = __wlp_alloc_device_info(wlp);
		if (result < 0)
			goto out;
	}
	result = sscanf(buf, "%u", &sub);
	if (sub <= max_sub)
		wlp->dev_info->prim_dev_type.OUIsubdiv = sub;
	else
		result = -EINVAL;
out:
	mutex_unlock(&wlp->mutex);
	return result < 0 ? result : size;
}
EXPORT_SYMBOL_GPL(wlp_dev_prim_OUI_sub_store);

ssize_t wlp_dev_prim_subcat_show(struct wlp *wlp, char *buf)
{
	ssize_t result = 0;
	mutex_lock(&wlp->mutex);
	if (wlp->dev_info == NULL) {
		result = __wlp_setup_device_info(wlp);
		if (result < 0)
			goto out;
	}
	result = scnprintf(buf, PAGE_SIZE, "%u\n",
			   wlp->dev_info->prim_dev_type.subID);
out:
	mutex_unlock(&wlp->mutex);
	return result;
}
EXPORT_SYMBOL_GPL(wlp_dev_prim_subcat_show);

ssize_t wlp_dev_prim_subcat_store(struct wlp *wlp, const char *buf,
				  size_t size)
{
	ssize_t result;
	unsigned sub;
	__le16 max_sub = ~0;
	mutex_lock(&wlp->mutex);
	if (wlp->dev_info == NULL) {
		result = __wlp_alloc_device_info(wlp);
		if (result < 0)
			goto out;
	}
	result = sscanf(buf, "%u", &sub);
	if (sub <= max_sub)
		wlp->dev_info->prim_dev_type.subID = sub;
	else
		result = -EINVAL;
out:
	mutex_unlock(&wlp->mutex);
	return result < 0 ? result : size;
}
EXPORT_SYMBOL_GPL(wlp_dev_prim_subcat_store);

/**
 * Subsystem implementation for interaction with individual WSS via sysfs
 *
 * Followed instructions for subsystem in Documentation/filesystems/sysfs.txt
 */

#define kobj_to_wlp_wss(obj) container_of(obj, struct wlp_wss, kobj)
#define attr_to_wlp_wss_attr(_attr) \
	container_of(_attr, struct wlp_wss_attribute, attr)

/**
 * Sysfs subsystem: forward read calls
 *
 * Sysfs operation for forwarding read call to the show method of the
 * attribute owner
 */
static
ssize_t wlp_wss_attr_show(struct kobject *kobj, struct attribute *attr,
			  char *buf)
{
	struct wlp_wss_attribute *wss_attr = attr_to_wlp_wss_attr(attr);
	struct wlp_wss *wss = kobj_to_wlp_wss(kobj);
	ssize_t ret = -EIO;

	if (wss_attr->show)
		ret = wss_attr->show(wss, buf);
	return ret;
}
/**
 * Sysfs subsystem: forward write calls
 *
 * Sysfs operation for forwarding write call to the store method of the
 * attribute owner
 */
static
ssize_t wlp_wss_attr_store(struct kobject *kobj, struct attribute *attr,
			   const char *buf, size_t count)
{
	struct wlp_wss_attribute *wss_attr = attr_to_wlp_wss_attr(attr);
	struct wlp_wss *wss = kobj_to_wlp_wss(kobj);
	ssize_t ret = -EIO;

	if (wss_attr->store)
		ret = wss_attr->store(wss, buf, count);
	return ret;
}

static
struct sysfs_ops wss_sysfs_ops = {
	.show	= wlp_wss_attr_show,
	.store	= wlp_wss_attr_store,
};

struct kobj_type wss_ktype = {
	.release	= wlp_wss_release,
	.sysfs_ops	= &wss_sysfs_ops,
};


/**
 * Sysfs files for individual WSS
 */

/**
 * Print static properties of this WSS
 *
 * The name of a WSS may not be null teminated. It's max size is 64 bytes
 * so we copy it to a larger array just to make sure we print sane data.
 */
static ssize_t wlp_wss_properties_show(struct wlp_wss *wss, char *buf)
{
	int result = 0;

	if (mutex_lock_interruptible(&wss->mutex))
		goto out;
	result = __wlp_wss_properties_show(wss, buf, PAGE_SIZE);
	mutex_unlock(&wss->mutex);
out:
	return result;
}
WSS_ATTR(properties, S_IRUGO, wlp_wss_properties_show, NULL);

/**
 * Print all connected members of this WSS
 * The EDA cache contains all members of WSS neighborhood.
 */
static ssize_t wlp_wss_members_show(struct wlp_wss *wss, char *buf)
{
	struct wlp *wlp = container_of(wss, struct wlp, wss);
	return wlp_eda_show(wlp, buf);
}
WSS_ATTR(members, S_IRUGO, wlp_wss_members_show, NULL);

static
const char *__wlp_strstate[] = {
	"none",
	"partially enrolled",
	"enrolled",
	"active",
	"connected",
};

static const char *wlp_wss_strstate(unsigned state)
{
	if (state >= ARRAY_SIZE(__wlp_strstate))
		return "unknown state";
	return __wlp_strstate[state];
}

/*
 * Print current state of this WSS
 */
static ssize_t wlp_wss_state_show(struct wlp_wss *wss, char *buf)
{
	int result = 0;

	if (mutex_lock_interruptible(&wss->mutex))
		goto out;
	result = scnprintf(buf, PAGE_SIZE, "%s\n",
			   wlp_wss_strstate(wss->state));
	mutex_unlock(&wss->mutex);
out:
	return result;
}
WSS_ATTR(state, S_IRUGO, wlp_wss_state_show, NULL);


static
struct attribute *wss_attrs[] = {
	&wss_attr_properties.attr,
	&wss_attr_members.attr,
	&wss_attr_state.attr,
	NULL,
};

struct attribute_group wss_attr_group = {
	.name = NULL,	/* we want them in the same directory */
	.attrs = wss_attrs,
};
OpenPOWER on IntegriCloud