]> nv-tegra.nvidia Code Review - linux-2.6.git/blob - drivers/net/wireless/bcmdhd/bcmsdh_sdmmc_linux.c
a9b6629f386bab90558f800cc08b3d4f99cbc787
[linux-2.6.git] / drivers / net / wireless / bcmdhd / bcmsdh_sdmmc_linux.c
1 /*
2  * BCMSDH Function Driver for the native SDIO/MMC driver in the Linux Kernel
3  *
4  * Copyright (C) 1999-2011, Broadcom Corporation
5  * 
6  *         Unless you and Broadcom execute a separate written software license
7  * agreement governing use of this software, this software is licensed to you
8  * under the terms of the GNU General Public License version 2 (the "GPL"),
9  * available at http://www.broadcom.com/licenses/GPLv2.php, with the
10  * following added to such license:
11  * 
12  *      As a special exception, the copyright holders of this software give you
13  * permission to link this software with independent modules, and to copy and
14  * distribute the resulting executable under terms of your choice, provided that
15  * you also meet, for each linked independent module, the terms and conditions of
16  * the license of that module.  An independent module is a module which is not
17  * derived from this software.  The special exception does not apply to any
18  * modifications of the software.
19  * 
20  *      Notwithstanding the above, under no circumstances may you combine this
21  * software in any way with any other Broadcom software provided under a license
22  * other than the GPL, without Broadcom's express prior written consent.
23  *
24  * $Id: bcmsdh_sdmmc_linux.c 312783 2012-02-03 22:53:56Z $
25  */
26
27 #include <typedefs.h>
28 #include <bcmutils.h>
29 #include <sdio.h>       /* SDIO Device and Protocol Specs */
30 #include <bcmsdbus.h>   /* bcmsdh to/from specific controller APIs */
31 #include <sdiovar.h>    /* to get msglevel bit values */
32
33 #include <linux/sched.h>        /* request_irq() */
34
35 #include <linux/mmc/core.h>
36 #include <linux/mmc/card.h>
37 #include <linux/mmc/sdio_func.h>
38 #include <linux/mmc/sdio_ids.h>
39
40 #if !defined(SDIO_VENDOR_ID_BROADCOM)
41 #define SDIO_VENDOR_ID_BROADCOM         0x02d0
42 #endif /* !defined(SDIO_VENDOR_ID_BROADCOM) */
43
44 #define SDIO_DEVICE_ID_BROADCOM_DEFAULT 0x0000
45
46 #if !defined(SDIO_DEVICE_ID_BROADCOM_4325_SDGWB)
47 #define SDIO_DEVICE_ID_BROADCOM_4325_SDGWB      0x0492  /* BCM94325SDGWB */
48 #endif /* !defined(SDIO_DEVICE_ID_BROADCOM_4325_SDGWB) */
49 #if !defined(SDIO_DEVICE_ID_BROADCOM_4325)
50 #define SDIO_DEVICE_ID_BROADCOM_4325    0x0493
51 #endif /* !defined(SDIO_DEVICE_ID_BROADCOM_4325) */
52 #if !defined(SDIO_DEVICE_ID_BROADCOM_4319)
53 #define SDIO_DEVICE_ID_BROADCOM_4319    0x4319
54 #endif /* !defined(SDIO_DEVICE_ID_BROADCOM_4319) */
55 #if !defined(SDIO_DEVICE_ID_BROADCOM_4330)
56 #define SDIO_DEVICE_ID_BROADCOM_4330    0x4330
57 #endif /* !defined(SDIO_DEVICE_ID_BROADCOM_4330) */
58 #if !defined(SDIO_DEVICE_ID_BROADCOM_4334)
59 #define SDIO_DEVICE_ID_BROADCOM_4334    0x4334
60 #endif /* !defined(SDIO_DEVICE_ID_BROADCOM_4334) */
61 #if !defined(SDIO_DEVICE_ID_BROADCOM_4324)
62 #define SDIO_DEVICE_ID_BROADCOM_4324    0x4324
63 #endif /* !defined(SDIO_DEVICE_ID_BROADCOM_4324) */
64 #if !defined(SDIO_DEVICE_ID_BROADCOM_43239)
65 #define SDIO_DEVICE_ID_BROADCOM_43239    43239
66 #endif /* !defined(SDIO_DEVICE_ID_BROADCOM_43239) */
67
68 #include <bcmsdh_sdmmc.h>
69
70 #include <dhd_dbg.h>
71
72 #ifdef WL_CFG80211
73 extern void wl_cfg80211_set_parent_dev(void *dev);
74 #endif
75
76 extern void sdioh_sdmmc_devintr_off(sdioh_info_t *sd);
77 extern void sdioh_sdmmc_devintr_on(sdioh_info_t *sd);
78 extern int dhd_os_check_wakelock(void *dhdp);
79 extern int dhd_os_check_if_up(void *dhdp);
80 extern void *bcmsdh_get_drvdata(void);
81
82 int sdio_function_init(void);
83 void sdio_function_cleanup(void);
84
85 #define DESCRIPTION "bcmsdh_sdmmc Driver"
86 #define AUTHOR "Broadcom Corporation"
87
88 /* module param defaults */
89 static int clockoverride = 0;
90
91 module_param(clockoverride, int, 0644);
92 MODULE_PARM_DESC(clockoverride, "SDIO card clock override");
93
94 PBCMSDH_SDMMC_INSTANCE gInstance;
95
96 /* Maximum number of bcmsdh_sdmmc devices supported by driver */
97 #define BCMSDH_SDMMC_MAX_DEVICES 1
98
99 extern int bcmsdh_probe_bcmdhd(struct device *dev);
100 extern int bcmsdh_remove_bcmdhd(struct device *dev);
101
102 extern volatile bool dhd_mmc_suspend;
103
104 static int bcmsdh_sdmmc_probe(struct sdio_func *func,
105                               const struct sdio_device_id *id)
106 {
107         int ret = 0;
108         static struct sdio_func sdio_func_0;
109         sd_trace(("bcmsdh_sdmmc: %s Enter\n", __FUNCTION__));
110         sd_trace(("sdio_bcmsdh: func->class=%x\n", func->class));
111         sd_trace(("sdio_vendor: 0x%04x\n", func->vendor));
112         sd_trace(("sdio_device: 0x%04x\n", func->device));
113         sd_trace(("Function#: 0x%04x\n", func->num));
114
115         if (func->num == 1) {
116                 sdio_func_0.num = 0;
117                 sdio_func_0.card = func->card;
118                 gInstance->func[0] = &sdio_func_0;
119                 if(func->device == 0x4) { /* 4318 */
120                         gInstance->func[2] = NULL;
121                         sd_trace(("NIC found, calling bcmsdh_probe_bcmdhd...\n"));
122                         ret = bcmsdh_probe_bcmdhd(&func->dev);
123                 }
124         }
125
126         gInstance->func[func->num] = func;
127
128         if (func->num == 2) {
129 #ifdef WL_CFG80211
130                 wl_cfg80211_set_parent_dev(&func->dev);
131 #endif
132                 sd_trace(("F2 found, calling bcmsdh_probe_bcmdhd...\n"));
133                 ret = bcmsdh_probe_bcmdhd(&func->dev);
134         }
135
136         return ret;
137 }
138
139 static void bcmsdh_sdmmc_remove(struct sdio_func *func)
140 {
141         sd_trace(("bcmsdh_sdmmc: %s Enter\n", __FUNCTION__));
142         sd_info(("sdio_bcmsdh: func->class=%x\n", func->class));
143         sd_info(("sdio_vendor: 0x%04x\n", func->vendor));
144         sd_info(("sdio_device: 0x%04x\n", func->device));
145         sd_info(("Function#: 0x%04x\n", func->num));
146
147         if (func->num == 2) {
148                 sd_trace(("F2 found, calling bcmsdh_remove_bcmdhd...\n"));
149                 bcmsdh_remove_bcmdhd(&func->dev);
150         } else if (func->num == 1) {
151                 sdio_claim_host(func);
152                 sdio_disable_func(func);
153                 sdio_release_host(func);
154                 gInstance->func[1] = NULL;
155         }
156 }
157
158 /* devices we support, null terminated */
159 static const struct sdio_device_id bcmsdh_sdmmc_ids[] = {
160         { SDIO_DEVICE(SDIO_VENDOR_ID_BROADCOM, SDIO_DEVICE_ID_BROADCOM_DEFAULT) },
161         { SDIO_DEVICE(SDIO_VENDOR_ID_BROADCOM, SDIO_DEVICE_ID_BROADCOM_4325_SDGWB) },
162         { SDIO_DEVICE(SDIO_VENDOR_ID_BROADCOM, SDIO_DEVICE_ID_BROADCOM_4325) },
163         { SDIO_DEVICE(SDIO_VENDOR_ID_BROADCOM, SDIO_DEVICE_ID_BROADCOM_4319) },
164         { SDIO_DEVICE(SDIO_VENDOR_ID_BROADCOM, SDIO_DEVICE_ID_BROADCOM_4330) },
165         { SDIO_DEVICE(SDIO_VENDOR_ID_BROADCOM, SDIO_DEVICE_ID_BROADCOM_4334) },
166         { SDIO_DEVICE(SDIO_VENDOR_ID_BROADCOM, SDIO_DEVICE_ID_BROADCOM_4324) },
167         { SDIO_DEVICE(SDIO_VENDOR_ID_BROADCOM, SDIO_DEVICE_ID_BROADCOM_43239) },
168         { SDIO_DEVICE_CLASS(SDIO_CLASS_NONE)            },
169         { /* end: all zeroes */                         },
170 };
171
172 MODULE_DEVICE_TABLE(sdio, bcmsdh_sdmmc_ids);
173
174 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 39)) && defined(CONFIG_PM)
175 static int bcmsdh_sdmmc_suspend(struct device *pdev)
176 {
177         struct sdio_func *func = dev_to_sdio_func(pdev);
178         mmc_pm_flag_t sdio_flags;
179         int ret = 0;
180
181         if (func->num != 2)
182                 return 0;
183
184         sd_trace(("%s Enter\n", __FUNCTION__));
185
186         if (dhd_os_check_wakelock(bcmsdh_get_drvdata()))
187                 return -EBUSY;
188
189
190         sdio_flags = sdio_get_host_pm_caps(func);
191
192         if (!(sdio_flags & MMC_PM_KEEP_POWER)) {
193                 sd_err(("%s: can't keep power while host is suspended\n", __FUNCTION__));
194                 return  -EINVAL;
195         }
196
197         /* keep power while host suspended */
198         ret = sdio_set_host_pm_flags(func, MMC_PM_KEEP_POWER);
199         if (ret) {
200                 sd_err(("%s: error while trying to keep power\n", __FUNCTION__));
201                 return ret;
202         }
203
204 #if defined(OOB_INTR_ONLY)
205         bcmsdh_oob_intr_set(0);
206 #endif /* defined(OOB_INTR_ONLY) */
207         smp_mb();
208
209         sdio_flags = sdio_get_host_pm_caps(func);
210
211         if (!(sdio_flags & MMC_PM_KEEP_POWER)) {
212                 sd_err(("can't keep power while host "
213                                 "is suspended\n", __FUNCTION__));
214                 ret = -EINVAL;
215                 goto out;
216         }
217
218         /* keep power while host suspended */
219         ret = sdio_set_host_pm_flags(func, MMC_PM_KEEP_POWER);
220         if (ret) {
221                 sd_err(("error while trying to keep power\n", __FUNCTION__));
222                 goto out;
223         }
224
225         dhd_mmc_suspend = TRUE;
226
227 out:
228         return ret;
229 }
230
231 static int bcmsdh_sdmmc_resume(struct device *pdev)
232 {
233 #if defined(OOB_INTR_ONLY)
234         struct sdio_func *func = dev_to_sdio_func(pdev);
235 #endif
236         sd_trace(("%s Enter\n", __FUNCTION__));
237         dhd_mmc_suspend = FALSE;
238 #if defined(OOB_INTR_ONLY)
239         if ((func->num == 2) && dhd_os_check_if_up(bcmsdh_get_drvdata()))
240                 bcmsdh_oob_intr_set(1);
241 #endif /* (OOB_INTR_ONLY) */
242
243         smp_mb();
244         return 0;
245 }
246
247 static const struct dev_pm_ops bcmsdh_sdmmc_pm_ops = {
248         .suspend        = bcmsdh_sdmmc_suspend,
249         .resume         = bcmsdh_sdmmc_resume,
250 };
251 #endif  /* (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 39)) && defined(CONFIG_PM) */
252
253 static struct sdio_driver bcmsdh_sdmmc_driver = {
254         .probe          = bcmsdh_sdmmc_probe,
255         .remove         = bcmsdh_sdmmc_remove,
256         .name           = "bcmsdh_sdmmc",
257         .id_table       = bcmsdh_sdmmc_ids,
258 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 39)) && defined(CONFIG_PM)
259         .drv = {
260                 .pm     = &bcmsdh_sdmmc_pm_ops,
261         },
262 #endif /* (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 39)) && defined(CONFIG_PM) */
263 };
264
265 struct sdos_info {
266         sdioh_info_t *sd;
267         spinlock_t lock;
268 };
269
270
271 int
272 sdioh_sdmmc_osinit(sdioh_info_t *sd)
273 {
274         struct sdos_info *sdos;
275
276         sdos = (struct sdos_info*)MALLOC(sd->osh, sizeof(struct sdos_info));
277         sd->sdos_info = (void*)sdos;
278         if (sdos == NULL)
279                 return BCME_NOMEM;
280
281         sdos->sd = sd;
282         spin_lock_init(&sdos->lock);
283         return BCME_OK;
284 }
285
286 void
287 sdioh_sdmmc_osfree(sdioh_info_t *sd)
288 {
289         struct sdos_info *sdos;
290         ASSERT(sd && sd->sdos_info);
291
292         sdos = (struct sdos_info *)sd->sdos_info;
293         MFREE(sd->osh, sdos, sizeof(struct sdos_info));
294 }
295
296 /* Interrupt enable/disable */
297 SDIOH_API_RC
298 sdioh_interrupt_set(sdioh_info_t *sd, bool enable)
299 {
300         ulong flags;
301         struct sdos_info *sdos;
302
303         sd_trace(("%s: %s\n", __FUNCTION__, enable ? "Enabling" : "Disabling"));
304
305         sdos = (struct sdos_info *)sd->sdos_info;
306         ASSERT(sdos);
307
308 #if !defined(OOB_INTR_ONLY)
309         if (enable && !(sd->intr_handler && sd->intr_handler_arg)) {
310                 sd_err(("%s: no handler registered, will not enable\n", __FUNCTION__));
311                 return SDIOH_API_RC_FAIL;
312         }
313 #endif /* !defined(OOB_INTR_ONLY) */
314
315         /* Ensure atomicity for enable/disable calls */
316         spin_lock_irqsave(&sdos->lock, flags);
317
318         sd->client_intr_enabled = enable;
319         if (enable) {
320                 sdioh_sdmmc_devintr_on(sd);
321         } else {
322                 sdioh_sdmmc_devintr_off(sd);
323         }
324
325         spin_unlock_irqrestore(&sdos->lock, flags);
326
327         return SDIOH_API_RC_SUCCESS;
328 }
329
330
331 #ifdef BCMSDH_MODULE
332 static int __init
333 bcmsdh_module_init(void)
334 {
335         int error = 0;
336         sdio_function_init();
337         return error;
338 }
339
340 static void __exit
341 bcmsdh_module_cleanup(void)
342 {
343         sdio_function_cleanup();
344 }
345
346 module_init(bcmsdh_module_init);
347 module_exit(bcmsdh_module_cleanup);
348
349 MODULE_LICENSE("GPL v2");
350 MODULE_DESCRIPTION(DESCRIPTION);
351 MODULE_AUTHOR(AUTHOR);
352
353 #endif /* BCMSDH_MODULE */
354 /*
355  * module init
356 */
357 int sdio_function_init(void)
358 {
359         int error = 0;
360         sd_trace(("bcmsdh_sdmmc: %s Enter\n", __FUNCTION__));
361
362         gInstance = kzalloc(sizeof(BCMSDH_SDMMC_INSTANCE), GFP_KERNEL);
363         if (!gInstance)
364                 return -ENOMEM;
365
366         error = sdio_register_driver(&bcmsdh_sdmmc_driver);
367
368         return error;
369 }
370
371 /*
372  * module cleanup
373 */
374 void sdio_function_cleanup(void)
375 {
376         sd_trace(("%s Enter\n", __FUNCTION__));
377
378
379         sdio_unregister_driver(&bcmsdh_sdmmc_driver);
380
381         if (gInstance)
382                 kfree(gInstance);
383 }