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