blob: 006eb1233a8b3af658128f524d12fb3354849672 [file] [log] [blame]
Inaky Perez-Gonzalez024f7f32008-12-20 16:57:44 -08001/*
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()
Inaky Perez-Gonzalez6a0f7ab2009-02-28 23:42:49 +000051 * i2400m_firmware_check()
Inaky Perez-Gonzalez024f7f32008-12-20 16:57:44 -080052 * i2400m_check_mac_addr()
53 * wimax_dev_add()
54 *
55 * i2400m_release()
56 * wimax_dev_rm()
57 * i2400m_dev_stop()
58 * __i2400m_dev_stop()
59 * i2400m_dev_shutdown()
60 * i2400m->bus_dev_stop()
61 * i2400m_tx_release()
62 * unregister_netdev()
63 */
64#include "i2400m.h"
Inaky Perez-Gonzalezfe442682009-04-22 16:53:08 -070065#include <linux/etherdevice.h>
Inaky Perez-Gonzalez024f7f32008-12-20 16:57:44 -080066#include <linux/wimax/i2400m.h>
67#include <linux/module.h>
68#include <linux/moduleparam.h>
69
70#define D_SUBMODULE driver
71#include "debug-levels.h"
72
73
74int i2400m_idle_mode_disabled; /* 0 (idle mode enabled) by default */
75module_param_named(idle_mode_disabled, i2400m_idle_mode_disabled, int, 0644);
76MODULE_PARM_DESC(idle_mode_disabled,
77 "If true, the device will not enable idle mode negotiation "
78 "with the base station (when connected) to save power.");
79
Inaky Perez-Gonzalezc7475832009-02-28 23:42:54 +000080int i2400m_rx_reorder_disabled; /* 0 (rx reorder enabled) by default */
81module_param_named(rx_reorder_disabled, i2400m_rx_reorder_disabled, int, 0644);
82MODULE_PARM_DESC(rx_reorder_disabled,
83 "If true, RX reordering will be disabled.");
84
Inaky Perez-Gonzalezfb101672009-05-07 10:27:42 -070085int i2400m_power_save_disabled; /* 0 (power saving enabled) by default */
86module_param_named(power_save_disabled, i2400m_power_save_disabled, int, 0644);
87MODULE_PARM_DESC(power_save_disabled,
88 "If true, the driver will not tell the device to enter "
89 "power saving mode when it reports it is ready for it. "
90 "False by default (so the device is told to do power "
91 "saving).");
92
Inaky Perez-Gonzalez024f7f32008-12-20 16:57:44 -080093/**
94 * i2400m_queue_work - schedule work on a i2400m's queue
95 *
96 * @i2400m: device descriptor
97 *
98 * @fn: function to run to execute work. It gets passed a 'struct
99 * work_struct' that is wrapped in a 'struct i2400m_work'. Once
100 * done, you have to (1) i2400m_put(i2400m_work->i2400m) and then
101 * (2) kfree(i2400m_work).
102 *
103 * @gfp_flags: GFP flags for memory allocation.
104 *
105 * @pl: pointer to a payload buffer that you want to pass to the _work
106 * function. Use this to pack (for example) a struct with extra
107 * arguments.
108 *
109 * @pl_size: size of the payload buffer.
110 *
111 * We do this quite often, so this just saves typing; allocate a
112 * wrapper for a i2400m, get a ref to it, pack arguments and launch
113 * the work.
114 *
115 * A usual workflow is:
116 *
117 * struct my_work_args {
118 * void *something;
119 * int whatever;
120 * };
121 * ...
122 *
123 * struct my_work_args my_args = {
124 * .something = FOO,
125 * .whaetever = BLAH
126 * };
127 * i2400m_queue_work(i2400m, 1, my_work_function, GFP_KERNEL,
128 * &args, sizeof(args))
129 *
130 * And now the work function can unpack the arguments and call the
131 * real function (or do the job itself):
132 *
133 * static
134 * void my_work_fn((struct work_struct *ws)
135 * {
136 * struct i2400m_work *iw =
137 * container_of(ws, struct i2400m_work, ws);
138 * struct my_work_args *my_args = (void *) iw->pl;
139 *
140 * my_work(iw->i2400m, my_args->something, my_args->whatevert);
141 * }
142 */
143int i2400m_queue_work(struct i2400m *i2400m,
144 void (*fn)(struct work_struct *), gfp_t gfp_flags,
145 const void *pl, size_t pl_size)
146{
147 int result;
148 struct i2400m_work *iw;
149
150 BUG_ON(i2400m->work_queue == NULL);
151 result = -ENOMEM;
152 iw = kzalloc(sizeof(*iw) + pl_size, gfp_flags);
153 if (iw == NULL)
154 goto error_kzalloc;
155 iw->i2400m = i2400m_get(i2400m);
156 memcpy(iw->pl, pl, pl_size);
157 INIT_WORK(&iw->ws, fn);
158 result = queue_work(i2400m->work_queue, &iw->ws);
159error_kzalloc:
160 return result;
161}
162EXPORT_SYMBOL_GPL(i2400m_queue_work);
163
164
165/*
166 * Schedule i2400m's specific work on the system's queue.
167 *
168 * Used for a few cases where we really need it; otherwise, identical
169 * to i2400m_queue_work().
170 *
171 * Returns < 0 errno code on error, 1 if ok.
172 *
173 * If it returns zero, something really bad happened, as it means the
174 * works struct was already queued, but we have just allocated it, so
175 * it should not happen.
176 */
177int i2400m_schedule_work(struct i2400m *i2400m,
178 void (*fn)(struct work_struct *), gfp_t gfp_flags)
179{
180 int result;
181 struct i2400m_work *iw;
182
183 BUG_ON(i2400m->work_queue == NULL);
184 result = -ENOMEM;
185 iw = kzalloc(sizeof(*iw), gfp_flags);
186 if (iw == NULL)
187 goto error_kzalloc;
188 iw->i2400m = i2400m_get(i2400m);
189 INIT_WORK(&iw->ws, fn);
190 result = schedule_work(&iw->ws);
191 if (result == 0)
192 result = -ENXIO;
193error_kzalloc:
194 return result;
195}
196
197
198/*
199 * WiMAX stack operation: relay a message from user space
200 *
201 * @wimax_dev: device descriptor
202 * @pipe_name: named pipe the message is for
203 * @msg_buf: pointer to the message bytes
204 * @msg_len: length of the buffer
205 * @genl_info: passed by the generic netlink layer
206 *
207 * The WiMAX stack will call this function when a message was received
208 * from user space.
209 *
210 * For the i2400m, this is an L3L4 message, as specified in
211 * include/linux/wimax/i2400m.h, and thus prefixed with a 'struct
212 * i2400m_l3l4_hdr'. Driver (and device) expect the messages to be
213 * coded in Little Endian.
214 *
215 * This function just verifies that the header declaration and the
216 * payload are consistent and then deals with it, either forwarding it
217 * to the device or procesing it locally.
218 *
219 * In the i2400m, messages are basically commands that will carry an
220 * ack, so we use i2400m_msg_to_dev() and then deliver the ack back to
221 * user space. The rx.c code might intercept the response and use it
222 * to update the driver's state, but then it will pass it on so it can
223 * be relayed back to user space.
224 *
225 * Note that asynchronous events from the device are processed and
226 * sent to user space in rx.c.
227 */
228static
229int i2400m_op_msg_from_user(struct wimax_dev *wimax_dev,
230 const char *pipe_name,
231 const void *msg_buf, size_t msg_len,
232 const struct genl_info *genl_info)
233{
234 int result;
235 struct i2400m *i2400m = wimax_dev_to_i2400m(wimax_dev);
236 struct device *dev = i2400m_dev(i2400m);
237 struct sk_buff *ack_skb;
238
239 d_fnstart(4, dev, "(wimax_dev %p [i2400m %p] msg_buf %p "
240 "msg_len %zu genl_info %p)\n", wimax_dev, i2400m,
241 msg_buf, msg_len, genl_info);
242 ack_skb = i2400m_msg_to_dev(i2400m, msg_buf, msg_len);
243 result = PTR_ERR(ack_skb);
244 if (IS_ERR(ack_skb))
245 goto error_msg_to_dev;
Inaky Perez-Gonzalez024f7f32008-12-20 16:57:44 -0800246 result = wimax_msg_send(&i2400m->wimax_dev, ack_skb);
247error_msg_to_dev:
248 d_fnend(4, dev, "(wimax_dev %p [i2400m %p] msg_buf %p msg_len %zu "
249 "genl_info %p) = %d\n", wimax_dev, i2400m, msg_buf, msg_len,
250 genl_info, result);
251 return result;
252}
253
254
255/*
256 * Context to wait for a reset to finalize
257 */
258struct i2400m_reset_ctx {
259 struct completion completion;
260 int result;
261};
262
263
264/*
265 * WiMAX stack operation: reset a device
266 *
267 * @wimax_dev: device descriptor
268 *
269 * See the documentation for wimax_reset() and wimax_dev->op_reset for
270 * the requirements of this function. The WiMAX stack guarantees
271 * serialization on calls to this function.
272 *
273 * Do a warm reset on the device; if it fails, resort to a cold reset
274 * and return -ENODEV. On successful warm reset, we need to block
275 * until it is complete.
276 *
277 * The bus-driver implementation of reset takes care of falling back
278 * to cold reset if warm fails.
279 */
280static
281int i2400m_op_reset(struct wimax_dev *wimax_dev)
282{
283 int result;
284 struct i2400m *i2400m = wimax_dev_to_i2400m(wimax_dev);
285 struct device *dev = i2400m_dev(i2400m);
286 struct i2400m_reset_ctx ctx = {
287 .completion = COMPLETION_INITIALIZER_ONSTACK(ctx.completion),
288 .result = 0,
289 };
290
291 d_fnstart(4, dev, "(wimax_dev %p)\n", wimax_dev);
292 mutex_lock(&i2400m->init_mutex);
293 i2400m->reset_ctx = &ctx;
294 mutex_unlock(&i2400m->init_mutex);
295 result = i2400m->bus_reset(i2400m, I2400M_RT_WARM);
296 if (result < 0)
297 goto out;
298 result = wait_for_completion_timeout(&ctx.completion, 4*HZ);
299 if (result == 0)
300 result = -ETIMEDOUT;
301 else if (result > 0)
302 result = ctx.result;
303 /* if result < 0, pass it on */
304 mutex_lock(&i2400m->init_mutex);
305 i2400m->reset_ctx = NULL;
306 mutex_unlock(&i2400m->init_mutex);
307out:
308 d_fnend(4, dev, "(wimax_dev %p) = %d\n", wimax_dev, result);
309 return result;
310}
311
312
313/*
314 * Check the MAC address we got from boot mode is ok
315 *
316 * @i2400m: device descriptor
317 *
318 * Returns: 0 if ok, < 0 errno code on error.
319 */
320static
321int i2400m_check_mac_addr(struct i2400m *i2400m)
322{
323 int result;
324 struct device *dev = i2400m_dev(i2400m);
325 struct sk_buff *skb;
326 const struct i2400m_tlv_detailed_device_info *ddi;
327 struct net_device *net_dev = i2400m->wimax_dev.net_dev;
328 const unsigned char zeromac[ETH_ALEN] = { 0 };
329
330 d_fnstart(3, dev, "(i2400m %p)\n", i2400m);
331 skb = i2400m_get_device_info(i2400m);
332 if (IS_ERR(skb)) {
333 result = PTR_ERR(skb);
334 dev_err(dev, "Cannot verify MAC address, error reading: %d\n",
335 result);
336 goto error;
337 }
338 /* Extract MAC addresss */
339 ddi = (void *) skb->data;
340 BUILD_BUG_ON(ETH_ALEN != sizeof(ddi->mac_address));
341 d_printf(2, dev, "GET DEVICE INFO: mac addr "
342 "%02x:%02x:%02x:%02x:%02x:%02x\n",
343 ddi->mac_address[0], ddi->mac_address[1],
344 ddi->mac_address[2], ddi->mac_address[3],
345 ddi->mac_address[4], ddi->mac_address[5]);
346 if (!memcmp(net_dev->perm_addr, ddi->mac_address,
347 sizeof(ddi->mac_address)))
348 goto ok;
349 dev_warn(dev, "warning: device reports a different MAC address "
350 "to that of boot mode's\n");
351 dev_warn(dev, "device reports %02x:%02x:%02x:%02x:%02x:%02x\n",
352 ddi->mac_address[0], ddi->mac_address[1],
353 ddi->mac_address[2], ddi->mac_address[3],
354 ddi->mac_address[4], ddi->mac_address[5]);
355 dev_warn(dev, "boot mode reported %02x:%02x:%02x:%02x:%02x:%02x\n",
356 net_dev->perm_addr[0], net_dev->perm_addr[1],
357 net_dev->perm_addr[2], net_dev->perm_addr[3],
358 net_dev->perm_addr[4], net_dev->perm_addr[5]);
359 if (!memcmp(zeromac, ddi->mac_address, sizeof(zeromac)))
360 dev_err(dev, "device reports an invalid MAC address, "
361 "not updating\n");
362 else {
363 dev_warn(dev, "updating MAC address\n");
364 net_dev->addr_len = ETH_ALEN;
365 memcpy(net_dev->perm_addr, ddi->mac_address, ETH_ALEN);
366 memcpy(net_dev->dev_addr, ddi->mac_address, ETH_ALEN);
367 }
368ok:
369 result = 0;
370 kfree_skb(skb);
371error:
372 d_fnend(3, dev, "(i2400m %p) = %d\n", i2400m, result);
373 return result;
374}
375
376
377/**
378 * __i2400m_dev_start - Bring up driver communication with the device
379 *
380 * @i2400m: device descriptor
381 * @flags: boot mode flags
382 *
383 * Returns: 0 if ok, < 0 errno code on error.
384 *
385 * Uploads firmware and brings up all the resources needed to be able
386 * to communicate with the device.
387 *
Inaky Perez-Gonzaleze9a6b452009-05-08 13:02:41 -0700388 * The workqueue has to be setup early, at least before RX handling
389 * (it's only real user for now) so it can process reports as they
390 * arrive. We also want to destroy it if we retry, to make sure it is
391 * flushed...easier like this.
392 *
Inaky Perez-Gonzalez024f7f32008-12-20 16:57:44 -0800393 * TX needs to be setup before the bus-specific code (otherwise on
394 * shutdown, the bus-tx code could try to access it).
395 */
396static
397int __i2400m_dev_start(struct i2400m *i2400m, enum i2400m_bri flags)
398{
399 int result;
400 struct wimax_dev *wimax_dev = &i2400m->wimax_dev;
401 struct net_device *net_dev = wimax_dev->net_dev;
402 struct device *dev = i2400m_dev(i2400m);
403 int times = 3;
404
405 d_fnstart(3, dev, "(i2400m %p)\n", i2400m);
406retry:
407 result = i2400m_dev_bootstrap(i2400m, flags);
408 if (result < 0) {
409 dev_err(dev, "cannot bootstrap device: %d\n", result);
410 goto error_bootstrap;
411 }
412 result = i2400m_tx_setup(i2400m);
413 if (result < 0)
414 goto error_tx_setup;
Inaky Perez-Gonzalezc7475832009-02-28 23:42:54 +0000415 result = i2400m_rx_setup(i2400m);
416 if (result < 0)
417 goto error_rx_setup;
Inaky Perez-Gonzalez024f7f32008-12-20 16:57:44 -0800418 i2400m->work_queue = create_singlethread_workqueue(wimax_dev->name);
419 if (i2400m->work_queue == NULL) {
420 result = -ENOMEM;
421 dev_err(dev, "cannot create workqueue\n");
422 goto error_create_workqueue;
423 }
Inaky Perez-Gonzaleze9a6b452009-05-08 13:02:41 -0700424 result = i2400m->bus_dev_start(i2400m);
425 if (result < 0)
426 goto error_bus_dev_start;
Inaky Perez-Gonzalez6a0f7ab2009-02-28 23:42:49 +0000427 result = i2400m_firmware_check(i2400m); /* fw versions ok? */
428 if (result < 0)
429 goto error_fw_check;
Inaky Perez-Gonzalez024f7f32008-12-20 16:57:44 -0800430 /* At this point is ok to send commands to the device */
431 result = i2400m_check_mac_addr(i2400m);
432 if (result < 0)
433 goto error_check_mac_addr;
434 i2400m->ready = 1;
435 wimax_state_change(wimax_dev, WIMAX_ST_UNINITIALIZED);
436 result = i2400m_dev_initialize(i2400m);
437 if (result < 0)
438 goto error_dev_initialize;
439 /* At this point, reports will come for the device and set it
440 * to the right state if it is different than UNINITIALIZED */
441 d_fnend(3, dev, "(net_dev %p [i2400m %p]) = %d\n",
442 net_dev, i2400m, result);
443 return result;
444
445error_dev_initialize:
446error_check_mac_addr:
Inaky Perez-Gonzalez6a0f7ab2009-02-28 23:42:49 +0000447error_fw_check:
Inaky Perez-Gonzalez024f7f32008-12-20 16:57:44 -0800448 i2400m->bus_dev_stop(i2400m);
449error_bus_dev_start:
Inaky Perez-Gonzaleze9a6b452009-05-08 13:02:41 -0700450 destroy_workqueue(i2400m->work_queue);
451error_create_workqueue:
Inaky Perez-Gonzalezc7475832009-02-28 23:42:54 +0000452 i2400m_rx_release(i2400m);
453error_rx_setup:
Inaky Perez-Gonzalez024f7f32008-12-20 16:57:44 -0800454 i2400m_tx_release(i2400m);
455error_tx_setup:
456error_bootstrap:
457 if (result == -ERESTARTSYS && times-- > 0) {
458 flags = I2400M_BRI_SOFT;
459 goto retry;
460 }
461 d_fnend(3, dev, "(net_dev %p [i2400m %p]) = %d\n",
462 net_dev, i2400m, result);
463 return result;
464}
465
466
467static
468int i2400m_dev_start(struct i2400m *i2400m, enum i2400m_bri bm_flags)
469{
470 int result;
471 mutex_lock(&i2400m->init_mutex); /* Well, start the device */
472 result = __i2400m_dev_start(i2400m, bm_flags);
473 if (result >= 0)
474 i2400m->updown = 1;
475 mutex_unlock(&i2400m->init_mutex);
476 return result;
477}
478
479
480/**
481 * i2400m_dev_stop - Tear down driver communication with the device
482 *
483 * @i2400m: device descriptor
484 *
485 * Returns: 0 if ok, < 0 errno code on error.
486 *
Inaky Perez-Gonzaleze9a6b452009-05-08 13:02:41 -0700487 * Releases all the resources allocated to communicate with the
488 * device. Note we cannot destroy the workqueue earlier as until RX is
489 * fully destroyed, it could still try to schedule jobs.
Inaky Perez-Gonzalez024f7f32008-12-20 16:57:44 -0800490 */
491static
492void __i2400m_dev_stop(struct i2400m *i2400m)
493{
494 struct wimax_dev *wimax_dev = &i2400m->wimax_dev;
495 struct device *dev = i2400m_dev(i2400m);
496
497 d_fnstart(3, dev, "(i2400m %p)\n", i2400m);
498 wimax_state_change(wimax_dev, __WIMAX_ST_QUIESCING);
499 i2400m_dev_shutdown(i2400m);
500 i2400m->ready = 0;
Inaky Perez-Gonzalez024f7f32008-12-20 16:57:44 -0800501 i2400m->bus_dev_stop(i2400m);
Inaky Perez-Gonzaleze9a6b452009-05-08 13:02:41 -0700502 destroy_workqueue(i2400m->work_queue);
Inaky Perez-Gonzalezc7475832009-02-28 23:42:54 +0000503 i2400m_rx_release(i2400m);
Inaky Perez-Gonzalez024f7f32008-12-20 16:57:44 -0800504 i2400m_tx_release(i2400m);
505 wimax_state_change(wimax_dev, WIMAX_ST_DOWN);
506 d_fnend(3, dev, "(i2400m %p) = 0\n", i2400m);
507}
508
509
510/*
511 * Watch out -- we only need to stop if there is a need for it. The
512 * device could have reset itself and failed to come up again (see
513 * _i2400m_dev_reset_handle()).
514 */
515static
516void i2400m_dev_stop(struct i2400m *i2400m)
517{
518 mutex_lock(&i2400m->init_mutex);
519 if (i2400m->updown) {
520 __i2400m_dev_stop(i2400m);
521 i2400m->updown = 0;
522 }
523 mutex_unlock(&i2400m->init_mutex);
524}
525
526
527/*
528 * The device has rebooted; fix up the device and the driver
529 *
530 * Tear down the driver communication with the device, reload the
531 * firmware and reinitialize the communication with the device.
532 *
533 * If someone calls a reset when the device's firmware is down, in
534 * theory we won't see it because we are not listening. However, just
535 * in case, leave the code to handle it.
536 *
537 * If there is a reset context, use it; this means someone is waiting
538 * for us to tell him when the reset operation is complete and the
539 * device is ready to rock again.
540 *
541 * NOTE: if we are in the process of bringing up or down the
542 * communication with the device [running i2400m_dev_start() or
543 * _stop()], don't do anything, let it fail and handle it.
544 *
545 * This function is ran always in a thread context
546 */
547static
548void __i2400m_dev_reset_handle(struct work_struct *ws)
549{
550 int result;
551 struct i2400m_work *iw = container_of(ws, struct i2400m_work, ws);
552 struct i2400m *i2400m = iw->i2400m;
553 struct device *dev = i2400m_dev(i2400m);
554 enum wimax_st wimax_state;
555 struct i2400m_reset_ctx *ctx = i2400m->reset_ctx;
556
557 d_fnstart(3, dev, "(ws %p i2400m %p)\n", ws, i2400m);
558 result = 0;
559 if (mutex_trylock(&i2400m->init_mutex) == 0) {
560 /* We are still in i2400m_dev_start() [let it fail] or
561 * i2400m_dev_stop() [we are shutting down anyway, so
562 * ignore it] or we are resetting somewhere else. */
563 dev_err(dev, "device rebooted\n");
564 i2400m_msg_to_dev_cancel_wait(i2400m, -ERESTARTSYS);
565 complete(&i2400m->msg_completion);
566 goto out;
567 }
568 wimax_state = wimax_state_get(&i2400m->wimax_dev);
569 if (wimax_state < WIMAX_ST_UNINITIALIZED) {
570 dev_info(dev, "device rebooted: it is down, ignoring\n");
571 goto out_unlock; /* ifconfig up/down wasn't called */
572 }
573 dev_err(dev, "device rebooted: reinitializing driver\n");
574 __i2400m_dev_stop(i2400m);
575 i2400m->updown = 0;
576 result = __i2400m_dev_start(i2400m,
577 I2400M_BRI_SOFT | I2400M_BRI_MAC_REINIT);
578 if (result < 0) {
579 dev_err(dev, "device reboot: cannot start the device: %d\n",
580 result);
581 result = i2400m->bus_reset(i2400m, I2400M_RT_BUS);
582 if (result >= 0)
583 result = -ENODEV;
584 } else
585 i2400m->updown = 1;
586out_unlock:
587 if (i2400m->reset_ctx) {
588 ctx->result = result;
589 complete(&ctx->completion);
590 }
591 mutex_unlock(&i2400m->init_mutex);
592out:
593 i2400m_put(i2400m);
594 kfree(iw);
595 d_fnend(3, dev, "(ws %p i2400m %p) = void\n", ws, i2400m);
596 return;
597}
598
599
600/**
601 * i2400m_dev_reset_handle - Handle a device's reset in a thread context
602 *
603 * Schedule a device reset handling out on a thread context, so it
604 * is safe to call from atomic context. We can't use the i2400m's
605 * queue as we are going to destroy it and reinitialize it as part of
606 * the driver bringup/bringup process.
607 *
608 * See __i2400m_dev_reset_handle() for details; that takes care of
609 * reinitializing the driver to handle the reset, calling into the
610 * bus-specific functions ops as needed.
611 */
612int i2400m_dev_reset_handle(struct i2400m *i2400m)
613{
614 return i2400m_schedule_work(i2400m, __i2400m_dev_reset_handle,
615 GFP_ATOMIC);
616}
617EXPORT_SYMBOL_GPL(i2400m_dev_reset_handle);
618
619
620/**
621 * i2400m_setup - bus-generic setup function for the i2400m device
622 *
623 * @i2400m: device descriptor (bus-specific parts have been initialized)
624 *
625 * Returns: 0 if ok, < 0 errno code on error.
626 *
627 * Initializes the bus-generic parts of the i2400m driver; the
628 * bus-specific parts have been initialized, function pointers filled
629 * out by the bus-specific probe function.
630 *
631 * As well, this registers the WiMAX and net device nodes. Once this
632 * function returns, the device is operative and has to be ready to
633 * receive and send network traffic and WiMAX control operations.
634 */
635int i2400m_setup(struct i2400m *i2400m, enum i2400m_bri bm_flags)
636{
637 int result = -ENODEV;
638 struct device *dev = i2400m_dev(i2400m);
639 struct wimax_dev *wimax_dev = &i2400m->wimax_dev;
640 struct net_device *net_dev = i2400m->wimax_dev.net_dev;
641
642 d_fnstart(3, dev, "(i2400m %p)\n", i2400m);
643
644 snprintf(wimax_dev->name, sizeof(wimax_dev->name),
Kay Sievers347707b2009-02-28 23:42:51 +0000645 "i2400m-%s:%s", dev->bus->name, dev_name(dev));
Inaky Perez-Gonzalez024f7f32008-12-20 16:57:44 -0800646
647 i2400m->bm_cmd_buf = kzalloc(I2400M_BM_CMD_BUF_SIZE, GFP_KERNEL);
648 if (i2400m->bm_cmd_buf == NULL) {
649 dev_err(dev, "cannot allocate USB command buffer\n");
650 goto error_bm_cmd_kzalloc;
651 }
652 i2400m->bm_ack_buf = kzalloc(I2400M_BM_ACK_BUF_SIZE, GFP_KERNEL);
653 if (i2400m->bm_ack_buf == NULL) {
654 dev_err(dev, "cannot allocate USB ack buffer\n");
655 goto error_bm_ack_buf_kzalloc;
656 }
657 result = i2400m_bootrom_init(i2400m, bm_flags);
658 if (result < 0) {
659 dev_err(dev, "read mac addr: bootrom init "
660 "failed: %d\n", result);
661 goto error_bootrom_init;
662 }
663 result = i2400m_read_mac_addr(i2400m);
664 if (result < 0)
665 goto error_read_mac_addr;
Inaky Perez-Gonzalezfe442682009-04-22 16:53:08 -0700666 random_ether_addr(i2400m->src_mac_addr);
Inaky Perez-Gonzalez024f7f32008-12-20 16:57:44 -0800667
668 result = register_netdev(net_dev); /* Okey dokey, bring it up */
669 if (result < 0) {
670 dev_err(dev, "cannot register i2400m network device: %d\n",
671 result);
672 goto error_register_netdev;
673 }
674 netif_carrier_off(net_dev);
675
676 result = i2400m_dev_start(i2400m, bm_flags);
677 if (result < 0)
678 goto error_dev_start;
679
680 i2400m->wimax_dev.op_msg_from_user = i2400m_op_msg_from_user;
681 i2400m->wimax_dev.op_rfkill_sw_toggle = i2400m_op_rfkill_sw_toggle;
682 i2400m->wimax_dev.op_reset = i2400m_op_reset;
683 result = wimax_dev_add(&i2400m->wimax_dev, net_dev);
684 if (result < 0)
685 goto error_wimax_dev_add;
686 /* User space needs to do some init stuff */
687 wimax_state_change(wimax_dev, WIMAX_ST_UNINITIALIZED);
688
689 /* Now setup all that requires a registered net and wimax device. */
Inaky Perez-Gonzalez89876912009-02-28 23:42:50 +0000690 result = sysfs_create_group(&net_dev->dev.kobj, &i2400m_dev_attr_group);
691 if (result < 0) {
692 dev_err(dev, "cannot setup i2400m's sysfs: %d\n", result);
693 goto error_sysfs_setup;
694 }
Inaky Perez-Gonzalez024f7f32008-12-20 16:57:44 -0800695 result = i2400m_debugfs_add(i2400m);
696 if (result < 0) {
697 dev_err(dev, "cannot setup i2400m's debugfs: %d\n", result);
698 goto error_debugfs_setup;
699 }
700 d_fnend(3, dev, "(i2400m %p) = %d\n", i2400m, result);
701 return result;
702
703error_debugfs_setup:
Inaky Perez-Gonzalez89876912009-02-28 23:42:50 +0000704 sysfs_remove_group(&i2400m->wimax_dev.net_dev->dev.kobj,
705 &i2400m_dev_attr_group);
706error_sysfs_setup:
Inaky Perez-Gonzalez024f7f32008-12-20 16:57:44 -0800707 wimax_dev_rm(&i2400m->wimax_dev);
708error_wimax_dev_add:
709 i2400m_dev_stop(i2400m);
710error_dev_start:
711 unregister_netdev(net_dev);
712error_register_netdev:
713error_read_mac_addr:
714error_bootrom_init:
715 kfree(i2400m->bm_ack_buf);
716error_bm_ack_buf_kzalloc:
717 kfree(i2400m->bm_cmd_buf);
718error_bm_cmd_kzalloc:
719 d_fnend(3, dev, "(i2400m %p) = %d\n", i2400m, result);
720 return result;
721}
722EXPORT_SYMBOL_GPL(i2400m_setup);
723
724
725/**
726 * i2400m_release - release the bus-generic driver resources
727 *
728 * Sends a disconnect message and undoes any setup done by i2400m_setup()
729 */
730void i2400m_release(struct i2400m *i2400m)
731{
732 struct device *dev = i2400m_dev(i2400m);
733
734 d_fnstart(3, dev, "(i2400m %p)\n", i2400m);
735 netif_stop_queue(i2400m->wimax_dev.net_dev);
736
737 i2400m_debugfs_rm(i2400m);
Inaky Perez-Gonzalez89876912009-02-28 23:42:50 +0000738 sysfs_remove_group(&i2400m->wimax_dev.net_dev->dev.kobj,
739 &i2400m_dev_attr_group);
Inaky Perez-Gonzalez024f7f32008-12-20 16:57:44 -0800740 wimax_dev_rm(&i2400m->wimax_dev);
741 i2400m_dev_stop(i2400m);
742 unregister_netdev(i2400m->wimax_dev.net_dev);
743 kfree(i2400m->bm_ack_buf);
744 kfree(i2400m->bm_cmd_buf);
745 d_fnend(3, dev, "(i2400m %p) = void\n", i2400m);
746}
747EXPORT_SYMBOL_GPL(i2400m_release);
748
749
Inaky Perez-Gonzalez1af7ad52009-01-29 17:18:31 -0800750/*
751 * Debug levels control; see debug.h
752 */
753struct d_level D_LEVEL[] = {
754 D_SUBMODULE_DEFINE(control),
755 D_SUBMODULE_DEFINE(driver),
756 D_SUBMODULE_DEFINE(debugfs),
757 D_SUBMODULE_DEFINE(fw),
758 D_SUBMODULE_DEFINE(netdev),
759 D_SUBMODULE_DEFINE(rfkill),
760 D_SUBMODULE_DEFINE(rx),
761 D_SUBMODULE_DEFINE(tx),
762};
763size_t D_LEVEL_SIZE = ARRAY_SIZE(D_LEVEL);
764
765
Inaky Perez-Gonzalez024f7f32008-12-20 16:57:44 -0800766static
767int __init i2400m_driver_init(void)
768{
769 return 0;
770}
771module_init(i2400m_driver_init);
772
773static
774void __exit i2400m_driver_exit(void)
775{
776 /* for scheds i2400m_dev_reset_handle() */
777 flush_scheduled_work();
778 return;
779}
780module_exit(i2400m_driver_exit);
781
782MODULE_AUTHOR("Intel Corporation <linux-wimax@intel.com>");
783MODULE_DESCRIPTION("Intel 2400M WiMAX networking bus-generic driver");
784MODULE_LICENSE("GPL");