Michael Buesch | e7ec2e3 | 2008-03-10 17:26:32 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Sonics Silicon Backplane |
| 3 | * Common SPROM support routines |
| 4 | * |
Michael Büsch | eb032b9 | 2011-07-04 20:50:05 +0200 | [diff] [blame] | 5 | * Copyright (C) 2005-2008 Michael Buesch <m@bues.ch> |
Michael Buesch | e7ec2e3 | 2008-03-10 17:26:32 +0100 | [diff] [blame] | 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 | |
| 14 | #include "ssb_private.h" |
| 15 | |
Michael Buesch | e33761e | 2009-11-23 20:58:06 +0100 | [diff] [blame] | 16 | #include <linux/ctype.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 17 | #include <linux/slab.h> |
Michael Buesch | e33761e | 2009-11-23 20:58:06 +0100 | [diff] [blame] | 18 | |
Michael Buesch | e7ec2e3 | 2008-03-10 17:26:32 +0100 | [diff] [blame] | 19 | |
Hauke Mehrtens | b3ae52b | 2011-05-10 23:31:30 +0200 | [diff] [blame] | 20 | static int(*get_fallback_sprom)(struct ssb_bus *dev, struct ssb_sprom *out); |
Michael Buesch | e79c1ba | 2009-02-27 16:59:05 +0100 | [diff] [blame] | 21 | |
| 22 | |
Michael Buesch | e7ec2e3 | 2008-03-10 17:26:32 +0100 | [diff] [blame] | 23 | static int sprom2hex(const u16 *sprom, char *buf, size_t buf_len, |
| 24 | size_t sprom_size_words) |
| 25 | { |
| 26 | int i, pos = 0; |
| 27 | |
| 28 | for (i = 0; i < sprom_size_words; i++) |
Takashi Iwai | ca44e47 | 2020-03-11 10:17:39 +0100 | [diff] [blame] | 29 | pos += scnprintf(buf + pos, buf_len - pos - 1, |
Michael Buesch | e7ec2e3 | 2008-03-10 17:26:32 +0100 | [diff] [blame] | 30 | "%04X", swab16(sprom[i]) & 0xFFFF); |
Takashi Iwai | ca44e47 | 2020-03-11 10:17:39 +0100 | [diff] [blame] | 31 | pos += scnprintf(buf + pos, buf_len - pos - 1, "\n"); |
Michael Buesch | e7ec2e3 | 2008-03-10 17:26:32 +0100 | [diff] [blame] | 32 | |
| 33 | return pos + 1; |
| 34 | } |
| 35 | |
| 36 | static int hex2sprom(u16 *sprom, const char *dump, size_t len, |
| 37 | size_t sprom_size_words) |
| 38 | { |
Michael Buesch | e33761e | 2009-11-23 20:58:06 +0100 | [diff] [blame] | 39 | char c, tmp[5] = { 0 }; |
| 40 | int err, cnt = 0; |
Michael Buesch | e7ec2e3 | 2008-03-10 17:26:32 +0100 | [diff] [blame] | 41 | unsigned long parsed; |
| 42 | |
Michael Buesch | e33761e | 2009-11-23 20:58:06 +0100 | [diff] [blame] | 43 | /* Strip whitespace at the end. */ |
| 44 | while (len) { |
| 45 | c = dump[len - 1]; |
| 46 | if (!isspace(c) && c != '\0') |
| 47 | break; |
| 48 | len--; |
| 49 | } |
| 50 | /* Length must match exactly. */ |
| 51 | if (len != sprom_size_words * 4) |
Michael Buesch | e7ec2e3 | 2008-03-10 17:26:32 +0100 | [diff] [blame] | 52 | return -EINVAL; |
| 53 | |
| 54 | while (cnt < sprom_size_words) { |
| 55 | memcpy(tmp, dump, 4); |
| 56 | dump += 4; |
Jingoo Han | 6b1ea4b | 2013-05-31 21:35:23 +0000 | [diff] [blame] | 57 | err = kstrtoul(tmp, 16, &parsed); |
Michael Buesch | e33761e | 2009-11-23 20:58:06 +0100 | [diff] [blame] | 58 | if (err) |
| 59 | return err; |
Michael Buesch | e7ec2e3 | 2008-03-10 17:26:32 +0100 | [diff] [blame] | 60 | sprom[cnt++] = swab16((u16)parsed); |
| 61 | } |
| 62 | |
| 63 | return 0; |
| 64 | } |
| 65 | |
| 66 | /* Common sprom device-attribute show-handler */ |
| 67 | ssize_t ssb_attr_sprom_show(struct ssb_bus *bus, char *buf, |
| 68 | int (*sprom_read)(struct ssb_bus *bus, u16 *sprom)) |
| 69 | { |
| 70 | u16 *sprom; |
| 71 | int err = -ENOMEM; |
| 72 | ssize_t count = 0; |
| 73 | size_t sprom_size_words = bus->sprom_size; |
| 74 | |
| 75 | sprom = kcalloc(sprom_size_words, sizeof(u16), GFP_KERNEL); |
| 76 | if (!sprom) |
| 77 | goto out; |
| 78 | |
| 79 | /* Use interruptible locking, as the SPROM write might |
| 80 | * be holding the lock for several seconds. So allow userspace |
John Oldman | 2aad9f8 | 2020-04-24 18:50:43 +0100 | [diff] [blame] | 81 | * to cancel operation. |
| 82 | */ |
Michael Buesch | e7ec2e3 | 2008-03-10 17:26:32 +0100 | [diff] [blame] | 83 | err = -ERESTARTSYS; |
| 84 | if (mutex_lock_interruptible(&bus->sprom_mutex)) |
| 85 | goto out_kfree; |
| 86 | err = sprom_read(bus, sprom); |
| 87 | mutex_unlock(&bus->sprom_mutex); |
| 88 | |
| 89 | if (!err) |
| 90 | count = sprom2hex(sprom, buf, PAGE_SIZE, sprom_size_words); |
| 91 | |
| 92 | out_kfree: |
| 93 | kfree(sprom); |
| 94 | out: |
| 95 | return err ? err : count; |
| 96 | } |
| 97 | |
| 98 | /* Common sprom device-attribute store-handler */ |
| 99 | ssize_t ssb_attr_sprom_store(struct ssb_bus *bus, |
| 100 | const char *buf, size_t count, |
| 101 | int (*sprom_check_crc)(const u16 *sprom, size_t size), |
| 102 | int (*sprom_write)(struct ssb_bus *bus, const u16 *sprom)) |
| 103 | { |
| 104 | u16 *sprom; |
| 105 | int res = 0, err = -ENOMEM; |
| 106 | size_t sprom_size_words = bus->sprom_size; |
Michael Buesch | 3ba6018 | 2009-11-23 20:12:13 +0100 | [diff] [blame] | 107 | struct ssb_freeze_context freeze; |
Michael Buesch | e7ec2e3 | 2008-03-10 17:26:32 +0100 | [diff] [blame] | 108 | |
| 109 | sprom = kcalloc(bus->sprom_size, sizeof(u16), GFP_KERNEL); |
| 110 | if (!sprom) |
| 111 | goto out; |
| 112 | err = hex2sprom(sprom, buf, count, sprom_size_words); |
| 113 | if (err) { |
| 114 | err = -EINVAL; |
| 115 | goto out_kfree; |
| 116 | } |
| 117 | err = sprom_check_crc(sprom, sprom_size_words); |
| 118 | if (err) { |
| 119 | err = -EINVAL; |
| 120 | goto out_kfree; |
| 121 | } |
| 122 | |
| 123 | /* Use interruptible locking, as the SPROM write might |
| 124 | * be holding the lock for several seconds. So allow userspace |
John Oldman | 2aad9f8 | 2020-04-24 18:50:43 +0100 | [diff] [blame] | 125 | * to cancel operation. |
| 126 | */ |
Michael Buesch | e7ec2e3 | 2008-03-10 17:26:32 +0100 | [diff] [blame] | 127 | err = -ERESTARTSYS; |
| 128 | if (mutex_lock_interruptible(&bus->sprom_mutex)) |
| 129 | goto out_kfree; |
Michael Buesch | 3ba6018 | 2009-11-23 20:12:13 +0100 | [diff] [blame] | 130 | err = ssb_devices_freeze(bus, &freeze); |
Michael Buesch | e7ec2e3 | 2008-03-10 17:26:32 +0100 | [diff] [blame] | 131 | if (err) { |
Michael Büsch | b8b6069 | 2018-07-31 21:56:38 +0200 | [diff] [blame] | 132 | pr_err("SPROM write: Could not freeze all devices\n"); |
Michael Buesch | e7ec2e3 | 2008-03-10 17:26:32 +0100 | [diff] [blame] | 133 | goto out_unlock; |
| 134 | } |
| 135 | res = sprom_write(bus, sprom); |
Michael Buesch | 3ba6018 | 2009-11-23 20:12:13 +0100 | [diff] [blame] | 136 | err = ssb_devices_thaw(&freeze); |
Michael Buesch | e7ec2e3 | 2008-03-10 17:26:32 +0100 | [diff] [blame] | 137 | if (err) |
Michael Büsch | b8b6069 | 2018-07-31 21:56:38 +0200 | [diff] [blame] | 138 | pr_err("SPROM write: Could not thaw all devices\n"); |
Michael Buesch | e7ec2e3 | 2008-03-10 17:26:32 +0100 | [diff] [blame] | 139 | out_unlock: |
| 140 | mutex_unlock(&bus->sprom_mutex); |
| 141 | out_kfree: |
| 142 | kfree(sprom); |
| 143 | out: |
| 144 | if (res) |
| 145 | return res; |
| 146 | return err ? err : count; |
| 147 | } |
Michael Buesch | e79c1ba | 2009-02-27 16:59:05 +0100 | [diff] [blame] | 148 | |
| 149 | /** |
Hauke Mehrtens | b3ae52b | 2011-05-10 23:31:30 +0200 | [diff] [blame] | 150 | * ssb_arch_register_fallback_sprom - Registers a method providing a |
| 151 | * fallback SPROM if no SPROM is found. |
Michael Buesch | e79c1ba | 2009-02-27 16:59:05 +0100 | [diff] [blame] | 152 | * |
Hauke Mehrtens | b3ae52b | 2011-05-10 23:31:30 +0200 | [diff] [blame] | 153 | * @sprom_callback: The callback function. |
Michael Buesch | e79c1ba | 2009-02-27 16:59:05 +0100 | [diff] [blame] | 154 | * |
Hauke Mehrtens | b3ae52b | 2011-05-10 23:31:30 +0200 | [diff] [blame] | 155 | * With this function the architecture implementation may register a |
| 156 | * callback handler which fills the SPROM data structure. The fallback is |
| 157 | * only used for PCI based SSB devices, where no valid SPROM can be found |
| 158 | * in the shadow registers. |
Michael Buesch | e79c1ba | 2009-02-27 16:59:05 +0100 | [diff] [blame] | 159 | * |
Hauke Mehrtens | b3ae52b | 2011-05-10 23:31:30 +0200 | [diff] [blame] | 160 | * This function is useful for weird architectures that have a half-assed |
| 161 | * SSB device hardwired to their PCI bus. |
Michael Buesch | e79c1ba | 2009-02-27 16:59:05 +0100 | [diff] [blame] | 162 | * |
Hauke Mehrtens | b3ae52b | 2011-05-10 23:31:30 +0200 | [diff] [blame] | 163 | * Note that it does only work with PCI attached SSB devices. PCMCIA |
| 164 | * devices currently don't use this fallback. |
| 165 | * Architectures must provide the SPROM for native SSB devices anyway, so |
| 166 | * the fallback also isn't used for native devices. |
Michael Buesch | e79c1ba | 2009-02-27 16:59:05 +0100 | [diff] [blame] | 167 | * |
Hauke Mehrtens | b3ae52b | 2011-05-10 23:31:30 +0200 | [diff] [blame] | 168 | * This function is available for architecture code, only. So it is not |
| 169 | * exported. |
Michael Buesch | e79c1ba | 2009-02-27 16:59:05 +0100 | [diff] [blame] | 170 | */ |
Hauke Mehrtens | b3ae52b | 2011-05-10 23:31:30 +0200 | [diff] [blame] | 171 | int ssb_arch_register_fallback_sprom(int (*sprom_callback)(struct ssb_bus *bus, |
| 172 | struct ssb_sprom *out)) |
Michael Buesch | e79c1ba | 2009-02-27 16:59:05 +0100 | [diff] [blame] | 173 | { |
Hauke Mehrtens | b3ae52b | 2011-05-10 23:31:30 +0200 | [diff] [blame] | 174 | if (get_fallback_sprom) |
Michael Buesch | e79c1ba | 2009-02-27 16:59:05 +0100 | [diff] [blame] | 175 | return -EEXIST; |
Hauke Mehrtens | b3ae52b | 2011-05-10 23:31:30 +0200 | [diff] [blame] | 176 | get_fallback_sprom = sprom_callback; |
Michael Buesch | e79c1ba | 2009-02-27 16:59:05 +0100 | [diff] [blame] | 177 | |
| 178 | return 0; |
| 179 | } |
| 180 | |
Hauke Mehrtens | b3ae52b | 2011-05-10 23:31:30 +0200 | [diff] [blame] | 181 | int ssb_fill_sprom_with_fallback(struct ssb_bus *bus, struct ssb_sprom *out) |
Michael Buesch | e79c1ba | 2009-02-27 16:59:05 +0100 | [diff] [blame] | 182 | { |
Hauke Mehrtens | b3ae52b | 2011-05-10 23:31:30 +0200 | [diff] [blame] | 183 | if (!get_fallback_sprom) |
| 184 | return -ENOENT; |
| 185 | |
| 186 | return get_fallback_sprom(bus, out); |
Michael Buesch | e79c1ba | 2009-02-27 16:59:05 +0100 | [diff] [blame] | 187 | } |
John W. Linville | d53cdbb9 | 2010-03-31 21:39:35 +0200 | [diff] [blame] | 188 | |
Alexander A. Klimov | 0db5bc7 | 2020-07-10 08:21:51 +0200 | [diff] [blame] | 189 | /* https://bcm-v4.sipsolutions.net/802.11/IsSpromAvailable */ |
John W. Linville | d53cdbb9 | 2010-03-31 21:39:35 +0200 | [diff] [blame] | 190 | bool ssb_is_sprom_available(struct ssb_bus *bus) |
| 191 | { |
| 192 | /* status register only exists on chipcomon rev >= 11 and we need check |
John Oldman | 2aad9f8 | 2020-04-24 18:50:43 +0100 | [diff] [blame] | 193 | * for >= 31 only |
| 194 | */ |
John W. Linville | d53cdbb9 | 2010-03-31 21:39:35 +0200 | [diff] [blame] | 195 | /* this routine differs from specs as we do not access SPROM directly |
John Oldman | 2aad9f8 | 2020-04-24 18:50:43 +0100 | [diff] [blame] | 196 | * on PCMCIA |
| 197 | */ |
John W. Linville | d53cdbb9 | 2010-03-31 21:39:35 +0200 | [diff] [blame] | 198 | if (bus->bustype == SSB_BUSTYPE_PCI && |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 199 | bus->chipco.dev && /* can be unavailable! */ |
John W. Linville | d53cdbb9 | 2010-03-31 21:39:35 +0200 | [diff] [blame] | 200 | bus->chipco.dev->id.revision >= 31) |
| 201 | return bus->chipco.capabilities & SSB_CHIPCO_CAP_SPROM; |
| 202 | |
| 203 | return true; |
| 204 | } |