sprom.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /*
  2. * Sonics Silicon Backplane
  3. * Common SPROM support routines
  4. *
  5. * Copyright (C) 2005-2008 Michael Buesch <mb@bu3sch.de>
  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. static const struct ssb_sprom *fallback_sprom;
  16. static int sprom2hex(const u16 *sprom, char *buf, size_t buf_len,
  17. size_t sprom_size_words)
  18. {
  19. int i, pos = 0;
  20. for (i = 0; i < sprom_size_words; i++)
  21. pos += snprintf(buf + pos, buf_len - pos - 1,
  22. "%04X", swab16(sprom[i]) & 0xFFFF);
  23. pos += snprintf(buf + pos, buf_len - pos - 1, "\n");
  24. return pos + 1;
  25. }
  26. static int hex2sprom(u16 *sprom, const char *dump, size_t len,
  27. size_t sprom_size_words)
  28. {
  29. char c, tmp[5] = { 0 };
  30. int err, cnt = 0;
  31. unsigned long parsed;
  32. /* Strip whitespace at the end. */
  33. while (len) {
  34. c = dump[len - 1];
  35. if (!isspace(c) && c != '\0')
  36. break;
  37. len--;
  38. }
  39. /* Length must match exactly. */
  40. if (len != sprom_size_words * 4)
  41. return -EINVAL;
  42. while (cnt < sprom_size_words) {
  43. memcpy(tmp, dump, 4);
  44. dump += 4;
  45. err = strict_strtoul(tmp, 16, &parsed);
  46. if (err)
  47. return err;
  48. sprom[cnt++] = swab16((u16)parsed);
  49. }
  50. return 0;
  51. }
  52. /* Common sprom device-attribute show-handler */
  53. ssize_t ssb_attr_sprom_show(struct ssb_bus *bus, char *buf,
  54. int (*sprom_read)(struct ssb_bus *bus, u16 *sprom))
  55. {
  56. u16 *sprom;
  57. int err = -ENOMEM;
  58. ssize_t count = 0;
  59. size_t sprom_size_words = bus->sprom_size;
  60. sprom = kcalloc(sprom_size_words, sizeof(u16), GFP_KERNEL);
  61. if (!sprom)
  62. goto out;
  63. /* Use interruptible locking, as the SPROM write might
  64. * be holding the lock for several seconds. So allow userspace
  65. * to cancel operation. */
  66. err = -ERESTARTSYS;
  67. if (mutex_lock_interruptible(&bus->sprom_mutex))
  68. goto out_kfree;
  69. err = sprom_read(bus, sprom);
  70. mutex_unlock(&bus->sprom_mutex);
  71. if (!err)
  72. count = sprom2hex(sprom, buf, PAGE_SIZE, sprom_size_words);
  73. out_kfree:
  74. kfree(sprom);
  75. out:
  76. return err ? err : count;
  77. }
  78. /* Common sprom device-attribute store-handler */
  79. ssize_t ssb_attr_sprom_store(struct ssb_bus *bus,
  80. const char *buf, size_t count,
  81. int (*sprom_check_crc)(const u16 *sprom, size_t size),
  82. int (*sprom_write)(struct ssb_bus *bus, const u16 *sprom))
  83. {
  84. u16 *sprom;
  85. int res = 0, err = -ENOMEM;
  86. size_t sprom_size_words = bus->sprom_size;
  87. struct ssb_freeze_context freeze;
  88. sprom = kcalloc(bus->sprom_size, sizeof(u16), GFP_KERNEL);
  89. if (!sprom)
  90. goto out;
  91. err = hex2sprom(sprom, buf, count, sprom_size_words);
  92. if (err) {
  93. err = -EINVAL;
  94. goto out_kfree;
  95. }
  96. err = sprom_check_crc(sprom, sprom_size_words);
  97. if (err) {
  98. err = -EINVAL;
  99. goto out_kfree;
  100. }
  101. /* Use interruptible locking, as the SPROM write might
  102. * be holding the lock for several seconds. So allow userspace
  103. * to cancel operation. */
  104. err = -ERESTARTSYS;
  105. if (mutex_lock_interruptible(&bus->sprom_mutex))
  106. goto out_kfree;
  107. err = ssb_devices_freeze(bus, &freeze);
  108. if (err) {
  109. ssb_printk(KERN_ERR PFX "SPROM write: Could not freeze all devices\n");
  110. goto out_unlock;
  111. }
  112. res = sprom_write(bus, sprom);
  113. err = ssb_devices_thaw(&freeze);
  114. if (err)
  115. ssb_printk(KERN_ERR PFX "SPROM write: Could not thaw all devices\n");
  116. out_unlock:
  117. mutex_unlock(&bus->sprom_mutex);
  118. out_kfree:
  119. kfree(sprom);
  120. out:
  121. if (res)
  122. return res;
  123. return err ? err : count;
  124. }
  125. /**
  126. * ssb_arch_set_fallback_sprom - Set a fallback SPROM for use if no SPROM is found.
  127. *
  128. * @sprom: The SPROM data structure to register.
  129. *
  130. * With this function the architecture implementation may register a fallback
  131. * SPROM data structure. The fallback is only used for PCI based SSB devices,
  132. * where no valid SPROM can be found in the shadow registers.
  133. *
  134. * This function is useful for weird architectures that have a half-assed SSB device
  135. * hardwired to their PCI bus.
  136. *
  137. * Note that it does only work with PCI attached SSB devices. PCMCIA devices currently
  138. * don't use this fallback.
  139. * Architectures must provide the SPROM for native SSB devices anyway,
  140. * so the fallback also isn't used for native devices.
  141. *
  142. * This function is available for architecture code, only. So it is not exported.
  143. */
  144. int ssb_arch_set_fallback_sprom(const struct ssb_sprom *sprom)
  145. {
  146. if (fallback_sprom)
  147. return -EEXIST;
  148. fallback_sprom = sprom;
  149. return 0;
  150. }
  151. const struct ssb_sprom *ssb_get_fallback_sprom(void)
  152. {
  153. return fallback_sprom;
  154. }