iwmc3200wifi: Add new Intel Wireless Multicomm 802.11 driver
[linux-block.git] / drivers / net / wireless / iwmc3200wifi / sdio.c
CommitLineData
bb9f8692
ZY
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.h>
69#include <linux/mmc/sdio_func.h>
70
71#include "iwm.h"
72#include "debug.h"
73#include "bus.h"
74#include "sdio.h"
75
76static void iwm_sdio_isr_worker(struct work_struct *work)
77{
78 struct iwm_sdio_priv *hw;
79 struct iwm_priv *iwm;
80 struct iwm_rx_info *rx_info;
81 struct sk_buff *skb;
82 u8 *rx_buf;
83 unsigned long rx_size;
84
85 hw = container_of(work, struct iwm_sdio_priv, isr_worker);
86 iwm = hw_to_iwm(hw);
87
88 while (!skb_queue_empty(&iwm->rx_list)) {
89 skb = skb_dequeue(&iwm->rx_list);
90 rx_info = skb_to_rx_info(skb);
91 rx_size = rx_info->rx_size;
92 rx_buf = skb->data;
93
94 IWM_HEXDUMP(iwm, DBG, SDIO, "RX: ", rx_buf, rx_size);
95 if (iwm_rx_handle(iwm, rx_buf, rx_size) < 0)
96 IWM_WARN(iwm, "RX error\n");
97
98 kfree_skb(skb);
99 }
100}
101
102static void iwm_sdio_isr(struct sdio_func *func)
103{
104 struct iwm_priv *iwm;
105 struct iwm_sdio_priv *hw;
106 struct iwm_rx_info *rx_info;
107 struct sk_buff *skb;
108 unsigned long buf_size, read_size;
109 int ret;
110 u8 val;
111
112 hw = sdio_get_drvdata(func);
113 iwm = hw_to_iwm(hw);
114
115 buf_size = hw->blk_size;
116
117 /* We're checking the status */
118 val = sdio_readb(func, IWM_SDIO_INTR_STATUS_ADDR, &ret);
119 if (val == 0 || ret < 0) {
120 IWM_ERR(iwm, "Wrong INTR_STATUS\n");
121 return;
122 }
123
124 /* See if we have free buffers */
125 if (skb_queue_len(&iwm->rx_list) > IWM_RX_LIST_SIZE) {
126 IWM_ERR(iwm, "No buffer for more Rx frames\n");
127 return;
128 }
129
130 /* We first read the transaction size */
131 read_size = sdio_readb(func, IWM_SDIO_INTR_GET_SIZE_ADDR + 1, &ret);
132 read_size = read_size << 8;
133
134 if (ret < 0) {
135 IWM_ERR(iwm, "Couldn't read the xfer size\n");
136 return;
137 }
138
139 /* We need to clear the INT register */
140 sdio_writeb(func, 1, IWM_SDIO_INTR_CLEAR_ADDR, &ret);
141 if (ret < 0) {
142 IWM_ERR(iwm, "Couldn't clear the INT register\n");
143 return;
144 }
145
146 while (buf_size < read_size)
147 buf_size <<= 1;
148
149 skb = dev_alloc_skb(buf_size);
150 if (!skb) {
151 IWM_ERR(iwm, "Couldn't alloc RX skb\n");
152 return;
153 }
154 rx_info = skb_to_rx_info(skb);
155 rx_info->rx_size = read_size;
156 rx_info->rx_buf_size = buf_size;
157
158 /* Now we can read the actual buffer */
159 ret = sdio_memcpy_fromio(func, skb_put(skb, read_size),
160 IWM_SDIO_DATA_ADDR, read_size);
161
162 /* The skb is put on a driver's specific Rx SKB list */
163 skb_queue_tail(&iwm->rx_list, skb);
164
165 /* We can now schedule the actual worker */
166 queue_work(hw->isr_wq, &hw->isr_worker);
167}
168
169static void iwm_sdio_rx_free(struct iwm_sdio_priv *hw)
170{
171 struct iwm_priv *iwm = hw_to_iwm(hw);
172
173 flush_workqueue(hw->isr_wq);
174
175 skb_queue_purge(&iwm->rx_list);
176}
177
178/* Bus ops */
179static int if_sdio_enable(struct iwm_priv *iwm)
180{
181 struct iwm_sdio_priv *hw = iwm_to_if_sdio(iwm);
182 int ret;
183
184 sdio_claim_host(hw->func);
185
186 ret = sdio_enable_func(hw->func);
187 if (ret) {
188 IWM_ERR(iwm, "Couldn't enable the device: is TOP driver "
189 "loaded and functional?\n");
190 goto release_host;
191 }
192
193 iwm_reset(iwm);
194
195 ret = sdio_claim_irq(hw->func, iwm_sdio_isr);
196 if (ret) {
197 IWM_ERR(iwm, "Failed to claim irq: %d\n", ret);
198 goto release_host;
199 }
200
201 sdio_writeb(hw->func, 1, IWM_SDIO_INTR_ENABLE_ADDR, &ret);
202 if (ret < 0) {
203 IWM_ERR(iwm, "Couldn't enable INTR: %d\n", ret);
204 goto release_irq;
205 }
206
207 sdio_release_host(hw->func);
208
209 IWM_DBG_SDIO(iwm, INFO, "IWM SDIO enable\n");
210
211 return 0;
212
213 release_irq:
214 sdio_release_irq(hw->func);
215 release_host:
216 sdio_release_host(hw->func);
217
218 return ret;
219}
220
221static int if_sdio_disable(struct iwm_priv *iwm)
222{
223 struct iwm_sdio_priv *hw = iwm_to_if_sdio(iwm);
224 int ret;
225
226 iwm_reset(iwm);
227
228 sdio_claim_host(hw->func);
229 sdio_writeb(hw->func, 0, IWM_SDIO_INTR_ENABLE_ADDR, &ret);
230 if (ret < 0)
231 IWM_WARN(iwm, "Couldn't disable INTR: %d\n", ret);
232
233 sdio_release_irq(hw->func);
234 sdio_disable_func(hw->func);
235 sdio_release_host(hw->func);
236
237 iwm_sdio_rx_free(hw);
238
239 IWM_DBG_SDIO(iwm, INFO, "IWM SDIO disable\n");
240
241 return 0;
242}
243
244static int if_sdio_send_chunk(struct iwm_priv *iwm, u8 *buf, int count)
245{
246 struct iwm_sdio_priv *hw = iwm_to_if_sdio(iwm);
247 int aligned_count = ALIGN(count, hw->blk_size);
248 int ret;
249
250 if ((unsigned long)buf & 0x3) {
251 IWM_ERR(iwm, "buf <%p> is not dword aligned\n", buf);
252 /* TODO: Is this a hardware limitation? use get_unligned */
253 return -EINVAL;
254 }
255
256 sdio_claim_host(hw->func);
257 ret = sdio_memcpy_toio(hw->func, IWM_SDIO_DATA_ADDR, buf,
258 aligned_count);
259 sdio_release_host(hw->func);
260
261 return ret;
262}
263
264/* debugfs hooks */
265static int iwm_debugfs_sdio_open(struct inode *inode, struct file *filp)
266{
267 filp->private_data = inode->i_private;
268 return 0;
269}
270
271static ssize_t iwm_debugfs_sdio_read(struct file *filp, char __user *buffer,
272 size_t count, loff_t *ppos)
273{
274 struct iwm_priv *iwm = filp->private_data;
275 struct iwm_sdio_priv *hw = iwm_to_if_sdio(iwm);
276 char *buf;
277 u8 cccr;
278 int buf_len = 4096, ret;
279 size_t len = 0;
280
281 if (*ppos != 0)
282 return 0;
283 if (count < sizeof(buf))
284 return -ENOSPC;
285
286 buf = kzalloc(buf_len, GFP_KERNEL);
287 if (!buf)
288 return -ENOMEM;
289
290 sdio_claim_host(hw->func);
291
292 cccr = sdio_f0_readb(hw->func, SDIO_CCCR_IOEx, &ret);
293 if (ret) {
294 IWM_ERR(iwm, "Could not read SDIO_CCCR_IOEx\n");
295 goto err;
296 }
297 len += snprintf(buf + len, buf_len - len, "CCCR_IOEx: 0x%x\n", cccr);
298
299 cccr = sdio_f0_readb(hw->func, SDIO_CCCR_IORx, &ret);
300 if (ret) {
301 IWM_ERR(iwm, "Could not read SDIO_CCCR_IORx\n");
302 goto err;
303 }
304 len += snprintf(buf + len, buf_len - len, "CCCR_IORx: 0x%x\n", cccr);
305
306
307 cccr = sdio_f0_readb(hw->func, SDIO_CCCR_IENx, &ret);
308 if (ret) {
309 IWM_ERR(iwm, "Could not read SDIO_CCCR_IENx\n");
310 goto err;
311 }
312 len += snprintf(buf + len, buf_len - len, "CCCR_IENx: 0x%x\n", cccr);
313
314
315 cccr = sdio_f0_readb(hw->func, SDIO_CCCR_INTx, &ret);
316 if (ret) {
317 IWM_ERR(iwm, "Could not read SDIO_CCCR_INTx\n");
318 goto err;
319 }
320 len += snprintf(buf + len, buf_len - len, "CCCR_INTx: 0x%x\n", cccr);
321
322
323 cccr = sdio_f0_readb(hw->func, SDIO_CCCR_ABORT, &ret);
324 if (ret) {
325 IWM_ERR(iwm, "Could not read SDIO_CCCR_ABORTx\n");
326 goto err;
327 }
328 len += snprintf(buf + len, buf_len - len, "CCCR_ABORT: 0x%x\n", cccr);
329
330 cccr = sdio_f0_readb(hw->func, SDIO_CCCR_IF, &ret);
331 if (ret) {
332 IWM_ERR(iwm, "Could not read SDIO_CCCR_IF\n");
333 goto err;
334 }
335 len += snprintf(buf + len, buf_len - len, "CCCR_IF: 0x%x\n", cccr);
336
337
338 cccr = sdio_f0_readb(hw->func, SDIO_CCCR_CAPS, &ret);
339 if (ret) {
340 IWM_ERR(iwm, "Could not read SDIO_CCCR_CAPS\n");
341 goto err;
342 }
343 len += snprintf(buf + len, buf_len - len, "CCCR_CAPS: 0x%x\n", cccr);
344
345 cccr = sdio_f0_readb(hw->func, SDIO_CCCR_CIS, &ret);
346 if (ret) {
347 IWM_ERR(iwm, "Could not read SDIO_CCCR_CIS\n");
348 goto err;
349 }
350 len += snprintf(buf + len, buf_len - len, "CCCR_CIS: 0x%x\n", cccr);
351
352 ret = simple_read_from_buffer(buffer, len, ppos, buf, buf_len);
353err:
354 sdio_release_host(hw->func);
355
356 kfree(buf);
357
358 return ret;
359}
360
361static const struct file_operations iwm_debugfs_sdio_fops = {
362 .owner = THIS_MODULE,
363 .open = iwm_debugfs_sdio_open,
364 .read = iwm_debugfs_sdio_read,
365};
366
367static int if_sdio_debugfs_init(struct iwm_priv *iwm, struct dentry *parent_dir)
368{
369 int result;
370 struct iwm_sdio_priv *hw = iwm_to_if_sdio(iwm);
371
372 hw->cccr_dentry = debugfs_create_file("cccr", 0200,
373 parent_dir, iwm,
374 &iwm_debugfs_sdio_fops);
375 result = PTR_ERR(hw->cccr_dentry);
376 if (IS_ERR(hw->cccr_dentry) && (result != -ENODEV)) {
377 IWM_ERR(iwm, "Couldn't create CCCR entry: %d\n", result);
378 return result;
379 }
380
381 return 0;
382}
383
384static void if_sdio_debugfs_exit(struct iwm_priv *iwm)
385{
386 struct iwm_sdio_priv *hw = iwm_to_if_sdio(iwm);
387
388 debugfs_remove(hw->cccr_dentry);
389}
390
391static struct iwm_if_ops if_sdio_ops = {
392 .enable = if_sdio_enable,
393 .disable = if_sdio_disable,
394 .send_chunk = if_sdio_send_chunk,
395 .debugfs_init = if_sdio_debugfs_init,
396 .debugfs_exit = if_sdio_debugfs_exit,
397 .umac_name = "iwmc3200wifi-umac-sdio.bin",
398 .calib_lmac_name = "iwmc3200wifi-lmac-calib-sdio.bin",
399 .lmac_name = "iwmc3200wifi-lmac-sdio.bin",
400};
401
402static int iwm_sdio_probe(struct sdio_func *func,
403 const struct sdio_device_id *id)
404{
405 struct iwm_priv *iwm;
406 struct iwm_sdio_priv *hw;
407 struct device *dev = &func->dev;
408 int ret;
409
410 /* check if TOP has already initialized the card */
411 sdio_claim_host(func);
412 ret = sdio_enable_func(func);
413 if (ret) {
414 dev_err(dev, "wait for TOP to enable the device\n");
415 sdio_release_host(func);
416 return ret;
417 }
418
419 ret = sdio_set_block_size(func, IWM_SDIO_BLK_SIZE);
420
421 sdio_disable_func(func);
422 sdio_release_host(func);
423
424 if (ret < 0) {
425 dev_err(dev, "Failed to set block size: %d\n", ret);
426 return ret;
427 }
428
429 iwm = iwm_if_alloc(sizeof(struct iwm_sdio_priv), dev, &if_sdio_ops);
430 if (IS_ERR(iwm)) {
431 dev_err(dev, "allocate SDIO interface failed\n");
432 return PTR_ERR(iwm);
433 }
434
435 hw = iwm_private(iwm);
436 hw->iwm = iwm;
437
438 ret = iwm_debugfs_init(iwm);
439 if (ret < 0) {
440 IWM_ERR(iwm, "Debugfs registration failed\n");
441 goto if_free;
442 }
443
444 sdio_set_drvdata(func, hw);
445
446 hw->func = func;
447 hw->blk_size = IWM_SDIO_BLK_SIZE;
448
449 hw->isr_wq = create_singlethread_workqueue(KBUILD_MODNAME "_sdio");
450 if (!hw->isr_wq) {
451 ret = -ENOMEM;
452 goto debugfs_exit;
453 }
454
455 INIT_WORK(&hw->isr_worker, iwm_sdio_isr_worker);
456
457 dev_info(dev, "IWM SDIO probe\n");
458
459 return 0;
460
461 debugfs_exit:
462 iwm_debugfs_exit(iwm);
463 if_free:
464 iwm_if_free(iwm);
465 return ret;
466}
467
468static void iwm_sdio_remove(struct sdio_func *func)
469{
470 struct iwm_sdio_priv *hw = sdio_get_drvdata(func);
471 struct iwm_priv *iwm = hw_to_iwm(hw);
472 struct device *dev = &func->dev;
473
474 iwm_debugfs_exit(iwm);
475 iwm_if_free(iwm);
476 destroy_workqueue(hw->isr_wq);
477
478 sdio_set_drvdata(func, NULL);
479
480 dev_info(dev, "IWM SDIO remove\n");
481
482 return;
483}
484
485static const struct sdio_device_id iwm_sdio_ids[] = {
486 { SDIO_DEVICE(SDIO_VENDOR_ID_INTEL, SDIO_DEVICE_ID_IWM) },
487 { /* end: all zeroes */ },
488};
489MODULE_DEVICE_TABLE(sdio, iwm_sdio_ids);
490
491static struct sdio_driver iwm_sdio_driver = {
492 .name = "iwm_sdio",
493 .id_table = iwm_sdio_ids,
494 .probe = iwm_sdio_probe,
495 .remove = iwm_sdio_remove,
496};
497
498static int __init iwm_sdio_init_module(void)
499{
500 int ret;
501
502 ret = sdio_register_driver(&iwm_sdio_driver);
503
504 return ret;
505}
506
507static void __exit iwm_sdio_exit_module(void)
508{
509 sdio_unregister_driver(&iwm_sdio_driver);
510}
511
512module_init(iwm_sdio_init_module);
513module_exit(iwm_sdio_exit_module);
514
515MODULE_LICENSE("GPL");
516MODULE_AUTHOR(IWM_COPYRIGHT " " IWM_AUTHOR);