sprom.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /*
  2. * Sonics Silicon Backplane
  3. * Common SPROM support routines
  4. *
  5. * Copyright (C) 2005-2008 Michael Buesch <[email protected]>
  6. * Copyright (C) 2005 Martin Langer <[email protected]>
  7. * Copyright (C) 2005 Stefano Brivio <[email protected]>
  8. * Copyright (C) 2005 Danny van Dyk <[email protected]>
  9. * Copyright (C) 2005 Andreas Jaggi <[email protected]>
  10. *
  11. * Licensed under the GNU/GPL. See COPYING for details.
  12. */
  13. #include "ssb_private.h"
  14. #include <linux/ctype.h>
  15. #include <linux/slab.h>
  16. static int(*get_fallback_sprom)(struct ssb_bus *dev, struct ssb_sprom *out);
  17. static int sprom2hex(const u16 *sprom, char *buf, size_t buf_len,
  18. size_t sprom_size_words)
  19. {
  20. int i, pos = 0;
  21. for (i = 0; i < sprom_size_words; i++)
  22. pos += scnprintf(buf + pos, buf_len - pos - 1,
  23. "%04X", swab16(sprom[i]) & 0xFFFF);
  24. pos += scnprintf(buf + pos, buf_len - pos - 1, "\n");
  25. return pos + 1;
  26. }
  27. static int hex2sprom(u16 *sprom, const char *dump, size_t len,
  28. size_t sprom_size_words)
  29. {
  30. char c, tmp[5] = { 0 };
  31. int err, cnt = 0;
  32. unsigned long parsed;
  33. /* Strip whitespace at the end. */
  34. while (len) {
  35. c = dump[len - 1];
  36. if (!isspace(c) && c != '\0')
  37. break;
  38. len--;
  39. }
  40. /* Length must match exactly. */
  41. if (len != sprom_size_words * 4)
  42. return -EINVAL;
  43. while (cnt < sprom_size_words) {
  44. memcpy(tmp, dump, 4);
  45. dump += 4;
  46. err = kstrtoul(tmp, 16, &parsed);
  47. if (err)
  48. return err;
  49. sprom[cnt++] = swab16((u16)parsed);
  50. }
  51. return 0;
  52. }
  53. /* Common sprom device-attribute show-handler */
  54. ssize_t ssb_attr_sprom_show(struct ssb_bus *bus, char *buf,
  55. int (*sprom_read)(struct ssb_bus *bus, u16 *sprom))
  56. {
  57. u16 *sprom;
  58. int err = -ENOMEM;
  59. ssize_t count = 0;
  60. size_t sprom_size_words = bus->sprom_size;
  61. sprom = kcalloc(sprom_size_words, sizeof(u16), GFP_KERNEL);
  62. if (!sprom)
  63. goto out;
  64. /* Use interruptible locking, as the SPROM write might
  65. * be holding the lock for several seconds. So allow userspace
  66. * to cancel operation.
  67. */
  68. err = -ERESTARTSYS;
  69. if (mutex_lock_interruptible(&bus->sprom_mutex))
  70. goto out_kfree;
  71. err = sprom_read(bus, sprom);
  72. mutex_unlock(&bus->sprom_mutex);
  73. if (!err)
  74. count = sprom2hex(sprom, buf, PAGE_SIZE, sprom_size_words);
  75. out_kfree:
  76. kfree(sprom);
  77. out:
  78. return err ? err : count;
  79. }
  80. /* Common sprom device-attribute store-handler */
  81. ssize_t ssb_attr_sprom_store(struct ssb_bus *bus,
  82. const char *buf, size_t count,
  83. int (*sprom_check_crc)(const u16 *sprom, size_t size),
  84. int (*sprom_write)(struct ssb_bus *bus, const u16 *sprom))
  85. {
  86. u16 *sprom;
  87. int res = 0, err = -ENOMEM;
  88. size_t sprom_size_words = bus->sprom_size;
  89. struct ssb_freeze_context freeze;
  90. sprom = kcalloc(bus->sprom_size, sizeof(u16), GFP_KERNEL);
  91. if (!sprom)
  92. goto out;
  93. err = hex2sprom(sprom, buf, count, sprom_size_words);
  94. if (err) {
  95. err = -EINVAL;
  96. goto out_kfree;
  97. }
  98. err = sprom_check_crc(sprom, sprom_size_words);
  99. if (err) {
  100. err = -EINVAL;
  101. goto out_kfree;
  102. }
  103. /* Use interruptible locking, as the SPROM write might
  104. * be holding the lock for several seconds. So allow userspace
  105. * to cancel operation.
  106. */
  107. err = -ERESTARTSYS;
  108. if (mutex_lock_interruptible(&bus->sprom_mutex))
  109. goto out_kfree;
  110. err = ssb_devices_freeze(bus, &freeze);
  111. if (err) {
  112. pr_err("SPROM write: Could not freeze all devices\n");
  113. goto out_unlock;
  114. }
  115. res = sprom_write(bus, sprom);
  116. err = ssb_devices_thaw(&freeze);
  117. if (err)
  118. pr_err("SPROM write: Could not thaw all devices\n");
  119. out_unlock:
  120. mutex_unlock(&bus->sprom_mutex);
  121. out_kfree:
  122. kfree(sprom);
  123. out:
  124. if (res)
  125. return res;
  126. return err ? err : count;
  127. }
  128. /**
  129. * ssb_arch_register_fallback_sprom - Registers a method providing a
  130. * fallback SPROM if no SPROM is found.
  131. *
  132. * @sprom_callback: The callback function.
  133. *
  134. * With this function the architecture implementation may register a
  135. * callback handler which fills the SPROM data structure. The fallback is
  136. * only used for PCI based SSB devices, where no valid SPROM can be found
  137. * in the shadow registers.
  138. *
  139. * This function is useful for weird architectures that have a half-assed
  140. * SSB device hardwired to their PCI bus.
  141. *
  142. * Note that it does only work with PCI attached SSB devices. PCMCIA
  143. * devices currently don't use this fallback.
  144. * Architectures must provide the SPROM for native SSB devices anyway, so
  145. * the fallback also isn't used for native devices.
  146. *
  147. * This function is available for architecture code, only. So it is not
  148. * exported.
  149. */
  150. int ssb_arch_register_fallback_sprom(int (*sprom_callback)(struct ssb_bus *bus,
  151. struct ssb_sprom *out))
  152. {
  153. if (get_fallback_sprom)
  154. return -EEXIST;
  155. get_fallback_sprom = sprom_callback;
  156. return 0;
  157. }
  158. int ssb_fill_sprom_with_fallback(struct ssb_bus *bus, struct ssb_sprom *out)
  159. {
  160. if (!get_fallback_sprom)
  161. return -ENOENT;
  162. return get_fallback_sprom(bus, out);
  163. }
  164. /* https://bcm-v4.sipsolutions.net/802.11/IsSpromAvailable */
  165. bool ssb_is_sprom_available(struct ssb_bus *bus)
  166. {
  167. /* status register only exists on chipcomon rev >= 11 and we need check
  168. * for >= 31 only
  169. */
  170. /* this routine differs from specs as we do not access SPROM directly
  171. * on PCMCIA
  172. */
  173. if (bus->bustype == SSB_BUSTYPE_PCI &&
  174. bus->chipco.dev && /* can be unavailable! */
  175. bus->chipco.dev->id.revision >= 31)
  176. return bus->chipco.capabilities & SSB_CHIPCO_CAP_SPROM;
  177. return true;
  178. }