linux/drivers/mmc/core/sdio_cis.c
<<
>>
Prefs
   1/*
   2 * linux/drivers/mmc/core/sdio_cis.c
   3 *
   4 * Author:      Nicolas Pitre
   5 * Created:     June 11, 2007
   6 * Copyright:   MontaVista Software Inc.
   7 *
   8 * Copyright 2007 Pierre Ossman
   9 *
  10 * This program is free software; you can redistribute it and/or modify
  11 * it under the terms of the GNU General Public License as published by
  12 * the Free Software Foundation; either version 2 of the License, or (at
  13 * your option) any later version.
  14 */
  15
  16#include <linux/kernel.h>
  17
  18#include <linux/mmc/host.h>
  19#include <linux/mmc/card.h>
  20#include <linux/mmc/sdio.h>
  21#include <linux/mmc/sdio_func.h>
  22
  23#include "sdio_cis.h"
  24#include "sdio_ops.h"
  25
  26static int cistpl_vers_1(struct mmc_card *card, struct sdio_func *func,
  27                         const unsigned char *buf, unsigned size)
  28{
  29        unsigned i, nr_strings;
  30        char **buffer, *string;
  31
  32        /* Find all null-terminated (including zero length) strings in
  33           the TPLLV1_INFO field. Trailing garbage is ignored. */
  34        buf += 2;
  35        size -= 2;
  36
  37        nr_strings = 0;
  38        for (i = 0; i < size; i++) {
  39                if (buf[i] == 0xff)
  40                        break;
  41                if (buf[i] == 0)
  42                        nr_strings++;
  43        }
  44        if (nr_strings == 0)
  45                return 0;
  46
  47        size = i;
  48
  49        buffer = kzalloc(sizeof(char*) * nr_strings + size, GFP_KERNEL);
  50        if (!buffer)
  51                return -ENOMEM;
  52
  53        string = (char*)(buffer + nr_strings);
  54
  55        for (i = 0; i < nr_strings; i++) {
  56                buffer[i] = string;
  57                strcpy(string, buf);
  58                string += strlen(string) + 1;
  59                buf += strlen(buf) + 1;
  60        }
  61
  62        if (func) {
  63                func->num_info = nr_strings;
  64                func->info = (const char**)buffer;
  65        } else {
  66                card->num_info = nr_strings;
  67                card->info = (const char**)buffer;
  68        }
  69
  70        return 0;
  71}
  72
  73static int cistpl_manfid(struct mmc_card *card, struct sdio_func *func,
  74                         const unsigned char *buf, unsigned size)
  75{
  76        unsigned int vendor, device;
  77
  78        /* TPLMID_MANF */
  79        vendor = buf[0] | (buf[1] << 8);
  80
  81        /* TPLMID_CARD */
  82        device = buf[2] | (buf[3] << 8);
  83
  84        if (func) {
  85                func->vendor = vendor;
  86                func->device = device;
  87        } else {
  88                card->cis.vendor = vendor;
  89                card->cis.device = device;
  90        }
  91
  92        return 0;
  93}
  94
  95static const unsigned char speed_val[16] =
  96        { 0, 10, 12, 13, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 70, 80 };
  97static const unsigned int speed_unit[8] =
  98        { 10000, 100000, 1000000, 10000000, 0, 0, 0, 0 };
  99
 100
 101typedef int (tpl_parse_t)(struct mmc_card *, struct sdio_func *,
 102                           const unsigned char *, unsigned);
 103
 104struct cis_tpl {
 105        unsigned char code;
 106        unsigned char min_size;
 107        tpl_parse_t *parse;
 108};
 109
 110static int cis_tpl_parse(struct mmc_card *card, struct sdio_func *func,
 111                         const char *tpl_descr,
 112                         const struct cis_tpl *tpl, int tpl_count,
 113                         unsigned char code,
 114                         const unsigned char *buf, unsigned size)
 115{
 116        int i, ret;
 117
 118        /* look for a matching code in the table */
 119        for (i = 0; i < tpl_count; i++, tpl++) {
 120                if (tpl->code == code)
 121                        break;
 122        }
 123        if (i < tpl_count) {
 124                if (size >= tpl->min_size) {
 125                        if (tpl->parse)
 126                                ret = tpl->parse(card, func, buf, size);
 127                        else
 128                                ret = -EILSEQ;  /* known tuple, not parsed */
 129                } else {
 130                        /* invalid tuple */
 131                        ret = -EINVAL;
 132                }
 133                if (ret && ret != -EILSEQ && ret != -ENOENT) {
 134                        printk(KERN_ERR "%s: bad %s tuple 0x%02x (%u bytes)\n",
 135                               mmc_hostname(card->host), tpl_descr, code, size);
 136                }
 137        } else {
 138                /* unknown tuple */
 139                ret = -ENOENT;
 140        }
 141
 142        return ret;
 143}
 144
 145static int cistpl_funce_common(struct mmc_card *card, struct sdio_func *func,
 146                               const unsigned char *buf, unsigned size)
 147{
 148        /* Only valid for the common CIS (function 0) */
 149        if (func)
 150                return -EINVAL;
 151
 152        /* TPLFE_FN0_BLK_SIZE */
 153        card->cis.blksize = buf[1] | (buf[2] << 8);
 154
 155        /* TPLFE_MAX_TRAN_SPEED */
 156        card->cis.max_dtr = speed_val[(buf[3] >> 3) & 15] *
 157                            speed_unit[buf[3] & 7];
 158
 159        return 0;
 160}
 161
 162static int cistpl_funce_func(struct mmc_card *card, struct sdio_func *func,
 163                             const unsigned char *buf, unsigned size)
 164{
 165        unsigned vsn;
 166        unsigned min_size;
 167
 168        /* Only valid for the individual function's CIS (1-7) */
 169        if (!func)
 170                return -EINVAL;
 171
 172        /*
 173         * This tuple has a different length depending on the SDIO spec
 174         * version.
 175         */
 176        vsn = func->card->cccr.sdio_vsn;
 177        min_size = (vsn == SDIO_SDIO_REV_1_00) ? 28 : 42;
 178
 179        if (size < min_size)
 180                return -EINVAL;
 181
 182        /* TPLFE_MAX_BLK_SIZE */
 183        func->max_blksize = buf[12] | (buf[13] << 8);
 184
 185        /* TPLFE_ENABLE_TIMEOUT_VAL, present in ver 1.1 and above */
 186        if (vsn > SDIO_SDIO_REV_1_00)
 187                func->enable_timeout = (buf[28] | (buf[29] << 8)) * 10;
 188        else
 189                func->enable_timeout = jiffies_to_msecs(HZ);
 190
 191        return 0;
 192}
 193
 194/*
 195 * Known TPLFE_TYPEs table for CISTPL_FUNCE tuples.
 196 *
 197 * Note that, unlike PCMCIA, CISTPL_FUNCE tuples are not parsed depending
 198 * on the TPLFID_FUNCTION value of the previous CISTPL_FUNCID as on SDIO
 199 * TPLFID_FUNCTION is always hardcoded to 0x0C.
 200 */
 201static const struct cis_tpl cis_tpl_funce_list[] = {
 202        {       0x00,   4,      cistpl_funce_common             },
 203        {       0x01,   0,      cistpl_funce_func               },
 204        {       0x04,   1+1+6,  /* CISTPL_FUNCE_LAN_NODE_ID */  },
 205};
 206
 207static int cistpl_funce(struct mmc_card *card, struct sdio_func *func,
 208                        const unsigned char *buf, unsigned size)
 209{
 210        if (size < 1)
 211                return -EINVAL;
 212
 213        return cis_tpl_parse(card, func, "CISTPL_FUNCE",
 214                             cis_tpl_funce_list,
 215                             ARRAY_SIZE(cis_tpl_funce_list),
 216                             buf[0], buf, size);
 217}
 218
 219/* Known TPL_CODEs table for CIS tuples */
 220static const struct cis_tpl cis_tpl_list[] = {
 221        {       0x15,   3,      cistpl_vers_1           },
 222        {       0x20,   4,      cistpl_manfid           },
 223        {       0x21,   2,      /* cistpl_funcid */     },
 224        {       0x22,   0,      cistpl_funce            },
 225};
 226
 227static int sdio_read_cis(struct mmc_card *card, struct sdio_func *func)
 228{
 229        int ret;
 230        struct sdio_func_tuple *this, **prev;
 231        unsigned i, ptr = 0;
 232
 233        /*
 234         * Note that this works for the common CIS (function number 0) as
 235         * well as a function's CIS * since SDIO_CCCR_CIS and SDIO_FBR_CIS
 236         * have the same offset.
 237         */
 238        for (i = 0; i < 3; i++) {
 239                unsigned char x, fn;
 240
 241                if (func)
 242                        fn = func->num;
 243                else
 244                        fn = 0;
 245
 246                ret = mmc_io_rw_direct(card, 0, 0,
 247                        SDIO_FBR_BASE(fn) + SDIO_FBR_CIS + i, 0, &x);
 248                if (ret)
 249                        return ret;
 250                ptr |= x << (i * 8);
 251        }
 252
 253        if (func)
 254                prev = &func->tuples;
 255        else
 256                prev = &card->tuples;
 257
 258        BUG_ON(*prev);
 259
 260        do {
 261                unsigned char tpl_code, tpl_link;
 262
 263                ret = mmc_io_rw_direct(card, 0, 0, ptr++, 0, &tpl_code);
 264                if (ret)
 265                        break;
 266
 267                /* 0xff means we're done */
 268                if (tpl_code == 0xff)
 269                        break;
 270
 271                /* null entries have no link field or data */
 272                if (tpl_code == 0x00)
 273                        continue;
 274
 275                ret = mmc_io_rw_direct(card, 0, 0, ptr++, 0, &tpl_link);
 276                if (ret)
 277                        break;
 278
 279                /* a size of 0xff also means we're done */
 280                if (tpl_link == 0xff)
 281                        break;
 282
 283                this = kmalloc(sizeof(*this) + tpl_link, GFP_KERNEL);
 284                if (!this)
 285                        return -ENOMEM;
 286
 287                for (i = 0; i < tpl_link; i++) {
 288                        ret = mmc_io_rw_direct(card, 0, 0,
 289                                               ptr + i, 0, &this->data[i]);
 290                        if (ret)
 291                                break;
 292                }
 293                if (ret) {
 294                        kfree(this);
 295                        break;
 296                }
 297
 298                /* Try to parse the CIS tuple */
 299                ret = cis_tpl_parse(card, func, "CIS",
 300                                    cis_tpl_list, ARRAY_SIZE(cis_tpl_list),
 301                                    tpl_code, this->data, tpl_link);
 302                if (ret == -EILSEQ || ret == -ENOENT) {
 303                        /*
 304                         * The tuple is unknown or known but not parsed.
 305                         * Queue the tuple for the function driver.
 306                         */
 307                        this->next = NULL;
 308                        this->code = tpl_code;
 309                        this->size = tpl_link;
 310                        *prev = this;
 311                        prev = &this->next;
 312
 313                        if (ret == -ENOENT) {
 314                                /* warn about unknown tuples */
 315                                printk(KERN_WARNING "%s: queuing unknown"
 316                                       " CIS tuple 0x%02x (%u bytes)\n",
 317                                       mmc_hostname(card->host),
 318                                       tpl_code, tpl_link);
 319                        }
 320
 321                        /* keep on analyzing tuples */
 322                        ret = 0;
 323                } else {
 324                        /*
 325                         * We don't need the tuple anymore if it was
 326                         * successfully parsed by the SDIO core or if it is
 327                         * not going to be queued for a driver.
 328                         */
 329                        kfree(this);
 330                }
 331
 332                ptr += tpl_link;
 333        } while (!ret);
 334
 335        /*
 336         * Link in all unknown tuples found in the common CIS so that
 337         * drivers don't have to go digging in two places.
 338         */
 339        if (func)
 340                *prev = card->tuples;
 341
 342        return ret;
 343}
 344
 345int sdio_read_common_cis(struct mmc_card *card)
 346{
 347        return sdio_read_cis(card, NULL);
 348}
 349
 350void sdio_free_common_cis(struct mmc_card *card)
 351{
 352        struct sdio_func_tuple *tuple, *victim;
 353
 354        tuple = card->tuples;
 355
 356        while (tuple) {
 357                victim = tuple;
 358                tuple = tuple->next;
 359                kfree(victim);
 360        }
 361
 362        card->tuples = NULL;
 363}
 364
 365int sdio_read_func_cis(struct sdio_func *func)
 366{
 367        int ret;
 368
 369        ret = sdio_read_cis(func->card, func);
 370        if (ret)
 371                return ret;
 372
 373        /*
 374         * Since we've linked to tuples in the card structure,
 375         * we must make sure we have a reference to it.
 376         */
 377        get_device(&func->card->dev);
 378
 379        /*
 380         * Vendor/device id is optional for function CIS, so
 381         * copy it from the card structure as needed.
 382         */
 383        if (func->vendor == 0) {
 384                func->vendor = func->card->cis.vendor;
 385                func->device = func->card->cis.device;
 386        }
 387
 388        return 0;
 389}
 390
 391void sdio_free_func_cis(struct sdio_func *func)
 392{
 393        struct sdio_func_tuple *tuple, *victim;
 394
 395        tuple = func->tuples;
 396
 397        while (tuple && tuple != func->card->tuples) {
 398                victim = tuple;
 399                tuple = tuple->next;
 400                kfree(victim);
 401        }
 402
 403        func->tuples = NULL;
 404
 405        /*
 406         * We have now removed the link to the tuples in the
 407         * card structure, so remove the reference.
 408         */
 409        put_device(&func->card->dev);
 410}
 411
 412
lxr.linux.no kindly hosted by Redpill Linpro AS, provider of Linux consulting and operations services since 1995.