3 * Initialization, addition and removal of wimax devices
6 * Copyright (C) 2005-2006 Intel Corporation <linux-wimax@intel.com>
7 * Inaky Perez-Gonzalez <inaky.perez-gonzalez@intel.com>
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License version
11 * 2 as published by the Free Software Foundation.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
26 * - basic life cycle of 'struct wimax_dev' [wimax_dev_*()]; on
27 * addition/registration initialize all subfields and allocate
28 * generic netlink resources for user space communication. On
29 * removal/unregistration, undo all that.
31 * - device state machine [wimax_state_change()] and support to send
32 * reports to user space when the state changes
33 * [wimax_gnl_re_state_change*()].
35 * See include/net/wimax.h for rationales and design.
39 * [__]wimax_state_change() Called by drivers to update device's state
40 * wimax_gnl_re_state_change_alloc()
41 * wimax_gnl_re_state_change_send()
43 * wimax_dev_init() Init a device
44 * wimax_dev_add() Register
46 * wimax_gnl_add() Register all the generic netlink resources.
47 * wimax_id_table_add()
48 * wimax_dev_rm() Unregister
53 #include <linux/device.h>
54 #include <linux/gfp.h>
55 #include <net/genetlink.h>
56 #include <linux/netdevice.h>
57 #include <linux/wimax.h>
58 #include "wimax-internal.h"
61 #define D_SUBMODULE stack
62 #include "debug-levels.h"
64 static char wimax_debug_params
[128];
65 module_param_string(debug
, wimax_debug_params
, sizeof(wimax_debug_params
),
67 MODULE_PARM_DESC(debug
,
68 "String of space-separated NAME:VALUE pairs, where NAMEs "
69 "are the different debug submodules and VALUE are the "
70 "initial debug value to set.");
73 * Authoritative source for the RE_STATE_CHANGE attribute policy
75 * We don't really use it here, but /me likes to keep the definition
76 * close to where the data is generated.
79 static const struct nla_policy wimax_gnl_re_status_change[WIMAX_GNL_ATTR_MAX + 1] = {
80 [WIMAX_GNL_STCH_STATE_OLD] = { .type = NLA_U8 },
81 [WIMAX_GNL_STCH_STATE_NEW] = { .type = NLA_U8 },
87 * Allocate a Report State Change message
89 * @header: save it, you need it for _send()
91 * Creates and fills a basic state change message; different code
92 * paths can then add more attributes to the message as needed.
94 * Use wimax_gnl_re_state_change_send() to send the returned skb.
96 * Returns: skb with the genl message if ok, IS_ERR() ptr on error
100 struct sk_buff
*wimax_gnl_re_state_change_alloc(
101 struct wimax_dev
*wimax_dev
,
102 enum wimax_st new_state
, enum wimax_st old_state
,
106 struct device
*dev
= wimax_dev_to_dev(wimax_dev
);
108 struct sk_buff
*report_skb
;
110 d_fnstart(3, dev
, "(wimax_dev %p new_state %u old_state %u)\n",
111 wimax_dev
, new_state
, old_state
);
113 report_skb
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
114 if (report_skb
== NULL
) {
115 dev_err(dev
, "RE_STCH: can't create message\n");
118 data
= genlmsg_put(report_skb
, 0, wimax_gnl_mcg
.id
, &wimax_gnl_family
,
119 0, WIMAX_GNL_RE_STATE_CHANGE
);
121 dev_err(dev
, "RE_STCH: can't put data into message\n");
126 result
= nla_put_u8(report_skb
, WIMAX_GNL_STCH_STATE_OLD
, old_state
);
128 dev_err(dev
, "RE_STCH: Error adding OLD attr: %d\n", result
);
131 result
= nla_put_u8(report_skb
, WIMAX_GNL_STCH_STATE_NEW
, new_state
);
133 dev_err(dev
, "RE_STCH: Error adding NEW attr: %d\n", result
);
136 result
= nla_put_u32(report_skb
, WIMAX_GNL_STCH_IFIDX
,
137 wimax_dev
->net_dev
->ifindex
);
139 dev_err(dev
, "RE_STCH: Error adding IFINDEX attribute\n");
142 d_fnend(3, dev
, "(wimax_dev %p new_state %u old_state %u) = %p\n",
143 wimax_dev
, new_state
, old_state
, report_skb
);
147 nlmsg_free(report_skb
);
149 d_fnend(3, dev
, "(wimax_dev %p new_state %u old_state %u) = %d\n",
150 wimax_dev
, new_state
, old_state
, result
);
151 return ERR_PTR(result
);
156 * Send a Report State Change message (as created with _alloc).
158 * @report_skb: as returned by wimax_gnl_re_state_change_alloc()
159 * @header: as returned by wimax_gnl_re_state_change_alloc()
161 * Returns: 0 if ok, < 0 errno code on error.
163 * If the message is NULL, pretend it didn't happen.
166 int wimax_gnl_re_state_change_send(
167 struct wimax_dev
*wimax_dev
, struct sk_buff
*report_skb
,
171 struct device
*dev
= wimax_dev_to_dev(wimax_dev
);
172 d_fnstart(3, dev
, "(wimax_dev %p report_skb %p)\n",
173 wimax_dev
, report_skb
);
174 if (report_skb
== NULL
) {
178 genlmsg_end(report_skb
, header
);
179 genlmsg_multicast(report_skb
, 0, wimax_gnl_mcg
.id
, GFP_KERNEL
);
181 d_fnend(3, dev
, "(wimax_dev %p report_skb %p) = %d\n",
182 wimax_dev
, report_skb
, result
);
188 void __check_new_state(enum wimax_st old_state
, enum wimax_st new_state
,
189 unsigned allowed_states_bm
)
191 if (WARN_ON(((1 << new_state
) & allowed_states_bm
) == 0)) {
192 printk(KERN_ERR
"SW BUG! Forbidden state change %u -> %u\n",
193 old_state
, new_state
);
199 * Set the current state of a WiMAX device [unlocking version of
200 * wimax_state_change().
202 void __wimax_state_change(struct wimax_dev
*wimax_dev
, enum wimax_st new_state
)
204 struct device
*dev
= wimax_dev_to_dev(wimax_dev
);
205 enum wimax_st old_state
= wimax_dev
->state
;
206 struct sk_buff
*stch_skb
;
209 d_fnstart(3, dev
, "(wimax_dev %p new_state %u [old %u])\n",
210 wimax_dev
, new_state
, old_state
);
212 if (WARN_ON(new_state
>= __WIMAX_ST_INVALID
)) {
213 dev_err(dev
, "SW BUG: requesting invalid state %u\n",
217 if (old_state
== new_state
)
219 header
= NULL
; /* gcc complains? can't grok why */
220 stch_skb
= wimax_gnl_re_state_change_alloc(
221 wimax_dev
, new_state
, old_state
, &header
);
223 /* Verify the state transition and do exit-from-state actions */
225 case __WIMAX_ST_NULL
:
226 __check_new_state(old_state
, new_state
,
230 __check_new_state(old_state
, new_state
,
231 1 << __WIMAX_ST_QUIESCING
232 | 1 << WIMAX_ST_UNINITIALIZED
233 | 1 << WIMAX_ST_RADIO_OFF
);
235 case __WIMAX_ST_QUIESCING
:
236 __check_new_state(old_state
, new_state
, 1 << WIMAX_ST_DOWN
);
238 case WIMAX_ST_UNINITIALIZED
:
239 __check_new_state(old_state
, new_state
,
240 1 << __WIMAX_ST_QUIESCING
241 | 1 << WIMAX_ST_RADIO_OFF
);
243 case WIMAX_ST_RADIO_OFF
:
244 __check_new_state(old_state
, new_state
,
245 1 << __WIMAX_ST_QUIESCING
246 | 1 << WIMAX_ST_READY
);
249 __check_new_state(old_state
, new_state
,
250 1 << __WIMAX_ST_QUIESCING
251 | 1 << WIMAX_ST_RADIO_OFF
252 | 1 << WIMAX_ST_SCANNING
253 | 1 << WIMAX_ST_CONNECTING
254 | 1 << WIMAX_ST_CONNECTED
);
256 case WIMAX_ST_SCANNING
:
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_CONNECTING
262 | 1 << WIMAX_ST_CONNECTED
);
264 case WIMAX_ST_CONNECTING
:
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 | 1 << WIMAX_ST_SCANNING
270 | 1 << WIMAX_ST_CONNECTED
);
272 case WIMAX_ST_CONNECTED
:
273 __check_new_state(old_state
, new_state
,
274 1 << __WIMAX_ST_QUIESCING
275 | 1 << WIMAX_ST_RADIO_OFF
276 | 1 << WIMAX_ST_READY
);
277 netif_tx_disable(wimax_dev
->net_dev
);
278 netif_carrier_off(wimax_dev
->net_dev
);
280 case __WIMAX_ST_INVALID
:
282 dev_err(dev
, "SW BUG: wimax_dev %p is in unknown state %u\n",
283 wimax_dev
, wimax_dev
->state
);
288 /* Execute the actions of entry to the new state */
290 case __WIMAX_ST_NULL
:
291 dev_err(dev
, "SW BUG: wimax_dev %p entering NULL state "
292 "from %u\n", wimax_dev
, wimax_dev
->state
);
293 WARN_ON(1); /* Nobody can enter this state */
297 case __WIMAX_ST_QUIESCING
:
299 case WIMAX_ST_UNINITIALIZED
:
301 case WIMAX_ST_RADIO_OFF
:
305 case WIMAX_ST_SCANNING
:
307 case WIMAX_ST_CONNECTING
:
309 case WIMAX_ST_CONNECTED
:
310 netif_carrier_on(wimax_dev
->net_dev
);
311 netif_wake_queue(wimax_dev
->net_dev
);
313 case __WIMAX_ST_INVALID
:
317 __wimax_state_set(wimax_dev
, new_state
);
318 if (!IS_ERR(stch_skb
))
319 wimax_gnl_re_state_change_send(wimax_dev
, stch_skb
, header
);
321 d_fnend(3, dev
, "(wimax_dev %p new_state %u [old %u]) = void\n",
322 wimax_dev
, new_state
, old_state
);
327 * wimax_state_change - Set the current state of a WiMAX device
329 * @wimax_dev: WiMAX device descriptor (properly referenced)
330 * @new_state: New state to switch to
332 * This implements the state changes for the wimax devices. It will
334 * - verify that the state transition is legal (for now it'll just
335 * print a warning if not) according to the table in
336 * linux/wimax.h's documentation for 'enum wimax_st'.
338 * - perform the actions needed for leaving the current state and
339 * whichever are needed for entering the new state.
341 * - issue a report to user space indicating the new state (and an
342 * optional payload with information about the new state).
344 * NOTE: @wimax_dev must be locked
346 void wimax_state_change(struct wimax_dev
*wimax_dev
, enum wimax_st new_state
)
349 * A driver cannot take the wimax_dev out of the
350 * __WIMAX_ST_NULL state unless by calling wimax_dev_add(). If
351 * the wimax_dev's state is still NULL, we ignore any request
352 * to change its state because it means it hasn't been yet
355 * There is no need to complain about it, as routines that
356 * call this might be shared from different code paths that
357 * are called before or after wimax_dev_add() has done its
360 mutex_lock(&wimax_dev
->mutex
);
361 if (wimax_dev
->state
> __WIMAX_ST_NULL
)
362 __wimax_state_change(wimax_dev
, new_state
);
363 mutex_unlock(&wimax_dev
->mutex
);
365 EXPORT_SYMBOL_GPL(wimax_state_change
);
369 * wimax_state_get() - Return the current state of a WiMAX device
371 * @wimax_dev: WiMAX device descriptor
373 * Returns: Current state of the device according to its driver.
375 enum wimax_st
wimax_state_get(struct wimax_dev
*wimax_dev
)
378 mutex_lock(&wimax_dev
->mutex
);
379 state
= wimax_dev
->state
;
380 mutex_unlock(&wimax_dev
->mutex
);
383 EXPORT_SYMBOL_GPL(wimax_state_get
);
387 * wimax_dev_init - initialize a newly allocated instance
389 * @wimax_dev: WiMAX device descriptor to initialize.
391 * Initializes fields of a freshly allocated @wimax_dev instance. This
392 * function assumes that after allocation, the memory occupied by
393 * @wimax_dev was zeroed.
395 void wimax_dev_init(struct wimax_dev
*wimax_dev
)
397 INIT_LIST_HEAD(&wimax_dev
->id_table_node
);
398 __wimax_state_set(wimax_dev
, __WIMAX_ST_NULL
);
399 mutex_init(&wimax_dev
->mutex
);
400 mutex_init(&wimax_dev
->mutex_reset
);
402 EXPORT_SYMBOL_GPL(wimax_dev_init
);
405 * This extern is declared here because it's easier to keep track --
406 * both declarations are a list of the same
408 extern struct genl_ops
409 wimax_gnl_msg_from_user
,
415 struct genl_ops
*wimax_gnl_ops
[] = {
416 &wimax_gnl_msg_from_user
,
419 &wimax_gnl_state_get
,
424 size_t wimax_addr_scnprint(char *addr_str
, size_t addr_str_size
,
425 unsigned char *addr
, size_t addr_len
)
428 for (total
= cnt
= 0; cnt
< addr_len
; cnt
++)
429 total
+= scnprintf(addr_str
+ total
, addr_str_size
- total
,
431 cnt
== addr_len
- 1 ? '\0' : ':');
437 * wimax_dev_add - Register a new WiMAX device
439 * @wimax_dev: WiMAX device descriptor (as embedded in your @net_dev's
440 * priv data). You must have called wimax_dev_init() on it before.
442 * @net_dev: net device the @wimax_dev is associated with. The
443 * function expects SET_NETDEV_DEV() and register_netdev() were
444 * already called on it.
446 * Registers the new WiMAX device, sets up the user-kernel control
447 * interface (generic netlink) and common WiMAX infrastructure.
449 * Note that the parts that will allow interaction with user space are
450 * setup at the very end, when the rest is in place, as once that
451 * happens, the driver might get user space control requests via
452 * netlink or from debugfs that might translate into calls into
455 int wimax_dev_add(struct wimax_dev
*wimax_dev
, struct net_device
*net_dev
)
458 struct device
*dev
= net_dev
->dev
.parent
;
461 d_fnstart(3, dev
, "(wimax_dev %p net_dev %p)\n", wimax_dev
, net_dev
);
463 /* Do the RFKILL setup before locking, as RFKILL will call
464 * into our functions. */
465 wimax_dev
->net_dev
= net_dev
;
466 result
= wimax_rfkill_add(wimax_dev
);
468 goto error_rfkill_add
;
470 /* Set up user-space interaction */
471 mutex_lock(&wimax_dev
->mutex
);
472 wimax_id_table_add(wimax_dev
);
473 result
= wimax_debugfs_add(wimax_dev
);
475 dev_err(dev
, "cannot initialize debugfs: %d\n",
477 goto error_debugfs_add
;
480 __wimax_state_set(wimax_dev
, WIMAX_ST_DOWN
);
481 mutex_unlock(&wimax_dev
->mutex
);
483 wimax_addr_scnprint(addr_str
, sizeof(addr_str
),
484 net_dev
->dev_addr
, net_dev
->addr_len
);
485 dev_err(dev
, "WiMAX interface %s (%s) ready\n",
486 net_dev
->name
, addr_str
);
487 d_fnend(3, dev
, "(wimax_dev %p net_dev %p) = 0\n", wimax_dev
, net_dev
);
491 wimax_id_table_rm(wimax_dev
);
492 mutex_unlock(&wimax_dev
->mutex
);
493 wimax_rfkill_rm(wimax_dev
);
495 d_fnend(3, dev
, "(wimax_dev %p net_dev %p) = %d\n",
496 wimax_dev
, net_dev
, result
);
499 EXPORT_SYMBOL_GPL(wimax_dev_add
);
503 * wimax_dev_rm - Unregister an existing WiMAX device
505 * @wimax_dev: WiMAX device descriptor
507 * Unregisters a WiMAX device previously registered for use with
510 * IMPORTANT! Must call before calling unregister_netdev().
512 * After this function returns, you will not get any more user space
513 * control requests (via netlink or debugfs) and thus to wimax_dev->ops.
515 * Reentrancy control is ensured by setting the state to
516 * %__WIMAX_ST_QUIESCING. rfkill operations coming through
517 * wimax_*rfkill*() will be stopped by the quiescing state; ops coming
518 * from the rfkill subsystem will be stopped by the support being
519 * removed by wimax_rfkill_rm().
521 void wimax_dev_rm(struct wimax_dev
*wimax_dev
)
523 d_fnstart(3, NULL
, "(wimax_dev %p)\n", wimax_dev
);
525 mutex_lock(&wimax_dev
->mutex
);
526 __wimax_state_change(wimax_dev
, __WIMAX_ST_QUIESCING
);
527 wimax_debugfs_rm(wimax_dev
);
528 wimax_id_table_rm(wimax_dev
);
529 __wimax_state_change(wimax_dev
, WIMAX_ST_DOWN
);
530 mutex_unlock(&wimax_dev
->mutex
);
531 wimax_rfkill_rm(wimax_dev
);
532 d_fnend(3, NULL
, "(wimax_dev %p) = void\n", wimax_dev
);
534 EXPORT_SYMBOL_GPL(wimax_dev_rm
);
537 /* Debug framework control of debug levels */
538 struct d_level D_LEVEL
[] = {
539 D_SUBMODULE_DEFINE(debugfs
),
540 D_SUBMODULE_DEFINE(id_table
),
541 D_SUBMODULE_DEFINE(op_msg
),
542 D_SUBMODULE_DEFINE(op_reset
),
543 D_SUBMODULE_DEFINE(op_rfkill
),
544 D_SUBMODULE_DEFINE(op_state_get
),
545 D_SUBMODULE_DEFINE(stack
),
547 size_t D_LEVEL_SIZE
= ARRAY_SIZE(D_LEVEL
);
550 struct genl_family wimax_gnl_family
= {
551 .id
= GENL_ID_GENERATE
,
553 .version
= WIMAX_GNL_VERSION
,
555 .maxattr
= WIMAX_GNL_ATTR_MAX
,
558 struct genl_multicast_group wimax_gnl_mcg
= {
564 /* Shutdown the wimax stack */
566 int __init
wimax_subsys_init(void)
570 d_fnstart(4, NULL
, "()\n");
571 d_parse_params(D_LEVEL
, D_LEVEL_SIZE
, wimax_debug_params
,
574 snprintf(wimax_gnl_family
.name
, sizeof(wimax_gnl_family
.name
),
576 result
= genl_register_family(&wimax_gnl_family
);
577 if (unlikely(result
< 0)) {
578 printk(KERN_ERR
"cannot register generic netlink family: %d\n",
580 goto error_register_family
;
583 for (cnt
= 0; cnt
< ARRAY_SIZE(wimax_gnl_ops
); cnt
++) {
584 result
= genl_register_ops(&wimax_gnl_family
,
586 d_printf(4, NULL
, "registering generic netlink op code "
587 "%u: %d\n", wimax_gnl_ops
[cnt
]->cmd
, result
);
588 if (unlikely(result
< 0)) {
589 printk(KERN_ERR
"cannot register generic netlink op "
591 wimax_gnl_ops
[cnt
]->cmd
, result
);
592 goto error_register_ops
;
596 result
= genl_register_mc_group(&wimax_gnl_family
, &wimax_gnl_mcg
);
599 d_fnend(4, NULL
, "() = 0\n");
604 for (cnt
--; cnt
>= 0; cnt
--)
605 genl_unregister_ops(&wimax_gnl_family
,
607 genl_unregister_family(&wimax_gnl_family
);
608 error_register_family
:
609 d_fnend(4, NULL
, "() = %d\n", result
);
613 module_init(wimax_subsys_init
);
616 /* Shutdown the wimax stack */
618 void __exit
wimax_subsys_exit(void)
621 wimax_id_table_release();
622 genl_unregister_mc_group(&wimax_gnl_family
, &wimax_gnl_mcg
);
623 for (cnt
= ARRAY_SIZE(wimax_gnl_ops
) - 1; cnt
>= 0; cnt
--)
624 genl_unregister_ops(&wimax_gnl_family
,
626 genl_unregister_family(&wimax_gnl_family
);
628 module_exit(wimax_subsys_exit
);
630 MODULE_AUTHOR("Intel Corporation <linux-wimax@intel.com>");
631 MODULE_DESCRIPTION("Linux WiMAX stack");
632 MODULE_LICENSE("GPL");