OMAP3 PM: CPUFreq driver for OMAP3
[linux-ginger.git] / drivers / parisc / pdc_stable.c
blob13a64bc081b6762d873204c3ddd0cbe7c35d074c
1 /*
2 * Interfaces to retrieve and set PDC Stable options (firmware)
4 * Copyright (C) 2005-2006 Thibaut VARENE <varenet@parisc-linux.org>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License, version 2, as
8 * published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 * DEV NOTE: the PDC Procedures reference states that:
21 * "A minimum of 96 bytes of Stable Storage is required. Providing more than
22 * 96 bytes of Stable Storage is optional [...]. Failure to provide the
23 * optional locations from 96 to 192 results in the loss of certain
24 * functionality during boot."
26 * Since locations between 96 and 192 are the various paths, most (if not
27 * all) PA-RISC machines should have them. Anyway, for safety reasons, the
28 * following code can deal with just 96 bytes of Stable Storage, and all
29 * sizes between 96 and 192 bytes (provided they are multiple of struct
30 * device_path size, eg: 128, 160 and 192) to provide full information.
31 * One last word: there's one path we can always count on: the primary path.
32 * Anything above 224 bytes is used for 'osdep2' OS-dependent storage area.
34 * The first OS-dependent area should always be available. Obviously, this is
35 * not true for the other one. Also bear in mind that reading/writing from/to
36 * osdep2 is much more expensive than from/to osdep1.
37 * NOTE: We do not handle the 2 bytes OS-dep area at 0x5D, nor the first
38 * 2 bytes of storage available right after OSID. That's a total of 4 bytes
39 * sacrificed: -ETOOLAZY :P
41 * The current policy wrt file permissions is:
42 * - write: root only
43 * - read: (reading triggers PDC calls) ? root only : everyone
44 * The rationale is that PDC calls could hog (DoS) the machine.
46 * TODO:
47 * - timer/fastsize write calls
50 #undef PDCS_DEBUG
51 #ifdef PDCS_DEBUG
52 #define DPRINTK(fmt, args...) printk(KERN_DEBUG fmt, ## args)
53 #else
54 #define DPRINTK(fmt, args...)
55 #endif
57 #include <linux/module.h>
58 #include <linux/init.h>
59 #include <linux/kernel.h>
60 #include <linux/string.h>
61 #include <linux/capability.h>
62 #include <linux/ctype.h>
63 #include <linux/sysfs.h>
64 #include <linux/kobject.h>
65 #include <linux/device.h>
66 #include <linux/errno.h>
67 #include <linux/spinlock.h>
69 #include <asm/pdc.h>
70 #include <asm/page.h>
71 #include <asm/uaccess.h>
72 #include <asm/hardware.h>
74 #define PDCS_VERSION "0.30"
75 #define PDCS_PREFIX "PDC Stable Storage"
77 #define PDCS_ADDR_PPRI 0x00
78 #define PDCS_ADDR_OSID 0x40
79 #define PDCS_ADDR_OSD1 0x48
80 #define PDCS_ADDR_DIAG 0x58
81 #define PDCS_ADDR_FSIZ 0x5C
82 #define PDCS_ADDR_PCON 0x60
83 #define PDCS_ADDR_PALT 0x80
84 #define PDCS_ADDR_PKBD 0xA0
85 #define PDCS_ADDR_OSD2 0xE0
87 MODULE_AUTHOR("Thibaut VARENE <varenet@parisc-linux.org>");
88 MODULE_DESCRIPTION("sysfs interface to HP PDC Stable Storage data");
89 MODULE_LICENSE("GPL");
90 MODULE_VERSION(PDCS_VERSION);
92 /* holds Stable Storage size. Initialized once and for all, no lock needed */
93 static unsigned long pdcs_size __read_mostly;
95 /* holds OS ID. Initialized once and for all, hopefully to 0x0006 */
96 static u16 pdcs_osid __read_mostly;
98 /* This struct defines what we need to deal with a parisc pdc path entry */
99 struct pdcspath_entry {
100 rwlock_t rw_lock; /* to protect path entry access */
101 short ready; /* entry record is valid if != 0 */
102 unsigned long addr; /* entry address in stable storage */
103 char *name; /* entry name */
104 struct device_path devpath; /* device path in parisc representation */
105 struct device *dev; /* corresponding device */
106 struct kobject kobj;
109 struct pdcspath_attribute {
110 struct attribute attr;
111 ssize_t (*show)(struct pdcspath_entry *entry, char *buf);
112 ssize_t (*store)(struct pdcspath_entry *entry, const char *buf, size_t count);
115 #define PDCSPATH_ENTRY(_addr, _name) \
116 struct pdcspath_entry pdcspath_entry_##_name = { \
117 .ready = 0, \
118 .addr = _addr, \
119 .name = __stringify(_name), \
122 #define PDCS_ATTR(_name, _mode, _show, _store) \
123 struct kobj_attribute pdcs_attr_##_name = { \
124 .attr = {.name = __stringify(_name), .mode = _mode}, \
125 .show = _show, \
126 .store = _store, \
129 #define PATHS_ATTR(_name, _mode, _show, _store) \
130 struct pdcspath_attribute paths_attr_##_name = { \
131 .attr = {.name = __stringify(_name), .mode = _mode}, \
132 .show = _show, \
133 .store = _store, \
136 #define to_pdcspath_attribute(_attr) container_of(_attr, struct pdcspath_attribute, attr)
137 #define to_pdcspath_entry(obj) container_of(obj, struct pdcspath_entry, kobj)
140 * pdcspath_fetch - This function populates the path entry structs.
141 * @entry: A pointer to an allocated pdcspath_entry.
143 * The general idea is that you don't read from the Stable Storage every time
144 * you access the files provided by the facilites. We store a copy of the
145 * content of the stable storage WRT various paths in these structs. We read
146 * these structs when reading the files, and we will write to these structs when
147 * writing to the files, and only then write them back to the Stable Storage.
149 * This function expects to be called with @entry->rw_lock write-hold.
151 static int
152 pdcspath_fetch(struct pdcspath_entry *entry)
154 struct device_path *devpath;
156 if (!entry)
157 return -EINVAL;
159 devpath = &entry->devpath;
161 DPRINTK("%s: fetch: 0x%p, 0x%p, addr: 0x%lx\n", __func__,
162 entry, devpath, entry->addr);
164 /* addr, devpath and count must be word aligned */
165 if (pdc_stable_read(entry->addr, devpath, sizeof(*devpath)) != PDC_OK)
166 return -EIO;
168 /* Find the matching device.
169 NOTE: hardware_path overlays with device_path, so the nice cast can
170 be used */
171 entry->dev = hwpath_to_device((struct hardware_path *)devpath);
173 entry->ready = 1;
175 DPRINTK("%s: device: 0x%p\n", __func__, entry->dev);
177 return 0;
181 * pdcspath_store - This function writes a path to stable storage.
182 * @entry: A pointer to an allocated pdcspath_entry.
184 * It can be used in two ways: either by passing it a preset devpath struct
185 * containing an already computed hardware path, or by passing it a device
186 * pointer, from which it'll find out the corresponding hardware path.
187 * For now we do not handle the case where there's an error in writing to the
188 * Stable Storage area, so you'd better not mess up the data :P
190 * This function expects to be called with @entry->rw_lock write-hold.
192 static void
193 pdcspath_store(struct pdcspath_entry *entry)
195 struct device_path *devpath;
197 BUG_ON(!entry);
199 devpath = &entry->devpath;
201 /* We expect the caller to set the ready flag to 0 if the hardware
202 path struct provided is invalid, so that we know we have to fill it.
203 First case, we don't have a preset hwpath... */
204 if (!entry->ready) {
205 /* ...but we have a device, map it */
206 BUG_ON(!entry->dev);
207 device_to_hwpath(entry->dev, (struct hardware_path *)devpath);
209 /* else, we expect the provided hwpath to be valid. */
211 DPRINTK("%s: store: 0x%p, 0x%p, addr: 0x%lx\n", __func__,
212 entry, devpath, entry->addr);
214 /* addr, devpath and count must be word aligned */
215 if (pdc_stable_write(entry->addr, devpath, sizeof(*devpath)) != PDC_OK) {
216 printk(KERN_ERR "%s: an error occured when writing to PDC.\n"
217 "It is likely that the Stable Storage data has been corrupted.\n"
218 "Please check it carefully upon next reboot.\n", __func__);
219 WARN_ON(1);
222 /* kobject is already registered */
223 entry->ready = 2;
225 DPRINTK("%s: device: 0x%p\n", __func__, entry->dev);
229 * pdcspath_hwpath_read - This function handles hardware path pretty printing.
230 * @entry: An allocated and populated pdscpath_entry struct.
231 * @buf: The output buffer to write to.
233 * We will call this function to format the output of the hwpath attribute file.
235 static ssize_t
236 pdcspath_hwpath_read(struct pdcspath_entry *entry, char *buf)
238 char *out = buf;
239 struct device_path *devpath;
240 short i;
242 if (!entry || !buf)
243 return -EINVAL;
245 read_lock(&entry->rw_lock);
246 devpath = &entry->devpath;
247 i = entry->ready;
248 read_unlock(&entry->rw_lock);
250 if (!i) /* entry is not ready */
251 return -ENODATA;
253 for (i = 0; i < 6; i++) {
254 if (devpath->bc[i] >= 128)
255 continue;
256 out += sprintf(out, "%u/", (unsigned char)devpath->bc[i]);
258 out += sprintf(out, "%u\n", (unsigned char)devpath->mod);
260 return out - buf;
264 * pdcspath_hwpath_write - This function handles hardware path modifying.
265 * @entry: An allocated and populated pdscpath_entry struct.
266 * @buf: The input buffer to read from.
267 * @count: The number of bytes to be read.
269 * We will call this function to change the current hardware path.
270 * Hardware paths are to be given '/'-delimited, without brackets.
271 * We make sure that the provided path actually maps to an existing
272 * device, BUT nothing would prevent some foolish user to set the path to some
273 * PCI bridge or even a CPU...
274 * A better work around would be to make sure we are at the end of a device tree
275 * for instance, but it would be IMHO beyond the simple scope of that driver.
276 * The aim is to provide a facility. Data correctness is left to userland.
278 static ssize_t
279 pdcspath_hwpath_write(struct pdcspath_entry *entry, const char *buf, size_t count)
281 struct hardware_path hwpath;
282 unsigned short i;
283 char in[count+1], *temp;
284 struct device *dev;
285 int ret;
287 if (!entry || !buf || !count)
288 return -EINVAL;
290 /* We'll use a local copy of buf */
291 memset(in, 0, count+1);
292 strncpy(in, buf, count);
294 /* Let's clean up the target. 0xff is a blank pattern */
295 memset(&hwpath, 0xff, sizeof(hwpath));
297 /* First, pick the mod field (the last one of the input string) */
298 if (!(temp = strrchr(in, '/')))
299 return -EINVAL;
301 hwpath.mod = simple_strtoul(temp+1, NULL, 10);
302 in[temp-in] = '\0'; /* truncate the remaining string. just precaution */
303 DPRINTK("%s: mod: %d\n", __func__, hwpath.mod);
305 /* Then, loop for each delimiter, making sure we don't have too many.
306 we write the bc fields in a down-top way. No matter what, we stop
307 before writing the last field. If there are too many fields anyway,
308 then the user is a moron and it'll be caught up later when we'll
309 check the consistency of the given hwpath. */
310 for (i=5; ((temp = strrchr(in, '/'))) && (temp-in > 0) && (likely(i)); i--) {
311 hwpath.bc[i] = simple_strtoul(temp+1, NULL, 10);
312 in[temp-in] = '\0';
313 DPRINTK("%s: bc[%d]: %d\n", __func__, i, hwpath.bc[i]);
316 /* Store the final field */
317 hwpath.bc[i] = simple_strtoul(in, NULL, 10);
318 DPRINTK("%s: bc[%d]: %d\n", __func__, i, hwpath.bc[i]);
320 /* Now we check that the user isn't trying to lure us */
321 if (!(dev = hwpath_to_device((struct hardware_path *)&hwpath))) {
322 printk(KERN_WARNING "%s: attempt to set invalid \"%s\" "
323 "hardware path: %s\n", __func__, entry->name, buf);
324 return -EINVAL;
327 /* So far so good, let's get in deep */
328 write_lock(&entry->rw_lock);
329 entry->ready = 0;
330 entry->dev = dev;
332 /* Now, dive in. Write back to the hardware */
333 pdcspath_store(entry);
335 /* Update the symlink to the real device */
336 sysfs_remove_link(&entry->kobj, "device");
337 ret = sysfs_create_link(&entry->kobj, &entry->dev->kobj, "device");
338 WARN_ON(ret);
340 write_unlock(&entry->rw_lock);
342 printk(KERN_INFO PDCS_PREFIX ": changed \"%s\" path to \"%s\"\n",
343 entry->name, buf);
345 return count;
349 * pdcspath_layer_read - Extended layer (eg. SCSI ids) pretty printing.
350 * @entry: An allocated and populated pdscpath_entry struct.
351 * @buf: The output buffer to write to.
353 * We will call this function to format the output of the layer attribute file.
355 static ssize_t
356 pdcspath_layer_read(struct pdcspath_entry *entry, char *buf)
358 char *out = buf;
359 struct device_path *devpath;
360 short i;
362 if (!entry || !buf)
363 return -EINVAL;
365 read_lock(&entry->rw_lock);
366 devpath = &entry->devpath;
367 i = entry->ready;
368 read_unlock(&entry->rw_lock);
370 if (!i) /* entry is not ready */
371 return -ENODATA;
373 for (i = 0; i < 6 && devpath->layers[i]; i++)
374 out += sprintf(out, "%u ", devpath->layers[i]);
376 out += sprintf(out, "\n");
378 return out - buf;
382 * pdcspath_layer_write - This function handles extended layer modifying.
383 * @entry: An allocated and populated pdscpath_entry struct.
384 * @buf: The input buffer to read from.
385 * @count: The number of bytes to be read.
387 * We will call this function to change the current layer value.
388 * Layers are to be given '.'-delimited, without brackets.
389 * XXX beware we are far less checky WRT input data provided than for hwpath.
390 * Potential harm can be done, since there's no way to check the validity of
391 * the layer fields.
393 static ssize_t
394 pdcspath_layer_write(struct pdcspath_entry *entry, const char *buf, size_t count)
396 unsigned int layers[6]; /* device-specific info (ctlr#, unit#, ...) */
397 unsigned short i;
398 char in[count+1], *temp;
400 if (!entry || !buf || !count)
401 return -EINVAL;
403 /* We'll use a local copy of buf */
404 memset(in, 0, count+1);
405 strncpy(in, buf, count);
407 /* Let's clean up the target. 0 is a blank pattern */
408 memset(&layers, 0, sizeof(layers));
410 /* First, pick the first layer */
411 if (unlikely(!isdigit(*in)))
412 return -EINVAL;
413 layers[0] = simple_strtoul(in, NULL, 10);
414 DPRINTK("%s: layer[0]: %d\n", __func__, layers[0]);
416 temp = in;
417 for (i=1; ((temp = strchr(temp, '.'))) && (likely(i<6)); i++) {
418 if (unlikely(!isdigit(*(++temp))))
419 return -EINVAL;
420 layers[i] = simple_strtoul(temp, NULL, 10);
421 DPRINTK("%s: layer[%d]: %d\n", __func__, i, layers[i]);
424 /* So far so good, let's get in deep */
425 write_lock(&entry->rw_lock);
427 /* First, overwrite the current layers with the new ones, not touching
428 the hardware path. */
429 memcpy(&entry->devpath.layers, &layers, sizeof(layers));
431 /* Now, dive in. Write back to the hardware */
432 pdcspath_store(entry);
433 write_unlock(&entry->rw_lock);
435 printk(KERN_INFO PDCS_PREFIX ": changed \"%s\" layers to \"%s\"\n",
436 entry->name, buf);
438 return count;
442 * pdcspath_attr_show - Generic read function call wrapper.
443 * @kobj: The kobject to get info from.
444 * @attr: The attribute looked upon.
445 * @buf: The output buffer.
447 static ssize_t
448 pdcspath_attr_show(struct kobject *kobj, struct attribute *attr, char *buf)
450 struct pdcspath_entry *entry = to_pdcspath_entry(kobj);
451 struct pdcspath_attribute *pdcs_attr = to_pdcspath_attribute(attr);
452 ssize_t ret = 0;
454 if (pdcs_attr->show)
455 ret = pdcs_attr->show(entry, buf);
457 return ret;
461 * pdcspath_attr_store - Generic write function call wrapper.
462 * @kobj: The kobject to write info to.
463 * @attr: The attribute to be modified.
464 * @buf: The input buffer.
465 * @count: The size of the buffer.
467 static ssize_t
468 pdcspath_attr_store(struct kobject *kobj, struct attribute *attr,
469 const char *buf, size_t count)
471 struct pdcspath_entry *entry = to_pdcspath_entry(kobj);
472 struct pdcspath_attribute *pdcs_attr = to_pdcspath_attribute(attr);
473 ssize_t ret = 0;
475 if (!capable(CAP_SYS_ADMIN))
476 return -EACCES;
478 if (pdcs_attr->store)
479 ret = pdcs_attr->store(entry, buf, count);
481 return ret;
484 static struct sysfs_ops pdcspath_attr_ops = {
485 .show = pdcspath_attr_show,
486 .store = pdcspath_attr_store,
489 /* These are the two attributes of any PDC path. */
490 static PATHS_ATTR(hwpath, 0644, pdcspath_hwpath_read, pdcspath_hwpath_write);
491 static PATHS_ATTR(layer, 0644, pdcspath_layer_read, pdcspath_layer_write);
493 static struct attribute *paths_subsys_attrs[] = {
494 &paths_attr_hwpath.attr,
495 &paths_attr_layer.attr,
496 NULL,
499 /* Specific kobject type for our PDC paths */
500 static struct kobj_type ktype_pdcspath = {
501 .sysfs_ops = &pdcspath_attr_ops,
502 .default_attrs = paths_subsys_attrs,
505 /* We hard define the 4 types of path we expect to find */
506 static PDCSPATH_ENTRY(PDCS_ADDR_PPRI, primary);
507 static PDCSPATH_ENTRY(PDCS_ADDR_PCON, console);
508 static PDCSPATH_ENTRY(PDCS_ADDR_PALT, alternative);
509 static PDCSPATH_ENTRY(PDCS_ADDR_PKBD, keyboard);
511 /* An array containing all PDC paths we will deal with */
512 static struct pdcspath_entry *pdcspath_entries[] = {
513 &pdcspath_entry_primary,
514 &pdcspath_entry_alternative,
515 &pdcspath_entry_console,
516 &pdcspath_entry_keyboard,
517 NULL,
521 /* For more insight of what's going on here, refer to PDC Procedures doc,
522 * Section PDC_STABLE */
525 * pdcs_size_read - Stable Storage size output.
526 * @buf: The output buffer to write to.
528 static ssize_t pdcs_size_read(struct kobject *kobj,
529 struct kobj_attribute *attr,
530 char *buf)
532 char *out = buf;
534 if (!buf)
535 return -EINVAL;
537 /* show the size of the stable storage */
538 out += sprintf(out, "%ld\n", pdcs_size);
540 return out - buf;
544 * pdcs_auto_read - Stable Storage autoboot/search flag output.
545 * @buf: The output buffer to write to.
546 * @knob: The PF_AUTOBOOT or PF_AUTOSEARCH flag
548 static ssize_t pdcs_auto_read(struct kobject *kobj,
549 struct kobj_attribute *attr,
550 char *buf, int knob)
552 char *out = buf;
553 struct pdcspath_entry *pathentry;
555 if (!buf)
556 return -EINVAL;
558 /* Current flags are stored in primary boot path entry */
559 pathentry = &pdcspath_entry_primary;
561 read_lock(&pathentry->rw_lock);
562 out += sprintf(out, "%s\n", (pathentry->devpath.flags & knob) ?
563 "On" : "Off");
564 read_unlock(&pathentry->rw_lock);
566 return out - buf;
570 * pdcs_autoboot_read - Stable Storage autoboot flag output.
571 * @buf: The output buffer to write to.
573 static ssize_t pdcs_autoboot_read(struct kobject *kobj,
574 struct kobj_attribute *attr, char *buf)
576 return pdcs_auto_read(kobj, attr, buf, PF_AUTOBOOT);
580 * pdcs_autosearch_read - Stable Storage autoboot flag output.
581 * @buf: The output buffer to write to.
583 static ssize_t pdcs_autosearch_read(struct kobject *kobj,
584 struct kobj_attribute *attr, char *buf)
586 return pdcs_auto_read(kobj, attr, buf, PF_AUTOSEARCH);
590 * pdcs_timer_read - Stable Storage timer count output (in seconds).
591 * @buf: The output buffer to write to.
593 * The value of the timer field correponds to a number of seconds in powers of 2.
595 static ssize_t pdcs_timer_read(struct kobject *kobj,
596 struct kobj_attribute *attr, char *buf)
598 char *out = buf;
599 struct pdcspath_entry *pathentry;
601 if (!buf)
602 return -EINVAL;
604 /* Current flags are stored in primary boot path entry */
605 pathentry = &pdcspath_entry_primary;
607 /* print the timer value in seconds */
608 read_lock(&pathentry->rw_lock);
609 out += sprintf(out, "%u\n", (pathentry->devpath.flags & PF_TIMER) ?
610 (1 << (pathentry->devpath.flags & PF_TIMER)) : 0);
611 read_unlock(&pathentry->rw_lock);
613 return out - buf;
617 * pdcs_osid_read - Stable Storage OS ID register output.
618 * @buf: The output buffer to write to.
620 static ssize_t pdcs_osid_read(struct kobject *kobj,
621 struct kobj_attribute *attr, char *buf)
623 char *out = buf;
625 if (!buf)
626 return -EINVAL;
628 out += sprintf(out, "%s dependent data (0x%.4x)\n",
629 os_id_to_string(pdcs_osid), pdcs_osid);
631 return out - buf;
635 * pdcs_osdep1_read - Stable Storage OS-Dependent data area 1 output.
636 * @buf: The output buffer to write to.
638 * This can hold 16 bytes of OS-Dependent data.
640 static ssize_t pdcs_osdep1_read(struct kobject *kobj,
641 struct kobj_attribute *attr, char *buf)
643 char *out = buf;
644 u32 result[4];
646 if (!buf)
647 return -EINVAL;
649 if (pdc_stable_read(PDCS_ADDR_OSD1, &result, sizeof(result)) != PDC_OK)
650 return -EIO;
652 out += sprintf(out, "0x%.8x\n", result[0]);
653 out += sprintf(out, "0x%.8x\n", result[1]);
654 out += sprintf(out, "0x%.8x\n", result[2]);
655 out += sprintf(out, "0x%.8x\n", result[3]);
657 return out - buf;
661 * pdcs_diagnostic_read - Stable Storage Diagnostic register output.
662 * @buf: The output buffer to write to.
664 * I have NFC how to interpret the content of that register ;-).
666 static ssize_t pdcs_diagnostic_read(struct kobject *kobj,
667 struct kobj_attribute *attr, char *buf)
669 char *out = buf;
670 u32 result;
672 if (!buf)
673 return -EINVAL;
675 /* get diagnostic */
676 if (pdc_stable_read(PDCS_ADDR_DIAG, &result, sizeof(result)) != PDC_OK)
677 return -EIO;
679 out += sprintf(out, "0x%.4x\n", (result >> 16));
681 return out - buf;
685 * pdcs_fastsize_read - Stable Storage FastSize register output.
686 * @buf: The output buffer to write to.
688 * This register holds the amount of system RAM to be tested during boot sequence.
690 static ssize_t pdcs_fastsize_read(struct kobject *kobj,
691 struct kobj_attribute *attr, char *buf)
693 char *out = buf;
694 u32 result;
696 if (!buf)
697 return -EINVAL;
699 /* get fast-size */
700 if (pdc_stable_read(PDCS_ADDR_FSIZ, &result, sizeof(result)) != PDC_OK)
701 return -EIO;
703 if ((result & 0x0F) < 0x0E)
704 out += sprintf(out, "%d kB", (1<<(result & 0x0F))*256);
705 else
706 out += sprintf(out, "All");
707 out += sprintf(out, "\n");
709 return out - buf;
713 * pdcs_osdep2_read - Stable Storage OS-Dependent data area 2 output.
714 * @buf: The output buffer to write to.
716 * This can hold pdcs_size - 224 bytes of OS-Dependent data, when available.
718 static ssize_t pdcs_osdep2_read(struct kobject *kobj,
719 struct kobj_attribute *attr, char *buf)
721 char *out = buf;
722 unsigned long size;
723 unsigned short i;
724 u32 result;
726 if (unlikely(pdcs_size <= 224))
727 return -ENODATA;
729 size = pdcs_size - 224;
731 if (!buf)
732 return -EINVAL;
734 for (i=0; i<size; i+=4) {
735 if (unlikely(pdc_stable_read(PDCS_ADDR_OSD2 + i, &result,
736 sizeof(result)) != PDC_OK))
737 return -EIO;
738 out += sprintf(out, "0x%.8x\n", result);
741 return out - buf;
745 * pdcs_auto_write - This function handles autoboot/search flag modifying.
746 * @buf: The input buffer to read from.
747 * @count: The number of bytes to be read.
748 * @knob: The PF_AUTOBOOT or PF_AUTOSEARCH flag
750 * We will call this function to change the current autoboot flag.
751 * We expect a precise syntax:
752 * \"n\" (n == 0 or 1) to toggle AutoBoot Off or On
754 static ssize_t pdcs_auto_write(struct kobject *kobj,
755 struct kobj_attribute *attr, const char *buf,
756 size_t count, int knob)
758 struct pdcspath_entry *pathentry;
759 unsigned char flags;
760 char in[count+1], *temp;
761 char c;
763 if (!capable(CAP_SYS_ADMIN))
764 return -EACCES;
766 if (!buf || !count)
767 return -EINVAL;
769 /* We'll use a local copy of buf */
770 memset(in, 0, count+1);
771 strncpy(in, buf, count);
773 /* Current flags are stored in primary boot path entry */
774 pathentry = &pdcspath_entry_primary;
776 /* Be nice to the existing flag record */
777 read_lock(&pathentry->rw_lock);
778 flags = pathentry->devpath.flags;
779 read_unlock(&pathentry->rw_lock);
781 DPRINTK("%s: flags before: 0x%X\n", __func__, flags);
783 temp = in;
785 while (*temp && isspace(*temp))
786 temp++;
788 c = *temp++ - '0';
789 if ((c != 0) && (c != 1))
790 goto parse_error;
791 if (c == 0)
792 flags &= ~knob;
793 else
794 flags |= knob;
796 DPRINTK("%s: flags after: 0x%X\n", __func__, flags);
798 /* So far so good, let's get in deep */
799 write_lock(&pathentry->rw_lock);
801 /* Change the path entry flags first */
802 pathentry->devpath.flags = flags;
804 /* Now, dive in. Write back to the hardware */
805 pdcspath_store(pathentry);
806 write_unlock(&pathentry->rw_lock);
808 printk(KERN_INFO PDCS_PREFIX ": changed \"%s\" to \"%s\"\n",
809 (knob & PF_AUTOBOOT) ? "autoboot" : "autosearch",
810 (flags & knob) ? "On" : "Off");
812 return count;
814 parse_error:
815 printk(KERN_WARNING "%s: Parse error: expect \"n\" (n == 0 or 1)\n", __func__);
816 return -EINVAL;
820 * pdcs_autoboot_write - This function handles autoboot flag modifying.
821 * @buf: The input buffer to read from.
822 * @count: The number of bytes to be read.
824 * We will call this function to change the current boot flags.
825 * We expect a precise syntax:
826 * \"n\" (n == 0 or 1) to toggle AutoSearch Off or On
828 static ssize_t pdcs_autoboot_write(struct kobject *kobj,
829 struct kobj_attribute *attr,
830 const char *buf, size_t count)
832 return pdcs_auto_write(kobj, attr, buf, count, PF_AUTOBOOT);
836 * pdcs_autosearch_write - This function handles autosearch flag modifying.
837 * @buf: The input buffer to read from.
838 * @count: The number of bytes to be read.
840 * We will call this function to change the current boot flags.
841 * We expect a precise syntax:
842 * \"n\" (n == 0 or 1) to toggle AutoSearch Off or On
844 static ssize_t pdcs_autosearch_write(struct kobject *kobj,
845 struct kobj_attribute *attr,
846 const char *buf, size_t count)
848 return pdcs_auto_write(kobj, attr, buf, count, PF_AUTOSEARCH);
852 * pdcs_osdep1_write - Stable Storage OS-Dependent data area 1 input.
853 * @buf: The input buffer to read from.
854 * @count: The number of bytes to be read.
856 * This can store 16 bytes of OS-Dependent data. We use a byte-by-byte
857 * write approach. It's up to userspace to deal with it when constructing
858 * its input buffer.
860 static ssize_t pdcs_osdep1_write(struct kobject *kobj,
861 struct kobj_attribute *attr,
862 const char *buf, size_t count)
864 u8 in[16];
866 if (!capable(CAP_SYS_ADMIN))
867 return -EACCES;
869 if (!buf || !count)
870 return -EINVAL;
872 if (unlikely(pdcs_osid != OS_ID_LINUX))
873 return -EPERM;
875 if (count > 16)
876 return -EMSGSIZE;
878 /* We'll use a local copy of buf */
879 memset(in, 0, 16);
880 memcpy(in, buf, count);
882 if (pdc_stable_write(PDCS_ADDR_OSD1, &in, sizeof(in)) != PDC_OK)
883 return -EIO;
885 return count;
889 * pdcs_osdep2_write - Stable Storage OS-Dependent data area 2 input.
890 * @buf: The input buffer to read from.
891 * @count: The number of bytes to be read.
893 * This can store pdcs_size - 224 bytes of OS-Dependent data. We use a
894 * byte-by-byte write approach. It's up to userspace to deal with it when
895 * constructing its input buffer.
897 static ssize_t pdcs_osdep2_write(struct kobject *kobj,
898 struct kobj_attribute *attr,
899 const char *buf, size_t count)
901 unsigned long size;
902 unsigned short i;
903 u8 in[4];
905 if (!capable(CAP_SYS_ADMIN))
906 return -EACCES;
908 if (!buf || !count)
909 return -EINVAL;
911 if (unlikely(pdcs_size <= 224))
912 return -ENOSYS;
914 if (unlikely(pdcs_osid != OS_ID_LINUX))
915 return -EPERM;
917 size = pdcs_size - 224;
919 if (count > size)
920 return -EMSGSIZE;
922 /* We'll use a local copy of buf */
924 for (i=0; i<count; i+=4) {
925 memset(in, 0, 4);
926 memcpy(in, buf+i, (count-i < 4) ? count-i : 4);
927 if (unlikely(pdc_stable_write(PDCS_ADDR_OSD2 + i, &in,
928 sizeof(in)) != PDC_OK))
929 return -EIO;
932 return count;
935 /* The remaining attributes. */
936 static PDCS_ATTR(size, 0444, pdcs_size_read, NULL);
937 static PDCS_ATTR(autoboot, 0644, pdcs_autoboot_read, pdcs_autoboot_write);
938 static PDCS_ATTR(autosearch, 0644, pdcs_autosearch_read, pdcs_autosearch_write);
939 static PDCS_ATTR(timer, 0444, pdcs_timer_read, NULL);
940 static PDCS_ATTR(osid, 0444, pdcs_osid_read, NULL);
941 static PDCS_ATTR(osdep1, 0600, pdcs_osdep1_read, pdcs_osdep1_write);
942 static PDCS_ATTR(diagnostic, 0400, pdcs_diagnostic_read, NULL);
943 static PDCS_ATTR(fastsize, 0400, pdcs_fastsize_read, NULL);
944 static PDCS_ATTR(osdep2, 0600, pdcs_osdep2_read, pdcs_osdep2_write);
946 static struct attribute *pdcs_subsys_attrs[] = {
947 &pdcs_attr_size.attr,
948 &pdcs_attr_autoboot.attr,
949 &pdcs_attr_autosearch.attr,
950 &pdcs_attr_timer.attr,
951 &pdcs_attr_osid.attr,
952 &pdcs_attr_osdep1.attr,
953 &pdcs_attr_diagnostic.attr,
954 &pdcs_attr_fastsize.attr,
955 &pdcs_attr_osdep2.attr,
956 NULL,
959 static struct attribute_group pdcs_attr_group = {
960 .attrs = pdcs_subsys_attrs,
963 static struct kobject *stable_kobj;
964 static struct kset *paths_kset;
967 * pdcs_register_pathentries - Prepares path entries kobjects for sysfs usage.
969 * It creates kobjects corresponding to each path entry with nice sysfs
970 * links to the real device. This is where the magic takes place: when
971 * registering the subsystem attributes during module init, each kobject hereby
972 * created will show in the sysfs tree as a folder containing files as defined
973 * by path_subsys_attr[].
975 static inline int __init
976 pdcs_register_pathentries(void)
978 unsigned short i;
979 struct pdcspath_entry *entry;
980 int err;
982 /* Initialize the entries rw_lock before anything else */
983 for (i = 0; (entry = pdcspath_entries[i]); i++)
984 rwlock_init(&entry->rw_lock);
986 for (i = 0; (entry = pdcspath_entries[i]); i++) {
987 write_lock(&entry->rw_lock);
988 err = pdcspath_fetch(entry);
989 write_unlock(&entry->rw_lock);
991 if (err < 0)
992 continue;
994 entry->kobj.kset = paths_kset;
995 err = kobject_init_and_add(&entry->kobj, &ktype_pdcspath, NULL,
996 "%s", entry->name);
997 if (err)
998 return err;
1000 /* kobject is now registered */
1001 write_lock(&entry->rw_lock);
1002 entry->ready = 2;
1004 /* Add a nice symlink to the real device */
1005 if (entry->dev) {
1006 err = sysfs_create_link(&entry->kobj, &entry->dev->kobj, "device");
1007 WARN_ON(err);
1010 write_unlock(&entry->rw_lock);
1011 kobject_uevent(&entry->kobj, KOBJ_ADD);
1014 return 0;
1018 * pdcs_unregister_pathentries - Routine called when unregistering the module.
1020 static inline void
1021 pdcs_unregister_pathentries(void)
1023 unsigned short i;
1024 struct pdcspath_entry *entry;
1026 for (i = 0; (entry = pdcspath_entries[i]); i++) {
1027 read_lock(&entry->rw_lock);
1028 if (entry->ready >= 2)
1029 kobject_put(&entry->kobj);
1030 read_unlock(&entry->rw_lock);
1035 * For now we register the stable subsystem with the firmware subsystem
1036 * and the paths subsystem with the stable subsystem
1038 static int __init
1039 pdc_stable_init(void)
1041 int rc = 0, error = 0;
1042 u32 result;
1044 /* find the size of the stable storage */
1045 if (pdc_stable_get_size(&pdcs_size) != PDC_OK)
1046 return -ENODEV;
1048 /* make sure we have enough data */
1049 if (pdcs_size < 96)
1050 return -ENODATA;
1052 printk(KERN_INFO PDCS_PREFIX " facility v%s\n", PDCS_VERSION);
1054 /* get OSID */
1055 if (pdc_stable_read(PDCS_ADDR_OSID, &result, sizeof(result)) != PDC_OK)
1056 return -EIO;
1058 /* the actual result is 16 bits away */
1059 pdcs_osid = (u16)(result >> 16);
1061 /* For now we'll register the directory at /sys/firmware/stable */
1062 stable_kobj = kobject_create_and_add("stable", firmware_kobj);
1063 if (!stable_kobj) {
1064 rc = -ENOMEM;
1065 goto fail_firmreg;
1068 /* Don't forget the root entries */
1069 error = sysfs_create_group(stable_kobj, &pdcs_attr_group);
1071 /* register the paths kset as a child of the stable kset */
1072 paths_kset = kset_create_and_add("paths", NULL, stable_kobj);
1073 if (!paths_kset) {
1074 rc = -ENOMEM;
1075 goto fail_ksetreg;
1078 /* now we create all "files" for the paths kset */
1079 if ((rc = pdcs_register_pathentries()))
1080 goto fail_pdcsreg;
1082 return rc;
1084 fail_pdcsreg:
1085 pdcs_unregister_pathentries();
1086 kset_unregister(paths_kset);
1088 fail_ksetreg:
1089 kobject_put(stable_kobj);
1091 fail_firmreg:
1092 printk(KERN_INFO PDCS_PREFIX " bailing out\n");
1093 return rc;
1096 static void __exit
1097 pdc_stable_exit(void)
1099 pdcs_unregister_pathentries();
1100 kset_unregister(paths_kset);
1101 kobject_put(stable_kobj);
1105 module_init(pdc_stable_init);
1106 module_exit(pdc_stable_exit);