| Inaky Perez-Gonzalez | 15530df | 2008-12-20 16:57:36 -0800 | [diff] [blame] | 1 | /* | 
 | 2 |  * Linux WiMAX | 
 | 3 |  * Initialization, addition and removal of wimax devices | 
 | 4 |  * | 
 | 5 |  * | 
 | 6 |  * Copyright (C) 2005-2006 Intel Corporation <linux-wimax@intel.com> | 
 | 7 |  * Inaky Perez-Gonzalez <inaky.perez-gonzalez@intel.com> | 
 | 8 |  * | 
 | 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. | 
 | 12 |  * | 
 | 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. | 
 | 17 |  * | 
 | 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 | 
 | 21 |  * 02110-1301, USA. | 
 | 22 |  * | 
 | 23 |  * | 
 | 24 |  * This implements: | 
 | 25 |  * | 
 | 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. | 
 | 30 |  * | 
 | 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*()]. | 
 | 34 |  * | 
 | 35 |  * See include/net/wimax.h for rationales and design. | 
 | 36 |  * | 
 | 37 |  * ROADMAP | 
 | 38 |  * | 
 | 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() | 
 | 42 |  * | 
 | 43 |  * wimax_dev_init()	        Init a device | 
 | 44 |  * wimax_dev_add()              Register | 
 | 45 |  *   wimax_rfkill_add() | 
 | 46 |  *   wimax_gnl_add()            Register all the generic netlink resources. | 
 | 47 |  *   wimax_id_table_add() | 
 | 48 |  * wimax_dev_rm()               Unregister | 
 | 49 |  *   wimax_id_table_rm() | 
 | 50 |  *   wimax_gnl_rm() | 
 | 51 |  *   wimax_rfkill_rm() | 
 | 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 |  * Authoritative source for the RE_STATE_CHANGE attribute policy | 
 | 65 |  * | 
 | 66 |  * We don't really use it here, but /me likes to keep the definition | 
 | 67 |  * close to where the data is generated. | 
 | 68 |  */ | 
 | 69 | /* | 
 | 70 | static const | 
 | 71 | struct nla_policy wimax_gnl_re_status_change[WIMAX_GNL_ATTR_MAX + 1] = { | 
 | 72 | 	[WIMAX_GNL_STCH_STATE_OLD] = { .type = NLA_U8 }, | 
 | 73 | 	[WIMAX_GNL_STCH_STATE_NEW] = { .type = NLA_U8 }, | 
 | 74 | }; | 
 | 75 | */ | 
 | 76 |  | 
 | 77 |  | 
 | 78 | /* | 
 | 79 |  * Allocate a Report State Change message | 
 | 80 |  * | 
 | 81 |  * @header: save it, you need it for _send() | 
 | 82 |  * | 
 | 83 |  * Creates and fills a basic state change message; different code | 
 | 84 |  * paths can then add more attributes to the message as needed. | 
 | 85 |  * | 
 | 86 |  * Use wimax_gnl_re_state_change_send() to send the returned skb. | 
 | 87 |  * | 
 | 88 |  * Returns: skb with the genl message if ok, IS_ERR() ptr on error | 
 | 89 |  *     with an errno code. | 
 | 90 |  */ | 
 | 91 | static | 
 | 92 | struct 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 |  | 
 | 138 | error_put: | 
 | 139 | 	nlmsg_free(report_skb); | 
 | 140 | error_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 |  * Send a Report State Change message (as created with _alloc). | 
 | 149 |  * | 
 | 150 |  * @report_skb: as returned by wimax_gnl_re_state_change_alloc() | 
 | 151 |  * @header: as returned by wimax_gnl_re_state_change_alloc() | 
 | 152 |  * | 
 | 153 |  * Returns: 0 if ok, < 0 errno code on error. | 
 | 154 |  * | 
 | 155 |  * If the message is  NULL, pretend it didn't happen. | 
 | 156 |  */ | 
 | 157 | static | 
 | 158 | int 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); | 
| Pablo Neira Ayuso | ff491a7 | 2009-02-05 23:56:36 -0800 | [diff] [blame] | 166 | 	if (report_skb == NULL) { | 
 | 167 | 		result = -ENOMEM; | 
| Inaky Perez-Gonzalez | 15530df | 2008-12-20 16:57:36 -0800 | [diff] [blame] | 168 | 		goto out; | 
| Inaky Perez-Gonzalez | 15530df | 2008-12-20 16:57:36 -0800 | [diff] [blame] | 169 | 	} | 
| Pablo Neira Ayuso | ff491a7 | 2009-02-05 23:56:36 -0800 | [diff] [blame] | 170 | 	genlmsg_end(report_skb, header); | 
 | 171 | 	genlmsg_multicast(report_skb, 0, wimax_gnl_mcg.id, GFP_KERNEL); | 
| Inaky Perez-Gonzalez | 15530df | 2008-12-20 16:57:36 -0800 | [diff] [blame] | 172 | out: | 
 | 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 |  | 
 | 179 | static | 
 | 180 | void __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 |  * Set the current state of a WiMAX device [unlocking version of | 
 | 192 |  * wimax_state_change(). | 
 | 193 |  */ | 
 | 194 | void __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;	/* gcc complains? can't grok why */ | 
 | 212 | 	stch_skb = wimax_gnl_re_state_change_alloc( | 
 | 213 | 		wimax_dev, new_state, old_state, &header); | 
 | 214 |  | 
 | 215 | 	/* Verify the state transition and do exit-from-state actions */ | 
 | 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 | 	/* Execute the actions of entry to the new state */ | 
 | 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);		/* Nobody can enter this state */ | 
 | 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); | 
 | 312 | out: | 
 | 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 |  * wimax_state_change - Set the current state of a WiMAX device | 
 | 321 |  * | 
 | 322 |  * @wimax_dev: WiMAX device descriptor (properly referenced) | 
 | 323 |  * @new_state: New state to switch to | 
 | 324 |  * | 
 | 325 |  * This implements the state changes for the wimax devices. It will | 
 | 326 |  * | 
 | 327 |  * - verify that the state transition is legal (for now it'll just | 
 | 328 |  *   print a warning if not) according to the table in | 
 | 329 |  *   linux/wimax.h's documentation for 'enum wimax_st'. | 
 | 330 |  * | 
 | 331 |  * - perform the actions needed for leaving the current state and | 
 | 332 |  *   whichever are needed for entering the new state. | 
 | 333 |  * | 
 | 334 |  * - issue a report to user space indicating the new state (and an | 
 | 335 |  *   optional payload with information about the new state). | 
 | 336 |  * | 
 | 337 |  * NOTE: @wimax_dev must be locked | 
 | 338 |  */ | 
 | 339 | void wimax_state_change(struct wimax_dev *wimax_dev, enum wimax_st new_state) | 
 | 340 | { | 
| Inaky Perez-Gonzalez | 94c7f2d | 2009-05-02 02:30:28 -0700 | [diff] [blame] | 341 | 	/* | 
 | 342 | 	 * A driver cannot take the wimax_dev out of the | 
 | 343 | 	 * __WIMAX_ST_NULL state unless by calling wimax_dev_add(). If | 
 | 344 | 	 * the wimax_dev's state is still NULL, we ignore any request | 
 | 345 | 	 * to change its state because it means it hasn't been yet | 
 | 346 | 	 * registered. | 
 | 347 | 	 * | 
 | 348 | 	 * There is no need to complain about it, as routines that | 
 | 349 | 	 * call this might be shared from different code paths that | 
 | 350 | 	 * are called before or after wimax_dev_add() has done its | 
 | 351 | 	 * job. | 
 | 352 | 	 */ | 
| Inaky Perez-Gonzalez | 15530df | 2008-12-20 16:57:36 -0800 | [diff] [blame] | 353 | 	mutex_lock(&wimax_dev->mutex); | 
| Inaky Perez-Gonzalez | 94c7f2d | 2009-05-02 02:30:28 -0700 | [diff] [blame] | 354 | 	if (wimax_dev->state > __WIMAX_ST_NULL) | 
 | 355 | 		__wimax_state_change(wimax_dev, new_state); | 
| Inaky Perez-Gonzalez | 15530df | 2008-12-20 16:57:36 -0800 | [diff] [blame] | 356 | 	mutex_unlock(&wimax_dev->mutex); | 
 | 357 | 	return; | 
 | 358 | } | 
 | 359 | EXPORT_SYMBOL_GPL(wimax_state_change); | 
 | 360 |  | 
 | 361 |  | 
 | 362 | /** | 
 | 363 |  * wimax_state_get() - Return the current state of a WiMAX device | 
 | 364 |  * | 
 | 365 |  * @wimax_dev: WiMAX device descriptor | 
 | 366 |  * | 
 | 367 |  * Returns: Current state of the device according to its driver. | 
 | 368 |  */ | 
 | 369 | enum 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 | } | 
 | 377 | EXPORT_SYMBOL_GPL(wimax_state_get); | 
 | 378 |  | 
 | 379 |  | 
 | 380 | /** | 
 | 381 |  * wimax_dev_init - initialize a newly allocated instance | 
 | 382 |  * | 
 | 383 |  * @wimax_dev: WiMAX device descriptor to initialize. | 
 | 384 |  * | 
 | 385 |  * Initializes fields of a freshly allocated @wimax_dev instance. This | 
 | 386 |  * function assumes that after allocation, the memory occupied by | 
 | 387 |  * @wimax_dev was zeroed. | 
 | 388 |  */ | 
 | 389 | void wimax_dev_init(struct wimax_dev *wimax_dev) | 
 | 390 | { | 
 | 391 | 	INIT_LIST_HEAD(&wimax_dev->id_table_node); | 
| Inaky Perez-Gonzalez | 94c7f2d | 2009-05-02 02:30:28 -0700 | [diff] [blame] | 392 | 	__wimax_state_set(wimax_dev, __WIMAX_ST_NULL); | 
| Inaky Perez-Gonzalez | 15530df | 2008-12-20 16:57:36 -0800 | [diff] [blame] | 393 | 	mutex_init(&wimax_dev->mutex); | 
 | 394 | 	mutex_init(&wimax_dev->mutex_reset); | 
 | 395 | } | 
 | 396 | EXPORT_SYMBOL_GPL(wimax_dev_init); | 
 | 397 |  | 
 | 398 | /* | 
 | 399 |  * This extern is declared here because it's easier to keep track -- | 
 | 400 |  * both declarations are a list of the same | 
 | 401 |  */ | 
 | 402 | extern struct genl_ops | 
 | 403 | 	wimax_gnl_msg_from_user, | 
 | 404 | 	wimax_gnl_reset, | 
 | 405 | 	wimax_gnl_rfkill; | 
 | 406 |  | 
 | 407 | static | 
 | 408 | struct genl_ops *wimax_gnl_ops[] = { | 
 | 409 | 	&wimax_gnl_msg_from_user, | 
 | 410 | 	&wimax_gnl_reset, | 
 | 411 | 	&wimax_gnl_rfkill, | 
 | 412 | }; | 
 | 413 |  | 
 | 414 |  | 
 | 415 | static | 
 | 416 | size_t wimax_addr_scnprint(char *addr_str, size_t addr_str_size, | 
 | 417 | 			   unsigned char *addr, size_t addr_len) | 
 | 418 | { | 
 | 419 | 	unsigned cnt, total; | 
 | 420 | 	for (total = cnt = 0; cnt < addr_len; cnt++) | 
 | 421 | 		total += scnprintf(addr_str + total, addr_str_size - total, | 
 | 422 | 				   "%02x%c", addr[cnt], | 
 | 423 | 				   cnt == addr_len - 1 ? '\0' : ':'); | 
 | 424 | 	return total; | 
 | 425 | } | 
 | 426 |  | 
 | 427 |  | 
 | 428 | /** | 
 | 429 |  * wimax_dev_add - Register a new WiMAX device | 
 | 430 |  * | 
 | 431 |  * @wimax_dev: WiMAX device descriptor (as embedded in your @net_dev's | 
 | 432 |  *     priv data). You must have called wimax_dev_init() on it before. | 
 | 433 |  * | 
 | 434 |  * @net_dev: net device the @wimax_dev is associated with. The | 
 | 435 |  *     function expects SET_NETDEV_DEV() and register_netdev() were | 
 | 436 |  *     already called on it. | 
 | 437 |  * | 
 | 438 |  * Registers the new WiMAX device, sets up the user-kernel control | 
 | 439 |  * interface (generic netlink) and common WiMAX infrastructure. | 
 | 440 |  * | 
 | 441 |  * Note that the parts that will allow interaction with user space are | 
 | 442 |  * setup at the very end, when the rest is in place, as once that | 
 | 443 |  * happens, the driver might get user space control requests via | 
 | 444 |  * netlink or from debugfs that might translate into calls into | 
 | 445 |  * wimax_dev->op_*(). | 
 | 446 |  */ | 
 | 447 | int wimax_dev_add(struct wimax_dev *wimax_dev, struct net_device *net_dev) | 
 | 448 | { | 
 | 449 | 	int result; | 
 | 450 | 	struct device *dev = net_dev->dev.parent; | 
 | 451 | 	char addr_str[32]; | 
 | 452 |  | 
 | 453 | 	d_fnstart(3, dev, "(wimax_dev %p net_dev %p)\n", wimax_dev, net_dev); | 
 | 454 |  | 
 | 455 | 	/* Do the RFKILL setup before locking, as RFKILL will call | 
 | 456 | 	 * into our functions. */ | 
 | 457 | 	wimax_dev->net_dev = net_dev; | 
 | 458 | 	result = wimax_rfkill_add(wimax_dev); | 
 | 459 | 	if (result < 0) | 
 | 460 | 		goto error_rfkill_add; | 
 | 461 |  | 
 | 462 | 	/* Set up user-space interaction */ | 
 | 463 | 	mutex_lock(&wimax_dev->mutex); | 
 | 464 | 	wimax_id_table_add(wimax_dev); | 
 | 465 | 	result = wimax_debugfs_add(wimax_dev); | 
 | 466 | 	if (result < 0) { | 
 | 467 | 		dev_err(dev, "cannot initialize debugfs: %d\n", | 
 | 468 | 			result); | 
 | 469 | 		goto error_debugfs_add; | 
 | 470 | 	} | 
 | 471 |  | 
 | 472 | 	__wimax_state_set(wimax_dev, WIMAX_ST_DOWN); | 
 | 473 | 	mutex_unlock(&wimax_dev->mutex); | 
 | 474 |  | 
 | 475 | 	wimax_addr_scnprint(addr_str, sizeof(addr_str), | 
 | 476 | 			    net_dev->dev_addr, net_dev->addr_len); | 
 | 477 | 	dev_err(dev, "WiMAX interface %s (%s) ready\n", | 
 | 478 | 		net_dev->name, addr_str); | 
 | 479 | 	d_fnend(3, dev, "(wimax_dev %p net_dev %p) = 0\n", wimax_dev, net_dev); | 
 | 480 | 	return 0; | 
 | 481 |  | 
 | 482 | error_debugfs_add: | 
 | 483 | 	wimax_id_table_rm(wimax_dev); | 
 | 484 | 	mutex_unlock(&wimax_dev->mutex); | 
 | 485 | 	wimax_rfkill_rm(wimax_dev); | 
 | 486 | error_rfkill_add: | 
 | 487 | 	d_fnend(3, dev, "(wimax_dev %p net_dev %p) = %d\n", | 
 | 488 | 		wimax_dev, net_dev, result); | 
 | 489 | 	return result; | 
 | 490 | } | 
 | 491 | EXPORT_SYMBOL_GPL(wimax_dev_add); | 
 | 492 |  | 
 | 493 |  | 
 | 494 | /** | 
 | 495 |  * wimax_dev_rm - Unregister an existing WiMAX device | 
 | 496 |  * | 
 | 497 |  * @wimax_dev: WiMAX device descriptor | 
 | 498 |  * | 
 | 499 |  * Unregisters a WiMAX device previously registered for use with | 
 | 500 |  * wimax_add_rm(). | 
 | 501 |  * | 
 | 502 |  * IMPORTANT! Must call before calling unregister_netdev(). | 
 | 503 |  * | 
 | 504 |  * After this function returns, you will not get any more user space | 
 | 505 |  * control requests (via netlink or debugfs) and thus to wimax_dev->ops. | 
 | 506 |  * | 
 | 507 |  * Reentrancy control is ensured by setting the state to | 
 | 508 |  * %__WIMAX_ST_QUIESCING. rfkill operations coming through | 
 | 509 |  * wimax_*rfkill*() will be stopped by the quiescing state; ops coming | 
 | 510 |  * from the rfkill subsystem will be stopped by the support being | 
 | 511 |  * removed by wimax_rfkill_rm(). | 
 | 512 |  */ | 
 | 513 | void wimax_dev_rm(struct wimax_dev *wimax_dev) | 
 | 514 | { | 
 | 515 | 	d_fnstart(3, NULL, "(wimax_dev %p)\n", wimax_dev); | 
 | 516 |  | 
 | 517 | 	mutex_lock(&wimax_dev->mutex); | 
 | 518 | 	__wimax_state_change(wimax_dev, __WIMAX_ST_QUIESCING); | 
 | 519 | 	wimax_debugfs_rm(wimax_dev); | 
 | 520 | 	wimax_id_table_rm(wimax_dev); | 
 | 521 | 	__wimax_state_change(wimax_dev, WIMAX_ST_DOWN); | 
 | 522 | 	mutex_unlock(&wimax_dev->mutex); | 
 | 523 | 	wimax_rfkill_rm(wimax_dev); | 
 | 524 | 	d_fnend(3, NULL, "(wimax_dev %p) = void\n", wimax_dev); | 
 | 525 | } | 
 | 526 | EXPORT_SYMBOL_GPL(wimax_dev_rm); | 
 | 527 |  | 
| Inaky Perez-Gonzalez | 1af7ad5 | 2009-01-29 17:18:31 -0800 | [diff] [blame] | 528 |  | 
 | 529 | /* Debug framework control of debug levels */ | 
 | 530 | struct d_level D_LEVEL[] = { | 
 | 531 | 	D_SUBMODULE_DEFINE(debugfs), | 
 | 532 | 	D_SUBMODULE_DEFINE(id_table), | 
 | 533 | 	D_SUBMODULE_DEFINE(op_msg), | 
 | 534 | 	D_SUBMODULE_DEFINE(op_reset), | 
 | 535 | 	D_SUBMODULE_DEFINE(op_rfkill), | 
 | 536 | 	D_SUBMODULE_DEFINE(stack), | 
 | 537 | }; | 
 | 538 | size_t D_LEVEL_SIZE = ARRAY_SIZE(D_LEVEL); | 
 | 539 |  | 
 | 540 |  | 
| Inaky Perez-Gonzalez | 15530df | 2008-12-20 16:57:36 -0800 | [diff] [blame] | 541 | struct genl_family wimax_gnl_family = { | 
 | 542 | 	.id = GENL_ID_GENERATE, | 
 | 543 | 	.name = "WiMAX", | 
 | 544 | 	.version = WIMAX_GNL_VERSION, | 
 | 545 | 	.hdrsize = 0, | 
 | 546 | 	.maxattr = WIMAX_GNL_ATTR_MAX, | 
 | 547 | }; | 
 | 548 |  | 
 | 549 | struct genl_multicast_group wimax_gnl_mcg = { | 
 | 550 | 	.name = "msg", | 
 | 551 | }; | 
 | 552 |  | 
 | 553 |  | 
 | 554 |  | 
 | 555 | /* Shutdown the wimax stack */ | 
 | 556 | static | 
 | 557 | int __init wimax_subsys_init(void) | 
 | 558 | { | 
 | 559 | 	int result, cnt; | 
 | 560 |  | 
 | 561 | 	d_fnstart(4, NULL, "()\n"); | 
 | 562 | 	snprintf(wimax_gnl_family.name, sizeof(wimax_gnl_family.name), | 
 | 563 | 		 "WiMAX"); | 
 | 564 | 	result = genl_register_family(&wimax_gnl_family); | 
 | 565 | 	if (unlikely(result < 0)) { | 
 | 566 | 		printk(KERN_ERR "cannot register generic netlink family: %d\n", | 
 | 567 | 		       result); | 
 | 568 | 		goto error_register_family; | 
 | 569 | 	} | 
 | 570 |  | 
 | 571 | 	for (cnt = 0; cnt < ARRAY_SIZE(wimax_gnl_ops); cnt++) { | 
 | 572 | 		result = genl_register_ops(&wimax_gnl_family, | 
 | 573 | 					   wimax_gnl_ops[cnt]); | 
 | 574 | 		d_printf(4, NULL, "registering generic netlink op code " | 
 | 575 | 			 "%u: %d\n", wimax_gnl_ops[cnt]->cmd, result); | 
 | 576 | 		if (unlikely(result < 0)) { | 
 | 577 | 			printk(KERN_ERR "cannot register generic netlink op " | 
 | 578 | 			       "code %u: %d\n", | 
 | 579 | 			       wimax_gnl_ops[cnt]->cmd, result); | 
 | 580 | 			goto error_register_ops; | 
 | 581 | 		} | 
 | 582 | 	} | 
 | 583 |  | 
 | 584 | 	result = genl_register_mc_group(&wimax_gnl_family, &wimax_gnl_mcg); | 
 | 585 | 	if (result < 0) | 
 | 586 | 		goto error_mc_group; | 
 | 587 | 	d_fnend(4, NULL, "() = 0\n"); | 
 | 588 | 	return 0; | 
 | 589 |  | 
 | 590 | error_mc_group: | 
 | 591 | error_register_ops: | 
 | 592 | 	for (cnt--; cnt >= 0; cnt--) | 
 | 593 | 		genl_unregister_ops(&wimax_gnl_family, | 
 | 594 | 				    wimax_gnl_ops[cnt]); | 
 | 595 | 	genl_unregister_family(&wimax_gnl_family); | 
 | 596 | error_register_family: | 
 | 597 | 	d_fnend(4, NULL, "() = %d\n", result); | 
 | 598 | 	return result; | 
 | 599 |  | 
 | 600 | } | 
 | 601 | module_init(wimax_subsys_init); | 
 | 602 |  | 
 | 603 |  | 
 | 604 | /* Shutdown the wimax stack */ | 
 | 605 | static | 
 | 606 | void __exit wimax_subsys_exit(void) | 
 | 607 | { | 
 | 608 | 	int cnt; | 
 | 609 | 	wimax_id_table_release(); | 
 | 610 | 	genl_unregister_mc_group(&wimax_gnl_family, &wimax_gnl_mcg); | 
 | 611 | 	for (cnt = ARRAY_SIZE(wimax_gnl_ops) - 1; cnt >= 0; cnt--) | 
 | 612 | 		genl_unregister_ops(&wimax_gnl_family, | 
 | 613 | 				    wimax_gnl_ops[cnt]); | 
 | 614 | 	genl_unregister_family(&wimax_gnl_family); | 
 | 615 | } | 
 | 616 | module_exit(wimax_subsys_exit); | 
 | 617 |  | 
 | 618 | MODULE_AUTHOR("Intel Corporation <linux-wimax@intel.com>"); | 
 | 619 | MODULE_DESCRIPTION("Linux WiMAX stack"); | 
 | 620 | MODULE_LICENSE("GPL"); | 
 | 621 |  |