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