diff options
author | Hans de Goede <j.w.r.degoede@hhs.nl> | 2007-09-21 11:03:32 -0400 |
---|---|---|
committer | Mark M. Hoffman <mhoffman@lightlink.com> | 2007-10-09 22:56:31 -0400 |
commit | 2ed4263308971af3a7a0f20bef6b2257ea230a71 (patch) | |
tree | 4638108f95cc9d14efd2f5b186188c1421fcfad0 /Documentation/hwmon/sysfs-interface | |
parent | cc28a610d4fcce1831d26f2099e23fbb3eb7260c (diff) |
hwmon: update sysfs interface document - error handling
Here is a patch adding some text to the sysfs interface documentation on how
settings written to sysfs attributes should be handled, focussing mainly on
error handling. This version incorperates Jean's latest comments.
Signed-off-by: Hans de Goede <j.w.r.degoede@hhs.nl>
Acked-by: Jean Delvare <khali@linux-fr.org>
Signed-off-by: Mark M. Hoffman <mhoffman@lightlink.com>
Diffstat (limited to 'Documentation/hwmon/sysfs-interface')
-rw-r--r-- | Documentation/hwmon/sysfs-interface | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/Documentation/hwmon/sysfs-interface b/Documentation/hwmon/sysfs-interface index db7bb4a659c4..a2153c4616ad 100644 --- a/Documentation/hwmon/sysfs-interface +++ b/Documentation/hwmon/sysfs-interface | |||
@@ -67,6 +67,10 @@ between readings to be caught and alarmed. The exact definition of an | |||
67 | alarm (for example, whether a threshold must be met or must be exceeded | 67 | alarm (for example, whether a threshold must be met or must be exceeded |
68 | to cause an alarm) is chip-dependent. | 68 | to cause an alarm) is chip-dependent. |
69 | 69 | ||
70 | When setting values of hwmon sysfs attributes, the string representation of | ||
71 | the desired value must be written, note that strings which are not a number | ||
72 | are interpreted as 0! For more on how written strings are interpreted see the | ||
73 | "sysfs attribute writes interpretation" section at the end of this file. | ||
70 | 74 | ||
71 | ------------------------------------------------------------------------- | 75 | ------------------------------------------------------------------------- |
72 | 76 | ||
@@ -411,3 +415,57 @@ beep_mask Bitmask for beep. | |||
411 | use discouraged for the same reason. Use individual | 415 | use discouraged for the same reason. Use individual |
412 | *_beep files instead. | 416 | *_beep files instead. |
413 | RW | 417 | RW |
418 | |||
419 | |||
420 | sysfs attribute writes interpretation | ||
421 | ------------------------------------- | ||
422 | |||
423 | hwmon sysfs attributes always contain numbers, so the first thing to do is to | ||
424 | convert the input to a number, there are 2 ways todo this depending whether | ||
425 | the number can be negative or not: | ||
426 | unsigned long u = simple_strtoul(buf, NULL, 10); | ||
427 | long s = simple_strtol(buf, NULL, 10); | ||
428 | |||
429 | With buf being the buffer with the user input being passed by the kernel. | ||
430 | Notice that we do not use the second argument of strto[u]l, and thus cannot | ||
431 | tell when 0 is returned, if this was really 0 or is caused by invalid input. | ||
432 | This is done deliberately as checking this everywhere would add a lot of | ||
433 | code to the kernel. | ||
434 | |||
435 | Notice that it is important to always store the converted value in an | ||
436 | unsigned long or long, so that no wrap around can happen before any further | ||
437 | checking. | ||
438 | |||
439 | After the input string is converted to an (unsigned) long, the value should be | ||
440 | checked if its acceptable. Be careful with further conversions on the value | ||
441 | before checking it for validity, as these conversions could still cause a wrap | ||
442 | around before the check. For example do not multiply the result, and only | ||
443 | add/subtract if it has been divided before the add/subtract. | ||
444 | |||
445 | What to do if a value is found to be invalid, depends on the type of the | ||
446 | sysfs attribute that is being set. If it is a continuous setting like a | ||
447 | tempX_max or inX_max attribute, then the value should be clamped to its | ||
448 | limits using SENSORS_LIMIT(value, min_limit, max_limit). If it is not | ||
449 | continuous like for example a tempX_type, then when an invalid value is | ||
450 | written, -EINVAL should be returned. | ||
451 | |||
452 | Example1, temp1_max, register is a signed 8 bit value (-128 - 127 degrees): | ||
453 | --- begin code --- | ||
454 | long v = simple_strtol(buf, NULL, 10) / 1000; | ||
455 | SENSORS_LIMIT(v, -128, 127); | ||
456 | /* write v to register */ | ||
457 | --- end code --- | ||
458 | |||
459 | Example2, fan divider setting, valid values 2, 4 and 8: | ||
460 | --- begin code --- | ||
461 | unsigned long v = simple_strtoul(buf, NULL, 10); | ||
462 | |||
463 | switch (v) { | ||
464 | case 2: v = 1; break; | ||
465 | case 4: v = 2; break; | ||
466 | case 8: v = 3; break; | ||
467 | default: | ||
468 | return -EINVAL; | ||
469 | } | ||
470 | /* write v to register */ | ||
471 | --- end code --- | ||