sprom.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /*
  2. * Sonics Silicon Backplane
  3. * Common SPROM support routines
  4. *
  5. * Copyright (C) 2005-2008 Michael Buesch <m@bues.ch>
  6. * Copyright (C) 2005 Martin Langer <martin-langer@gmx.de>
  7. * Copyright (C) 2005 Stefano Brivio <st3@riseup.net>
  8. * Copyright (C) 2005 Danny van Dyk <kugelfang@gentoo.org>
  9. * Copyright (C) 2005 Andreas Jaggi <andreas.jaggi@waterwave.ch>
  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 += snprintf(buf + pos, buf_len - pos - 1,
  23. "%04X", swab16(sprom[i]) & 0xFFFF);
  24. pos += snprintf(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. err = -ERESTARTSYS;
  68. if (mutex_lock_interruptible(&bus->sprom_mutex))
  69. goto out_kfree;
  70. err = sprom_read(bus, sprom);
  71. mutex_unlock(&bus->sprom_mutex);
  72. if (!err)
  73. count = sprom2hex(sprom, buf, PAGE_SIZE, sprom_size_words);
  74. out_kfree:
  75. kfree(sprom);
  76. out:
  77. return err ? err : count;
  78. }
  79. /* Common sprom device-attribute store-handler */
  80. ssize_t ssb_attr_sprom_store(struct ssb_bus *bus,
  81. const char *buf, size_t count,
  82. int (*sprom_check_crc)(const u16 *sprom, size_t size),
  83. int (*sprom_write)(struct ssb_bus *bus, const u16 *sprom))
  84. {
  85. u16 *sprom;
  86. int res = 0, err = -ENOMEM;
  87. size_t sprom_size_words = bus->sprom_size;
  88. struct ssb_freeze_context freeze;
  89. sprom = kcalloc(bus->sprom_size, sizeof(u16), GFP_KERNEL);
  90. if (!sprom)
  91. goto out;
  92. err = hex2sprom(sprom, buf, count, sprom_size_words);
  93. if (err) {
  94. err = -EINVAL;
  95. goto out_kfree;
  96. }
  97. err = sprom_check_crc(sprom, sprom_size_words);
  98. if (err) {
  99. err = -EINVAL;
  100. goto out_kfree;
  101. }
  102. /* Use interruptible locking, as the SPROM write might
  103. * be holding the lock for several seconds. So allow userspace
  104. * to cancel operation. */
  105. err = -ERESTARTSYS;
  106. if (mutex_lock_interruptible(&bus->sprom_mutex))
  107. goto out_kfree;
  108. err = ssb_devices_freeze(bus, &freeze);
  109. if (err) {
  110. ssb_err("SPROM write: Could not freeze all devices\n");
  111. goto out_unlock;
  112. }
  113. res = sprom_write(bus, sprom);
  114. err = ssb_devices_thaw(&freeze);
  115. if (err)
  116. ssb_err("SPROM write: Could not thaw all devices\n");
  117. out_unlock:
  118. mutex_unlock(&bus->sprom_mutex);
  119. out_kfree:
  120. kfree(sprom);
  121. out:
  122. if (res)
  123. return res;
  124. return err ? err : count;
  125. }
  126. /**
  127. * ssb_arch_register_fallback_sprom - Registers a method providing a
  128. * fallback SPROM if no SPROM is found.
  129. *
  130. * @sprom_callback: The callback function.
  131. *
  132. * With this function the architecture implementation may register a
  133. * callback handler which fills the SPROM data structure. The fallback is
  134. * only used for PCI based SSB devices, where no valid SPROM can be found
  135. * in the shadow registers.
  136. *
  137. * This function is useful for weird architectures that have a half-assed
  138. * SSB device hardwired to their PCI bus.
  139. *
  140. * Note that it does only work with PCI attached SSB devices. PCMCIA
  141. * devices currently don't use this fallback.
  142. * Architectures must provide the SPROM for native SSB devices anyway, so
  143. * the fallback also isn't used for native devices.
  144. *
  145. * This function is available for architecture code, only. So it is not
  146. * exported.
  147. */
  148. int ssb_arch_register_fallback_sprom(int (*sprom_callback)(struct ssb_bus *bus,
  149. struct ssb_sprom *out))
  150. {
  151. if (get_fallback_sprom)
  152. return -EEXIST;
  153. get_fallback_sprom = sprom_callback;
  154. return 0;
  155. }
  156. int ssb_fill_sprom_with_fallback(struct ssb_bus *bus, struct ssb_sprom *out)
  157. {
  158. if (!get_fallback_sprom)
  159. return -ENOENT;
  160. return get_fallback_sprom(bus, out);
  161. }
  162. /* http://bcm-v4.sipsolutions.net/802.11/IsSpromAvailable */
  163. bool ssb_is_sprom_available(struct ssb_bus *bus)
  164. {
  165. /* status register only exists on chipcomon rev >= 11 and we need check
  166. for >= 31 only */
  167. /* this routine differs from specs as we do not access SPROM directly
  168. on PCMCIA */
  169. if (bus->bustype == SSB_BUSTYPE_PCI &&
  170. bus->chipco.dev && /* can be unavailable! */
  171. bus->chipco.dev->id.revision >= 31)
  172. return bus->chipco.capabilities & SSB_CHIPCO_CAP_SPROM;
  173. return true;
  174. }