[PATCH] ixp2000: fix gcc4 breakage
[deliverable/linux.git] / drivers / parisc / pdc_stable.c
CommitLineData
1da177e4
LT
1/*
2 * Interfaces to retrieve and set PDC Stable options (firmware)
3 *
c7428422 4 * Copyright (C) 2005-2006 Thibaut VARENE <varenet@parisc-linux.org>
1da177e4
LT
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
c7428422 29 * following code can deal with just 96 bytes of Stable Storage, and all
1da177e4
LT
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.
c7428422
TV
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
1da177e4
LT
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>
1da177e4
LT
53#include <linux/kernel.h>
54#include <linux/string.h>
c59ede7b 55#include <linux/capability.h>
1da177e4
LT
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>
c7428422 61#include <linux/spinlock.h>
1da177e4
LT
62
63#include <asm/pdc.h>
64#include <asm/page.h>
65#include <asm/uaccess.h>
66#include <asm/hardware.h>
67
c7428422
TV
68#define PDCS_VERSION "0.22"
69#define PDCS_PREFIX "PDC Stable Storage"
1da177e4
LT
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
78MODULE_AUTHOR("Thibaut VARENE <varenet@parisc-linux.org>");
79MODULE_DESCRIPTION("sysfs interface to HP PDC Stable Storage data");
80MODULE_LICENSE("GPL");
81MODULE_VERSION(PDCS_VERSION);
82
c7428422 83/* holds Stable Storage size. Initialized once and for all, no lock needed */
8039de10 84static unsigned long pdcs_size __read_mostly;
1da177e4
LT
85
86/* This struct defines what we need to deal with a parisc pdc path entry */
87struct pdcspath_entry {
c7428422 88 rwlock_t rw_lock; /* to protect path entry access */
1da177e4
LT
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
97struct 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) \
104struct 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) \
111struct 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) \
118struct 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.
c7428422
TV
136 *
137 * This function expects to be called with @entry->rw_lock write-hold.
1da177e4
LT
138 */
139static int
140pdcspath_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
c7428422
TV
177 *
178 * This function expects to be called with @entry->rw_lock write-hold.
1da177e4 179 */
c7428422 180static void
1da177e4
LT
181pdcspath_store(struct pdcspath_entry *entry)
182{
183 struct device_path *devpath;
184
c7428422 185 BUG_ON(!entry);
1da177e4
LT
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 */
c7428422
TV
194 BUG_ON(!entry->dev);
195 device_to_hwpath(entry->dev, (struct hardware_path *)devpath);
1da177e4
LT
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__);
c7428422 207 WARN_ON(1);
1da177e4
LT
208 }
209
4b991da7
TV
210 /* kobject is already registered */
211 entry->ready = 2;
1da177e4
LT
212
213 DPRINTK("%s: device: 0x%p\n", __func__, entry->dev);
1da177e4
LT
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 */
223static ssize_t
224pdcspath_hwpath_read(struct pdcspath_entry *entry, char *buf)
225{
226 char *out = buf;
227 struct device_path *devpath;
c7428422 228 short i;
1da177e4
LT
229
230 if (!entry || !buf)
231 return -EINVAL;
232
c7428422 233 read_lock(&entry->rw_lock);
1da177e4 234 devpath = &entry->devpath;
c7428422
TV
235 i = entry->ready;
236 read_unlock(&entry->rw_lock);
1da177e4 237
c7428422 238 if (!i) /* entry is not ready */
1da177e4
LT
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.
c7428422 259 * We make sure that the provided path actually maps to an existing
1da177e4
LT
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 */
266static ssize_t
267pdcspath_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 */
c7428422 315 write_lock(&entry->rw_lock);
1da177e4
LT
316 entry->ready = 0;
317 entry->dev = dev;
318
319 /* Now, dive in. Write back to the hardware */
c7428422 320 pdcspath_store(entry);
1da177e4
LT
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");
c7428422 325 write_unlock(&entry->rw_lock);
1da177e4 326
c7428422 327 printk(KERN_INFO PDCS_PREFIX ": changed \"%s\" path to \"%s\"\n",
1da177e4
LT
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 */
340static ssize_t
341pdcspath_layer_read(struct pdcspath_entry *entry, char *buf)
342{
343 char *out = buf;
344 struct device_path *devpath;
c7428422 345 short i;
1da177e4
LT
346
347 if (!entry || !buf)
348 return -EINVAL;
349
c7428422 350 read_lock(&entry->rw_lock);
1da177e4 351 devpath = &entry->devpath;
c7428422
TV
352 i = entry->ready;
353 read_unlock(&entry->rw_lock);
1da177e4 354
c7428422 355 if (!i) /* entry is not ready */
1da177e4
LT
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 */
378static ssize_t
379pdcspath_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 */
c7428422 410 write_lock(&entry->rw_lock);
1da177e4
LT
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 */
c7428422
TV
417 pdcspath_store(entry);
418 write_unlock(&entry->rw_lock);
1da177e4 419
c7428422 420 printk(KERN_INFO PDCS_PREFIX ": changed \"%s\" layers to \"%s\"\n",
1da177e4
LT
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 */
432static ssize_t
433pdcspath_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
1da177e4
LT
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 */
452static ssize_t
453pdcspath_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
469static 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. */
c7428422
TV
475static PATHS_ATTR(hwpath, 0644, pdcspath_hwpath_read, pdcspath_hwpath_write);
476static PATHS_ATTR(layer, 0644, pdcspath_layer_read, pdcspath_layer_write);
1da177e4
LT
477
478static 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 */
485static 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 */
491static PDCSPATH_ENTRY(PDCS_ADDR_PPRI, primary);
492static PDCSPATH_ENTRY(PDCS_ADDR_PCON, console);
493static PDCSPATH_ENTRY(PDCS_ADDR_PALT, alternative);
494static PDCSPATH_ENTRY(PDCS_ADDR_PKBD, keyboard);
495
496/* An array containing all PDC paths we will deal with */
497static 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
c7428422
TV
505
506/* For more insight of what's going on here, refer to PDC Procedures doc,
507 * Section PDC_STABLE */
508
1da177e4 509/**
c7428422 510 * pdcs_size_read - Stable Storage size output.
1da177e4
LT
511 * @entry: An allocated and populated subsytem struct. We don't use it tho.
512 * @buf: The output buffer to write to.
1da177e4
LT
513 */
514static ssize_t
c7428422 515pdcs_size_read(struct subsystem *entry, char *buf)
1da177e4
LT
516{
517 char *out = buf;
1da177e4
LT
518
519 if (!entry || !buf)
520 return -EINVAL;
521
522 /* show the size of the stable storage */
c7428422 523 out += sprintf(out, "%ld\n", pdcs_size);
1da177e4 524
c7428422
TV
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 */
534static ssize_t
535pdcs_auto_read(struct subsystem *entry, char *buf, int knob)
536{
537 char *out = buf;
538 struct pdcspath_entry *pathentry;
1da177e4 539
c7428422
TV
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 */
559static inline ssize_t
560pdcs_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 */
570static inline ssize_t
571pdcs_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 */
583static ssize_t
584pdcs_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 */
609static ssize_t
610pdcs_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;
1da177e4
LT
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 }
c7428422
TV
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 */
645static ssize_t
646pdcs_fastsize_read(struct subsystem *entry, char *buf)
647{
648 char *out = buf;
649 __u32 result;
650
651 if (!entry || !buf)
652 return -EINVAL;
1da177e4
LT
653
654 /* get fast-size */
655 if (pdc_stable_read(PDCS_ADDR_FSIZ, &result, sizeof(result)) != PDC_OK)
656 return -EIO;
657
1da177e4 658 if ((result & 0x0F) < 0x0E)
abff7543 659 out += sprintf(out, "%d kB", (1<<(result & 0x0F))*256);
1da177e4
LT
660 else
661 out += sprintf(out, "All");
662 out += sprintf(out, "\n");
663
664 return out - buf;
665}
666
667/**
c7428422 668 * pdcs_auto_write - This function handles autoboot/search flag modifying.
1da177e4
LT
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.
c7428422 672 * @knob: The PF_AUTOBOOT or PF_AUTOSEARCH flag
1da177e4 673 *
c7428422 674 * We will call this function to change the current autoboot flag.
1da177e4 675 * We expect a precise syntax:
c7428422 676 * \"n\" (n == 0 or 1) to toggle AutoBoot Off or On
1da177e4
LT
677 */
678static ssize_t
c7428422 679pdcs_auto_write(struct subsystem *entry, const char *buf, size_t count, int knob)
1da177e4
LT
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 */
c7428422 700 read_lock(&pathentry->rw_lock);
1da177e4 701 flags = pathentry->devpath.flags;
c7428422 702 read_unlock(&pathentry->rw_lock);
1da177e4
LT
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)
c7428422 715 flags &= ~knob;
1da177e4 716 else
c7428422 717 flags |= knob;
1da177e4
LT
718
719 DPRINTK("%s: flags after: 0x%X\n", __func__, flags);
720
721 /* So far so good, let's get in deep */
c7428422 722 write_lock(&pathentry->rw_lock);
1da177e4
LT
723
724 /* Change the path entry flags first */
725 pathentry->devpath.flags = flags;
726
727 /* Now, dive in. Write back to the hardware */
c7428422
TV
728 pdcspath_store(pathentry);
729 write_unlock(&pathentry->rw_lock);
1da177e4 730
c7428422
TV
731 printk(KERN_INFO PDCS_PREFIX ": changed \"%s\" to \"%s\"\n",
732 (knob & PF_AUTOBOOT) ? "autoboot" : "autosearch",
733 (flags & knob) ? "On" : "Off");
1da177e4
LT
734
735 return count;
736
737parse_error:
c7428422 738 printk(KERN_WARNING "%s: Parse error: expect \"n\" (n == 0 or 1)\n", __func__);
1da177e4
LT
739 return -EINVAL;
740}
741
c7428422
TV
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 */
752static inline ssize_t
753pdcs_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 */
768static inline ssize_t
769pdcs_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. */
775static PDCS_ATTR(size, 0444, pdcs_size_read, NULL);
776static PDCS_ATTR(autoboot, 0644, pdcs_autoboot_read, pdcs_autoboot_write);
777static PDCS_ATTR(autosearch, 0644, pdcs_autosearch_read, pdcs_autosearch_write);
778static PDCS_ATTR(timer, 0444, pdcs_timer_read, NULL);
779static PDCS_ATTR(osid, 0400, pdcs_osid_read, NULL);
780static PDCS_ATTR(fastsize, 0400, pdcs_fastsize_read, NULL);
1da177e4
LT
781
782static struct subsys_attribute *pdcs_subsys_attrs[] = {
c7428422
TV
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,
1da177e4
LT
790};
791
792static decl_subsys(paths, &ktype_pdcspath, NULL);
c7428422 793static decl_subsys(stable, NULL, NULL);
1da177e4
LT
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 */
804static inline int __init
805pdcs_register_pathentries(void)
806{
807 unsigned short i;
808 struct pdcspath_entry *entry;
4b991da7 809 int err;
1da177e4 810
c7428422
TV
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
1da177e4 815 for (i = 0; (entry = pdcspath_entries[i]); i++) {
c7428422
TV
816 write_lock(&entry->rw_lock);
817 err = pdcspath_fetch(entry);
818 write_unlock(&entry->rw_lock);
819
820 if (err < 0)
1da177e4
LT
821 continue;
822
4b991da7
TV
823 if ((err = kobject_set_name(&entry->kobj, "%s", entry->name)))
824 return err;
1da177e4 825 kobj_set_kset_s(entry, paths_subsys);
4b991da7
TV
826 if ((err = kobject_register(&entry->kobj)))
827 return err;
828
829 /* kobject is now registered */
c7428422 830 write_lock(&entry->rw_lock);
4b991da7
TV
831 entry->ready = 2;
832
1da177e4 833 /* Add a nice symlink to the real device */
c7428422
TV
834 if (entry->dev)
835 sysfs_create_link(&entry->kobj, &entry->dev->kobj, "device");
836
837 write_unlock(&entry->rw_lock);
1da177e4
LT
838 }
839
840 return 0;
841}
842
843/**
844 * pdcs_unregister_pathentries - Routine called when unregistering the module.
845 */
4b991da7 846static inline void
1da177e4
LT
847pdcs_unregister_pathentries(void)
848{
849 unsigned short i;
850 struct pdcspath_entry *entry;
851
c7428422
TV
852 for (i = 0; (entry = pdcspath_entries[i]); i++) {
853 read_lock(&entry->rw_lock);
4b991da7 854 if (entry->ready >= 2)
c7428422
TV
855 kobject_unregister(&entry->kobj);
856 read_unlock(&entry->rw_lock);
857 }
1da177e4
LT
858}
859
860/*
c7428422
TV
861 * For now we register the stable subsystem with the firmware subsystem
862 * and the paths subsystem with the stable subsystem
1da177e4
LT
863 */
864static int __init
865pdc_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
c7428422
TV
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);
1da177e4 879
c7428422
TV
880 /* For now we'll register the stable subsys within this driver */
881 if ((rc = firmware_register(&stable_subsys)))
4b991da7 882 goto fail_firmreg;
1da177e4 883
c7428422 884 /* Don't forget the root entries */
1da177e4
LT
885 for (i = 0; (attr = pdcs_subsys_attrs[i]) && !error; i++)
886 if (attr->show)
c7428422 887 error = subsys_create_file(&stable_subsys, attr);
1da177e4 888
c7428422
TV
889 /* register the paths subsys as a subsystem of stable subsys */
890 kset_set_kset_s(&paths_subsys, stable_subsys);
4b991da7
TV
891 if ((rc= subsystem_register(&paths_subsys)))
892 goto fail_subsysreg;
1da177e4
LT
893
894 /* now we create all "files" for the paths subsys */
4b991da7
TV
895 if ((rc = pdcs_register_pathentries()))
896 goto fail_pdcsreg;
897
898 return rc;
1da177e4 899
4b991da7
TV
900fail_pdcsreg:
901 pdcs_unregister_pathentries();
902 subsystem_unregister(&paths_subsys);
903
904fail_subsysreg:
c7428422 905 firmware_unregister(&stable_subsys);
4b991da7
TV
906
907fail_firmreg:
c7428422 908 printk(KERN_INFO PDCS_PREFIX " bailing out\n");
4b991da7 909 return rc;
1da177e4
LT
910}
911
912static void __exit
913pdc_stable_exit(void)
914{
915 pdcs_unregister_pathentries();
916 subsystem_unregister(&paths_subsys);
917
c7428422 918 firmware_unregister(&stable_subsys);
1da177e4
LT
919}
920
921
922module_init(pdc_stable_init);
923module_exit(pdc_stable_exit);
This page took 0.166699 seconds and 5 git commands to generate.