blob: b895f198a950c1a8577ef675909b4b427bed92a6 [file] [log] [blame]
Arend van Spriel5b435de2011-10-05 13:19:03 +02001/*
2 * Copyright (c) 2010 Broadcom Corporation
3 *
4 * Permission to use, copy, modify, and/or distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
11 * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
13 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
14 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
16#include <linux/types.h>
17#include <linux/netdevice.h>
18#include <linux/mmc/sdio.h>
19#include <linux/mmc/core.h>
20#include <linux/mmc/sdio_func.h>
21#include <linux/mmc/sdio_ids.h>
22#include <linux/mmc/card.h>
23#include <linux/suspend.h>
24#include <linux/errno.h>
25#include <linux/sched.h> /* request_irq() */
Stephen Rothwellb7a57e72011-10-12 21:35:07 +020026#include <linux/module.h>
Arend van Spriel5b435de2011-10-05 13:19:03 +020027#include <net/cfg80211.h>
28
29#include <defs.h>
30#include <brcm_hw_ids.h>
31#include <brcmu_utils.h>
32#include <brcmu_wifi.h>
33#include "sdio_host.h"
34#include "dhd.h"
35#include "dhd_dbg.h"
36#include "wl_cfg80211.h"
37
38#define SDIO_VENDOR_ID_BROADCOM 0x02d0
39
40#define DMA_ALIGN_MASK 0x03
41
42#define SDIO_DEVICE_ID_BROADCOM_4329 0x4329
Franky Lince2d7d72011-12-08 15:06:39 -080043#define SDIO_DEVICE_ID_BROADCOM_4330 0x4330
Arend van Spriel5b435de2011-10-05 13:19:03 +020044
45#define SDIO_FUNC1_BLOCKSIZE 64
46#define SDIO_FUNC2_BLOCKSIZE 512
47
48/* devices we support, null terminated */
49static const struct sdio_device_id brcmf_sdmmc_ids[] = {
50 {SDIO_DEVICE(SDIO_VENDOR_ID_BROADCOM, SDIO_DEVICE_ID_BROADCOM_4329)},
Franky Lince2d7d72011-12-08 15:06:39 -080051 {SDIO_DEVICE(SDIO_VENDOR_ID_BROADCOM, SDIO_DEVICE_ID_BROADCOM_4330)},
Arend van Spriel5b435de2011-10-05 13:19:03 +020052 { /* end: all zeroes */ },
53};
54MODULE_DEVICE_TABLE(sdio, brcmf_sdmmc_ids);
55
56static bool
57brcmf_pm_resume_error(struct brcmf_sdio_dev *sdiodev)
58{
59 bool is_err = false;
60#ifdef CONFIG_PM_SLEEP
61 is_err = atomic_read(&sdiodev->suspend);
62#endif
63 return is_err;
64}
65
66static void
67brcmf_pm_resume_wait(struct brcmf_sdio_dev *sdiodev, wait_queue_head_t *wq)
68{
69#ifdef CONFIG_PM_SLEEP
70 int retry = 0;
71 while (atomic_read(&sdiodev->suspend) && retry++ != 30)
72 wait_event_timeout(*wq, false, HZ/100);
73#endif
74}
75
76static inline int brcmf_sdioh_f0_write_byte(struct brcmf_sdio_dev *sdiodev,
77 uint regaddr, u8 *byte)
78{
79 struct sdio_func *sdfunc = sdiodev->func[0];
80 int err_ret;
81
82 /*
83 * Can only directly write to some F0 registers.
84 * Handle F2 enable/disable and Abort command
85 * as a special case.
86 */
87 if (regaddr == SDIO_CCCR_IOEx) {
88 sdfunc = sdiodev->func[2];
89 if (sdfunc) {
90 sdio_claim_host(sdfunc);
91 if (*byte & SDIO_FUNC_ENABLE_2) {
92 /* Enable Function 2 */
93 err_ret = sdio_enable_func(sdfunc);
94 if (err_ret)
95 brcmf_dbg(ERROR,
96 "enable F2 failed:%d\n",
97 err_ret);
98 } else {
99 /* Disable Function 2 */
100 err_ret = sdio_disable_func(sdfunc);
101 if (err_ret)
102 brcmf_dbg(ERROR,
103 "Disable F2 failed:%d\n",
104 err_ret);
105 }
106 sdio_release_host(sdfunc);
107 }
108 } else if (regaddr == SDIO_CCCR_ABORT) {
109 sdio_claim_host(sdfunc);
110 sdio_writeb(sdfunc, *byte, regaddr, &err_ret);
111 sdio_release_host(sdfunc);
112 } else if (regaddr < 0xF0) {
113 brcmf_dbg(ERROR, "F0 Wr:0x%02x: write disallowed\n", regaddr);
114 err_ret = -EPERM;
115 } else {
116 sdio_claim_host(sdfunc);
117 sdio_f0_writeb(sdfunc, *byte, regaddr, &err_ret);
118 sdio_release_host(sdfunc);
119 }
120
121 return err_ret;
122}
123
124int brcmf_sdioh_request_byte(struct brcmf_sdio_dev *sdiodev, uint rw, uint func,
125 uint regaddr, u8 *byte)
126{
127 int err_ret;
128
129 brcmf_dbg(INFO, "rw=%d, func=%d, addr=0x%05x\n", rw, func, regaddr);
130
131 brcmf_pm_resume_wait(sdiodev, &sdiodev->request_byte_wait);
132 if (brcmf_pm_resume_error(sdiodev))
133 return -EIO;
134
135 if (rw && func == 0) {
136 /* handle F0 separately */
137 err_ret = brcmf_sdioh_f0_write_byte(sdiodev, regaddr, byte);
138 } else {
139 sdio_claim_host(sdiodev->func[func]);
140 if (rw) /* CMD52 Write */
141 sdio_writeb(sdiodev->func[func], *byte, regaddr,
142 &err_ret);
143 else if (func == 0) {
144 *byte = sdio_f0_readb(sdiodev->func[func], regaddr,
145 &err_ret);
146 } else {
147 *byte = sdio_readb(sdiodev->func[func], regaddr,
148 &err_ret);
149 }
150 sdio_release_host(sdiodev->func[func]);
151 }
152
153 if (err_ret)
154 brcmf_dbg(ERROR, "Failed to %s byte F%d:@0x%05x=%02x, Err: %d\n",
155 rw ? "write" : "read", func, regaddr, *byte, err_ret);
156
157 return err_ret;
158}
159
160int brcmf_sdioh_request_word(struct brcmf_sdio_dev *sdiodev,
161 uint rw, uint func, uint addr, u32 *word,
162 uint nbytes)
163{
164 int err_ret = -EIO;
165
166 if (func == 0) {
167 brcmf_dbg(ERROR, "Only CMD52 allowed to F0\n");
168 return -EINVAL;
169 }
170
171 brcmf_dbg(INFO, "rw=%d, func=%d, addr=0x%05x, nbytes=%d\n",
172 rw, func, addr, nbytes);
173
174 brcmf_pm_resume_wait(sdiodev, &sdiodev->request_word_wait);
175 if (brcmf_pm_resume_error(sdiodev))
176 return -EIO;
177 /* Claim host controller */
178 sdio_claim_host(sdiodev->func[func]);
179
180 if (rw) { /* CMD52 Write */
181 if (nbytes == 4)
182 sdio_writel(sdiodev->func[func], *word, addr,
183 &err_ret);
184 else if (nbytes == 2)
185 sdio_writew(sdiodev->func[func], (*word & 0xFFFF),
186 addr, &err_ret);
187 else
188 brcmf_dbg(ERROR, "Invalid nbytes: %d\n", nbytes);
189 } else { /* CMD52 Read */
190 if (nbytes == 4)
191 *word = sdio_readl(sdiodev->func[func], addr, &err_ret);
192 else if (nbytes == 2)
193 *word = sdio_readw(sdiodev->func[func], addr,
194 &err_ret) & 0xFFFF;
195 else
196 brcmf_dbg(ERROR, "Invalid nbytes: %d\n", nbytes);
197 }
198
199 /* Release host controller */
200 sdio_release_host(sdiodev->func[func]);
201
202 if (err_ret)
203 brcmf_dbg(ERROR, "Failed to %s word, Err: 0x%08x\n",
204 rw ? "write" : "read", err_ret);
205
206 return err_ret;
207}
208
Arend van Spriel5adfeb62011-11-22 17:21:38 -0800209/* precondition: host controller is claimed */
210static int
211brcmf_sdioh_request_data(struct brcmf_sdio_dev *sdiodev, uint write, bool fifo,
212 uint func, uint addr, struct sk_buff *pkt, uint pktlen)
213{
214 int err_ret = 0;
215
216 if ((write) && (!fifo)) {
217 err_ret = sdio_memcpy_toio(sdiodev->func[func], addr,
218 ((u8 *) (pkt->data)), pktlen);
219 } else if (write) {
220 err_ret = sdio_memcpy_toio(sdiodev->func[func], addr,
221 ((u8 *) (pkt->data)), pktlen);
222 } else if (fifo) {
223 err_ret = sdio_readsb(sdiodev->func[func],
224 ((u8 *) (pkt->data)), addr, pktlen);
225 } else {
226 err_ret = sdio_memcpy_fromio(sdiodev->func[func],
227 ((u8 *) (pkt->data)),
228 addr, pktlen);
229 }
230
231 return err_ret;
232}
233
Arend van Spriela52dd172011-11-22 17:21:45 -0800234/*
235 * This function takes a queue of packets. The packets on the queue
236 * are assumed to be properly aligned by the caller.
237 */
Arend van Spriel5adfeb62011-11-22 17:21:38 -0800238int
239brcmf_sdioh_request_chain(struct brcmf_sdio_dev *sdiodev, uint fix_inc,
240 uint write, uint func, uint addr,
241 struct sk_buff_head *pktq)
242{
243 bool fifo = (fix_inc == SDIOH_DATA_FIX);
244 u32 SGCount = 0;
245 int err_ret = 0;
246
247 struct sk_buff *pkt;
248
249 brcmf_dbg(TRACE, "Enter\n");
250
Arend van Spriela52dd172011-11-22 17:21:45 -0800251 brcmf_pm_resume_wait(sdiodev, &sdiodev->request_chain_wait);
Arend van Spriel5adfeb62011-11-22 17:21:38 -0800252 if (brcmf_pm_resume_error(sdiodev))
253 return -EIO;
254
255 /* Claim host controller */
256 sdio_claim_host(sdiodev->func[func]);
257
258 skb_queue_walk(pktq, pkt) {
259 uint pkt_len = pkt->len;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200260 pkt_len += 3;
261 pkt_len &= 0xFFFFFFFC;
262
Arend van Spriel5adfeb62011-11-22 17:21:38 -0800263 err_ret = brcmf_sdioh_request_data(sdiodev, write, fifo, func,
264 addr, pkt, pkt_len);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200265 if (err_ret) {
266 brcmf_dbg(ERROR, "%s FAILED %p[%d], addr=0x%05x, pkt_len=%d, ERR=0x%08x\n",
Arend van Spriel5adfeb62011-11-22 17:21:38 -0800267 write ? "TX" : "RX", pkt, SGCount, addr,
Arend van Spriel5b435de2011-10-05 13:19:03 +0200268 pkt_len, err_ret);
269 } else {
270 brcmf_dbg(TRACE, "%s xfr'd %p[%d], addr=0x%05x, len=%d\n",
Arend van Spriel5adfeb62011-11-22 17:21:38 -0800271 write ? "TX" : "RX", pkt, SGCount, addr,
Arend van Spriel5b435de2011-10-05 13:19:03 +0200272 pkt_len);
273 }
Arend van Spriel5b435de2011-10-05 13:19:03 +0200274 if (!fifo)
275 addr += pkt_len;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200276
Arend van Spriel5adfeb62011-11-22 17:21:38 -0800277 SGCount++;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200278 }
279
280 /* Release host controller */
281 sdio_release_host(sdiodev->func[func]);
282
283 brcmf_dbg(TRACE, "Exit\n");
284 return err_ret;
285}
286
287/*
Arend van Spriel80543212011-11-22 17:21:44 -0800288 * This function takes a single DMA-able packet.
Arend van Spriel5b435de2011-10-05 13:19:03 +0200289 */
290int brcmf_sdioh_request_buffer(struct brcmf_sdio_dev *sdiodev,
291 uint fix_inc, uint write, uint func, uint addr,
Arend van Spriel4c6e8692011-11-22 17:21:40 -0800292 struct sk_buff *pkt)
Arend van Spriel5b435de2011-10-05 13:19:03 +0200293{
Arend van Spriel4c6e8692011-11-22 17:21:40 -0800294 int status;
Arend van Spriel80543212011-11-22 17:21:44 -0800295 uint pkt_len = pkt->len;
296 bool fifo = (fix_inc == SDIOH_DATA_FIX);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200297
298 brcmf_dbg(TRACE, "Enter\n");
299
Arend van Spriel4c6e8692011-11-22 17:21:40 -0800300 if (pkt == NULL)
301 return -EINVAL;
302
Arend van Spriel5b435de2011-10-05 13:19:03 +0200303 brcmf_pm_resume_wait(sdiodev, &sdiodev->request_buffer_wait);
304 if (brcmf_pm_resume_error(sdiodev))
305 return -EIO;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200306
Arend van Spriel80543212011-11-22 17:21:44 -0800307 /* Claim host controller */
308 sdio_claim_host(sdiodev->func[func]);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200309
Arend van Spriel80543212011-11-22 17:21:44 -0800310 pkt_len += 3;
311 pkt_len &= (uint)~3;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200312
Arend van Spriel80543212011-11-22 17:21:44 -0800313 status = brcmf_sdioh_request_data(sdiodev, write, fifo, func,
314 addr, pkt, pkt_len);
315 if (status) {
316 brcmf_dbg(ERROR, "%s FAILED %p, addr=0x%05x, pkt_len=%d, ERR=0x%08x\n",
317 write ? "TX" : "RX", pkt, addr, pkt_len, status);
318 } else {
319 brcmf_dbg(TRACE, "%s xfr'd %p, addr=0x%05x, len=%d\n",
320 write ? "TX" : "RX", pkt, addr, pkt_len);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200321 }
322
Arend van Spriel80543212011-11-22 17:21:44 -0800323 /* Release host controller */
324 sdio_release_host(sdiodev->func[func]);
325
Arend van Spriel4c6e8692011-11-22 17:21:40 -0800326 return status;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200327}
328
329/* Read client card reg */
330static int
331brcmf_sdioh_card_regread(struct brcmf_sdio_dev *sdiodev, int func, u32 regaddr,
332 int regsize, u32 *data)
333{
334
335 if ((func == 0) || (regsize == 1)) {
336 u8 temp = 0;
337
338 brcmf_sdioh_request_byte(sdiodev, SDIOH_READ, func, regaddr,
339 &temp);
340 *data = temp;
341 *data &= 0xff;
342 brcmf_dbg(DATA, "byte read data=0x%02x\n", *data);
343 } else {
344 brcmf_sdioh_request_word(sdiodev, SDIOH_READ, func, regaddr,
345 data, regsize);
346 if (regsize == 2)
347 *data &= 0xffff;
348
349 brcmf_dbg(DATA, "word read data=0x%08x\n", *data);
350 }
351
352 return SUCCESS;
353}
354
355static int brcmf_sdioh_get_cisaddr(struct brcmf_sdio_dev *sdiodev, u32 regaddr)
356{
357 /* read 24 bits and return valid 17 bit addr */
358 int i;
359 u32 scratch, regdata;
360 __le32 scratch_le;
361 u8 *ptr = (u8 *)&scratch_le;
362
363 for (i = 0; i < 3; i++) {
364 if ((brcmf_sdioh_card_regread(sdiodev, 0, regaddr, 1,
365 &regdata)) != SUCCESS)
366 brcmf_dbg(ERROR, "Can't read!\n");
367
368 *ptr++ = (u8) regdata;
369 regaddr++;
370 }
371
372 /* Only the lower 17-bits are valid */
373 scratch = le32_to_cpu(scratch_le);
374 scratch &= 0x0001FFFF;
375 return scratch;
376}
377
378static int brcmf_sdioh_enablefuncs(struct brcmf_sdio_dev *sdiodev)
379{
380 int err_ret;
381 u32 fbraddr;
382 u8 func;
383
384 brcmf_dbg(TRACE, "\n");
385
386 /* Get the Card's common CIS address */
387 sdiodev->func_cis_ptr[0] = brcmf_sdioh_get_cisaddr(sdiodev,
388 SDIO_CCCR_CIS);
389 brcmf_dbg(INFO, "Card's Common CIS Ptr = 0x%x\n",
390 sdiodev->func_cis_ptr[0]);
391
392 /* Get the Card's function CIS (for each function) */
393 for (fbraddr = SDIO_FBR_BASE(1), func = 1;
394 func <= sdiodev->num_funcs; func++, fbraddr += SDIOD_FBR_SIZE) {
395 sdiodev->func_cis_ptr[func] =
396 brcmf_sdioh_get_cisaddr(sdiodev, SDIO_FBR_CIS + fbraddr);
397 brcmf_dbg(INFO, "Function %d CIS Ptr = 0x%x\n",
398 func, sdiodev->func_cis_ptr[func]);
399 }
400
401 /* Enable Function 1 */
402 sdio_claim_host(sdiodev->func[1]);
403 err_ret = sdio_enable_func(sdiodev->func[1]);
404 sdio_release_host(sdiodev->func[1]);
405 if (err_ret)
406 brcmf_dbg(ERROR, "Failed to enable F1 Err: 0x%08x\n", err_ret);
407
408 return false;
409}
410
411/*
412 * Public entry points & extern's
413 */
414int brcmf_sdioh_attach(struct brcmf_sdio_dev *sdiodev)
415{
416 int err_ret = 0;
417
418 brcmf_dbg(TRACE, "\n");
419
420 sdiodev->num_funcs = 2;
421
422 sdio_claim_host(sdiodev->func[1]);
423 err_ret = sdio_set_block_size(sdiodev->func[1], SDIO_FUNC1_BLOCKSIZE);
424 sdio_release_host(sdiodev->func[1]);
425 if (err_ret) {
426 brcmf_dbg(ERROR, "Failed to set F1 blocksize\n");
427 goto out;
428 }
429
430 sdio_claim_host(sdiodev->func[2]);
431 err_ret = sdio_set_block_size(sdiodev->func[2], SDIO_FUNC2_BLOCKSIZE);
432 sdio_release_host(sdiodev->func[2]);
433 if (err_ret) {
434 brcmf_dbg(ERROR, "Failed to set F2 blocksize\n");
435 goto out;
436 }
437
438 brcmf_sdioh_enablefuncs(sdiodev);
439
440out:
441 brcmf_dbg(TRACE, "Done\n");
442 return err_ret;
443}
444
445void brcmf_sdioh_detach(struct brcmf_sdio_dev *sdiodev)
446{
447 brcmf_dbg(TRACE, "\n");
448
449 /* Disable Function 2 */
450 sdio_claim_host(sdiodev->func[2]);
451 sdio_disable_func(sdiodev->func[2]);
452 sdio_release_host(sdiodev->func[2]);
453
454 /* Disable Function 1 */
455 sdio_claim_host(sdiodev->func[1]);
456 sdio_disable_func(sdiodev->func[1]);
457 sdio_release_host(sdiodev->func[1]);
458
459}
460
461static int brcmf_ops_sdio_probe(struct sdio_func *func,
462 const struct sdio_device_id *id)
463{
464 int ret = 0;
465 struct brcmf_sdio_dev *sdiodev;
Franky Lin655713b2011-11-22 17:21:51 -0800466 struct brcmf_bus *bus_if;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200467 brcmf_dbg(TRACE, "Enter\n");
468 brcmf_dbg(TRACE, "func->class=%x\n", func->class);
469 brcmf_dbg(TRACE, "sdio_vendor: 0x%04x\n", func->vendor);
470 brcmf_dbg(TRACE, "sdio_device: 0x%04x\n", func->device);
471 brcmf_dbg(TRACE, "Function#: 0x%04x\n", func->num);
472
473 if (func->num == 1) {
474 if (dev_get_drvdata(&func->card->dev)) {
475 brcmf_dbg(ERROR, "card private drvdata occupied\n");
476 return -ENXIO;
477 }
Franky Lin655713b2011-11-22 17:21:51 -0800478 bus_if = kzalloc(sizeof(struct brcmf_bus), GFP_KERNEL);
479 if (!bus_if)
480 return -ENOMEM;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200481 sdiodev = kzalloc(sizeof(struct brcmf_sdio_dev), GFP_KERNEL);
Dan Carpenterad9547c2011-11-30 11:49:38 +0300482 if (!sdiodev) {
483 kfree(bus_if);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200484 return -ENOMEM;
Dan Carpenterad9547c2011-11-30 11:49:38 +0300485 }
Arend van Spriel5b435de2011-10-05 13:19:03 +0200486 sdiodev->func[0] = func->card->sdio_func[0];
487 sdiodev->func[1] = func;
Franky Lind76d1c82011-12-08 15:06:37 -0800488 sdiodev->bus_if = bus_if;
Franky Lin655713b2011-11-22 17:21:51 -0800489 bus_if->bus_priv = sdiodev;
490 bus_if->type = SDIO_BUS;
Franky Lind76d1c82011-12-08 15:06:37 -0800491 dev_set_drvdata(&func->card->dev, sdiodev);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200492
493 atomic_set(&sdiodev->suspend, false);
494 init_waitqueue_head(&sdiodev->request_byte_wait);
495 init_waitqueue_head(&sdiodev->request_word_wait);
Arend van Spriela52dd172011-11-22 17:21:45 -0800496 init_waitqueue_head(&sdiodev->request_chain_wait);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200497 init_waitqueue_head(&sdiodev->request_buffer_wait);
498 }
499
500 if (func->num == 2) {
Franky Lind76d1c82011-12-08 15:06:37 -0800501 sdiodev = dev_get_drvdata(&func->card->dev);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200502 if ((!sdiodev) || (sdiodev->func[1]->card != func->card))
503 return -ENODEV;
504 sdiodev->func[2] = func;
505
Franky Lind76d1c82011-12-08 15:06:37 -0800506 bus_if = sdiodev->bus_if;
507 sdiodev->dev = &func->dev;
508 dev_set_drvdata(&func->dev, bus_if);
509
Arend van Spriel5b435de2011-10-05 13:19:03 +0200510 brcmf_dbg(TRACE, "F2 found, calling brcmf_sdio_probe...\n");
511 ret = brcmf_sdio_probe(sdiodev);
512 }
513
514 return ret;
515}
516
517static void brcmf_ops_sdio_remove(struct sdio_func *func)
518{
Franky Lin655713b2011-11-22 17:21:51 -0800519 struct brcmf_bus *bus_if;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200520 struct brcmf_sdio_dev *sdiodev;
521 brcmf_dbg(TRACE, "Enter\n");
522 brcmf_dbg(INFO, "func->class=%x\n", func->class);
523 brcmf_dbg(INFO, "sdio_vendor: 0x%04x\n", func->vendor);
524 brcmf_dbg(INFO, "sdio_device: 0x%04x\n", func->device);
525 brcmf_dbg(INFO, "Function#: 0x%04x\n", func->num);
526
527 if (func->num == 2) {
Franky Lind76d1c82011-12-08 15:06:37 -0800528 bus_if = dev_get_drvdata(&func->dev);
Franky Lin655713b2011-11-22 17:21:51 -0800529 sdiodev = bus_if->bus_priv;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200530 brcmf_dbg(TRACE, "F2 found, calling brcmf_sdio_remove...\n");
531 brcmf_sdio_remove(sdiodev);
532 dev_set_drvdata(&func->card->dev, NULL);
Franky Lind76d1c82011-12-08 15:06:37 -0800533 dev_set_drvdata(&func->dev, NULL);
Franky Lin655713b2011-11-22 17:21:51 -0800534 kfree(bus_if);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200535 kfree(sdiodev);
536 }
537}
538
539#ifdef CONFIG_PM_SLEEP
540static int brcmf_sdio_suspend(struct device *dev)
541{
542 mmc_pm_flag_t sdio_flags;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200543 struct sdio_func *func = dev_to_sdio_func(dev);
Franky Lind76d1c82011-12-08 15:06:37 -0800544 struct brcmf_sdio_dev *sdiodev = dev_get_drvdata(&func->card->dev);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200545 int ret = 0;
546
547 brcmf_dbg(TRACE, "\n");
548
Arend van Spriel5b435de2011-10-05 13:19:03 +0200549 atomic_set(&sdiodev->suspend, true);
550
551 sdio_flags = sdio_get_host_pm_caps(sdiodev->func[1]);
552 if (!(sdio_flags & MMC_PM_KEEP_POWER)) {
553 brcmf_dbg(ERROR, "Host can't keep power while suspended\n");
554 return -EINVAL;
555 }
556
557 ret = sdio_set_host_pm_flags(sdiodev->func[1], MMC_PM_KEEP_POWER);
558 if (ret) {
559 brcmf_dbg(ERROR, "Failed to set pm_flags\n");
560 return ret;
561 }
562
563 brcmf_sdio_wdtmr_enable(sdiodev, false);
564
565 return ret;
566}
567
568static int brcmf_sdio_resume(struct device *dev)
569{
Arend van Spriel5b435de2011-10-05 13:19:03 +0200570 struct sdio_func *func = dev_to_sdio_func(dev);
Franky Lind76d1c82011-12-08 15:06:37 -0800571 struct brcmf_sdio_dev *sdiodev = dev_get_drvdata(&func->card->dev);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200572
Arend van Spriel5b435de2011-10-05 13:19:03 +0200573 brcmf_sdio_wdtmr_enable(sdiodev, true);
574 atomic_set(&sdiodev->suspend, false);
575 return 0;
576}
577
578static const struct dev_pm_ops brcmf_sdio_pm_ops = {
579 .suspend = brcmf_sdio_suspend,
580 .resume = brcmf_sdio_resume,
581};
582#endif /* CONFIG_PM_SLEEP */
583
584static struct sdio_driver brcmf_sdmmc_driver = {
585 .probe = brcmf_ops_sdio_probe,
586 .remove = brcmf_ops_sdio_remove,
587 .name = "brcmfmac",
588 .id_table = brcmf_sdmmc_ids,
589#ifdef CONFIG_PM_SLEEP
590 .drv = {
591 .pm = &brcmf_sdio_pm_ops,
592 },
593#endif /* CONFIG_PM_SLEEP */
594};
595
Franky Lin3392c882011-11-22 17:21:47 -0800596static void __exit brcmf_sdio_exit(void)
Arend van Spriel5b435de2011-10-05 13:19:03 +0200597{
598 brcmf_dbg(TRACE, "Enter\n");
599
600 sdio_unregister_driver(&brcmf_sdmmc_driver);
601}
Franky Lin3392c882011-11-22 17:21:47 -0800602
603static int __init brcmf_sdio_init(void)
604{
605 int ret;
606
607 brcmf_dbg(TRACE, "Enter\n");
608
609 ret = sdio_register_driver(&brcmf_sdmmc_driver);
610
611 if (ret)
612 brcmf_dbg(ERROR, "sdio_register_driver failed: %d\n", ret);
613
614 return ret;
615}
616
617module_init(brcmf_sdio_init);
618module_exit(brcmf_sdio_exit);