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