]> nv-tegra.nvidia Code Review - linux-2.6.git/blob - drivers/net/wireless/bcmdhd/bcmsdh_sdmmc_linux.c
22343784cb570b2f23574b23093455213428dfd5
[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 308645 2012-01-17 02:33:26Z $
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_43239)
62 #define SDIO_DEVICE_ID_BROADCOM_43239    43239
63 #endif /* !defined(SDIO_DEVICE_ID_BROADCOM_43239) */
64
65 #include <bcmsdh_sdmmc.h>
66
67 #include <dhd_dbg.h>
68
69 #ifdef WL_CFG80211
70 extern void wl_cfg80211_set_parent_dev(void *dev);
71 #endif
72
73 extern void sdioh_sdmmc_devintr_off(sdioh_info_t *sd);
74 extern void sdioh_sdmmc_devintr_on(sdioh_info_t *sd);
75 extern int dhd_os_check_wakelock(void *dhdp);
76 extern int dhd_os_check_if_up(void *dhdp);
77 extern void *bcmsdh_get_drvdata(void);
78
79 int sdio_function_init(void);
80 void sdio_function_cleanup(void);
81
82 #define DESCRIPTION "bcmsdh_sdmmc Driver"
83 #define AUTHOR "Broadcom Corporation"
84
85 /* module param defaults */
86 static int clockoverride = 0;
87
88 module_param(clockoverride, int, 0644);
89 MODULE_PARM_DESC(clockoverride, "SDIO card clock override");
90
91 PBCMSDH_SDMMC_INSTANCE gInstance;
92
93 /* Maximum number of bcmsdh_sdmmc devices supported by driver */
94 #define BCMSDH_SDMMC_MAX_DEVICES 1
95
96 extern int bcmsdh_probe_bcmdhd(struct device *dev);
97 extern int bcmsdh_remove_bcmdhd(struct device *dev);
98
99 extern volatile bool dhd_mmc_suspend;
100
101 static int bcmsdh_sdmmc_probe(struct sdio_func *func,
102                               const struct sdio_device_id *id)
103 {
104         int ret = 0;
105         static struct sdio_func sdio_func_0;
106         sd_trace(("bcmsdh_sdmmc: %s Enter\n", __FUNCTION__));
107         sd_trace(("sdio_bcmsdh: func->class=%x\n", func->class));
108         sd_trace(("sdio_vendor: 0x%04x\n", func->vendor));
109         sd_trace(("sdio_device: 0x%04x\n", func->device));
110         sd_trace(("Function#: 0x%04x\n", func->num));
111
112         if (func->num == 1) {
113                 sdio_func_0.num = 0;
114                 sdio_func_0.card = func->card;
115                 gInstance->func[0] = &sdio_func_0;
116                 if(func->device == 0x4) { /* 4318 */
117                         gInstance->func[2] = NULL;
118                         sd_trace(("NIC found, calling bcmsdh_probe_bcmdhd...\n"));
119                         ret = bcmsdh_probe_bcmdhd(&func->dev);
120                 }
121         }
122
123         gInstance->func[func->num] = func;
124
125         if (func->num == 2) {
126 #ifdef WL_CFG80211
127                 wl_cfg80211_set_parent_dev(&func->dev);
128 #endif
129                 sd_trace(("F2 found, calling bcmsdh_probe_bcmdhd...\n"));
130                 ret = bcmsdh_probe_bcmdhd(&func->dev);
131         }
132
133         return ret;
134 }
135
136 static void bcmsdh_sdmmc_remove(struct sdio_func *func)
137 {
138         sd_trace(("bcmsdh_sdmmc: %s Enter\n", __FUNCTION__));
139         sd_info(("sdio_bcmsdh: func->class=%x\n", func->class));
140         sd_info(("sdio_vendor: 0x%04x\n", func->vendor));
141         sd_info(("sdio_device: 0x%04x\n", func->device));
142         sd_info(("Function#: 0x%04x\n", func->num));
143
144         if (func->num == 2) {
145                 sd_trace(("F2 found, calling bcmsdh_remove_bcmdhd...\n"));
146                 bcmsdh_remove_bcmdhd(&func->dev);
147         } else if (func->num == 1) {
148                 sdio_claim_host(func);
149                 sdio_disable_func(func);
150                 sdio_release_host(func);
151                 gInstance->func[1] = NULL;
152         }
153 }
154
155 /* devices we support, null terminated */
156 static const struct sdio_device_id bcmsdh_sdmmc_ids[] = {
157         { SDIO_DEVICE(SDIO_VENDOR_ID_BROADCOM, SDIO_DEVICE_ID_BROADCOM_DEFAULT) },
158         { SDIO_DEVICE(SDIO_VENDOR_ID_BROADCOM, SDIO_DEVICE_ID_BROADCOM_4325_SDGWB) },
159         { SDIO_DEVICE(SDIO_VENDOR_ID_BROADCOM, SDIO_DEVICE_ID_BROADCOM_4325) },
160         { SDIO_DEVICE(SDIO_VENDOR_ID_BROADCOM, SDIO_DEVICE_ID_BROADCOM_4319) },
161         { SDIO_DEVICE(SDIO_VENDOR_ID_BROADCOM, SDIO_DEVICE_ID_BROADCOM_4330) },
162         { SDIO_DEVICE(SDIO_VENDOR_ID_BROADCOM, SDIO_DEVICE_ID_BROADCOM_4334) },
163         { SDIO_DEVICE(SDIO_VENDOR_ID_BROADCOM, SDIO_DEVICE_ID_BROADCOM_43239) },
164         { SDIO_DEVICE_CLASS(SDIO_CLASS_NONE)            },
165         { /* end: all zeroes */                         },
166 };
167
168 MODULE_DEVICE_TABLE(sdio, bcmsdh_sdmmc_ids);
169
170 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 39)) && defined(CONFIG_PM)
171 static int bcmsdh_sdmmc_suspend(struct device *pdev)
172 {
173         struct sdio_func *func = dev_to_sdio_func(pdev);
174         mmc_pm_flag_t sdio_flags;
175         int ret = 0;
176
177         if (func->num != 2)
178                 return 0;
179
180         sd_trace(("%s Enter\n", __FUNCTION__));
181
182         if (dhd_os_check_wakelock(bcmsdh_get_drvdata()))
183                 return -EBUSY;
184 #if defined(OOB_INTR_ONLY)
185         bcmsdh_oob_intr_set(0);
186 #endif /* defined(OOB_INTR_ONLY) */
187         smp_mb();
188
189         sdio_flags = sdio_get_host_pm_caps(func);
190
191         if (!(sdio_flags & MMC_PM_KEEP_POWER)) {
192                 sd_err(("can't keep power while host "
193                                 "is suspended\n", __FUNCTION__));
194                 ret = -EINVAL;
195                 goto out;
196         }
197
198         /* keep power while host suspended */
199         ret = sdio_set_host_pm_flags(func, MMC_PM_KEEP_POWER);
200         if (ret) {
201                 sd_err(("error while trying to keep power\n", __FUNCTION__));
202                 goto out;
203         }
204
205         dhd_mmc_suspend = TRUE;
206
207 out:
208         return ret;
209 }
210
211 static int bcmsdh_sdmmc_resume(struct device *pdev)
212 {
213         struct sdio_func *func = dev_to_sdio_func(pdev);
214
215         sd_trace(("%s Enter\n", __FUNCTION__));
216         dhd_mmc_suspend = FALSE;
217 #if defined(OOB_INTR_ONLY)
218         if ((func->num == 2) && dhd_os_check_if_up(bcmsdh_get_drvdata()))
219                 bcmsdh_oob_intr_set(1);
220 #endif /* (OOB_INTR_ONLY) */
221
222         smp_mb();
223         return 0;
224 }
225
226 static const struct dev_pm_ops bcmsdh_sdmmc_pm_ops = {
227         .suspend        = bcmsdh_sdmmc_suspend,
228         .resume         = bcmsdh_sdmmc_resume,
229 };
230 #endif  /* (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 39)) && defined(CONFIG_PM) */
231
232 static struct sdio_driver bcmsdh_sdmmc_driver = {
233         .probe          = bcmsdh_sdmmc_probe,
234         .remove         = bcmsdh_sdmmc_remove,
235         .name           = "bcmsdh_sdmmc",
236         .id_table       = bcmsdh_sdmmc_ids,
237 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 39)) && defined(CONFIG_PM)
238         .drv = {
239                 .pm     = &bcmsdh_sdmmc_pm_ops,
240         },
241 #endif /* (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 39)) && defined(CONFIG_PM) */
242 };
243
244 struct sdos_info {
245         sdioh_info_t *sd;
246         spinlock_t lock;
247 };
248
249
250 int
251 sdioh_sdmmc_osinit(sdioh_info_t *sd)
252 {
253         struct sdos_info *sdos;
254
255         sdos = (struct sdos_info*)MALLOC(sd->osh, sizeof(struct sdos_info));
256         sd->sdos_info = (void*)sdos;
257         if (sdos == NULL)
258                 return BCME_NOMEM;
259
260         sdos->sd = sd;
261         spin_lock_init(&sdos->lock);
262         return BCME_OK;
263 }
264
265 void
266 sdioh_sdmmc_osfree(sdioh_info_t *sd)
267 {
268         struct sdos_info *sdos;
269         ASSERT(sd && sd->sdos_info);
270
271         sdos = (struct sdos_info *)sd->sdos_info;
272         MFREE(sd->osh, sdos, sizeof(struct sdos_info));
273 }
274
275 /* Interrupt enable/disable */
276 SDIOH_API_RC
277 sdioh_interrupt_set(sdioh_info_t *sd, bool enable)
278 {
279         ulong flags;
280         struct sdos_info *sdos;
281
282         sd_trace(("%s: %s\n", __FUNCTION__, enable ? "Enabling" : "Disabling"));
283
284         sdos = (struct sdos_info *)sd->sdos_info;
285         ASSERT(sdos);
286
287 #if !defined(OOB_INTR_ONLY)
288         if (enable && !(sd->intr_handler && sd->intr_handler_arg)) {
289                 sd_err(("%s: no handler registered, will not enable\n", __FUNCTION__));
290                 return SDIOH_API_RC_FAIL;
291         }
292 #endif /* !defined(OOB_INTR_ONLY) */
293
294         /* Ensure atomicity for enable/disable calls */
295         spin_lock_irqsave(&sdos->lock, flags);
296
297         sd->client_intr_enabled = enable;
298         if (enable) {
299                 sdioh_sdmmc_devintr_on(sd);
300         } else {
301                 sdioh_sdmmc_devintr_off(sd);
302         }
303
304         spin_unlock_irqrestore(&sdos->lock, flags);
305
306         return SDIOH_API_RC_SUCCESS;
307 }
308
309
310 #ifdef BCMSDH_MODULE
311 static int __init
312 bcmsdh_module_init(void)
313 {
314         int error = 0;
315         sdio_function_init();
316         return error;
317 }
318
319 static void __exit
320 bcmsdh_module_cleanup(void)
321 {
322         sdio_function_cleanup();
323 }
324
325 module_init(bcmsdh_module_init);
326 module_exit(bcmsdh_module_cleanup);
327
328 MODULE_LICENSE("GPL v2");
329 MODULE_DESCRIPTION(DESCRIPTION);
330 MODULE_AUTHOR(AUTHOR);
331
332 #endif /* BCMSDH_MODULE */
333 /*
334  * module init
335 */
336 int sdio_function_init(void)
337 {
338         int error = 0;
339         sd_trace(("bcmsdh_sdmmc: %s Enter\n", __FUNCTION__));
340
341         gInstance = kzalloc(sizeof(BCMSDH_SDMMC_INSTANCE), GFP_KERNEL);
342         if (!gInstance)
343                 return -ENOMEM;
344
345         error = sdio_register_driver(&bcmsdh_sdmmc_driver);
346
347         return error;
348 }
349
350 /*
351  * module cleanup
352 */
353 void sdio_function_cleanup(void)
354 {
355         sd_trace(("%s Enter\n", __FUNCTION__));
356
357
358         sdio_unregister_driver(&bcmsdh_sdmmc_driver);
359
360         if (gInstance)
361                 kfree(gInstance);
362 }