Linux-libre 4.4.162-gnu
[librecmc/linux-libre.git] / drivers / net / wireless / rsi / rsi_91x_sdio.c
1 /**
2  * Copyright (c) 2014 Redpine Signals Inc.
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
11  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15  *
16  */
17
18 #include <linux/module.h>
19 #include "rsi_sdio.h"
20 #include "rsi_common.h"
21
22 /**
23  * rsi_sdio_set_cmd52_arg() - This function prepares cmd 52 read/write arg.
24  * @rw: Read/write
25  * @func: function number
26  * @raw: indicates whether to perform read after write
27  * @address: address to which to read/write
28  * @writedata: data to write
29  *
30  * Return: argument
31  */
32 static u32 rsi_sdio_set_cmd52_arg(bool rw,
33                                   u8 func,
34                                   u8 raw,
35                                   u32 address,
36                                   u8 writedata)
37 {
38         return ((rw & 1) << 31) | ((func & 0x7) << 28) |
39                 ((raw & 1) << 27) | (1 << 26) |
40                 ((address & 0x1FFFF) << 9) | (1 << 8) |
41                 (writedata & 0xFF);
42 }
43
44 /**
45  * rsi_cmd52writebyte() - This function issues cmd52 byte write onto the card.
46  * @card: Pointer to the mmc_card.
47  * @address: Address to write.
48  * @byte: Data to write.
49  *
50  * Return: Write status.
51  */
52 static int rsi_cmd52writebyte(struct mmc_card *card,
53                               u32 address,
54                               u8 byte)
55 {
56         struct mmc_command io_cmd;
57         u32 arg;
58
59         memset(&io_cmd, 0, sizeof(io_cmd));
60         arg = rsi_sdio_set_cmd52_arg(1, 0, 0, address, byte);
61         io_cmd.opcode = SD_IO_RW_DIRECT;
62         io_cmd.arg = arg;
63         io_cmd.flags = MMC_RSP_R5 | MMC_CMD_AC;
64
65         return mmc_wait_for_cmd(card->host, &io_cmd, 0);
66 }
67
68 /**
69  * rsi_cmd52readbyte() - This function issues cmd52 byte read onto the card.
70  * @card: Pointer to the mmc_card.
71  * @address: Address to read from.
72  * @byte: Variable to store read value.
73  *
74  * Return: Read status.
75  */
76 static int rsi_cmd52readbyte(struct mmc_card *card,
77                              u32 address,
78                              u8 *byte)
79 {
80         struct mmc_command io_cmd;
81         u32 arg;
82         int err;
83
84         memset(&io_cmd, 0, sizeof(io_cmd));
85         arg = rsi_sdio_set_cmd52_arg(0, 0, 0, address, 0);
86         io_cmd.opcode = SD_IO_RW_DIRECT;
87         io_cmd.arg = arg;
88         io_cmd.flags = MMC_RSP_R5 | MMC_CMD_AC;
89
90         err = mmc_wait_for_cmd(card->host, &io_cmd, 0);
91         if ((!err) && (byte))
92                 *byte =  io_cmd.resp[0] & 0xFF;
93         return err;
94 }
95
96 /**
97  * rsi_issue_sdiocommand() - This function issues sdio commands.
98  * @func: Pointer to the sdio_func structure.
99  * @opcode: Opcode value.
100  * @arg: Arguments to pass.
101  * @flags: Flags which are set.
102  * @resp: Pointer to store response.
103  *
104  * Return: err: command status as 0 or -1.
105  */
106 static int rsi_issue_sdiocommand(struct sdio_func *func,
107                                  u32 opcode,
108                                  u32 arg,
109                                  u32 flags,
110                                  u32 *resp)
111 {
112         struct mmc_command cmd;
113         struct mmc_host *host;
114         int err;
115
116         host = func->card->host;
117
118         memset(&cmd, 0, sizeof(struct mmc_command));
119         cmd.opcode = opcode;
120         cmd.arg = arg;
121         cmd.flags = flags;
122         err = mmc_wait_for_cmd(host, &cmd, 3);
123
124         if ((!err) && (resp))
125                 *resp = cmd.resp[0];
126
127         return err;
128 }
129
130 /**
131  * rsi_handle_interrupt() - This function is called upon the occurence
132  *                          of an interrupt.
133  * @function: Pointer to the sdio_func structure.
134  *
135  * Return: None.
136  */
137 static void rsi_handle_interrupt(struct sdio_func *function)
138 {
139         struct rsi_hw *adapter = sdio_get_drvdata(function);
140
141         sdio_release_host(function);
142         rsi_interrupt_handler(adapter);
143         sdio_claim_host(function);
144 }
145
146 /**
147  * rsi_reset_card() - This function resets and re-initializes the card.
148  * @pfunction: Pointer to the sdio_func structure.
149  *
150  * Return: None.
151  */
152 static void rsi_reset_card(struct sdio_func *pfunction)
153 {
154         int ret = 0;
155         int err;
156         struct mmc_card *card = pfunction->card;
157         struct mmc_host *host = card->host;
158         u8 cmd52_resp;
159         u32 clock, resp, i;
160         u16 rca;
161
162         /* Reset 9110 chip */
163         ret = rsi_cmd52writebyte(pfunction->card,
164                                  SDIO_CCCR_ABORT,
165                                  (1 << 3));
166
167         /* Card will not send any response as it is getting reset immediately
168          * Hence expect a timeout status from host controller
169          */
170         if (ret != -ETIMEDOUT)
171                 rsi_dbg(ERR_ZONE, "%s: Reset failed : %d\n", __func__, ret);
172
173         /* Wait for few milli seconds to get rid of residue charges if any */
174         msleep(20);
175
176         /* Initialize the SDIO card */
177         host->ios.chip_select = MMC_CS_DONTCARE;
178         host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
179         host->ios.power_mode = MMC_POWER_UP;
180         host->ios.bus_width = MMC_BUS_WIDTH_1;
181         host->ios.timing = MMC_TIMING_LEGACY;
182         host->ops->set_ios(host, &host->ios);
183
184         /*
185          * This delay should be sufficient to allow the power supply
186          * to reach the minimum voltage.
187          */
188         msleep(20);
189
190         host->ios.clock = host->f_min;
191         host->ios.power_mode = MMC_POWER_ON;
192         host->ops->set_ios(host, &host->ios);
193
194         /*
195          * This delay must be at least 74 clock sizes, or 1 ms, or the
196          * time required to reach a stable voltage.
197          */
198         msleep(20);
199
200         /* Issue CMD0. Goto idle state */
201         host->ios.chip_select = MMC_CS_HIGH;
202         host->ops->set_ios(host, &host->ios);
203         msleep(20);
204         err = rsi_issue_sdiocommand(pfunction,
205                                     MMC_GO_IDLE_STATE,
206                                     0,
207                                     (MMC_RSP_NONE | MMC_CMD_BC),
208                                     NULL);
209         host->ios.chip_select = MMC_CS_DONTCARE;
210         host->ops->set_ios(host, &host->ios);
211         msleep(20);
212         host->use_spi_crc = 0;
213
214         if (err)
215                 rsi_dbg(ERR_ZONE, "%s: CMD0 failed : %d\n", __func__, err);
216
217         if (!host->ocr_avail) {
218                 /* Issue CMD5, arg = 0 */
219                 err = rsi_issue_sdiocommand(pfunction,
220                                             SD_IO_SEND_OP_COND,
221                                             0,
222                                             (MMC_RSP_R4 | MMC_CMD_BCR),
223                                             &resp);
224                 if (err)
225                         rsi_dbg(ERR_ZONE, "%s: CMD5 failed : %d\n",
226                                 __func__, err);
227                 host->ocr_avail = resp;
228         }
229
230         /* Issue CMD5, arg = ocr. Wait till card is ready  */
231         for (i = 0; i < 100; i++) {
232                 err = rsi_issue_sdiocommand(pfunction,
233                                             SD_IO_SEND_OP_COND,
234                                             host->ocr_avail,
235                                             (MMC_RSP_R4 | MMC_CMD_BCR),
236                                             &resp);
237                 if (err) {
238                         rsi_dbg(ERR_ZONE, "%s: CMD5 failed : %d\n",
239                                 __func__, err);
240                         break;
241                 }
242
243                 if (resp & MMC_CARD_BUSY)
244                         break;
245                 msleep(20);
246         }
247
248         if ((i == 100) || (err)) {
249                 rsi_dbg(ERR_ZONE, "%s: card in not ready : %d %d\n",
250                         __func__, i, err);
251                 return;
252         }
253
254         /* Issue CMD3, get RCA */
255         err = rsi_issue_sdiocommand(pfunction,
256                                     SD_SEND_RELATIVE_ADDR,
257                                     0,
258                                     (MMC_RSP_R6 | MMC_CMD_BCR),
259                                     &resp);
260         if (err) {
261                 rsi_dbg(ERR_ZONE, "%s: CMD3 failed : %d\n", __func__, err);
262                 return;
263         }
264         rca = resp >> 16;
265         host->ios.bus_mode = MMC_BUSMODE_PUSHPULL;
266         host->ops->set_ios(host, &host->ios);
267
268         /* Issue CMD7, select card  */
269         err = rsi_issue_sdiocommand(pfunction,
270                                     MMC_SELECT_CARD,
271                                     (rca << 16),
272                                     (MMC_RSP_R1 | MMC_CMD_AC),
273                                     NULL);
274         if (err) {
275                 rsi_dbg(ERR_ZONE, "%s: CMD7 failed : %d\n", __func__, err);
276                 return;
277         }
278
279         /* Enable high speed */
280         if (card->host->caps & MMC_CAP_SD_HIGHSPEED) {
281                 rsi_dbg(ERR_ZONE, "%s: Set high speed mode\n", __func__);
282                 err = rsi_cmd52readbyte(card, SDIO_CCCR_SPEED, &cmd52_resp);
283                 if (err) {
284                         rsi_dbg(ERR_ZONE, "%s: CCCR speed reg read failed: %d\n",
285                                 __func__, err);
286                 } else {
287                         err = rsi_cmd52writebyte(card,
288                                                  SDIO_CCCR_SPEED,
289                                                  (cmd52_resp | SDIO_SPEED_EHS));
290                         if (err) {
291                                 rsi_dbg(ERR_ZONE,
292                                         "%s: CCR speed regwrite failed %d\n",
293                                         __func__, err);
294                                 return;
295                         }
296                         host->ios.timing = MMC_TIMING_SD_HS;
297                         host->ops->set_ios(host, &host->ios);
298                 }
299         }
300
301         /* Set clock */
302         if (mmc_card_hs(card))
303                 clock = 50000000;
304         else
305                 clock = card->cis.max_dtr;
306
307         if (clock > host->f_max)
308                 clock = host->f_max;
309
310         host->ios.clock = clock;
311         host->ops->set_ios(host, &host->ios);
312
313         if (card->host->caps & MMC_CAP_4_BIT_DATA) {
314                 /* CMD52: Set bus width & disable card detect resistor */
315                 err = rsi_cmd52writebyte(card,
316                                          SDIO_CCCR_IF,
317                                          (SDIO_BUS_CD_DISABLE |
318                                           SDIO_BUS_WIDTH_4BIT));
319                 if (err) {
320                         rsi_dbg(ERR_ZONE, "%s: Set bus mode failed : %d\n",
321                                 __func__, err);
322                         return;
323                 }
324                 host->ios.bus_width = MMC_BUS_WIDTH_4;
325                 host->ops->set_ios(host, &host->ios);
326         }
327 }
328
329 /**
330  * rsi_setclock() - This function sets the clock frequency.
331  * @adapter: Pointer to the adapter structure.
332  * @freq: Clock frequency.
333  *
334  * Return: None.
335  */
336 static void rsi_setclock(struct rsi_hw *adapter, u32 freq)
337 {
338         struct rsi_91x_sdiodev *dev =
339                 (struct rsi_91x_sdiodev *)adapter->rsi_dev;
340         struct mmc_host *host = dev->pfunction->card->host;
341         u32 clock;
342
343         clock = freq * 1000;
344         if (clock > host->f_max)
345                 clock = host->f_max;
346         host->ios.clock = clock;
347         host->ops->set_ios(host, &host->ios);
348 }
349
350 /**
351  * rsi_setblocklength() - This function sets the host block length.
352  * @adapter: Pointer to the adapter structure.
353  * @length: Block length to be set.
354  *
355  * Return: status: 0 on success, -1 on failure.
356  */
357 static int rsi_setblocklength(struct rsi_hw *adapter, u32 length)
358 {
359         struct rsi_91x_sdiodev *dev =
360                 (struct rsi_91x_sdiodev *)adapter->rsi_dev;
361         int status;
362         rsi_dbg(INIT_ZONE, "%s: Setting the block length\n", __func__);
363
364         status = sdio_set_block_size(dev->pfunction, length);
365         dev->pfunction->max_blksize = 256;
366
367         rsi_dbg(INFO_ZONE,
368                 "%s: Operational blk length is %d\n", __func__, length);
369         return status;
370 }
371
372 /**
373  * rsi_setupcard() - This function queries and sets the card's features.
374  * @adapter: Pointer to the adapter structure.
375  *
376  * Return: status: 0 on success, -1 on failure.
377  */
378 static int rsi_setupcard(struct rsi_hw *adapter)
379 {
380         struct rsi_91x_sdiodev *dev =
381                 (struct rsi_91x_sdiodev *)adapter->rsi_dev;
382         int status = 0;
383
384         rsi_setclock(adapter, 50000);
385
386         dev->tx_blk_size = 256;
387         status = rsi_setblocklength(adapter, dev->tx_blk_size);
388         if (status)
389                 rsi_dbg(ERR_ZONE,
390                         "%s: Unable to set block length\n", __func__);
391         return status;
392 }
393
394 /**
395  * rsi_sdio_read_register() - This function reads one byte of information
396  *                            from a register.
397  * @adapter: Pointer to the adapter structure.
398  * @addr: Address of the register.
399  * @data: Pointer to the data that stores the data read.
400  *
401  * Return: 0 on success, -1 on failure.
402  */
403 int rsi_sdio_read_register(struct rsi_hw *adapter,
404                            u32 addr,
405                            u8 *data)
406 {
407         struct rsi_91x_sdiodev *dev =
408                 (struct rsi_91x_sdiodev *)adapter->rsi_dev;
409         u8 fun_num = 0;
410         int status;
411
412         sdio_claim_host(dev->pfunction);
413
414         if (fun_num == 0)
415                 *data = sdio_f0_readb(dev->pfunction, addr, &status);
416         else
417                 *data = sdio_readb(dev->pfunction, addr, &status);
418
419         sdio_release_host(dev->pfunction);
420
421         return status;
422 }
423
424 /**
425  * rsi_sdio_write_register() - This function writes one byte of information
426  *                             into a register.
427  * @adapter: Pointer to the adapter structure.
428  * @function: Function Number.
429  * @addr: Address of the register.
430  * @data: Pointer to the data tha has to be written.
431  *
432  * Return: 0 on success, -1 on failure.
433  */
434 int rsi_sdio_write_register(struct rsi_hw *adapter,
435                             u8 function,
436                             u32 addr,
437                             u8 *data)
438 {
439         struct rsi_91x_sdiodev *dev =
440                 (struct rsi_91x_sdiodev *)adapter->rsi_dev;
441         int status = 0;
442
443         sdio_claim_host(dev->pfunction);
444
445         if (function == 0)
446                 sdio_f0_writeb(dev->pfunction, *data, addr, &status);
447         else
448                 sdio_writeb(dev->pfunction, *data, addr, &status);
449
450         sdio_release_host(dev->pfunction);
451
452         return status;
453 }
454
455 /**
456  * rsi_sdio_ack_intr() - This function acks the interrupt received.
457  * @adapter: Pointer to the adapter structure.
458  * @int_bit: Interrupt bit to write into register.
459  *
460  * Return: None.
461  */
462 void rsi_sdio_ack_intr(struct rsi_hw *adapter, u8 int_bit)
463 {
464         int status;
465         status = rsi_sdio_write_register(adapter,
466                                          1,
467                                          (SDIO_FUN1_INTR_CLR_REG |
468                                           RSI_SD_REQUEST_MASTER),
469                                          &int_bit);
470         if (status)
471                 rsi_dbg(ERR_ZONE, "%s: unable to send ack\n", __func__);
472 }
473
474
475
476 /**
477  * rsi_sdio_read_register_multiple() - This function read multiple bytes of
478  *                                     information from the SD card.
479  * @adapter: Pointer to the adapter structure.
480  * @addr: Address of the register.
481  * @count: Number of multiple bytes to be read.
482  * @data: Pointer to the read data.
483  *
484  * Return: 0 on success, -1 on failure.
485  */
486 static int rsi_sdio_read_register_multiple(struct rsi_hw *adapter,
487                                            u32 addr,
488                                            u32 count,
489                                            u8 *data)
490 {
491         struct rsi_91x_sdiodev *dev =
492                 (struct rsi_91x_sdiodev *)adapter->rsi_dev;
493         u32 status;
494
495         sdio_claim_host(dev->pfunction);
496
497         status =  sdio_readsb(dev->pfunction, data, addr, count);
498
499         sdio_release_host(dev->pfunction);
500
501         if (status != 0)
502                 rsi_dbg(ERR_ZONE, "%s: Synch Cmd53 read failed\n", __func__);
503         return status;
504 }
505
506 /**
507  * rsi_sdio_write_register_multiple() - This function writes multiple bytes of
508  *                                      information to the SD card.
509  * @adapter: Pointer to the adapter structure.
510  * @addr: Address of the register.
511  * @data: Pointer to the data that has to be written.
512  * @count: Number of multiple bytes to be written.
513  *
514  * Return: 0 on success, -1 on failure.
515  */
516 int rsi_sdio_write_register_multiple(struct rsi_hw *adapter,
517                                      u32 addr,
518                                      u8 *data,
519                                      u32 count)
520 {
521         struct rsi_91x_sdiodev *dev =
522                 (struct rsi_91x_sdiodev *)adapter->rsi_dev;
523         int status;
524
525         if (dev->write_fail > 1) {
526                 rsi_dbg(ERR_ZONE, "%s: Stopping card writes\n", __func__);
527                 return 0;
528         } else if (dev->write_fail == 1) {
529                 /**
530                  * Assuming it is a CRC failure, we want to allow another
531                  *  card write
532                  */
533                 rsi_dbg(ERR_ZONE, "%s: Continue card writes\n", __func__);
534                 dev->write_fail++;
535         }
536
537         sdio_claim_host(dev->pfunction);
538
539         status = sdio_writesb(dev->pfunction, addr, data, count);
540
541         sdio_release_host(dev->pfunction);
542
543         if (status) {
544                 rsi_dbg(ERR_ZONE, "%s: Synch Cmd53 write failed %d\n",
545                         __func__, status);
546                 dev->write_fail = 2;
547         } else {
548                 memcpy(dev->prev_desc, data, FRAME_DESC_SZ);
549         }
550         return status;
551 }
552
553 /**
554  * rsi_sdio_host_intf_write_pkt() - This function writes the packet to device.
555  * @adapter: Pointer to the adapter structure.
556  * @pkt: Pointer to the data to be written on to the device.
557  * @len: length of the data to be written on to the device.
558  *
559  * Return: 0 on success, -1 on failure.
560  */
561 static int rsi_sdio_host_intf_write_pkt(struct rsi_hw *adapter,
562                                         u8 *pkt,
563                                         u32 len)
564 {
565         struct rsi_91x_sdiodev *dev =
566                 (struct rsi_91x_sdiodev *)adapter->rsi_dev;
567         u32 block_size = dev->tx_blk_size;
568         u32 num_blocks, address, length;
569         u32 queueno;
570         int status;
571
572         queueno = ((pkt[1] >> 4) & 0xf);
573
574         num_blocks = len / block_size;
575
576         if (len % block_size)
577                 num_blocks++;
578
579         address = (num_blocks * block_size | (queueno << 12));
580         length  = num_blocks * block_size;
581
582         status = rsi_sdio_write_register_multiple(adapter,
583                                                   address,
584                                                   (u8 *)pkt,
585                                                   length);
586         if (status)
587                 rsi_dbg(ERR_ZONE, "%s: Unable to write onto the card: %d\n",
588                         __func__, status);
589         rsi_dbg(DATA_TX_ZONE, "%s: Successfully written onto card\n", __func__);
590         return status;
591 }
592
593 /**
594  * rsi_sdio_host_intf_read_pkt() - This function reads the packet
595                                    from the device.
596  * @adapter: Pointer to the adapter data structure.
597  * @pkt: Pointer to the packet data to be read from the the device.
598  * @length: Length of the data to be read from the device.
599  *
600  * Return: 0 on success, -1 on failure.
601  */
602 int rsi_sdio_host_intf_read_pkt(struct rsi_hw *adapter,
603                                 u8 *pkt,
604                                 u32 length)
605 {
606         int status = -EINVAL;
607
608         if (!length) {
609                 rsi_dbg(ERR_ZONE, "%s: Pkt size is zero\n", __func__);
610                 return status;
611         }
612
613         status = rsi_sdio_read_register_multiple(adapter,
614                                                  length,
615                                                  length, /*num of bytes*/
616                                                  (u8 *)pkt);
617
618         if (status)
619                 rsi_dbg(ERR_ZONE, "%s: Failed to read frame: %d\n", __func__,
620                         status);
621         return status;
622 }
623
624 /**
625  * rsi_init_sdio_interface() - This function does init specific to SDIO.
626  *
627  * @adapter: Pointer to the adapter data structure.
628  * @pkt: Pointer to the packet data to be read from the the device.
629  *
630  * Return: 0 on success, -1 on failure.
631  */
632
633 static int rsi_init_sdio_interface(struct rsi_hw *adapter,
634                                    struct sdio_func *pfunction)
635 {
636         struct rsi_91x_sdiodev *rsi_91x_dev;
637         int status = -ENOMEM;
638
639         rsi_91x_dev = kzalloc(sizeof(*rsi_91x_dev), GFP_KERNEL);
640         if (!rsi_91x_dev)
641                 return status;
642
643         adapter->rsi_dev = rsi_91x_dev;
644
645         sdio_claim_host(pfunction);
646
647         pfunction->enable_timeout = 100;
648         status = sdio_enable_func(pfunction);
649         if (status) {
650                 rsi_dbg(ERR_ZONE, "%s: Failed to enable interface\n", __func__);
651                 sdio_release_host(pfunction);
652                 return status;
653         }
654
655         rsi_dbg(INIT_ZONE, "%s: Enabled the interface\n", __func__);
656
657         rsi_91x_dev->pfunction = pfunction;
658         adapter->device = &pfunction->dev;
659
660         sdio_set_drvdata(pfunction, adapter);
661
662         status = rsi_setupcard(adapter);
663         if (status) {
664                 rsi_dbg(ERR_ZONE, "%s: Failed to setup card\n", __func__);
665                 goto fail;
666         }
667
668         rsi_dbg(INIT_ZONE, "%s: Setup card succesfully\n", __func__);
669
670         status = rsi_init_sdio_slave_regs(adapter);
671         if (status) {
672                 rsi_dbg(ERR_ZONE, "%s: Failed to init slave regs\n", __func__);
673                 goto fail;
674         }
675         sdio_release_host(pfunction);
676
677         adapter->host_intf_write_pkt = rsi_sdio_host_intf_write_pkt;
678         adapter->host_intf_read_pkt = rsi_sdio_host_intf_read_pkt;
679         adapter->determine_event_timeout = rsi_sdio_determine_event_timeout;
680         adapter->check_hw_queue_status = rsi_sdio_read_buffer_status_register;
681
682 #ifdef CONFIG_RSI_DEBUGFS
683         adapter->num_debugfs_entries = MAX_DEBUGFS_ENTRIES;
684 #endif
685         return status;
686 fail:
687         sdio_disable_func(pfunction);
688         sdio_release_host(pfunction);
689         return status;
690 }
691
692 /**
693  * rsi_probe() - This function is called by kernel when the driver provided
694  *               Vendor and device IDs are matched. All the initialization
695  *               work is done here.
696  * @pfunction: Pointer to the sdio_func structure.
697  * @id: Pointer to sdio_device_id structure.
698  *
699  * Return: 0 on success, 1 on failure.
700  */
701 static int rsi_probe(struct sdio_func *pfunction,
702                      const struct sdio_device_id *id)
703 {
704         struct rsi_hw *adapter;
705
706         rsi_dbg(INIT_ZONE, "%s: Init function called\n", __func__);
707
708         adapter = rsi_91x_init();
709         if (!adapter) {
710                 rsi_dbg(ERR_ZONE, "%s: Failed to init os intf ops\n",
711                         __func__);
712                 return 1;
713         }
714
715         if (rsi_init_sdio_interface(adapter, pfunction)) {
716                 rsi_dbg(ERR_ZONE, "%s: Failed to init sdio interface\n",
717                         __func__);
718                 goto fail;
719         }
720
721         if (rsi_sdio_device_init(adapter->priv)) {
722                 rsi_dbg(ERR_ZONE, "%s: Failed in device init\n", __func__);
723                 sdio_claim_host(pfunction);
724                 sdio_disable_func(pfunction);
725                 sdio_release_host(pfunction);
726                 goto fail;
727         }
728
729         sdio_claim_host(pfunction);
730         if (sdio_claim_irq(pfunction, rsi_handle_interrupt)) {
731                 rsi_dbg(ERR_ZONE, "%s: Failed to request IRQ\n", __func__);
732                 sdio_release_host(pfunction);
733                 goto fail;
734         }
735
736         sdio_release_host(pfunction);
737         rsi_dbg(INIT_ZONE, "%s: Registered Interrupt handler\n", __func__);
738
739         return 0;
740 fail:
741         rsi_91x_deinit(adapter);
742         rsi_dbg(ERR_ZONE, "%s: Failed in probe...Exiting\n", __func__);
743         return 1;
744 }
745
746 /**
747  * rsi_disconnect() - This function performs the reverse of the probe function.
748  * @pfunction: Pointer to the sdio_func structure.
749  *
750  * Return: void.
751  */
752 static void rsi_disconnect(struct sdio_func *pfunction)
753 {
754         struct rsi_hw *adapter = sdio_get_drvdata(pfunction);
755         struct rsi_91x_sdiodev *dev;
756
757         if (!adapter)
758                 return;
759
760         dev = (struct rsi_91x_sdiodev *)adapter->rsi_dev;
761
762         dev->write_fail = 2;
763         rsi_mac80211_detach(adapter);
764
765         sdio_claim_host(pfunction);
766         sdio_release_irq(pfunction);
767         sdio_disable_func(pfunction);
768         rsi_91x_deinit(adapter);
769         /* Resetting to take care of the case, where-in driver is re-loaded */
770         rsi_reset_card(pfunction);
771         sdio_release_host(pfunction);
772 }
773
774 #ifdef CONFIG_PM
775 static int rsi_suspend(struct device *dev)
776 {
777         /* Not yet implemented */
778         return -ENOSYS;
779 }
780
781 static int rsi_resume(struct device *dev)
782 {
783         /* Not yet implemented */
784         return -ENOSYS;
785 }
786
787 static const struct dev_pm_ops rsi_pm_ops = {
788         .suspend = rsi_suspend,
789         .resume = rsi_resume,
790 };
791 #endif
792
793 static const struct sdio_device_id rsi_dev_table[] =  {
794         { SDIO_DEVICE(0x303, 0x100) },
795         { SDIO_DEVICE(0x041B, 0x0301) },
796         { SDIO_DEVICE(0x041B, 0x0201) },
797         { SDIO_DEVICE(0x041B, 0x9330) },
798         { /* Blank */},
799 };
800
801 static struct sdio_driver rsi_driver = {
802         .name       = "RSI-SDIO WLAN",
803         .probe      = rsi_probe,
804         .remove     = rsi_disconnect,
805         .id_table   = rsi_dev_table,
806 #ifdef CONFIG_PM
807         .drv = {
808                 .pm = &rsi_pm_ops,
809         }
810 #endif
811 };
812
813 /**
814  * rsi_module_init() - This function registers the sdio module.
815  * @void: Void.
816  *
817  * Return: 0 on success.
818  */
819 static int rsi_module_init(void)
820 {
821         int ret;
822
823         ret = sdio_register_driver(&rsi_driver);
824         rsi_dbg(INIT_ZONE, "%s: Registering driver\n", __func__);
825         return ret;
826 }
827
828 /**
829  * rsi_module_exit() - This function unregisters the sdio module.
830  * @void: Void.
831  *
832  * Return: None.
833  */
834 static void rsi_module_exit(void)
835 {
836         sdio_unregister_driver(&rsi_driver);
837         rsi_dbg(INFO_ZONE, "%s: Unregistering driver\n", __func__);
838 }
839
840 module_init(rsi_module_init);
841 module_exit(rsi_module_exit);
842
843 MODULE_AUTHOR("Redpine Signals Inc");
844 MODULE_DESCRIPTION("Common SDIO layer for RSI drivers");
845 MODULE_SUPPORTED_DEVICE("RSI-91x");
846 MODULE_DEVICE_TABLE(sdio, rsi_dev_table);
847 /*(DEBLOBBED)*/
848 MODULE_VERSION("0.1");
849 MODULE_LICENSE("Dual BSD/GPL");