hp accelerometer: fix LED handling and add freefall detection
Subsystems (3)
lis3 chip can provide interrupt when it detects freefall; it is done by hardware and is rather reliable. This adds (experimental) support for it.
LED on HP notebooks is connected through ACPI. That unfortunately means that it needs to be delayed by using schedule_work() to avoid calling ACPI interpretter in invalid context. This patch fixes that.
drivers/hwmon/hp_accel.c +77 -17
@@ -3,7 +3,7 @@* * Copyright (C) 2007-2008 Yan Burman * Copyright (C) 2008 Eric Piel -* Copyright (C) 2008 Pavel Machek +* Copyright (C) 2008-2009 Pavel Machek * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by
@@ -44,6 +44,36 @@ #include "lis3lv02d.h"#define DRIVER_NAME "lis3lv02d"#define ACPI_MDPS_CLASS "accelerometer" +/* Delayed LEDs infrastructure ------------------------------------ */ + +/* Special LED class that can defer work */ +struct delayed_led_classdev { +struct led_classdev led_classdev; +struct work_struct work; +enum led_brightness new_brightness; + +unsigned int led;/* For driver */ +void (*set_brightness)(struct delayed_led_classdev *data,enum led_brightness value); +}; + +static inline void delayed_set_status_worker (struct work_struct *work) +{ +struct delayed_led_classdev *data = + container_of(work, struct delayed_led_classdev, work); + + data->set_brightness (data, data->new_brightness); +} + +static inline void delayed_sysfs_set (struct led_classdev *led_cdev, +enum led_brightness brightness) +{ +struct delayed_led_classdev *data =container_of (led_cdev, + struct delayed_led_classdev, led_classdev); + data->new_brightness = brightness; +schedule_work (&data->work); +} + +/* HP-specific accelerometer driver ------------------------------------ */ /* For automatic insertion of the module */ static struct acpi_device_id lis3lv02d_device_ids[] = {
@@ -155,29 +185,52 @@ static struct dmi_system_id lis3lv02d_dm*/ }; -static acpi_status hpled_acpi_write (acpi_handle handle,int reg) +static void hpled_set (struct delayed_led_classdev *led_cdev,enum led_brightness value) { +acpi_handle handle = adev.device->handle;unsigned long long ret;/* Not used when writing */ union acpi_object in_obj[1 ];struct acpi_object_list args = {1 , in_obj }; in_obj[0 ].type =ACPI_TYPE_INTEGER ; - in_obj[0 ].integer.value = reg; + in_obj[0 ].integer.value = !!value; -return acpi_evaluate_integer (handle,"ALED" , &args, &ret); +acpi_evaluate_integer (handle,"ALED" , &args, &ret); } -static void hpled_set (struct led_classdev *led_cdev, -enum led_brightness value) +static struct delayed_led_classdev hpled_led = { + .led_classdev = { + .name ="hp::hddprotect" , + .default_trigger ="none" , + .brightness_set = delayed_sysfs_set, + .flags =LED_CORE_SUSPENDRESUME , + }, + .set_brightness = hpled_set, +}; + +static acpi_status +lis3lv02d_get_resource (struct acpi_resource *resource,void *context) { -hpled_acpi_write (adev.device->handle, !!value); +if (resource->type ==ACPI_RESOURCE_TYPE_EXTENDED_IRQ ) { +struct acpi_resource_extended_irq *irq; +u32 *device_irq = context; + + irq = &resource->data.extended_irq; + *device_irq = irq->interrupts[0 ]; + } + +return AE_OK ; } -static struct led_classdev hpled_led = { - .name ="hp:red:hddprotection" , - .default_trigger ="heartbeat" , - .brightness_set = hpled_set, -}; +static void lis3lv02d_enum_resources (struct acpi_device *device) +{ +acpi_status status; + + status =acpi_walk_resources (device->handle,METHOD_NAME__CRS , + lis3lv02d_get_resource, &adev.irq); +if (ACPI_FAILURE (status)) + printk(KERN_DEBUG DRIVER_NAME ": Error getting resources\n"); +}static int lis3lv02d_add (struct acpi_device *device) {
@@ -208,13 +261,20 @@ static int lis3lv02d_add(struct acpi_dev adev.ac = lis3lv02d_axis_normal; } - ret =led_classdev_register (NULL , &hpled_led); +INIT_WORK (&hpled_led.work, delayed_set_status_worker); + ret =led_classdev_register (NULL , &hpled_led.led_classdev);if (ret)return ret; +/* obtain IRQ number of our device from ACPI */ +lis3lv02d_enum_resources (adev.device); + ret =lis3lv02d_init_device (&adev);if (ret) { -led_classdev_unregister (&hpled_led); +while (work_pending (&hpled_led.work)) +schedule (); + +led_classdev_unregister (&hpled_led.led_classdev);return ret; }
@@ -229,7 +289,9 @@ static int lis3lv02d_remove(struct acpi_lis3lv02d_joystick_disable ();lis3lv02d_poweroff (device->handle); -led_classdev_unregister (&hpled_led); +while (work_pending (&hpled_led.work)) +schedule (); +led_classdev_unregister (&hpled_led.led_classdev);return lis3lv02d_remove_fs (); }
@@ -240,7 +302,6 @@ static int lis3lv02d_suspend(struct acpi {/* make sure the device is off when we suspend */ lis3lv02d_poweroff (device->handle); -led_classdev_suspend (&hpled_led);return 0 ; }
@@ -253,7 +314,6 @@ static int lis3lv02d_resume(struct acpi_ else lis3lv02d_poweroff(device->handle);mutex_unlock (&adev.lock); -led_classdev_resume (&hpled_led); return 0; }#else
drivers/hwmon/lis3lv02d.c +152 -17
229 lines not on this page · raw message
drivers/hwmon/lis3lv02d.h +5 -0
@@ -170,6 +170,11 @@ struct acpi_lis3lv02d {unsigned char is_on;/* whether the device is on or off */ unsigned char usage;/* usage counter */ struct axis_conversion ac;/* hw -> logical axis */ + +u32 irq;/* IRQ number */ +struct fasync_struct *async_queue;/* queue for the misc device */ +wait_queue_head_t misc_wait;/* Wait queue for the misc device */ +unsigned long misc_opened;/* bit0: whether the device is open */ };int lis3lv02d_init_device (struct acpi_lis3lv02d *dev);
Signature · 2 lines
(english) http://www.livejournal.com/~pavelmachek (cesky, pictures) http://atrey.karlin.mff.cuni.cz/~pavel/picture/horses/blog.html
Quoted 8 lines · Pavel Machek
lis3 chip can provide interrupt when it detects freefall; it is done by hardware and is rather reliable. This adds (experimental) support for it.
LED on HP notebooks is connected through ACPI. That unfortunately means that it needs to be delayed by using
schedule_work()to avoid calling ACPI interpretter in invalid context. This patch fixes that.
Hi Pavel,
Thanks a lot for doing it! I'll test it this week. However, I think eventually the LED fix and the freefall infrastructure should be two different patches. Also, while adding the freefall support, you should add the info in the doc, as well as the little C program demonstrating its use.
See you,
Eric
A message that is not in the archive (sent off-list or not yet received): [email protected]
Quoted 2 lines
Hi,
Very good work.
Thanks :-).
Quoted 3 lines
Really hard to get accurate accelerometer data by sensor, there need some emulation for sensor like your work. I am using lis331 in our phone. :)
Fortunately, HP is doing most of the work for us.
Pavel
Signature · 2 lines
(english) http://www.livejournal.com/~pavelmachek (cesky, pictures) http://atrey.karlin.mff.cuni.cz/~pavel/picture/horses/blog.html
hp accelerometer: add freefall detection
In reply to Éric PielQuoted 6 lines
Thanks a lot for doing it! I'll test it this week. However, I think eventually the LED fix and the freefall infrastructure should be two different patches. Also, while adding the freefall support, you should add the info in the doc, as well as the little C program demonstrating its use.
Agreed :-).
---
This adds freefall handling to hp_accel driver. According to HP, it should just work, without us having to set the chip up by hand.
hpfall.c is example .c program that parks the disk when accelerometer detects free fall. It should work; for now, it uses fixed 20seconds protection period.
Documentation/hwmon/hpfall.c new file +101 -0
@@ -0,0 +1,101 @@ +/* Disk protection for HP machines. +* +* Copyright 2008 Eric Piel +* Copyright 2009 Pavel Machek <[email protected]> +* +* GPLv2. +*/ + +#include <stdio.h> +#include <stdlib.h> +#include <unistd.h> +#include <fcntl.h> +#include <sys/stat.h> +#include <sys/types.h> +#include <string.h> +#include <stdint.h> +#include <errno.h> +#include <signal.h> + +void write_int (char *path,int i) +{ +char buf[1024 ]; +int fd =open (path,O_RDWR ); +if (fd <0 ) { +perror ("open" ); +exit (1 ); + } +sprintf (buf,"%d" , i); +if (write (fd, buf,strlen (buf)) !=strlen (buf)) { +perror ("write" ); +exit (1 ); + } +close (fd); +} + +void set_led (int on) +{ +write_int ("/sys/class/leds/hp::hddprotect/brightness" , on); +} + +void protect (int seconds) +{ +write_int ("/sys/block/sda/device/unload_heads" , seconds*1000 ); +} + +int on_ac (void ) +{ +// /sys/class/power_supply/AC0/online +} + +int lid_open (void ) +{ +// /proc/acpi/button/lid/LID/state +} + +void ignore_me (void ) +{ +protect (0 ); +set_led (0 ); + +} + +int main (int argc,char * argv[]) +{ +int fd, ret; + + fd =open ("/dev/freefall" ,O_RDONLY ); +if (fd <0 ) { +perror ("open" ); +return EXIT_FAILURE ; + } + +signal (SIGALRM , ignore_me); + +for (;;) { +unsigned char count; + + ret =read (fd, &count,sizeof (count)); +alarm (0 ); +if ((ret ==-1 ) && (errno ==EINTR )) { +/* Alarm expired, time to unpark the heads */ +continue ; + } + +if (ret !=sizeof (count)) { +perror ("read" ); +break ; + } + +protect (21 ); +set_led (1 ); +if (1 ||on_ac () ||lid_open ()) { +alarm (2 ); + }else { +alarm (20 ); + } + } + +close (fd); +return EXIT_SUCCESS ; +}
Documentation/hwmon/lis3lv02d +8 -0
@@ -33,6 +33,14 @@ rate - reports the sampling rate of the This driver also provides an absolute input class device, allowing the laptop to act as a pinball machine-esque joystick. +Another feature of the driver is misc device called "freefall" that +acts similar to /dev/rtc and reacts on free-fall interrupts received +from the device. It supports blocking operations, poll/select and +fasync operation modes. You must read 1 bytes from the device. The +result is number of free-fall interrupts since the last successful +read (or 255 if number of interrupts would not fit). + + Axes orientation ----------------
drivers/hwmon/hp_accel.c +27 -0
@@ -208,6 +208,30 @@ static struct delayed_led_classdev hpled .set_brightness = hpled_set, }; +static acpi_status +lis3lv02d_get_resource (struct acpi_resource *resource,void *context) +{ +if (resource->type ==ACPI_RESOURCE_TYPE_EXTENDED_IRQ ) { +struct acpi_resource_extended_irq *irq; +u32 *device_irq = context; + + irq = &resource->data.extended_irq; + *device_irq = irq->interrupts[0 ]; + } + +return AE_OK ; +} + +static void lis3lv02d_enum_resources (struct acpi_device *device) +{ +acpi_status status; + + status =acpi_walk_resources (device->handle,METHOD_NAME__CRS , + lis3lv02d_get_resource, &adev.irq); +if (ACPI_FAILURE (status)) + printk(KERN_DEBUG DRIVER_NAME ": Error getting resources\n"); +} +static int lis3lv02d_add (struct acpi_device *device) {u8 val;
@@ -242,6 +266,9 @@ static int lis3lv02d_add(struct acpi_devif (ret)return ret; +/* obtain IRQ number of our device from ACPI */ +lis3lv02d_enum_resources (adev.device); + ret =lis3lv02d_init_device (&adev);if (ret) {flush_work (&hpled_led.work);
drivers/hwmon/lis3lv02d.c +155 -17
232 lines not on this page · raw message
drivers/hwmon/lis3lv02d.h +5 -0
@@ -170,6 +170,11 @@ struct acpi_lis3lv02d {unsigned char is_on;/* whether the device is on or off */ unsigned char usage;/* usage counter */ struct axis_conversion ac;/* hw -> logical axis */ + +u32 irq;/* IRQ number */ +struct fasync_struct *async_queue;/* queue for the misc device */ +wait_queue_head_t misc_wait;/* Wait queue for the misc device */ +unsigned long misc_opened;/* bit0: whether the device is open */ };int lis3lv02d_init_device (struct acpi_lis3lv02d *dev);
Signature · 2 lines
(english) http://www.livejournal.com/~pavelmachek (cesky, pictures) http://atrey.karlin.mff.cuni.cz/~pavel/picture/horses/blog.html
Re: hp accelerometer: add freefall detection
Documentation/hwmon/hpfall.c
Quoted 4 lines · On Fri, 16 Jan 2009 13:19:40 +0100 Pavel Machek <[email protected]> wrote:
new file mode 100755
index 0000000..7d17764Documentation/hwmon/hpfall.c new file +0
-0
checkpatch has fun with this file.
Documentation/hwmon/hpfall.c
Quoted 9 lines
...
Documentation/hwmon/hpfall.c new file +4
-1-struct acpi_lis3lv02d adev; +struct acpi_lis3lv02d adev = { + .misc_wait =__WAIT_QUEUE_HEAD_INITIALIZER (adev.misc_wait), +}; +EXPORT_SYMBOL_GPL (adev);
This seems a rather poor name for a globally-visible identifier.
checkpatch fun (was Re: hp accelerometer: add freefall detection)
Quoted 9 lines · Andrew Morton
Documentation/hwmon/hpfall.c
Quoted 4 lines · On Fri, 16 Jan 2009 13:19:40 +0100 Pavel Machek <[email protected]> wrote:
new file mode 100755
index 0000000..7d17764Documentation/hwmon/hpfall.c new file +0
-0checkpatch has fun with this file.
Well, I have fun with checkpatch :-(.
pavel@amd:/data/l/linux/Documentation$ ../scripts/checkpatch.pl hwmon/hpfall.c ERROR: Does not appear to be a unified-diff format patch
total: 1 errors, 0 warnings, 0 lines checked
hwmon/hpfall.c has style problems, please review. If any of these errors
are false positives report them to the maintainer, see CHECKPATCH in MAINTAINERS.
pavel@amd:/data/l/linux/Documentation$ ../scripts/checkpatch.pl -f hwmon/hpfall.c Unknown option: f pavel@amd:/data/l/linux/Documentation$ ../scripts/checkpatch.pl -h Unknown option: h pavel@amd:/data/l/linux/Documentation$ ../scripts/checkpatch.pl --help Unknown option: help pavel@amd:/data/l/linux/Documentation$
Ok, will fix.
Quoted 8 lines
Documentation/hwmon/hpfall.c
Quoted 6 lines
Documentation/hwmon/hpfall.c new file +4
-1-struct acpi_lis3lv02d adev; +struct acpi_lis3lv02d adev = { + .misc_wait =__WAIT_QUEUE_HEAD_INITIALIZER (adev.misc_wait), +}; +EXPORT_SYMBOL_GPL (adev);This seems a rather poor name for a globally-visible identifier.
Hmm, right. lis_acpi_dev should be better. Will fix.
---
I tried checkpatch-ing a file, and not only my attempts to use it failed, my attempts to get help failed too.
pavel@amd:/data/l/linux/Documentation$ ../scripts/checkpatch.pl hwmon/hpfall.c ERROR: Does not appear to be a unified-diff format patch .... pavel@amd:/data/l/linux/Documentation$ ../scripts/checkpatch.pl -f hwmon/hpfall.c Unknown option: f pavel@amd:/data/l/linux/Documentation$ ../scripts/checkpatch.pl -h Unknown option: h pavel@amd:/data/l/linux/Documentation$ ../scripts/checkpatch.pl --help Unknown option: help
Make -f alias of --file, and print help if user passes unknown option.
---
commit 4ef496ab198210648c310b8ae25872b9f99bee27
tree 1155a0dad369dc33362994bb6ff74868c7408f4c
parent f155017822e2fea0bc7434545cc63c2c8d363e59 author Pavel <[email protected]> Tue, 20 Jan 2009 11:31:52 +0100 committer Pavel <[email protected]> Tue, 20 Jan 2009 11:31:52 +0100
scripts/checkpatch.pl | 34 ++++++++++++++++++++-------------- 1 files changed, 20 insertions(+), 14 deletions(-)
scripts/checkpatch.pl +20 -14
@@ -28,6 +28,22 @@ my $mailback = 0; my $summary_file = 0; my $root; my %debug; + +sub help { + print "usage: $P [options] patchfile\n"; + print "version: $V\n"; + print "options: -q => quiet\n"; + print " --no-tree => run without a kernel tree\n"; + print " --terse => one line per report\n"; + print " --emacs => emacs compile window format\n"; + print " --file => check a source file\n"; + print " --strict => enable more subjective tests\n"; + print " --root => path to the kernel tree root\n"; + print " --no-summary => suppress the per-file summary\n"; + print " --summary-file => include the filename in summary\n"; + exit(1); +} + GetOptions( 'q|quiet+' => \$quiet, 'tree!' => \$tree,
@@ -35,7 +51,7 @@ GetOptions( 'patch!' => \$chk_patch, 'emacs!' => \$emacs, 'terse!' => \$terse, - 'file!' => \$file, + 'f|file!' => \$file, 'subjective!' => \$check, 'strict!' => \$check, 'root=s' => \$root,
@@ -45,23 +61,13 @@ GetOptions( 'debug=s' => \%debug, 'test-only=s' => \$tst_only, -) or exit; +) or help; my $exit = 0; + if ($#ARGV < 0) { - print "usage: $P [options] patchfile\n"; - print "version: $V\n"; - print "options: -q => quiet\n"; - print " --no-tree => run without a kernel tree\n"; - print " --terse => one line per report\n"; - print " --emacs => emacs compile window format\n"; - print " --file => check a source file\n"; - print " --strict => enable more subjective tests\n"; - print " --root => path to the kernel tree root\n"; - print " --no-summary => suppress the per-file summary\n"; - print " --summary-file => include the filename in summary\n"; - exit(1); + help; } my $dbg_values = 0;
Signature · 2 lines
(english) http://www.livejournal.com/~pavelmachek (cesky, pictures) http://atrey.karlin.mff.cuni.cz/~pavel/picture/horses/blog.html
PATCH 1/1 checkpatch: make -f alias --file, add --help, more verbose help message
Impact:
- More verbose help/usage message.
- Make the option -f an alias for --file.
- On -h, --help, and --version display help message and exit(0).
- With no FILE(s) given, exit(1) with "no input files".
- On invalid options display help/usage and exit(1).
Based on a patch by Pavel Machek.
---
parts from the original mail:
Quoted 17 lines · Pavel Machek
Subject: checkpatch fun (was Re: hp accelerometer: add freefall detection)
[... skip ...]
I tried checkpatch-ing a file, and not only my attempts to use it failed, my attempts to get help failed too.
pavel@amd:/data/l/linux/Documentation$ ../scripts/checkpatch.pl hwmon/hpfall.c ERROR: Does not appear to be a unified-diff format patch .... pavel@amd:/data/l/linux/Documentation$ ../scripts/checkpatch.pl -f hwmon/hpfall.c Unknown option: f pavel@amd:/data/l/linux/Documentation$ ../scripts/checkpatch.pl -h Unknown option: h pavel@amd:/data/l/linux/Documentation$ ../scripts/checkpatch.pl --help Unknown option: help
Quoted 2 lines
[... skip ...]
scripts/checkpatch.pl | 55 +++++++++++++++++++++++++++++++++++++------------ 1 files changed, 42 insertions(+), 13 deletions(-)
scripts/checkpatch.pl +42 -13
@@ -28,6 +28,41 @@ my $mailback = 0; my $summary_file = 0; my $root; my %debug; +my $help = 0; + +sub help { + my ($exitcode) = @_; + + print << "EOM"; +Usage: $P [OPTION]... [FILE]... +Version: $V + +Options: + -q, --quiet quiet + --no-tree run without a kernel tree + --no-signoff do not check for 'Signed-off-by' line + --patch treat FILE as patchfile (default) + --emacs emacs compile window format + --terse one line per report + -f, --file treat FILE as regular source file + --subjective, --strict enable more subjective tests + --root=PATH PATH to the kernel tree root + --no-summary suppress the per-file summary + --mailback only produce a report in case of warnings/errors + --summary-file include the filename in summary + --debug KEY=[0|1] turn on/off debugging of KEY, where KEY is one of + 'values', 'possible', 'type', and 'attr' (default + is all off) + --test-only=WORD report only warnings/errors containing WORD + literally + -h, --help, --version display this help and exit + +When FILE is - read standard input. +EOM + + exit($exitcode); +} + GetOptions( 'q|quiet+' => \$quiet, 'tree!' => \$tree,
@@ -35,7 +70,7 @@ GetOptions( 'patch!' => \$chk_patch, 'emacs!' => \$emacs, 'terse!' => \$terse, - 'file!' => \$file, + 'f|file!' => \$file, 'subjective!' => \$check, 'strict!' => \$check, 'root=s' => \$root,
@@ -45,22 +80,16 @@ GetOptions( 'debug=s' => \%debug, 'test-only=s' => \$tst_only, -) or exit; + 'h|help' => \$help, + 'version' => \$help +) or help(1); + +help(0) if ($help); my $exit = 0; if ($#ARGV < 0) { - print "usage: $P [options] patchfile\n"; - print "version: $V\n"; - print "options: -q => quiet\n"; - print " --no-tree => run without a kernel tree\n"; - print " --terse => one line per report\n"; - print " --emacs => emacs compile window format\n"; - print " --file => check a source file\n"; - print " --strict => enable more subjective tests\n"; - print " --root => path to the kernel tree root\n"; - print " --no-summary => suppress the per-file summary\n"; - print " --summary-file => include the filename in summary\n"; + print "$P: no input files\n"; exit(1); }
Andy Whitcroft
maintainerRe: [PATCH] checkpatch: make -f alias --file, add --help, more verbose help message
Quoted 8 lines · Hannes Eder
Impact:
- More verbose help/usage message.
- Make the option -f an alias for --file.
- On -h, --help, and --version display help message and exit(0).
- With no FILE(s) given, exit(1) with "no input files".
- On invalid options display help/usage and exit(1).Based on a patch by Pavel Machek.
That looks reasonable. Will integate it into my next set of updates.
-apw
Andy Whitcroft
maintainerRe: [PATCH] checkpatch: make -f alias --file, add --help, more verbose help message
Quoted 11 lines · Andy Whitcroft
Quoted 8 lines · Hannes Eder
Impact:
- More verbose help/usage message.
- Make the option -f an alias for --file.
- On -h, --help, and --version display help message and exit(0).
- With no FILE(s) given, exit(1) with "no input files".
- On invalid options display help/usage and exit(1).Based on a patch by Pavel Machek.
That looks reasonable. Will integate it into my next set of updates.
Ok, now in my testing branch. See:
http://www.kernel.org/pub/linux/kernel/people/apw/checkpatch/checkpatch.pl-testing
-apw
Re: [PATCH] checkpatch: make -f alias --file, add --help, more verbose help message
In reply to Hannes EderQuoted 11 lines · Hannes Eder
Impact:
- More verbose help/usage message.
- Make the option -f an alias for --file.
- On -h, --help, and --version display help message and exit(0).
- With no FILE(s) given, exit(1) with "no input files".
- On invalid options display help/usage and exit(1).Based on a patch by Pavel Machek.
Signed-off-by Hannes Eder <[email protected]>Cc Pavel Machek <[email protected]>
ACK and thanks...
Signature · 2 lines
(english) http://www.livejournal.com/~pavelmachek (cesky, pictures) http://atrey.karlin.mff.cuni.cz/~pavel/picture/horses/blog.html