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