wifi: broadcom sta + ap mode use hostapd [1/3]
[GitHub/LineageOS/G12/android_hardware_amlogic_kernel-modules_dhd-driver.git] / bcmdhd.1.363.59.144.x.cn / bcmsdh_linux.c
CommitLineData
ef6a5fee
RC
1/*
2 * SDIO access interface for drivers - linux specific (pci only)
3 *
4 * Copyright (C) 1999-2016, 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 *
25 * <<Broadcom-WL-IPTag/Open:>>
26 *
27 * $Id: bcmsdh_linux.c 514727 2014-11-12 03:02:48Z $
28 */
29
30/**
31 * @file bcmsdh_linux.c
32 */
33
34#define __UNDEF_NO_VERSION__
35
36#include <typedefs.h>
37#include <linuxver.h>
38#include <linux/pci.h>
39#include <linux/completion.h>
40
41#include <osl.h>
42#include <pcicfg.h>
43#include <bcmdefs.h>
44#include <bcmdevs.h>
45#include <linux/irq.h>
46extern void dhdsdio_isr(void * args);
47#include <bcmutils.h>
48#include <dngl_stats.h>
49#include <dhd.h>
50#if defined(CONFIG_ARCH_ODIN)
51#include <linux/platform_data/gpio-odin.h>
52#endif /* defined(CONFIG_ARCH_ODIN) */
53#include <dhd_linux.h>
54
55/* driver info, initialized when bcmsdh_register is called */
56static bcmsdh_driver_t drvinfo = {NULL, NULL, NULL, NULL};
57
58typedef enum {
59 DHD_INTR_INVALID = 0,
60 DHD_INTR_INBAND,
61 DHD_INTR_HWOOB,
62 DHD_INTR_SWOOB
63} DHD_HOST_INTR_TYPE;
64
65/* the BCMSDH module comprises the generic part (bcmsdh.c) and OS specific layer (e.g.
66 * bcmsdh_linux.c). Put all OS specific variables (e.g. irq number and flags) here rather
67 * than in the common structure bcmsdh_info. bcmsdh_info only keeps a handle (os_ctx) to this
68 * structure.
69 */
70typedef struct bcmsdh_os_info {
71 DHD_HOST_INTR_TYPE intr_type;
72 int oob_irq_num; /* valid when hardware or software oob in use */
73 unsigned long oob_irq_flags; /* valid when hardware or software oob in use */
74 bool oob_irq_registered;
75 bool oob_irq_enabled;
76 bool oob_irq_wake_enabled;
77 spinlock_t oob_irq_spinlock;
78 bcmsdh_cb_fn_t oob_irq_handler;
79 void *oob_irq_handler_context;
80 void *context; /* context returned from upper layer */
81 void *sdioh; /* handle to lower layer (sdioh) */
82 void *dev; /* handle to the underlying device */
83 bool dev_wake_enabled;
84} bcmsdh_os_info_t;
85
86/* debugging macros */
87#define SDLX_MSG(x) printf x
88
89/**
90 * Checks to see if vendor and device IDs match a supported SDIO Host Controller.
91 */
92bool
93bcmsdh_chipmatch(uint16 vendor, uint16 device)
94{
95 /* Add other vendors and devices as required */
96
97#ifdef BCMSDIOH_STD
98 /* Check for Arasan host controller */
99 if (vendor == VENDOR_SI_IMAGE) {
100 return (TRUE);
101 }
102 /* Check for BRCM 27XX Standard host controller */
103 if (device == BCM27XX_SDIOH_ID && vendor == VENDOR_BROADCOM) {
104 return (TRUE);
105 }
106 /* Check for BRCM Standard host controller */
107 if (device == SDIOH_FPGA_ID && vendor == VENDOR_BROADCOM) {
108 return (TRUE);
109 }
110 /* Check for TI PCIxx21 Standard host controller */
111 if (device == PCIXX21_SDIOH_ID && vendor == VENDOR_TI) {
112 return (TRUE);
113 }
114 if (device == PCIXX21_SDIOH0_ID && vendor == VENDOR_TI) {
115 return (TRUE);
116 }
117 /* Ricoh R5C822 Standard SDIO Host */
118 if (device == R5C822_SDIOH_ID && vendor == VENDOR_RICOH) {
119 return (TRUE);
120 }
121 /* JMicron Standard SDIO Host */
122 if (device == JMICRON_SDIOH_ID && vendor == VENDOR_JMICRON) {
123 return (TRUE);
124 }
125
126#endif /* BCMSDIOH_STD */
127#ifdef BCMSDIOH_SPI
128 /* This is the PciSpiHost. */
129 if (device == SPIH_FPGA_ID && vendor == VENDOR_BROADCOM) {
130 printf("Found PCI SPI Host Controller\n");
131 return (TRUE);
132 }
133
134#endif /* BCMSDIOH_SPI */
135
136 return (FALSE);
137}
138
139void* bcmsdh_probe(osl_t *osh, void *dev, void *sdioh, void *adapter_info, uint bus_type,
140 uint bus_num, uint slot_num)
141{
142 ulong regs;
143 bcmsdh_info_t *bcmsdh;
144 uint32 vendevid;
145 bcmsdh_os_info_t *bcmsdh_osinfo = NULL;
146
147 bcmsdh = bcmsdh_attach(osh, sdioh, &regs);
148 if (bcmsdh == NULL) {
149 SDLX_MSG(("%s: bcmsdh_attach failed\n", __FUNCTION__));
150 goto err;
151 }
152 bcmsdh_osinfo = MALLOC(osh, sizeof(bcmsdh_os_info_t));
153 if (bcmsdh_osinfo == NULL) {
154 SDLX_MSG(("%s: failed to allocate bcmsdh_os_info_t\n", __FUNCTION__));
155 goto err;
156 }
157 bzero((char *)bcmsdh_osinfo, sizeof(bcmsdh_os_info_t));
158 bcmsdh->os_cxt = bcmsdh_osinfo;
159 bcmsdh_osinfo->sdioh = sdioh;
160 bcmsdh_osinfo->dev = dev;
161 osl_set_bus_handle(osh, bcmsdh);
162
163#if !defined(CONFIG_HAS_WAKELOCK) && (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 36))
164 if (dev && device_init_wakeup(dev, true) == 0)
165 bcmsdh_osinfo->dev_wake_enabled = TRUE;
166#endif /* !defined(CONFIG_HAS_WAKELOCK) && (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 36)) */
167
168#if defined(OOB_INTR_ONLY)
169 spin_lock_init(&bcmsdh_osinfo->oob_irq_spinlock);
170 /* Get customer specific OOB IRQ parametres: IRQ number as IRQ type */
171 bcmsdh_osinfo->oob_irq_num = wifi_platform_get_irq_number(adapter_info,
172 &bcmsdh_osinfo->oob_irq_flags);
173 if (bcmsdh_osinfo->oob_irq_num < 0) {
174 SDLX_MSG(("%s: Host OOB irq is not defined\n", __FUNCTION__));
175 goto err;
176 }
177#endif /* defined(BCMLXSDMMC) */
178
179 /* Read the vendor/device ID from the CIS */
180 vendevid = bcmsdh_query_device(bcmsdh);
181 /* try to attach to the target device */
182 bcmsdh_osinfo->context = drvinfo.probe((vendevid >> 16), (vendevid & 0xFFFF), bus_num,
183 slot_num, 0, bus_type, (void *)regs, osh, bcmsdh);
184 if (bcmsdh_osinfo->context == NULL) {
185 SDLX_MSG(("%s: device attach failed\n", __FUNCTION__));
186 goto err;
187 }
188
189 return bcmsdh;
190
191 /* error handling */
192err:
193 if (bcmsdh != NULL)
194 bcmsdh_detach(osh, bcmsdh);
195 if (bcmsdh_osinfo != NULL)
196 MFREE(osh, bcmsdh_osinfo, sizeof(bcmsdh_os_info_t));
197 return NULL;
198}
199
200int bcmsdh_remove(bcmsdh_info_t *bcmsdh)
201{
202 bcmsdh_os_info_t *bcmsdh_osinfo = bcmsdh->os_cxt;
203
204#if !defined(CONFIG_HAS_WAKELOCK) && (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 36))
205 if (bcmsdh_osinfo->dev)
206 device_init_wakeup(bcmsdh_osinfo->dev, false);
207 bcmsdh_osinfo->dev_wake_enabled = FALSE;
208#endif /* !defined(CONFIG_HAS_WAKELOCK) && (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 36)) */
209
210 drvinfo.remove(bcmsdh_osinfo->context);
211 MFREE(bcmsdh->osh, bcmsdh->os_cxt, sizeof(bcmsdh_os_info_t));
212 bcmsdh_detach(bcmsdh->osh, bcmsdh);
213
214 return 0;
215}
216
217int bcmsdh_suspend(bcmsdh_info_t *bcmsdh)
218{
219 bcmsdh_os_info_t *bcmsdh_osinfo = bcmsdh->os_cxt;
220
221 if (drvinfo.suspend && drvinfo.suspend(bcmsdh_osinfo->context))
222 return -EBUSY;
223 return 0;
224}
225
226int bcmsdh_resume(bcmsdh_info_t *bcmsdh)
227{
228 bcmsdh_os_info_t *bcmsdh_osinfo = bcmsdh->os_cxt;
229
230 if (drvinfo.resume)
231 return drvinfo.resume(bcmsdh_osinfo->context);
232 return 0;
233}
234
235extern int bcmsdh_register_client_driver(void);
236extern void bcmsdh_unregister_client_driver(void);
237extern int sdio_func_reg_notify(void* semaphore);
238extern void sdio_func_unreg_notify(void);
239
240#if defined(BCMLXSDMMC)
241int bcmsdh_reg_sdio_notify(void* semaphore)
242{
243 return sdio_func_reg_notify(semaphore);
244}
245
246void bcmsdh_unreg_sdio_notify(void)
247{
248 sdio_func_unreg_notify();
249}
250#endif /* defined(BCMLXSDMMC) */
251
252int
253bcmsdh_register(bcmsdh_driver_t *driver)
254{
255 int error = 0;
256
257 drvinfo = *driver;
258 SDLX_MSG(("%s: register client driver\n", __FUNCTION__));
259 error = bcmsdh_register_client_driver();
260 if (error)
261 SDLX_MSG(("%s: failed %d\n", __FUNCTION__, error));
262
263 return error;
264}
265
266void
267bcmsdh_unregister(void)
268{
269#if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 0))
270 if (bcmsdh_pci_driver.node.next == NULL)
271 return;
272#endif
273
274 bcmsdh_unregister_client_driver();
275}
276
277void bcmsdh_dev_pm_stay_awake(bcmsdh_info_t *bcmsdh)
278{
279#if !defined(CONFIG_HAS_WAKELOCK) && (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 36))
280 bcmsdh_os_info_t *bcmsdh_osinfo = bcmsdh->os_cxt;
281 pm_stay_awake(bcmsdh_osinfo->dev);
282#endif /* !defined(CONFIG_HAS_WAKELOCK) && (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 36)) */
283}
284
285void bcmsdh_dev_relax(bcmsdh_info_t *bcmsdh)
286{
287#if !defined(CONFIG_HAS_WAKELOCK) && (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 36))
288 bcmsdh_os_info_t *bcmsdh_osinfo = bcmsdh->os_cxt;
289 pm_relax(bcmsdh_osinfo->dev);
290#endif /* !defined(CONFIG_HAS_WAKELOCK) && (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 36)) */
291}
292
293bool bcmsdh_dev_pm_enabled(bcmsdh_info_t *bcmsdh)
294{
295 bcmsdh_os_info_t *bcmsdh_osinfo = bcmsdh->os_cxt;
296
297 return bcmsdh_osinfo->dev_wake_enabled;
298}
299
300#if defined(OOB_INTR_ONLY)
301void bcmsdh_oob_intr_set(bcmsdh_info_t *bcmsdh, bool enable)
302{
303 unsigned long flags;
304 bcmsdh_os_info_t *bcmsdh_osinfo;
305
306 if (!bcmsdh)
307 return;
308
309 bcmsdh_osinfo = bcmsdh->os_cxt;
310 spin_lock_irqsave(&bcmsdh_osinfo->oob_irq_spinlock, flags);
311 if (bcmsdh_osinfo->oob_irq_enabled != enable) {
312 if (enable)
313 enable_irq(bcmsdh_osinfo->oob_irq_num);
314 else
315 disable_irq_nosync(bcmsdh_osinfo->oob_irq_num);
316 bcmsdh_osinfo->oob_irq_enabled = enable;
317 }
318 spin_unlock_irqrestore(&bcmsdh_osinfo->oob_irq_spinlock, flags);
319}
320
321static irqreturn_t wlan_oob_irq(int irq, void *dev_id)
322{
323 bcmsdh_info_t *bcmsdh = (bcmsdh_info_t *)dev_id;
324 bcmsdh_os_info_t *bcmsdh_osinfo = bcmsdh->os_cxt;
08dfb6c4 325
ef6a5fee
RC
326 bcmsdh_oob_intr_set(bcmsdh, FALSE);
327 bcmsdh_osinfo->oob_irq_handler(bcmsdh_osinfo->oob_irq_handler_context);
328
329 return IRQ_HANDLED;
330}
331
332int bcmsdh_oob_intr_register(bcmsdh_info_t *bcmsdh, bcmsdh_cb_fn_t oob_irq_handler,
333 void* oob_irq_handler_context)
334{
335 int err = 0;
336 bcmsdh_os_info_t *bcmsdh_osinfo = bcmsdh->os_cxt;
337
338 SDLX_MSG(("%s: Enter\n", __FUNCTION__));
339 if (bcmsdh_osinfo->oob_irq_registered) {
340 SDLX_MSG(("%s: irq is already registered\n", __FUNCTION__));
341 return -EBUSY;
342 }
343#ifdef HW_OOB
344 printf("%s: HW_OOB enabled\n", __FUNCTION__);
345#else
346 printf("%s: SW_OOB enabled\n", __FUNCTION__);
347#endif
08dfb6c4
RC
348 SDLX_MSG(("%s OOB irq=%d flags=0x%X\n", __FUNCTION__,
349 (int)bcmsdh_osinfo->oob_irq_num, (int)bcmsdh_osinfo->oob_irq_flags));
ef6a5fee
RC
350 bcmsdh_osinfo->oob_irq_handler = oob_irq_handler;
351 bcmsdh_osinfo->oob_irq_handler_context = oob_irq_handler_context;
352 bcmsdh_osinfo->oob_irq_enabled = TRUE;
353 bcmsdh_osinfo->oob_irq_registered = TRUE;
354#if defined(CONFIG_ARCH_ODIN)
355 err = odin_gpio_sms_request_irq(bcmsdh_osinfo->oob_irq_num, wlan_oob_irq,
356 bcmsdh_osinfo->oob_irq_flags, "bcmsdh_sdmmc", bcmsdh);
357#else
358 err = request_irq(bcmsdh_osinfo->oob_irq_num, wlan_oob_irq,
359 bcmsdh_osinfo->oob_irq_flags, "bcmsdh_sdmmc", bcmsdh);
360#endif /* defined(CONFIG_ARCH_ODIN) */
361 if (err) {
362 bcmsdh_osinfo->oob_irq_enabled = FALSE;
363 bcmsdh_osinfo->oob_irq_registered = FALSE;
364 SDLX_MSG(("%s: request_irq failed with %d\n", __FUNCTION__, err));
365 return err;
366 }
367
368#if defined(DISABLE_WOWLAN)
369 SDLX_MSG(("%s: disable_irq_wake\n", __FUNCTION__));
370 bcmsdh_osinfo->oob_irq_wake_enabled = FALSE;
371#else
372 SDLX_MSG(("%s: enable_irq_wake\n", __FUNCTION__));
373 err = enable_irq_wake(bcmsdh_osinfo->oob_irq_num);
374 if (err)
375 SDLX_MSG(("%s: enable_irq_wake failed with %d\n", __FUNCTION__, err));
376 else
377 bcmsdh_osinfo->oob_irq_wake_enabled = TRUE;
378#endif
379
380 return 0;
381}
382
383void bcmsdh_oob_intr_unregister(bcmsdh_info_t *bcmsdh)
384{
385 int err = 0;
386 bcmsdh_os_info_t *bcmsdh_osinfo = bcmsdh->os_cxt;
387
388 SDLX_MSG(("%s: Enter\n", __FUNCTION__));
389 if (!bcmsdh_osinfo->oob_irq_registered) {
390 SDLX_MSG(("%s: irq is not registered\n", __FUNCTION__));
391 return;
392 }
393 if (bcmsdh_osinfo->oob_irq_wake_enabled) {
394 err = disable_irq_wake(bcmsdh_osinfo->oob_irq_num);
395 if (!err)
396 bcmsdh_osinfo->oob_irq_wake_enabled = FALSE;
397 }
398 if (bcmsdh_osinfo->oob_irq_enabled) {
399 disable_irq(bcmsdh_osinfo->oob_irq_num);
400 bcmsdh_osinfo->oob_irq_enabled = FALSE;
401 }
402 free_irq(bcmsdh_osinfo->oob_irq_num, bcmsdh);
403 bcmsdh_osinfo->oob_irq_registered = FALSE;
404}
405#endif
406
407/* Module parameters specific to each host-controller driver */
408
409extern uint sd_msglevel; /* Debug message level */
410module_param(sd_msglevel, uint, 0);
411
412extern uint sd_power; /* 0 = SD Power OFF, 1 = SD Power ON. */
413module_param(sd_power, uint, 0);
414
415extern uint sd_clock; /* SD Clock Control, 0 = SD Clock OFF, 1 = SD Clock ON */
416module_param(sd_clock, uint, 0);
417
418extern uint sd_divisor; /* Divisor (-1 means external clock) */
419module_param(sd_divisor, uint, 0);
420
421extern uint sd_sdmode; /* Default is SD4, 0=SPI, 1=SD1, 2=SD4 */
422module_param(sd_sdmode, uint, 0);
423
424extern uint sd_hiok; /* Ok to use hi-speed mode */
425module_param(sd_hiok, uint, 0);
426
427extern uint sd_f2_blocksize;
428module_param(sd_f2_blocksize, int, 0);
429
430#ifdef BCMSDIOH_STD
431extern int sd_uhsimode;
432module_param(sd_uhsimode, int, 0);
433extern uint sd_tuning_period;
434module_param(sd_tuning_period, uint, 0);
435extern int sd_delay_value;
436module_param(sd_delay_value, uint, 0);
437
438/* SDIO Drive Strength for UHSI mode specific to SDIO3.0 */
439extern char dhd_sdiod_uhsi_ds_override[2];
440module_param_string(dhd_sdiod_uhsi_ds_override, dhd_sdiod_uhsi_ds_override, 2, 0);
441
442#endif
443
444#ifdef BCMSDH_MODULE
445EXPORT_SYMBOL(bcmsdh_attach);
446EXPORT_SYMBOL(bcmsdh_detach);
447EXPORT_SYMBOL(bcmsdh_intr_query);
448EXPORT_SYMBOL(bcmsdh_intr_enable);
449EXPORT_SYMBOL(bcmsdh_intr_disable);
450EXPORT_SYMBOL(bcmsdh_intr_reg);
451EXPORT_SYMBOL(bcmsdh_intr_dereg);
452
453#if defined(DHD_DEBUG)
454EXPORT_SYMBOL(bcmsdh_intr_pending);
455#endif
456
457EXPORT_SYMBOL(bcmsdh_devremove_reg);
458EXPORT_SYMBOL(bcmsdh_cfg_read);
459EXPORT_SYMBOL(bcmsdh_cfg_write);
460EXPORT_SYMBOL(bcmsdh_cis_read);
461EXPORT_SYMBOL(bcmsdh_reg_read);
462EXPORT_SYMBOL(bcmsdh_reg_write);
463EXPORT_SYMBOL(bcmsdh_regfail);
464EXPORT_SYMBOL(bcmsdh_send_buf);
465EXPORT_SYMBOL(bcmsdh_recv_buf);
466
467EXPORT_SYMBOL(bcmsdh_rwdata);
468EXPORT_SYMBOL(bcmsdh_abort);
469EXPORT_SYMBOL(bcmsdh_query_device);
470EXPORT_SYMBOL(bcmsdh_query_iofnum);
471EXPORT_SYMBOL(bcmsdh_iovar_op);
472EXPORT_SYMBOL(bcmsdh_register);
473EXPORT_SYMBOL(bcmsdh_unregister);
474EXPORT_SYMBOL(bcmsdh_chipmatch);
475EXPORT_SYMBOL(bcmsdh_reset);
476EXPORT_SYMBOL(bcmsdh_waitlockfree);
477
478EXPORT_SYMBOL(bcmsdh_get_dstatus);
479EXPORT_SYMBOL(bcmsdh_cfg_read_word);
480EXPORT_SYMBOL(bcmsdh_cfg_write_word);
481EXPORT_SYMBOL(bcmsdh_cur_sbwad);
482EXPORT_SYMBOL(bcmsdh_chipinfo);
483
484#endif /* BCMSDH_MODULE */