PageRenderTime 58ms CodeModel.GetById 27ms RepoModel.GetById 1ms app.codeStats 0ms

/drivers/net/wireless/iwmc3200wifi/sdio.c

https://github.com/xianai/Ultra-KSM-for-Linux-2.6.33.2-
C | 528 lines | 353 code | 100 blank | 75 comment | 36 complexity | cac768f6a51240f1260854001b842b16 MD5 | raw file
  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. * This is the SDIO bus specific hooks for iwm.
  40. * It also is the module's entry point.
  41. *
  42. * Interesting code paths:
  43. * iwm_sdio_probe() (Called by an SDIO bus scan)
  44. * -> iwm_if_alloc() (netdev.c)
  45. * -> iwm_wdev_alloc() (cfg80211.c, allocates and register our wiphy)
  46. * -> wiphy_new()
  47. * -> wiphy_register()
  48. * -> alloc_netdev_mq()
  49. * -> register_netdev()
  50. *
  51. * iwm_sdio_remove()
  52. * -> iwm_if_free() (netdev.c)
  53. * -> unregister_netdev()
  54. * -> iwm_wdev_free() (cfg80211.c)
  55. * -> wiphy_unregister()
  56. * -> wiphy_free()
  57. *
  58. * iwm_sdio_isr() (called in process context from the SDIO core code)
  59. * -> queue_work(.., isr_worker)
  60. * -- [async] --> iwm_sdio_isr_worker()
  61. * -> iwm_rx_handle()
  62. */
  63. #include <linux/kernel.h>
  64. #include <linux/netdevice.h>
  65. #include <linux/debugfs.h>
  66. #include <linux/mmc/sdio_ids.h>
  67. #include <linux/mmc/sdio.h>
  68. #include <linux/mmc/sdio_func.h>
  69. #include "iwm.h"
  70. #include "debug.h"
  71. #include "bus.h"
  72. #include "sdio.h"
  73. static void iwm_sdio_isr_worker(struct work_struct *work)
  74. {
  75. struct iwm_sdio_priv *hw;
  76. struct iwm_priv *iwm;
  77. struct iwm_rx_info *rx_info;
  78. struct sk_buff *skb;
  79. u8 *rx_buf;
  80. unsigned long rx_size;
  81. hw = container_of(work, struct iwm_sdio_priv, isr_worker);
  82. iwm = hw_to_iwm(hw);
  83. while (!skb_queue_empty(&iwm->rx_list)) {
  84. skb = skb_dequeue(&iwm->rx_list);
  85. rx_info = skb_to_rx_info(skb);
  86. rx_size = rx_info->rx_size;
  87. rx_buf = skb->data;
  88. IWM_HEXDUMP(iwm, DBG, SDIO, "RX: ", rx_buf, rx_size);
  89. if (iwm_rx_handle(iwm, rx_buf, rx_size) < 0)
  90. IWM_WARN(iwm, "RX error\n");
  91. kfree_skb(skb);
  92. }
  93. }
  94. static void iwm_sdio_isr(struct sdio_func *func)
  95. {
  96. struct iwm_priv *iwm;
  97. struct iwm_sdio_priv *hw;
  98. struct iwm_rx_info *rx_info;
  99. struct sk_buff *skb;
  100. unsigned long buf_size, read_size;
  101. int ret;
  102. u8 val;
  103. hw = sdio_get_drvdata(func);
  104. iwm = hw_to_iwm(hw);
  105. buf_size = hw->blk_size;
  106. /* We're checking the status */
  107. val = sdio_readb(func, IWM_SDIO_INTR_STATUS_ADDR, &ret);
  108. if (val == 0 || ret < 0) {
  109. IWM_ERR(iwm, "Wrong INTR_STATUS\n");
  110. return;
  111. }
  112. /* See if we have free buffers */
  113. if (skb_queue_len(&iwm->rx_list) > IWM_RX_LIST_SIZE) {
  114. IWM_ERR(iwm, "No buffer for more Rx frames\n");
  115. return;
  116. }
  117. /* We first read the transaction size */
  118. read_size = sdio_readb(func, IWM_SDIO_INTR_GET_SIZE_ADDR + 1, &ret);
  119. read_size = read_size << 8;
  120. if (ret < 0) {
  121. IWM_ERR(iwm, "Couldn't read the xfer size\n");
  122. return;
  123. }
  124. /* We need to clear the INT register */
  125. sdio_writeb(func, 1, IWM_SDIO_INTR_CLEAR_ADDR, &ret);
  126. if (ret < 0) {
  127. IWM_ERR(iwm, "Couldn't clear the INT register\n");
  128. return;
  129. }
  130. while (buf_size < read_size)
  131. buf_size <<= 1;
  132. skb = dev_alloc_skb(buf_size);
  133. if (!skb) {
  134. IWM_ERR(iwm, "Couldn't alloc RX skb\n");
  135. return;
  136. }
  137. rx_info = skb_to_rx_info(skb);
  138. rx_info->rx_size = read_size;
  139. rx_info->rx_buf_size = buf_size;
  140. /* Now we can read the actual buffer */
  141. ret = sdio_memcpy_fromio(func, skb_put(skb, read_size),
  142. IWM_SDIO_DATA_ADDR, read_size);
  143. /* The skb is put on a driver's specific Rx SKB list */
  144. skb_queue_tail(&iwm->rx_list, skb);
  145. /* We can now schedule the actual worker */
  146. queue_work(hw->isr_wq, &hw->isr_worker);
  147. }
  148. static void iwm_sdio_rx_free(struct iwm_sdio_priv *hw)
  149. {
  150. struct iwm_priv *iwm = hw_to_iwm(hw);
  151. flush_workqueue(hw->isr_wq);
  152. skb_queue_purge(&iwm->rx_list);
  153. }
  154. /* Bus ops */
  155. static int if_sdio_enable(struct iwm_priv *iwm)
  156. {
  157. struct iwm_sdio_priv *hw = iwm_to_if_sdio(iwm);
  158. int ret;
  159. sdio_claim_host(hw->func);
  160. ret = sdio_enable_func(hw->func);
  161. if (ret) {
  162. IWM_ERR(iwm, "Couldn't enable the device: is TOP driver "
  163. "loaded and functional?\n");
  164. goto release_host;
  165. }
  166. iwm_reset(iwm);
  167. ret = sdio_claim_irq(hw->func, iwm_sdio_isr);
  168. if (ret) {
  169. IWM_ERR(iwm, "Failed to claim irq: %d\n", ret);
  170. goto release_host;
  171. }
  172. sdio_writeb(hw->func, 1, IWM_SDIO_INTR_ENABLE_ADDR, &ret);
  173. if (ret < 0) {
  174. IWM_ERR(iwm, "Couldn't enable INTR: %d\n", ret);
  175. goto release_irq;
  176. }
  177. sdio_release_host(hw->func);
  178. IWM_DBG_SDIO(iwm, INFO, "IWM SDIO enable\n");
  179. return 0;
  180. release_irq:
  181. sdio_release_irq(hw->func);
  182. release_host:
  183. sdio_release_host(hw->func);
  184. return ret;
  185. }
  186. static int if_sdio_disable(struct iwm_priv *iwm)
  187. {
  188. struct iwm_sdio_priv *hw = iwm_to_if_sdio(iwm);
  189. int ret;
  190. sdio_claim_host(hw->func);
  191. sdio_writeb(hw->func, 0, IWM_SDIO_INTR_ENABLE_ADDR, &ret);
  192. if (ret < 0)
  193. IWM_WARN(iwm, "Couldn't disable INTR: %d\n", ret);
  194. sdio_release_irq(hw->func);
  195. sdio_disable_func(hw->func);
  196. sdio_release_host(hw->func);
  197. iwm_sdio_rx_free(hw);
  198. iwm_reset(iwm);
  199. IWM_DBG_SDIO(iwm, INFO, "IWM SDIO disable\n");
  200. return 0;
  201. }
  202. static int if_sdio_send_chunk(struct iwm_priv *iwm, u8 *buf, int count)
  203. {
  204. struct iwm_sdio_priv *hw = iwm_to_if_sdio(iwm);
  205. int aligned_count = ALIGN(count, hw->blk_size);
  206. int ret;
  207. if ((unsigned long)buf & 0x3) {
  208. IWM_ERR(iwm, "buf <%p> is not dword aligned\n", buf);
  209. /* TODO: Is this a hardware limitation? use get_unligned */
  210. return -EINVAL;
  211. }
  212. sdio_claim_host(hw->func);
  213. ret = sdio_memcpy_toio(hw->func, IWM_SDIO_DATA_ADDR, buf,
  214. aligned_count);
  215. sdio_release_host(hw->func);
  216. return ret;
  217. }
  218. /* debugfs hooks */
  219. static int iwm_debugfs_sdio_open(struct inode *inode, struct file *filp)
  220. {
  221. filp->private_data = inode->i_private;
  222. return 0;
  223. }
  224. static ssize_t iwm_debugfs_sdio_read(struct file *filp, char __user *buffer,
  225. size_t count, loff_t *ppos)
  226. {
  227. struct iwm_priv *iwm = filp->private_data;
  228. struct iwm_sdio_priv *hw = iwm_to_if_sdio(iwm);
  229. char *buf;
  230. u8 cccr;
  231. int buf_len = 4096, ret;
  232. size_t len = 0;
  233. if (*ppos != 0)
  234. return 0;
  235. if (count < sizeof(buf))
  236. return -ENOSPC;
  237. buf = kzalloc(buf_len, GFP_KERNEL);
  238. if (!buf)
  239. return -ENOMEM;
  240. sdio_claim_host(hw->func);
  241. cccr = sdio_f0_readb(hw->func, SDIO_CCCR_IOEx, &ret);
  242. if (ret) {
  243. IWM_ERR(iwm, "Could not read SDIO_CCCR_IOEx\n");
  244. goto err;
  245. }
  246. len += snprintf(buf + len, buf_len - len, "CCCR_IOEx: 0x%x\n", cccr);
  247. cccr = sdio_f0_readb(hw->func, SDIO_CCCR_IORx, &ret);
  248. if (ret) {
  249. IWM_ERR(iwm, "Could not read SDIO_CCCR_IORx\n");
  250. goto err;
  251. }
  252. len += snprintf(buf + len, buf_len - len, "CCCR_IORx: 0x%x\n", cccr);
  253. cccr = sdio_f0_readb(hw->func, SDIO_CCCR_IENx, &ret);
  254. if (ret) {
  255. IWM_ERR(iwm, "Could not read SDIO_CCCR_IENx\n");
  256. goto err;
  257. }
  258. len += snprintf(buf + len, buf_len - len, "CCCR_IENx: 0x%x\n", cccr);
  259. cccr = sdio_f0_readb(hw->func, SDIO_CCCR_INTx, &ret);
  260. if (ret) {
  261. IWM_ERR(iwm, "Could not read SDIO_CCCR_INTx\n");
  262. goto err;
  263. }
  264. len += snprintf(buf + len, buf_len - len, "CCCR_INTx: 0x%x\n", cccr);
  265. cccr = sdio_f0_readb(hw->func, SDIO_CCCR_ABORT, &ret);
  266. if (ret) {
  267. IWM_ERR(iwm, "Could not read SDIO_CCCR_ABORTx\n");
  268. goto err;
  269. }
  270. len += snprintf(buf + len, buf_len - len, "CCCR_ABORT: 0x%x\n", cccr);
  271. cccr = sdio_f0_readb(hw->func, SDIO_CCCR_IF, &ret);
  272. if (ret) {
  273. IWM_ERR(iwm, "Could not read SDIO_CCCR_IF\n");
  274. goto err;
  275. }
  276. len += snprintf(buf + len, buf_len - len, "CCCR_IF: 0x%x\n", cccr);
  277. cccr = sdio_f0_readb(hw->func, SDIO_CCCR_CAPS, &ret);
  278. if (ret) {
  279. IWM_ERR(iwm, "Could not read SDIO_CCCR_CAPS\n");
  280. goto err;
  281. }
  282. len += snprintf(buf + len, buf_len - len, "CCCR_CAPS: 0x%x\n", cccr);
  283. cccr = sdio_f0_readb(hw->func, SDIO_CCCR_CIS, &ret);
  284. if (ret) {
  285. IWM_ERR(iwm, "Could not read SDIO_CCCR_CIS\n");
  286. goto err;
  287. }
  288. len += snprintf(buf + len, buf_len - len, "CCCR_CIS: 0x%x\n", cccr);
  289. ret = simple_read_from_buffer(buffer, len, ppos, buf, buf_len);
  290. err:
  291. sdio_release_host(hw->func);
  292. kfree(buf);
  293. return ret;
  294. }
  295. static const struct file_operations iwm_debugfs_sdio_fops = {
  296. .owner = THIS_MODULE,
  297. .open = iwm_debugfs_sdio_open,
  298. .read = iwm_debugfs_sdio_read,
  299. };
  300. static int if_sdio_debugfs_init(struct iwm_priv *iwm, struct dentry *parent_dir)
  301. {
  302. int result;
  303. struct iwm_sdio_priv *hw = iwm_to_if_sdio(iwm);
  304. hw->cccr_dentry = debugfs_create_file("cccr", 0200,
  305. parent_dir, iwm,
  306. &iwm_debugfs_sdio_fops);
  307. result = PTR_ERR(hw->cccr_dentry);
  308. if (IS_ERR(hw->cccr_dentry) && (result != -ENODEV)) {
  309. IWM_ERR(iwm, "Couldn't create CCCR entry: %d\n", result);
  310. return result;
  311. }
  312. return 0;
  313. }
  314. static void if_sdio_debugfs_exit(struct iwm_priv *iwm)
  315. {
  316. struct iwm_sdio_priv *hw = iwm_to_if_sdio(iwm);
  317. debugfs_remove(hw->cccr_dentry);
  318. }
  319. static struct iwm_if_ops if_sdio_ops = {
  320. .enable = if_sdio_enable,
  321. .disable = if_sdio_disable,
  322. .send_chunk = if_sdio_send_chunk,
  323. .debugfs_init = if_sdio_debugfs_init,
  324. .debugfs_exit = if_sdio_debugfs_exit,
  325. .umac_name = "iwmc3200wifi-umac-sdio.bin",
  326. .calib_lmac_name = "iwmc3200wifi-calib-sdio.bin",
  327. .lmac_name = "iwmc3200wifi-lmac-sdio.bin",
  328. };
  329. MODULE_FIRMWARE("iwmc3200wifi-umac-sdio.bin");
  330. MODULE_FIRMWARE("iwmc3200wifi-calib-sdio.bin");
  331. MODULE_FIRMWARE("iwmc3200wifi-lmac-sdio.bin");
  332. static int iwm_sdio_probe(struct sdio_func *func,
  333. const struct sdio_device_id *id)
  334. {
  335. struct iwm_priv *iwm;
  336. struct iwm_sdio_priv *hw;
  337. struct device *dev = &func->dev;
  338. int ret;
  339. /* check if TOP has already initialized the card */
  340. sdio_claim_host(func);
  341. ret = sdio_enable_func(func);
  342. if (ret) {
  343. dev_err(dev, "wait for TOP to enable the device\n");
  344. sdio_release_host(func);
  345. return ret;
  346. }
  347. ret = sdio_set_block_size(func, IWM_SDIO_BLK_SIZE);
  348. sdio_disable_func(func);
  349. sdio_release_host(func);
  350. if (ret < 0) {
  351. dev_err(dev, "Failed to set block size: %d\n", ret);
  352. return ret;
  353. }
  354. iwm = iwm_if_alloc(sizeof(struct iwm_sdio_priv), dev, &if_sdio_ops);
  355. if (IS_ERR(iwm)) {
  356. dev_err(dev, "allocate SDIO interface failed\n");
  357. return PTR_ERR(iwm);
  358. }
  359. hw = iwm_private(iwm);
  360. hw->iwm = iwm;
  361. ret = iwm_debugfs_init(iwm);
  362. if (ret < 0) {
  363. IWM_ERR(iwm, "Debugfs registration failed\n");
  364. goto if_free;
  365. }
  366. sdio_set_drvdata(func, hw);
  367. hw->func = func;
  368. hw->blk_size = IWM_SDIO_BLK_SIZE;
  369. hw->isr_wq = create_singlethread_workqueue(KBUILD_MODNAME "_sdio");
  370. if (!hw->isr_wq) {
  371. ret = -ENOMEM;
  372. goto debugfs_exit;
  373. }
  374. INIT_WORK(&hw->isr_worker, iwm_sdio_isr_worker);
  375. ret = iwm_if_add(iwm);
  376. if (ret) {
  377. dev_err(dev, "add SDIO interface failed\n");
  378. goto destroy_wq;
  379. }
  380. dev_info(dev, "IWM SDIO probe\n");
  381. return 0;
  382. destroy_wq:
  383. destroy_workqueue(hw->isr_wq);
  384. debugfs_exit:
  385. iwm_debugfs_exit(iwm);
  386. if_free:
  387. iwm_if_free(iwm);
  388. return ret;
  389. }
  390. static void iwm_sdio_remove(struct sdio_func *func)
  391. {
  392. struct iwm_sdio_priv *hw = sdio_get_drvdata(func);
  393. struct iwm_priv *iwm = hw_to_iwm(hw);
  394. struct device *dev = &func->dev;
  395. iwm_if_remove(iwm);
  396. destroy_workqueue(hw->isr_wq);
  397. iwm_debugfs_exit(iwm);
  398. iwm_if_free(iwm);
  399. sdio_set_drvdata(func, NULL);
  400. dev_info(dev, "IWM SDIO remove\n");
  401. return;
  402. }
  403. static const struct sdio_device_id iwm_sdio_ids[] = {
  404. /* Global/AGN SKU */
  405. { SDIO_DEVICE(SDIO_VENDOR_ID_INTEL, 0x1403) },
  406. /* BGN SKU */
  407. { SDIO_DEVICE(SDIO_VENDOR_ID_INTEL, 0x1408) },
  408. { /* end: all zeroes */ },
  409. };
  410. MODULE_DEVICE_TABLE(sdio, iwm_sdio_ids);
  411. static struct sdio_driver iwm_sdio_driver = {
  412. .name = "iwm_sdio",
  413. .id_table = iwm_sdio_ids,
  414. .probe = iwm_sdio_probe,
  415. .remove = iwm_sdio_remove,
  416. };
  417. static int __init iwm_sdio_init_module(void)
  418. {
  419. return sdio_register_driver(&iwm_sdio_driver);
  420. }
  421. static void __exit iwm_sdio_exit_module(void)
  422. {
  423. sdio_unregister_driver(&iwm_sdio_driver);
  424. }
  425. module_init(iwm_sdio_init_module);
  426. module_exit(iwm_sdio_exit_module);
  427. MODULE_LICENSE("GPL");
  428. MODULE_AUTHOR(IWM_COPYRIGHT " " IWM_AUTHOR);