[media] v4l: kill the BKL
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / ssb / sprom.c
blob4f7cc8d13277ddb84126430923202f9fa7938ac7
1 /*
2 * Sonics Silicon Backplane
3 * Common SPROM support routines
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>
11 * Licensed under the GNU/GPL. See COPYING for details.
14 #include "ssb_private.h"
16 #include <linux/ctype.h>
17 #include <linux/slab.h>
20 static const struct ssb_sprom *fallback_sprom;
23 static int sprom2hex(const u16 *sprom, char *buf, size_t buf_len,
24 size_t sprom_size_words)
26 int i, pos = 0;
28 for (i = 0; i < sprom_size_words; i++)
29 pos += snprintf(buf + pos, buf_len - pos - 1,
30 "%04X", swab16(sprom[i]) & 0xFFFF);
31 pos += snprintf(buf + pos, buf_len - pos - 1, "\n");
33 return pos + 1;
36 static int hex2sprom(u16 *sprom, const char *dump, size_t len,
37 size_t sprom_size_words)
39 char c, tmp[5] = { 0 };
40 int err, cnt = 0;
41 unsigned long parsed;
43 /* Strip whitespace at the end. */
44 while (len) {
45 c = dump[len - 1];
46 if (!isspace(c) && c != '\0')
47 break;
48 len--;
50 /* Length must match exactly. */
51 if (len != sprom_size_words * 4)
52 return -EINVAL;
54 while (cnt < sprom_size_words) {
55 memcpy(tmp, dump, 4);
56 dump += 4;
57 err = strict_strtoul(tmp, 16, &parsed);
58 if (err)
59 return err;
60 sprom[cnt++] = swab16((u16)parsed);
63 return 0;
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))
70 u16 *sprom;
71 int err = -ENOMEM;
72 ssize_t count = 0;
73 size_t sprom_size_words = bus->sprom_size;
75 sprom = kcalloc(sprom_size_words, sizeof(u16), GFP_KERNEL);
76 if (!sprom)
77 goto out;
79 /* Use interruptible locking, as the SPROM write might
80 * be holding the lock for several seconds. So allow userspace
81 * to cancel operation. */
82 err = -ERESTARTSYS;
83 if (mutex_lock_interruptible(&bus->sprom_mutex))
84 goto out_kfree;
85 err = sprom_read(bus, sprom);
86 mutex_unlock(&bus->sprom_mutex);
88 if (!err)
89 count = sprom2hex(sprom, buf, PAGE_SIZE, sprom_size_words);
91 out_kfree:
92 kfree(sprom);
93 out:
94 return err ? err : count;
97 /* Common sprom device-attribute store-handler */
98 ssize_t ssb_attr_sprom_store(struct ssb_bus *bus,
99 const char *buf, size_t count,
100 int (*sprom_check_crc)(const u16 *sprom, size_t size),
101 int (*sprom_write)(struct ssb_bus *bus, const u16 *sprom))
103 u16 *sprom;
104 int res = 0, err = -ENOMEM;
105 size_t sprom_size_words = bus->sprom_size;
106 struct ssb_freeze_context freeze;
108 sprom = kcalloc(bus->sprom_size, sizeof(u16), GFP_KERNEL);
109 if (!sprom)
110 goto out;
111 err = hex2sprom(sprom, buf, count, sprom_size_words);
112 if (err) {
113 err = -EINVAL;
114 goto out_kfree;
116 err = sprom_check_crc(sprom, sprom_size_words);
117 if (err) {
118 err = -EINVAL;
119 goto out_kfree;
122 /* Use interruptible locking, as the SPROM write might
123 * be holding the lock for several seconds. So allow userspace
124 * to cancel operation. */
125 err = -ERESTARTSYS;
126 if (mutex_lock_interruptible(&bus->sprom_mutex))
127 goto out_kfree;
128 err = ssb_devices_freeze(bus, &freeze);
129 if (err) {
130 ssb_printk(KERN_ERR PFX "SPROM write: Could not freeze all devices\n");
131 goto out_unlock;
133 res = sprom_write(bus, sprom);
134 err = ssb_devices_thaw(&freeze);
135 if (err)
136 ssb_printk(KERN_ERR PFX "SPROM write: Could not thaw all devices\n");
137 out_unlock:
138 mutex_unlock(&bus->sprom_mutex);
139 out_kfree:
140 kfree(sprom);
141 out:
142 if (res)
143 return res;
144 return err ? err : count;
148 * ssb_arch_set_fallback_sprom - Set a fallback SPROM for use if no SPROM is found.
150 * @sprom: The SPROM data structure to register.
152 * With this function the architecture implementation may register a fallback
153 * SPROM data structure. The fallback is only used for PCI based SSB devices,
154 * where no valid SPROM can be found in the shadow registers.
156 * This function is useful for weird architectures that have a half-assed SSB device
157 * hardwired to their PCI bus.
159 * Note that it does only work with PCI attached SSB devices. PCMCIA devices currently
160 * don't use this fallback.
161 * Architectures must provide the SPROM for native SSB devices anyway,
162 * so the fallback also isn't used for native devices.
164 * This function is available for architecture code, only. So it is not exported.
166 int ssb_arch_set_fallback_sprom(const struct ssb_sprom *sprom)
168 if (fallback_sprom)
169 return -EEXIST;
170 fallback_sprom = sprom;
172 return 0;
175 const struct ssb_sprom *ssb_get_fallback_sprom(void)
177 return fallback_sprom;
180 /* http://bcm-v4.sipsolutions.net/802.11/IsSpromAvailable */
181 bool ssb_is_sprom_available(struct ssb_bus *bus)
183 /* status register only exists on chipcomon rev >= 11 and we need check
184 for >= 31 only */
185 /* this routine differs from specs as we do not access SPROM directly
186 on PCMCIA */
187 if (bus->bustype == SSB_BUSTYPE_PCI &&
188 bus->chipco.dev && /* can be unavailible! */
189 bus->chipco.dev->id.revision >= 31)
190 return bus->chipco.capabilities & SSB_CHIPCO_CAP_SPROM;
192 return true;