linux/drivers/net/wireless/orinoco/fw.c
<<
>>
Prefs
   1/* Firmware file reading and download helpers
   2 *
   3 * See copyright notice in main.c
   4 */
   5#include <linux/kernel.h>
   6#include <linux/firmware.h>
   7#include <linux/device.h>
   8
   9#include "hermes.h"
  10#include "hermes_dld.h"
  11#include "orinoco.h"
  12
  13#include "fw.h"
  14
  15/* End markers (for Symbol firmware only) */
  16#define TEXT_END        0x1A            /* End of text header */
  17
  18struct fw_info {
  19        char *pri_fw;
  20        char *sta_fw;
  21        char *ap_fw;
  22        u32 pda_addr;
  23        u16 pda_size;
  24};
  25
  26static const struct fw_info orinoco_fw[] = {
  27        { NULL, "agere_sta_fw.bin", "agere_ap_fw.bin", 0x00390000, 1000 },
  28        { NULL, "prism_sta_fw.bin", "prism_ap_fw.bin", 0, 1024 },
  29        { "symbol_sp24t_prim_fw", "symbol_sp24t_sec_fw", NULL, 0x00003100, 512 }
  30};
  31
  32/* Structure used to access fields in FW
  33 * Make sure LE decoding macros are used
  34 */
  35struct orinoco_fw_header {
  36        char hdr_vers[6];       /* ASCII string for header version */
  37        __le16 headersize;      /* Total length of header */
  38        __le32 entry_point;     /* NIC entry point */
  39        __le32 blocks;          /* Number of blocks to program */
  40        __le32 block_offset;    /* Offset of block data from eof header */
  41        __le32 pdr_offset;      /* Offset to PDR data from eof header */
  42        __le32 pri_offset;      /* Offset to primary plug data */
  43        __le32 compat_offset;   /* Offset to compatibility data*/
  44        char signature[0];      /* FW signature length headersize-20 */
  45} __attribute__ ((packed));
  46
  47/* Check the range of various header entries. Return a pointer to a
  48 * description of the problem, or NULL if everything checks out. */
  49static const char *validate_fw(const struct orinoco_fw_header *hdr, size_t len)
  50{
  51        u16 hdrsize;
  52
  53        if (len < sizeof(*hdr))
  54                return "image too small";
  55        if (memcmp(hdr->hdr_vers, "HFW", 3) != 0)
  56                return "format not recognised";
  57
  58        hdrsize = le16_to_cpu(hdr->headersize);
  59        if (hdrsize > len)
  60                return "bad headersize";
  61        if ((hdrsize + le32_to_cpu(hdr->block_offset)) > len)
  62                return "bad block offset";
  63        if ((hdrsize + le32_to_cpu(hdr->pdr_offset)) > len)
  64                return "bad PDR offset";
  65        if ((hdrsize + le32_to_cpu(hdr->pri_offset)) > len)
  66                return "bad PRI offset";
  67        if ((hdrsize + le32_to_cpu(hdr->compat_offset)) > len)
  68                return "bad compat offset";
  69
  70        /* TODO: consider adding a checksum or CRC to the firmware format */
  71        return NULL;
  72}
  73
  74#if defined(CONFIG_HERMES_CACHE_FW_ON_INIT) || defined(CONFIG_PM_SLEEP)
  75static inline const struct firmware *
  76orinoco_cached_fw_get(struct orinoco_private *priv, bool primary)
  77{
  78        if (primary)
  79                return priv->cached_pri_fw;
  80        else
  81                return priv->cached_fw;
  82}
  83#else
  84#define orinoco_cached_fw_get(priv, primary) (NULL)
  85#endif
  86
  87/* Download either STA or AP firmware into the card. */
  88static int
  89orinoco_dl_firmware(struct orinoco_private *priv,
  90                    const struct fw_info *fw,
  91                    int ap)
  92{
  93        /* Plug Data Area (PDA) */
  94        __le16 *pda;
  95
  96        hermes_t *hw = &priv->hw;
  97        const struct firmware *fw_entry;
  98        const struct orinoco_fw_header *hdr;
  99        const unsigned char *first_block;
 100        const void *end;
 101        const char *firmware;
 102        const char *fw_err;
 103        struct device *dev = priv->dev;
 104        int err = 0;
 105
 106        pda = kzalloc(fw->pda_size, GFP_KERNEL);
 107        if (!pda)
 108                return -ENOMEM;
 109
 110        if (ap)
 111                firmware = fw->ap_fw;
 112        else
 113                firmware = fw->sta_fw;
 114
 115        dev_dbg(dev, "Attempting to download firmware %s\n", firmware);
 116
 117        /* Read current plug data */
 118        err = hermes_read_pda(hw, pda, fw->pda_addr, fw->pda_size, 0);
 119        dev_dbg(dev, "Read PDA returned %d\n", err);
 120        if (err)
 121                goto free;
 122
 123        if (!orinoco_cached_fw_get(priv, false)) {
 124                err = request_firmware(&fw_entry, firmware, priv->dev);
 125
 126                if (err) {
 127                        dev_err(dev, "Cannot find firmware %s\n", firmware);
 128                        err = -ENOENT;
 129                        goto free;
 130                }
 131        } else
 132                fw_entry = orinoco_cached_fw_get(priv, false);
 133
 134        hdr = (const struct orinoco_fw_header *) fw_entry->data;
 135
 136        fw_err = validate_fw(hdr, fw_entry->size);
 137        if (fw_err) {
 138                dev_warn(dev, "Invalid firmware image detected (%s). "
 139                         "Aborting download\n", fw_err);
 140                err = -EINVAL;
 141                goto abort;
 142        }
 143
 144        /* Enable aux port to allow programming */
 145        err = hermesi_program_init(hw, le32_to_cpu(hdr->entry_point));
 146        dev_dbg(dev, "Program init returned %d\n", err);
 147        if (err != 0)
 148                goto abort;
 149
 150        /* Program data */
 151        first_block = (fw_entry->data +
 152                       le16_to_cpu(hdr->headersize) +
 153                       le32_to_cpu(hdr->block_offset));
 154        end = fw_entry->data + fw_entry->size;
 155
 156        err = hermes_program(hw, first_block, end);
 157        dev_dbg(dev, "Program returned %d\n", err);
 158        if (err != 0)
 159                goto abort;
 160
 161        /* Update production data */
 162        first_block = (fw_entry->data +
 163                       le16_to_cpu(hdr->headersize) +
 164                       le32_to_cpu(hdr->pdr_offset));
 165
 166        err = hermes_apply_pda_with_defaults(hw, first_block, end, pda,
 167                                             &pda[fw->pda_size / sizeof(*pda)]);
 168        dev_dbg(dev, "Apply PDA returned %d\n", err);
 169        if (err)
 170                goto abort;
 171
 172        /* Tell card we've finished */
 173        err = hermesi_program_end(hw);
 174        dev_dbg(dev, "Program end returned %d\n", err);
 175        if (err != 0)
 176                goto abort;
 177
 178        /* Check if we're running */
 179        dev_dbg(dev, "hermes_present returned %d\n", hermes_present(hw));
 180
 181abort:
 182        /* If we requested the firmware, release it. */
 183        if (!orinoco_cached_fw_get(priv, false))
 184                release_firmware(fw_entry);
 185
 186free:
 187        kfree(pda);
 188        return err;
 189}
 190
 191/*
 192 * Process a firmware image - stop the card, load the firmware, reset
 193 * the card and make sure it responds.  For the secondary firmware take
 194 * care of the PDA - read it and then write it on top of the firmware.
 195 */
 196static int
 197symbol_dl_image(struct orinoco_private *priv, const struct fw_info *fw,
 198                const unsigned char *image, const void *end,
 199                int secondary)
 200{
 201        hermes_t *hw = &priv->hw;
 202        int ret = 0;
 203        const unsigned char *ptr;
 204        const unsigned char *first_block;
 205
 206        /* Plug Data Area (PDA) */
 207        __le16 *pda = NULL;
 208
 209        /* Binary block begins after the 0x1A marker */
 210        ptr = image;
 211        while (*ptr++ != TEXT_END);
 212        first_block = ptr;
 213
 214        /* Read the PDA from EEPROM */
 215        if (secondary) {
 216                pda = kzalloc(fw->pda_size, GFP_KERNEL);
 217                if (!pda)
 218                        return -ENOMEM;
 219
 220                ret = hermes_read_pda(hw, pda, fw->pda_addr, fw->pda_size, 1);
 221                if (ret)
 222                        goto free;
 223        }
 224
 225        /* Stop the firmware, so that it can be safely rewritten */
 226        if (priv->stop_fw) {
 227                ret = priv->stop_fw(priv, 1);
 228                if (ret)
 229                        goto free;
 230        }
 231
 232        /* Program the adapter with new firmware */
 233        ret = hermes_program(hw, first_block, end);
 234        if (ret)
 235                goto free;
 236
 237        /* Write the PDA to the adapter */
 238        if (secondary) {
 239                size_t len = hermes_blocks_length(first_block, end);
 240                ptr = first_block + len;
 241                ret = hermes_apply_pda(hw, ptr, end, pda,
 242                                       &pda[fw->pda_size / sizeof(*pda)]);
 243                kfree(pda);
 244                if (ret)
 245                        return ret;
 246        }
 247
 248        /* Run the firmware */
 249        if (priv->stop_fw) {
 250                ret = priv->stop_fw(priv, 0);
 251                if (ret)
 252                        return ret;
 253        }
 254
 255        /* Reset hermes chip and make sure it responds */
 256        ret = hermes_init(hw);
 257
 258        /* hermes_reset() should return 0 with the secondary firmware */
 259        if (secondary && ret != 0)
 260                return -ENODEV;
 261
 262        /* And this should work with any firmware */
 263        if (!hermes_present(hw))
 264                return -ENODEV;
 265
 266        return 0;
 267
 268free:
 269        kfree(pda);
 270        return ret;
 271}
 272
 273
 274/*
 275 * Download the firmware into the card, this also does a PCMCIA soft
 276 * reset on the card, to make sure it's in a sane state.
 277 */
 278static int
 279symbol_dl_firmware(struct orinoco_private *priv,
 280                   const struct fw_info *fw)
 281{
 282        struct device *dev = priv->dev;
 283        int ret;
 284        const struct firmware *fw_entry;
 285
 286        if (!orinoco_cached_fw_get(priv, true)) {
 287                if (request_firmware(&fw_entry, fw->pri_fw, priv->dev) != 0) {
 288                        dev_err(dev, "Cannot find firmware: %s\n", fw->pri_fw);
 289                        return -ENOENT;
 290                }
 291        } else
 292                fw_entry = orinoco_cached_fw_get(priv, true);
 293
 294        /* Load primary firmware */
 295        ret = symbol_dl_image(priv, fw, fw_entry->data,
 296                              fw_entry->data + fw_entry->size, 0);
 297
 298        if (!orinoco_cached_fw_get(priv, true))
 299                release_firmware(fw_entry);
 300        if (ret) {
 301                dev_err(dev, "Primary firmware download failed\n");
 302                return ret;
 303        }
 304
 305        if (!orinoco_cached_fw_get(priv, false)) {
 306                if (request_firmware(&fw_entry, fw->sta_fw, priv->dev) != 0) {
 307                        dev_err(dev, "Cannot find firmware: %s\n", fw->sta_fw);
 308                        return -ENOENT;
 309                }
 310        } else
 311                fw_entry = orinoco_cached_fw_get(priv, false);
 312
 313        /* Load secondary firmware */
 314        ret = symbol_dl_image(priv, fw, fw_entry->data,
 315                              fw_entry->data + fw_entry->size, 1);
 316        if (!orinoco_cached_fw_get(priv, false))
 317                release_firmware(fw_entry);
 318        if (ret) {
 319                dev_err(dev, "Secondary firmware download failed\n");
 320        }
 321
 322        return ret;
 323}
 324
 325int orinoco_download(struct orinoco_private *priv)
 326{
 327        int err = 0;
 328        /* Reload firmware */
 329        switch (priv->firmware_type) {
 330        case FIRMWARE_TYPE_AGERE:
 331                /* case FIRMWARE_TYPE_INTERSIL: */
 332                err = orinoco_dl_firmware(priv,
 333                                          &orinoco_fw[priv->firmware_type], 0);
 334                break;
 335
 336        case FIRMWARE_TYPE_SYMBOL:
 337                err = symbol_dl_firmware(priv,
 338                                         &orinoco_fw[priv->firmware_type]);
 339                break;
 340        case FIRMWARE_TYPE_INTERSIL:
 341                break;
 342        }
 343        /* TODO: if we fail we probably need to reinitialise
 344         * the driver */
 345
 346        return err;
 347}
 348
 349#if defined(CONFIG_HERMES_CACHE_FW_ON_INIT) || defined(CONFIG_PM_SLEEP)
 350void orinoco_cache_fw(struct orinoco_private *priv, int ap)
 351{
 352        const struct firmware *fw_entry = NULL;
 353        const char *pri_fw;
 354        const char *fw;
 355
 356        pri_fw = orinoco_fw[priv->firmware_type].pri_fw;
 357        if (ap)
 358                fw = orinoco_fw[priv->firmware_type].ap_fw;
 359        else
 360                fw = orinoco_fw[priv->firmware_type].sta_fw;
 361
 362        if (pri_fw) {
 363                if (request_firmware(&fw_entry, pri_fw, priv->dev) == 0)
 364                        priv->cached_pri_fw = fw_entry;
 365        }
 366
 367        if (fw) {
 368                if (request_firmware(&fw_entry, fw, priv->dev) == 0)
 369                        priv->cached_fw = fw_entry;
 370        }
 371}
 372
 373void orinoco_uncache_fw(struct orinoco_private *priv)
 374{
 375        if (priv->cached_pri_fw)
 376                release_firmware(priv->cached_pri_fw);
 377        if (priv->cached_fw)
 378                release_firmware(priv->cached_fw);
 379
 380        priv->cached_pri_fw = NULL;
 381        priv->cached_fw = NULL;
 382}
 383#endif
 384