Merge branch 'master' into for-linus
[sfrench/cifs-2.6.git] / drivers / net / wireless / iwmc3200wifi / sdio.c
1 /*
2  * Intel Wireless Multicomm 3200 WiFi driver
3  *
4  * Copyright (C) 2009 Intel Corporation. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  *   * Redistributions of source code must retain the above copyright
11  *     notice, this list of conditions and the following disclaimer.
12  *   * Redistributions in binary form must reproduce the above copyright
13  *     notice, this list of conditions and the following disclaimer in
14  *     the documentation and/or other materials provided with the
15  *     distribution.
16  *   * Neither the name of Intel Corporation nor the names of its
17  *     contributors may be used to endorse or promote products derived
18  *     from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  *
32  *
33  * Intel Corporation <ilw@linux.intel.com>
34  * Samuel Ortiz <samuel.ortiz@intel.com>
35  * Zhu Yi <yi.zhu@intel.com>
36  *
37  */
38
39 /*
40  * This is the SDIO bus specific hooks for iwm.
41  * It also is the module's entry point.
42  *
43  * Interesting code paths:
44  * iwm_sdio_probe() (Called by an SDIO bus scan)
45  *  -> iwm_if_alloc() (netdev.c)
46  *      -> iwm_wdev_alloc() (cfg80211.c, allocates and register our wiphy)
47  *          -> wiphy_new()
48  *          -> wiphy_register()
49  *      -> alloc_netdev_mq()
50  *      -> register_netdev()
51  *
52  * iwm_sdio_remove()
53  *  -> iwm_if_free() (netdev.c)
54  *      -> unregister_netdev()
55  *      -> iwm_wdev_free() (cfg80211.c)
56  *          -> wiphy_unregister()
57  *          -> wiphy_free()
58  *
59  * iwm_sdio_isr() (called in process context from the SDIO core code)
60  *  -> queue_work(.., isr_worker)
61  *      -- [async] --> iwm_sdio_isr_worker()
62  *                      -> iwm_rx_handle()
63  */
64
65 #include <linux/kernel.h>
66 #include <linux/netdevice.h>
67 #include <linux/debugfs.h>
68 #include <linux/mmc/sdio_ids.h>
69 #include <linux/mmc/sdio.h>
70 #include <linux/mmc/sdio_func.h>
71
72 #include "iwm.h"
73 #include "debug.h"
74 #include "bus.h"
75 #include "sdio.h"
76
77 static void iwm_sdio_isr_worker(struct work_struct *work)
78 {
79         struct iwm_sdio_priv *hw;
80         struct iwm_priv *iwm;
81         struct iwm_rx_info *rx_info;
82         struct sk_buff *skb;
83         u8 *rx_buf;
84         unsigned long rx_size;
85
86         hw = container_of(work, struct iwm_sdio_priv, isr_worker);
87         iwm = hw_to_iwm(hw);
88
89         while (!skb_queue_empty(&iwm->rx_list)) {
90                 skb = skb_dequeue(&iwm->rx_list);
91                 rx_info = skb_to_rx_info(skb);
92                 rx_size = rx_info->rx_size;
93                 rx_buf = skb->data;
94
95                 IWM_HEXDUMP(iwm, DBG, SDIO, "RX: ", rx_buf, rx_size);
96                 if (iwm_rx_handle(iwm, rx_buf, rx_size) < 0)
97                         IWM_WARN(iwm, "RX error\n");
98
99                 kfree_skb(skb);
100         }
101 }
102
103 static void iwm_sdio_isr(struct sdio_func *func)
104 {
105         struct iwm_priv *iwm;
106         struct iwm_sdio_priv *hw;
107         struct iwm_rx_info *rx_info;
108         struct sk_buff *skb;
109         unsigned long buf_size, read_size;
110         int ret;
111         u8 val;
112
113         hw = sdio_get_drvdata(func);
114         iwm = hw_to_iwm(hw);
115
116         buf_size = hw->blk_size;
117
118         /* We're checking the status */
119         val = sdio_readb(func, IWM_SDIO_INTR_STATUS_ADDR, &ret);
120         if (val == 0 || ret < 0) {
121                 IWM_ERR(iwm, "Wrong INTR_STATUS\n");
122                 return;
123         }
124
125         /* See if we have free buffers */
126         if (skb_queue_len(&iwm->rx_list) > IWM_RX_LIST_SIZE) {
127                 IWM_ERR(iwm, "No buffer for more Rx frames\n");
128                 return;
129         }
130
131         /* We first read the transaction size */
132         read_size = sdio_readb(func, IWM_SDIO_INTR_GET_SIZE_ADDR + 1, &ret);
133         read_size = read_size << 8;
134
135         if (ret < 0) {
136                 IWM_ERR(iwm, "Couldn't read the xfer size\n");
137                 return;
138         }
139
140         /* We need to clear the INT register */
141         sdio_writeb(func, 1, IWM_SDIO_INTR_CLEAR_ADDR, &ret);
142         if (ret < 0) {
143                 IWM_ERR(iwm, "Couldn't clear the INT register\n");
144                 return;
145         }
146
147         while (buf_size < read_size)
148                 buf_size <<= 1;
149
150         skb = dev_alloc_skb(buf_size);
151         if (!skb) {
152                 IWM_ERR(iwm, "Couldn't alloc RX skb\n");
153                 return;
154         }
155         rx_info = skb_to_rx_info(skb);
156         rx_info->rx_size = read_size;
157         rx_info->rx_buf_size = buf_size;
158
159         /* Now we can read the actual buffer */
160         ret = sdio_memcpy_fromio(func, skb_put(skb, read_size),
161                                  IWM_SDIO_DATA_ADDR, read_size);
162
163         /* The skb is put on a driver's specific Rx SKB list */
164         skb_queue_tail(&iwm->rx_list, skb);
165
166         /* We can now schedule the actual worker */
167         queue_work(hw->isr_wq, &hw->isr_worker);
168 }
169
170 static void iwm_sdio_rx_free(struct iwm_sdio_priv *hw)
171 {
172         struct iwm_priv *iwm = hw_to_iwm(hw);
173
174         flush_workqueue(hw->isr_wq);
175
176         skb_queue_purge(&iwm->rx_list);
177 }
178
179 /* Bus ops */
180 static int if_sdio_enable(struct iwm_priv *iwm)
181 {
182         struct iwm_sdio_priv *hw = iwm_to_if_sdio(iwm);
183         int ret;
184
185         sdio_claim_host(hw->func);
186
187         ret = sdio_enable_func(hw->func);
188         if (ret) {
189                 IWM_ERR(iwm, "Couldn't enable the device: is TOP driver "
190                         "loaded and functional?\n");
191                 goto release_host;
192         }
193
194         iwm_reset(iwm);
195
196         ret = sdio_claim_irq(hw->func, iwm_sdio_isr);
197         if (ret) {
198                 IWM_ERR(iwm, "Failed to claim irq: %d\n", ret);
199                 goto release_host;
200         }
201
202         sdio_writeb(hw->func, 1, IWM_SDIO_INTR_ENABLE_ADDR, &ret);
203         if (ret < 0) {
204                 IWM_ERR(iwm, "Couldn't enable INTR: %d\n", ret);
205                 goto release_irq;
206         }
207
208         sdio_release_host(hw->func);
209
210         IWM_DBG_SDIO(iwm, INFO, "IWM SDIO enable\n");
211
212         return 0;
213
214  release_irq:
215         sdio_release_irq(hw->func);
216  release_host:
217         sdio_release_host(hw->func);
218
219         return ret;
220 }
221
222 static int if_sdio_disable(struct iwm_priv *iwm)
223 {
224         struct iwm_sdio_priv *hw = iwm_to_if_sdio(iwm);
225         int ret;
226
227         iwm_reset(iwm);
228
229         sdio_claim_host(hw->func);
230         sdio_writeb(hw->func, 0, IWM_SDIO_INTR_ENABLE_ADDR, &ret);
231         if (ret < 0)
232                 IWM_WARN(iwm, "Couldn't disable INTR: %d\n", ret);
233
234         sdio_release_irq(hw->func);
235         sdio_disable_func(hw->func);
236         sdio_release_host(hw->func);
237
238         iwm_sdio_rx_free(hw);
239
240         IWM_DBG_SDIO(iwm, INFO, "IWM SDIO disable\n");
241
242         return 0;
243 }
244
245 static int if_sdio_send_chunk(struct iwm_priv *iwm, u8 *buf, int count)
246 {
247         struct iwm_sdio_priv *hw = iwm_to_if_sdio(iwm);
248         int aligned_count = ALIGN(count, hw->blk_size);
249         int ret;
250
251         if ((unsigned long)buf & 0x3) {
252                 IWM_ERR(iwm, "buf <%p> is not dword aligned\n", buf);
253                 /* TODO: Is this a hardware limitation? use get_unligned */
254                 return -EINVAL;
255         }
256
257         sdio_claim_host(hw->func);
258         ret = sdio_memcpy_toio(hw->func, IWM_SDIO_DATA_ADDR, buf,
259                                aligned_count);
260         sdio_release_host(hw->func);
261
262         return ret;
263 }
264
265 /* debugfs hooks */
266 static int iwm_debugfs_sdio_open(struct inode *inode, struct file *filp)
267 {
268         filp->private_data = inode->i_private;
269         return 0;
270 }
271
272 static ssize_t iwm_debugfs_sdio_read(struct file *filp, char __user *buffer,
273                                      size_t count, loff_t *ppos)
274 {
275         struct iwm_priv *iwm = filp->private_data;
276         struct iwm_sdio_priv *hw = iwm_to_if_sdio(iwm);
277         char *buf;
278         u8 cccr;
279         int buf_len = 4096, ret;
280         size_t len = 0;
281
282         if (*ppos != 0)
283                 return 0;
284         if (count < sizeof(buf))
285                 return -ENOSPC;
286
287         buf = kzalloc(buf_len, GFP_KERNEL);
288         if (!buf)
289                 return -ENOMEM;
290
291         sdio_claim_host(hw->func);
292
293         cccr =  sdio_f0_readb(hw->func, SDIO_CCCR_IOEx, &ret);
294         if (ret) {
295                 IWM_ERR(iwm, "Could not read SDIO_CCCR_IOEx\n");
296                 goto err;
297         }
298         len += snprintf(buf + len, buf_len - len, "CCCR_IOEx:  0x%x\n", cccr);
299
300         cccr =  sdio_f0_readb(hw->func, SDIO_CCCR_IORx, &ret);
301         if (ret) {
302                 IWM_ERR(iwm, "Could not read SDIO_CCCR_IORx\n");
303                 goto err;
304         }
305         len += snprintf(buf + len, buf_len - len, "CCCR_IORx:  0x%x\n", cccr);
306
307
308         cccr =  sdio_f0_readb(hw->func, SDIO_CCCR_IENx, &ret);
309         if (ret) {
310                 IWM_ERR(iwm, "Could not read SDIO_CCCR_IENx\n");
311                 goto err;
312         }
313         len += snprintf(buf + len, buf_len - len, "CCCR_IENx:  0x%x\n", cccr);
314
315
316         cccr =  sdio_f0_readb(hw->func, SDIO_CCCR_INTx, &ret);
317         if (ret) {
318                 IWM_ERR(iwm, "Could not read SDIO_CCCR_INTx\n");
319                 goto err;
320         }
321         len += snprintf(buf + len, buf_len - len, "CCCR_INTx:  0x%x\n", cccr);
322
323
324         cccr =  sdio_f0_readb(hw->func, SDIO_CCCR_ABORT, &ret);
325         if (ret) {
326                 IWM_ERR(iwm, "Could not read SDIO_CCCR_ABORTx\n");
327                 goto err;
328         }
329         len += snprintf(buf + len, buf_len - len, "CCCR_ABORT: 0x%x\n", cccr);
330
331         cccr =  sdio_f0_readb(hw->func, SDIO_CCCR_IF, &ret);
332         if (ret) {
333                 IWM_ERR(iwm, "Could not read SDIO_CCCR_IF\n");
334                 goto err;
335         }
336         len += snprintf(buf + len, buf_len - len, "CCCR_IF:    0x%x\n", cccr);
337
338
339         cccr =  sdio_f0_readb(hw->func, SDIO_CCCR_CAPS, &ret);
340         if (ret) {
341                 IWM_ERR(iwm, "Could not read SDIO_CCCR_CAPS\n");
342                 goto err;
343         }
344         len += snprintf(buf + len, buf_len - len, "CCCR_CAPS:  0x%x\n", cccr);
345
346         cccr =  sdio_f0_readb(hw->func, SDIO_CCCR_CIS, &ret);
347         if (ret) {
348                 IWM_ERR(iwm, "Could not read SDIO_CCCR_CIS\n");
349                 goto err;
350         }
351         len += snprintf(buf + len, buf_len - len, "CCCR_CIS:   0x%x\n", cccr);
352
353         ret = simple_read_from_buffer(buffer, len, ppos, buf, buf_len);
354 err:
355         sdio_release_host(hw->func);
356
357         kfree(buf);
358
359         return ret;
360 }
361
362 static const struct file_operations iwm_debugfs_sdio_fops = {
363         .owner =        THIS_MODULE,
364         .open =         iwm_debugfs_sdio_open,
365         .read =         iwm_debugfs_sdio_read,
366 };
367
368 static int if_sdio_debugfs_init(struct iwm_priv *iwm, struct dentry *parent_dir)
369 {
370         int result;
371         struct iwm_sdio_priv *hw = iwm_to_if_sdio(iwm);
372
373         hw->cccr_dentry = debugfs_create_file("cccr", 0200,
374                                               parent_dir, iwm,
375                                               &iwm_debugfs_sdio_fops);
376         result = PTR_ERR(hw->cccr_dentry);
377         if (IS_ERR(hw->cccr_dentry) && (result != -ENODEV)) {
378                 IWM_ERR(iwm, "Couldn't create CCCR entry: %d\n", result);
379                 return result;
380         }
381
382         return 0;
383 }
384
385 static void if_sdio_debugfs_exit(struct iwm_priv *iwm)
386 {
387         struct iwm_sdio_priv *hw = iwm_to_if_sdio(iwm);
388
389         debugfs_remove(hw->cccr_dentry);
390 }
391
392 static struct iwm_if_ops if_sdio_ops = {
393         .enable = if_sdio_enable,
394         .disable = if_sdio_disable,
395         .send_chunk = if_sdio_send_chunk,
396         .debugfs_init = if_sdio_debugfs_init,
397         .debugfs_exit = if_sdio_debugfs_exit,
398         .umac_name = "iwmc3200wifi-umac-sdio.bin",
399         .calib_lmac_name = "iwmc3200wifi-calib-sdio.bin",
400         .lmac_name = "iwmc3200wifi-lmac-sdio.bin",
401 };
402
403 static int iwm_sdio_probe(struct sdio_func *func,
404                           const struct sdio_device_id *id)
405 {
406         struct iwm_priv *iwm;
407         struct iwm_sdio_priv *hw;
408         struct device *dev = &func->dev;
409         int ret;
410
411         /* check if TOP has already initialized the card */
412         sdio_claim_host(func);
413         ret = sdio_enable_func(func);
414         if (ret) {
415                 dev_err(dev, "wait for TOP to enable the device\n");
416                 sdio_release_host(func);
417                 return ret;
418         }
419
420         ret = sdio_set_block_size(func, IWM_SDIO_BLK_SIZE);
421
422         sdio_disable_func(func);
423         sdio_release_host(func);
424
425         if (ret < 0) {
426                 dev_err(dev, "Failed to set block size: %d\n", ret);
427                 return ret;
428         }
429
430         iwm = iwm_if_alloc(sizeof(struct iwm_sdio_priv), dev, &if_sdio_ops);
431         if (IS_ERR(iwm)) {
432                 dev_err(dev, "allocate SDIO interface failed\n");
433                 return PTR_ERR(iwm);
434         }
435
436         hw = iwm_private(iwm);
437         hw->iwm = iwm;
438
439         ret = iwm_debugfs_init(iwm);
440         if (ret < 0) {
441                 IWM_ERR(iwm, "Debugfs registration failed\n");
442                 goto if_free;
443         }
444
445         sdio_set_drvdata(func, hw);
446
447         hw->func = func;
448         hw->blk_size = IWM_SDIO_BLK_SIZE;
449
450         hw->isr_wq = create_singlethread_workqueue(KBUILD_MODNAME "_sdio");
451         if (!hw->isr_wq) {
452                 ret = -ENOMEM;
453                 goto debugfs_exit;
454         }
455
456         INIT_WORK(&hw->isr_worker, iwm_sdio_isr_worker);
457
458         ret = iwm_if_add(iwm);
459         if (ret) {
460                 dev_err(dev, "add SDIO interface failed\n");
461                 goto destroy_wq;
462         }
463
464         dev_info(dev, "IWM SDIO probe\n");
465
466         return 0;
467
468  destroy_wq:
469         destroy_workqueue(hw->isr_wq);
470  debugfs_exit:
471         iwm_debugfs_exit(iwm);
472  if_free:
473         iwm_if_free(iwm);
474         return ret;
475 }
476
477 static void iwm_sdio_remove(struct sdio_func *func)
478 {
479         struct iwm_sdio_priv *hw = sdio_get_drvdata(func);
480         struct iwm_priv *iwm = hw_to_iwm(hw);
481         struct device *dev = &func->dev;
482
483         iwm_if_remove(iwm);
484         destroy_workqueue(hw->isr_wq);
485         iwm_debugfs_exit(iwm);
486         iwm_if_free(iwm);
487
488         sdio_set_drvdata(func, NULL);
489
490         dev_info(dev, "IWM SDIO remove\n");
491
492         return;
493 }
494
495 static const struct sdio_device_id iwm_sdio_ids[] = {
496         { SDIO_DEVICE(SDIO_VENDOR_ID_INTEL,
497                       SDIO_DEVICE_ID_INTEL_IWMC3200WIFI) },
498         { /* end: all zeroes */ },
499 };
500 MODULE_DEVICE_TABLE(sdio, iwm_sdio_ids);
501
502 static struct sdio_driver iwm_sdio_driver = {
503         .name           = "iwm_sdio",
504         .id_table       = iwm_sdio_ids,
505         .probe          = iwm_sdio_probe,
506         .remove         = iwm_sdio_remove,
507 };
508
509 static int __init iwm_sdio_init_module(void)
510 {
511         return sdio_register_driver(&iwm_sdio_driver);
512 }
513
514 static void __exit iwm_sdio_exit_module(void)
515 {
516         sdio_unregister_driver(&iwm_sdio_driver);
517 }
518
519 module_init(iwm_sdio_init_module);
520 module_exit(iwm_sdio_exit_module);
521
522 MODULE_LICENSE("GPL");
523 MODULE_AUTHOR(IWM_COPYRIGHT " " IWM_AUTHOR);