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