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"
17 static int sprom2hex(const u16 *sprom, char *buf, size_t buf_len,
18 size_t sprom_size_words)
22 for (i = 0; i < sprom_size_words; i++)
23 pos += snprintf(buf + pos, buf_len - pos - 1,
24 "%04X", swab16(sprom[i]) & 0xFFFF);
25 pos += snprintf(buf + pos, buf_len - pos - 1, "\n");
30 static int hex2sprom(u16 *sprom, const char *dump, size_t len,
31 size_t sprom_size_words)
37 if (len < sprom_size_words * 2)
40 while (cnt < sprom_size_words) {
43 parsed = simple_strtoul(tmp, NULL, 16);
44 sprom[cnt++] = swab16((u16)parsed);
50 /* Common sprom device-attribute show-handler */
51 ssize_t ssb_attr_sprom_show(struct ssb_bus *bus, char *buf,
52 int (*sprom_read)(struct ssb_bus *bus, u16 *sprom))
57 size_t sprom_size_words = bus->sprom_size;
59 sprom = kcalloc(sprom_size_words, sizeof(u16), GFP_KERNEL);
63 /* Use interruptible locking, as the SPROM write might
64 * be holding the lock for several seconds. So allow userspace
65 * to cancel operation. */
67 if (mutex_lock_interruptible(&bus->sprom_mutex))
69 err = sprom_read(bus, sprom);
70 mutex_unlock(&bus->sprom_mutex);
73 count = sprom2hex(sprom, buf, PAGE_SIZE, sprom_size_words);
78 return err ? err : count;
81 /* Common sprom device-attribute store-handler */
82 ssize_t ssb_attr_sprom_store(struct ssb_bus *bus,
83 const char *buf, size_t count,
84 int (*sprom_check_crc)(const u16 *sprom, size_t size),
85 int (*sprom_write)(struct ssb_bus *bus, const u16 *sprom))
88 int res = 0, err = -ENOMEM;
89 size_t sprom_size_words = bus->sprom_size;
91 sprom = kcalloc(bus->sprom_size, sizeof(u16), GFP_KERNEL);
94 err = hex2sprom(sprom, buf, count, sprom_size_words);
99 err = sprom_check_crc(sprom, sprom_size_words);
105 /* Use interruptible locking, as the SPROM write might
106 * be holding the lock for several seconds. So allow userspace
107 * to cancel operation. */
109 if (mutex_lock_interruptible(&bus->sprom_mutex))
111 err = ssb_devices_freeze(bus);
112 if (err == -EOPNOTSUPP) {
113 ssb_printk(KERN_ERR PFX "SPROM write: Could not freeze devices. "
114 "No suspend support. Is CONFIG_PM enabled?\n");
118 ssb_printk(KERN_ERR PFX "SPROM write: Could not freeze all devices\n");
121 res = sprom_write(bus, sprom);
122 err = ssb_devices_thaw(bus);
124 ssb_printk(KERN_ERR PFX "SPROM write: Could not thaw all devices\n");
126 mutex_unlock(&bus->sprom_mutex);
132 return err ? err : count;