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