blob: a21318b34bf23fd219fec6b06c2f9049077bdfc3 [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;
238 if (unlikely(i2400m->trace_msg_from_user))
239 wimax_msg(&i2400m->wimax_dev, "trace",
240 msg_buf, msg_len, GFP_KERNEL);
241 result = wimax_msg_send(&i2400m->wimax_dev, ack_skb);
242error_msg_to_dev:
243 d_fnend(4, dev, "(wimax_dev %p [i2400m %p] msg_buf %p msg_len %zu "
244 "genl_info %p) = %d\n", wimax_dev, i2400m, msg_buf, msg_len,
245 genl_info, result);
246 return result;
247}
248
249
250/*
251 * Context to wait for a reset to finalize
252 */
253struct i2400m_reset_ctx {
254 struct completion completion;
255 int result;
256};
257
258
259/*
260 * WiMAX stack operation: reset a device
261 *
262 * @wimax_dev: device descriptor
263 *
264 * See the documentation for wimax_reset() and wimax_dev->op_reset for
265 * the requirements of this function. The WiMAX stack guarantees
266 * serialization on calls to this function.
267 *
268 * Do a warm reset on the device; if it fails, resort to a cold reset
269 * and return -ENODEV. On successful warm reset, we need to block
270 * until it is complete.
271 *
272 * The bus-driver implementation of reset takes care of falling back
273 * to cold reset if warm fails.
274 */
275static
276int i2400m_op_reset(struct wimax_dev *wimax_dev)
277{
278 int result;
279 struct i2400m *i2400m = wimax_dev_to_i2400m(wimax_dev);
280 struct device *dev = i2400m_dev(i2400m);
281 struct i2400m_reset_ctx ctx = {
282 .completion = COMPLETION_INITIALIZER_ONSTACK(ctx.completion),
283 .result = 0,
284 };
285
286 d_fnstart(4, dev, "(wimax_dev %p)\n", wimax_dev);
287 mutex_lock(&i2400m->init_mutex);
288 i2400m->reset_ctx = &ctx;
289 mutex_unlock(&i2400m->init_mutex);
290 result = i2400m->bus_reset(i2400m, I2400M_RT_WARM);
291 if (result < 0)
292 goto out;
293 result = wait_for_completion_timeout(&ctx.completion, 4*HZ);
294 if (result == 0)
295 result = -ETIMEDOUT;
296 else if (result > 0)
297 result = ctx.result;
298 /* if result < 0, pass it on */
299 mutex_lock(&i2400m->init_mutex);
300 i2400m->reset_ctx = NULL;
301 mutex_unlock(&i2400m->init_mutex);
302out:
303 d_fnend(4, dev, "(wimax_dev %p) = %d\n", wimax_dev, result);
304 return result;
305}
306
307
308/*
309 * Check the MAC address we got from boot mode is ok
310 *
311 * @i2400m: device descriptor
312 *
313 * Returns: 0 if ok, < 0 errno code on error.
314 */
315static
316int i2400m_check_mac_addr(struct i2400m *i2400m)
317{
318 int result;
319 struct device *dev = i2400m_dev(i2400m);
320 struct sk_buff *skb;
321 const struct i2400m_tlv_detailed_device_info *ddi;
322 struct net_device *net_dev = i2400m->wimax_dev.net_dev;
323 const unsigned char zeromac[ETH_ALEN] = { 0 };
324
325 d_fnstart(3, dev, "(i2400m %p)\n", i2400m);
326 skb = i2400m_get_device_info(i2400m);
327 if (IS_ERR(skb)) {
328 result = PTR_ERR(skb);
329 dev_err(dev, "Cannot verify MAC address, error reading: %d\n",
330 result);
331 goto error;
332 }
333 /* Extract MAC addresss */
334 ddi = (void *) skb->data;
335 BUILD_BUG_ON(ETH_ALEN != sizeof(ddi->mac_address));
336 d_printf(2, dev, "GET DEVICE INFO: mac addr "
337 "%02x:%02x:%02x:%02x:%02x:%02x\n",
338 ddi->mac_address[0], ddi->mac_address[1],
339 ddi->mac_address[2], ddi->mac_address[3],
340 ddi->mac_address[4], ddi->mac_address[5]);
341 if (!memcmp(net_dev->perm_addr, ddi->mac_address,
342 sizeof(ddi->mac_address)))
343 goto ok;
344 dev_warn(dev, "warning: device reports a different MAC address "
345 "to that of boot mode's\n");
346 dev_warn(dev, "device reports %02x:%02x:%02x:%02x:%02x:%02x\n",
347 ddi->mac_address[0], ddi->mac_address[1],
348 ddi->mac_address[2], ddi->mac_address[3],
349 ddi->mac_address[4], ddi->mac_address[5]);
350 dev_warn(dev, "boot mode reported %02x:%02x:%02x:%02x:%02x:%02x\n",
351 net_dev->perm_addr[0], net_dev->perm_addr[1],
352 net_dev->perm_addr[2], net_dev->perm_addr[3],
353 net_dev->perm_addr[4], net_dev->perm_addr[5]);
354 if (!memcmp(zeromac, ddi->mac_address, sizeof(zeromac)))
355 dev_err(dev, "device reports an invalid MAC address, "
356 "not updating\n");
357 else {
358 dev_warn(dev, "updating MAC address\n");
359 net_dev->addr_len = ETH_ALEN;
360 memcpy(net_dev->perm_addr, ddi->mac_address, ETH_ALEN);
361 memcpy(net_dev->dev_addr, ddi->mac_address, ETH_ALEN);
362 }
363ok:
364 result = 0;
365 kfree_skb(skb);
366error:
367 d_fnend(3, dev, "(i2400m %p) = %d\n", i2400m, result);
368 return result;
369}
370
371
372/**
373 * __i2400m_dev_start - Bring up driver communication with the device
374 *
375 * @i2400m: device descriptor
376 * @flags: boot mode flags
377 *
378 * Returns: 0 if ok, < 0 errno code on error.
379 *
380 * Uploads firmware and brings up all the resources needed to be able
381 * to communicate with the device.
382 *
383 * TX needs to be setup before the bus-specific code (otherwise on
384 * shutdown, the bus-tx code could try to access it).
385 */
386static
387int __i2400m_dev_start(struct i2400m *i2400m, enum i2400m_bri flags)
388{
389 int result;
390 struct wimax_dev *wimax_dev = &i2400m->wimax_dev;
391 struct net_device *net_dev = wimax_dev->net_dev;
392 struct device *dev = i2400m_dev(i2400m);
393 int times = 3;
394
395 d_fnstart(3, dev, "(i2400m %p)\n", i2400m);
396retry:
397 result = i2400m_dev_bootstrap(i2400m, flags);
398 if (result < 0) {
399 dev_err(dev, "cannot bootstrap device: %d\n", result);
400 goto error_bootstrap;
401 }
402 result = i2400m_tx_setup(i2400m);
403 if (result < 0)
404 goto error_tx_setup;
Inaky Perez-Gonzalezc7475832009-02-28 23:42:54 +0000405 result = i2400m_rx_setup(i2400m);
406 if (result < 0)
407 goto error_rx_setup;
Inaky Perez-Gonzalez024f7f32008-12-20 16:57:44 -0800408 result = i2400m->bus_dev_start(i2400m);
409 if (result < 0)
410 goto error_bus_dev_start;
411 i2400m->work_queue = create_singlethread_workqueue(wimax_dev->name);
412 if (i2400m->work_queue == NULL) {
413 result = -ENOMEM;
414 dev_err(dev, "cannot create workqueue\n");
415 goto error_create_workqueue;
416 }
Inaky Perez-Gonzalez6a0f7ab2009-02-28 23:42:49 +0000417 result = i2400m_firmware_check(i2400m); /* fw versions ok? */
418 if (result < 0)
419 goto error_fw_check;
Inaky Perez-Gonzalez024f7f32008-12-20 16:57:44 -0800420 /* At this point is ok to send commands to the device */
421 result = i2400m_check_mac_addr(i2400m);
422 if (result < 0)
423 goto error_check_mac_addr;
424 i2400m->ready = 1;
425 wimax_state_change(wimax_dev, WIMAX_ST_UNINITIALIZED);
426 result = i2400m_dev_initialize(i2400m);
427 if (result < 0)
428 goto error_dev_initialize;
429 /* At this point, reports will come for the device and set it
430 * to the right state if it is different than UNINITIALIZED */
431 d_fnend(3, dev, "(net_dev %p [i2400m %p]) = %d\n",
432 net_dev, i2400m, result);
433 return result;
434
435error_dev_initialize:
436error_check_mac_addr:
Inaky Perez-Gonzalez6a0f7ab2009-02-28 23:42:49 +0000437error_fw_check:
Inaky Perez-Gonzalez024f7f32008-12-20 16:57:44 -0800438 destroy_workqueue(i2400m->work_queue);
439error_create_workqueue:
440 i2400m->bus_dev_stop(i2400m);
441error_bus_dev_start:
Inaky Perez-Gonzalezc7475832009-02-28 23:42:54 +0000442 i2400m_rx_release(i2400m);
443error_rx_setup:
Inaky Perez-Gonzalez024f7f32008-12-20 16:57:44 -0800444 i2400m_tx_release(i2400m);
445error_tx_setup:
446error_bootstrap:
447 if (result == -ERESTARTSYS && times-- > 0) {
448 flags = I2400M_BRI_SOFT;
449 goto retry;
450 }
451 d_fnend(3, dev, "(net_dev %p [i2400m %p]) = %d\n",
452 net_dev, i2400m, result);
453 return result;
454}
455
456
457static
458int i2400m_dev_start(struct i2400m *i2400m, enum i2400m_bri bm_flags)
459{
460 int result;
461 mutex_lock(&i2400m->init_mutex); /* Well, start the device */
462 result = __i2400m_dev_start(i2400m, bm_flags);
463 if (result >= 0)
464 i2400m->updown = 1;
465 mutex_unlock(&i2400m->init_mutex);
466 return result;
467}
468
469
470/**
471 * i2400m_dev_stop - Tear down driver communication with the device
472 *
473 * @i2400m: device descriptor
474 *
475 * Returns: 0 if ok, < 0 errno code on error.
476 *
477 * Releases all the resources allocated to communicate with the device.
478 */
479static
480void __i2400m_dev_stop(struct i2400m *i2400m)
481{
482 struct wimax_dev *wimax_dev = &i2400m->wimax_dev;
483 struct device *dev = i2400m_dev(i2400m);
484
485 d_fnstart(3, dev, "(i2400m %p)\n", i2400m);
486 wimax_state_change(wimax_dev, __WIMAX_ST_QUIESCING);
487 i2400m_dev_shutdown(i2400m);
488 i2400m->ready = 0;
489 destroy_workqueue(i2400m->work_queue);
490 i2400m->bus_dev_stop(i2400m);
Inaky Perez-Gonzalezc7475832009-02-28 23:42:54 +0000491 i2400m_rx_release(i2400m);
Inaky Perez-Gonzalez024f7f32008-12-20 16:57:44 -0800492 i2400m_tx_release(i2400m);
493 wimax_state_change(wimax_dev, WIMAX_ST_DOWN);
494 d_fnend(3, dev, "(i2400m %p) = 0\n", i2400m);
495}
496
497
498/*
499 * Watch out -- we only need to stop if there is a need for it. The
500 * device could have reset itself and failed to come up again (see
501 * _i2400m_dev_reset_handle()).
502 */
503static
504void i2400m_dev_stop(struct i2400m *i2400m)
505{
506 mutex_lock(&i2400m->init_mutex);
507 if (i2400m->updown) {
508 __i2400m_dev_stop(i2400m);
509 i2400m->updown = 0;
510 }
511 mutex_unlock(&i2400m->init_mutex);
512}
513
514
515/*
516 * The device has rebooted; fix up the device and the driver
517 *
518 * Tear down the driver communication with the device, reload the
519 * firmware and reinitialize the communication with the device.
520 *
521 * If someone calls a reset when the device's firmware is down, in
522 * theory we won't see it because we are not listening. However, just
523 * in case, leave the code to handle it.
524 *
525 * If there is a reset context, use it; this means someone is waiting
526 * for us to tell him when the reset operation is complete and the
527 * device is ready to rock again.
528 *
529 * NOTE: if we are in the process of bringing up or down the
530 * communication with the device [running i2400m_dev_start() or
531 * _stop()], don't do anything, let it fail and handle it.
532 *
533 * This function is ran always in a thread context
534 */
535static
536void __i2400m_dev_reset_handle(struct work_struct *ws)
537{
538 int result;
539 struct i2400m_work *iw = container_of(ws, struct i2400m_work, ws);
540 struct i2400m *i2400m = iw->i2400m;
541 struct device *dev = i2400m_dev(i2400m);
542 enum wimax_st wimax_state;
543 struct i2400m_reset_ctx *ctx = i2400m->reset_ctx;
544
545 d_fnstart(3, dev, "(ws %p i2400m %p)\n", ws, i2400m);
546 result = 0;
547 if (mutex_trylock(&i2400m->init_mutex) == 0) {
548 /* We are still in i2400m_dev_start() [let it fail] or
549 * i2400m_dev_stop() [we are shutting down anyway, so
550 * ignore it] or we are resetting somewhere else. */
551 dev_err(dev, "device rebooted\n");
552 i2400m_msg_to_dev_cancel_wait(i2400m, -ERESTARTSYS);
553 complete(&i2400m->msg_completion);
554 goto out;
555 }
556 wimax_state = wimax_state_get(&i2400m->wimax_dev);
557 if (wimax_state < WIMAX_ST_UNINITIALIZED) {
558 dev_info(dev, "device rebooted: it is down, ignoring\n");
559 goto out_unlock; /* ifconfig up/down wasn't called */
560 }
561 dev_err(dev, "device rebooted: reinitializing driver\n");
562 __i2400m_dev_stop(i2400m);
563 i2400m->updown = 0;
564 result = __i2400m_dev_start(i2400m,
565 I2400M_BRI_SOFT | I2400M_BRI_MAC_REINIT);
566 if (result < 0) {
567 dev_err(dev, "device reboot: cannot start the device: %d\n",
568 result);
569 result = i2400m->bus_reset(i2400m, I2400M_RT_BUS);
570 if (result >= 0)
571 result = -ENODEV;
572 } else
573 i2400m->updown = 1;
574out_unlock:
575 if (i2400m->reset_ctx) {
576 ctx->result = result;
577 complete(&ctx->completion);
578 }
579 mutex_unlock(&i2400m->init_mutex);
580out:
581 i2400m_put(i2400m);
582 kfree(iw);
583 d_fnend(3, dev, "(ws %p i2400m %p) = void\n", ws, i2400m);
584 return;
585}
586
587
588/**
589 * i2400m_dev_reset_handle - Handle a device's reset in a thread context
590 *
591 * Schedule a device reset handling out on a thread context, so it
592 * is safe to call from atomic context. We can't use the i2400m's
593 * queue as we are going to destroy it and reinitialize it as part of
594 * the driver bringup/bringup process.
595 *
596 * See __i2400m_dev_reset_handle() for details; that takes care of
597 * reinitializing the driver to handle the reset, calling into the
598 * bus-specific functions ops as needed.
599 */
600int i2400m_dev_reset_handle(struct i2400m *i2400m)
601{
602 return i2400m_schedule_work(i2400m, __i2400m_dev_reset_handle,
603 GFP_ATOMIC);
604}
605EXPORT_SYMBOL_GPL(i2400m_dev_reset_handle);
606
607
608/**
609 * i2400m_setup - bus-generic setup function for the i2400m device
610 *
611 * @i2400m: device descriptor (bus-specific parts have been initialized)
612 *
613 * Returns: 0 if ok, < 0 errno code on error.
614 *
615 * Initializes the bus-generic parts of the i2400m driver; the
616 * bus-specific parts have been initialized, function pointers filled
617 * out by the bus-specific probe function.
618 *
619 * As well, this registers the WiMAX and net device nodes. Once this
620 * function returns, the device is operative and has to be ready to
621 * receive and send network traffic and WiMAX control operations.
622 */
623int i2400m_setup(struct i2400m *i2400m, enum i2400m_bri bm_flags)
624{
625 int result = -ENODEV;
626 struct device *dev = i2400m_dev(i2400m);
627 struct wimax_dev *wimax_dev = &i2400m->wimax_dev;
628 struct net_device *net_dev = i2400m->wimax_dev.net_dev;
629
630 d_fnstart(3, dev, "(i2400m %p)\n", i2400m);
631
632 snprintf(wimax_dev->name, sizeof(wimax_dev->name),
Kay Sievers347707b2009-02-28 23:42:51 +0000633 "i2400m-%s:%s", dev->bus->name, dev_name(dev));
Inaky Perez-Gonzalez024f7f32008-12-20 16:57:44 -0800634
635 i2400m->bm_cmd_buf = kzalloc(I2400M_BM_CMD_BUF_SIZE, GFP_KERNEL);
636 if (i2400m->bm_cmd_buf == NULL) {
637 dev_err(dev, "cannot allocate USB command buffer\n");
638 goto error_bm_cmd_kzalloc;
639 }
640 i2400m->bm_ack_buf = kzalloc(I2400M_BM_ACK_BUF_SIZE, GFP_KERNEL);
641 if (i2400m->bm_ack_buf == NULL) {
642 dev_err(dev, "cannot allocate USB ack buffer\n");
643 goto error_bm_ack_buf_kzalloc;
644 }
645 result = i2400m_bootrom_init(i2400m, bm_flags);
646 if (result < 0) {
647 dev_err(dev, "read mac addr: bootrom init "
648 "failed: %d\n", result);
649 goto error_bootrom_init;
650 }
651 result = i2400m_read_mac_addr(i2400m);
652 if (result < 0)
653 goto error_read_mac_addr;
Inaky Perez-Gonzalezfe442682009-04-22 16:53:08 -0700654 random_ether_addr(i2400m->src_mac_addr);
Inaky Perez-Gonzalez024f7f32008-12-20 16:57:44 -0800655
656 result = register_netdev(net_dev); /* Okey dokey, bring it up */
657 if (result < 0) {
658 dev_err(dev, "cannot register i2400m network device: %d\n",
659 result);
660 goto error_register_netdev;
661 }
662 netif_carrier_off(net_dev);
663
664 result = i2400m_dev_start(i2400m, bm_flags);
665 if (result < 0)
666 goto error_dev_start;
667
668 i2400m->wimax_dev.op_msg_from_user = i2400m_op_msg_from_user;
669 i2400m->wimax_dev.op_rfkill_sw_toggle = i2400m_op_rfkill_sw_toggle;
670 i2400m->wimax_dev.op_reset = i2400m_op_reset;
671 result = wimax_dev_add(&i2400m->wimax_dev, net_dev);
672 if (result < 0)
673 goto error_wimax_dev_add;
674 /* User space needs to do some init stuff */
675 wimax_state_change(wimax_dev, WIMAX_ST_UNINITIALIZED);
676
677 /* Now setup all that requires a registered net and wimax device. */
Inaky Perez-Gonzalez89876912009-02-28 23:42:50 +0000678 result = sysfs_create_group(&net_dev->dev.kobj, &i2400m_dev_attr_group);
679 if (result < 0) {
680 dev_err(dev, "cannot setup i2400m's sysfs: %d\n", result);
681 goto error_sysfs_setup;
682 }
Inaky Perez-Gonzalez024f7f32008-12-20 16:57:44 -0800683 result = i2400m_debugfs_add(i2400m);
684 if (result < 0) {
685 dev_err(dev, "cannot setup i2400m's debugfs: %d\n", result);
686 goto error_debugfs_setup;
687 }
688 d_fnend(3, dev, "(i2400m %p) = %d\n", i2400m, result);
689 return result;
690
691error_debugfs_setup:
Inaky Perez-Gonzalez89876912009-02-28 23:42:50 +0000692 sysfs_remove_group(&i2400m->wimax_dev.net_dev->dev.kobj,
693 &i2400m_dev_attr_group);
694error_sysfs_setup:
Inaky Perez-Gonzalez024f7f32008-12-20 16:57:44 -0800695 wimax_dev_rm(&i2400m->wimax_dev);
696error_wimax_dev_add:
697 i2400m_dev_stop(i2400m);
698error_dev_start:
699 unregister_netdev(net_dev);
700error_register_netdev:
701error_read_mac_addr:
702error_bootrom_init:
703 kfree(i2400m->bm_ack_buf);
704error_bm_ack_buf_kzalloc:
705 kfree(i2400m->bm_cmd_buf);
706error_bm_cmd_kzalloc:
707 d_fnend(3, dev, "(i2400m %p) = %d\n", i2400m, result);
708 return result;
709}
710EXPORT_SYMBOL_GPL(i2400m_setup);
711
712
713/**
714 * i2400m_release - release the bus-generic driver resources
715 *
716 * Sends a disconnect message and undoes any setup done by i2400m_setup()
717 */
718void i2400m_release(struct i2400m *i2400m)
719{
720 struct device *dev = i2400m_dev(i2400m);
721
722 d_fnstart(3, dev, "(i2400m %p)\n", i2400m);
723 netif_stop_queue(i2400m->wimax_dev.net_dev);
724
725 i2400m_debugfs_rm(i2400m);
Inaky Perez-Gonzalez89876912009-02-28 23:42:50 +0000726 sysfs_remove_group(&i2400m->wimax_dev.net_dev->dev.kobj,
727 &i2400m_dev_attr_group);
Inaky Perez-Gonzalez024f7f32008-12-20 16:57:44 -0800728 wimax_dev_rm(&i2400m->wimax_dev);
729 i2400m_dev_stop(i2400m);
730 unregister_netdev(i2400m->wimax_dev.net_dev);
731 kfree(i2400m->bm_ack_buf);
732 kfree(i2400m->bm_cmd_buf);
733 d_fnend(3, dev, "(i2400m %p) = void\n", i2400m);
734}
735EXPORT_SYMBOL_GPL(i2400m_release);
736
737
Inaky Perez-Gonzalez1af7ad52009-01-29 17:18:31 -0800738/*
739 * Debug levels control; see debug.h
740 */
741struct d_level D_LEVEL[] = {
742 D_SUBMODULE_DEFINE(control),
743 D_SUBMODULE_DEFINE(driver),
744 D_SUBMODULE_DEFINE(debugfs),
745 D_SUBMODULE_DEFINE(fw),
746 D_SUBMODULE_DEFINE(netdev),
747 D_SUBMODULE_DEFINE(rfkill),
748 D_SUBMODULE_DEFINE(rx),
749 D_SUBMODULE_DEFINE(tx),
750};
751size_t D_LEVEL_SIZE = ARRAY_SIZE(D_LEVEL);
752
753
Inaky Perez-Gonzalez024f7f32008-12-20 16:57:44 -0800754static
755int __init i2400m_driver_init(void)
756{
757 return 0;
758}
759module_init(i2400m_driver_init);
760
761static
762void __exit i2400m_driver_exit(void)
763{
764 /* for scheds i2400m_dev_reset_handle() */
765 flush_scheduled_work();
766 return;
767}
768module_exit(i2400m_driver_exit);
769
770MODULE_AUTHOR("Intel Corporation <linux-wimax@intel.com>");
771MODULE_DESCRIPTION("Intel 2400M WiMAX networking bus-generic driver");
772MODULE_LICENSE("GPL");