sdio_cis.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. * 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. #include <linux/kernel.h>
  14. #include <linux/mmc/host.h>
  15. #include <linux/mmc/sdio.h>
  16. #include <linux/mmc/sdio_func.h>
  17. #include "sdio_cis.h"
  18. #include "sdio_ops.h"
  19. static int cistpl_manfid(struct sdio_func *func,
  20. const unsigned char *buf,
  21. unsigned size)
  22. {
  23. /* TPLMID_MANF */
  24. func->vendor = buf[0] | (buf[1] << 8);
  25. /* TPLMID_CARD */
  26. func->device = buf[2] | (buf[3] << 8);
  27. return 0;
  28. }
  29. struct cis_tpl {
  30. unsigned char code;
  31. unsigned char min_size;
  32. int (*parse)(struct sdio_func *, const unsigned char *buf, unsigned size);
  33. };
  34. static const struct cis_tpl cis_tpl_list[] = {
  35. { 0x15, 3, /* cistpl_vers_1 */ },
  36. { 0x20, 4, cistpl_manfid },
  37. { 0x21, 2, /* cistpl_funcid */ },
  38. { 0x22, 0, /* cistpl_funce */ },
  39. };
  40. int sdio_read_cis(struct sdio_func *func)
  41. {
  42. int ret;
  43. unsigned char *buf;
  44. unsigned i, ptr = 0;
  45. for (i = 0; i < 3; i++) {
  46. unsigned char x;
  47. ret = mmc_io_rw_direct(func->card, 0, 0,
  48. func->num * 0x100 + SDIO_FBR_CIS + i, 0, &x);
  49. if (ret)
  50. return ret;
  51. ptr |= x << (i * 8);
  52. }
  53. buf = kmalloc(256, GFP_KERNEL);
  54. if (!buf)
  55. return -ENOMEM;
  56. do {
  57. unsigned char tpl_code, tpl_link;
  58. const struct cis_tpl *tpl;
  59. ret = mmc_io_rw_direct(func->card, 0, 0, ptr++, 0, &tpl_code);
  60. if (ret)
  61. break;
  62. /* 0xff means we're done */
  63. if (tpl_code == 0xff)
  64. break;
  65. ret = mmc_io_rw_direct(func->card, 0, 0, ptr++, 0, &tpl_link);
  66. if (ret)
  67. break;
  68. for (i = 0; i < ARRAY_SIZE(cis_tpl_list); i++)
  69. if (cis_tpl_list[i].code == tpl_code)
  70. break;
  71. if (i >= ARRAY_SIZE(cis_tpl_list)) {
  72. printk(KERN_WARNING
  73. "%s: unknown CIS tuple 0x%02x of length %u\n",
  74. sdio_func_id(func), tpl_code, tpl_link);
  75. ptr += tpl_link;
  76. continue;
  77. }
  78. tpl = cis_tpl_list + i;
  79. if (tpl_link < tpl->min_size) {
  80. printk(KERN_ERR
  81. "%s: bad CIS tuple 0x%02x (length = %u, expected >= %u\n",
  82. sdio_func_id(func), tpl_code, tpl_link, tpl->min_size);
  83. ret = -EINVAL;
  84. break;
  85. }
  86. for (i = 0; i < tpl_link; i++) {
  87. ret = mmc_io_rw_direct(func->card, 0, 0, ptr + i, 0, &buf[i]);
  88. if (ret)
  89. break;
  90. }
  91. if (ret)
  92. break;
  93. ptr += tpl_link;
  94. if (tpl->parse)
  95. ret = tpl->parse(func, buf, tpl_link);
  96. } while (!ret);
  97. kfree(buf);
  98. return ret;
  99. }