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#include <linux/device.h>
54#include <net/genetlink.h>
55#include <linux/netdevice.h>
56#include <linux/wimax.h>
57#include "wimax-internal.h"
58
59
60#define D_SUBMODULE stack
61#include "debug-levels.h"
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
91static
92struct sk_buff *wimax_gnl_re_state_change_alloc(
93 struct wimax_dev *wimax_dev,
94 enum wimax_st new_state, enum wimax_st old_state,
95 void **header)
96{
97 int result;
98 struct device *dev = wimax_dev_to_dev(wimax_dev);
99 void *data;
100 struct sk_buff *report_skb;
101
102 d_fnstart(3, dev, "(wimax_dev %p new_state %u old_state %u)\n",
103 wimax_dev, new_state, old_state);
104 result = -ENOMEM;
105 report_skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
106 if (report_skb == NULL) {
107 dev_err(dev, "RE_STCH: can't create message\n");
108 goto error_new;
109 }
110 data = genlmsg_put(report_skb, 0, wimax_gnl_mcg.id, &wimax_gnl_family,
111 0, WIMAX_GNL_RE_STATE_CHANGE);
112 if (data == NULL) {
113 dev_err(dev, "RE_STCH: can't put data into message\n");
114 goto error_put;
115 }
116 *header = data;
117
118 result = nla_put_u8(report_skb, WIMAX_GNL_STCH_STATE_OLD, old_state);
119 if (result < 0) {
120 dev_err(dev, "RE_STCH: Error adding OLD attr: %d\n", result);
121 goto error_put;
122 }
123 result = nla_put_u8(report_skb, WIMAX_GNL_STCH_STATE_NEW, new_state);
124 if (result < 0) {
125 dev_err(dev, "RE_STCH: Error adding NEW attr: %d\n", result);
126 goto error_put;
127 }
128 result = nla_put_u32(report_skb, WIMAX_GNL_STCH_IFIDX,
129 wimax_dev->net_dev->ifindex);
130 if (result < 0) {
131 dev_err(dev, "RE_STCH: Error adding IFINDEX attribute\n");
132 goto error_put;
133 }
134 d_fnend(3, dev, "(wimax_dev %p new_state %u old_state %u) = %p\n",
135 wimax_dev, new_state, old_state, report_skb);
136 return report_skb;
137
138error_put:
139 nlmsg_free(report_skb);
140error_new:
141 d_fnend(3, dev, "(wimax_dev %p new_state %u old_state %u) = %d\n",
142 wimax_dev, new_state, old_state, result);
143 return ERR_PTR(result);
144}
145
146
147
148
149
150
151
152
153
154
155
156
157static
158int wimax_gnl_re_state_change_send(
159 struct wimax_dev *wimax_dev, struct sk_buff *report_skb,
160 void *header)
161{
162 int result = 0;
163 struct device *dev = wimax_dev_to_dev(wimax_dev);
164 d_fnstart(3, dev, "(wimax_dev %p report_skb %p)\n",
165 wimax_dev, report_skb);
166 if (report_skb == NULL) {
167 result = -ENOMEM;
168 goto out;
169 }
170 genlmsg_end(report_skb, header);
171 genlmsg_multicast(report_skb, 0, wimax_gnl_mcg.id, GFP_KERNEL);
172out:
173 d_fnend(3, dev, "(wimax_dev %p report_skb %p) = %d\n",
174 wimax_dev, report_skb, result);
175 return result;
176}
177
178
179static
180void __check_new_state(enum wimax_st old_state, enum wimax_st new_state,
181 unsigned allowed_states_bm)
182{
183 if (WARN_ON(((1 << new_state) & allowed_states_bm) == 0)) {
184 printk(KERN_ERR "SW BUG! Forbidden state change %u -> %u\n",
185 old_state, new_state);
186 }
187}
188
189
190
191
192
193
194void __wimax_state_change(struct wimax_dev *wimax_dev, enum wimax_st new_state)
195{
196 struct device *dev = wimax_dev_to_dev(wimax_dev);
197 enum wimax_st old_state = wimax_dev->state;
198 struct sk_buff *stch_skb;
199 void *header;
200
201 d_fnstart(3, dev, "(wimax_dev %p new_state %u [old %u])\n",
202 wimax_dev, new_state, old_state);
203
204 if (WARN_ON(new_state >= __WIMAX_ST_INVALID)) {
205 dev_err(dev, "SW BUG: requesting invalid state %u\n",
206 new_state);
207 goto out;
208 }
209 if (old_state == new_state)
210 goto out;
211 header = NULL;
212 stch_skb = wimax_gnl_re_state_change_alloc(
213 wimax_dev, new_state, old_state, &header);
214
215
216 switch (old_state) {
217 case __WIMAX_ST_NULL:
218 __check_new_state(old_state, new_state,
219 1 << WIMAX_ST_DOWN);
220 break;
221 case WIMAX_ST_DOWN:
222 __check_new_state(old_state, new_state,
223 1 << __WIMAX_ST_QUIESCING
224 | 1 << WIMAX_ST_UNINITIALIZED
225 | 1 << WIMAX_ST_RADIO_OFF);
226 break;
227 case __WIMAX_ST_QUIESCING:
228 __check_new_state(old_state, new_state, 1 << WIMAX_ST_DOWN);
229 break;
230 case WIMAX_ST_UNINITIALIZED:
231 __check_new_state(old_state, new_state,
232 1 << __WIMAX_ST_QUIESCING
233 | 1 << WIMAX_ST_RADIO_OFF);
234 break;
235 case WIMAX_ST_RADIO_OFF:
236 __check_new_state(old_state, new_state,
237 1 << __WIMAX_ST_QUIESCING
238 | 1 << WIMAX_ST_READY);
239 break;
240 case WIMAX_ST_READY:
241 __check_new_state(old_state, new_state,
242 1 << __WIMAX_ST_QUIESCING
243 | 1 << WIMAX_ST_RADIO_OFF
244 | 1 << WIMAX_ST_SCANNING
245 | 1 << WIMAX_ST_CONNECTING
246 | 1 << WIMAX_ST_CONNECTED);
247 break;
248 case WIMAX_ST_SCANNING:
249 __check_new_state(old_state, new_state,
250 1 << __WIMAX_ST_QUIESCING
251 | 1 << WIMAX_ST_RADIO_OFF
252 | 1 << WIMAX_ST_READY
253 | 1 << WIMAX_ST_CONNECTING
254 | 1 << WIMAX_ST_CONNECTED);
255 break;
256 case WIMAX_ST_CONNECTING:
257 __check_new_state(old_state, new_state,
258 1 << __WIMAX_ST_QUIESCING
259 | 1 << WIMAX_ST_RADIO_OFF
260 | 1 << WIMAX_ST_READY
261 | 1 << WIMAX_ST_SCANNING
262 | 1 << WIMAX_ST_CONNECTED);
263 break;
264 case WIMAX_ST_CONNECTED:
265 __check_new_state(old_state, new_state,
266 1 << __WIMAX_ST_QUIESCING
267 | 1 << WIMAX_ST_RADIO_OFF
268 | 1 << WIMAX_ST_READY);
269 netif_tx_disable(wimax_dev->net_dev);
270 netif_carrier_off(wimax_dev->net_dev);
271 break;
272 case __WIMAX_ST_INVALID:
273 default:
274 dev_err(dev, "SW BUG: wimax_dev %p is in unknown state %u\n",
275 wimax_dev, wimax_dev->state);
276 WARN_ON(1);
277 goto out;
278 }
279
280
281 switch (new_state) {
282 case __WIMAX_ST_NULL:
283 dev_err(dev, "SW BUG: wimax_dev %p entering NULL state "
284 "from %u\n", wimax_dev, wimax_dev->state);
285 WARN_ON(1);
286 break;
287 case WIMAX_ST_DOWN:
288 break;
289 case __WIMAX_ST_QUIESCING:
290 break;
291 case WIMAX_ST_UNINITIALIZED:
292 break;
293 case WIMAX_ST_RADIO_OFF:
294 break;
295 case WIMAX_ST_READY:
296 break;
297 case WIMAX_ST_SCANNING:
298 break;
299 case WIMAX_ST_CONNECTING:
300 break;
301 case WIMAX_ST_CONNECTED:
302 netif_carrier_on(wimax_dev->net_dev);
303 netif_wake_queue(wimax_dev->net_dev);
304 break;
305 case __WIMAX_ST_INVALID:
306 default:
307 BUG();
308 }
309 __wimax_state_set(wimax_dev, new_state);
310 if (stch_skb)
311 wimax_gnl_re_state_change_send(wimax_dev, stch_skb, header);
312out:
313 d_fnend(3, dev, "(wimax_dev %p new_state %u [old %u]) = void\n",
314 wimax_dev, new_state, old_state);
315 return;
316}
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339void wimax_state_change(struct wimax_dev *wimax_dev, enum wimax_st new_state)
340{
341
342
343
344
345
346
347
348
349
350
351
352
353 mutex_lock(&wimax_dev->mutex);
354 if (wimax_dev->state > __WIMAX_ST_NULL)
355 __wimax_state_change(wimax_dev, new_state);
356 mutex_unlock(&wimax_dev->mutex);
357 return;
358}
359EXPORT_SYMBOL_GPL(wimax_state_change);
360
361
362
363
364
365
366
367
368
369enum wimax_st wimax_state_get(struct wimax_dev *wimax_dev)
370{
371 enum wimax_st state;
372 mutex_lock(&wimax_dev->mutex);
373 state = wimax_dev->state;
374 mutex_unlock(&wimax_dev->mutex);
375 return state;
376}
377EXPORT_SYMBOL_GPL(wimax_state_get);
378
379
380
381
382
383
384
385
386
387
388
389void wimax_dev_init(struct wimax_dev *wimax_dev)
390{
391 INIT_LIST_HEAD(&wimax_dev->id_table_node);
392 __wimax_state_set(wimax_dev, __WIMAX_ST_NULL);
393 mutex_init(&wimax_dev->mutex);
394 mutex_init(&wimax_dev->mutex_reset);
395}
396EXPORT_SYMBOL_GPL(wimax_dev_init);
397
398
399
400
401
402extern struct genl_ops
403 wimax_gnl_msg_from_user,
404 wimax_gnl_reset,
405 wimax_gnl_rfkill,
406 wimax_gnl_state_get;
407
408static
409struct genl_ops *wimax_gnl_ops[] = {
410 &wimax_gnl_msg_from_user,
411 &wimax_gnl_reset,
412 &wimax_gnl_rfkill,
413 &wimax_gnl_state_get,
414};
415
416
417static
418size_t wimax_addr_scnprint(char *addr_str, size_t addr_str_size,
419 unsigned char *addr, size_t addr_len)
420{
421 unsigned cnt, total;
422 for (total = cnt = 0; cnt < addr_len; cnt++)
423 total += scnprintf(addr_str + total, addr_str_size - total,
424 "%02x%c", addr[cnt],
425 cnt == addr_len - 1 ? '\0' : ':');
426 return total;
427}
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449int wimax_dev_add(struct wimax_dev *wimax_dev, struct net_device *net_dev)
450{
451 int result;
452 struct device *dev = net_dev->dev.parent;
453 char addr_str[32];
454
455 d_fnstart(3, dev, "(wimax_dev %p net_dev %p)\n", wimax_dev, net_dev);
456
457
458
459 wimax_dev->net_dev = net_dev;
460 result = wimax_rfkill_add(wimax_dev);
461 if (result < 0)
462 goto error_rfkill_add;
463
464
465 mutex_lock(&wimax_dev->mutex);
466 wimax_id_table_add(wimax_dev);
467 result = wimax_debugfs_add(wimax_dev);
468 if (result < 0) {
469 dev_err(dev, "cannot initialize debugfs: %d\n",
470 result);
471 goto error_debugfs_add;
472 }
473
474 __wimax_state_set(wimax_dev, WIMAX_ST_DOWN);
475 mutex_unlock(&wimax_dev->mutex);
476
477 wimax_addr_scnprint(addr_str, sizeof(addr_str),
478 net_dev->dev_addr, net_dev->addr_len);
479 dev_err(dev, "WiMAX interface %s (%s) ready\n",
480 net_dev->name, addr_str);
481 d_fnend(3, dev, "(wimax_dev %p net_dev %p) = 0\n", wimax_dev, net_dev);
482 return 0;
483
484error_debugfs_add:
485 wimax_id_table_rm(wimax_dev);
486 mutex_unlock(&wimax_dev->mutex);
487 wimax_rfkill_rm(wimax_dev);
488error_rfkill_add:
489 d_fnend(3, dev, "(wimax_dev %p net_dev %p) = %d\n",
490 wimax_dev, net_dev, result);
491 return result;
492}
493EXPORT_SYMBOL_GPL(wimax_dev_add);
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515void wimax_dev_rm(struct wimax_dev *wimax_dev)
516{
517 d_fnstart(3, NULL, "(wimax_dev %p)\n", wimax_dev);
518
519 mutex_lock(&wimax_dev->mutex);
520 __wimax_state_change(wimax_dev, __WIMAX_ST_QUIESCING);
521 wimax_debugfs_rm(wimax_dev);
522 wimax_id_table_rm(wimax_dev);
523 __wimax_state_change(wimax_dev, WIMAX_ST_DOWN);
524 mutex_unlock(&wimax_dev->mutex);
525 wimax_rfkill_rm(wimax_dev);
526 d_fnend(3, NULL, "(wimax_dev %p) = void\n", wimax_dev);
527}
528EXPORT_SYMBOL_GPL(wimax_dev_rm);
529
530
531
532struct d_level D_LEVEL[] = {
533 D_SUBMODULE_DEFINE(debugfs),
534 D_SUBMODULE_DEFINE(id_table),
535 D_SUBMODULE_DEFINE(op_msg),
536 D_SUBMODULE_DEFINE(op_reset),
537 D_SUBMODULE_DEFINE(op_rfkill),
538 D_SUBMODULE_DEFINE(op_state_get),
539 D_SUBMODULE_DEFINE(stack),
540};
541size_t D_LEVEL_SIZE = ARRAY_SIZE(D_LEVEL);
542
543
544struct genl_family wimax_gnl_family = {
545 .id = GENL_ID_GENERATE,
546 .name = "WiMAX",
547 .version = WIMAX_GNL_VERSION,
548 .hdrsize = 0,
549 .maxattr = WIMAX_GNL_ATTR_MAX,
550};
551
552struct genl_multicast_group wimax_gnl_mcg = {
553 .name = "msg",
554};
555
556
557
558
559static
560int __init wimax_subsys_init(void)
561{
562 int result, cnt;
563
564 d_fnstart(4, NULL, "()\n");
565 snprintf(wimax_gnl_family.name, sizeof(wimax_gnl_family.name),
566 "WiMAX");
567 result = genl_register_family(&wimax_gnl_family);
568 if (unlikely(result < 0)) {
569 printk(KERN_ERR "cannot register generic netlink family: %d\n",
570 result);
571 goto error_register_family;
572 }
573
574 for (cnt = 0; cnt < ARRAY_SIZE(wimax_gnl_ops); cnt++) {
575 result = genl_register_ops(&wimax_gnl_family,
576 wimax_gnl_ops[cnt]);
577 d_printf(4, NULL, "registering generic netlink op code "
578 "%u: %d\n", wimax_gnl_ops[cnt]->cmd, result);
579 if (unlikely(result < 0)) {
580 printk(KERN_ERR "cannot register generic netlink op "
581 "code %u: %d\n",
582 wimax_gnl_ops[cnt]->cmd, result);
583 goto error_register_ops;
584 }
585 }
586
587 result = genl_register_mc_group(&wimax_gnl_family, &wimax_gnl_mcg);
588 if (result < 0)
589 goto error_mc_group;
590 d_fnend(4, NULL, "() = 0\n");
591 return 0;
592
593error_mc_group:
594error_register_ops:
595 for (cnt--; cnt >= 0; cnt--)
596 genl_unregister_ops(&wimax_gnl_family,
597 wimax_gnl_ops[cnt]);
598 genl_unregister_family(&wimax_gnl_family);
599error_register_family:
600 d_fnend(4, NULL, "() = %d\n", result);
601 return result;
602
603}
604module_init(wimax_subsys_init);
605
606
607
608static
609void __exit wimax_subsys_exit(void)
610{
611 int cnt;
612 wimax_id_table_release();
613 genl_unregister_mc_group(&wimax_gnl_family, &wimax_gnl_mcg);
614 for (cnt = ARRAY_SIZE(wimax_gnl_ops) - 1; cnt >= 0; cnt--)
615 genl_unregister_ops(&wimax_gnl_family,
616 wimax_gnl_ops[cnt]);
617 genl_unregister_family(&wimax_gnl_family);
618}
619module_exit(wimax_subsys_exit);
620
621MODULE_AUTHOR("Intel Corporation <linux-wimax@intel.com>");
622MODULE_DESCRIPTION("Linux WiMAX stack");
623MODULE_LICENSE("GPL");
624
625