Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 1 | /* |
| 2 | * linux/drivers/net/wireless/libertas/if_sdio.c |
| 3 | * |
Pierre Ossman | ad3868b | 2008-06-28 12:52:45 +0200 | [diff] [blame] | 4 | * Copyright 2007-2008 Pierre Ossman |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 5 | * |
| 6 | * Inspired by if_cs.c, Copyright 2007 Holger Schurig |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; either version 2 of the License, or (at |
| 11 | * your option) any later version. |
| 12 | * |
| 13 | * This hardware has more or less no CMD53 support, so all registers |
| 14 | * must be accessed using sdio_readb()/sdio_writeb(). |
| 15 | * |
| 16 | * Transfers must be in one transaction or the firmware goes bonkers. |
| 17 | * This means that the transfer must either be small enough to do a |
| 18 | * byte based transfer or it must be padded to a multiple of the |
| 19 | * current block size. |
| 20 | * |
| 21 | * As SDIO is still new to the kernel, it is unfortunately common with |
David Woodhouse | 7e22627 | 2007-12-14 22:53:41 -0500 | [diff] [blame] | 22 | * bugs in the host controllers related to that. One such bug is that |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 23 | * controllers cannot do transfers that aren't a multiple of 4 bytes. |
| 24 | * If you don't have time to fix the host controller driver, you can |
| 25 | * work around the problem by modifying if_sdio_host_to_card() and |
| 26 | * if_sdio_card_to_host() to pad the data. |
| 27 | */ |
| 28 | |
Joe Perches | 0e4e06a | 2011-05-02 16:49:14 -0700 | [diff] [blame^] | 29 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 30 | |
Ilpo Järvinen | 2382792 | 2008-12-13 21:33:13 +0200 | [diff] [blame] | 31 | #include <linux/kernel.h> |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 32 | #include <linux/moduleparam.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 33 | #include <linux/slab.h> |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 34 | #include <linux/firmware.h> |
| 35 | #include <linux/netdevice.h> |
| 36 | #include <linux/delay.h> |
| 37 | #include <linux/mmc/card.h> |
| 38 | #include <linux/mmc/sdio_func.h> |
| 39 | #include <linux/mmc/sdio_ids.h> |
Daniel Mack | 8a64c0f | 2010-04-06 10:52:44 +0200 | [diff] [blame] | 40 | #include <linux/mmc/sdio.h> |
| 41 | #include <linux/mmc/host.h> |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 42 | |
| 43 | #include "host.h" |
| 44 | #include "decl.h" |
| 45 | #include "defs.h" |
| 46 | #include "dev.h" |
Bing Zhao | 6bc61f4 | 2009-06-01 18:04:36 -0700 | [diff] [blame] | 47 | #include "cmd.h" |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 48 | #include "if_sdio.h" |
| 49 | |
Bing Zhao | 6bc61f4 | 2009-06-01 18:04:36 -0700 | [diff] [blame] | 50 | /* The if_sdio_remove() callback function is called when |
| 51 | * user removes this module from kernel space or ejects |
| 52 | * the card from the slot. The driver handles these 2 cases |
| 53 | * differently for SD8688 combo chip. |
| 54 | * If the user is removing the module, the FUNC_SHUTDOWN |
| 55 | * command for SD8688 is sent to the firmware. |
| 56 | * If the card is removed, there is no need to send this command. |
| 57 | * |
| 58 | * The variable 'user_rmmod' is used to distinguish these two |
| 59 | * scenarios. This flag is initialized as FALSE in case the card |
| 60 | * is removed, and will be set to TRUE for module removal when |
| 61 | * module_exit function is called. |
| 62 | */ |
| 63 | static u8 user_rmmod; |
| 64 | |
Holger Schurig | 1007832 | 2007-11-15 18:05:47 -0500 | [diff] [blame] | 65 | static char *lbs_helper_name = NULL; |
| 66 | module_param_named(helper_name, lbs_helper_name, charp, 0644); |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 67 | |
Holger Schurig | 1007832 | 2007-11-15 18:05:47 -0500 | [diff] [blame] | 68 | static char *lbs_fw_name = NULL; |
| 69 | module_param_named(fw_name, lbs_fw_name, charp, 0644); |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 70 | |
| 71 | static const struct sdio_device_id if_sdio_ids[] = { |
Bing Zhao | e45d8e5 | 2009-04-06 15:50:56 -0700 | [diff] [blame] | 72 | { SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, |
| 73 | SDIO_DEVICE_ID_MARVELL_LIBERTAS) }, |
| 74 | { SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, |
| 75 | SDIO_DEVICE_ID_MARVELL_8688WLAN) }, |
| 76 | { /* end: all zeroes */ }, |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 77 | }; |
| 78 | |
| 79 | MODULE_DEVICE_TABLE(sdio, if_sdio_ids); |
| 80 | |
Dan Williams | 3d32a58 | 2010-08-07 21:15:52 -0500 | [diff] [blame] | 81 | #define MODEL_8385 0x04 |
| 82 | #define MODEL_8686 0x0b |
| 83 | #define MODEL_8688 0x10 |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 84 | |
Dan Williams | 3d32a58 | 2010-08-07 21:15:52 -0500 | [diff] [blame] | 85 | static const struct lbs_fw_table fw_table[] = { |
| 86 | { MODEL_8385, "libertas/sd8385_helper.bin", "libertas/sd8385.bin" }, |
| 87 | { MODEL_8385, "sd8385_helper.bin", "sd8385.bin" }, |
| 88 | { MODEL_8686, "libertas/sd8686_v9_helper.bin", "libertas/sd8686_v9.bin" }, |
| 89 | { MODEL_8686, "libertas/sd8686_v8_helper.bin", "libertas/sd8686_v8.bin" }, |
| 90 | { MODEL_8686, "sd8686_helper.bin", "sd8686.bin" }, |
| 91 | { MODEL_8688, "libertas/sd8688_helper.bin", "libertas/sd8688.bin" }, |
| 92 | { MODEL_8688, "sd8688_helper.bin", "sd8688.bin" }, |
| 93 | { 0, NULL, NULL } |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 94 | }; |
Dan Williams | 3d32a58 | 2010-08-07 21:15:52 -0500 | [diff] [blame] | 95 | MODULE_FIRMWARE("libertas/sd8385_helper.bin"); |
| 96 | MODULE_FIRMWARE("libertas/sd8385.bin"); |
Ben Hutchings | a974a4b | 2009-11-07 22:00:03 +0000 | [diff] [blame] | 97 | MODULE_FIRMWARE("sd8385_helper.bin"); |
| 98 | MODULE_FIRMWARE("sd8385.bin"); |
Dan Williams | 3d32a58 | 2010-08-07 21:15:52 -0500 | [diff] [blame] | 99 | MODULE_FIRMWARE("libertas/sd8686_v9_helper.bin"); |
| 100 | MODULE_FIRMWARE("libertas/sd8686_v9.bin"); |
| 101 | MODULE_FIRMWARE("libertas/sd8686_v8_helper.bin"); |
| 102 | MODULE_FIRMWARE("libertas/sd8686_v8.bin"); |
Ben Hutchings | a974a4b | 2009-11-07 22:00:03 +0000 | [diff] [blame] | 103 | MODULE_FIRMWARE("sd8686_helper.bin"); |
| 104 | MODULE_FIRMWARE("sd8686.bin"); |
Dan Williams | 3d32a58 | 2010-08-07 21:15:52 -0500 | [diff] [blame] | 105 | MODULE_FIRMWARE("libertas/sd8688_helper.bin"); |
| 106 | MODULE_FIRMWARE("libertas/sd8688.bin"); |
Ben Hutchings | a974a4b | 2009-11-07 22:00:03 +0000 | [diff] [blame] | 107 | MODULE_FIRMWARE("sd8688_helper.bin"); |
| 108 | MODULE_FIRMWARE("sd8688.bin"); |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 109 | |
| 110 | struct if_sdio_packet { |
| 111 | struct if_sdio_packet *next; |
| 112 | u16 nb; |
| 113 | u8 buffer[0] __attribute__((aligned(4))); |
| 114 | }; |
| 115 | |
| 116 | struct if_sdio_card { |
| 117 | struct sdio_func *func; |
Holger Schurig | 69f9032 | 2007-11-23 15:43:44 +0100 | [diff] [blame] | 118 | struct lbs_private *priv; |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 119 | |
| 120 | int model; |
| 121 | unsigned long ioport; |
Bing Zhao | 2c7e579 | 2009-05-21 11:32:34 -0700 | [diff] [blame] | 122 | unsigned int scratch_reg; |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 123 | |
| 124 | const char *helper; |
| 125 | const char *firmware; |
Rusty Russell | 886275c | 2010-08-11 23:04:33 -0600 | [diff] [blame] | 126 | bool helper_allocated; |
| 127 | bool firmware_allocated; |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 128 | |
Mike Rapoport | 557de5e | 2010-08-22 14:22:40 +0300 | [diff] [blame] | 129 | u8 buffer[65536] __attribute__((aligned(4))); |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 130 | |
| 131 | spinlock_t lock; |
| 132 | struct if_sdio_packet *packets; |
Dan Williams | 9b02f41 | 2009-02-20 12:27:38 -0500 | [diff] [blame] | 133 | |
| 134 | struct workqueue_struct *workqueue; |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 135 | struct work_struct packet_worker; |
Bing Zhao | b136a14 | 2009-05-19 19:48:19 -0700 | [diff] [blame] | 136 | |
| 137 | u8 rx_unit; |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 138 | }; |
| 139 | |
| 140 | /********************************************************************/ |
| 141 | /* I/O */ |
| 142 | /********************************************************************/ |
| 143 | |
Bing Zhao | 2c7e579 | 2009-05-21 11:32:34 -0700 | [diff] [blame] | 144 | /* |
| 145 | * For SD8385/SD8686, this function reads firmware status after |
| 146 | * the image is downloaded, or reads RX packet length when |
| 147 | * interrupt (with IF_SDIO_H_INT_UPLD bit set) is received. |
| 148 | * For SD8688, this function reads firmware status only. |
| 149 | */ |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 150 | static u16 if_sdio_read_scratch(struct if_sdio_card *card, int *err) |
| 151 | { |
Bing Zhao | 2c7e579 | 2009-05-21 11:32:34 -0700 | [diff] [blame] | 152 | int ret; |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 153 | u16 scratch; |
| 154 | |
Bing Zhao | 2c7e579 | 2009-05-21 11:32:34 -0700 | [diff] [blame] | 155 | scratch = sdio_readb(card->func, card->scratch_reg, &ret); |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 156 | if (!ret) |
Bing Zhao | 2c7e579 | 2009-05-21 11:32:34 -0700 | [diff] [blame] | 157 | scratch |= sdio_readb(card->func, card->scratch_reg + 1, |
| 158 | &ret) << 8; |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 159 | |
| 160 | if (err) |
| 161 | *err = ret; |
| 162 | |
| 163 | if (ret) |
| 164 | return 0xffff; |
| 165 | |
| 166 | return scratch; |
| 167 | } |
| 168 | |
Bing Zhao | b136a14 | 2009-05-19 19:48:19 -0700 | [diff] [blame] | 169 | static u8 if_sdio_read_rx_unit(struct if_sdio_card *card) |
| 170 | { |
| 171 | int ret; |
| 172 | u8 rx_unit; |
| 173 | |
| 174 | rx_unit = sdio_readb(card->func, IF_SDIO_RX_UNIT, &ret); |
| 175 | |
| 176 | if (ret) |
| 177 | rx_unit = 0; |
| 178 | |
| 179 | return rx_unit; |
| 180 | } |
| 181 | |
| 182 | static u16 if_sdio_read_rx_len(struct if_sdio_card *card, int *err) |
| 183 | { |
| 184 | int ret; |
| 185 | u16 rx_len; |
| 186 | |
| 187 | switch (card->model) { |
Dan Williams | 3d32a58 | 2010-08-07 21:15:52 -0500 | [diff] [blame] | 188 | case MODEL_8385: |
| 189 | case MODEL_8686: |
Bing Zhao | b136a14 | 2009-05-19 19:48:19 -0700 | [diff] [blame] | 190 | rx_len = if_sdio_read_scratch(card, &ret); |
| 191 | break; |
Dan Williams | 3d32a58 | 2010-08-07 21:15:52 -0500 | [diff] [blame] | 192 | case MODEL_8688: |
Bing Zhao | b136a14 | 2009-05-19 19:48:19 -0700 | [diff] [blame] | 193 | default: /* for newer chipsets */ |
| 194 | rx_len = sdio_readb(card->func, IF_SDIO_RX_LEN, &ret); |
| 195 | if (!ret) |
| 196 | rx_len <<= card->rx_unit; |
| 197 | else |
| 198 | rx_len = 0xffff; /* invalid length */ |
| 199 | |
| 200 | break; |
| 201 | } |
| 202 | |
| 203 | if (err) |
| 204 | *err = ret; |
| 205 | |
| 206 | return rx_len; |
| 207 | } |
| 208 | |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 209 | static int if_sdio_handle_cmd(struct if_sdio_card *card, |
| 210 | u8 *buffer, unsigned size) |
| 211 | { |
Holger Schurig | 7919b89 | 2008-04-01 14:50:43 +0200 | [diff] [blame] | 212 | struct lbs_private *priv = card->priv; |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 213 | int ret; |
| 214 | unsigned long flags; |
Holger Schurig | 7919b89 | 2008-04-01 14:50:43 +0200 | [diff] [blame] | 215 | u8 i; |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 216 | |
| 217 | lbs_deb_enter(LBS_DEB_SDIO); |
| 218 | |
Dan Williams | ddac452 | 2007-12-11 13:49:39 -0500 | [diff] [blame] | 219 | if (size > LBS_CMD_BUFFER_SIZE) { |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 220 | lbs_deb_sdio("response packet too large (%d bytes)\n", |
| 221 | (int)size); |
| 222 | ret = -E2BIG; |
| 223 | goto out; |
| 224 | } |
| 225 | |
Holger Schurig | 7919b89 | 2008-04-01 14:50:43 +0200 | [diff] [blame] | 226 | spin_lock_irqsave(&priv->driver_lock, flags); |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 227 | |
Holger Schurig | 7919b89 | 2008-04-01 14:50:43 +0200 | [diff] [blame] | 228 | i = (priv->resp_idx == 0) ? 1 : 0; |
| 229 | BUG_ON(priv->resp_len[i]); |
| 230 | priv->resp_len[i] = size; |
| 231 | memcpy(priv->resp_buf[i], buffer, size); |
| 232 | lbs_notify_command_response(priv, i); |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 233 | |
Holger Schurig | 7919b89 | 2008-04-01 14:50:43 +0200 | [diff] [blame] | 234 | spin_unlock_irqrestore(&card->priv->driver_lock, flags); |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 235 | |
| 236 | ret = 0; |
| 237 | |
| 238 | out: |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 239 | lbs_deb_leave_args(LBS_DEB_SDIO, "ret %d", ret); |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 240 | return ret; |
| 241 | } |
| 242 | |
| 243 | static int if_sdio_handle_data(struct if_sdio_card *card, |
| 244 | u8 *buffer, unsigned size) |
| 245 | { |
| 246 | int ret; |
| 247 | struct sk_buff *skb; |
| 248 | char *data; |
| 249 | |
| 250 | lbs_deb_enter(LBS_DEB_SDIO); |
| 251 | |
| 252 | if (size > MRVDRV_ETH_RX_PACKET_BUFFER_SIZE) { |
| 253 | lbs_deb_sdio("response packet too large (%d bytes)\n", |
| 254 | (int)size); |
| 255 | ret = -E2BIG; |
| 256 | goto out; |
| 257 | } |
| 258 | |
Pierre Ossman | 51e6b71 | 2007-10-22 19:05:32 +0200 | [diff] [blame] | 259 | skb = dev_alloc_skb(MRVDRV_ETH_RX_PACKET_BUFFER_SIZE + NET_IP_ALIGN); |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 260 | if (!skb) { |
| 261 | ret = -ENOMEM; |
| 262 | goto out; |
| 263 | } |
| 264 | |
Pierre Ossman | 51e6b71 | 2007-10-22 19:05:32 +0200 | [diff] [blame] | 265 | skb_reserve(skb, NET_IP_ALIGN); |
| 266 | |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 267 | data = skb_put(skb, size); |
| 268 | |
| 269 | memcpy(data, buffer, size); |
| 270 | |
Holger Schurig | 1007832 | 2007-11-15 18:05:47 -0500 | [diff] [blame] | 271 | lbs_process_rxed_packet(card->priv, skb); |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 272 | |
| 273 | ret = 0; |
| 274 | |
| 275 | out: |
| 276 | lbs_deb_leave_args(LBS_DEB_SDIO, "ret %d", ret); |
| 277 | |
| 278 | return ret; |
| 279 | } |
| 280 | |
| 281 | static int if_sdio_handle_event(struct if_sdio_card *card, |
| 282 | u8 *buffer, unsigned size) |
| 283 | { |
| 284 | int ret; |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 285 | u32 event; |
| 286 | |
| 287 | lbs_deb_enter(LBS_DEB_SDIO); |
| 288 | |
Dan Williams | 3d32a58 | 2010-08-07 21:15:52 -0500 | [diff] [blame] | 289 | if (card->model == MODEL_8385) { |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 290 | event = sdio_readb(card->func, IF_SDIO_EVENT, &ret); |
| 291 | if (ret) |
| 292 | goto out; |
Bing Zhao | f5ac2b9 | 2009-02-04 22:22:39 -0800 | [diff] [blame] | 293 | |
| 294 | /* right shift 3 bits to get the event id */ |
| 295 | event >>= 3; |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 296 | } else { |
| 297 | if (size < 4) { |
| 298 | lbs_deb_sdio("event packet too small (%d bytes)\n", |
| 299 | (int)size); |
| 300 | ret = -EINVAL; |
| 301 | goto out; |
| 302 | } |
| 303 | event = buffer[3] << 24; |
| 304 | event |= buffer[2] << 16; |
| 305 | event |= buffer[1] << 8; |
| 306 | event |= buffer[0] << 0; |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 307 | } |
| 308 | |
Holger Schurig | 7919b89 | 2008-04-01 14:50:43 +0200 | [diff] [blame] | 309 | lbs_queue_event(card->priv, event & 0xFF); |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 310 | ret = 0; |
| 311 | |
| 312 | out: |
| 313 | lbs_deb_leave_args(LBS_DEB_SDIO, "ret %d", ret); |
| 314 | |
| 315 | return ret; |
| 316 | } |
| 317 | |
Dan Williams | 96021f0 | 2010-04-15 13:27:44 -0700 | [diff] [blame] | 318 | static int if_sdio_wait_status(struct if_sdio_card *card, const u8 condition) |
| 319 | { |
| 320 | u8 status; |
| 321 | unsigned long timeout; |
| 322 | int ret = 0; |
| 323 | |
| 324 | timeout = jiffies + HZ; |
| 325 | while (1) { |
| 326 | status = sdio_readb(card->func, IF_SDIO_STATUS, &ret); |
Dan Williams | ca962557 | 2010-04-28 12:37:46 -0700 | [diff] [blame] | 327 | if (ret) |
| 328 | return ret; |
| 329 | if ((status & condition) == condition) |
Dan Williams | 96021f0 | 2010-04-15 13:27:44 -0700 | [diff] [blame] | 330 | break; |
| 331 | if (time_after(jiffies, timeout)) |
| 332 | return -ETIMEDOUT; |
| 333 | mdelay(1); |
| 334 | } |
| 335 | return ret; |
| 336 | } |
| 337 | |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 338 | static int if_sdio_card_to_host(struct if_sdio_card *card) |
| 339 | { |
| 340 | int ret; |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 341 | u16 size, type, chunk; |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 342 | |
| 343 | lbs_deb_enter(LBS_DEB_SDIO); |
| 344 | |
Bing Zhao | b136a14 | 2009-05-19 19:48:19 -0700 | [diff] [blame] | 345 | size = if_sdio_read_rx_len(card, &ret); |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 346 | if (ret) |
| 347 | goto out; |
| 348 | |
| 349 | if (size < 4) { |
| 350 | lbs_deb_sdio("invalid packet size (%d bytes) from firmware\n", |
| 351 | (int)size); |
| 352 | ret = -EINVAL; |
| 353 | goto out; |
| 354 | } |
| 355 | |
Dan Williams | 96021f0 | 2010-04-15 13:27:44 -0700 | [diff] [blame] | 356 | ret = if_sdio_wait_status(card, IF_SDIO_IO_RDY); |
| 357 | if (ret) |
| 358 | goto out; |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 359 | |
| 360 | /* |
| 361 | * The transfer must be in one transaction or the firmware |
Pierre Ossman | ad3868b | 2008-06-28 12:52:45 +0200 | [diff] [blame] | 362 | * goes suicidal. There's no way to guarantee that for all |
| 363 | * controllers, but we can at least try. |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 364 | */ |
Pierre Ossman | ad3868b | 2008-06-28 12:52:45 +0200 | [diff] [blame] | 365 | chunk = sdio_align_size(card->func, size); |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 366 | |
| 367 | ret = sdio_readsb(card->func, card->buffer, card->ioport, chunk); |
| 368 | if (ret) |
| 369 | goto out; |
| 370 | |
| 371 | chunk = card->buffer[0] | (card->buffer[1] << 8); |
| 372 | type = card->buffer[2] | (card->buffer[3] << 8); |
| 373 | |
| 374 | lbs_deb_sdio("packet of type %d and size %d bytes\n", |
| 375 | (int)type, (int)chunk); |
| 376 | |
| 377 | if (chunk > size) { |
| 378 | lbs_deb_sdio("packet fragment (%d > %d)\n", |
| 379 | (int)chunk, (int)size); |
| 380 | ret = -EINVAL; |
| 381 | goto out; |
| 382 | } |
| 383 | |
| 384 | if (chunk < size) { |
| 385 | lbs_deb_sdio("packet fragment (%d < %d)\n", |
| 386 | (int)chunk, (int)size); |
| 387 | } |
| 388 | |
| 389 | switch (type) { |
| 390 | case MVMS_CMD: |
| 391 | ret = if_sdio_handle_cmd(card, card->buffer + 4, chunk - 4); |
| 392 | if (ret) |
| 393 | goto out; |
| 394 | break; |
| 395 | case MVMS_DAT: |
| 396 | ret = if_sdio_handle_data(card, card->buffer + 4, chunk - 4); |
| 397 | if (ret) |
| 398 | goto out; |
| 399 | break; |
| 400 | case MVMS_EVENT: |
| 401 | ret = if_sdio_handle_event(card, card->buffer + 4, chunk - 4); |
| 402 | if (ret) |
| 403 | goto out; |
| 404 | break; |
| 405 | default: |
| 406 | lbs_deb_sdio("invalid type (%d) from firmware\n", |
| 407 | (int)type); |
| 408 | ret = -EINVAL; |
| 409 | goto out; |
| 410 | } |
| 411 | |
| 412 | out: |
| 413 | if (ret) |
Joe Perches | 0e4e06a | 2011-05-02 16:49:14 -0700 | [diff] [blame^] | 414 | pr_err("problem fetching packet from firmware\n"); |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 415 | |
| 416 | lbs_deb_leave_args(LBS_DEB_SDIO, "ret %d", ret); |
| 417 | |
| 418 | return ret; |
| 419 | } |
| 420 | |
| 421 | static void if_sdio_host_to_card_worker(struct work_struct *work) |
| 422 | { |
| 423 | struct if_sdio_card *card; |
| 424 | struct if_sdio_packet *packet; |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 425 | int ret; |
| 426 | unsigned long flags; |
| 427 | |
| 428 | lbs_deb_enter(LBS_DEB_SDIO); |
| 429 | |
| 430 | card = container_of(work, struct if_sdio_card, packet_worker); |
| 431 | |
| 432 | while (1) { |
| 433 | spin_lock_irqsave(&card->lock, flags); |
| 434 | packet = card->packets; |
| 435 | if (packet) |
| 436 | card->packets = packet->next; |
| 437 | spin_unlock_irqrestore(&card->lock, flags); |
| 438 | |
| 439 | if (!packet) |
| 440 | break; |
| 441 | |
| 442 | sdio_claim_host(card->func); |
| 443 | |
Dan Williams | 96021f0 | 2010-04-15 13:27:44 -0700 | [diff] [blame] | 444 | ret = if_sdio_wait_status(card, IF_SDIO_IO_RDY); |
| 445 | if (ret == 0) { |
| 446 | ret = sdio_writesb(card->func, card->ioport, |
| 447 | packet->buffer, packet->nb); |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 448 | } |
| 449 | |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 450 | if (ret) |
Joe Perches | 0e4e06a | 2011-05-02 16:49:14 -0700 | [diff] [blame^] | 451 | pr_err("error %d sending packet to firmware\n", ret); |
Dan Williams | 96021f0 | 2010-04-15 13:27:44 -0700 | [diff] [blame] | 452 | |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 453 | sdio_release_host(card->func); |
| 454 | |
| 455 | kfree(packet); |
| 456 | } |
| 457 | |
| 458 | lbs_deb_leave(LBS_DEB_SDIO); |
| 459 | } |
| 460 | |
| 461 | /********************************************************************/ |
| 462 | /* Firmware */ |
| 463 | /********************************************************************/ |
| 464 | |
Dan Williams | 96021f0 | 2010-04-15 13:27:44 -0700 | [diff] [blame] | 465 | #define FW_DL_READY_STATUS (IF_SDIO_IO_RDY | IF_SDIO_DL_RDY) |
| 466 | |
Dan Williams | 3d32a58 | 2010-08-07 21:15:52 -0500 | [diff] [blame] | 467 | static int if_sdio_prog_helper(struct if_sdio_card *card, |
| 468 | const struct firmware *fw) |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 469 | { |
| 470 | int ret; |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 471 | unsigned long timeout; |
| 472 | u8 *chunk_buffer; |
| 473 | u32 chunk_size; |
David Woodhouse | 6dfff89 | 2008-05-23 18:37:51 +0100 | [diff] [blame] | 474 | const u8 *firmware; |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 475 | size_t size; |
| 476 | |
| 477 | lbs_deb_enter(LBS_DEB_SDIO); |
| 478 | |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 479 | chunk_buffer = kzalloc(64, GFP_KERNEL); |
| 480 | if (!chunk_buffer) { |
| 481 | ret = -ENOMEM; |
Dan Williams | 3d32a58 | 2010-08-07 21:15:52 -0500 | [diff] [blame] | 482 | goto out; |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 483 | } |
| 484 | |
| 485 | sdio_claim_host(card->func); |
| 486 | |
| 487 | ret = sdio_set_block_size(card->func, 32); |
| 488 | if (ret) |
| 489 | goto release; |
| 490 | |
| 491 | firmware = fw->data; |
| 492 | size = fw->size; |
| 493 | |
| 494 | while (size) { |
Dan Williams | 96021f0 | 2010-04-15 13:27:44 -0700 | [diff] [blame] | 495 | ret = if_sdio_wait_status(card, FW_DL_READY_STATUS); |
| 496 | if (ret) |
| 497 | goto release; |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 498 | |
Dan Williams | ac1a474 | 2010-04-15 13:32:58 -0700 | [diff] [blame] | 499 | /* On some platforms (like Davinci) the chip needs more time |
| 500 | * between helper blocks. |
| 501 | */ |
| 502 | mdelay(2); |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 503 | |
| 504 | chunk_size = min(size, (size_t)60); |
| 505 | |
Holger Schurig | c2df2ef | 2007-12-07 15:30:44 +0000 | [diff] [blame] | 506 | *((__le32*)chunk_buffer) = cpu_to_le32(chunk_size); |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 507 | memcpy(chunk_buffer + 4, firmware, chunk_size); |
| 508 | /* |
| 509 | lbs_deb_sdio("sending %d bytes chunk\n", chunk_size); |
| 510 | */ |
| 511 | ret = sdio_writesb(card->func, card->ioport, |
| 512 | chunk_buffer, 64); |
| 513 | if (ret) |
| 514 | goto release; |
| 515 | |
| 516 | firmware += chunk_size; |
| 517 | size -= chunk_size; |
| 518 | } |
| 519 | |
| 520 | /* an empty block marks the end of the transfer */ |
| 521 | memset(chunk_buffer, 0, 4); |
| 522 | ret = sdio_writesb(card->func, card->ioport, chunk_buffer, 64); |
| 523 | if (ret) |
| 524 | goto release; |
| 525 | |
| 526 | lbs_deb_sdio("waiting for helper to boot...\n"); |
| 527 | |
| 528 | /* wait for the helper to boot by looking at the size register */ |
| 529 | timeout = jiffies + HZ; |
| 530 | while (1) { |
| 531 | u16 req_size; |
| 532 | |
| 533 | req_size = sdio_readb(card->func, IF_SDIO_RD_BASE, &ret); |
| 534 | if (ret) |
| 535 | goto release; |
| 536 | |
| 537 | req_size |= sdio_readb(card->func, IF_SDIO_RD_BASE + 1, &ret) << 8; |
| 538 | if (ret) |
| 539 | goto release; |
| 540 | |
| 541 | if (req_size != 0) |
| 542 | break; |
| 543 | |
| 544 | if (time_after(jiffies, timeout)) { |
| 545 | ret = -ETIMEDOUT; |
| 546 | goto release; |
| 547 | } |
| 548 | |
| 549 | msleep(10); |
| 550 | } |
| 551 | |
| 552 | ret = 0; |
| 553 | |
| 554 | release: |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 555 | sdio_release_host(card->func); |
| 556 | kfree(chunk_buffer); |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 557 | |
| 558 | out: |
| 559 | if (ret) |
Joe Perches | 0e4e06a | 2011-05-02 16:49:14 -0700 | [diff] [blame^] | 560 | pr_err("failed to load helper firmware\n"); |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 561 | |
| 562 | lbs_deb_leave_args(LBS_DEB_SDIO, "ret %d", ret); |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 563 | return ret; |
| 564 | } |
| 565 | |
Dan Williams | 3d32a58 | 2010-08-07 21:15:52 -0500 | [diff] [blame] | 566 | static int if_sdio_prog_real(struct if_sdio_card *card, |
| 567 | const struct firmware *fw) |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 568 | { |
| 569 | int ret; |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 570 | unsigned long timeout; |
| 571 | u8 *chunk_buffer; |
| 572 | u32 chunk_size; |
David Woodhouse | 6dfff89 | 2008-05-23 18:37:51 +0100 | [diff] [blame] | 573 | const u8 *firmware; |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 574 | size_t size, req_size; |
| 575 | |
| 576 | lbs_deb_enter(LBS_DEB_SDIO); |
| 577 | |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 578 | chunk_buffer = kzalloc(512, GFP_KERNEL); |
| 579 | if (!chunk_buffer) { |
| 580 | ret = -ENOMEM; |
Dan Williams | 3d32a58 | 2010-08-07 21:15:52 -0500 | [diff] [blame] | 581 | goto out; |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 582 | } |
| 583 | |
| 584 | sdio_claim_host(card->func); |
| 585 | |
| 586 | ret = sdio_set_block_size(card->func, 32); |
| 587 | if (ret) |
| 588 | goto release; |
| 589 | |
| 590 | firmware = fw->data; |
| 591 | size = fw->size; |
| 592 | |
| 593 | while (size) { |
Dan Williams | 96021f0 | 2010-04-15 13:27:44 -0700 | [diff] [blame] | 594 | ret = if_sdio_wait_status(card, FW_DL_READY_STATUS); |
| 595 | if (ret) |
| 596 | goto release; |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 597 | |
| 598 | req_size = sdio_readb(card->func, IF_SDIO_RD_BASE, &ret); |
| 599 | if (ret) |
| 600 | goto release; |
| 601 | |
| 602 | req_size |= sdio_readb(card->func, IF_SDIO_RD_BASE + 1, &ret) << 8; |
| 603 | if (ret) |
| 604 | goto release; |
| 605 | /* |
| 606 | lbs_deb_sdio("firmware wants %d bytes\n", (int)req_size); |
| 607 | */ |
| 608 | if (req_size == 0) { |
| 609 | lbs_deb_sdio("firmware helper gave up early\n"); |
| 610 | ret = -EIO; |
| 611 | goto release; |
| 612 | } |
| 613 | |
| 614 | if (req_size & 0x01) { |
| 615 | lbs_deb_sdio("firmware helper signalled error\n"); |
| 616 | ret = -EIO; |
| 617 | goto release; |
| 618 | } |
| 619 | |
| 620 | if (req_size > size) |
| 621 | req_size = size; |
| 622 | |
| 623 | while (req_size) { |
| 624 | chunk_size = min(req_size, (size_t)512); |
| 625 | |
| 626 | memcpy(chunk_buffer, firmware, chunk_size); |
| 627 | /* |
| 628 | lbs_deb_sdio("sending %d bytes (%d bytes) chunk\n", |
| 629 | chunk_size, (chunk_size + 31) / 32 * 32); |
| 630 | */ |
| 631 | ret = sdio_writesb(card->func, card->ioport, |
Ilpo Järvinen | 2382792 | 2008-12-13 21:33:13 +0200 | [diff] [blame] | 632 | chunk_buffer, roundup(chunk_size, 32)); |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 633 | if (ret) |
| 634 | goto release; |
| 635 | |
| 636 | firmware += chunk_size; |
| 637 | size -= chunk_size; |
| 638 | req_size -= chunk_size; |
| 639 | } |
| 640 | } |
| 641 | |
| 642 | ret = 0; |
| 643 | |
| 644 | lbs_deb_sdio("waiting for firmware to boot...\n"); |
| 645 | |
| 646 | /* wait for the firmware to boot */ |
| 647 | timeout = jiffies + HZ; |
| 648 | while (1) { |
| 649 | u16 scratch; |
| 650 | |
| 651 | scratch = if_sdio_read_scratch(card, &ret); |
| 652 | if (ret) |
| 653 | goto release; |
| 654 | |
| 655 | if (scratch == IF_SDIO_FIRMWARE_OK) |
| 656 | break; |
| 657 | |
| 658 | if (time_after(jiffies, timeout)) { |
| 659 | ret = -ETIMEDOUT; |
| 660 | goto release; |
| 661 | } |
| 662 | |
| 663 | msleep(10); |
| 664 | } |
| 665 | |
| 666 | ret = 0; |
| 667 | |
| 668 | release: |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 669 | sdio_release_host(card->func); |
| 670 | kfree(chunk_buffer); |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 671 | |
| 672 | out: |
| 673 | if (ret) |
Joe Perches | 0e4e06a | 2011-05-02 16:49:14 -0700 | [diff] [blame^] | 674 | pr_err("failed to load firmware\n"); |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 675 | |
| 676 | lbs_deb_leave_args(LBS_DEB_SDIO, "ret %d", ret); |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 677 | return ret; |
| 678 | } |
| 679 | |
| 680 | static int if_sdio_prog_firmware(struct if_sdio_card *card) |
| 681 | { |
| 682 | int ret; |
| 683 | u16 scratch; |
Dan Williams | 3d32a58 | 2010-08-07 21:15:52 -0500 | [diff] [blame] | 684 | const struct firmware *helper = NULL; |
| 685 | const struct firmware *mainfw = NULL; |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 686 | |
| 687 | lbs_deb_enter(LBS_DEB_SDIO); |
| 688 | |
Paul Fox | 731b203 | 2010-10-29 14:57:28 +0100 | [diff] [blame] | 689 | /* |
| 690 | * Disable interrupts |
| 691 | */ |
| 692 | sdio_claim_host(card->func); |
| 693 | sdio_writeb(card->func, 0x00, IF_SDIO_H_INT_MASK, &ret); |
| 694 | sdio_release_host(card->func); |
| 695 | |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 696 | sdio_claim_host(card->func); |
| 697 | scratch = if_sdio_read_scratch(card, &ret); |
| 698 | sdio_release_host(card->func); |
| 699 | |
Paul Fox | 731b203 | 2010-10-29 14:57:28 +0100 | [diff] [blame] | 700 | lbs_deb_sdio("firmware status = %#x\n", scratch); |
| 701 | lbs_deb_sdio("scratch ret = %d\n", ret); |
| 702 | |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 703 | if (ret) |
| 704 | goto out; |
| 705 | |
Bing Zhao | 2c7e579 | 2009-05-21 11:32:34 -0700 | [diff] [blame] | 706 | |
Paul Fox | 731b203 | 2010-10-29 14:57:28 +0100 | [diff] [blame] | 707 | /* |
| 708 | * The manual clearly describes that FEDC is the right code to use |
| 709 | * to detect firmware presence, but for SD8686 it is not that simple. |
| 710 | * Scratch is also used to store the RX packet length, so we lose |
| 711 | * the FEDC value early on. So we use a non-zero check in order |
| 712 | * to validate firmware presence. |
| 713 | * Additionally, the SD8686 in the Gumstix always has the high scratch |
| 714 | * bit set, even when the firmware is not loaded. So we have to |
| 715 | * exclude that from the test. |
| 716 | */ |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 717 | if (scratch == IF_SDIO_FIRMWARE_OK) { |
| 718 | lbs_deb_sdio("firmware already loaded\n"); |
| 719 | goto success; |
Paul Fox | 731b203 | 2010-10-29 14:57:28 +0100 | [diff] [blame] | 720 | } else if ((card->model == MODEL_8686) && (scratch & 0x7fff)) { |
| 721 | lbs_deb_sdio("firmware may be running\n"); |
| 722 | goto success; |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 723 | } |
| 724 | |
Dan Williams | 3d32a58 | 2010-08-07 21:15:52 -0500 | [diff] [blame] | 725 | ret = lbs_get_firmware(&card->func->dev, lbs_helper_name, lbs_fw_name, |
| 726 | card->model, &fw_table[0], &helper, &mainfw); |
| 727 | if (ret) { |
Joe Perches | 0e4e06a | 2011-05-02 16:49:14 -0700 | [diff] [blame^] | 728 | pr_err("failed to find firmware (%d)\n", ret); |
Dan Williams | 3d32a58 | 2010-08-07 21:15:52 -0500 | [diff] [blame] | 729 | goto out; |
| 730 | } |
| 731 | |
| 732 | ret = if_sdio_prog_helper(card, helper); |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 733 | if (ret) |
| 734 | goto out; |
| 735 | |
Paul Fox | 731b203 | 2010-10-29 14:57:28 +0100 | [diff] [blame] | 736 | lbs_deb_sdio("Helper firmware loaded\n"); |
| 737 | |
Dan Williams | 3d32a58 | 2010-08-07 21:15:52 -0500 | [diff] [blame] | 738 | ret = if_sdio_prog_real(card, mainfw); |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 739 | if (ret) |
| 740 | goto out; |
| 741 | |
Paul Fox | 731b203 | 2010-10-29 14:57:28 +0100 | [diff] [blame] | 742 | lbs_deb_sdio("Firmware loaded\n"); |
| 743 | |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 744 | success: |
Bing Zhao | d26285f8 | 2009-05-19 19:48:20 -0700 | [diff] [blame] | 745 | sdio_claim_host(card->func); |
| 746 | sdio_set_block_size(card->func, IF_SDIO_BLOCK_SIZE); |
| 747 | sdio_release_host(card->func); |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 748 | ret = 0; |
| 749 | |
| 750 | out: |
Dan Williams | 3d32a58 | 2010-08-07 21:15:52 -0500 | [diff] [blame] | 751 | if (helper) |
| 752 | release_firmware(helper); |
| 753 | if (mainfw) |
| 754 | release_firmware(mainfw); |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 755 | |
Dan Williams | 3d32a58 | 2010-08-07 21:15:52 -0500 | [diff] [blame] | 756 | lbs_deb_leave_args(LBS_DEB_SDIO, "ret %d", ret); |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 757 | return ret; |
| 758 | } |
| 759 | |
| 760 | /*******************************************************************/ |
| 761 | /* Libertas callbacks */ |
| 762 | /*******************************************************************/ |
| 763 | |
Holger Schurig | 69f9032 | 2007-11-23 15:43:44 +0100 | [diff] [blame] | 764 | static int if_sdio_host_to_card(struct lbs_private *priv, |
| 765 | u8 type, u8 *buf, u16 nb) |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 766 | { |
| 767 | int ret; |
| 768 | struct if_sdio_card *card; |
| 769 | struct if_sdio_packet *packet, *cur; |
| 770 | u16 size; |
| 771 | unsigned long flags; |
| 772 | |
| 773 | lbs_deb_enter_args(LBS_DEB_SDIO, "type %d, bytes %d", type, nb); |
| 774 | |
| 775 | card = priv->card; |
| 776 | |
| 777 | if (nb > (65536 - sizeof(struct if_sdio_packet) - 4)) { |
| 778 | ret = -EINVAL; |
| 779 | goto out; |
| 780 | } |
| 781 | |
| 782 | /* |
| 783 | * The transfer must be in one transaction or the firmware |
Pierre Ossman | ad3868b | 2008-06-28 12:52:45 +0200 | [diff] [blame] | 784 | * goes suicidal. There's no way to guarantee that for all |
| 785 | * controllers, but we can at least try. |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 786 | */ |
Pierre Ossman | ad3868b | 2008-06-28 12:52:45 +0200 | [diff] [blame] | 787 | size = sdio_align_size(card->func, nb + 4); |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 788 | |
| 789 | packet = kzalloc(sizeof(struct if_sdio_packet) + size, |
| 790 | GFP_ATOMIC); |
| 791 | if (!packet) { |
| 792 | ret = -ENOMEM; |
| 793 | goto out; |
| 794 | } |
| 795 | |
| 796 | packet->next = NULL; |
| 797 | packet->nb = size; |
| 798 | |
| 799 | /* |
| 800 | * SDIO specific header. |
| 801 | */ |
| 802 | packet->buffer[0] = (nb + 4) & 0xff; |
| 803 | packet->buffer[1] = ((nb + 4) >> 8) & 0xff; |
| 804 | packet->buffer[2] = type; |
| 805 | packet->buffer[3] = 0; |
| 806 | |
| 807 | memcpy(packet->buffer + 4, buf, nb); |
| 808 | |
| 809 | spin_lock_irqsave(&card->lock, flags); |
| 810 | |
| 811 | if (!card->packets) |
| 812 | card->packets = packet; |
| 813 | else { |
| 814 | cur = card->packets; |
| 815 | while (cur->next) |
| 816 | cur = cur->next; |
| 817 | cur->next = packet; |
| 818 | } |
| 819 | |
| 820 | switch (type) { |
| 821 | case MVMS_CMD: |
| 822 | priv->dnld_sent = DNLD_CMD_SENT; |
| 823 | break; |
| 824 | case MVMS_DAT: |
| 825 | priv->dnld_sent = DNLD_DATA_SENT; |
| 826 | break; |
| 827 | default: |
| 828 | lbs_deb_sdio("unknown packet type %d\n", (int)type); |
| 829 | } |
| 830 | |
| 831 | spin_unlock_irqrestore(&card->lock, flags); |
| 832 | |
Dan Williams | 9b02f41 | 2009-02-20 12:27:38 -0500 | [diff] [blame] | 833 | queue_work(card->workqueue, &card->packet_worker); |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 834 | |
| 835 | ret = 0; |
| 836 | |
| 837 | out: |
| 838 | lbs_deb_leave_args(LBS_DEB_SDIO, "ret %d", ret); |
| 839 | |
| 840 | return ret; |
| 841 | } |
| 842 | |
Amitkumar Karwar | 4912545 | 2009-09-30 20:04:38 -0700 | [diff] [blame] | 843 | static int if_sdio_enter_deep_sleep(struct lbs_private *priv) |
| 844 | { |
| 845 | int ret = -1; |
| 846 | struct cmd_header cmd; |
| 847 | |
| 848 | memset(&cmd, 0, sizeof(cmd)); |
| 849 | |
| 850 | lbs_deb_sdio("send DEEP_SLEEP command\n"); |
| 851 | ret = __lbs_cmd(priv, CMD_802_11_DEEP_SLEEP, &cmd, sizeof(cmd), |
| 852 | lbs_cmd_copyback, (unsigned long) &cmd); |
| 853 | if (ret) |
Joe Perches | 0e4e06a | 2011-05-02 16:49:14 -0700 | [diff] [blame^] | 854 | pr_err("DEEP_SLEEP cmd failed\n"); |
Amitkumar Karwar | 4912545 | 2009-09-30 20:04:38 -0700 | [diff] [blame] | 855 | |
| 856 | mdelay(200); |
| 857 | return ret; |
| 858 | } |
| 859 | |
| 860 | static int if_sdio_exit_deep_sleep(struct lbs_private *priv) |
| 861 | { |
| 862 | struct if_sdio_card *card = priv->card; |
| 863 | int ret = -1; |
| 864 | |
| 865 | lbs_deb_enter(LBS_DEB_SDIO); |
| 866 | sdio_claim_host(card->func); |
| 867 | |
| 868 | sdio_writeb(card->func, HOST_POWER_UP, CONFIGURATION_REG, &ret); |
| 869 | if (ret) |
Joe Perches | 0e4e06a | 2011-05-02 16:49:14 -0700 | [diff] [blame^] | 870 | pr_err("sdio_writeb failed!\n"); |
Amitkumar Karwar | 4912545 | 2009-09-30 20:04:38 -0700 | [diff] [blame] | 871 | |
| 872 | sdio_release_host(card->func); |
| 873 | lbs_deb_leave_args(LBS_DEB_SDIO, "ret %d", ret); |
| 874 | return ret; |
| 875 | } |
| 876 | |
| 877 | static int if_sdio_reset_deep_sleep_wakeup(struct lbs_private *priv) |
| 878 | { |
| 879 | struct if_sdio_card *card = priv->card; |
| 880 | int ret = -1; |
| 881 | |
| 882 | lbs_deb_enter(LBS_DEB_SDIO); |
| 883 | sdio_claim_host(card->func); |
| 884 | |
| 885 | sdio_writeb(card->func, 0, CONFIGURATION_REG, &ret); |
| 886 | if (ret) |
Joe Perches | 0e4e06a | 2011-05-02 16:49:14 -0700 | [diff] [blame^] | 887 | pr_err("sdio_writeb failed!\n"); |
Amitkumar Karwar | 4912545 | 2009-09-30 20:04:38 -0700 | [diff] [blame] | 888 | |
| 889 | sdio_release_host(card->func); |
| 890 | lbs_deb_leave_args(LBS_DEB_SDIO, "ret %d", ret); |
| 891 | return ret; |
| 892 | |
| 893 | } |
| 894 | |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 895 | /*******************************************************************/ |
| 896 | /* SDIO callbacks */ |
| 897 | /*******************************************************************/ |
| 898 | |
| 899 | static void if_sdio_interrupt(struct sdio_func *func) |
| 900 | { |
| 901 | int ret; |
| 902 | struct if_sdio_card *card; |
| 903 | u8 cause; |
| 904 | |
| 905 | lbs_deb_enter(LBS_DEB_SDIO); |
| 906 | |
| 907 | card = sdio_get_drvdata(func); |
| 908 | |
| 909 | cause = sdio_readb(card->func, IF_SDIO_H_INT_STATUS, &ret); |
| 910 | if (ret) |
| 911 | goto out; |
| 912 | |
| 913 | lbs_deb_sdio("interrupt: 0x%X\n", (unsigned)cause); |
| 914 | |
| 915 | sdio_writeb(card->func, ~cause, IF_SDIO_H_INT_STATUS, &ret); |
| 916 | if (ret) |
| 917 | goto out; |
| 918 | |
| 919 | /* |
| 920 | * Ignore the define name, this really means the card has |
| 921 | * successfully received the command. |
| 922 | */ |
Amitkumar Karwar | 4912545 | 2009-09-30 20:04:38 -0700 | [diff] [blame] | 923 | card->priv->is_activity_detected = 1; |
David Woodhouse | e775ed7 | 2007-12-06 14:36:11 +0000 | [diff] [blame] | 924 | if (cause & IF_SDIO_H_INT_DNLD) |
| 925 | lbs_host_to_card_done(card->priv); |
| 926 | |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 927 | |
| 928 | if (cause & IF_SDIO_H_INT_UPLD) { |
| 929 | ret = if_sdio_card_to_host(card); |
| 930 | if (ret) |
| 931 | goto out; |
| 932 | } |
| 933 | |
| 934 | ret = 0; |
| 935 | |
| 936 | out: |
| 937 | lbs_deb_leave_args(LBS_DEB_SDIO, "ret %d", ret); |
| 938 | } |
| 939 | |
| 940 | static int if_sdio_probe(struct sdio_func *func, |
| 941 | const struct sdio_device_id *id) |
| 942 | { |
| 943 | struct if_sdio_card *card; |
Holger Schurig | 69f9032 | 2007-11-23 15:43:44 +0100 | [diff] [blame] | 944 | struct lbs_private *priv; |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 945 | int ret, i; |
| 946 | unsigned int model; |
| 947 | struct if_sdio_packet *packet; |
Daniel Mack | 8a64c0f | 2010-04-06 10:52:44 +0200 | [diff] [blame] | 948 | struct mmc_host *host = func->card->host; |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 949 | |
| 950 | lbs_deb_enter(LBS_DEB_SDIO); |
| 951 | |
| 952 | for (i = 0;i < func->card->num_info;i++) { |
| 953 | if (sscanf(func->card->info[i], |
| 954 | "802.11 SDIO ID: %x", &model) == 1) |
| 955 | break; |
| 956 | if (sscanf(func->card->info[i], |
| 957 | "ID: %x", &model) == 1) |
| 958 | break; |
Bing Zhao | e70a5ac | 2009-05-19 19:48:18 -0700 | [diff] [blame] | 959 | if (!strcmp(func->card->info[i], "IBIS Wireless SDIO Card")) { |
Dan Williams | 3d32a58 | 2010-08-07 21:15:52 -0500 | [diff] [blame] | 960 | model = MODEL_8385; |
Bing Zhao | e70a5ac | 2009-05-19 19:48:18 -0700 | [diff] [blame] | 961 | break; |
| 962 | } |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 963 | } |
| 964 | |
| 965 | if (i == func->card->num_info) { |
Joe Perches | 0e4e06a | 2011-05-02 16:49:14 -0700 | [diff] [blame^] | 966 | pr_err("unable to identify card model\n"); |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 967 | return -ENODEV; |
| 968 | } |
| 969 | |
| 970 | card = kzalloc(sizeof(struct if_sdio_card), GFP_KERNEL); |
| 971 | if (!card) |
| 972 | return -ENOMEM; |
| 973 | |
| 974 | card->func = func; |
| 975 | card->model = model; |
Bing Zhao | 2c7e579 | 2009-05-21 11:32:34 -0700 | [diff] [blame] | 976 | |
| 977 | switch (card->model) { |
Dan Williams | 3d32a58 | 2010-08-07 21:15:52 -0500 | [diff] [blame] | 978 | case MODEL_8385: |
Bing Zhao | 2c7e579 | 2009-05-21 11:32:34 -0700 | [diff] [blame] | 979 | card->scratch_reg = IF_SDIO_SCRATCH_OLD; |
| 980 | break; |
Dan Williams | 3d32a58 | 2010-08-07 21:15:52 -0500 | [diff] [blame] | 981 | case MODEL_8686: |
Bing Zhao | 2c7e579 | 2009-05-21 11:32:34 -0700 | [diff] [blame] | 982 | card->scratch_reg = IF_SDIO_SCRATCH; |
| 983 | break; |
Dan Williams | 3d32a58 | 2010-08-07 21:15:52 -0500 | [diff] [blame] | 984 | case MODEL_8688: |
Bing Zhao | 2c7e579 | 2009-05-21 11:32:34 -0700 | [diff] [blame] | 985 | default: /* for newer chipsets */ |
| 986 | card->scratch_reg = IF_SDIO_FW_STATUS; |
| 987 | break; |
| 988 | } |
| 989 | |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 990 | spin_lock_init(&card->lock); |
Dan Williams | 9b02f41 | 2009-02-20 12:27:38 -0500 | [diff] [blame] | 991 | card->workqueue = create_workqueue("libertas_sdio"); |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 992 | INIT_WORK(&card->packet_worker, if_sdio_host_to_card_worker); |
| 993 | |
Dan Williams | 3d32a58 | 2010-08-07 21:15:52 -0500 | [diff] [blame] | 994 | /* Check if we support this card */ |
| 995 | for (i = 0; i < ARRAY_SIZE(fw_table); i++) { |
| 996 | if (card->model == fw_table[i].model) |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 997 | break; |
| 998 | } |
Dan Williams | 3d32a58 | 2010-08-07 21:15:52 -0500 | [diff] [blame] | 999 | if (i == ARRAY_SIZE(fw_table)) { |
Joe Perches | 0e4e06a | 2011-05-02 16:49:14 -0700 | [diff] [blame^] | 1000 | pr_err("unknown card model 0x%x\n", card->model); |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 1001 | ret = -ENODEV; |
| 1002 | goto free; |
| 1003 | } |
| 1004 | |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 1005 | sdio_claim_host(func); |
| 1006 | |
| 1007 | ret = sdio_enable_func(func); |
| 1008 | if (ret) |
| 1009 | goto release; |
| 1010 | |
| 1011 | ret = sdio_claim_irq(func, if_sdio_interrupt); |
| 1012 | if (ret) |
| 1013 | goto disable; |
| 1014 | |
Daniel Mack | 8a64c0f | 2010-04-06 10:52:44 +0200 | [diff] [blame] | 1015 | /* For 1-bit transfers to the 8686 model, we need to enable the |
| 1016 | * interrupt flag in the CCCR register. Set the MMC_QUIRK_LENIENT_FN0 |
| 1017 | * bit to allow access to non-vendor registers. */ |
Dan Williams | 3d32a58 | 2010-08-07 21:15:52 -0500 | [diff] [blame] | 1018 | if ((card->model == MODEL_8686) && |
Daniel Mack | 8a64c0f | 2010-04-06 10:52:44 +0200 | [diff] [blame] | 1019 | (host->caps & MMC_CAP_SDIO_IRQ) && |
| 1020 | (host->ios.bus_width == MMC_BUS_WIDTH_1)) { |
| 1021 | u8 reg; |
| 1022 | |
| 1023 | func->card->quirks |= MMC_QUIRK_LENIENT_FN0; |
| 1024 | reg = sdio_f0_readb(func, SDIO_CCCR_IF, &ret); |
| 1025 | if (ret) |
| 1026 | goto release_int; |
| 1027 | |
| 1028 | reg |= SDIO_BUS_ECSI; |
| 1029 | sdio_f0_writeb(func, reg, SDIO_CCCR_IF, &ret); |
| 1030 | if (ret) |
| 1031 | goto release_int; |
| 1032 | } |
| 1033 | |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 1034 | card->ioport = sdio_readb(func, IF_SDIO_IOPORT, &ret); |
| 1035 | if (ret) |
| 1036 | goto release_int; |
| 1037 | |
| 1038 | card->ioport |= sdio_readb(func, IF_SDIO_IOPORT + 1, &ret) << 8; |
| 1039 | if (ret) |
| 1040 | goto release_int; |
| 1041 | |
| 1042 | card->ioport |= sdio_readb(func, IF_SDIO_IOPORT + 2, &ret) << 16; |
| 1043 | if (ret) |
| 1044 | goto release_int; |
| 1045 | |
| 1046 | sdio_release_host(func); |
| 1047 | |
| 1048 | sdio_set_drvdata(func, card); |
| 1049 | |
| 1050 | lbs_deb_sdio("class = 0x%X, vendor = 0x%X, " |
| 1051 | "device = 0x%X, model = 0x%X, ioport = 0x%X\n", |
| 1052 | func->class, func->vendor, func->device, |
| 1053 | model, (unsigned)card->ioport); |
| 1054 | |
| 1055 | ret = if_sdio_prog_firmware(card); |
| 1056 | if (ret) |
| 1057 | goto reclaim; |
| 1058 | |
Holger Schurig | 1007832 | 2007-11-15 18:05:47 -0500 | [diff] [blame] | 1059 | priv = lbs_add_card(card, &func->dev); |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 1060 | if (!priv) { |
| 1061 | ret = -ENOMEM; |
| 1062 | goto reclaim; |
| 1063 | } |
| 1064 | |
| 1065 | card->priv = priv; |
| 1066 | |
| 1067 | priv->card = card; |
| 1068 | priv->hw_host_to_card = if_sdio_host_to_card; |
Amitkumar Karwar | 4912545 | 2009-09-30 20:04:38 -0700 | [diff] [blame] | 1069 | priv->enter_deep_sleep = if_sdio_enter_deep_sleep; |
| 1070 | priv->exit_deep_sleep = if_sdio_exit_deep_sleep; |
| 1071 | priv->reset_deep_sleep_wakeup = if_sdio_reset_deep_sleep_wakeup; |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 1072 | |
Bing Zhao | b136a14 | 2009-05-19 19:48:19 -0700 | [diff] [blame] | 1073 | sdio_claim_host(func); |
| 1074 | |
| 1075 | /* |
| 1076 | * Get rx_unit if the chip is SD8688 or newer. |
| 1077 | * SD8385 & SD8686 do not have rx_unit. |
| 1078 | */ |
Dan Williams | 3d32a58 | 2010-08-07 21:15:52 -0500 | [diff] [blame] | 1079 | if ((card->model != MODEL_8385) |
| 1080 | && (card->model != MODEL_8686)) |
Bing Zhao | b136a14 | 2009-05-19 19:48:19 -0700 | [diff] [blame] | 1081 | card->rx_unit = if_sdio_read_rx_unit(card); |
| 1082 | else |
| 1083 | card->rx_unit = 0; |
| 1084 | |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 1085 | /* |
| 1086 | * Enable interrupts now that everything is set up |
| 1087 | */ |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 1088 | sdio_writeb(func, 0x0f, IF_SDIO_H_INT_MASK, &ret); |
| 1089 | sdio_release_host(func); |
| 1090 | if (ret) |
| 1091 | goto reclaim; |
| 1092 | |
Paul Fox | 731b203 | 2010-10-29 14:57:28 +0100 | [diff] [blame] | 1093 | priv->fw_ready = 1; |
| 1094 | |
Bing Zhao | d26285f8 | 2009-05-19 19:48:20 -0700 | [diff] [blame] | 1095 | /* |
| 1096 | * FUNC_INIT is required for SD8688 WLAN/BT multiple functions |
| 1097 | */ |
Dan Williams | 3d32a58 | 2010-08-07 21:15:52 -0500 | [diff] [blame] | 1098 | if (card->model == MODEL_8688) { |
Bing Zhao | 6bc61f4 | 2009-06-01 18:04:36 -0700 | [diff] [blame] | 1099 | struct cmd_header cmd; |
| 1100 | |
| 1101 | memset(&cmd, 0, sizeof(cmd)); |
| 1102 | |
| 1103 | lbs_deb_sdio("send function INIT command\n"); |
| 1104 | if (__lbs_cmd(priv, CMD_FUNC_INIT, &cmd, sizeof(cmd), |
| 1105 | lbs_cmd_copyback, (unsigned long) &cmd)) |
Joe Perches | 0e4e06a | 2011-05-02 16:49:14 -0700 | [diff] [blame^] | 1106 | pr_alert("CMD_FUNC_INIT cmd failed\n"); |
Bing Zhao | 6bc61f4 | 2009-06-01 18:04:36 -0700 | [diff] [blame] | 1107 | } |
Bing Zhao | d26285f8 | 2009-05-19 19:48:20 -0700 | [diff] [blame] | 1108 | |
Holger Schurig | 1007832 | 2007-11-15 18:05:47 -0500 | [diff] [blame] | 1109 | ret = lbs_start_card(priv); |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 1110 | if (ret) |
| 1111 | goto err_activate_card; |
| 1112 | |
| 1113 | out: |
| 1114 | lbs_deb_leave_args(LBS_DEB_SDIO, "ret %d", ret); |
| 1115 | |
| 1116 | return ret; |
| 1117 | |
| 1118 | err_activate_card: |
Dan Williams | 9b02f41 | 2009-02-20 12:27:38 -0500 | [diff] [blame] | 1119 | flush_workqueue(card->workqueue); |
| 1120 | lbs_remove_card(priv); |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 1121 | reclaim: |
| 1122 | sdio_claim_host(func); |
| 1123 | release_int: |
| 1124 | sdio_release_irq(func); |
| 1125 | disable: |
| 1126 | sdio_disable_func(func); |
| 1127 | release: |
| 1128 | sdio_release_host(func); |
| 1129 | free: |
Dan Williams | 9b02f41 | 2009-02-20 12:27:38 -0500 | [diff] [blame] | 1130 | destroy_workqueue(card->workqueue); |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 1131 | while (card->packets) { |
| 1132 | packet = card->packets; |
| 1133 | card->packets = card->packets->next; |
| 1134 | kfree(packet); |
| 1135 | } |
| 1136 | |
Rusty Russell | 886275c | 2010-08-11 23:04:33 -0600 | [diff] [blame] | 1137 | if (card->helper_allocated) |
| 1138 | kfree(card->helper); |
| 1139 | if (card->firmware_allocated) |
| 1140 | kfree(card->firmware); |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 1141 | kfree(card); |
| 1142 | |
| 1143 | goto out; |
| 1144 | } |
| 1145 | |
| 1146 | static void if_sdio_remove(struct sdio_func *func) |
| 1147 | { |
| 1148 | struct if_sdio_card *card; |
| 1149 | struct if_sdio_packet *packet; |
| 1150 | |
| 1151 | lbs_deb_enter(LBS_DEB_SDIO); |
| 1152 | |
| 1153 | card = sdio_get_drvdata(func); |
| 1154 | |
Dan Williams | 3d32a58 | 2010-08-07 21:15:52 -0500 | [diff] [blame] | 1155 | if (user_rmmod && (card->model == MODEL_8688)) { |
Bing Zhao | 6bc61f4 | 2009-06-01 18:04:36 -0700 | [diff] [blame] | 1156 | /* |
| 1157 | * FUNC_SHUTDOWN is required for SD8688 WLAN/BT |
| 1158 | * multiple functions |
| 1159 | */ |
| 1160 | struct cmd_header cmd; |
| 1161 | |
| 1162 | memset(&cmd, 0, sizeof(cmd)); |
| 1163 | |
| 1164 | lbs_deb_sdio("send function SHUTDOWN command\n"); |
| 1165 | if (__lbs_cmd(card->priv, CMD_FUNC_SHUTDOWN, |
| 1166 | &cmd, sizeof(cmd), lbs_cmd_copyback, |
| 1167 | (unsigned long) &cmd)) |
Joe Perches | 0e4e06a | 2011-05-02 16:49:14 -0700 | [diff] [blame^] | 1168 | pr_alert("CMD_FUNC_SHUTDOWN cmd failed\n"); |
Bing Zhao | 6bc61f4 | 2009-06-01 18:04:36 -0700 | [diff] [blame] | 1169 | } |
Bing Zhao | d26285f8 | 2009-05-19 19:48:20 -0700 | [diff] [blame] | 1170 | |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 1171 | |
| 1172 | lbs_deb_sdio("call remove card\n"); |
Bing Zhao | 6bc61f4 | 2009-06-01 18:04:36 -0700 | [diff] [blame] | 1173 | lbs_stop_card(card->priv); |
Holger Schurig | 1007832 | 2007-11-15 18:05:47 -0500 | [diff] [blame] | 1174 | lbs_remove_card(card->priv); |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 1175 | |
Dan Williams | 9b02f41 | 2009-02-20 12:27:38 -0500 | [diff] [blame] | 1176 | flush_workqueue(card->workqueue); |
| 1177 | destroy_workqueue(card->workqueue); |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 1178 | |
| 1179 | sdio_claim_host(func); |
| 1180 | sdio_release_irq(func); |
| 1181 | sdio_disable_func(func); |
| 1182 | sdio_release_host(func); |
| 1183 | |
| 1184 | while (card->packets) { |
| 1185 | packet = card->packets; |
| 1186 | card->packets = card->packets->next; |
| 1187 | kfree(packet); |
| 1188 | } |
| 1189 | |
Rusty Russell | 886275c | 2010-08-11 23:04:33 -0600 | [diff] [blame] | 1190 | if (card->helper_allocated) |
| 1191 | kfree(card->helper); |
| 1192 | if (card->firmware_allocated) |
| 1193 | kfree(card->firmware); |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 1194 | kfree(card); |
| 1195 | |
| 1196 | lbs_deb_leave(LBS_DEB_SDIO); |
| 1197 | } |
| 1198 | |
Amitkumar Karwar | 66fceb6 | 2010-05-19 03:24:38 -0700 | [diff] [blame] | 1199 | static int if_sdio_suspend(struct device *dev) |
| 1200 | { |
| 1201 | struct sdio_func *func = dev_to_sdio_func(dev); |
| 1202 | int ret; |
| 1203 | struct if_sdio_card *card = sdio_get_drvdata(func); |
| 1204 | |
| 1205 | mmc_pm_flag_t flags = sdio_get_host_pm_caps(func); |
| 1206 | |
Joe Perches | 0e4e06a | 2011-05-02 16:49:14 -0700 | [diff] [blame^] | 1207 | pr_info("%s: suspend: PM flags = 0x%x\n", sdio_func_id(func), flags); |
Amitkumar Karwar | 66fceb6 | 2010-05-19 03:24:38 -0700 | [diff] [blame] | 1208 | |
| 1209 | /* If we aren't being asked to wake on anything, we should bail out |
| 1210 | * and let the SD stack power down the card. |
| 1211 | */ |
| 1212 | if (card->priv->wol_criteria == EHS_REMOVE_WAKEUP) { |
Joe Perches | 0e4e06a | 2011-05-02 16:49:14 -0700 | [diff] [blame^] | 1213 | pr_info("Suspend without wake params -- powering down card\n"); |
Amitkumar Karwar | 66fceb6 | 2010-05-19 03:24:38 -0700 | [diff] [blame] | 1214 | return -ENOSYS; |
| 1215 | } |
| 1216 | |
| 1217 | if (!(flags & MMC_PM_KEEP_POWER)) { |
Joe Perches | 0e4e06a | 2011-05-02 16:49:14 -0700 | [diff] [blame^] | 1218 | pr_err("%s: cannot remain alive while host is suspended\n", |
| 1219 | sdio_func_id(func)); |
Amitkumar Karwar | 66fceb6 | 2010-05-19 03:24:38 -0700 | [diff] [blame] | 1220 | return -ENOSYS; |
| 1221 | } |
| 1222 | |
| 1223 | ret = sdio_set_host_pm_flags(func, MMC_PM_KEEP_POWER); |
| 1224 | if (ret) |
| 1225 | return ret; |
| 1226 | |
| 1227 | ret = lbs_suspend(card->priv); |
| 1228 | if (ret) |
| 1229 | return ret; |
| 1230 | |
| 1231 | return sdio_set_host_pm_flags(func, MMC_PM_WAKE_SDIO_IRQ); |
| 1232 | } |
| 1233 | |
| 1234 | static int if_sdio_resume(struct device *dev) |
| 1235 | { |
| 1236 | struct sdio_func *func = dev_to_sdio_func(dev); |
| 1237 | struct if_sdio_card *card = sdio_get_drvdata(func); |
| 1238 | int ret; |
| 1239 | |
Joe Perches | 0e4e06a | 2011-05-02 16:49:14 -0700 | [diff] [blame^] | 1240 | pr_info("%s: resume: we're back\n", sdio_func_id(func)); |
Amitkumar Karwar | 66fceb6 | 2010-05-19 03:24:38 -0700 | [diff] [blame] | 1241 | |
| 1242 | ret = lbs_resume(card->priv); |
| 1243 | |
| 1244 | return ret; |
| 1245 | } |
| 1246 | |
| 1247 | static const struct dev_pm_ops if_sdio_pm_ops = { |
| 1248 | .suspend = if_sdio_suspend, |
| 1249 | .resume = if_sdio_resume, |
| 1250 | }; |
| 1251 | |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 1252 | static struct sdio_driver if_sdio_driver = { |
| 1253 | .name = "libertas_sdio", |
| 1254 | .id_table = if_sdio_ids, |
| 1255 | .probe = if_sdio_probe, |
| 1256 | .remove = if_sdio_remove, |
Amitkumar Karwar | 66fceb6 | 2010-05-19 03:24:38 -0700 | [diff] [blame] | 1257 | .drv = { |
| 1258 | .pm = &if_sdio_pm_ops, |
| 1259 | }, |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 1260 | }; |
| 1261 | |
| 1262 | /*******************************************************************/ |
| 1263 | /* Module functions */ |
| 1264 | /*******************************************************************/ |
| 1265 | |
Andres Salomon | 4fb910f | 2007-11-20 17:43:45 -0500 | [diff] [blame] | 1266 | static int __init if_sdio_init_module(void) |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 1267 | { |
| 1268 | int ret = 0; |
| 1269 | |
| 1270 | lbs_deb_enter(LBS_DEB_SDIO); |
| 1271 | |
| 1272 | printk(KERN_INFO "libertas_sdio: Libertas SDIO driver\n"); |
| 1273 | printk(KERN_INFO "libertas_sdio: Copyright Pierre Ossman\n"); |
| 1274 | |
| 1275 | ret = sdio_register_driver(&if_sdio_driver); |
| 1276 | |
Bing Zhao | 6bc61f4 | 2009-06-01 18:04:36 -0700 | [diff] [blame] | 1277 | /* Clear the flag in case user removes the card. */ |
| 1278 | user_rmmod = 0; |
| 1279 | |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 1280 | lbs_deb_leave_args(LBS_DEB_SDIO, "ret %d", ret); |
| 1281 | |
| 1282 | return ret; |
| 1283 | } |
| 1284 | |
Andres Salomon | 4fb910f | 2007-11-20 17:43:45 -0500 | [diff] [blame] | 1285 | static void __exit if_sdio_exit_module(void) |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 1286 | { |
| 1287 | lbs_deb_enter(LBS_DEB_SDIO); |
| 1288 | |
Bing Zhao | 6bc61f4 | 2009-06-01 18:04:36 -0700 | [diff] [blame] | 1289 | /* Set the flag as user is removing this module. */ |
| 1290 | user_rmmod = 1; |
Bing Zhao | d26285f8 | 2009-05-19 19:48:20 -0700 | [diff] [blame] | 1291 | |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 1292 | sdio_unregister_driver(&if_sdio_driver); |
| 1293 | |
| 1294 | lbs_deb_leave(LBS_DEB_SDIO); |
| 1295 | } |
| 1296 | |
| 1297 | module_init(if_sdio_init_module); |
| 1298 | module_exit(if_sdio_exit_module); |
| 1299 | |
| 1300 | MODULE_DESCRIPTION("Libertas SDIO WLAN Driver"); |
| 1301 | MODULE_AUTHOR("Pierre Ossman"); |
| 1302 | MODULE_LICENSE("GPL"); |