This commit was manufactured by cvs2svn to create branch 'vserver'.
[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 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/sched.h>                /* for capable() */
46 #include <linux/kernel.h>
47 #include <linux/string.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.09"
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 = 0;
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         entry->ready = 1;
198         
199         DPRINTK("%s: device: 0x%p\n", __func__, entry->dev);
200         
201         return 0;
202 }
203
204 /**
205  * pdcspath_hwpath_read - This function handles hardware path pretty printing.
206  * @entry: An allocated and populated pdscpath_entry struct.
207  * @buf: The output buffer to write to.
208  * 
209  * We will call this function to format the output of the hwpath attribute file.
210  */
211 static ssize_t
212 pdcspath_hwpath_read(struct pdcspath_entry *entry, char *buf)
213 {
214         char *out = buf;
215         struct device_path *devpath;
216         unsigned short i;
217
218         if (!entry || !buf)
219                 return -EINVAL;
220
221         devpath = &entry->devpath;
222
223         if (!entry->ready)
224                 return -ENODATA;
225         
226         for (i = 0; i < 6; i++) {
227                 if (devpath->bc[i] >= 128)
228                         continue;
229                 out += sprintf(out, "%u/", (unsigned char)devpath->bc[i]);
230         }
231         out += sprintf(out, "%u\n", (unsigned char)devpath->mod);
232         
233         return out - buf;
234 }
235
236 /**
237  * pdcspath_hwpath_write - This function handles hardware path modifying.
238  * @entry: An allocated and populated pdscpath_entry struct.
239  * @buf: The input buffer to read from.
240  * @count: The number of bytes to be read.
241  * 
242  * We will call this function to change the current hardware path.
243  * Hardware paths are to be given '/'-delimited, without brackets.
244  * We take care to make sure that the provided path actually maps to an existing
245  * device, BUT nothing would prevent some foolish user to set the path to some
246  * PCI bridge or even a CPU...
247  * A better work around would be to make sure we are at the end of a device tree
248  * for instance, but it would be IMHO beyond the simple scope of that driver.
249  * The aim is to provide a facility. Data correctness is left to userland.
250  */
251 static ssize_t
252 pdcspath_hwpath_write(struct pdcspath_entry *entry, const char *buf, size_t count)
253 {
254         struct hardware_path hwpath;
255         unsigned short i;
256         char in[count+1], *temp;
257         struct device *dev;
258
259         if (!entry || !buf || !count)
260                 return -EINVAL;
261
262         /* We'll use a local copy of buf */
263         memset(in, 0, count+1);
264         strncpy(in, buf, count);
265         
266         /* Let's clean up the target. 0xff is a blank pattern */
267         memset(&hwpath, 0xff, sizeof(hwpath));
268         
269         /* First, pick the mod field (the last one of the input string) */
270         if (!(temp = strrchr(in, '/')))
271                 return -EINVAL;
272                         
273         hwpath.mod = simple_strtoul(temp+1, NULL, 10);
274         in[temp-in] = '\0';     /* truncate the remaining string. just precaution */
275         DPRINTK("%s: mod: %d\n", __func__, hwpath.mod);
276         
277         /* Then, loop for each delimiter, making sure we don't have too many.
278            we write the bc fields in a down-top way. No matter what, we stop
279            before writing the last field. If there are too many fields anyway,
280            then the user is a moron and it'll be caught up later when we'll
281            check the consistency of the given hwpath. */
282         for (i=5; ((temp = strrchr(in, '/'))) && (temp-in > 0) && (likely(i)); i--) {
283                 hwpath.bc[i] = simple_strtoul(temp+1, NULL, 10);
284                 in[temp-in] = '\0';
285                 DPRINTK("%s: bc[%d]: %d\n", __func__, i, hwpath.bc[i]);
286         }
287         
288         /* Store the final field */             
289         hwpath.bc[i] = simple_strtoul(in, NULL, 10);
290         DPRINTK("%s: bc[%d]: %d\n", __func__, i, hwpath.bc[i]);
291         
292         /* Now we check that the user isn't trying to lure us */
293         if (!(dev = hwpath_to_device((struct hardware_path *)&hwpath))) {
294                 printk(KERN_WARNING "%s: attempt to set invalid \"%s\" "
295                         "hardware path: %s\n", __func__, entry->name, buf);
296                 return -EINVAL;
297         }
298         
299         /* So far so good, let's get in deep */
300         entry->ready = 0;
301         entry->dev = dev;
302         
303         /* Now, dive in. Write back to the hardware */
304         WARN_ON(pdcspath_store(entry)); /* this warn should *NEVER* happen */
305         
306         /* Update the symlink to the real device */
307         sysfs_remove_link(&entry->kobj, "device");
308         sysfs_create_link(&entry->kobj, &entry->dev->kobj, "device");
309         
310         printk(KERN_INFO "PDC Stable Storage: changed \"%s\" path to \"%s\"\n",
311                 entry->name, buf);
312         
313         return count;
314 }
315
316 /**
317  * pdcspath_layer_read - Extended layer (eg. SCSI ids) pretty printing.
318  * @entry: An allocated and populated pdscpath_entry struct.
319  * @buf: The output buffer to write to.
320  * 
321  * We will call this function to format the output of the layer attribute file.
322  */
323 static ssize_t
324 pdcspath_layer_read(struct pdcspath_entry *entry, char *buf)
325 {
326         char *out = buf;
327         struct device_path *devpath;
328         unsigned short i;
329
330         if (!entry || !buf)
331                 return -EINVAL;
332         
333         devpath = &entry->devpath;
334
335         if (!entry->ready)
336                 return -ENODATA;
337         
338         for (i = 0; devpath->layers[i] && (likely(i < 6)); i++)
339                 out += sprintf(out, "%u ", devpath->layers[i]);
340
341         out += sprintf(out, "\n");
342         
343         return out - buf;
344 }
345
346 /**
347  * pdcspath_layer_write - This function handles extended layer modifying.
348  * @entry: An allocated and populated pdscpath_entry struct.
349  * @buf: The input buffer to read from.
350  * @count: The number of bytes to be read.
351  * 
352  * We will call this function to change the current layer value.
353  * Layers are to be given '.'-delimited, without brackets.
354  * XXX beware we are far less checky WRT input data provided than for hwpath.
355  * Potential harm can be done, since there's no way to check the validity of
356  * the layer fields.
357  */
358 static ssize_t
359 pdcspath_layer_write(struct pdcspath_entry *entry, const char *buf, size_t count)
360 {
361         unsigned int layers[6]; /* device-specific info (ctlr#, unit#, ...) */
362         unsigned short i;
363         char in[count+1], *temp;
364
365         if (!entry || !buf || !count)
366                 return -EINVAL;
367
368         /* We'll use a local copy of buf */
369         memset(in, 0, count+1);
370         strncpy(in, buf, count);
371         
372         /* Let's clean up the target. 0 is a blank pattern */
373         memset(&layers, 0, sizeof(layers));
374         
375         /* First, pick the first layer */
376         if (unlikely(!isdigit(*in)))
377                 return -EINVAL;
378         layers[0] = simple_strtoul(in, NULL, 10);
379         DPRINTK("%s: layer[0]: %d\n", __func__, layers[0]);
380         
381         temp = in;
382         for (i=1; ((temp = strchr(temp, '.'))) && (likely(i<6)); i++) {
383                 if (unlikely(!isdigit(*(++temp))))
384                         return -EINVAL;
385                 layers[i] = simple_strtoul(temp, NULL, 10);
386                 DPRINTK("%s: layer[%d]: %d\n", __func__, i, layers[i]);
387         }
388                 
389         /* So far so good, let's get in deep */
390         
391         /* First, overwrite the current layers with the new ones, not touching
392            the hardware path. */
393         memcpy(&entry->devpath.layers, &layers, sizeof(layers));
394         
395         /* Now, dive in. Write back to the hardware */
396         WARN_ON(pdcspath_store(entry)); /* this warn should *NEVER* happen */
397         
398         printk(KERN_INFO "PDC Stable Storage: changed \"%s\" layers to \"%s\"\n",
399                 entry->name, buf);
400         
401         return count;
402 }
403
404 /**
405  * pdcspath_attr_show - Generic read function call wrapper.
406  * @kobj: The kobject to get info from.
407  * @attr: The attribute looked upon.
408  * @buf: The output buffer.
409  */
410 static ssize_t
411 pdcspath_attr_show(struct kobject *kobj, struct attribute *attr, char *buf)
412 {
413         struct pdcspath_entry *entry = to_pdcspath_entry(kobj);
414         struct pdcspath_attribute *pdcs_attr = to_pdcspath_attribute(attr);
415         ssize_t ret = 0;
416
417         if (!capable(CAP_SYS_ADMIN))
418                 return -EACCES;
419
420         if (pdcs_attr->show)
421                 ret = pdcs_attr->show(entry, buf);
422
423         return ret;
424 }
425
426 /**
427  * pdcspath_attr_store - Generic write function call wrapper.
428  * @kobj: The kobject to write info to.
429  * @attr: The attribute to be modified.
430  * @buf: The input buffer.
431  * @count: The size of the buffer.
432  */
433 static ssize_t
434 pdcspath_attr_store(struct kobject *kobj, struct attribute *attr,
435                         const char *buf, size_t count)
436 {
437         struct pdcspath_entry *entry = to_pdcspath_entry(kobj);
438         struct pdcspath_attribute *pdcs_attr = to_pdcspath_attribute(attr);
439         ssize_t ret = 0;
440
441         if (!capable(CAP_SYS_ADMIN))
442                 return -EACCES;
443
444         if (pdcs_attr->store)
445                 ret = pdcs_attr->store(entry, buf, count);
446
447         return ret;
448 }
449
450 static struct sysfs_ops pdcspath_attr_ops = {
451         .show = pdcspath_attr_show,
452         .store = pdcspath_attr_store,
453 };
454
455 /* These are the two attributes of any PDC path. */
456 static PATHS_ATTR(hwpath, 0600, pdcspath_hwpath_read, pdcspath_hwpath_write);
457 static PATHS_ATTR(layer, 0600, pdcspath_layer_read, pdcspath_layer_write);
458
459 static struct attribute *paths_subsys_attrs[] = {
460         &paths_attr_hwpath.attr,
461         &paths_attr_layer.attr,
462         NULL,
463 };
464
465 /* Specific kobject type for our PDC paths */
466 static struct kobj_type ktype_pdcspath = {
467         .sysfs_ops = &pdcspath_attr_ops,
468         .default_attrs = paths_subsys_attrs,
469 };
470
471 /* We hard define the 4 types of path we expect to find */
472 static PDCSPATH_ENTRY(PDCS_ADDR_PPRI, primary);
473 static PDCSPATH_ENTRY(PDCS_ADDR_PCON, console);
474 static PDCSPATH_ENTRY(PDCS_ADDR_PALT, alternative);
475 static PDCSPATH_ENTRY(PDCS_ADDR_PKBD, keyboard);
476
477 /* An array containing all PDC paths we will deal with */
478 static struct pdcspath_entry *pdcspath_entries[] = {
479         &pdcspath_entry_primary,
480         &pdcspath_entry_alternative,
481         &pdcspath_entry_console,
482         &pdcspath_entry_keyboard,
483         NULL,
484 };
485
486 /**
487  * pdcs_info_read - Pretty printing of the remaining useful data.
488  * @entry: An allocated and populated subsytem struct. We don't use it tho.
489  * @buf: The output buffer to write to.
490  * 
491  * We will call this function to format the output of the 'info' attribute file.
492  * Please refer to PDC Procedures documentation, section PDC_STABLE to get a
493  * better insight of what we're doing here.
494  */
495 static ssize_t
496 pdcs_info_read(struct subsystem *entry, char *buf)
497 {
498         char *out = buf;
499         __u32 result;
500         struct device_path devpath;
501         char *tmpstr = NULL;
502         
503         if (!entry || !buf)
504                 return -EINVAL;
505                 
506         /* show the size of the stable storage */
507         out += sprintf(out, "Stable Storage size: %ld bytes\n", pdcs_size);
508
509         /* deal with flags */
510         if (pdc_stable_read(PDCS_ADDR_PPRI, &devpath, sizeof(devpath)) != PDC_OK)
511                 return -EIO;
512         
513         out += sprintf(out, "Autoboot: %s\n", (devpath.flags & PF_AUTOBOOT) ? "On" : "Off");
514         out += sprintf(out, "Autosearch: %s\n", (devpath.flags & PF_AUTOSEARCH) ? "On" : "Off");
515         out += sprintf(out, "Timer: %u s\n", (devpath.flags & PF_TIMER) ? (1 << (devpath.flags & PF_TIMER)) : 0);
516
517         /* get OSID */
518         if (pdc_stable_read(PDCS_ADDR_OSID, &result, sizeof(result)) != PDC_OK)
519                 return -EIO;
520
521         /* the actual result is 16 bits away */
522         switch (result >> 16) {
523                 case 0x0000:    tmpstr = "No OS-dependent data"; break;
524                 case 0x0001:    tmpstr = "HP-UX dependent data"; break;
525                 case 0x0002:    tmpstr = "MPE-iX dependent data"; break;
526                 case 0x0003:    tmpstr = "OSF dependent data"; break;
527                 case 0x0004:    tmpstr = "HP-RT dependent data"; break;
528                 case 0x0005:    tmpstr = "Novell Netware dependent data"; break;
529                 default:        tmpstr = "Unknown"; break;
530         }
531         out += sprintf(out, "OS ID: %s (0x%.4x)\n", tmpstr, (result >> 16));
532
533         /* get fast-size */
534         if (pdc_stable_read(PDCS_ADDR_FSIZ, &result, sizeof(result)) != PDC_OK)
535                 return -EIO;
536
537         out += sprintf(out, "Memory tested: ");
538         if ((result & 0x0F) < 0x0E)
539                 out += sprintf(out, "%.3f MB", 0.256*(1<<(result & 0x0F)));
540         else
541                 out += sprintf(out, "All");
542         out += sprintf(out, "\n");
543         
544         return out - buf;
545 }
546
547 /**
548  * pdcs_info_write - This function handles boot flag modifying.
549  * @entry: An allocated and populated subsytem struct. We don't use it tho.
550  * @buf: The input buffer to read from.
551  * @count: The number of bytes to be read.
552  * 
553  * We will call this function to change the current boot flags.
554  * We expect a precise syntax:
555  *      \"n n\" (n == 0 or 1) to toggle respectively AutoBoot and AutoSearch
556  *
557  * As of now there is no incentive on my side to provide more "knobs" to that
558  * interface, since modifying the rest of the data is pretty meaningless when
559  * the machine is running and for the expected use of that facility, such as
560  * PALO setting up the boot disk when installing a Linux distribution...
561  */
562 static ssize_t
563 pdcs_info_write(struct subsystem *entry, const char *buf, size_t count)
564 {
565         struct pdcspath_entry *pathentry;
566         unsigned char flags;
567         char in[count+1], *temp;
568         char c;
569
570         if (!capable(CAP_SYS_ADMIN))
571                 return -EACCES;
572
573         if (!entry || !buf || !count)
574                 return -EINVAL;
575
576         /* We'll use a local copy of buf */
577         memset(in, 0, count+1);
578         strncpy(in, buf, count);
579
580         /* Current flags are stored in primary boot path entry */
581         pathentry = &pdcspath_entry_primary;
582         
583         /* Be nice to the existing flag record */
584         flags = pathentry->devpath.flags;
585         
586         DPRINTK("%s: flags before: 0x%X\n", __func__, flags);
587                         
588         temp = in;
589         
590         while (*temp && isspace(*temp))
591                 temp++;
592         
593         c = *temp++ - '0';
594         if ((c != 0) && (c != 1))
595                 goto parse_error;
596         if (c == 0)
597                 flags &= ~PF_AUTOBOOT;
598         else
599                 flags |= PF_AUTOBOOT;
600         
601         if (*temp++ != ' ')
602                 goto parse_error;
603         
604         c = *temp++ - '0';
605         if ((c != 0) && (c != 1))
606                 goto parse_error;
607         if (c == 0)
608                 flags &= ~PF_AUTOSEARCH;
609         else
610                 flags |= PF_AUTOSEARCH;
611         
612         DPRINTK("%s: flags after: 0x%X\n", __func__, flags);
613                 
614         /* So far so good, let's get in deep */
615         
616         /* Change the path entry flags first */
617         pathentry->devpath.flags = flags;
618                 
619         /* Now, dive in. Write back to the hardware */
620         WARN_ON(pdcspath_store(pathentry));     /* this warn should *NEVER* happen */
621         
622         printk(KERN_INFO "PDC Stable Storage: changed flags to \"%s\"\n", buf);
623         
624         return count;
625
626 parse_error:
627         printk(KERN_WARNING "%s: Parse error: expect \"n n\" (n == 0 or 1) for AB and AS\n", __func__);
628         return -EINVAL;
629 }
630
631 /* The last attribute (the 'root' one actually) with all remaining data. */
632 static PDCS_ATTR(info, 0600, pdcs_info_read, pdcs_info_write);
633
634 static struct subsys_attribute *pdcs_subsys_attrs[] = {
635         &pdcs_attr_info,
636         NULL,   /* maybe more in the future? */
637 };
638
639 static decl_subsys(paths, &ktype_pdcspath, NULL);
640 static decl_subsys(pdc, NULL, NULL);
641
642 /**
643  * pdcs_register_pathentries - Prepares path entries kobjects for sysfs usage.
644  * 
645  * It creates kobjects corresponding to each path entry with nice sysfs
646  * links to the real device. This is where the magic takes place: when
647  * registering the subsystem attributes during module init, each kobject hereby
648  * created will show in the sysfs tree as a folder containing files as defined
649  * by path_subsys_attr[].
650  */
651 static inline int __init
652 pdcs_register_pathentries(void)
653 {
654         unsigned short i;
655         struct pdcspath_entry *entry;
656         
657         for (i = 0; (entry = pdcspath_entries[i]); i++) {
658                 if (pdcspath_fetch(entry) < 0)
659                         continue;
660
661                 kobject_set_name(&entry->kobj, "%s", entry->name);
662                 kobj_set_kset_s(entry, paths_subsys);
663                 kobject_register(&entry->kobj);
664
665                 if (!entry->dev)
666                         continue;
667
668                 /* Add a nice symlink to the real device */
669                 sysfs_create_link(&entry->kobj, &entry->dev->kobj, "device");
670         }
671         
672         return 0;
673 }
674
675 /**
676  * pdcs_unregister_pathentries - Routine called when unregistering the module.
677  */
678 static inline void __exit
679 pdcs_unregister_pathentries(void)
680 {
681         unsigned short i;
682         struct pdcspath_entry *entry;
683         
684         for (i = 0; (entry = pdcspath_entries[i]); i++)
685                 if (entry->ready)
686                         kobject_unregister(&entry->kobj);       
687 }
688
689 /*
690  * For now we register the pdc subsystem with the firmware subsystem
691  * and the paths subsystem with the pdc subsystem
692  */
693 static int __init
694 pdc_stable_init(void)
695 {
696         struct subsys_attribute *attr;
697         int i, rc = 0, error = 0;
698
699         /* find the size of the stable storage */
700         if (pdc_stable_get_size(&pdcs_size) != PDC_OK) 
701                 return -ENODEV;
702
703         printk(KERN_INFO "PDC Stable Storage facility v%s\n", PDCS_VERSION);
704
705         /* For now we'll register the pdc subsys within this driver */
706         if ((rc = firmware_register(&pdc_subsys)))
707                 return rc;
708
709         /* Don't forget the info entry */
710         for (i = 0; (attr = pdcs_subsys_attrs[i]) && !error; i++)
711                 if (attr->show)
712                         error = subsys_create_file(&pdc_subsys, attr);
713         
714         /* register the paths subsys as a subsystem of pdc subsys */
715         kset_set_kset_s(&paths_subsys, pdc_subsys);
716         subsystem_register(&paths_subsys);
717
718         /* now we create all "files" for the paths subsys */
719         pdcs_register_pathentries();
720         
721         return 0;
722 }
723
724 static void __exit
725 pdc_stable_exit(void)
726 {
727         pdcs_unregister_pathentries();
728         subsystem_unregister(&paths_subsys);
729
730         firmware_unregister(&pdc_subsys);
731 }
732
733
734 module_init(pdc_stable_init);
735 module_exit(pdc_stable_exit);