Fedora kernel-2.6.17-1.2142_FC4 patched with stable patch-2.6.17.4-vs2.0.2-rc26.diff
[linux-2.6.git] / drivers / parisc / pdc_stable.c
1 /* 
2  *    Interfaces to retrieve and set PDC Stable options (firmware)
3  *
4  *    Copyright (C) 2005-2006 Thibaut VARENE <varenet@parisc-linux.org>
5  *
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.
9  *
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.
14  *
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
18  *
19  *
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."
25  *
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  *    The code makes no use of data above 192 bytes. One last word: there's one
32  *    path we can always count on: the primary path.
33  *
34  *    The current policy wrt file permissions is:
35  *      - write: root only
36  *      - read: (reading triggers PDC calls) ? root only : everyone
37  *    The rationale is that PDC calls could hog (DoS) the machine.
38  *
39  *      TODO:
40  *      - timer/fastsize write calls
41  */
42
43 #undef PDCS_DEBUG
44 #ifdef PDCS_DEBUG
45 #define DPRINTK(fmt, args...)   printk(KERN_DEBUG fmt, ## args)
46 #else
47 #define DPRINTK(fmt, args...)
48 #endif
49
50 #include <linux/module.h>
51 #include <linux/init.h>
52 #include <linux/kernel.h>
53 #include <linux/string.h>
54 #include <linux/capability.h>
55 #include <linux/ctype.h>
56 #include <linux/sysfs.h>
57 #include <linux/kobject.h>
58 #include <linux/device.h>
59 #include <linux/errno.h>
60 #include <linux/spinlock.h>
61
62 #include <asm/pdc.h>
63 #include <asm/page.h>
64 #include <asm/uaccess.h>
65 #include <asm/hardware.h>
66
67 #define PDCS_VERSION    "0.22"
68 #define PDCS_PREFIX     "PDC Stable Storage"
69
70 #define PDCS_ADDR_PPRI  0x00
71 #define PDCS_ADDR_OSID  0x40
72 #define PDCS_ADDR_FSIZ  0x5C
73 #define PDCS_ADDR_PCON  0x60
74 #define PDCS_ADDR_PALT  0x80
75 #define PDCS_ADDR_PKBD  0xA0
76
77 MODULE_AUTHOR("Thibaut VARENE <varenet@parisc-linux.org>");
78 MODULE_DESCRIPTION("sysfs interface to HP PDC Stable Storage data");
79 MODULE_LICENSE("GPL");
80 MODULE_VERSION(PDCS_VERSION);
81
82 /* holds Stable Storage size. Initialized once and for all, no lock needed */
83 static unsigned long pdcs_size __read_mostly;
84
85 /* This struct defines what we need to deal with a parisc pdc path entry */
86 struct pdcspath_entry {
87         rwlock_t rw_lock;               /* to protect path entry access */
88         short ready;                    /* entry record is valid if != 0 */
89         unsigned long addr;             /* entry address in stable storage */
90         char *name;                     /* entry name */
91         struct device_path devpath;     /* device path in parisc representation */
92         struct device *dev;             /* corresponding device */
93         struct kobject kobj;
94 };
95
96 struct pdcspath_attribute {
97         struct attribute attr;
98         ssize_t (*show)(struct pdcspath_entry *entry, char *buf);
99         ssize_t (*store)(struct pdcspath_entry *entry, const char *buf, size_t count);
100 };
101
102 #define PDCSPATH_ENTRY(_addr, _name) \
103 struct pdcspath_entry pdcspath_entry_##_name = { \
104         .ready = 0, \
105         .addr = _addr, \
106         .name = __stringify(_name), \
107 };
108
109 #define PDCS_ATTR(_name, _mode, _show, _store) \
110 struct subsys_attribute pdcs_attr_##_name = { \
111         .attr = {.name = __stringify(_name), .mode = _mode, .owner = THIS_MODULE}, \
112         .show = _show, \
113         .store = _store, \
114 };
115
116 #define PATHS_ATTR(_name, _mode, _show, _store) \
117 struct pdcspath_attribute paths_attr_##_name = { \
118         .attr = {.name = __stringify(_name), .mode = _mode, .owner = THIS_MODULE}, \
119         .show = _show, \
120         .store = _store, \
121 };
122
123 #define to_pdcspath_attribute(_attr) container_of(_attr, struct pdcspath_attribute, attr)
124 #define to_pdcspath_entry(obj)  container_of(obj, struct pdcspath_entry, kobj)
125
126 /**
127  * pdcspath_fetch - This function populates the path entry structs.
128  * @entry: A pointer to an allocated pdcspath_entry.
129  * 
130  * The general idea is that you don't read from the Stable Storage every time
131  * you access the files provided by the facilites. We store a copy of the
132  * content of the stable storage WRT various paths in these structs. We read
133  * these structs when reading the files, and we will write to these structs when
134  * writing to the files, and only then write them back to the Stable Storage.
135  *
136  * This function expects to be called with @entry->rw_lock write-hold.
137  */
138 static int
139 pdcspath_fetch(struct pdcspath_entry *entry)
140 {
141         struct device_path *devpath;
142
143         if (!entry)
144                 return -EINVAL;
145
146         devpath = &entry->devpath;
147         
148         DPRINTK("%s: fetch: 0x%p, 0x%p, addr: 0x%lx\n", __func__,
149                         entry, devpath, entry->addr);
150
151         /* addr, devpath and count must be word aligned */
152         if (pdc_stable_read(entry->addr, devpath, sizeof(*devpath)) != PDC_OK)
153                 return -EIO;
154                 
155         /* Find the matching device.
156            NOTE: hardware_path overlays with device_path, so the nice cast can
157            be used */
158         entry->dev = hwpath_to_device((struct hardware_path *)devpath);
159
160         entry->ready = 1;
161         
162         DPRINTK("%s: device: 0x%p\n", __func__, entry->dev);
163         
164         return 0;
165 }
166
167 /**
168  * pdcspath_store - This function writes a path to stable storage.
169  * @entry: A pointer to an allocated pdcspath_entry.
170  * 
171  * It can be used in two ways: either by passing it a preset devpath struct
172  * containing an already computed hardware path, or by passing it a device
173  * pointer, from which it'll find out the corresponding hardware path.
174  * For now we do not handle the case where there's an error in writing to the
175  * Stable Storage area, so you'd better not mess up the data :P
176  *
177  * This function expects to be called with @entry->rw_lock write-hold.
178  */
179 static void
180 pdcspath_store(struct pdcspath_entry *entry)
181 {
182         struct device_path *devpath;
183
184         BUG_ON(!entry);
185
186         devpath = &entry->devpath;
187         
188         /* We expect the caller to set the ready flag to 0 if the hardware
189            path struct provided is invalid, so that we know we have to fill it.
190            First case, we don't have a preset hwpath... */
191         if (!entry->ready) {
192                 /* ...but we have a device, map it */
193                 BUG_ON(!entry->dev);
194                 device_to_hwpath(entry->dev, (struct hardware_path *)devpath);
195         }
196         /* else, we expect the provided hwpath to be valid. */
197         
198         DPRINTK("%s: store: 0x%p, 0x%p, addr: 0x%lx\n", __func__,
199                         entry, devpath, entry->addr);
200
201         /* addr, devpath and count must be word aligned */
202         if (pdc_stable_write(entry->addr, devpath, sizeof(*devpath)) != PDC_OK) {
203                 printk(KERN_ERR "%s: an error occured when writing to PDC.\n"
204                                 "It is likely that the Stable Storage data has been corrupted.\n"
205                                 "Please check it carefully upon next reboot.\n", __func__);
206                 WARN_ON(1);
207         }
208                 
209         /* kobject is already registered */
210         entry->ready = 2;
211         
212         DPRINTK("%s: device: 0x%p\n", __func__, entry->dev);
213 }
214
215 /**
216  * pdcspath_hwpath_read - This function handles hardware path pretty printing.
217  * @entry: An allocated and populated pdscpath_entry struct.
218  * @buf: The output buffer to write to.
219  * 
220  * We will call this function to format the output of the hwpath attribute file.
221  */
222 static ssize_t
223 pdcspath_hwpath_read(struct pdcspath_entry *entry, char *buf)
224 {
225         char *out = buf;
226         struct device_path *devpath;
227         short i;
228
229         if (!entry || !buf)
230                 return -EINVAL;
231
232         read_lock(&entry->rw_lock);
233         devpath = &entry->devpath;
234         i = entry->ready;
235         read_unlock(&entry->rw_lock);
236
237         if (!i) /* entry is not ready */
238                 return -ENODATA;
239         
240         for (i = 0; i < 6; i++) {
241                 if (devpath->bc[i] >= 128)
242                         continue;
243                 out += sprintf(out, "%u/", (unsigned char)devpath->bc[i]);
244         }
245         out += sprintf(out, "%u\n", (unsigned char)devpath->mod);
246         
247         return out - buf;
248 }
249
250 /**
251  * pdcspath_hwpath_write - This function handles hardware path modifying.
252  * @entry: An allocated and populated pdscpath_entry struct.
253  * @buf: The input buffer to read from.
254  * @count: The number of bytes to be read.
255  * 
256  * We will call this function to change the current hardware path.
257  * Hardware paths are to be given '/'-delimited, without brackets.
258  * We make sure that the provided path actually maps to an existing
259  * device, BUT nothing would prevent some foolish user to set the path to some
260  * PCI bridge or even a CPU...
261  * A better work around would be to make sure we are at the end of a device tree
262  * for instance, but it would be IMHO beyond the simple scope of that driver.
263  * The aim is to provide a facility. Data correctness is left to userland.
264  */
265 static ssize_t
266 pdcspath_hwpath_write(struct pdcspath_entry *entry, const char *buf, size_t count)
267 {
268         struct hardware_path hwpath;
269         unsigned short i;
270         char in[count+1], *temp;
271         struct device *dev;
272
273         if (!entry || !buf || !count)
274                 return -EINVAL;
275
276         /* We'll use a local copy of buf */
277         memset(in, 0, count+1);
278         strncpy(in, buf, count);
279         
280         /* Let's clean up the target. 0xff is a blank pattern */
281         memset(&hwpath, 0xff, sizeof(hwpath));
282         
283         /* First, pick the mod field (the last one of the input string) */
284         if (!(temp = strrchr(in, '/')))
285                 return -EINVAL;
286                         
287         hwpath.mod = simple_strtoul(temp+1, NULL, 10);
288         in[temp-in] = '\0';     /* truncate the remaining string. just precaution */
289         DPRINTK("%s: mod: %d\n", __func__, hwpath.mod);
290         
291         /* Then, loop for each delimiter, making sure we don't have too many.
292            we write the bc fields in a down-top way. No matter what, we stop
293            before writing the last field. If there are too many fields anyway,
294            then the user is a moron and it'll be caught up later when we'll
295            check the consistency of the given hwpath. */
296         for (i=5; ((temp = strrchr(in, '/'))) && (temp-in > 0) && (likely(i)); i--) {
297                 hwpath.bc[i] = simple_strtoul(temp+1, NULL, 10);
298                 in[temp-in] = '\0';
299                 DPRINTK("%s: bc[%d]: %d\n", __func__, i, hwpath.bc[i]);
300         }
301         
302         /* Store the final field */             
303         hwpath.bc[i] = simple_strtoul(in, NULL, 10);
304         DPRINTK("%s: bc[%d]: %d\n", __func__, i, hwpath.bc[i]);
305         
306         /* Now we check that the user isn't trying to lure us */
307         if (!(dev = hwpath_to_device((struct hardware_path *)&hwpath))) {
308                 printk(KERN_WARNING "%s: attempt to set invalid \"%s\" "
309                         "hardware path: %s\n", __func__, entry->name, buf);
310                 return -EINVAL;
311         }
312         
313         /* So far so good, let's get in deep */
314         write_lock(&entry->rw_lock);
315         entry->ready = 0;
316         entry->dev = dev;
317         
318         /* Now, dive in. Write back to the hardware */
319         pdcspath_store(entry);
320         
321         /* Update the symlink to the real device */
322         sysfs_remove_link(&entry->kobj, "device");
323         sysfs_create_link(&entry->kobj, &entry->dev->kobj, "device");
324         write_unlock(&entry->rw_lock);
325         
326         printk(KERN_INFO PDCS_PREFIX ": changed \"%s\" path to \"%s\"\n",
327                 entry->name, buf);
328         
329         return count;
330 }
331
332 /**
333  * pdcspath_layer_read - Extended layer (eg. SCSI ids) pretty printing.
334  * @entry: An allocated and populated pdscpath_entry struct.
335  * @buf: The output buffer to write to.
336  * 
337  * We will call this function to format the output of the layer attribute file.
338  */
339 static ssize_t
340 pdcspath_layer_read(struct pdcspath_entry *entry, char *buf)
341 {
342         char *out = buf;
343         struct device_path *devpath;
344         short i;
345
346         if (!entry || !buf)
347                 return -EINVAL;
348         
349         read_lock(&entry->rw_lock);
350         devpath = &entry->devpath;
351         i = entry->ready;
352         read_unlock(&entry->rw_lock);
353
354         if (!i) /* entry is not ready */
355                 return -ENODATA;
356         
357         for (i = 0; devpath->layers[i] && (likely(i < 6)); i++)
358                 out += sprintf(out, "%u ", devpath->layers[i]);
359
360         out += sprintf(out, "\n");
361         
362         return out - buf;
363 }
364
365 /**
366  * pdcspath_layer_write - This function handles extended layer modifying.
367  * @entry: An allocated and populated pdscpath_entry struct.
368  * @buf: The input buffer to read from.
369  * @count: The number of bytes to be read.
370  * 
371  * We will call this function to change the current layer value.
372  * Layers are to be given '.'-delimited, without brackets.
373  * XXX beware we are far less checky WRT input data provided than for hwpath.
374  * Potential harm can be done, since there's no way to check the validity of
375  * the layer fields.
376  */
377 static ssize_t
378 pdcspath_layer_write(struct pdcspath_entry *entry, const char *buf, size_t count)
379 {
380         unsigned int layers[6]; /* device-specific info (ctlr#, unit#, ...) */
381         unsigned short i;
382         char in[count+1], *temp;
383
384         if (!entry || !buf || !count)
385                 return -EINVAL;
386
387         /* We'll use a local copy of buf */
388         memset(in, 0, count+1);
389         strncpy(in, buf, count);
390         
391         /* Let's clean up the target. 0 is a blank pattern */
392         memset(&layers, 0, sizeof(layers));
393         
394         /* First, pick the first layer */
395         if (unlikely(!isdigit(*in)))
396                 return -EINVAL;
397         layers[0] = simple_strtoul(in, NULL, 10);
398         DPRINTK("%s: layer[0]: %d\n", __func__, layers[0]);
399         
400         temp = in;
401         for (i=1; ((temp = strchr(temp, '.'))) && (likely(i<6)); i++) {
402                 if (unlikely(!isdigit(*(++temp))))
403                         return -EINVAL;
404                 layers[i] = simple_strtoul(temp, NULL, 10);
405                 DPRINTK("%s: layer[%d]: %d\n", __func__, i, layers[i]);
406         }
407                 
408         /* So far so good, let's get in deep */
409         write_lock(&entry->rw_lock);
410         
411         /* First, overwrite the current layers with the new ones, not touching
412            the hardware path. */
413         memcpy(&entry->devpath.layers, &layers, sizeof(layers));
414         
415         /* Now, dive in. Write back to the hardware */
416         pdcspath_store(entry);
417         write_unlock(&entry->rw_lock);
418         
419         printk(KERN_INFO PDCS_PREFIX ": changed \"%s\" layers to \"%s\"\n",
420                 entry->name, buf);
421         
422         return count;
423 }
424
425 /**
426  * pdcspath_attr_show - Generic read function call wrapper.
427  * @kobj: The kobject to get info from.
428  * @attr: The attribute looked upon.
429  * @buf: The output buffer.
430  */
431 static ssize_t
432 pdcspath_attr_show(struct kobject *kobj, struct attribute *attr, char *buf)
433 {
434         struct pdcspath_entry *entry = to_pdcspath_entry(kobj);
435         struct pdcspath_attribute *pdcs_attr = to_pdcspath_attribute(attr);
436         ssize_t ret = 0;
437
438         if (pdcs_attr->show)
439                 ret = pdcs_attr->show(entry, buf);
440
441         return ret;
442 }
443
444 /**
445  * pdcspath_attr_store - Generic write function call wrapper.
446  * @kobj: The kobject to write info to.
447  * @attr: The attribute to be modified.
448  * @buf: The input buffer.
449  * @count: The size of the buffer.
450  */
451 static ssize_t
452 pdcspath_attr_store(struct kobject *kobj, struct attribute *attr,
453                         const char *buf, size_t count)
454 {
455         struct pdcspath_entry *entry = to_pdcspath_entry(kobj);
456         struct pdcspath_attribute *pdcs_attr = to_pdcspath_attribute(attr);
457         ssize_t ret = 0;
458
459         if (!capable(CAP_SYS_ADMIN))
460                 return -EACCES;
461
462         if (pdcs_attr->store)
463                 ret = pdcs_attr->store(entry, buf, count);
464
465         return ret;
466 }
467
468 static struct sysfs_ops pdcspath_attr_ops = {
469         .show = pdcspath_attr_show,
470         .store = pdcspath_attr_store,
471 };
472
473 /* These are the two attributes of any PDC path. */
474 static PATHS_ATTR(hwpath, 0644, pdcspath_hwpath_read, pdcspath_hwpath_write);
475 static PATHS_ATTR(layer, 0644, pdcspath_layer_read, pdcspath_layer_write);
476
477 static struct attribute *paths_subsys_attrs[] = {
478         &paths_attr_hwpath.attr,
479         &paths_attr_layer.attr,
480         NULL,
481 };
482
483 /* Specific kobject type for our PDC paths */
484 static struct kobj_type ktype_pdcspath = {
485         .sysfs_ops = &pdcspath_attr_ops,
486         .default_attrs = paths_subsys_attrs,
487 };
488
489 /* We hard define the 4 types of path we expect to find */
490 static PDCSPATH_ENTRY(PDCS_ADDR_PPRI, primary);
491 static PDCSPATH_ENTRY(PDCS_ADDR_PCON, console);
492 static PDCSPATH_ENTRY(PDCS_ADDR_PALT, alternative);
493 static PDCSPATH_ENTRY(PDCS_ADDR_PKBD, keyboard);
494
495 /* An array containing all PDC paths we will deal with */
496 static struct pdcspath_entry *pdcspath_entries[] = {
497         &pdcspath_entry_primary,
498         &pdcspath_entry_alternative,
499         &pdcspath_entry_console,
500         &pdcspath_entry_keyboard,
501         NULL,
502 };
503
504
505 /* For more insight of what's going on here, refer to PDC Procedures doc,
506  * Section PDC_STABLE */
507
508 /**
509  * pdcs_size_read - Stable Storage size output.
510  * @entry: An allocated and populated subsytem struct. We don't use it tho.
511  * @buf: The output buffer to write to.
512  */
513 static ssize_t
514 pdcs_size_read(struct subsystem *entry, char *buf)
515 {
516         char *out = buf;
517         
518         if (!entry || !buf)
519                 return -EINVAL;
520                 
521         /* show the size of the stable storage */
522         out += sprintf(out, "%ld\n", pdcs_size);
523
524         return out - buf;
525 }
526
527 /**
528  * pdcs_auto_read - Stable Storage autoboot/search flag output.
529  * @entry: An allocated and populated subsytem struct. We don't use it tho.
530  * @buf: The output buffer to write to.
531  * @knob: The PF_AUTOBOOT or PF_AUTOSEARCH flag
532  */
533 static ssize_t
534 pdcs_auto_read(struct subsystem *entry, char *buf, int knob)
535 {
536         char *out = buf;
537         struct pdcspath_entry *pathentry;
538
539         if (!entry || !buf)
540                 return -EINVAL;
541
542         /* Current flags are stored in primary boot path entry */
543         pathentry = &pdcspath_entry_primary;
544
545         read_lock(&pathentry->rw_lock);
546         out += sprintf(out, "%s\n", (pathentry->devpath.flags & knob) ?
547                                         "On" : "Off");
548         read_unlock(&pathentry->rw_lock);
549
550         return out - buf;
551 }
552
553 /**
554  * pdcs_autoboot_read - Stable Storage autoboot flag output.
555  * @entry: An allocated and populated subsytem struct. We don't use it tho.
556  * @buf: The output buffer to write to.
557  */
558 static inline ssize_t
559 pdcs_autoboot_read(struct subsystem *entry, char *buf)
560 {
561         return pdcs_auto_read(entry, buf, PF_AUTOBOOT);
562 }
563
564 /**
565  * pdcs_autosearch_read - Stable Storage autoboot flag output.
566  * @entry: An allocated and populated subsytem struct. We don't use it tho.
567  * @buf: The output buffer to write to.
568  */
569 static inline ssize_t
570 pdcs_autosearch_read(struct subsystem *entry, char *buf)
571 {
572         return pdcs_auto_read(entry, buf, PF_AUTOSEARCH);
573 }
574
575 /**
576  * pdcs_timer_read - Stable Storage timer count output (in seconds).
577  * @entry: An allocated and populated subsytem struct. We don't use it tho.
578  * @buf: The output buffer to write to.
579  *
580  * The value of the timer field correponds to a number of seconds in powers of 2.
581  */
582 static ssize_t
583 pdcs_timer_read(struct subsystem *entry, char *buf)
584 {
585         char *out = buf;
586         struct pdcspath_entry *pathentry;
587
588         if (!entry || !buf)
589                 return -EINVAL;
590
591         /* Current flags are stored in primary boot path entry */
592         pathentry = &pdcspath_entry_primary;
593
594         /* print the timer value in seconds */
595         read_lock(&pathentry->rw_lock);
596         out += sprintf(out, "%u\n", (pathentry->devpath.flags & PF_TIMER) ?
597                                 (1 << (pathentry->devpath.flags & PF_TIMER)) : 0);
598         read_unlock(&pathentry->rw_lock);
599
600         return out - buf;
601 }
602
603 /**
604  * pdcs_osid_read - Stable Storage OS ID register output.
605  * @entry: An allocated and populated subsytem struct. We don't use it tho.
606  * @buf: The output buffer to write to.
607  */
608 static ssize_t
609 pdcs_osid_read(struct subsystem *entry, char *buf)
610 {
611         char *out = buf;
612         __u32 result;
613         char *tmpstr = NULL;
614
615         if (!entry || !buf)
616                 return -EINVAL;
617
618         /* get OSID */
619         if (pdc_stable_read(PDCS_ADDR_OSID, &result, sizeof(result)) != PDC_OK)
620                 return -EIO;
621
622         /* the actual result is 16 bits away */
623         switch (result >> 16) {
624                 case 0x0000:    tmpstr = "No OS-dependent data"; break;
625                 case 0x0001:    tmpstr = "HP-UX dependent data"; break;
626                 case 0x0002:    tmpstr = "MPE-iX dependent data"; break;
627                 case 0x0003:    tmpstr = "OSF dependent data"; break;
628                 case 0x0004:    tmpstr = "HP-RT dependent data"; break;
629                 case 0x0005:    tmpstr = "Novell Netware dependent data"; break;
630                 default:        tmpstr = "Unknown"; break;
631         }
632         out += sprintf(out, "%s (0x%.4x)\n", tmpstr, (result >> 16));
633
634         return out - buf;
635 }
636
637 /**
638  * pdcs_fastsize_read - Stable Storage FastSize register output.
639  * @entry: An allocated and populated subsytem struct. We don't use it tho.
640  * @buf: The output buffer to write to.
641  *
642  * This register holds the amount of system RAM to be tested during boot sequence.
643  */
644 static ssize_t
645 pdcs_fastsize_read(struct subsystem *entry, char *buf)
646 {
647         char *out = buf;
648         __u32 result;
649
650         if (!entry || !buf)
651                 return -EINVAL;
652
653         /* get fast-size */
654         if (pdc_stable_read(PDCS_ADDR_FSIZ, &result, sizeof(result)) != PDC_OK)
655                 return -EIO;
656
657         if ((result & 0x0F) < 0x0E)
658                 out += sprintf(out, "%d kB", (1<<(result & 0x0F))*256);
659         else
660                 out += sprintf(out, "All");
661         out += sprintf(out, "\n");
662         
663         return out - buf;
664 }
665
666 /**
667  * pdcs_auto_write - This function handles autoboot/search flag modifying.
668  * @entry: An allocated and populated subsytem struct. We don't use it tho.
669  * @buf: The input buffer to read from.
670  * @count: The number of bytes to be read.
671  * @knob: The PF_AUTOBOOT or PF_AUTOSEARCH flag
672  * 
673  * We will call this function to change the current autoboot flag.
674  * We expect a precise syntax:
675  *      \"n\" (n == 0 or 1) to toggle AutoBoot Off or On
676  */
677 static ssize_t
678 pdcs_auto_write(struct subsystem *entry, const char *buf, size_t count, int knob)
679 {
680         struct pdcspath_entry *pathentry;
681         unsigned char flags;
682         char in[count+1], *temp;
683         char c;
684
685         if (!capable(CAP_SYS_ADMIN))
686                 return -EACCES;
687
688         if (!entry || !buf || !count)
689                 return -EINVAL;
690
691         /* We'll use a local copy of buf */
692         memset(in, 0, count+1);
693         strncpy(in, buf, count);
694
695         /* Current flags are stored in primary boot path entry */
696         pathentry = &pdcspath_entry_primary;
697         
698         /* Be nice to the existing flag record */
699         read_lock(&pathentry->rw_lock);
700         flags = pathentry->devpath.flags;
701         read_unlock(&pathentry->rw_lock);
702         
703         DPRINTK("%s: flags before: 0x%X\n", __func__, flags);
704                         
705         temp = in;
706         
707         while (*temp && isspace(*temp))
708                 temp++;
709         
710         c = *temp++ - '0';
711         if ((c != 0) && (c != 1))
712                 goto parse_error;
713         if (c == 0)
714                 flags &= ~knob;
715         else
716                 flags |= knob;
717         
718         DPRINTK("%s: flags after: 0x%X\n", __func__, flags);
719                 
720         /* So far so good, let's get in deep */
721         write_lock(&pathentry->rw_lock);
722         
723         /* Change the path entry flags first */
724         pathentry->devpath.flags = flags;
725                 
726         /* Now, dive in. Write back to the hardware */
727         pdcspath_store(pathentry);
728         write_unlock(&pathentry->rw_lock);
729         
730         printk(KERN_INFO PDCS_PREFIX ": changed \"%s\" to \"%s\"\n",
731                 (knob & PF_AUTOBOOT) ? "autoboot" : "autosearch",
732                 (flags & knob) ? "On" : "Off");
733         
734         return count;
735
736 parse_error:
737         printk(KERN_WARNING "%s: Parse error: expect \"n\" (n == 0 or 1)\n", __func__);
738         return -EINVAL;
739 }
740
741 /**
742  * pdcs_autoboot_write - This function handles autoboot flag modifying.
743  * @entry: An allocated and populated subsytem struct. We don't use it tho.
744  * @buf: The input buffer to read from.
745  * @count: The number of bytes to be read.
746  *
747  * We will call this function to change the current boot flags.
748  * We expect a precise syntax:
749  *      \"n\" (n == 0 or 1) to toggle AutoSearch Off or On
750  */
751 static inline ssize_t
752 pdcs_autoboot_write(struct subsystem *entry, const char *buf, size_t count)
753 {
754         return pdcs_auto_write(entry, buf, count, PF_AUTOBOOT);
755 }
756
757 /**
758  * pdcs_autosearch_write - This function handles autosearch flag modifying.
759  * @entry: An allocated and populated subsytem struct. We don't use it tho.
760  * @buf: The input buffer to read from.
761  * @count: The number of bytes to be read.
762  *
763  * We will call this function to change the current boot flags.
764  * We expect a precise syntax:
765  *      \"n\" (n == 0 or 1) to toggle AutoSearch Off or On
766  */
767 static inline ssize_t
768 pdcs_autosearch_write(struct subsystem *entry, const char *buf, size_t count)
769 {
770         return pdcs_auto_write(entry, buf, count, PF_AUTOSEARCH);
771 }
772
773 /* The remaining attributes. */
774 static PDCS_ATTR(size, 0444, pdcs_size_read, NULL);
775 static PDCS_ATTR(autoboot, 0644, pdcs_autoboot_read, pdcs_autoboot_write);
776 static PDCS_ATTR(autosearch, 0644, pdcs_autosearch_read, pdcs_autosearch_write);
777 static PDCS_ATTR(timer, 0444, pdcs_timer_read, NULL);
778 static PDCS_ATTR(osid, 0400, pdcs_osid_read, NULL);
779 static PDCS_ATTR(fastsize, 0400, pdcs_fastsize_read, NULL);
780
781 static struct subsys_attribute *pdcs_subsys_attrs[] = {
782         &pdcs_attr_size,
783         &pdcs_attr_autoboot,
784         &pdcs_attr_autosearch,
785         &pdcs_attr_timer,
786         &pdcs_attr_osid,
787         &pdcs_attr_fastsize,
788         NULL,
789 };
790
791 static decl_subsys(paths, &ktype_pdcspath, NULL);
792 static decl_subsys(stable, NULL, NULL);
793
794 /**
795  * pdcs_register_pathentries - Prepares path entries kobjects for sysfs usage.
796  * 
797  * It creates kobjects corresponding to each path entry with nice sysfs
798  * links to the real device. This is where the magic takes place: when
799  * registering the subsystem attributes during module init, each kobject hereby
800  * created will show in the sysfs tree as a folder containing files as defined
801  * by path_subsys_attr[].
802  */
803 static inline int __init
804 pdcs_register_pathentries(void)
805 {
806         unsigned short i;
807         struct pdcspath_entry *entry;
808         int err;
809         
810         /* Initialize the entries rw_lock before anything else */
811         for (i = 0; (entry = pdcspath_entries[i]); i++)
812                 rwlock_init(&entry->rw_lock);
813
814         for (i = 0; (entry = pdcspath_entries[i]); i++) {
815                 write_lock(&entry->rw_lock);
816                 err = pdcspath_fetch(entry);
817                 write_unlock(&entry->rw_lock);
818
819                 if (err < 0)
820                         continue;
821
822                 if ((err = kobject_set_name(&entry->kobj, "%s", entry->name)))
823                         return err;
824                 kobj_set_kset_s(entry, paths_subsys);
825                 if ((err = kobject_register(&entry->kobj)))
826                         return err;
827                 
828                 /* kobject is now registered */
829                 write_lock(&entry->rw_lock);
830                 entry->ready = 2;
831                 
832                 /* Add a nice symlink to the real device */
833                 if (entry->dev)
834                         sysfs_create_link(&entry->kobj, &entry->dev->kobj, "device");
835
836                 write_unlock(&entry->rw_lock);
837         }
838         
839         return 0;
840 }
841
842 /**
843  * pdcs_unregister_pathentries - Routine called when unregistering the module.
844  */
845 static inline void
846 pdcs_unregister_pathentries(void)
847 {
848         unsigned short i;
849         struct pdcspath_entry *entry;
850         
851         for (i = 0; (entry = pdcspath_entries[i]); i++) {
852                 read_lock(&entry->rw_lock);
853                 if (entry->ready >= 2)
854                         kobject_unregister(&entry->kobj);
855                 read_unlock(&entry->rw_lock);
856         }
857 }
858
859 /*
860  * For now we register the stable subsystem with the firmware subsystem
861  * and the paths subsystem with the stable subsystem
862  */
863 static int __init
864 pdc_stable_init(void)
865 {
866         struct subsys_attribute *attr;
867         int i, rc = 0, error = 0;
868
869         /* find the size of the stable storage */
870         if (pdc_stable_get_size(&pdcs_size) != PDC_OK) 
871                 return -ENODEV;
872
873         /* make sure we have enough data */
874         if (pdcs_size < 96)
875                 return -ENODATA;
876
877         printk(KERN_INFO PDCS_PREFIX " facility v%s\n", PDCS_VERSION);
878
879         /* For now we'll register the stable subsys within this driver */
880         if ((rc = firmware_register(&stable_subsys)))
881                 goto fail_firmreg;
882
883         /* Don't forget the root entries */
884         for (i = 0; (attr = pdcs_subsys_attrs[i]) && !error; i++)
885                 if (attr->show)
886                         error = subsys_create_file(&stable_subsys, attr);
887         
888         /* register the paths subsys as a subsystem of stable subsys */
889         kset_set_kset_s(&paths_subsys, stable_subsys);
890         if ((rc= subsystem_register(&paths_subsys)))
891                 goto fail_subsysreg;
892
893         /* now we create all "files" for the paths subsys */
894         if ((rc = pdcs_register_pathentries()))
895                 goto fail_pdcsreg;
896
897         return rc;
898         
899 fail_pdcsreg:
900         pdcs_unregister_pathentries();
901         subsystem_unregister(&paths_subsys);
902         
903 fail_subsysreg:
904         firmware_unregister(&stable_subsys);
905         
906 fail_firmreg:
907         printk(KERN_INFO PDCS_PREFIX " bailing out\n");
908         return rc;
909 }
910
911 static void __exit
912 pdc_stable_exit(void)
913 {
914         pdcs_unregister_pathentries();
915         subsystem_unregister(&paths_subsys);
916
917         firmware_unregister(&stable_subsys);
918 }
919
920
921 module_init(pdc_stable_init);
922 module_exit(pdc_stable_exit);