wimax/i2400m: introduce module parameter to disable entering power save
[safe/jmp/linux-2.6] / 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_firmware_check()
52  *       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"
65 #include <linux/etherdevice.h>
66 #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
74 int i2400m_idle_mode_disabled;  /* 0 (idle mode enabled) by default */
75 module_param_named(idle_mode_disabled, i2400m_idle_mode_disabled, int, 0644);
76 MODULE_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
80 int i2400m_rx_reorder_disabled; /* 0 (rx reorder enabled) by default */
81 module_param_named(rx_reorder_disabled, i2400m_rx_reorder_disabled, int, 0644);
82 MODULE_PARM_DESC(rx_reorder_disabled,
83                  "If true, RX reordering will be disabled.");
84
85 int i2400m_power_save_disabled; /* 0 (power saving enabled) by default */
86 module_param_named(power_save_disabled, i2400m_power_save_disabled, int, 0644);
87 MODULE_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
93 /**
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  */
143 int 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);
159 error_kzalloc:
160         return result;
161 }
162 EXPORT_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  */
177 int 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;
193 error_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  */
228 static
229 int 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;
246         result = wimax_msg_send(&i2400m->wimax_dev, ack_skb);
247 error_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  */
258 struct 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  */
280 static
281 int 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);
307 out:
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  */
320 static
321 int 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         }
368 ok:
369         result = 0;
370         kfree_skb(skb);
371 error:
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  *
388  * TX needs to be setup before the bus-specific code (otherwise on
389  * shutdown, the bus-tx code could try to access it).
390  */
391 static
392 int __i2400m_dev_start(struct i2400m *i2400m, enum i2400m_bri flags)
393 {
394         int result;
395         struct wimax_dev *wimax_dev = &i2400m->wimax_dev;
396         struct net_device *net_dev = wimax_dev->net_dev;
397         struct device *dev = i2400m_dev(i2400m);
398         int times = 3;
399
400         d_fnstart(3, dev, "(i2400m %p)\n", i2400m);
401 retry:
402         result = i2400m_dev_bootstrap(i2400m, flags);
403         if (result < 0) {
404                 dev_err(dev, "cannot bootstrap device: %d\n", result);
405                 goto error_bootstrap;
406         }
407         result = i2400m_tx_setup(i2400m);
408         if (result < 0)
409                 goto error_tx_setup;
410         result = i2400m_rx_setup(i2400m);
411         if (result < 0)
412                 goto error_rx_setup;
413         result = i2400m->bus_dev_start(i2400m);
414         if (result < 0)
415                 goto error_bus_dev_start;
416         i2400m->work_queue = create_singlethread_workqueue(wimax_dev->name);
417         if (i2400m->work_queue == NULL) {
418                 result = -ENOMEM;
419                 dev_err(dev, "cannot create workqueue\n");
420                 goto error_create_workqueue;
421         }
422         result = i2400m_firmware_check(i2400m); /* fw versions ok? */
423         if (result < 0)
424                 goto error_fw_check;
425         /* At this point is ok to send commands to the device */
426         result = i2400m_check_mac_addr(i2400m);
427         if (result < 0)
428                 goto error_check_mac_addr;
429         i2400m->ready = 1;
430         wimax_state_change(wimax_dev, WIMAX_ST_UNINITIALIZED);
431         result = i2400m_dev_initialize(i2400m);
432         if (result < 0)
433                 goto error_dev_initialize;
434         /* At this point, reports will come for the device and set it
435          * to the right state if it is different than UNINITIALIZED */
436         d_fnend(3, dev, "(net_dev %p [i2400m %p]) = %d\n",
437                 net_dev, i2400m, result);
438         return result;
439
440 error_dev_initialize:
441 error_check_mac_addr:
442 error_fw_check:
443         destroy_workqueue(i2400m->work_queue);
444 error_create_workqueue:
445         i2400m->bus_dev_stop(i2400m);
446 error_bus_dev_start:
447         i2400m_rx_release(i2400m);
448 error_rx_setup:
449         i2400m_tx_release(i2400m);
450 error_tx_setup:
451 error_bootstrap:
452         if (result == -ERESTARTSYS && times-- > 0) {
453                 flags = I2400M_BRI_SOFT;
454                 goto retry;
455         }
456         d_fnend(3, dev, "(net_dev %p [i2400m %p]) = %d\n",
457                 net_dev, i2400m, result);
458         return result;
459 }
460
461
462 static
463 int i2400m_dev_start(struct i2400m *i2400m, enum i2400m_bri bm_flags)
464 {
465         int result;
466         mutex_lock(&i2400m->init_mutex);        /* Well, start the device */
467         result = __i2400m_dev_start(i2400m, bm_flags);
468         if (result >= 0)
469                 i2400m->updown = 1;
470         mutex_unlock(&i2400m->init_mutex);
471         return result;
472 }
473
474
475 /**
476  * i2400m_dev_stop - Tear down driver communication with the device
477  *
478  * @i2400m: device descriptor
479  *
480  * Returns: 0 if ok, < 0 errno code on error.
481  *
482  * Releases all the resources allocated to communicate with the device.
483  */
484 static
485 void __i2400m_dev_stop(struct i2400m *i2400m)
486 {
487         struct wimax_dev *wimax_dev = &i2400m->wimax_dev;
488         struct device *dev = i2400m_dev(i2400m);
489
490         d_fnstart(3, dev, "(i2400m %p)\n", i2400m);
491         wimax_state_change(wimax_dev, __WIMAX_ST_QUIESCING);
492         i2400m_dev_shutdown(i2400m);
493         i2400m->ready = 0;
494         destroy_workqueue(i2400m->work_queue);
495         i2400m->bus_dev_stop(i2400m);
496         i2400m_rx_release(i2400m);
497         i2400m_tx_release(i2400m);
498         wimax_state_change(wimax_dev, WIMAX_ST_DOWN);
499         d_fnend(3, dev, "(i2400m %p) = 0\n", i2400m);
500 }
501
502
503 /*
504  * Watch out -- we only need to stop if there is a need for it. The
505  * device could have reset itself and failed to come up again (see
506  * _i2400m_dev_reset_handle()).
507  */
508 static
509 void i2400m_dev_stop(struct i2400m *i2400m)
510 {
511         mutex_lock(&i2400m->init_mutex);
512         if (i2400m->updown) {
513                 __i2400m_dev_stop(i2400m);
514                 i2400m->updown = 0;
515         }
516         mutex_unlock(&i2400m->init_mutex);
517 }
518
519
520 /*
521  * The device has rebooted; fix up the device and the driver
522  *
523  * Tear down the driver communication with the device, reload the
524  * firmware and reinitialize the communication with the device.
525  *
526  * If someone calls a reset when the device's firmware is down, in
527  * theory we won't see it because we are not listening. However, just
528  * in case, leave the code to handle it.
529  *
530  * If there is a reset context, use it; this means someone is waiting
531  * for us to tell him when the reset operation is complete and the
532  * device is ready to rock again.
533  *
534  * NOTE: if we are in the process of bringing up or down the
535  *       communication with the device [running i2400m_dev_start() or
536  *       _stop()], don't do anything, let it fail and handle it.
537  *
538  * This function is ran always in a thread context
539  */
540 static
541 void __i2400m_dev_reset_handle(struct work_struct *ws)
542 {
543         int result;
544         struct i2400m_work *iw = container_of(ws, struct i2400m_work, ws);
545         struct i2400m *i2400m = iw->i2400m;
546         struct device *dev = i2400m_dev(i2400m);
547         enum wimax_st wimax_state;
548         struct i2400m_reset_ctx *ctx = i2400m->reset_ctx;
549
550         d_fnstart(3, dev, "(ws %p i2400m %p)\n", ws, i2400m);
551         result = 0;
552         if (mutex_trylock(&i2400m->init_mutex) == 0) {
553                 /* We are still in i2400m_dev_start() [let it fail] or
554                  * i2400m_dev_stop() [we are shutting down anyway, so
555                  * ignore it] or we are resetting somewhere else. */
556                 dev_err(dev, "device rebooted\n");
557                 i2400m_msg_to_dev_cancel_wait(i2400m, -ERESTARTSYS);
558                 complete(&i2400m->msg_completion);
559                 goto out;
560         }
561         wimax_state = wimax_state_get(&i2400m->wimax_dev);
562         if (wimax_state < WIMAX_ST_UNINITIALIZED) {
563                 dev_info(dev, "device rebooted: it is down, ignoring\n");
564                 goto out_unlock;        /* ifconfig up/down wasn't called */
565         }
566         dev_err(dev, "device rebooted: reinitializing driver\n");
567         __i2400m_dev_stop(i2400m);
568         i2400m->updown = 0;
569         result = __i2400m_dev_start(i2400m,
570                                     I2400M_BRI_SOFT | I2400M_BRI_MAC_REINIT);
571         if (result < 0) {
572                 dev_err(dev, "device reboot: cannot start the device: %d\n",
573                         result);
574                 result = i2400m->bus_reset(i2400m, I2400M_RT_BUS);
575                 if (result >= 0)
576                         result = -ENODEV;
577         } else
578                 i2400m->updown = 1;
579 out_unlock:
580         if (i2400m->reset_ctx) {
581                 ctx->result = result;
582                 complete(&ctx->completion);
583         }
584         mutex_unlock(&i2400m->init_mutex);
585 out:
586         i2400m_put(i2400m);
587         kfree(iw);
588         d_fnend(3, dev, "(ws %p i2400m %p) = void\n", ws, i2400m);
589         return;
590 }
591
592
593 /**
594  * i2400m_dev_reset_handle - Handle a device's reset in a thread context
595  *
596  * Schedule a device reset handling out on a thread context, so it
597  * is safe to call from atomic context. We can't use the i2400m's
598  * queue as we are going to destroy it and reinitialize it as part of
599  * the driver bringup/bringup process.
600  *
601  * See __i2400m_dev_reset_handle() for details; that takes care of
602  * reinitializing the driver to handle the reset, calling into the
603  * bus-specific functions ops as needed.
604  */
605 int i2400m_dev_reset_handle(struct i2400m *i2400m)
606 {
607         return i2400m_schedule_work(i2400m, __i2400m_dev_reset_handle,
608                                     GFP_ATOMIC);
609 }
610 EXPORT_SYMBOL_GPL(i2400m_dev_reset_handle);
611
612
613 /**
614  * i2400m_setup - bus-generic setup function for the i2400m device
615  *
616  * @i2400m: device descriptor (bus-specific parts have been initialized)
617  *
618  * Returns: 0 if ok, < 0 errno code on error.
619  *
620  * Initializes the bus-generic parts of the i2400m driver; the
621  * bus-specific parts have been initialized, function pointers filled
622  * out by the bus-specific probe function.
623  *
624  * As well, this registers the WiMAX and net device nodes. Once this
625  * function returns, the device is operative and has to be ready to
626  * receive and send network traffic and WiMAX control operations.
627  */
628 int i2400m_setup(struct i2400m *i2400m, enum i2400m_bri bm_flags)
629 {
630         int result = -ENODEV;
631         struct device *dev = i2400m_dev(i2400m);
632         struct wimax_dev *wimax_dev = &i2400m->wimax_dev;
633         struct net_device *net_dev = i2400m->wimax_dev.net_dev;
634
635         d_fnstart(3, dev, "(i2400m %p)\n", i2400m);
636
637         snprintf(wimax_dev->name, sizeof(wimax_dev->name),
638                  "i2400m-%s:%s", dev->bus->name, dev_name(dev));
639
640         i2400m->bm_cmd_buf = kzalloc(I2400M_BM_CMD_BUF_SIZE, GFP_KERNEL);
641         if (i2400m->bm_cmd_buf == NULL) {
642                 dev_err(dev, "cannot allocate USB command buffer\n");
643                 goto error_bm_cmd_kzalloc;
644         }
645         i2400m->bm_ack_buf = kzalloc(I2400M_BM_ACK_BUF_SIZE, GFP_KERNEL);
646         if (i2400m->bm_ack_buf == NULL) {
647                 dev_err(dev, "cannot allocate USB ack buffer\n");
648                 goto error_bm_ack_buf_kzalloc;
649         }
650         result = i2400m_bootrom_init(i2400m, bm_flags);
651         if (result < 0) {
652                 dev_err(dev, "read mac addr: bootrom init "
653                         "failed: %d\n", result);
654                 goto error_bootrom_init;
655         }
656         result = i2400m_read_mac_addr(i2400m);
657         if (result < 0)
658                 goto error_read_mac_addr;
659         random_ether_addr(i2400m->src_mac_addr);
660
661         result = register_netdev(net_dev);      /* Okey dokey, bring it up */
662         if (result < 0) {
663                 dev_err(dev, "cannot register i2400m network device: %d\n",
664                         result);
665                 goto error_register_netdev;
666         }
667         netif_carrier_off(net_dev);
668
669         result = i2400m_dev_start(i2400m, bm_flags);
670         if (result < 0)
671                 goto error_dev_start;
672
673         i2400m->wimax_dev.op_msg_from_user = i2400m_op_msg_from_user;
674         i2400m->wimax_dev.op_rfkill_sw_toggle = i2400m_op_rfkill_sw_toggle;
675         i2400m->wimax_dev.op_reset = i2400m_op_reset;
676         result = wimax_dev_add(&i2400m->wimax_dev, net_dev);
677         if (result < 0)
678                 goto error_wimax_dev_add;
679         /* User space needs to do some init stuff */
680         wimax_state_change(wimax_dev, WIMAX_ST_UNINITIALIZED);
681
682         /* Now setup all that requires a registered net and wimax device. */
683         result = sysfs_create_group(&net_dev->dev.kobj, &i2400m_dev_attr_group);
684         if (result < 0) {
685                 dev_err(dev, "cannot setup i2400m's sysfs: %d\n", result);
686                 goto error_sysfs_setup;
687         }
688         result = i2400m_debugfs_add(i2400m);
689         if (result < 0) {
690                 dev_err(dev, "cannot setup i2400m's debugfs: %d\n", result);
691                 goto error_debugfs_setup;
692         }
693         d_fnend(3, dev, "(i2400m %p) = %d\n", i2400m, result);
694         return result;
695
696 error_debugfs_setup:
697         sysfs_remove_group(&i2400m->wimax_dev.net_dev->dev.kobj,
698                            &i2400m_dev_attr_group);
699 error_sysfs_setup:
700         wimax_dev_rm(&i2400m->wimax_dev);
701 error_wimax_dev_add:
702         i2400m_dev_stop(i2400m);
703 error_dev_start:
704         unregister_netdev(net_dev);
705 error_register_netdev:
706 error_read_mac_addr:
707 error_bootrom_init:
708         kfree(i2400m->bm_ack_buf);
709 error_bm_ack_buf_kzalloc:
710         kfree(i2400m->bm_cmd_buf);
711 error_bm_cmd_kzalloc:
712         d_fnend(3, dev, "(i2400m %p) = %d\n", i2400m, result);
713         return result;
714 }
715 EXPORT_SYMBOL_GPL(i2400m_setup);
716
717
718 /**
719  * i2400m_release - release the bus-generic driver resources
720  *
721  * Sends a disconnect message and undoes any setup done by i2400m_setup()
722  */
723 void i2400m_release(struct i2400m *i2400m)
724 {
725         struct device *dev = i2400m_dev(i2400m);
726
727         d_fnstart(3, dev, "(i2400m %p)\n", i2400m);
728         netif_stop_queue(i2400m->wimax_dev.net_dev);
729
730         i2400m_debugfs_rm(i2400m);
731         sysfs_remove_group(&i2400m->wimax_dev.net_dev->dev.kobj,
732                            &i2400m_dev_attr_group);
733         wimax_dev_rm(&i2400m->wimax_dev);
734         i2400m_dev_stop(i2400m);
735         unregister_netdev(i2400m->wimax_dev.net_dev);
736         kfree(i2400m->bm_ack_buf);
737         kfree(i2400m->bm_cmd_buf);
738         d_fnend(3, dev, "(i2400m %p) = void\n", i2400m);
739 }
740 EXPORT_SYMBOL_GPL(i2400m_release);
741
742
743 /*
744  * Debug levels control; see debug.h
745  */
746 struct d_level D_LEVEL[] = {
747         D_SUBMODULE_DEFINE(control),
748         D_SUBMODULE_DEFINE(driver),
749         D_SUBMODULE_DEFINE(debugfs),
750         D_SUBMODULE_DEFINE(fw),
751         D_SUBMODULE_DEFINE(netdev),
752         D_SUBMODULE_DEFINE(rfkill),
753         D_SUBMODULE_DEFINE(rx),
754         D_SUBMODULE_DEFINE(tx),
755 };
756 size_t D_LEVEL_SIZE = ARRAY_SIZE(D_LEVEL);
757
758
759 static
760 int __init i2400m_driver_init(void)
761 {
762         return 0;
763 }
764 module_init(i2400m_driver_init);
765
766 static
767 void __exit i2400m_driver_exit(void)
768 {
769         /* for scheds i2400m_dev_reset_handle() */
770         flush_scheduled_work();
771         return;
772 }
773 module_exit(i2400m_driver_exit);
774
775 MODULE_AUTHOR("Intel Corporation <linux-wimax@intel.com>");
776 MODULE_DESCRIPTION("Intel 2400M WiMAX networking bus-generic driver");
777 MODULE_LICENSE("GPL");