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 | |
Ilpo Järvinen | 2382792 | 2008-12-13 21:33:13 +0200 | [diff] [blame] | 29 | #include <linux/kernel.h> |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 30 | #include <linux/moduleparam.h> |
| 31 | #include <linux/firmware.h> |
| 32 | #include <linux/netdevice.h> |
| 33 | #include <linux/delay.h> |
| 34 | #include <linux/mmc/card.h> |
| 35 | #include <linux/mmc/sdio_func.h> |
| 36 | #include <linux/mmc/sdio_ids.h> |
| 37 | |
| 38 | #include "host.h" |
| 39 | #include "decl.h" |
| 40 | #include "defs.h" |
| 41 | #include "dev.h" |
| 42 | #include "if_sdio.h" |
| 43 | |
Holger Schurig | 1007832 | 2007-11-15 18:05:47 -0500 | [diff] [blame] | 44 | static char *lbs_helper_name = NULL; |
| 45 | module_param_named(helper_name, lbs_helper_name, charp, 0644); |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 46 | |
Holger Schurig | 1007832 | 2007-11-15 18:05:47 -0500 | [diff] [blame] | 47 | static char *lbs_fw_name = NULL; |
| 48 | module_param_named(fw_name, lbs_fw_name, charp, 0644); |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 49 | |
| 50 | static const struct sdio_device_id if_sdio_ids[] = { |
Bing Zhao | e45d8e5 | 2009-04-06 15:50:56 -0700 | [diff] [blame] | 51 | { SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, |
| 52 | SDIO_DEVICE_ID_MARVELL_LIBERTAS) }, |
| 53 | { SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, |
| 54 | SDIO_DEVICE_ID_MARVELL_8688WLAN) }, |
| 55 | { /* end: all zeroes */ }, |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 56 | }; |
| 57 | |
| 58 | MODULE_DEVICE_TABLE(sdio, if_sdio_ids); |
| 59 | |
| 60 | struct if_sdio_model { |
| 61 | int model; |
| 62 | const char *helper; |
| 63 | const char *firmware; |
Bing Zhao | d26285f8 | 2009-05-19 19:48:20 -0700 | [diff] [blame] | 64 | struct if_sdio_card *card; |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 65 | }; |
| 66 | |
| 67 | static struct if_sdio_model if_sdio_models[] = { |
| 68 | { |
| 69 | /* 8385 */ |
Bing Zhao | e70a5ac | 2009-05-19 19:48:18 -0700 | [diff] [blame] | 70 | .model = IF_SDIO_MODEL_8385, |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 71 | .helper = "sd8385_helper.bin", |
| 72 | .firmware = "sd8385.bin", |
Bing Zhao | d26285f8 | 2009-05-19 19:48:20 -0700 | [diff] [blame] | 73 | .card = NULL, |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 74 | }, |
| 75 | { |
| 76 | /* 8686 */ |
Bing Zhao | e70a5ac | 2009-05-19 19:48:18 -0700 | [diff] [blame] | 77 | .model = IF_SDIO_MODEL_8686, |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 78 | .helper = "sd8686_helper.bin", |
| 79 | .firmware = "sd8686.bin", |
Bing Zhao | d26285f8 | 2009-05-19 19:48:20 -0700 | [diff] [blame] | 80 | .card = NULL, |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 81 | }, |
Bing Zhao | e45d8e5 | 2009-04-06 15:50:56 -0700 | [diff] [blame] | 82 | { |
| 83 | /* 8688 */ |
Bing Zhao | e70a5ac | 2009-05-19 19:48:18 -0700 | [diff] [blame] | 84 | .model = IF_SDIO_MODEL_8688, |
Bing Zhao | e45d8e5 | 2009-04-06 15:50:56 -0700 | [diff] [blame] | 85 | .helper = "sd8688_helper.bin", |
| 86 | .firmware = "sd8688.bin", |
Bing Zhao | d26285f8 | 2009-05-19 19:48:20 -0700 | [diff] [blame] | 87 | .card = NULL, |
Bing Zhao | e45d8e5 | 2009-04-06 15:50:56 -0700 | [diff] [blame] | 88 | }, |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 89 | }; |
| 90 | |
| 91 | struct if_sdio_packet { |
| 92 | struct if_sdio_packet *next; |
| 93 | u16 nb; |
| 94 | u8 buffer[0] __attribute__((aligned(4))); |
| 95 | }; |
| 96 | |
| 97 | struct if_sdio_card { |
| 98 | struct sdio_func *func; |
Holger Schurig | 69f9032 | 2007-11-23 15:43:44 +0100 | [diff] [blame] | 99 | struct lbs_private *priv; |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 100 | |
| 101 | int model; |
| 102 | unsigned long ioport; |
Bing Zhao | 2c7e579 | 2009-05-21 11:32:34 -0700 | [diff] [blame^] | 103 | unsigned int scratch_reg; |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 104 | |
| 105 | const char *helper; |
| 106 | const char *firmware; |
| 107 | |
| 108 | u8 buffer[65536]; |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 109 | |
| 110 | spinlock_t lock; |
| 111 | struct if_sdio_packet *packets; |
Dan Williams | 9b02f41 | 2009-02-20 12:27:38 -0500 | [diff] [blame] | 112 | |
| 113 | struct workqueue_struct *workqueue; |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 114 | struct work_struct packet_worker; |
Bing Zhao | b136a14 | 2009-05-19 19:48:19 -0700 | [diff] [blame] | 115 | |
| 116 | u8 rx_unit; |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 117 | }; |
| 118 | |
| 119 | /********************************************************************/ |
| 120 | /* I/O */ |
| 121 | /********************************************************************/ |
| 122 | |
Bing Zhao | 2c7e579 | 2009-05-21 11:32:34 -0700 | [diff] [blame^] | 123 | /* |
| 124 | * For SD8385/SD8686, this function reads firmware status after |
| 125 | * the image is downloaded, or reads RX packet length when |
| 126 | * interrupt (with IF_SDIO_H_INT_UPLD bit set) is received. |
| 127 | * For SD8688, this function reads firmware status only. |
| 128 | */ |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 129 | static u16 if_sdio_read_scratch(struct if_sdio_card *card, int *err) |
| 130 | { |
Bing Zhao | 2c7e579 | 2009-05-21 11:32:34 -0700 | [diff] [blame^] | 131 | int ret; |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 132 | u16 scratch; |
| 133 | |
Bing Zhao | 2c7e579 | 2009-05-21 11:32:34 -0700 | [diff] [blame^] | 134 | scratch = sdio_readb(card->func, card->scratch_reg, &ret); |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 135 | if (!ret) |
Bing Zhao | 2c7e579 | 2009-05-21 11:32:34 -0700 | [diff] [blame^] | 136 | scratch |= sdio_readb(card->func, card->scratch_reg + 1, |
| 137 | &ret) << 8; |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 138 | |
| 139 | if (err) |
| 140 | *err = ret; |
| 141 | |
| 142 | if (ret) |
| 143 | return 0xffff; |
| 144 | |
| 145 | return scratch; |
| 146 | } |
| 147 | |
Bing Zhao | b136a14 | 2009-05-19 19:48:19 -0700 | [diff] [blame] | 148 | static u8 if_sdio_read_rx_unit(struct if_sdio_card *card) |
| 149 | { |
| 150 | int ret; |
| 151 | u8 rx_unit; |
| 152 | |
| 153 | rx_unit = sdio_readb(card->func, IF_SDIO_RX_UNIT, &ret); |
| 154 | |
| 155 | if (ret) |
| 156 | rx_unit = 0; |
| 157 | |
| 158 | return rx_unit; |
| 159 | } |
| 160 | |
| 161 | static u16 if_sdio_read_rx_len(struct if_sdio_card *card, int *err) |
| 162 | { |
| 163 | int ret; |
| 164 | u16 rx_len; |
| 165 | |
| 166 | switch (card->model) { |
| 167 | case IF_SDIO_MODEL_8385: |
| 168 | case IF_SDIO_MODEL_8686: |
| 169 | rx_len = if_sdio_read_scratch(card, &ret); |
| 170 | break; |
| 171 | case IF_SDIO_MODEL_8688: |
| 172 | default: /* for newer chipsets */ |
| 173 | rx_len = sdio_readb(card->func, IF_SDIO_RX_LEN, &ret); |
| 174 | if (!ret) |
| 175 | rx_len <<= card->rx_unit; |
| 176 | else |
| 177 | rx_len = 0xffff; /* invalid length */ |
| 178 | |
| 179 | break; |
| 180 | } |
| 181 | |
| 182 | if (err) |
| 183 | *err = ret; |
| 184 | |
| 185 | return rx_len; |
| 186 | } |
| 187 | |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 188 | static int if_sdio_handle_cmd(struct if_sdio_card *card, |
| 189 | u8 *buffer, unsigned size) |
| 190 | { |
Holger Schurig | 7919b89 | 2008-04-01 14:50:43 +0200 | [diff] [blame] | 191 | struct lbs_private *priv = card->priv; |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 192 | int ret; |
| 193 | unsigned long flags; |
Holger Schurig | 7919b89 | 2008-04-01 14:50:43 +0200 | [diff] [blame] | 194 | u8 i; |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 195 | |
| 196 | lbs_deb_enter(LBS_DEB_SDIO); |
| 197 | |
Dan Williams | ddac452 | 2007-12-11 13:49:39 -0500 | [diff] [blame] | 198 | if (size > LBS_CMD_BUFFER_SIZE) { |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 199 | lbs_deb_sdio("response packet too large (%d bytes)\n", |
| 200 | (int)size); |
| 201 | ret = -E2BIG; |
| 202 | goto out; |
| 203 | } |
| 204 | |
Holger Schurig | 7919b89 | 2008-04-01 14:50:43 +0200 | [diff] [blame] | 205 | spin_lock_irqsave(&priv->driver_lock, flags); |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 206 | |
Holger Schurig | 7919b89 | 2008-04-01 14:50:43 +0200 | [diff] [blame] | 207 | i = (priv->resp_idx == 0) ? 1 : 0; |
| 208 | BUG_ON(priv->resp_len[i]); |
| 209 | priv->resp_len[i] = size; |
| 210 | memcpy(priv->resp_buf[i], buffer, size); |
| 211 | lbs_notify_command_response(priv, i); |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 212 | |
Holger Schurig | 7919b89 | 2008-04-01 14:50:43 +0200 | [diff] [blame] | 213 | spin_unlock_irqrestore(&card->priv->driver_lock, flags); |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 214 | |
| 215 | ret = 0; |
| 216 | |
| 217 | out: |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 218 | lbs_deb_leave_args(LBS_DEB_SDIO, "ret %d", ret); |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 219 | return ret; |
| 220 | } |
| 221 | |
| 222 | static int if_sdio_handle_data(struct if_sdio_card *card, |
| 223 | u8 *buffer, unsigned size) |
| 224 | { |
| 225 | int ret; |
| 226 | struct sk_buff *skb; |
| 227 | char *data; |
| 228 | |
| 229 | lbs_deb_enter(LBS_DEB_SDIO); |
| 230 | |
| 231 | if (size > MRVDRV_ETH_RX_PACKET_BUFFER_SIZE) { |
| 232 | lbs_deb_sdio("response packet too large (%d bytes)\n", |
| 233 | (int)size); |
| 234 | ret = -E2BIG; |
| 235 | goto out; |
| 236 | } |
| 237 | |
Pierre Ossman | 51e6b71 | 2007-10-22 19:05:32 +0200 | [diff] [blame] | 238 | 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] | 239 | if (!skb) { |
| 240 | ret = -ENOMEM; |
| 241 | goto out; |
| 242 | } |
| 243 | |
Pierre Ossman | 51e6b71 | 2007-10-22 19:05:32 +0200 | [diff] [blame] | 244 | skb_reserve(skb, NET_IP_ALIGN); |
| 245 | |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 246 | data = skb_put(skb, size); |
| 247 | |
| 248 | memcpy(data, buffer, size); |
| 249 | |
Holger Schurig | 1007832 | 2007-11-15 18:05:47 -0500 | [diff] [blame] | 250 | lbs_process_rxed_packet(card->priv, skb); |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 251 | |
| 252 | ret = 0; |
| 253 | |
| 254 | out: |
| 255 | lbs_deb_leave_args(LBS_DEB_SDIO, "ret %d", ret); |
| 256 | |
| 257 | return ret; |
| 258 | } |
| 259 | |
| 260 | static int if_sdio_handle_event(struct if_sdio_card *card, |
| 261 | u8 *buffer, unsigned size) |
| 262 | { |
| 263 | int ret; |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 264 | u32 event; |
| 265 | |
| 266 | lbs_deb_enter(LBS_DEB_SDIO); |
| 267 | |
Bing Zhao | e70a5ac | 2009-05-19 19:48:18 -0700 | [diff] [blame] | 268 | if (card->model == IF_SDIO_MODEL_8385) { |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 269 | event = sdio_readb(card->func, IF_SDIO_EVENT, &ret); |
| 270 | if (ret) |
| 271 | goto out; |
Bing Zhao | f5ac2b9 | 2009-02-04 22:22:39 -0800 | [diff] [blame] | 272 | |
| 273 | /* right shift 3 bits to get the event id */ |
| 274 | event >>= 3; |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 275 | } else { |
| 276 | if (size < 4) { |
| 277 | lbs_deb_sdio("event packet too small (%d bytes)\n", |
| 278 | (int)size); |
| 279 | ret = -EINVAL; |
| 280 | goto out; |
| 281 | } |
| 282 | event = buffer[3] << 24; |
| 283 | event |= buffer[2] << 16; |
| 284 | event |= buffer[1] << 8; |
| 285 | event |= buffer[0] << 0; |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 286 | } |
| 287 | |
Holger Schurig | 7919b89 | 2008-04-01 14:50:43 +0200 | [diff] [blame] | 288 | lbs_queue_event(card->priv, event & 0xFF); |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 289 | ret = 0; |
| 290 | |
| 291 | out: |
| 292 | lbs_deb_leave_args(LBS_DEB_SDIO, "ret %d", ret); |
| 293 | |
| 294 | return ret; |
| 295 | } |
| 296 | |
| 297 | static int if_sdio_card_to_host(struct if_sdio_card *card) |
| 298 | { |
| 299 | int ret; |
| 300 | u8 status; |
| 301 | u16 size, type, chunk; |
| 302 | unsigned long timeout; |
| 303 | |
| 304 | lbs_deb_enter(LBS_DEB_SDIO); |
| 305 | |
Bing Zhao | b136a14 | 2009-05-19 19:48:19 -0700 | [diff] [blame] | 306 | size = if_sdio_read_rx_len(card, &ret); |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 307 | if (ret) |
| 308 | goto out; |
| 309 | |
| 310 | if (size < 4) { |
| 311 | lbs_deb_sdio("invalid packet size (%d bytes) from firmware\n", |
| 312 | (int)size); |
| 313 | ret = -EINVAL; |
| 314 | goto out; |
| 315 | } |
| 316 | |
| 317 | timeout = jiffies + HZ; |
| 318 | while (1) { |
| 319 | status = sdio_readb(card->func, IF_SDIO_STATUS, &ret); |
| 320 | if (ret) |
| 321 | goto out; |
| 322 | if (status & IF_SDIO_IO_RDY) |
| 323 | break; |
| 324 | if (time_after(jiffies, timeout)) { |
| 325 | ret = -ETIMEDOUT; |
| 326 | goto out; |
| 327 | } |
| 328 | mdelay(1); |
| 329 | } |
| 330 | |
| 331 | /* |
| 332 | * The transfer must be in one transaction or the firmware |
Pierre Ossman | ad3868b | 2008-06-28 12:52:45 +0200 | [diff] [blame] | 333 | * goes suicidal. There's no way to guarantee that for all |
| 334 | * controllers, but we can at least try. |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 335 | */ |
Pierre Ossman | ad3868b | 2008-06-28 12:52:45 +0200 | [diff] [blame] | 336 | chunk = sdio_align_size(card->func, size); |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 337 | |
| 338 | ret = sdio_readsb(card->func, card->buffer, card->ioport, chunk); |
| 339 | if (ret) |
| 340 | goto out; |
| 341 | |
| 342 | chunk = card->buffer[0] | (card->buffer[1] << 8); |
| 343 | type = card->buffer[2] | (card->buffer[3] << 8); |
| 344 | |
| 345 | lbs_deb_sdio("packet of type %d and size %d bytes\n", |
| 346 | (int)type, (int)chunk); |
| 347 | |
| 348 | if (chunk > size) { |
| 349 | lbs_deb_sdio("packet fragment (%d > %d)\n", |
| 350 | (int)chunk, (int)size); |
| 351 | ret = -EINVAL; |
| 352 | goto out; |
| 353 | } |
| 354 | |
| 355 | if (chunk < size) { |
| 356 | lbs_deb_sdio("packet fragment (%d < %d)\n", |
| 357 | (int)chunk, (int)size); |
| 358 | } |
| 359 | |
| 360 | switch (type) { |
| 361 | case MVMS_CMD: |
| 362 | ret = if_sdio_handle_cmd(card, card->buffer + 4, chunk - 4); |
| 363 | if (ret) |
| 364 | goto out; |
| 365 | break; |
| 366 | case MVMS_DAT: |
| 367 | ret = if_sdio_handle_data(card, card->buffer + 4, chunk - 4); |
| 368 | if (ret) |
| 369 | goto out; |
| 370 | break; |
| 371 | case MVMS_EVENT: |
| 372 | ret = if_sdio_handle_event(card, card->buffer + 4, chunk - 4); |
| 373 | if (ret) |
| 374 | goto out; |
| 375 | break; |
| 376 | default: |
| 377 | lbs_deb_sdio("invalid type (%d) from firmware\n", |
| 378 | (int)type); |
| 379 | ret = -EINVAL; |
| 380 | goto out; |
| 381 | } |
| 382 | |
| 383 | out: |
| 384 | if (ret) |
| 385 | lbs_pr_err("problem fetching packet from firmware\n"); |
| 386 | |
| 387 | lbs_deb_leave_args(LBS_DEB_SDIO, "ret %d", ret); |
| 388 | |
| 389 | return ret; |
| 390 | } |
| 391 | |
| 392 | static void if_sdio_host_to_card_worker(struct work_struct *work) |
| 393 | { |
| 394 | struct if_sdio_card *card; |
| 395 | struct if_sdio_packet *packet; |
| 396 | unsigned long timeout; |
| 397 | u8 status; |
| 398 | int ret; |
| 399 | unsigned long flags; |
| 400 | |
| 401 | lbs_deb_enter(LBS_DEB_SDIO); |
| 402 | |
| 403 | card = container_of(work, struct if_sdio_card, packet_worker); |
| 404 | |
| 405 | while (1) { |
| 406 | spin_lock_irqsave(&card->lock, flags); |
| 407 | packet = card->packets; |
| 408 | if (packet) |
| 409 | card->packets = packet->next; |
| 410 | spin_unlock_irqrestore(&card->lock, flags); |
| 411 | |
| 412 | if (!packet) |
| 413 | break; |
| 414 | |
| 415 | sdio_claim_host(card->func); |
| 416 | |
| 417 | timeout = jiffies + HZ; |
| 418 | while (1) { |
| 419 | status = sdio_readb(card->func, IF_SDIO_STATUS, &ret); |
| 420 | if (ret) |
| 421 | goto release; |
| 422 | if (status & IF_SDIO_IO_RDY) |
| 423 | break; |
| 424 | if (time_after(jiffies, timeout)) { |
| 425 | ret = -ETIMEDOUT; |
| 426 | goto release; |
| 427 | } |
| 428 | mdelay(1); |
| 429 | } |
| 430 | |
| 431 | ret = sdio_writesb(card->func, card->ioport, |
| 432 | packet->buffer, packet->nb); |
| 433 | if (ret) |
| 434 | goto release; |
| 435 | release: |
| 436 | sdio_release_host(card->func); |
| 437 | |
| 438 | kfree(packet); |
| 439 | } |
| 440 | |
| 441 | lbs_deb_leave(LBS_DEB_SDIO); |
| 442 | } |
| 443 | |
| 444 | /********************************************************************/ |
| 445 | /* Firmware */ |
| 446 | /********************************************************************/ |
| 447 | |
| 448 | static int if_sdio_prog_helper(struct if_sdio_card *card) |
| 449 | { |
| 450 | int ret; |
| 451 | u8 status; |
| 452 | const struct firmware *fw; |
| 453 | unsigned long timeout; |
| 454 | u8 *chunk_buffer; |
| 455 | u32 chunk_size; |
David Woodhouse | 6dfff89 | 2008-05-23 18:37:51 +0100 | [diff] [blame] | 456 | const u8 *firmware; |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 457 | size_t size; |
| 458 | |
| 459 | lbs_deb_enter(LBS_DEB_SDIO); |
| 460 | |
| 461 | ret = request_firmware(&fw, card->helper, &card->func->dev); |
| 462 | if (ret) { |
| 463 | lbs_pr_err("can't load helper firmware\n"); |
| 464 | goto out; |
| 465 | } |
| 466 | |
| 467 | chunk_buffer = kzalloc(64, GFP_KERNEL); |
| 468 | if (!chunk_buffer) { |
| 469 | ret = -ENOMEM; |
| 470 | goto release_fw; |
| 471 | } |
| 472 | |
| 473 | sdio_claim_host(card->func); |
| 474 | |
| 475 | ret = sdio_set_block_size(card->func, 32); |
| 476 | if (ret) |
| 477 | goto release; |
| 478 | |
| 479 | firmware = fw->data; |
| 480 | size = fw->size; |
| 481 | |
| 482 | while (size) { |
| 483 | timeout = jiffies + HZ; |
| 484 | while (1) { |
| 485 | status = sdio_readb(card->func, IF_SDIO_STATUS, &ret); |
| 486 | if (ret) |
| 487 | goto release; |
| 488 | if ((status & IF_SDIO_IO_RDY) && |
| 489 | (status & IF_SDIO_DL_RDY)) |
| 490 | break; |
| 491 | if (time_after(jiffies, timeout)) { |
| 492 | ret = -ETIMEDOUT; |
| 493 | goto release; |
| 494 | } |
| 495 | mdelay(1); |
| 496 | } |
| 497 | |
| 498 | chunk_size = min(size, (size_t)60); |
| 499 | |
Holger Schurig | c2df2ef | 2007-12-07 15:30:44 +0000 | [diff] [blame] | 500 | *((__le32*)chunk_buffer) = cpu_to_le32(chunk_size); |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 501 | memcpy(chunk_buffer + 4, firmware, chunk_size); |
| 502 | /* |
| 503 | lbs_deb_sdio("sending %d bytes chunk\n", chunk_size); |
| 504 | */ |
| 505 | ret = sdio_writesb(card->func, card->ioport, |
| 506 | chunk_buffer, 64); |
| 507 | if (ret) |
| 508 | goto release; |
| 509 | |
| 510 | firmware += chunk_size; |
| 511 | size -= chunk_size; |
| 512 | } |
| 513 | |
| 514 | /* an empty block marks the end of the transfer */ |
| 515 | memset(chunk_buffer, 0, 4); |
| 516 | ret = sdio_writesb(card->func, card->ioport, chunk_buffer, 64); |
| 517 | if (ret) |
| 518 | goto release; |
| 519 | |
| 520 | lbs_deb_sdio("waiting for helper to boot...\n"); |
| 521 | |
| 522 | /* wait for the helper to boot by looking at the size register */ |
| 523 | timeout = jiffies + HZ; |
| 524 | while (1) { |
| 525 | u16 req_size; |
| 526 | |
| 527 | req_size = sdio_readb(card->func, IF_SDIO_RD_BASE, &ret); |
| 528 | if (ret) |
| 529 | goto release; |
| 530 | |
| 531 | req_size |= sdio_readb(card->func, IF_SDIO_RD_BASE + 1, &ret) << 8; |
| 532 | if (ret) |
| 533 | goto release; |
| 534 | |
| 535 | if (req_size != 0) |
| 536 | break; |
| 537 | |
| 538 | if (time_after(jiffies, timeout)) { |
| 539 | ret = -ETIMEDOUT; |
| 540 | goto release; |
| 541 | } |
| 542 | |
| 543 | msleep(10); |
| 544 | } |
| 545 | |
| 546 | ret = 0; |
| 547 | |
| 548 | release: |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 549 | sdio_release_host(card->func); |
| 550 | kfree(chunk_buffer); |
| 551 | release_fw: |
| 552 | release_firmware(fw); |
| 553 | |
| 554 | out: |
| 555 | if (ret) |
| 556 | lbs_pr_err("failed to load helper firmware\n"); |
| 557 | |
| 558 | lbs_deb_leave_args(LBS_DEB_SDIO, "ret %d", ret); |
| 559 | |
| 560 | return ret; |
| 561 | } |
| 562 | |
| 563 | static int if_sdio_prog_real(struct if_sdio_card *card) |
| 564 | { |
| 565 | int ret; |
| 566 | u8 status; |
| 567 | const struct firmware *fw; |
| 568 | unsigned long timeout; |
| 569 | u8 *chunk_buffer; |
| 570 | u32 chunk_size; |
David Woodhouse | 6dfff89 | 2008-05-23 18:37:51 +0100 | [diff] [blame] | 571 | const u8 *firmware; |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 572 | size_t size, req_size; |
| 573 | |
| 574 | lbs_deb_enter(LBS_DEB_SDIO); |
| 575 | |
| 576 | ret = request_firmware(&fw, card->firmware, &card->func->dev); |
| 577 | if (ret) { |
| 578 | lbs_pr_err("can't load firmware\n"); |
| 579 | goto out; |
| 580 | } |
| 581 | |
| 582 | chunk_buffer = kzalloc(512, GFP_KERNEL); |
| 583 | if (!chunk_buffer) { |
| 584 | ret = -ENOMEM; |
| 585 | goto release_fw; |
| 586 | } |
| 587 | |
| 588 | sdio_claim_host(card->func); |
| 589 | |
| 590 | ret = sdio_set_block_size(card->func, 32); |
| 591 | if (ret) |
| 592 | goto release; |
| 593 | |
| 594 | firmware = fw->data; |
| 595 | size = fw->size; |
| 596 | |
| 597 | while (size) { |
| 598 | timeout = jiffies + HZ; |
| 599 | while (1) { |
| 600 | status = sdio_readb(card->func, IF_SDIO_STATUS, &ret); |
| 601 | if (ret) |
| 602 | goto release; |
| 603 | if ((status & IF_SDIO_IO_RDY) && |
| 604 | (status & IF_SDIO_DL_RDY)) |
| 605 | break; |
| 606 | if (time_after(jiffies, timeout)) { |
| 607 | ret = -ETIMEDOUT; |
| 608 | goto release; |
| 609 | } |
| 610 | mdelay(1); |
| 611 | } |
| 612 | |
| 613 | req_size = sdio_readb(card->func, IF_SDIO_RD_BASE, &ret); |
| 614 | if (ret) |
| 615 | goto release; |
| 616 | |
| 617 | req_size |= sdio_readb(card->func, IF_SDIO_RD_BASE + 1, &ret) << 8; |
| 618 | if (ret) |
| 619 | goto release; |
| 620 | /* |
| 621 | lbs_deb_sdio("firmware wants %d bytes\n", (int)req_size); |
| 622 | */ |
| 623 | if (req_size == 0) { |
| 624 | lbs_deb_sdio("firmware helper gave up early\n"); |
| 625 | ret = -EIO; |
| 626 | goto release; |
| 627 | } |
| 628 | |
| 629 | if (req_size & 0x01) { |
| 630 | lbs_deb_sdio("firmware helper signalled error\n"); |
| 631 | ret = -EIO; |
| 632 | goto release; |
| 633 | } |
| 634 | |
| 635 | if (req_size > size) |
| 636 | req_size = size; |
| 637 | |
| 638 | while (req_size) { |
| 639 | chunk_size = min(req_size, (size_t)512); |
| 640 | |
| 641 | memcpy(chunk_buffer, firmware, chunk_size); |
| 642 | /* |
| 643 | lbs_deb_sdio("sending %d bytes (%d bytes) chunk\n", |
| 644 | chunk_size, (chunk_size + 31) / 32 * 32); |
| 645 | */ |
| 646 | ret = sdio_writesb(card->func, card->ioport, |
Ilpo Järvinen | 2382792 | 2008-12-13 21:33:13 +0200 | [diff] [blame] | 647 | chunk_buffer, roundup(chunk_size, 32)); |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 648 | if (ret) |
| 649 | goto release; |
| 650 | |
| 651 | firmware += chunk_size; |
| 652 | size -= chunk_size; |
| 653 | req_size -= chunk_size; |
| 654 | } |
| 655 | } |
| 656 | |
| 657 | ret = 0; |
| 658 | |
| 659 | lbs_deb_sdio("waiting for firmware to boot...\n"); |
| 660 | |
| 661 | /* wait for the firmware to boot */ |
| 662 | timeout = jiffies + HZ; |
| 663 | while (1) { |
| 664 | u16 scratch; |
| 665 | |
| 666 | scratch = if_sdio_read_scratch(card, &ret); |
| 667 | if (ret) |
| 668 | goto release; |
| 669 | |
| 670 | if (scratch == IF_SDIO_FIRMWARE_OK) |
| 671 | break; |
| 672 | |
| 673 | if (time_after(jiffies, timeout)) { |
| 674 | ret = -ETIMEDOUT; |
| 675 | goto release; |
| 676 | } |
| 677 | |
| 678 | msleep(10); |
| 679 | } |
| 680 | |
| 681 | ret = 0; |
| 682 | |
| 683 | release: |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 684 | sdio_release_host(card->func); |
| 685 | kfree(chunk_buffer); |
| 686 | release_fw: |
| 687 | release_firmware(fw); |
| 688 | |
| 689 | out: |
| 690 | if (ret) |
| 691 | lbs_pr_err("failed to load firmware\n"); |
| 692 | |
| 693 | lbs_deb_leave_args(LBS_DEB_SDIO, "ret %d", ret); |
| 694 | |
| 695 | return ret; |
| 696 | } |
| 697 | |
| 698 | static int if_sdio_prog_firmware(struct if_sdio_card *card) |
| 699 | { |
| 700 | int ret; |
| 701 | u16 scratch; |
| 702 | |
| 703 | lbs_deb_enter(LBS_DEB_SDIO); |
| 704 | |
| 705 | sdio_claim_host(card->func); |
| 706 | scratch = if_sdio_read_scratch(card, &ret); |
| 707 | sdio_release_host(card->func); |
| 708 | |
| 709 | if (ret) |
| 710 | goto out; |
| 711 | |
Bing Zhao | 2c7e579 | 2009-05-21 11:32:34 -0700 | [diff] [blame^] | 712 | lbs_deb_sdio("firmware status = %#x\n", scratch); |
| 713 | |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 714 | if (scratch == IF_SDIO_FIRMWARE_OK) { |
| 715 | lbs_deb_sdio("firmware already loaded\n"); |
| 716 | goto success; |
| 717 | } |
| 718 | |
| 719 | ret = if_sdio_prog_helper(card); |
| 720 | if (ret) |
| 721 | goto out; |
| 722 | |
| 723 | ret = if_sdio_prog_real(card); |
| 724 | if (ret) |
| 725 | goto out; |
| 726 | |
| 727 | success: |
Bing Zhao | d26285f8 | 2009-05-19 19:48:20 -0700 | [diff] [blame] | 728 | sdio_claim_host(card->func); |
| 729 | sdio_set_block_size(card->func, IF_SDIO_BLOCK_SIZE); |
| 730 | sdio_release_host(card->func); |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 731 | ret = 0; |
| 732 | |
| 733 | out: |
| 734 | lbs_deb_leave_args(LBS_DEB_SDIO, "ret %d", ret); |
| 735 | |
| 736 | return ret; |
| 737 | } |
| 738 | |
| 739 | /*******************************************************************/ |
| 740 | /* Libertas callbacks */ |
| 741 | /*******************************************************************/ |
| 742 | |
Holger Schurig | 69f9032 | 2007-11-23 15:43:44 +0100 | [diff] [blame] | 743 | static int if_sdio_host_to_card(struct lbs_private *priv, |
| 744 | u8 type, u8 *buf, u16 nb) |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 745 | { |
| 746 | int ret; |
| 747 | struct if_sdio_card *card; |
| 748 | struct if_sdio_packet *packet, *cur; |
| 749 | u16 size; |
| 750 | unsigned long flags; |
| 751 | |
| 752 | lbs_deb_enter_args(LBS_DEB_SDIO, "type %d, bytes %d", type, nb); |
| 753 | |
| 754 | card = priv->card; |
| 755 | |
| 756 | if (nb > (65536 - sizeof(struct if_sdio_packet) - 4)) { |
| 757 | ret = -EINVAL; |
| 758 | goto out; |
| 759 | } |
| 760 | |
| 761 | /* |
| 762 | * The transfer must be in one transaction or the firmware |
Pierre Ossman | ad3868b | 2008-06-28 12:52:45 +0200 | [diff] [blame] | 763 | * goes suicidal. There's no way to guarantee that for all |
| 764 | * controllers, but we can at least try. |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 765 | */ |
Pierre Ossman | ad3868b | 2008-06-28 12:52:45 +0200 | [diff] [blame] | 766 | size = sdio_align_size(card->func, nb + 4); |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 767 | |
| 768 | packet = kzalloc(sizeof(struct if_sdio_packet) + size, |
| 769 | GFP_ATOMIC); |
| 770 | if (!packet) { |
| 771 | ret = -ENOMEM; |
| 772 | goto out; |
| 773 | } |
| 774 | |
| 775 | packet->next = NULL; |
| 776 | packet->nb = size; |
| 777 | |
| 778 | /* |
| 779 | * SDIO specific header. |
| 780 | */ |
| 781 | packet->buffer[0] = (nb + 4) & 0xff; |
| 782 | packet->buffer[1] = ((nb + 4) >> 8) & 0xff; |
| 783 | packet->buffer[2] = type; |
| 784 | packet->buffer[3] = 0; |
| 785 | |
| 786 | memcpy(packet->buffer + 4, buf, nb); |
| 787 | |
| 788 | spin_lock_irqsave(&card->lock, flags); |
| 789 | |
| 790 | if (!card->packets) |
| 791 | card->packets = packet; |
| 792 | else { |
| 793 | cur = card->packets; |
| 794 | while (cur->next) |
| 795 | cur = cur->next; |
| 796 | cur->next = packet; |
| 797 | } |
| 798 | |
| 799 | switch (type) { |
| 800 | case MVMS_CMD: |
| 801 | priv->dnld_sent = DNLD_CMD_SENT; |
| 802 | break; |
| 803 | case MVMS_DAT: |
| 804 | priv->dnld_sent = DNLD_DATA_SENT; |
| 805 | break; |
| 806 | default: |
| 807 | lbs_deb_sdio("unknown packet type %d\n", (int)type); |
| 808 | } |
| 809 | |
| 810 | spin_unlock_irqrestore(&card->lock, flags); |
| 811 | |
Dan Williams | 9b02f41 | 2009-02-20 12:27:38 -0500 | [diff] [blame] | 812 | queue_work(card->workqueue, &card->packet_worker); |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 813 | |
| 814 | ret = 0; |
| 815 | |
| 816 | out: |
| 817 | lbs_deb_leave_args(LBS_DEB_SDIO, "ret %d", ret); |
| 818 | |
| 819 | return ret; |
| 820 | } |
| 821 | |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 822 | /*******************************************************************/ |
| 823 | /* SDIO callbacks */ |
| 824 | /*******************************************************************/ |
| 825 | |
| 826 | static void if_sdio_interrupt(struct sdio_func *func) |
| 827 | { |
| 828 | int ret; |
| 829 | struct if_sdio_card *card; |
| 830 | u8 cause; |
| 831 | |
| 832 | lbs_deb_enter(LBS_DEB_SDIO); |
| 833 | |
| 834 | card = sdio_get_drvdata(func); |
| 835 | |
| 836 | cause = sdio_readb(card->func, IF_SDIO_H_INT_STATUS, &ret); |
| 837 | if (ret) |
| 838 | goto out; |
| 839 | |
| 840 | lbs_deb_sdio("interrupt: 0x%X\n", (unsigned)cause); |
| 841 | |
| 842 | sdio_writeb(card->func, ~cause, IF_SDIO_H_INT_STATUS, &ret); |
| 843 | if (ret) |
| 844 | goto out; |
| 845 | |
| 846 | /* |
| 847 | * Ignore the define name, this really means the card has |
| 848 | * successfully received the command. |
| 849 | */ |
David Woodhouse | e775ed7 | 2007-12-06 14:36:11 +0000 | [diff] [blame] | 850 | if (cause & IF_SDIO_H_INT_DNLD) |
| 851 | lbs_host_to_card_done(card->priv); |
| 852 | |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 853 | |
| 854 | if (cause & IF_SDIO_H_INT_UPLD) { |
| 855 | ret = if_sdio_card_to_host(card); |
| 856 | if (ret) |
| 857 | goto out; |
| 858 | } |
| 859 | |
| 860 | ret = 0; |
| 861 | |
| 862 | out: |
| 863 | lbs_deb_leave_args(LBS_DEB_SDIO, "ret %d", ret); |
| 864 | } |
| 865 | |
| 866 | static int if_sdio_probe(struct sdio_func *func, |
| 867 | const struct sdio_device_id *id) |
| 868 | { |
| 869 | struct if_sdio_card *card; |
Holger Schurig | 69f9032 | 2007-11-23 15:43:44 +0100 | [diff] [blame] | 870 | struct lbs_private *priv; |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 871 | int ret, i; |
| 872 | unsigned int model; |
| 873 | struct if_sdio_packet *packet; |
| 874 | |
| 875 | lbs_deb_enter(LBS_DEB_SDIO); |
| 876 | |
| 877 | for (i = 0;i < func->card->num_info;i++) { |
| 878 | if (sscanf(func->card->info[i], |
| 879 | "802.11 SDIO ID: %x", &model) == 1) |
| 880 | break; |
| 881 | if (sscanf(func->card->info[i], |
| 882 | "ID: %x", &model) == 1) |
| 883 | break; |
Bing Zhao | e70a5ac | 2009-05-19 19:48:18 -0700 | [diff] [blame] | 884 | if (!strcmp(func->card->info[i], "IBIS Wireless SDIO Card")) { |
| 885 | model = IF_SDIO_MODEL_8385; |
| 886 | break; |
| 887 | } |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 888 | } |
| 889 | |
| 890 | if (i == func->card->num_info) { |
| 891 | lbs_pr_err("unable to identify card model\n"); |
| 892 | return -ENODEV; |
| 893 | } |
| 894 | |
| 895 | card = kzalloc(sizeof(struct if_sdio_card), GFP_KERNEL); |
| 896 | if (!card) |
| 897 | return -ENOMEM; |
| 898 | |
| 899 | card->func = func; |
| 900 | card->model = model; |
Bing Zhao | 2c7e579 | 2009-05-21 11:32:34 -0700 | [diff] [blame^] | 901 | |
| 902 | switch (card->model) { |
| 903 | case IF_SDIO_MODEL_8385: |
| 904 | card->scratch_reg = IF_SDIO_SCRATCH_OLD; |
| 905 | break; |
| 906 | case IF_SDIO_MODEL_8686: |
| 907 | card->scratch_reg = IF_SDIO_SCRATCH; |
| 908 | break; |
| 909 | case IF_SDIO_MODEL_8688: |
| 910 | default: /* for newer chipsets */ |
| 911 | card->scratch_reg = IF_SDIO_FW_STATUS; |
| 912 | break; |
| 913 | } |
| 914 | |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 915 | spin_lock_init(&card->lock); |
Dan Williams | 9b02f41 | 2009-02-20 12:27:38 -0500 | [diff] [blame] | 916 | card->workqueue = create_workqueue("libertas_sdio"); |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 917 | INIT_WORK(&card->packet_worker, if_sdio_host_to_card_worker); |
| 918 | |
| 919 | for (i = 0;i < ARRAY_SIZE(if_sdio_models);i++) { |
| 920 | if (card->model == if_sdio_models[i].model) |
| 921 | break; |
| 922 | } |
| 923 | |
| 924 | if (i == ARRAY_SIZE(if_sdio_models)) { |
| 925 | lbs_pr_err("unkown card model 0x%x\n", card->model); |
| 926 | ret = -ENODEV; |
| 927 | goto free; |
| 928 | } |
| 929 | |
Bing Zhao | d26285f8 | 2009-05-19 19:48:20 -0700 | [diff] [blame] | 930 | if_sdio_models[i].card = card; |
| 931 | |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 932 | card->helper = if_sdio_models[i].helper; |
| 933 | card->firmware = if_sdio_models[i].firmware; |
| 934 | |
Holger Schurig | 1007832 | 2007-11-15 18:05:47 -0500 | [diff] [blame] | 935 | if (lbs_helper_name) { |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 936 | lbs_deb_sdio("overriding helper firmware: %s\n", |
Holger Schurig | 1007832 | 2007-11-15 18:05:47 -0500 | [diff] [blame] | 937 | lbs_helper_name); |
| 938 | card->helper = lbs_helper_name; |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 939 | } |
| 940 | |
Holger Schurig | 1007832 | 2007-11-15 18:05:47 -0500 | [diff] [blame] | 941 | if (lbs_fw_name) { |
| 942 | lbs_deb_sdio("overriding firmware: %s\n", lbs_fw_name); |
| 943 | card->firmware = lbs_fw_name; |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 944 | } |
| 945 | |
| 946 | sdio_claim_host(func); |
| 947 | |
| 948 | ret = sdio_enable_func(func); |
| 949 | if (ret) |
| 950 | goto release; |
| 951 | |
| 952 | ret = sdio_claim_irq(func, if_sdio_interrupt); |
| 953 | if (ret) |
| 954 | goto disable; |
| 955 | |
| 956 | card->ioport = sdio_readb(func, IF_SDIO_IOPORT, &ret); |
| 957 | if (ret) |
| 958 | goto release_int; |
| 959 | |
| 960 | card->ioport |= sdio_readb(func, IF_SDIO_IOPORT + 1, &ret) << 8; |
| 961 | if (ret) |
| 962 | goto release_int; |
| 963 | |
| 964 | card->ioport |= sdio_readb(func, IF_SDIO_IOPORT + 2, &ret) << 16; |
| 965 | if (ret) |
| 966 | goto release_int; |
| 967 | |
| 968 | sdio_release_host(func); |
| 969 | |
| 970 | sdio_set_drvdata(func, card); |
| 971 | |
| 972 | lbs_deb_sdio("class = 0x%X, vendor = 0x%X, " |
| 973 | "device = 0x%X, model = 0x%X, ioport = 0x%X\n", |
| 974 | func->class, func->vendor, func->device, |
| 975 | model, (unsigned)card->ioport); |
| 976 | |
| 977 | ret = if_sdio_prog_firmware(card); |
| 978 | if (ret) |
| 979 | goto reclaim; |
| 980 | |
Holger Schurig | 1007832 | 2007-11-15 18:05:47 -0500 | [diff] [blame] | 981 | priv = lbs_add_card(card, &func->dev); |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 982 | if (!priv) { |
| 983 | ret = -ENOMEM; |
| 984 | goto reclaim; |
| 985 | } |
| 986 | |
| 987 | card->priv = priv; |
| 988 | |
| 989 | priv->card = card; |
| 990 | priv->hw_host_to_card = if_sdio_host_to_card; |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 991 | |
David Woodhouse | aa21c00 | 2007-12-08 20:04:36 +0000 | [diff] [blame] | 992 | priv->fw_ready = 1; |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 993 | |
Bing Zhao | b136a14 | 2009-05-19 19:48:19 -0700 | [diff] [blame] | 994 | sdio_claim_host(func); |
| 995 | |
| 996 | /* |
| 997 | * Get rx_unit if the chip is SD8688 or newer. |
| 998 | * SD8385 & SD8686 do not have rx_unit. |
| 999 | */ |
| 1000 | if ((card->model != IF_SDIO_MODEL_8385) |
| 1001 | && (card->model != IF_SDIO_MODEL_8686)) |
| 1002 | card->rx_unit = if_sdio_read_rx_unit(card); |
| 1003 | else |
| 1004 | card->rx_unit = 0; |
| 1005 | |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 1006 | /* |
| 1007 | * Enable interrupts now that everything is set up |
| 1008 | */ |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 1009 | sdio_writeb(func, 0x0f, IF_SDIO_H_INT_MASK, &ret); |
| 1010 | sdio_release_host(func); |
| 1011 | if (ret) |
| 1012 | goto reclaim; |
| 1013 | |
Bing Zhao | d26285f8 | 2009-05-19 19:48:20 -0700 | [diff] [blame] | 1014 | /* |
| 1015 | * FUNC_INIT is required for SD8688 WLAN/BT multiple functions |
| 1016 | */ |
| 1017 | priv->fn_init_required = |
| 1018 | (card->model == IF_SDIO_MODEL_8688) ? 1 : 0; |
| 1019 | |
Holger Schurig | 1007832 | 2007-11-15 18:05:47 -0500 | [diff] [blame] | 1020 | ret = lbs_start_card(priv); |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 1021 | if (ret) |
| 1022 | goto err_activate_card; |
| 1023 | |
Bing Zhao | f5ac2b9 | 2009-02-04 22:22:39 -0800 | [diff] [blame] | 1024 | if (priv->fwcapinfo & FW_CAPINFO_PS) |
| 1025 | priv->ps_supported = 1; |
| 1026 | |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 1027 | out: |
| 1028 | lbs_deb_leave_args(LBS_DEB_SDIO, "ret %d", ret); |
| 1029 | |
| 1030 | return ret; |
| 1031 | |
| 1032 | err_activate_card: |
Dan Williams | 9b02f41 | 2009-02-20 12:27:38 -0500 | [diff] [blame] | 1033 | flush_workqueue(card->workqueue); |
| 1034 | lbs_remove_card(priv); |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 1035 | reclaim: |
| 1036 | sdio_claim_host(func); |
| 1037 | release_int: |
| 1038 | sdio_release_irq(func); |
| 1039 | disable: |
| 1040 | sdio_disable_func(func); |
| 1041 | release: |
| 1042 | sdio_release_host(func); |
| 1043 | free: |
Dan Williams | 9b02f41 | 2009-02-20 12:27:38 -0500 | [diff] [blame] | 1044 | destroy_workqueue(card->workqueue); |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 1045 | while (card->packets) { |
| 1046 | packet = card->packets; |
| 1047 | card->packets = card->packets->next; |
| 1048 | kfree(packet); |
| 1049 | } |
| 1050 | |
| 1051 | kfree(card); |
| 1052 | |
| 1053 | goto out; |
| 1054 | } |
| 1055 | |
| 1056 | static void if_sdio_remove(struct sdio_func *func) |
| 1057 | { |
| 1058 | struct if_sdio_card *card; |
| 1059 | struct if_sdio_packet *packet; |
Bing Zhao | d26285f8 | 2009-05-19 19:48:20 -0700 | [diff] [blame] | 1060 | int ret; |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 1061 | |
| 1062 | lbs_deb_enter(LBS_DEB_SDIO); |
| 1063 | |
| 1064 | card = sdio_get_drvdata(func); |
| 1065 | |
Bing Zhao | d26285f8 | 2009-05-19 19:48:20 -0700 | [diff] [blame] | 1066 | lbs_stop_card(card->priv); |
| 1067 | |
David Woodhouse | aa21c00 | 2007-12-08 20:04:36 +0000 | [diff] [blame] | 1068 | card->priv->surpriseremoved = 1; |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 1069 | |
| 1070 | lbs_deb_sdio("call remove card\n"); |
Holger Schurig | 1007832 | 2007-11-15 18:05:47 -0500 | [diff] [blame] | 1071 | lbs_remove_card(card->priv); |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 1072 | |
Dan Williams | 9b02f41 | 2009-02-20 12:27:38 -0500 | [diff] [blame] | 1073 | flush_workqueue(card->workqueue); |
| 1074 | destroy_workqueue(card->workqueue); |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 1075 | |
| 1076 | sdio_claim_host(func); |
Bing Zhao | d26285f8 | 2009-05-19 19:48:20 -0700 | [diff] [blame] | 1077 | |
| 1078 | /* Disable interrupts */ |
| 1079 | sdio_writeb(func, 0x00, IF_SDIO_H_INT_MASK, &ret); |
| 1080 | |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 1081 | sdio_release_irq(func); |
| 1082 | sdio_disable_func(func); |
Bing Zhao | d26285f8 | 2009-05-19 19:48:20 -0700 | [diff] [blame] | 1083 | |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 1084 | sdio_release_host(func); |
| 1085 | |
| 1086 | while (card->packets) { |
| 1087 | packet = card->packets; |
| 1088 | card->packets = card->packets->next; |
| 1089 | kfree(packet); |
| 1090 | } |
| 1091 | |
| 1092 | kfree(card); |
| 1093 | |
| 1094 | lbs_deb_leave(LBS_DEB_SDIO); |
| 1095 | } |
| 1096 | |
| 1097 | static struct sdio_driver if_sdio_driver = { |
| 1098 | .name = "libertas_sdio", |
| 1099 | .id_table = if_sdio_ids, |
| 1100 | .probe = if_sdio_probe, |
| 1101 | .remove = if_sdio_remove, |
| 1102 | }; |
| 1103 | |
| 1104 | /*******************************************************************/ |
| 1105 | /* Module functions */ |
| 1106 | /*******************************************************************/ |
| 1107 | |
Andres Salomon | 4fb910f | 2007-11-20 17:43:45 -0500 | [diff] [blame] | 1108 | static int __init if_sdio_init_module(void) |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 1109 | { |
| 1110 | int ret = 0; |
| 1111 | |
| 1112 | lbs_deb_enter(LBS_DEB_SDIO); |
| 1113 | |
| 1114 | printk(KERN_INFO "libertas_sdio: Libertas SDIO driver\n"); |
| 1115 | printk(KERN_INFO "libertas_sdio: Copyright Pierre Ossman\n"); |
| 1116 | |
| 1117 | ret = sdio_register_driver(&if_sdio_driver); |
| 1118 | |
| 1119 | lbs_deb_leave_args(LBS_DEB_SDIO, "ret %d", ret); |
| 1120 | |
| 1121 | return ret; |
| 1122 | } |
| 1123 | |
Andres Salomon | 4fb910f | 2007-11-20 17:43:45 -0500 | [diff] [blame] | 1124 | static void __exit if_sdio_exit_module(void) |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 1125 | { |
Bing Zhao | d26285f8 | 2009-05-19 19:48:20 -0700 | [diff] [blame] | 1126 | int i; |
| 1127 | struct if_sdio_card *card; |
| 1128 | |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 1129 | lbs_deb_enter(LBS_DEB_SDIO); |
| 1130 | |
Bing Zhao | d26285f8 | 2009-05-19 19:48:20 -0700 | [diff] [blame] | 1131 | for (i = 0; i < ARRAY_SIZE(if_sdio_models); i++) { |
| 1132 | card = if_sdio_models[i].card; |
| 1133 | |
| 1134 | /* |
| 1135 | * FUNC_SHUTDOWN is required for SD8688 WLAN/BT |
| 1136 | * multiple functions |
| 1137 | */ |
| 1138 | if (card && card->priv) |
| 1139 | card->priv->fn_shutdown_required = |
| 1140 | (card->model == IF_SDIO_MODEL_8688) ? 1 : 0; |
| 1141 | } |
| 1142 | |
Pierre Ossman | 727c26e | 2007-10-17 22:24:24 +0200 | [diff] [blame] | 1143 | sdio_unregister_driver(&if_sdio_driver); |
| 1144 | |
| 1145 | lbs_deb_leave(LBS_DEB_SDIO); |
| 1146 | } |
| 1147 | |
| 1148 | module_init(if_sdio_init_module); |
| 1149 | module_exit(if_sdio_exit_module); |
| 1150 | |
| 1151 | MODULE_DESCRIPTION("Libertas SDIO WLAN Driver"); |
| 1152 | MODULE_AUTHOR("Pierre Ossman"); |
| 1153 | MODULE_LICENSE("GPL"); |