Commit 024f7f31ed15c471f80408d8b5045497e27e1135

Authored by Inaky Perez-Gonzalez
Committed by Greg Kroah-Hartman
1 parent ea24652d25

i2400m: Generic probe/disconnect, reset and message passing

Implements the generic probe and disconnect functions that will be
called by the USB and SDIO driver's probe/disconnect functions.

Implements the backends for the WiMAX stack's basic operations:
message passing, rfkill control and reset.

Signed-off-by: Inaky Perez-Gonzalez <inaky@linux.intel.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>

Showing 2 changed files with 935 additions and 0 deletions Side-by-side Diff

drivers/net/wimax/i2400m/driver.c
  1 +/*
  2 + * Intel Wireless WiMAX Connection 2400m
  3 + * Generic probe/disconnect, reset and message passing
  4 + *
  5 + *
  6 + * Copyright (C) 2007-2008 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 + * See i2400m.h for driver documentation. This contains helpers for
  25 + * the driver model glue [_setup()/_release()], handling device resets
  26 + * [_dev_reset_handle()], and the backends for the WiMAX stack ops
  27 + * reset [_op_reset()] and message from user [_op_msg_from_user()].
  28 + *
  29 + * ROADMAP:
  30 + *
  31 + * i2400m_op_msg_from_user()
  32 + * i2400m_msg_to_dev()
  33 + * wimax_msg_to_user_send()
  34 + *
  35 + * i2400m_op_reset()
  36 + * i240m->bus_reset()
  37 + *
  38 + * i2400m_dev_reset_handle()
  39 + * __i2400m_dev_reset_handle()
  40 + * __i2400m_dev_stop()
  41 + * __i2400m_dev_start()
  42 + *
  43 + * i2400m_setup()
  44 + * i2400m_bootrom_init()
  45 + * register_netdev()
  46 + * i2400m_dev_start()
  47 + * __i2400m_dev_start()
  48 + * i2400m_dev_bootstrap()
  49 + * i2400m_tx_setup()
  50 + * i2400m->bus_dev_start()
  51 + * i2400m_check_mac_addr()
  52 + * wimax_dev_add()
  53 + *
  54 + * i2400m_release()
  55 + * wimax_dev_rm()
  56 + * i2400m_dev_stop()
  57 + * __i2400m_dev_stop()
  58 + * i2400m_dev_shutdown()
  59 + * i2400m->bus_dev_stop()
  60 + * i2400m_tx_release()
  61 + * unregister_netdev()
  62 + */
  63 +#include "i2400m.h"
  64 +#include <linux/wimax/i2400m.h>
  65 +#include <linux/module.h>
  66 +#include <linux/moduleparam.h>
  67 +
  68 +#define D_SUBMODULE driver
  69 +#include "debug-levels.h"
  70 +
  71 +
  72 +int i2400m_idle_mode_disabled; /* 0 (idle mode enabled) by default */
  73 +module_param_named(idle_mode_disabled, i2400m_idle_mode_disabled, int, 0644);
  74 +MODULE_PARM_DESC(idle_mode_disabled,
  75 + "If true, the device will not enable idle mode negotiation "
  76 + "with the base station (when connected) to save power.");
  77 +
  78 +/**
  79 + * i2400m_queue_work - schedule work on a i2400m's queue
  80 + *
  81 + * @i2400m: device descriptor
  82 + *
  83 + * @fn: function to run to execute work. It gets passed a 'struct
  84 + * work_struct' that is wrapped in a 'struct i2400m_work'. Once
  85 + * done, you have to (1) i2400m_put(i2400m_work->i2400m) and then
  86 + * (2) kfree(i2400m_work).
  87 + *
  88 + * @gfp_flags: GFP flags for memory allocation.
  89 + *
  90 + * @pl: pointer to a payload buffer that you want to pass to the _work
  91 + * function. Use this to pack (for example) a struct with extra
  92 + * arguments.
  93 + *
  94 + * @pl_size: size of the payload buffer.
  95 + *
  96 + * We do this quite often, so this just saves typing; allocate a
  97 + * wrapper for a i2400m, get a ref to it, pack arguments and launch
  98 + * the work.
  99 + *
  100 + * A usual workflow is:
  101 + *
  102 + * struct my_work_args {
  103 + * void *something;
  104 + * int whatever;
  105 + * };
  106 + * ...
  107 + *
  108 + * struct my_work_args my_args = {
  109 + * .something = FOO,
  110 + * .whaetever = BLAH
  111 + * };
  112 + * i2400m_queue_work(i2400m, 1, my_work_function, GFP_KERNEL,
  113 + * &args, sizeof(args))
  114 + *
  115 + * And now the work function can unpack the arguments and call the
  116 + * real function (or do the job itself):
  117 + *
  118 + * static
  119 + * void my_work_fn((struct work_struct *ws)
  120 + * {
  121 + * struct i2400m_work *iw =
  122 + * container_of(ws, struct i2400m_work, ws);
  123 + * struct my_work_args *my_args = (void *) iw->pl;
  124 + *
  125 + * my_work(iw->i2400m, my_args->something, my_args->whatevert);
  126 + * }
  127 + */
  128 +int i2400m_queue_work(struct i2400m *i2400m,
  129 + void (*fn)(struct work_struct *), gfp_t gfp_flags,
  130 + const void *pl, size_t pl_size)
  131 +{
  132 + int result;
  133 + struct i2400m_work *iw;
  134 +
  135 + BUG_ON(i2400m->work_queue == NULL);
  136 + result = -ENOMEM;
  137 + iw = kzalloc(sizeof(*iw) + pl_size, gfp_flags);
  138 + if (iw == NULL)
  139 + goto error_kzalloc;
  140 + iw->i2400m = i2400m_get(i2400m);
  141 + memcpy(iw->pl, pl, pl_size);
  142 + INIT_WORK(&iw->ws, fn);
  143 + result = queue_work(i2400m->work_queue, &iw->ws);
  144 +error_kzalloc:
  145 + return result;
  146 +}
  147 +EXPORT_SYMBOL_GPL(i2400m_queue_work);
  148 +
  149 +
  150 +/*
  151 + * Schedule i2400m's specific work on the system's queue.
  152 + *
  153 + * Used for a few cases where we really need it; otherwise, identical
  154 + * to i2400m_queue_work().
  155 + *
  156 + * Returns < 0 errno code on error, 1 if ok.
  157 + *
  158 + * If it returns zero, something really bad happened, as it means the
  159 + * works struct was already queued, but we have just allocated it, so
  160 + * it should not happen.
  161 + */
  162 +int i2400m_schedule_work(struct i2400m *i2400m,
  163 + void (*fn)(struct work_struct *), gfp_t gfp_flags)
  164 +{
  165 + int result;
  166 + struct i2400m_work *iw;
  167 +
  168 + BUG_ON(i2400m->work_queue == NULL);
  169 + result = -ENOMEM;
  170 + iw = kzalloc(sizeof(*iw), gfp_flags);
  171 + if (iw == NULL)
  172 + goto error_kzalloc;
  173 + iw->i2400m = i2400m_get(i2400m);
  174 + INIT_WORK(&iw->ws, fn);
  175 + result = schedule_work(&iw->ws);
  176 + if (result == 0)
  177 + result = -ENXIO;
  178 +error_kzalloc:
  179 + return result;
  180 +}
  181 +
  182 +
  183 +/*
  184 + * WiMAX stack operation: relay a message from user space
  185 + *
  186 + * @wimax_dev: device descriptor
  187 + * @pipe_name: named pipe the message is for
  188 + * @msg_buf: pointer to the message bytes
  189 + * @msg_len: length of the buffer
  190 + * @genl_info: passed by the generic netlink layer
  191 + *
  192 + * The WiMAX stack will call this function when a message was received
  193 + * from user space.
  194 + *
  195 + * For the i2400m, this is an L3L4 message, as specified in
  196 + * include/linux/wimax/i2400m.h, and thus prefixed with a 'struct
  197 + * i2400m_l3l4_hdr'. Driver (and device) expect the messages to be
  198 + * coded in Little Endian.
  199 + *
  200 + * This function just verifies that the header declaration and the
  201 + * payload are consistent and then deals with it, either forwarding it
  202 + * to the device or procesing it locally.
  203 + *
  204 + * In the i2400m, messages are basically commands that will carry an
  205 + * ack, so we use i2400m_msg_to_dev() and then deliver the ack back to
  206 + * user space. The rx.c code might intercept the response and use it
  207 + * to update the driver's state, but then it will pass it on so it can
  208 + * be relayed back to user space.
  209 + *
  210 + * Note that asynchronous events from the device are processed and
  211 + * sent to user space in rx.c.
  212 + */
  213 +static
  214 +int i2400m_op_msg_from_user(struct wimax_dev *wimax_dev,
  215 + const char *pipe_name,
  216 + const void *msg_buf, size_t msg_len,
  217 + const struct genl_info *genl_info)
  218 +{
  219 + int result;
  220 + struct i2400m *i2400m = wimax_dev_to_i2400m(wimax_dev);
  221 + struct device *dev = i2400m_dev(i2400m);
  222 + struct sk_buff *ack_skb;
  223 +
  224 + d_fnstart(4, dev, "(wimax_dev %p [i2400m %p] msg_buf %p "
  225 + "msg_len %zu genl_info %p)\n", wimax_dev, i2400m,
  226 + msg_buf, msg_len, genl_info);
  227 + ack_skb = i2400m_msg_to_dev(i2400m, msg_buf, msg_len);
  228 + result = PTR_ERR(ack_skb);
  229 + if (IS_ERR(ack_skb))
  230 + goto error_msg_to_dev;
  231 + if (unlikely(i2400m->trace_msg_from_user))
  232 + wimax_msg(&i2400m->wimax_dev, "trace",
  233 + msg_buf, msg_len, GFP_KERNEL);
  234 + result = wimax_msg_send(&i2400m->wimax_dev, ack_skb);
  235 +error_msg_to_dev:
  236 + d_fnend(4, dev, "(wimax_dev %p [i2400m %p] msg_buf %p msg_len %zu "
  237 + "genl_info %p) = %d\n", wimax_dev, i2400m, msg_buf, msg_len,
  238 + genl_info, result);
  239 + return result;
  240 +}
  241 +
  242 +
  243 +/*
  244 + * Context to wait for a reset to finalize
  245 + */
  246 +struct i2400m_reset_ctx {
  247 + struct completion completion;
  248 + int result;
  249 +};
  250 +
  251 +
  252 +/*
  253 + * WiMAX stack operation: reset a device
  254 + *
  255 + * @wimax_dev: device descriptor
  256 + *
  257 + * See the documentation for wimax_reset() and wimax_dev->op_reset for
  258 + * the requirements of this function. The WiMAX stack guarantees
  259 + * serialization on calls to this function.
  260 + *
  261 + * Do a warm reset on the device; if it fails, resort to a cold reset
  262 + * and return -ENODEV. On successful warm reset, we need to block
  263 + * until it is complete.
  264 + *
  265 + * The bus-driver implementation of reset takes care of falling back
  266 + * to cold reset if warm fails.
  267 + */
  268 +static
  269 +int i2400m_op_reset(struct wimax_dev *wimax_dev)
  270 +{
  271 + int result;
  272 + struct i2400m *i2400m = wimax_dev_to_i2400m(wimax_dev);
  273 + struct device *dev = i2400m_dev(i2400m);
  274 + struct i2400m_reset_ctx ctx = {
  275 + .completion = COMPLETION_INITIALIZER_ONSTACK(ctx.completion),
  276 + .result = 0,
  277 + };
  278 +
  279 + d_fnstart(4, dev, "(wimax_dev %p)\n", wimax_dev);
  280 + mutex_lock(&i2400m->init_mutex);
  281 + i2400m->reset_ctx = &ctx;
  282 + mutex_unlock(&i2400m->init_mutex);
  283 + result = i2400m->bus_reset(i2400m, I2400M_RT_WARM);
  284 + if (result < 0)
  285 + goto out;
  286 + result = wait_for_completion_timeout(&ctx.completion, 4*HZ);
  287 + if (result == 0)
  288 + result = -ETIMEDOUT;
  289 + else if (result > 0)
  290 + result = ctx.result;
  291 + /* if result < 0, pass it on */
  292 + mutex_lock(&i2400m->init_mutex);
  293 + i2400m->reset_ctx = NULL;
  294 + mutex_unlock(&i2400m->init_mutex);
  295 +out:
  296 + d_fnend(4, dev, "(wimax_dev %p) = %d\n", wimax_dev, result);
  297 + return result;
  298 +}
  299 +
  300 +
  301 +/*
  302 + * Check the MAC address we got from boot mode is ok
  303 + *
  304 + * @i2400m: device descriptor
  305 + *
  306 + * Returns: 0 if ok, < 0 errno code on error.
  307 + */
  308 +static
  309 +int i2400m_check_mac_addr(struct i2400m *i2400m)
  310 +{
  311 + int result;
  312 + struct device *dev = i2400m_dev(i2400m);
  313 + struct sk_buff *skb;
  314 + const struct i2400m_tlv_detailed_device_info *ddi;
  315 + struct net_device *net_dev = i2400m->wimax_dev.net_dev;
  316 + const unsigned char zeromac[ETH_ALEN] = { 0 };
  317 +
  318 + d_fnstart(3, dev, "(i2400m %p)\n", i2400m);
  319 + skb = i2400m_get_device_info(i2400m);
  320 + if (IS_ERR(skb)) {
  321 + result = PTR_ERR(skb);
  322 + dev_err(dev, "Cannot verify MAC address, error reading: %d\n",
  323 + result);
  324 + goto error;
  325 + }
  326 + /* Extract MAC addresss */
  327 + ddi = (void *) skb->data;
  328 + BUILD_BUG_ON(ETH_ALEN != sizeof(ddi->mac_address));
  329 + d_printf(2, dev, "GET DEVICE INFO: mac addr "
  330 + "%02x:%02x:%02x:%02x:%02x:%02x\n",
  331 + ddi->mac_address[0], ddi->mac_address[1],
  332 + ddi->mac_address[2], ddi->mac_address[3],
  333 + ddi->mac_address[4], ddi->mac_address[5]);
  334 + if (!memcmp(net_dev->perm_addr, ddi->mac_address,
  335 + sizeof(ddi->mac_address)))
  336 + goto ok;
  337 + dev_warn(dev, "warning: device reports a different MAC address "
  338 + "to that of boot mode's\n");
  339 + dev_warn(dev, "device reports %02x:%02x:%02x:%02x:%02x:%02x\n",
  340 + ddi->mac_address[0], ddi->mac_address[1],
  341 + ddi->mac_address[2], ddi->mac_address[3],
  342 + ddi->mac_address[4], ddi->mac_address[5]);
  343 + dev_warn(dev, "boot mode reported %02x:%02x:%02x:%02x:%02x:%02x\n",
  344 + net_dev->perm_addr[0], net_dev->perm_addr[1],
  345 + net_dev->perm_addr[2], net_dev->perm_addr[3],
  346 + net_dev->perm_addr[4], net_dev->perm_addr[5]);
  347 + if (!memcmp(zeromac, ddi->mac_address, sizeof(zeromac)))
  348 + dev_err(dev, "device reports an invalid MAC address, "
  349 + "not updating\n");
  350 + else {
  351 + dev_warn(dev, "updating MAC address\n");
  352 + net_dev->addr_len = ETH_ALEN;
  353 + memcpy(net_dev->perm_addr, ddi->mac_address, ETH_ALEN);
  354 + memcpy(net_dev->dev_addr, ddi->mac_address, ETH_ALEN);
  355 + }
  356 +ok:
  357 + result = 0;
  358 + kfree_skb(skb);
  359 +error:
  360 + d_fnend(3, dev, "(i2400m %p) = %d\n", i2400m, result);
  361 + return result;
  362 +}
  363 +
  364 +
  365 +/**
  366 + * __i2400m_dev_start - Bring up driver communication with the device
  367 + *
  368 + * @i2400m: device descriptor
  369 + * @flags: boot mode flags
  370 + *
  371 + * Returns: 0 if ok, < 0 errno code on error.
  372 + *
  373 + * Uploads firmware and brings up all the resources needed to be able
  374 + * to communicate with the device.
  375 + *
  376 + * TX needs to be setup before the bus-specific code (otherwise on
  377 + * shutdown, the bus-tx code could try to access it).
  378 + */
  379 +static
  380 +int __i2400m_dev_start(struct i2400m *i2400m, enum i2400m_bri flags)
  381 +{
  382 + int result;
  383 + struct wimax_dev *wimax_dev = &i2400m->wimax_dev;
  384 + struct net_device *net_dev = wimax_dev->net_dev;
  385 + struct device *dev = i2400m_dev(i2400m);
  386 + int times = 3;
  387 +
  388 + d_fnstart(3, dev, "(i2400m %p)\n", i2400m);
  389 +retry:
  390 + result = i2400m_dev_bootstrap(i2400m, flags);
  391 + if (result < 0) {
  392 + dev_err(dev, "cannot bootstrap device: %d\n", result);
  393 + goto error_bootstrap;
  394 + }
  395 + result = i2400m_tx_setup(i2400m);
  396 + if (result < 0)
  397 + goto error_tx_setup;
  398 + result = i2400m->bus_dev_start(i2400m);
  399 + if (result < 0)
  400 + goto error_bus_dev_start;
  401 + i2400m->work_queue = create_singlethread_workqueue(wimax_dev->name);
  402 + if (i2400m->work_queue == NULL) {
  403 + result = -ENOMEM;
  404 + dev_err(dev, "cannot create workqueue\n");
  405 + goto error_create_workqueue;
  406 + }
  407 + /* At this point is ok to send commands to the device */
  408 + result = i2400m_check_mac_addr(i2400m);
  409 + if (result < 0)
  410 + goto error_check_mac_addr;
  411 + i2400m->ready = 1;
  412 + wimax_state_change(wimax_dev, WIMAX_ST_UNINITIALIZED);
  413 + result = i2400m_dev_initialize(i2400m);
  414 + if (result < 0)
  415 + goto error_dev_initialize;
  416 + /* At this point, reports will come for the device and set it
  417 + * to the right state if it is different than UNINITIALIZED */
  418 + d_fnend(3, dev, "(net_dev %p [i2400m %p]) = %d\n",
  419 + net_dev, i2400m, result);
  420 + return result;
  421 +
  422 +error_dev_initialize:
  423 +error_check_mac_addr:
  424 + destroy_workqueue(i2400m->work_queue);
  425 +error_create_workqueue:
  426 + i2400m->bus_dev_stop(i2400m);
  427 +error_bus_dev_start:
  428 + i2400m_tx_release(i2400m);
  429 +error_tx_setup:
  430 +error_bootstrap:
  431 + if (result == -ERESTARTSYS && times-- > 0) {
  432 + flags = I2400M_BRI_SOFT;
  433 + goto retry;
  434 + }
  435 + d_fnend(3, dev, "(net_dev %p [i2400m %p]) = %d\n",
  436 + net_dev, i2400m, result);
  437 + return result;
  438 +}
  439 +
  440 +
  441 +static
  442 +int i2400m_dev_start(struct i2400m *i2400m, enum i2400m_bri bm_flags)
  443 +{
  444 + int result;
  445 + mutex_lock(&i2400m->init_mutex); /* Well, start the device */
  446 + result = __i2400m_dev_start(i2400m, bm_flags);
  447 + if (result >= 0)
  448 + i2400m->updown = 1;
  449 + mutex_unlock(&i2400m->init_mutex);
  450 + return result;
  451 +}
  452 +
  453 +
  454 +/**
  455 + * i2400m_dev_stop - Tear down driver communication with the device
  456 + *
  457 + * @i2400m: device descriptor
  458 + *
  459 + * Returns: 0 if ok, < 0 errno code on error.
  460 + *
  461 + * Releases all the resources allocated to communicate with the device.
  462 + */
  463 +static
  464 +void __i2400m_dev_stop(struct i2400m *i2400m)
  465 +{
  466 + struct wimax_dev *wimax_dev = &i2400m->wimax_dev;
  467 + struct device *dev = i2400m_dev(i2400m);
  468 +
  469 + d_fnstart(3, dev, "(i2400m %p)\n", i2400m);
  470 + wimax_state_change(wimax_dev, __WIMAX_ST_QUIESCING);
  471 + i2400m_dev_shutdown(i2400m);
  472 + i2400m->ready = 0;
  473 + destroy_workqueue(i2400m->work_queue);
  474 + i2400m->bus_dev_stop(i2400m);
  475 + i2400m_tx_release(i2400m);
  476 + wimax_state_change(wimax_dev, WIMAX_ST_DOWN);
  477 + d_fnend(3, dev, "(i2400m %p) = 0\n", i2400m);
  478 +}
  479 +
  480 +
  481 +/*
  482 + * Watch out -- we only need to stop if there is a need for it. The
  483 + * device could have reset itself and failed to come up again (see
  484 + * _i2400m_dev_reset_handle()).
  485 + */
  486 +static
  487 +void i2400m_dev_stop(struct i2400m *i2400m)
  488 +{
  489 + mutex_lock(&i2400m->init_mutex);
  490 + if (i2400m->updown) {
  491 + __i2400m_dev_stop(i2400m);
  492 + i2400m->updown = 0;
  493 + }
  494 + mutex_unlock(&i2400m->init_mutex);
  495 +}
  496 +
  497 +
  498 +/*
  499 + * The device has rebooted; fix up the device and the driver
  500 + *
  501 + * Tear down the driver communication with the device, reload the
  502 + * firmware and reinitialize the communication with the device.
  503 + *
  504 + * If someone calls a reset when the device's firmware is down, in
  505 + * theory we won't see it because we are not listening. However, just
  506 + * in case, leave the code to handle it.
  507 + *
  508 + * If there is a reset context, use it; this means someone is waiting
  509 + * for us to tell him when the reset operation is complete and the
  510 + * device is ready to rock again.
  511 + *
  512 + * NOTE: if we are in the process of bringing up or down the
  513 + * communication with the device [running i2400m_dev_start() or
  514 + * _stop()], don't do anything, let it fail and handle it.
  515 + *
  516 + * This function is ran always in a thread context
  517 + */
  518 +static
  519 +void __i2400m_dev_reset_handle(struct work_struct *ws)
  520 +{
  521 + int result;
  522 + struct i2400m_work *iw = container_of(ws, struct i2400m_work, ws);
  523 + struct i2400m *i2400m = iw->i2400m;
  524 + struct device *dev = i2400m_dev(i2400m);
  525 + enum wimax_st wimax_state;
  526 + struct i2400m_reset_ctx *ctx = i2400m->reset_ctx;
  527 +
  528 + d_fnstart(3, dev, "(ws %p i2400m %p)\n", ws, i2400m);
  529 + result = 0;
  530 + if (mutex_trylock(&i2400m->init_mutex) == 0) {
  531 + /* We are still in i2400m_dev_start() [let it fail] or
  532 + * i2400m_dev_stop() [we are shutting down anyway, so
  533 + * ignore it] or we are resetting somewhere else. */
  534 + dev_err(dev, "device rebooted\n");
  535 + i2400m_msg_to_dev_cancel_wait(i2400m, -ERESTARTSYS);
  536 + complete(&i2400m->msg_completion);
  537 + goto out;
  538 + }
  539 + wimax_state = wimax_state_get(&i2400m->wimax_dev);
  540 + if (wimax_state < WIMAX_ST_UNINITIALIZED) {
  541 + dev_info(dev, "device rebooted: it is down, ignoring\n");
  542 + goto out_unlock; /* ifconfig up/down wasn't called */
  543 + }
  544 + dev_err(dev, "device rebooted: reinitializing driver\n");
  545 + __i2400m_dev_stop(i2400m);
  546 + i2400m->updown = 0;
  547 + result = __i2400m_dev_start(i2400m,
  548 + I2400M_BRI_SOFT | I2400M_BRI_MAC_REINIT);
  549 + if (result < 0) {
  550 + dev_err(dev, "device reboot: cannot start the device: %d\n",
  551 + result);
  552 + result = i2400m->bus_reset(i2400m, I2400M_RT_BUS);
  553 + if (result >= 0)
  554 + result = -ENODEV;
  555 + } else
  556 + i2400m->updown = 1;
  557 +out_unlock:
  558 + if (i2400m->reset_ctx) {
  559 + ctx->result = result;
  560 + complete(&ctx->completion);
  561 + }
  562 + mutex_unlock(&i2400m->init_mutex);
  563 +out:
  564 + i2400m_put(i2400m);
  565 + kfree(iw);
  566 + d_fnend(3, dev, "(ws %p i2400m %p) = void\n", ws, i2400m);
  567 + return;
  568 +}
  569 +
  570 +
  571 +/**
  572 + * i2400m_dev_reset_handle - Handle a device's reset in a thread context
  573 + *
  574 + * Schedule a device reset handling out on a thread context, so it
  575 + * is safe to call from atomic context. We can't use the i2400m's
  576 + * queue as we are going to destroy it and reinitialize it as part of
  577 + * the driver bringup/bringup process.
  578 + *
  579 + * See __i2400m_dev_reset_handle() for details; that takes care of
  580 + * reinitializing the driver to handle the reset, calling into the
  581 + * bus-specific functions ops as needed.
  582 + */
  583 +int i2400m_dev_reset_handle(struct i2400m *i2400m)
  584 +{
  585 + return i2400m_schedule_work(i2400m, __i2400m_dev_reset_handle,
  586 + GFP_ATOMIC);
  587 +}
  588 +EXPORT_SYMBOL_GPL(i2400m_dev_reset_handle);
  589 +
  590 +
  591 +/**
  592 + * i2400m_setup - bus-generic setup function for the i2400m device
  593 + *
  594 + * @i2400m: device descriptor (bus-specific parts have been initialized)
  595 + *
  596 + * Returns: 0 if ok, < 0 errno code on error.
  597 + *
  598 + * Initializes the bus-generic parts of the i2400m driver; the
  599 + * bus-specific parts have been initialized, function pointers filled
  600 + * out by the bus-specific probe function.
  601 + *
  602 + * As well, this registers the WiMAX and net device nodes. Once this
  603 + * function returns, the device is operative and has to be ready to
  604 + * receive and send network traffic and WiMAX control operations.
  605 + */
  606 +int i2400m_setup(struct i2400m *i2400m, enum i2400m_bri bm_flags)
  607 +{
  608 + int result = -ENODEV;
  609 + struct device *dev = i2400m_dev(i2400m);
  610 + struct wimax_dev *wimax_dev = &i2400m->wimax_dev;
  611 + struct net_device *net_dev = i2400m->wimax_dev.net_dev;
  612 +
  613 + d_fnstart(3, dev, "(i2400m %p)\n", i2400m);
  614 +
  615 + snprintf(wimax_dev->name, sizeof(wimax_dev->name),
  616 + "i2400m-%s:%s", dev->bus->name, dev->bus_id);
  617 +
  618 + i2400m->bm_cmd_buf = kzalloc(I2400M_BM_CMD_BUF_SIZE, GFP_KERNEL);
  619 + if (i2400m->bm_cmd_buf == NULL) {
  620 + dev_err(dev, "cannot allocate USB command buffer\n");
  621 + goto error_bm_cmd_kzalloc;
  622 + }
  623 + i2400m->bm_ack_buf = kzalloc(I2400M_BM_ACK_BUF_SIZE, GFP_KERNEL);
  624 + if (i2400m->bm_ack_buf == NULL) {
  625 + dev_err(dev, "cannot allocate USB ack buffer\n");
  626 + goto error_bm_ack_buf_kzalloc;
  627 + }
  628 + result = i2400m_bootrom_init(i2400m, bm_flags);
  629 + if (result < 0) {
  630 + dev_err(dev, "read mac addr: bootrom init "
  631 + "failed: %d\n", result);
  632 + goto error_bootrom_init;
  633 + }
  634 + result = i2400m_read_mac_addr(i2400m);
  635 + if (result < 0)
  636 + goto error_read_mac_addr;
  637 +
  638 + result = register_netdev(net_dev); /* Okey dokey, bring it up */
  639 + if (result < 0) {
  640 + dev_err(dev, "cannot register i2400m network device: %d\n",
  641 + result);
  642 + goto error_register_netdev;
  643 + }
  644 + netif_carrier_off(net_dev);
  645 +
  646 + result = i2400m_dev_start(i2400m, bm_flags);
  647 + if (result < 0)
  648 + goto error_dev_start;
  649 +
  650 + i2400m->wimax_dev.op_msg_from_user = i2400m_op_msg_from_user;
  651 + i2400m->wimax_dev.op_rfkill_sw_toggle = i2400m_op_rfkill_sw_toggle;
  652 + i2400m->wimax_dev.op_reset = i2400m_op_reset;
  653 + result = wimax_dev_add(&i2400m->wimax_dev, net_dev);
  654 + if (result < 0)
  655 + goto error_wimax_dev_add;
  656 + /* User space needs to do some init stuff */
  657 + wimax_state_change(wimax_dev, WIMAX_ST_UNINITIALIZED);
  658 +
  659 + /* Now setup all that requires a registered net and wimax device. */
  660 + result = i2400m_debugfs_add(i2400m);
  661 + if (result < 0) {
  662 + dev_err(dev, "cannot setup i2400m's debugfs: %d\n", result);
  663 + goto error_debugfs_setup;
  664 + }
  665 + d_fnend(3, dev, "(i2400m %p) = %d\n", i2400m, result);
  666 + return result;
  667 +
  668 +error_debugfs_setup:
  669 + wimax_dev_rm(&i2400m->wimax_dev);
  670 +error_wimax_dev_add:
  671 + i2400m_dev_stop(i2400m);
  672 +error_dev_start:
  673 + unregister_netdev(net_dev);
  674 +error_register_netdev:
  675 +error_read_mac_addr:
  676 +error_bootrom_init:
  677 + kfree(i2400m->bm_ack_buf);
  678 +error_bm_ack_buf_kzalloc:
  679 + kfree(i2400m->bm_cmd_buf);
  680 +error_bm_cmd_kzalloc:
  681 + d_fnend(3, dev, "(i2400m %p) = %d\n", i2400m, result);
  682 + return result;
  683 +}
  684 +EXPORT_SYMBOL_GPL(i2400m_setup);
  685 +
  686 +
  687 +/**
  688 + * i2400m_release - release the bus-generic driver resources
  689 + *
  690 + * Sends a disconnect message and undoes any setup done by i2400m_setup()
  691 + */
  692 +void i2400m_release(struct i2400m *i2400m)
  693 +{
  694 + struct device *dev = i2400m_dev(i2400m);
  695 +
  696 + d_fnstart(3, dev, "(i2400m %p)\n", i2400m);
  697 + netif_stop_queue(i2400m->wimax_dev.net_dev);
  698 +
  699 + i2400m_debugfs_rm(i2400m);
  700 + wimax_dev_rm(&i2400m->wimax_dev);
  701 + i2400m_dev_stop(i2400m);
  702 + unregister_netdev(i2400m->wimax_dev.net_dev);
  703 + kfree(i2400m->bm_ack_buf);
  704 + kfree(i2400m->bm_cmd_buf);
  705 + d_fnend(3, dev, "(i2400m %p) = void\n", i2400m);
  706 +}
  707 +EXPORT_SYMBOL_GPL(i2400m_release);
  708 +
  709 +
  710 +static
  711 +int __init i2400m_driver_init(void)
  712 +{
  713 + return 0;
  714 +}
  715 +module_init(i2400m_driver_init);
  716 +
  717 +static
  718 +void __exit i2400m_driver_exit(void)
  719 +{
  720 + /* for scheds i2400m_dev_reset_handle() */
  721 + flush_scheduled_work();
  722 + return;
  723 +}
  724 +module_exit(i2400m_driver_exit);
  725 +
  726 +MODULE_AUTHOR("Intel Corporation <linux-wimax@intel.com>");
  727 +MODULE_DESCRIPTION("Intel 2400M WiMAX networking bus-generic driver");
  728 +MODULE_LICENSE("GPL");
drivers/net/wimax/i2400m/op-rfkill.c
  1 +/*
  2 + * Intel Wireless WiMAX Connection 2400m
  3 + * Implement backend for the WiMAX stack rfkill support
  4 + *
  5 + *
  6 + * Copyright (C) 2007-2008 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 + * The WiMAX kernel stack integrates into RF-Kill and keeps the
  25 + * switches's status. We just need to:
  26 + *
  27 + * - report changes in the HW RF Kill switch [with
  28 + * wimax_rfkill_{sw,hw}_report(), which happens when we detect those
  29 + * indications coming through hardware reports]. We also do it on
  30 + * initialization to let the stack know the intial HW state.
  31 + *
  32 + * - implement indications from the stack to change the SW RF Kill
  33 + * switch (coming from sysfs, the wimax stack or user space).
  34 + */
  35 +#include "i2400m.h"
  36 +#include <linux/wimax/i2400m.h>
  37 +
  38 +
  39 +
  40 +#define D_SUBMODULE rfkill
  41 +#include "debug-levels.h"
  42 +
  43 +/*
  44 + * Return true if the i2400m radio is in the requested wimax_rf_state state
  45 + *
  46 + */
  47 +static
  48 +int i2400m_radio_is(struct i2400m *i2400m, enum wimax_rf_state state)
  49 +{
  50 + if (state == WIMAX_RF_OFF)
  51 + return i2400m->state == I2400M_SS_RF_OFF
  52 + || i2400m->state == I2400M_SS_RF_SHUTDOWN;
  53 + else if (state == WIMAX_RF_ON)
  54 + /* state == WIMAX_RF_ON */
  55 + return i2400m->state != I2400M_SS_RF_OFF
  56 + && i2400m->state != I2400M_SS_RF_SHUTDOWN;
  57 + else
  58 + BUG();
  59 +}
  60 +
  61 +
  62 +/*
  63 + * WiMAX stack operation: implement SW RFKill toggling
  64 + *
  65 + * @wimax_dev: device descriptor
  66 + * @skb: skb where the message has been received; skb->data is
  67 + * expected to point to the message payload.
  68 + * @genl_info: passed by the generic netlink layer
  69 + *
  70 + * Generic Netlink will call this function when a message is sent from
  71 + * userspace to change the software RF-Kill switch status.
  72 + *
  73 + * This function will set the device's sofware RF-Kill switch state to
  74 + * match what is requested.
  75 + *
  76 + * NOTE: the i2400m has a strict state machine; we can only set the
  77 + * RF-Kill switch when it is on, the HW RF-Kill is on and the
  78 + * device is initialized. So we ignore errors steaming from not
  79 + * being in the right state (-EILSEQ).
  80 + */
  81 +int i2400m_op_rfkill_sw_toggle(struct wimax_dev *wimax_dev,
  82 + enum wimax_rf_state state)
  83 +{
  84 + int result;
  85 + struct i2400m *i2400m = wimax_dev_to_i2400m(wimax_dev);
  86 + struct device *dev = i2400m_dev(i2400m);
  87 + struct sk_buff *ack_skb;
  88 + struct {
  89 + struct i2400m_l3l4_hdr hdr;
  90 + struct i2400m_tlv_rf_operation sw_rf;
  91 + } __attribute__((packed)) *cmd;
  92 + char strerr[32];
  93 +
  94 + d_fnstart(4, dev, "(wimax_dev %p state %d)\n", wimax_dev, state);
  95 +
  96 + result = -ENOMEM;
  97 + cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  98 + if (cmd == NULL)
  99 + goto error_alloc;
  100 + cmd->hdr.type = cpu_to_le16(I2400M_MT_CMD_RF_CONTROL);
  101 + cmd->hdr.length = sizeof(cmd->sw_rf);
  102 + cmd->hdr.version = cpu_to_le16(I2400M_L3L4_VERSION);
  103 + cmd->sw_rf.hdr.type = cpu_to_le16(I2400M_TLV_RF_OPERATION);
  104 + cmd->sw_rf.hdr.length = cpu_to_le16(sizeof(cmd->sw_rf.status));
  105 + switch (state) {
  106 + case WIMAX_RF_OFF: /* RFKILL ON, radio OFF */
  107 + cmd->sw_rf.status = cpu_to_le32(2);
  108 + break;
  109 + case WIMAX_RF_ON: /* RFKILL OFF, radio ON */
  110 + cmd->sw_rf.status = cpu_to_le32(1);
  111 + break;
  112 + default:
  113 + BUG();
  114 + }
  115 +
  116 + ack_skb = i2400m_msg_to_dev(i2400m, cmd, sizeof(*cmd));
  117 + result = PTR_ERR(ack_skb);
  118 + if (IS_ERR(ack_skb)) {
  119 + dev_err(dev, "Failed to issue 'RF Control' command: %d\n",
  120 + result);
  121 + goto error_msg_to_dev;
  122 + }
  123 + result = i2400m_msg_check_status(wimax_msg_data(ack_skb),
  124 + strerr, sizeof(strerr));
  125 + if (result < 0) {
  126 + dev_err(dev, "'RF Control' (0x%04x) command failed: %d - %s\n",
  127 + I2400M_MT_CMD_RF_CONTROL, result, strerr);
  128 + goto error_cmd;
  129 + }
  130 +
  131 + /* Now we wait for the state to change to RADIO_OFF or RADIO_ON */
  132 + result = wait_event_timeout(
  133 + i2400m->state_wq, i2400m_radio_is(i2400m, state),
  134 + 5 * HZ);
  135 + if (result == 0)
  136 + result = -ETIMEDOUT;
  137 + if (result < 0)
  138 + dev_err(dev, "Error waiting for device to toggle RF state: "
  139 + "%d\n", result);
  140 + result = 0;
  141 +error_cmd:
  142 + kfree_skb(ack_skb);
  143 +error_msg_to_dev:
  144 +error_alloc:
  145 + d_fnend(4, dev, "(wimax_dev %p state %d) = %d\n",
  146 + wimax_dev, state, result);
  147 + return result;
  148 +}
  149 +
  150 +
  151 +/*
  152 + * Inform the WiMAX stack of changes in the RF Kill switches reported
  153 + * by the device
  154 + *
  155 + * @i2400m: device descriptor
  156 + * @rfss: TLV for RF Switches status; already validated
  157 + *
  158 + * NOTE: the reports on RF switch status cannot be trusted
  159 + * or used until the device is in a state of RADIO_OFF
  160 + * or greater.
  161 + */
  162 +void i2400m_report_tlv_rf_switches_status(
  163 + struct i2400m *i2400m,
  164 + const struct i2400m_tlv_rf_switches_status *rfss)
  165 +{
  166 + struct device *dev = i2400m_dev(i2400m);
  167 + enum i2400m_rf_switch_status hw, sw;
  168 + enum wimax_st wimax_state;
  169 +
  170 + sw = le32_to_cpu(rfss->sw_rf_switch);
  171 + hw = le32_to_cpu(rfss->hw_rf_switch);
  172 +
  173 + d_fnstart(3, dev, "(i2400m %p rfss %p [hw %u sw %u])\n",
  174 + i2400m, rfss, hw, sw);
  175 + /* We only process rw switch evens when the device has been
  176 + * fully initialized */
  177 + wimax_state = wimax_state_get(&i2400m->wimax_dev);
  178 + if (wimax_state < WIMAX_ST_RADIO_OFF) {
  179 + d_printf(3, dev, "ignoring RF switches report, state %u\n",
  180 + wimax_state);
  181 + goto out;
  182 + }
  183 + switch (sw) {
  184 + case I2400M_RF_SWITCH_ON: /* RF Kill disabled (radio on) */
  185 + wimax_report_rfkill_sw(&i2400m->wimax_dev, WIMAX_RF_ON);
  186 + break;
  187 + case I2400M_RF_SWITCH_OFF: /* RF Kill enabled (radio off) */
  188 + wimax_report_rfkill_sw(&i2400m->wimax_dev, WIMAX_RF_OFF);
  189 + break;
  190 + default:
  191 + dev_err(dev, "HW BUG? Unknown RF SW state 0x%x\n", sw);
  192 + }
  193 +
  194 + switch (hw) {
  195 + case I2400M_RF_SWITCH_ON: /* RF Kill disabled (radio on) */
  196 + wimax_report_rfkill_hw(&i2400m->wimax_dev, WIMAX_RF_ON);
  197 + break;
  198 + case I2400M_RF_SWITCH_OFF: /* RF Kill enabled (radio off) */
  199 + wimax_report_rfkill_hw(&i2400m->wimax_dev, WIMAX_RF_OFF);
  200 + break;
  201 + default:
  202 + dev_err(dev, "HW BUG? Unknown RF HW state 0x%x\n", hw);
  203 + }
  204 +out:
  205 + d_fnend(3, dev, "(i2400m %p rfss %p [hw %u sw %u]) = void\n",
  206 + i2400m, rfss, hw, sw);
  207 +}