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