Revision: 117 http://svn.sourceforge.net/mactel-linux/?rev=117&view=rev Author: nboichat Date: 2007-04-15 20:16:51 -0700 (Sun, 15 Apr 2007)
Log Message: ----------- Fix crash when led triggers are activated on the keyboard backlight. Added Paths: ----------- trunk/kernel/mactel-patches-2.6.21/0013-applesmc_fix_sleep_in_led_brightness_set.patch trunk/kernel/mactel-patches-2.6.21/0014-applesmc_int.patch Removed Paths: ------------- trunk/kernel/mactel-patches-2.6.21/0013-applesmc_int.patch Added: trunk/kernel/mactel-patches-2.6.21/0013-applesmc_fix_sleep_in_led_brightness_set.patch =================================================================== --- trunk/kernel/mactel-patches-2.6.21/0013-applesmc_fix_sleep_in_led_brightness_set.patch (rev 0) +++ trunk/kernel/mactel-patches-2.6.21/0013-applesmc_fix_sleep_in_led_brightness_set.patch 2007-04-16 03:16:51 UTC (rev 117) @@ -0,0 +1,158 @@ +Cannot sleep in led->brightness_set handler, as it might be called from a softirq, so we use a workqueue to change the brightness (as recommended by Richard Purdie) + +From: Nicolas Boichat <[EMAIL PROTECTED]> + + +--- + + drivers/hwmon/applesmc.c | 61 +++++++++++++++++++++++++++++++++++++--------- + 1 files changed, 49 insertions(+), 12 deletions(-) + +diff --git a/drivers/hwmon/applesmc.c b/drivers/hwmon/applesmc.c +index 4ec38ef..ea0a004 100644 +--- a/drivers/hwmon/applesmc.c ++++ b/drivers/hwmon/applesmc.c +@@ -38,6 +38,7 @@ + #include <asm/io.h> + #include <linux/leds.h> + #include <linux/hwmon.h> ++#include <linux/workqueue.h> + + /* data port used by Apple SMC */ + #define APPLESMC_DATA_PORT 0x300 +@@ -116,7 +117,7 @@ struct dmi_match_data { + int temperature_set; + }; + +-static int debug = 0; ++static const int debug = 0; + static struct platform_device *pdev; + static s16 rest_x; + static s16 rest_y; +@@ -141,8 +142,10 @@ static struct mutex applesmc_lock; + */ + static unsigned int key_at_index; + ++static struct workqueue_struct *applesmc_led_wq; ++ + /* +- * __wait_status - Wait up to 100ms for the status port to get a certain value ++ * __wait_status - Wait up to 2ms for the status port to get a certain value + * (masked with 0x0f), returning zero if the value is obtained. Callers must + * hold applesmc_lock. + */ +@@ -152,9 +155,14 @@ static int __wait_status(u8 val) + + val = val & APPLESMC_STATUS_MASK; + +- for (i = 0; i < 10000; i++) { +- if ((inb(APPLESMC_CMD_PORT) & APPLESMC_STATUS_MASK) == val) ++ for (i = 0; i < 200; i++) { ++ if ((inb(APPLESMC_CMD_PORT) & APPLESMC_STATUS_MASK) == val) { ++ if (debug) ++ printk(KERN_DEBUG ++ "Waited %d us for status %x\n", ++ i*10, val); + return 0; ++ } + udelay(10); + } + +@@ -721,17 +729,33 @@ static ssize_t applesmc_calibrate_store(struct device *dev, + return count; + } + +-static void applesmc_backlight_set(struct led_classdev *led_cdev, +- enum led_brightness value) ++/* Store the next backlight value to be written by the work */ ++static unsigned int backlight_value; ++ ++static void applesmc_backlight_set(struct work_struct *work) + { + u8 buffer[2]; + + mutex_lock(&applesmc_lock); +- buffer[0] = value; ++ buffer[0] = backlight_value; + buffer[1] = 0x00; + applesmc_write_key(BACKLIGHT_KEY, buffer, 2); + mutex_unlock(&applesmc_lock); + } ++DECLARE_WORK(backlight_work, &applesmc_backlight_set); ++ ++static void applesmc_brightness_set(struct led_classdev *led_cdev, ++ enum led_brightness value) ++{ ++ int ret; ++ ++ backlight_value = value; ++ ret = queue_work(applesmc_led_wq, &backlight_work); ++ ++ if (debug && (!ret)) { ++ printk(KERN_DEBUG "applesmc: work was already on the queue.\n"); ++ } ++} + + static ssize_t applesmc_key_count_show(struct device *dev, + struct device_attribute *attr, char *sysfsbuf) +@@ -887,7 +911,7 @@ static ssize_t applesmc_key_at_index_store(struct device *dev, + static struct led_classdev applesmc_backlight = { + .name = "smc:kbd_backlight", + .default_trigger = "nand-disk", +- .brightness_set = applesmc_backlight_set, ++ .brightness_set = applesmc_brightness_set, + }; + + static DEVICE_ATTR(position, 0444, applesmc_position_show, NULL); +@@ -1234,25 +1258,35 @@ static int __init applesmc_init(void) + if (ret) + goto out_accelerometer; + ++ /* Create the workqueue */ ++ applesmc_led_wq = create_singlethread_workqueue("applesmc-led"); ++ if (!applesmc_led_wq) { ++ ret = -ENOMEM; ++ goto out_light_sysfs; ++ } ++ + /* register as a led device */ + ret = led_classdev_register(&pdev->dev, &applesmc_backlight); + if (ret < 0) +- goto out_light_sysfs; ++ goto out_light_wq; + } + + hwmon_class_dev = hwmon_device_register(&pdev->dev); + if (IS_ERR(hwmon_class_dev)) { + ret = PTR_ERR(hwmon_class_dev); +- goto out_light; ++ goto out_light_ledclass; + } + + printk(KERN_INFO "applesmc: driver successfully loaded.\n"); + + return 0; + +-out_light: ++out_light_ledclass: + if (applesmc_light) + led_classdev_unregister(&applesmc_backlight); ++out_light_wq: ++ if (applesmc_light) ++ destroy_workqueue(applesmc_led_wq); + out_light_sysfs: + if (applesmc_light) + sysfs_remove_file(&pdev->dev.kobj, &dev_attr_light.attr); +@@ -1280,8 +1314,11 @@ out: + static void __exit applesmc_exit(void) + { + hwmon_device_unregister(hwmon_class_dev); +- if (applesmc_light) ++ if (applesmc_light) { + led_classdev_unregister(&applesmc_backlight); ++ destroy_workqueue(applesmc_led_wq); ++ sysfs_remove_file(&pdev->dev.kobj, &dev_attr_light.attr); ++ } + if (applesmc_accelerometer) + applesmc_release_accelerometer(); + sysfs_remove_group(&pdev->dev.kobj, &temperature_attributes_group); Deleted: trunk/kernel/mactel-patches-2.6.21/0013-applesmc_int.patch =================================================================== --- trunk/kernel/mactel-patches-2.6.21/0013-applesmc_int.patch 2007-04-16 03:11:46 UTC (rev 116) +++ trunk/kernel/mactel-patches-2.6.21/0013-applesmc_int.patch 2007-04-16 03:16:51 UTC (rev 117) @@ -1,410 +0,0 @@ -Interrupt support for the accelerometer. - -From: Nicolas Boichat <[EMAIL PROTECTED]> - - ---- - - drivers/hwmon/applesmc.c | 316 +++++++++++++++++++++++++++++++++++++++++++--- - 1 files changed, 293 insertions(+), 23 deletions(-) - -diff --git a/drivers/hwmon/applesmc.c b/drivers/hwmon/applesmc.c -index 4ec38ef..6ad1c2d 100644 ---- a/drivers/hwmon/applesmc.c -+++ b/drivers/hwmon/applesmc.c -@@ -38,14 +38,20 @@ - #include <asm/io.h> - #include <linux/leds.h> - #include <linux/hwmon.h> -+#include <linux/interrupt.h> - - /* data port used by Apple SMC */ - #define APPLESMC_DATA_PORT 0x300 - /* command/status port used by Apple SMC */ - #define APPLESMC_CMD_PORT 0x304 -+/* status port used by Apple SMC to get which interrupt type just happened */ -+#define APPLESMC_INT_PORT 0x31f - - #define APPLESMC_NR_PORTS 32 /* 0x300-0x31f */ - -+/* Defined in ACPI DSDT table, should we read it from there? */ -+#define APPLESMC_IRQ 6 -+ - #define APPLESMC_MAX_DATA_LENGTH 32 - - #define APPLESMC_STATUS_MASK 0x0f -@@ -56,6 +62,8 @@ - - #define KEY_COUNT_KEY "#KEY" /* r-o ui32 */ - -+#define INTERRUPT_OK_KEY "NTOK" /* w-o ui8 */ -+ - #define LIGHT_SENSOR_LEFT_KEY "ALV0" /* r-o {alv (6 bytes) */ - #define LIGHT_SENSOR_RIGHT_KEY "ALV1" /* r-o {alv (6 bytes) */ - #define BACKLIGHT_KEY "LKSB" /* w-o {lkb (2 bytes) */ -@@ -67,6 +75,19 @@ - #define MOTION_SENSOR_Z_KEY "MO_Z" /* r-o sp78 (2 bytes) */ - #define MOTION_SENSOR_KEY "MOCN" /* r/w ui16 */ - -+/* -+ * Interrupt controls. -+ * If the norm of the position (sqrt(MO_X^2+MO_Y^2+MO_Z^2)) is smaller than -+ * MOLT (free fall), or bigger than MOHT (high acceleration) for longer than the -+ * value of MOLD (or MOHD), SMC will trigger an interrupt. -+ */ -+#define MOTION_LOW_NORM "MOLT" /* r/w sp78 (2 bytes) */ -+#define MOTION_HIGH_NORM "MOHT" /* r/w sp78 (2 bytes) */ -+#define MOTION_LOW_NORM_INTERVAL "MOLD" /* r/w ui8 */ -+#define MOTION_HIGH_NORM_INTERVAL "MOHD" /* r/w ui8 */ -+ -+#define MSDW_KEY "MSDW" /* r/w flag (1 byte) */ -+ - #define FANS_COUNT "FNum" /* r-o ui8 */ - #define FANS_MANUAL "FS! " /* r-w ui16 */ - #define FAN_ACTUAL_SPEED "F0Ac" /* r-o fpe2 (2 bytes) */ -@@ -340,12 +361,79 @@ static int applesmc_read_motion_sensor(int index, s16* value) - } - - /* -+ * applesmc_init_check_key_value - checks if a given key contains the bytes in -+ * buffer, if not, writes these bytes. -+ * In case of failure retry every INIT_WAIT_MSECS msec, and timeout if it -+ * waited more than INIT_TIMEOUT_MSECS in total. -+ * Returns zero on success or a negative error on failure. Callers must -+ * hold applesmc_lock. -+ */ -+static int applesmc_init_check_key_value(const char* key, u8* buffer, u8 len) -+{ -+ int total, ret, i, compare; -+ u8 rdbuffer[APPLESMC_MAX_DATA_LENGTH]; -+ -+ if (len > APPLESMC_MAX_DATA_LENGTH) { -+ printk(KERN_ERR "applesmc_init_check_key_value: cannot " -+ "read/write more than %d bytes", -+ APPLESMC_MAX_DATA_LENGTH); -+ return -EINVAL; -+ } -+ -+ for (total = INIT_TIMEOUT_MSECS; total > 0; total -= INIT_WAIT_MSECS) { -+ if (!(ret = applesmc_read_key(key, rdbuffer, len))) { -+ compare = 1; -+ for (i = 0; i < len; i++) { -+ if (rdbuffer[i] != buffer[i]) { -+ compare = 0; -+ break; -+ } -+ } -+ -+ if (compare) { -+ return 0; -+ } -+ } -+ ret = applesmc_write_key(key, buffer, len); -+ msleep(INIT_WAIT_MSECS); -+ } -+ -+ if (ret) -+ return ret; -+ else -+ return -EIO; -+} -+ -+irqreturn_t applesmc_irq_handler(int irq, void *dev_id) -+{ -+ u8 int_type = inb(APPLESMC_INT_PORT); -+ -+ switch (int_type) { -+ case 0x60: -+ printk("applesmc: received a free fall interrupt\n"); -+ break; -+ case 0x6f: -+ printk("applesmc: received a high acceleration interrupt\n"); -+ break; -+ case 0x80: -+ printk("applesmc: received a shock interrupt\n"); -+ break; -+ default: -+ printk("applesmc: received an unknown interrupt %x\n", int_type); -+ } -+ -+ return IRQ_NONE; -+} -+ -+/* - * applesmc_device_init - initialize the accelerometer. Returns zero on success - * and negative error code on failure. Can sleep. - */ - static int applesmc_device_init(void) - { -- int total, ret = -ENXIO; -+ int total; -+ int ret = -ENXIO; -+ int ret1, ret2; - u8 buffer[2]; - - if (!applesmc_accelerometer) -@@ -353,32 +441,79 @@ static int applesmc_device_init(void) - - mutex_lock(&applesmc_lock); - -+ /* Accept interrupts */ -+ buffer[0] = 0x01; - for (total = INIT_TIMEOUT_MSECS; total > 0; total -= INIT_WAIT_MSECS) { -- if (debug) -- printk(KERN_DEBUG "applesmc try %d\n", total); -- if (!applesmc_read_key(MOTION_SENSOR_KEY, buffer, 2) && -- (buffer[0] != 0x00 || buffer[1] != 0x00)) { -- if (total == INIT_TIMEOUT_MSECS) { -- printk(KERN_DEBUG "applesmc: device has" -- " already been initialized" -- " (0x%02x, 0x%02x).\n", -- buffer[0], buffer[1]); -- } else { -- printk(KERN_DEBUG "applesmc: device" -- " successfully initialized" -- " (0x%02x, 0x%02x).\n", -- buffer[0], buffer[1]); -- } -- ret = 0; -- goto out; -- } -- buffer[0] = 0xe0; -- buffer[1] = 0x00; -- applesmc_write_key(MOTION_SENSOR_KEY, buffer, 2); -+ ret1 = applesmc_write_key(INTERRUPT_OK_KEY, buffer, 1); -+ msleep(INIT_WAIT_MSECS); -+ -+ if (!ret1) -+ break; -+ } -+ if (ret1) -+ printk(KERN_WARNING "applesmc: Cannot set NTOK key, " -+ "will not receive interrupts.\n"); -+ -+ /* Setup interrupt controls. */ -+ buffer[0] = 20; /* 20 msecs */ -+ ret1 = applesmc_init_check_key_value(MOTION_LOW_NORM_INTERVAL, -+ buffer, 1); -+ -+ buffer[0] = 20; /* 20 msecs */ -+ ret2 = applesmc_init_check_key_value(MOTION_HIGH_NORM_INTERVAL, -+ buffer, 1); -+ -+ if (ret1 || ret2) { -+ printk(KERN_WARNING "applesmc: Cannot set motion sensor " -+ "interrupt interval, might not receive " -+ "some interrupts."); -+ } -+ -+ buffer[0] = 0x00; -+ buffer[1] = 0x60; -+ ret1 = applesmc_init_check_key_value(MOTION_LOW_NORM, buffer, 2); -+ -+ buffer[0] = 0x01; -+ buffer[1] = 0xc0; -+ ret2 = applesmc_init_check_key_value(MOTION_HIGH_NORM, buffer, 2); -+ -+ if (ret1 || ret2) { -+ printk(KERN_WARNING "applesmc: Cannot set motion sensor " -+ "min/max norm parameters, " -+ "might not receive some interrupts."); -+ } -+ -+ /* Mysterious key. */ -+ buffer[0] = 0x01; -+ for (total = INIT_TIMEOUT_MSECS; total > 0; total -= INIT_WAIT_MSECS) { -+ ret1 = applesmc_write_key(MSDW_KEY, buffer, 1); - msleep(INIT_WAIT_MSECS); -+ -+ if (!ret1) -+ break; -+ } -+ if (ret1) -+ printk(KERN_WARNING "applesmc: Cannot set MSDW key\n"); -+ -+ /* Initialize the device. */ -+ buffer[0] = 0xe0; -+ buffer[1] = 0xf8; -+ if (applesmc_init_check_key_value(MOTION_SENSOR_KEY, buffer, 2)) { -+ printk(KERN_WARNING "applesmc: failed to init " -+ "the accelerometer\n"); -+ goto out; - } - -- printk(KERN_WARNING "applesmc: failed to init the device\n"); -+ ret1 = request_irq(APPLESMC_IRQ, applesmc_irq_handler, IRQF_DISABLED, -+ "applesmc_irq_handler", NULL); -+ -+ if (ret1) { -+ printk(KERN_WARNING "applesmc: cannot setup irq handler\n"); -+ } -+ -+ printk(KERN_DEBUG "applesmc: accelerometer " -+ "successfully initialized.\n"); -+ ret = 0; - - out: - mutex_unlock(&applesmc_lock); -@@ -423,9 +558,16 @@ static int applesmc_resume(struct platform_device *dev) - return applesmc_device_init(); - } - -+static int applesmc_remove(struct platform_device *dev) -+{ -+ free_irq(6, NULL); -+ return 0; -+} -+ - static struct platform_driver applesmc_driver = { - .probe = applesmc_probe, - .resume = applesmc_resume, -+ .remove = applesmc_remove, - .driver = { - .name = "applesmc", - .owner = THIS_MODULE, -@@ -884,6 +1026,122 @@ static ssize_t applesmc_key_at_index_store(struct device *dev, - return count; - } - -+static ssize_t applesmc_accelerometer_show(struct device *dev, -+ struct device_attribute *attr, char *sysfsbuf) -+{ -+ int ret; -+ unsigned int value = 0; -+ u8 buffer[2]; -+ char* key; -+ int length; -+ struct sensor_device_attribute_2 *sensor_attr = -+ to_sensor_dev_attr_2(attr); -+ -+ switch(sensor_attr->index) { -+ case 0: -+ key = MOTION_LOW_NORM_INTERVAL; -+ length = 1; -+ break; -+ case 1: -+ key = MOTION_HIGH_NORM_INTERVAL; -+ length = 1; -+ break; -+ case 2: -+ key = MOTION_LOW_NORM; -+ length = 2; -+ break; -+ case 3: -+ key = MOTION_HIGH_NORM; -+ length = 2; -+ break; -+ default: -+ printk("Invalid index for applesmc_accelerometer_show"); -+ return -EINVAL; -+ } -+ -+ mutex_lock(&applesmc_lock); -+ -+ ret = applesmc_read_key(key, buffer, length); -+ if (length == 2) -+ value = ((unsigned int)buffer[0] << 8) | buffer[1]; -+ else if (length == 1) -+ value = buffer[0]; -+ else { -+ printk("Invalid length for applesmc_param_show"); -+ ret = -EINVAL; -+ } -+ -+ mutex_unlock(&applesmc_lock); -+ if (ret) -+ return ret; -+ else -+ return snprintf(sysfsbuf, PAGE_SIZE, "%u\n", value); -+} -+ -+static ssize_t applesmc_accelerometer_store(struct device *dev, -+ struct device_attribute *attr, -+ const char *sysfsbuf, size_t count) -+{ -+ int ret; -+ u32 value; -+ u8 buffer[2]; -+ char* key; -+ int length; -+ struct sensor_device_attribute_2 *sensor_attr = -+ to_sensor_dev_attr_2(attr); -+ -+ switch(sensor_attr->index) { -+ case 0: -+ key = MOTION_LOW_NORM_INTERVAL; -+ length = 1; -+ break; -+ case 1: -+ key = MOTION_HIGH_NORM_INTERVAL; -+ length = 1; -+ break; -+ case 2: -+ key = MOTION_LOW_NORM; -+ length = 2; -+ break; -+ case 3: -+ key = MOTION_HIGH_NORM; -+ length = 2; -+ break; -+ default: -+ printk("Invalid index for applesmc_accelerometer_show"); -+ return -EINVAL; -+ } -+ -+ value = simple_strtoul(sysfsbuf, NULL, 10); -+ -+ if (length == 2) { -+ if (value > 0xffff) -+ return -EINVAL; -+ -+ buffer[0] = (value >> 8) & 0xff; -+ buffer[1] = value & 0xff; -+ } else if (length == 1) { -+ if (value > 0xff) -+ return -EINVAL; -+ -+ buffer[0] = value & 0xff; -+ } else { -+ printk("Invalid length for applesmc_param_store"); -+ return -EINVAL; -+ } -+ -+ mutex_lock(&applesmc_lock); -+ -+ ret = applesmc_write_key(key, buffer, length); -+ -+ mutex_unlock(&applesmc_lock); -+ -+ if (ret) -+ return ret; -+ else -+ return count; -+} -+ - static struct led_classdev applesmc_backlight = { - .name = "smc:kbd_backlight", - .default_trigger = "nand-disk", -@@ -893,10 +1151,22 @@ static struct led_classdev applesmc_backlight = { - static DEVICE_ATTR(position, 0444, applesmc_position_show, NULL); - static DEVICE_ATTR(calibrate, 0644, - applesmc_calibrate_show, applesmc_calibrate_store); -+static SENSOR_DEVICE_ATTR(low_norm_trigger_interval, 0644, -+ applesmc_accelerometer_show, applesmc_accelerometer_store, 0); -+static SENSOR_DEVICE_ATTR(high_norm_trigger_interval, 0644, -+ applesmc_accelerometer_show, applesmc_accelerometer_store, 1); -+static SENSOR_DEVICE_ATTR(low_norm_trigger, 0644, -+ applesmc_accelerometer_show, applesmc_accelerometer_store, 2); -+static SENSOR_DEVICE_ATTR(high_norm_trigger, 0644, -+ applesmc_accelerometer_show, applesmc_accelerometer_store, 3); - - static struct attribute *accelerometer_attributes[] = { - &dev_attr_position.attr, - &dev_attr_calibrate.attr, -+ &sensor_dev_attr_low_norm_trigger.dev_attr.attr, -+ &sensor_dev_attr_high_norm_trigger.dev_attr.attr, -+ &sensor_dev_attr_low_norm_trigger_interval.dev_attr.attr, -+ &sensor_dev_attr_high_norm_trigger_interval.dev_attr.attr, - NULL - }; - Added: trunk/kernel/mactel-patches-2.6.21/0014-applesmc_int.patch =================================================================== --- trunk/kernel/mactel-patches-2.6.21/0014-applesmc_int.patch (rev 0) +++ trunk/kernel/mactel-patches-2.6.21/0014-applesmc_int.patch 2007-04-16 03:16:51 UTC (rev 117) @@ -0,0 +1,410 @@ +Interrupt support for the accelerometer. + +From: Nicolas Boichat <[EMAIL PROTECTED]> + + +--- + + drivers/hwmon/applesmc.c | 316 +++++++++++++++++++++++++++++++++++++++++++--- + 1 files changed, 293 insertions(+), 23 deletions(-) + +diff --git a/drivers/hwmon/applesmc.c b/drivers/hwmon/applesmc.c +index ea0a004..bf1b6e6 100644 +--- a/drivers/hwmon/applesmc.c ++++ b/drivers/hwmon/applesmc.c +@@ -39,14 +39,20 @@ + #include <linux/leds.h> + #include <linux/hwmon.h> + #include <linux/workqueue.h> ++#include <linux/interrupt.h> + + /* data port used by Apple SMC */ + #define APPLESMC_DATA_PORT 0x300 + /* command/status port used by Apple SMC */ + #define APPLESMC_CMD_PORT 0x304 ++/* status port used by Apple SMC to get which interrupt type just happened */ ++#define APPLESMC_INT_PORT 0x31f + + #define APPLESMC_NR_PORTS 32 /* 0x300-0x31f */ + ++/* Defined in ACPI DSDT table, should we read it from there? */ ++#define APPLESMC_IRQ 6 ++ + #define APPLESMC_MAX_DATA_LENGTH 32 + + #define APPLESMC_STATUS_MASK 0x0f +@@ -57,6 +63,8 @@ + + #define KEY_COUNT_KEY "#KEY" /* r-o ui32 */ + ++#define INTERRUPT_OK_KEY "NTOK" /* w-o ui8 */ ++ + #define LIGHT_SENSOR_LEFT_KEY "ALV0" /* r-o {alv (6 bytes) */ + #define LIGHT_SENSOR_RIGHT_KEY "ALV1" /* r-o {alv (6 bytes) */ + #define BACKLIGHT_KEY "LKSB" /* w-o {lkb (2 bytes) */ +@@ -68,6 +76,19 @@ + #define MOTION_SENSOR_Z_KEY "MO_Z" /* r-o sp78 (2 bytes) */ + #define MOTION_SENSOR_KEY "MOCN" /* r/w ui16 */ + ++/* ++ * Interrupt controls. ++ * If the norm of the position (sqrt(MO_X^2+MO_Y^2+MO_Z^2)) is smaller than ++ * MOLT (free fall), or bigger than MOHT (high acceleration) for longer than the ++ * value of MOLD (or MOHD), SMC will trigger an interrupt. ++ */ ++#define MOTION_LOW_NORM "MOLT" /* r/w sp78 (2 bytes) */ ++#define MOTION_HIGH_NORM "MOHT" /* r/w sp78 (2 bytes) */ ++#define MOTION_LOW_NORM_INTERVAL "MOLD" /* r/w ui8 */ ++#define MOTION_HIGH_NORM_INTERVAL "MOHD" /* r/w ui8 */ ++ ++#define MSDW_KEY "MSDW" /* r/w flag (1 byte) */ ++ + #define FANS_COUNT "FNum" /* r-o ui8 */ + #define FANS_MANUAL "FS! " /* r-w ui16 */ + #define FAN_ACTUAL_SPEED "F0Ac" /* r-o fpe2 (2 bytes) */ +@@ -348,12 +369,79 @@ static int applesmc_read_motion_sensor(int index, s16* value) + } + + /* ++ * applesmc_init_check_key_value - checks if a given key contains the bytes in ++ * buffer, if not, writes these bytes. ++ * In case of failure retry every INIT_WAIT_MSECS msec, and timeout if it ++ * waited more than INIT_TIMEOUT_MSECS in total. ++ * Returns zero on success or a negative error on failure. Callers must ++ * hold applesmc_lock. ++ */ ++static int applesmc_init_check_key_value(const char* key, u8* buffer, u8 len) ++{ ++ int total, ret, i, compare; ++ u8 rdbuffer[APPLESMC_MAX_DATA_LENGTH]; ++ ++ if (len > APPLESMC_MAX_DATA_LENGTH) { ++ printk(KERN_ERR "applesmc_init_check_key_value: cannot " ++ "read/write more than %d bytes", ++ APPLESMC_MAX_DATA_LENGTH); ++ return -EINVAL; ++ } ++ ++ for (total = INIT_TIMEOUT_MSECS; total > 0; total -= INIT_WAIT_MSECS) { ++ if (!(ret = applesmc_read_key(key, rdbuffer, len))) { ++ compare = 1; ++ for (i = 0; i < len; i++) { ++ if (rdbuffer[i] != buffer[i]) { ++ compare = 0; ++ break; ++ } ++ } ++ ++ if (compare) { ++ return 0; ++ } ++ } ++ ret = applesmc_write_key(key, buffer, len); ++ msleep(INIT_WAIT_MSECS); ++ } ++ ++ if (ret) ++ return ret; ++ else ++ return -EIO; ++} ++ ++irqreturn_t applesmc_irq_handler(int irq, void *dev_id) ++{ ++ u8 int_type = inb(APPLESMC_INT_PORT); ++ ++ switch (int_type) { ++ case 0x60: ++ printk("applesmc: received a free fall interrupt\n"); ++ break; ++ case 0x6f: ++ printk("applesmc: received a high acceleration interrupt\n"); ++ break; ++ case 0x80: ++ printk("applesmc: received a shock interrupt\n"); ++ break; ++ default: ++ printk("applesmc: received an unknown interrupt %x\n", int_type); ++ } ++ ++ return IRQ_NONE; ++} ++ ++/* + * applesmc_device_init - initialize the accelerometer. Returns zero on success + * and negative error code on failure. Can sleep. + */ + static int applesmc_device_init(void) + { +- int total, ret = -ENXIO; ++ int total; ++ int ret = -ENXIO; ++ int ret1, ret2; + u8 buffer[2]; + + if (!applesmc_accelerometer) +@@ -361,32 +449,79 @@ static int applesmc_device_init(void) + + mutex_lock(&applesmc_lock); + ++ /* Accept interrupts */ ++ buffer[0] = 0x01; + for (total = INIT_TIMEOUT_MSECS; total > 0; total -= INIT_WAIT_MSECS) { +- if (debug) +- printk(KERN_DEBUG "applesmc try %d\n", total); +- if (!applesmc_read_key(MOTION_SENSOR_KEY, buffer, 2) && +- (buffer[0] != 0x00 || buffer[1] != 0x00)) { +- if (total == INIT_TIMEOUT_MSECS) { +- printk(KERN_DEBUG "applesmc: device has" +- " already been initialized" +- " (0x%02x, 0x%02x).\n", +- buffer[0], buffer[1]); +- } else { +- printk(KERN_DEBUG "applesmc: device" +- " successfully initialized" +- " (0x%02x, 0x%02x).\n", +- buffer[0], buffer[1]); +- } +- ret = 0; +- goto out; +- } +- buffer[0] = 0xe0; +- buffer[1] = 0x00; +- applesmc_write_key(MOTION_SENSOR_KEY, buffer, 2); ++ ret1 = applesmc_write_key(INTERRUPT_OK_KEY, buffer, 1); ++ msleep(INIT_WAIT_MSECS); ++ ++ if (!ret1) ++ break; ++ } ++ if (ret1) ++ printk(KERN_WARNING "applesmc: Cannot set NTOK key, " ++ "will not receive interrupts.\n"); ++ ++ /* Setup interrupt controls. */ ++ buffer[0] = 20; /* 20 msecs */ ++ ret1 = applesmc_init_check_key_value(MOTION_LOW_NORM_INTERVAL, ++ buffer, 1); ++ ++ buffer[0] = 20; /* 20 msecs */ ++ ret2 = applesmc_init_check_key_value(MOTION_HIGH_NORM_INTERVAL, ++ buffer, 1); ++ ++ if (ret1 || ret2) { ++ printk(KERN_WARNING "applesmc: Cannot set motion sensor " ++ "interrupt interval, might not receive " ++ "some interrupts."); ++ } ++ ++ buffer[0] = 0x00; ++ buffer[1] = 0x60; ++ ret1 = applesmc_init_check_key_value(MOTION_LOW_NORM, buffer, 2); ++ ++ buffer[0] = 0x01; ++ buffer[1] = 0xc0; ++ ret2 = applesmc_init_check_key_value(MOTION_HIGH_NORM, buffer, 2); ++ ++ if (ret1 || ret2) { ++ printk(KERN_WARNING "applesmc: Cannot set motion sensor " ++ "min/max norm parameters, " ++ "might not receive some interrupts."); ++ } ++ ++ /* Mysterious key. */ ++ buffer[0] = 0x01; ++ for (total = INIT_TIMEOUT_MSECS; total > 0; total -= INIT_WAIT_MSECS) { ++ ret1 = applesmc_write_key(MSDW_KEY, buffer, 1); + msleep(INIT_WAIT_MSECS); ++ ++ if (!ret1) ++ break; ++ } ++ if (ret1) ++ printk(KERN_WARNING "applesmc: Cannot set MSDW key\n"); ++ ++ /* Initialize the device. */ ++ buffer[0] = 0xe0; ++ buffer[1] = 0xf8; ++ if (applesmc_init_check_key_value(MOTION_SENSOR_KEY, buffer, 2)) { ++ printk(KERN_WARNING "applesmc: failed to init " ++ "the accelerometer\n"); ++ goto out; + } + +- printk(KERN_WARNING "applesmc: failed to init the device\n"); ++ ret1 = request_irq(APPLESMC_IRQ, applesmc_irq_handler, IRQF_DISABLED, ++ "applesmc_irq_handler", NULL); ++ ++ if (ret1) { ++ printk(KERN_WARNING "applesmc: cannot setup irq handler\n"); ++ } ++ ++ printk(KERN_DEBUG "applesmc: accelerometer " ++ "successfully initialized.\n"); ++ ret = 0; + + out: + mutex_unlock(&applesmc_lock); +@@ -431,9 +566,16 @@ static int applesmc_resume(struct platform_device *dev) + return applesmc_device_init(); + } + ++static int applesmc_remove(struct platform_device *dev) ++{ ++ free_irq(6, NULL); ++ return 0; ++} ++ + static struct platform_driver applesmc_driver = { + .probe = applesmc_probe, + .resume = applesmc_resume, ++ .remove = applesmc_remove, + .driver = { + .name = "applesmc", + .owner = THIS_MODULE, +@@ -908,6 +1050,122 @@ static ssize_t applesmc_key_at_index_store(struct device *dev, + return count; + } + ++static ssize_t applesmc_accelerometer_show(struct device *dev, ++ struct device_attribute *attr, char *sysfsbuf) ++{ ++ int ret; ++ unsigned int value = 0; ++ u8 buffer[2]; ++ char* key; ++ int length; ++ struct sensor_device_attribute_2 *sensor_attr = ++ to_sensor_dev_attr_2(attr); ++ ++ switch(sensor_attr->index) { ++ case 0: ++ key = MOTION_LOW_NORM_INTERVAL; ++ length = 1; ++ break; ++ case 1: ++ key = MOTION_HIGH_NORM_INTERVAL; ++ length = 1; ++ break; ++ case 2: ++ key = MOTION_LOW_NORM; ++ length = 2; ++ break; ++ case 3: ++ key = MOTION_HIGH_NORM; ++ length = 2; ++ break; ++ default: ++ printk("Invalid index for applesmc_accelerometer_show"); ++ return -EINVAL; ++ } ++ ++ mutex_lock(&applesmc_lock); ++ ++ ret = applesmc_read_key(key, buffer, length); ++ if (length == 2) ++ value = ((unsigned int)buffer[0] << 8) | buffer[1]; ++ else if (length == 1) ++ value = buffer[0]; ++ else { ++ printk("Invalid length for applesmc_param_show"); ++ ret = -EINVAL; ++ } ++ ++ mutex_unlock(&applesmc_lock); ++ if (ret) ++ return ret; ++ else ++ return snprintf(sysfsbuf, PAGE_SIZE, "%u\n", value); ++} ++ ++static ssize_t applesmc_accelerometer_store(struct device *dev, ++ struct device_attribute *attr, ++ const char *sysfsbuf, size_t count) ++{ ++ int ret; ++ u32 value; ++ u8 buffer[2]; ++ char* key; ++ int length; ++ struct sensor_device_attribute_2 *sensor_attr = ++ to_sensor_dev_attr_2(attr); ++ ++ switch(sensor_attr->index) { ++ case 0: ++ key = MOTION_LOW_NORM_INTERVAL; ++ length = 1; ++ break; ++ case 1: ++ key = MOTION_HIGH_NORM_INTERVAL; ++ length = 1; ++ break; ++ case 2: ++ key = MOTION_LOW_NORM; ++ length = 2; ++ break; ++ case 3: ++ key = MOTION_HIGH_NORM; ++ length = 2; ++ break; ++ default: ++ printk("Invalid index for applesmc_accelerometer_show"); ++ return -EINVAL; ++ } ++ ++ value = simple_strtoul(sysfsbuf, NULL, 10); ++ ++ if (length == 2) { ++ if (value > 0xffff) ++ return -EINVAL; ++ ++ buffer[0] = (value >> 8) & 0xff; ++ buffer[1] = value & 0xff; ++ } else if (length == 1) { ++ if (value > 0xff) ++ return -EINVAL; ++ ++ buffer[0] = value & 0xff; ++ } else { ++ printk("Invalid length for applesmc_param_store"); ++ return -EINVAL; ++ } ++ ++ mutex_lock(&applesmc_lock); ++ ++ ret = applesmc_write_key(key, buffer, length); ++ ++ mutex_unlock(&applesmc_lock); ++ ++ if (ret) ++ return ret; ++ else ++ return count; ++} ++ + static struct led_classdev applesmc_backlight = { + .name = "smc:kbd_backlight", + .default_trigger = "nand-disk", +@@ -917,10 +1175,22 @@ static struct led_classdev applesmc_backlight = { + static DEVICE_ATTR(position, 0444, applesmc_position_show, NULL); + static DEVICE_ATTR(calibrate, 0644, + applesmc_calibrate_show, applesmc_calibrate_store); ++static SENSOR_DEVICE_ATTR(low_norm_trigger_interval, 0644, ++ applesmc_accelerometer_show, applesmc_accelerometer_store, 0); ++static SENSOR_DEVICE_ATTR(high_norm_trigger_interval, 0644, ++ applesmc_accelerometer_show, applesmc_accelerometer_store, 1); ++static SENSOR_DEVICE_ATTR(low_norm_trigger, 0644, ++ applesmc_accelerometer_show, applesmc_accelerometer_store, 2); ++static SENSOR_DEVICE_ATTR(high_norm_trigger, 0644, ++ applesmc_accelerometer_show, applesmc_accelerometer_store, 3); + + static struct attribute *accelerometer_attributes[] = { + &dev_attr_position.attr, + &dev_attr_calibrate.attr, ++ &sensor_dev_attr_low_norm_trigger.dev_attr.attr, ++ &sensor_dev_attr_high_norm_trigger.dev_attr.attr, ++ &sensor_dev_attr_low_norm_trigger_interval.dev_attr.attr, ++ &sensor_dev_attr_high_norm_trigger_interval.dev_attr.attr, + NULL + }; + This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. ------------------------------------------------------------------------- This SF.net email is sponsored by DB2 Express Download DB2 Express C - the FREE version of DB2 express and take control of your XML. No limits. Just data. Click to get it now. http://sourceforge.net/powerbar/db2/ _______________________________________________ Mactel-linux-devel mailing list Mactel-linux-devel@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/mactel-linux-devel