]> bbs.cooldavid.org Git - net-next-2.6.git/blob - drivers/net/wimax/i2400m/sdio.c
1bfa283bbd8aabef64c88364cdd749933b02cde8
[net-next-2.6.git] / drivers / net / wimax / i2400m / sdio.c
1 /*
2  * Intel Wireless WiMAX Connection 2400m
3  * Linux driver model glue for the SDIO device, reset & fw upload
4  *
5  *
6  * Copyright (C) 2007-2008 Intel Corporation <linux-wimax@intel.com>
7  * Dirk Brandewie <dirk.j.brandewie@intel.com>
8  * Inaky Perez-Gonzalez <inaky.perez-gonzalez@intel.com>
9  * Yanir Lubetkin <yanirx.lubetkin@intel.com>
10  *
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License version
13  * 2 as published by the Free Software Foundation.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
23  * 02110-1301, USA.
24  *
25  *
26  * See i2400m-sdio.h for a general description of this driver.
27  *
28  * This file implements driver model glue, and hook ups for the
29  * generic driver to implement the bus-specific functions (device
30  * communication setup/tear down, firmware upload and resetting).
31  *
32  * ROADMAP
33  *
34  * i2400m_probe()
35  *   alloc_netdev()
36  *     i2400ms_netdev_setup()
37  *       i2400ms_init()
38  *       i2400m_netdev_setup()
39  *   i2400ms_enable_function()
40  *   i2400m_setup()
41  *
42  * i2400m_remove()
43  *     i2400m_release()
44  *     free_netdev(net_dev)
45  *
46  * i2400ms_bus_reset()            Called by i2400m->bus_reset
47  *   __i2400ms_reset()
48  *     __i2400ms_send_barker()
49  *
50  * i2400ms_bus_dev_start()        Called by i2400m_dev_start() [who is
51  *   i2400ms_tx_setup()           called by i2400m_setup()]
52  *   i2400ms_rx_setup()
53  *
54  * i2400ms_bus_dev_stop()         Called by i2400m_dev_stop() [who is
55  *   i2400ms_rx_release()         is called by i2400m_release()]
56  *   i2400ms_tx_release()
57  *
58  */
59
60 #include <linux/debugfs.h>
61 #include <linux/mmc/sdio.h>
62 #include <linux/mmc/sdio_func.h>
63 #include "i2400m-sdio.h"
64 #include <linux/wimax/i2400m.h>
65
66 #define D_SUBMODULE main
67 #include "sdio-debug-levels.h"
68
69 /* IOE WiMAX function timeout in seconds */
70 static int ioe_timeout = 2;
71 module_param(ioe_timeout, int, 0);
72
73 /* Our firmware file name */
74 #define I2400MS_FW_FILE_NAME "i2400m-fw-sdio-" I2400M_FW_VERSION ".sbcf"
75
76 /*
77  * Enable the SDIO function
78  *
79  * Tries to enable the SDIO function; might fail if it is still not
80  * ready (in some hardware, the SDIO WiMAX function is only enabled
81  * when we ask it to explicitly doing). Tries until a timeout is
82  * reached.
83  *
84  * The reverse of this is...sdio_disable_function()
85  *
86  * Returns: 0 if the SDIO function was enabled, < 0 errno code on
87  *     error (-ENODEV when it was unable to enable the function).
88  */
89 static
90 int i2400ms_enable_function(struct sdio_func *func)
91 {
92         u64 timeout;
93         int err;
94         struct device *dev = &func->dev;
95
96         d_fnstart(3, dev, "(func %p)\n", func);
97         /* Setup timeout (FIXME: This needs to read the CIS table to
98          * get a real timeout) and then wait for the device to signal
99          * it is ready */
100         timeout = get_jiffies_64() + ioe_timeout * HZ;
101         err = -ENODEV;
102         while (err != 0 && time_before64(get_jiffies_64(), timeout)) {
103                 sdio_claim_host(func);
104                 err = sdio_enable_func(func);
105                 if (0 == err) {
106                         sdio_release_host(func);
107                         d_printf(2, dev, "SDIO function enabled\n");
108                         goto function_enabled;
109                 }
110                 d_printf(2, dev, "SDIO function failed to enable: %d\n", err);
111                 sdio_disable_func(func);
112                 sdio_release_host(func);
113                 msleep(I2400MS_INIT_SLEEP_INTERVAL);
114         }
115         /* If timed out, device is not there yet -- get -ENODEV so
116          * the device driver core will retry later on. */
117         if (err == -ETIME) {
118                 dev_err(dev, "Can't enable WiMAX function; "
119                         " has the function been enabled?\n");
120                 err = -ENODEV;
121         }
122 function_enabled:
123         d_fnend(3, dev, "(func %p) = %d\n", func, err);
124         return err;
125 }
126
127
128 /*
129  * Setup driver resources needed to communicate with the device
130  *
131  * The fw needs some time to settle, and it was just uploaded,
132  * so give it a break first. I'd prefer to just wait for the device to
133  * send something, but seems the poking we do to enable SDIO stuff
134  * interferes with it, so just give it a break before starting...
135  */
136 static
137 int i2400ms_bus_dev_start(struct i2400m *i2400m)
138 {
139         int result;
140         struct i2400ms *i2400ms = container_of(i2400m, struct i2400ms, i2400m);
141         struct sdio_func *func = i2400ms->func;
142         struct device *dev = &func->dev;
143
144         d_fnstart(3, dev, "(i2400m %p)\n", i2400m);
145         msleep(200);
146         result = i2400ms_rx_setup(i2400ms);
147         if (result < 0)
148                 goto error_rx_setup;
149         result = i2400ms_tx_setup(i2400ms);
150         if (result < 0)
151                 goto error_tx_setup;
152         d_fnend(3, dev, "(i2400m %p) = %d\n", i2400m, result);
153         return result;
154
155         i2400ms_tx_release(i2400ms);
156 error_tx_setup:
157         i2400ms_rx_release(i2400ms);
158 error_rx_setup:
159         d_fnend(3, dev, "(i2400m %p) = void\n", i2400m);
160         return result;
161 }
162
163
164 static
165 void i2400ms_bus_dev_stop(struct i2400m *i2400m)
166 {
167         struct i2400ms *i2400ms = container_of(i2400m, struct i2400ms, i2400m);
168         struct sdio_func *func = i2400ms->func;
169         struct device *dev = &func->dev;
170
171         d_fnstart(3, dev, "(i2400m %p)\n", i2400m);
172         i2400ms_rx_release(i2400ms);
173         i2400ms_tx_release(i2400ms);
174         d_fnend(3, dev, "(i2400m %p) = void\n", i2400m);
175 }
176
177
178 /*
179  * Sends a barker buffer to the device
180  *
181  * This helper will allocate a kmalloced buffer and use it to transmit
182  * (then free it). Reason for this is that the SDIO host controller
183  * expects alignment (unknown exactly which) which the stack won't
184  * really provide and certain arches/host-controller combinations
185  * cannot use stack/vmalloc/text areas for DMA transfers.
186  */
187 static
188 int __i2400ms_send_barker(struct i2400ms *i2400ms,
189                           const __le32 *barker, size_t barker_size)
190 {
191         int  ret;
192         struct sdio_func *func = i2400ms->func;
193         struct device *dev = &func->dev;
194         void *buffer;
195
196         ret = -ENOMEM;
197         buffer = kmalloc(I2400MS_BLK_SIZE, GFP_KERNEL);
198         if (buffer == NULL)
199                 goto error_kzalloc;
200
201         memcpy(buffer, barker, barker_size);
202         sdio_claim_host(func);
203         ret = sdio_memcpy_toio(func, 0, buffer, I2400MS_BLK_SIZE);
204         sdio_release_host(func);
205
206         if (ret < 0)
207                 d_printf(0, dev, "E: barker error: %d\n", ret);
208
209         kfree(buffer);
210 error_kzalloc:
211         return ret;
212 }
213
214
215 /*
216  * Reset a device at different levels (warm, cold or bus)
217  *
218  * @i2400ms: device descriptor
219  * @reset_type: soft, warm or bus reset (I2400M_RT_WARM/SOFT/BUS)
220  *
221  * FIXME: not tested -- need to confirm expected effects
222  *
223  * Warm and cold resets get an SDIO reset if they fail (unimplemented)
224  *
225  * Warm reset:
226  *
227  * The device will be fully reset internally, but won't be
228  * disconnected from the USB bus (so no reenumeration will
229  * happen). Firmware upload will be neccessary.
230  *
231  * The device will send a reboot barker in the notification endpoint
232  * that will trigger the driver to reinitialize the state
233  * automatically from notif.c:i2400m_notification_grok() into
234  * i2400m_dev_bootstrap_delayed().
235  *
236  * Cold and bus (USB) reset:
237  *
238  * The device will be fully reset internally, disconnected from the
239  * USB bus an a reenumeration will happen. Firmware upload will be
240  * neccessary. Thus, we don't do any locking or struct
241  * reinitialization, as we are going to be fully disconnected and
242  * reenumerated.
243  *
244  * Note we need to return -ENODEV if a warm reset was requested and we
245  * had to resort to a bus reset. See i2400m_op_reset(), wimax_reset()
246  * and wimax_dev->op_reset.
247  *
248  * WARNING: no driver state saved/fixed
249  */
250 static
251 int i2400ms_bus_reset(struct i2400m *i2400m, enum i2400m_reset_type rt)
252 {
253         int result;
254         struct i2400ms *i2400ms =
255                 container_of(i2400m, struct i2400ms, i2400m);
256         struct device *dev = i2400m_dev(i2400m);
257         static const __le32 i2400m_WARM_BOOT_BARKER[4] = {
258                 __constant_cpu_to_le32(I2400M_WARM_RESET_BARKER),
259                 __constant_cpu_to_le32(I2400M_WARM_RESET_BARKER),
260                 __constant_cpu_to_le32(I2400M_WARM_RESET_BARKER),
261                 __constant_cpu_to_le32(I2400M_WARM_RESET_BARKER),
262         };
263         static const __le32 i2400m_COLD_BOOT_BARKER[4] = {
264                 __constant_cpu_to_le32(I2400M_COLD_RESET_BARKER),
265                 __constant_cpu_to_le32(I2400M_COLD_RESET_BARKER),
266                 __constant_cpu_to_le32(I2400M_COLD_RESET_BARKER),
267                 __constant_cpu_to_le32(I2400M_COLD_RESET_BARKER),
268         };
269
270         if (rt == I2400M_RT_WARM)
271                 result = __i2400ms_send_barker(i2400ms, i2400m_WARM_BOOT_BARKER,
272                                                sizeof(i2400m_WARM_BOOT_BARKER));
273         else if (rt == I2400M_RT_COLD)
274                 result = __i2400ms_send_barker(i2400ms, i2400m_COLD_BOOT_BARKER,
275                                                sizeof(i2400m_COLD_BOOT_BARKER));
276         else if (rt == I2400M_RT_BUS) {
277 do_bus_reset:
278                 dev_err(dev, "FIXME: SDIO bus reset not implemented\n");
279                 result = rt == I2400M_RT_WARM ? -ENODEV : -ENOSYS;
280         } else
281                 BUG();
282         if (result < 0 && rt != I2400M_RT_BUS) {
283                 dev_err(dev, "%s reset failed (%d); trying SDIO reset\n",
284                         rt == I2400M_RT_WARM ? "warm" : "cold", result);
285                 rt = I2400M_RT_BUS;
286                 goto do_bus_reset;
287         }
288         return result;
289 }
290
291
292 static
293 void i2400ms_netdev_setup(struct net_device *net_dev)
294 {
295         struct i2400m *i2400m = net_dev_to_i2400m(net_dev);
296         struct i2400ms *i2400ms = container_of(i2400m, struct i2400ms, i2400m);
297         i2400ms_init(i2400ms);
298         i2400m_netdev_setup(net_dev);
299 }
300
301
302 /*
303  * Debug levels control; see debug.h
304  */
305 struct d_level D_LEVEL[] = {
306         D_SUBMODULE_DEFINE(main),
307         D_SUBMODULE_DEFINE(tx),
308         D_SUBMODULE_DEFINE(rx),
309         D_SUBMODULE_DEFINE(fw),
310 };
311 size_t D_LEVEL_SIZE = ARRAY_SIZE(D_LEVEL);
312
313
314 #define __debugfs_register(prefix, name, parent)                        \
315 do {                                                                    \
316         result = d_level_register_debugfs(prefix, name, parent);        \
317         if (result < 0)                                                 \
318                 goto error;                                             \
319 } while (0)
320
321
322 static
323 int i2400ms_debugfs_add(struct i2400ms *i2400ms)
324 {
325         int result;
326         struct dentry *dentry = i2400ms->i2400m.wimax_dev.debugfs_dentry;
327
328         dentry = debugfs_create_dir("i2400m-usb", dentry);
329         result = PTR_ERR(dentry);
330         if (IS_ERR(dentry)) {
331                 if (result == -ENODEV)
332                         result = 0;     /* No debugfs support */
333                 goto error;
334         }
335         i2400ms->debugfs_dentry = dentry;
336         __debugfs_register("dl_", main, dentry);
337         __debugfs_register("dl_", tx, dentry);
338         __debugfs_register("dl_", rx, dentry);
339         __debugfs_register("dl_", fw, dentry);
340
341         return 0;
342
343 error:
344         debugfs_remove_recursive(i2400ms->debugfs_dentry);
345         return result;
346 }
347
348
349 /*
350  * Probe a i2400m interface and register it
351  *
352  * @func:    SDIO function
353  * @id:      SDIO device ID
354  * @returns: 0 if ok, < 0 errno code on error.
355  *
356  * Alloc a net device, initialize the bus-specific details and then
357  * calls the bus-generic initialization routine. That will register
358  * the wimax and netdev devices, upload the firmware [using
359  * _bus_bm_*()], call _bus_dev_start() to finalize the setup of the
360  * communication with the device and then will start to talk to it to
361  * finnish setting it up.
362  *
363  * Initialization is tricky; some instances of the hw are packed with
364  * others in a way that requires a third driver that enables the WiMAX
365  * function. In those cases, we can't enable the SDIO function and
366  * we'll return with -ENODEV. When the driver that enables the WiMAX
367  * function does its thing, it has to do a bus_rescan_devices() on the
368  * SDIO bus so this driver is called again to enumerate the WiMAX
369  * function.
370  */
371 static
372 int i2400ms_probe(struct sdio_func *func,
373                   const struct sdio_device_id *id)
374 {
375         int result;
376         struct net_device *net_dev;
377         struct device *dev = &func->dev;
378         struct i2400m *i2400m;
379         struct i2400ms *i2400ms;
380
381         /* Allocate instance [calls i2400m_netdev_setup() on it]. */
382         result = -ENOMEM;
383         net_dev = alloc_netdev(sizeof(*i2400ms), "wmx%d",
384                                i2400ms_netdev_setup);
385         if (net_dev == NULL) {
386                 dev_err(dev, "no memory for network device instance\n");
387                 goto error_alloc_netdev;
388         }
389         SET_NETDEV_DEV(net_dev, dev);
390         i2400m = net_dev_to_i2400m(net_dev);
391         i2400ms = container_of(i2400m, struct i2400ms, i2400m);
392         i2400m->wimax_dev.net_dev = net_dev;
393         i2400ms->func = func;
394         sdio_set_drvdata(func, i2400ms);
395
396         i2400m->bus_tx_block_size = I2400MS_BLK_SIZE;
397         i2400m->bus_pl_size_max = I2400MS_PL_SIZE_MAX;
398         i2400m->bus_dev_start = i2400ms_bus_dev_start;
399         i2400m->bus_dev_stop = i2400ms_bus_dev_stop;
400         i2400m->bus_tx_kick = i2400ms_bus_tx_kick;
401         i2400m->bus_reset = i2400ms_bus_reset;
402         i2400m->bus_bm_cmd_send = i2400ms_bus_bm_cmd_send;
403         i2400m->bus_bm_wait_for_ack = i2400ms_bus_bm_wait_for_ack;
404         i2400m->bus_fw_name = I2400MS_FW_FILE_NAME;
405         i2400m->bus_bm_mac_addr_impaired = 1;
406
407         result = i2400ms_enable_function(i2400ms->func);
408         if (result < 0) {
409                 dev_err(dev, "Cannot enable SDIO function: %d\n", result);
410                 goto error_func_enable;
411         }
412
413         sdio_claim_host(func);
414         result = sdio_set_block_size(func, I2400MS_BLK_SIZE);
415         if (result < 0) {
416                 dev_err(dev, "Failed to set block size: %d\n", result);
417                 goto error_set_blk_size;
418         }
419         sdio_release_host(func);
420
421         result = i2400m_setup(i2400m, I2400M_BRI_NO_REBOOT);
422         if (result < 0) {
423                 dev_err(dev, "cannot setup device: %d\n", result);
424                 goto error_setup;
425         }
426
427         result = i2400ms_debugfs_add(i2400ms);
428         if (result < 0) {
429                 dev_err(dev, "cannot create SDIO debugfs: %d\n",
430                         result);
431                 goto error_debugfs_add;
432         }
433         return 0;
434
435 error_debugfs_add:
436         i2400m_release(i2400m);
437 error_setup:
438         sdio_set_drvdata(func, NULL);
439         sdio_claim_host(func);
440 error_set_blk_size:
441         sdio_disable_func(func);
442         sdio_release_host(func);
443 error_func_enable:
444         free_netdev(net_dev);
445 error_alloc_netdev:
446         return result;
447 }
448
449
450 static
451 void i2400ms_remove(struct sdio_func *func)
452 {
453         struct device *dev = &func->dev;
454         struct i2400ms *i2400ms = sdio_get_drvdata(func);
455         struct i2400m *i2400m = &i2400ms->i2400m;
456         struct net_device *net_dev = i2400m->wimax_dev.net_dev;
457
458         d_fnstart(3, dev, "SDIO func %p\n", func);
459         debugfs_remove_recursive(i2400ms->debugfs_dentry);
460         i2400m_release(i2400m);
461         sdio_set_drvdata(func, NULL);
462         sdio_claim_host(func);
463         sdio_disable_func(func);
464         sdio_release_host(func);
465         free_netdev(net_dev);
466         d_fnend(3, dev, "SDIO func %p\n", func);
467 }
468
469 enum {
470         I2400MS_INTEL_VID = 0x89,
471 };
472
473 static
474 const struct sdio_device_id i2400ms_sdio_ids[] = {
475         /* Intel: i2400m WiMAX over SDIO */
476         { SDIO_DEVICE(I2400MS_INTEL_VID, 0x1402) },
477         { },                    /* end: all zeroes */
478 };
479 MODULE_DEVICE_TABLE(sdio, i2400ms_sdio_ids);
480
481
482 static
483 struct sdio_driver i2400m_sdio_driver = {
484         .name           = KBUILD_MODNAME,
485         .probe          = i2400ms_probe,
486         .remove         = i2400ms_remove,
487         .id_table       = i2400ms_sdio_ids,
488 };
489
490
491 static
492 int __init i2400ms_driver_init(void)
493 {
494         return sdio_register_driver(&i2400m_sdio_driver);
495 }
496 module_init(i2400ms_driver_init);
497
498
499 static
500 void __exit i2400ms_driver_exit(void)
501 {
502         flush_scheduled_work(); /* for the stuff we schedule */
503         sdio_unregister_driver(&i2400m_sdio_driver);
504 }
505 module_exit(i2400ms_driver_exit);
506
507
508 MODULE_AUTHOR("Intel Corporation <linux-wimax@intel.com>");
509 MODULE_DESCRIPTION("Intel 2400M WiMAX networking for SDIO");
510 MODULE_LICENSE("GPL");
511 MODULE_FIRMWARE(I2400MS_FW_FILE_NAME);