2007-05-08 15:24:52 +08:00
|
|
|
/*
|
|
|
|
* drivers/hwmon/applesmc.c - driver for Apple's SMC (accelerometer, temperature
|
|
|
|
* sensors, fan control, keyboard backlight control) used in Intel-based Apple
|
|
|
|
* computers.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2007 Nicolas Boichat <nicolas@boichat.ch>
|
2010-11-09 23:15:11 +08:00
|
|
|
* Copyright (C) 2010 Henrik Rydberg <rydberg@euromail.se>
|
2007-05-08 15:24:52 +08:00
|
|
|
*
|
|
|
|
* Based on hdaps.c driver:
|
|
|
|
* Copyright (C) 2005 Robert Love <rml@novell.com>
|
2012-06-21 03:18:48 +08:00
|
|
|
* Copyright (C) 2005 Jesper Juhl <jj@chaosbits.net>
|
2007-05-08 15:24:52 +08:00
|
|
|
*
|
|
|
|
* Fan control based on smcFanControl:
|
|
|
|
* Copyright (C) 2006 Hendrik Holtmann <holtmann@mac.com>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU General Public License v2 as published by the
|
|
|
|
* Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
|
|
|
* more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along with
|
|
|
|
* this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
|
|
|
*/
|
|
|
|
|
2010-11-09 23:15:03 +08:00
|
|
|
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
|
|
|
|
|
2007-05-08 15:24:52 +08:00
|
|
|
#include <linux/delay.h>
|
|
|
|
#include <linux/platform_device.h>
|
2007-09-26 12:01:35 +08:00
|
|
|
#include <linux/input-polldev.h>
|
2007-05-08 15:24:52 +08:00
|
|
|
#include <linux/kernel.h>
|
2010-11-10 18:58:03 +08:00
|
|
|
#include <linux/slab.h>
|
2007-05-08 15:24:52 +08:00
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/timer.h>
|
|
|
|
#include <linux/dmi.h>
|
|
|
|
#include <linux/mutex.h>
|
|
|
|
#include <linux/hwmon-sysfs.h>
|
2009-09-15 23:18:13 +08:00
|
|
|
#include <linux/io.h>
|
2007-05-08 15:24:52 +08:00
|
|
|
#include <linux/leds.h>
|
|
|
|
#include <linux/hwmon.h>
|
|
|
|
#include <linux/workqueue.h>
|
2012-10-10 21:25:56 +08:00
|
|
|
#include <linux/err.h>
|
2007-05-08 15:24:52 +08:00
|
|
|
|
|
|
|
/* data port used by Apple SMC */
|
|
|
|
#define APPLESMC_DATA_PORT 0x300
|
|
|
|
/* command/status port used by Apple SMC */
|
|
|
|
#define APPLESMC_CMD_PORT 0x304
|
|
|
|
|
|
|
|
#define APPLESMC_NR_PORTS 32 /* 0x300-0x31f */
|
|
|
|
|
|
|
|
#define APPLESMC_MAX_DATA_LENGTH 32
|
|
|
|
|
2012-09-17 23:49:55 +08:00
|
|
|
/* wait up to 128 ms for a status change. */
|
hwmon: (applesmc) Shorten minimum wait time
The 2012 series of MacBooks have a faster SMC, and the current driver
timings do not work at all. Tests show that decreasing the minimum
wait time, from 64 us to 16 us, works well. Since this is still larger
than the original minimum of 10 us used before 2008, there is nothing
inherently problematic with changing it. The fail frequency on older
machines seems to increase slightly, but not enough to be noticeable.
Tested on MBA11, MBA31, MBA5,2, MBP9,2.
The patch was originally written by adamski99 (ubuntuforums.org) and
later tested by janhouse (bbs.archlinux.org).
Signed-off-by: Henrik Rydberg <rydberg@euromail.se>
Signed-off-by: Guenter Roeck <linux@roeck-us.net>
2012-07-09 18:10:26 +08:00
|
|
|
#define APPLESMC_MIN_WAIT 0x0010
|
hwmon: (applesmc) Decode and act on read/write status codes
The behavior of the SMC has changed several times over the years,
causing read failures in the driver. It seems the problem can be
explained by a shift in SMC speed combined with improper action on
status codes.
We should first wait for the SMC to settle, which was the most
frequent response on the old slow machines. Then, if the SMC is busy,
we need to try again later by resending the command. This was the most
likely response until 2012. Now, with a shorter wait time, we are
again most likely to poll while the SMC is settling, and as a result
we see high failure rates on many old and new models.
With the distinction between busy and failure, we can also wait longer
before retrying, without sacrificing speed. This seems to bring
failures down to virtually zero on all models.
Tested on: MBA1,1 MBA3,1 MBA5,1 MBA5,2 MBP9,2
Tested-by: Adam Somerville <adamsomerville@gmail.com>
Tested-by: Hubert Eichner <hubert.georg.eichner@gmail.com>
Signed-off-by: Henrik Rydberg <rydberg@euromail.se>
Signed-off-by: Guenter Roeck <linux@roeck-us.net>
2012-07-28 02:12:46 +08:00
|
|
|
#define APPLESMC_RETRY_WAIT 0x0100
|
2012-09-17 23:49:55 +08:00
|
|
|
#define APPLESMC_MAX_WAIT 0x20000
|
2008-10-19 11:27:43 +08:00
|
|
|
|
2007-05-08 15:24:52 +08:00
|
|
|
#define APPLESMC_READ_CMD 0x10
|
|
|
|
#define APPLESMC_WRITE_CMD 0x11
|
|
|
|
#define APPLESMC_GET_KEY_BY_INDEX_CMD 0x12
|
|
|
|
#define APPLESMC_GET_KEY_TYPE_CMD 0x13
|
|
|
|
|
|
|
|
#define KEY_COUNT_KEY "#KEY" /* r-o ui32 */
|
|
|
|
|
2008-10-19 11:27:39 +08:00
|
|
|
#define LIGHT_SENSOR_LEFT_KEY "ALV0" /* r-o {alv (6-10 bytes) */
|
|
|
|
#define LIGHT_SENSOR_RIGHT_KEY "ALV1" /* r-o {alv (6-10 bytes) */
|
2007-09-26 12:01:35 +08:00
|
|
|
#define BACKLIGHT_KEY "LKSB" /* w-o {lkb (2 bytes) */
|
2007-05-08 15:24:52 +08:00
|
|
|
|
2007-09-26 12:01:35 +08:00
|
|
|
#define CLAMSHELL_KEY "MSLD" /* r-o ui8 (unused) */
|
2007-05-08 15:24:52 +08:00
|
|
|
|
|
|
|
#define MOTION_SENSOR_X_KEY "MO_X" /* r-o sp78 (2 bytes) */
|
|
|
|
#define MOTION_SENSOR_Y_KEY "MO_Y" /* r-o sp78 (2 bytes) */
|
|
|
|
#define MOTION_SENSOR_Z_KEY "MO_Z" /* r-o sp78 (2 bytes) */
|
|
|
|
#define MOTION_SENSOR_KEY "MOCN" /* r/w ui16 */
|
|
|
|
|
|
|
|
#define FANS_COUNT "FNum" /* r-o ui8 */
|
|
|
|
#define FANS_MANUAL "FS! " /* r-w ui16 */
|
2010-11-09 23:15:08 +08:00
|
|
|
#define FAN_ID_FMT "F%dID" /* r-o char[16] */
|
2007-05-08 15:24:52 +08:00
|
|
|
|
2012-07-16 15:18:10 +08:00
|
|
|
#define TEMP_SENSOR_TYPE "sp78"
|
|
|
|
|
2007-05-08 15:24:52 +08:00
|
|
|
/* List of keys used to read/write fan speeds */
|
2010-11-09 23:15:08 +08:00
|
|
|
static const char *const fan_speed_fmt[] = {
|
|
|
|
"F%dAc", /* actual speed */
|
|
|
|
"F%dMn", /* minimum speed (rw) */
|
|
|
|
"F%dMx", /* maximum speed */
|
|
|
|
"F%dSf", /* safe speed - not all models */
|
|
|
|
"F%dTg", /* target speed (manual: rw) */
|
2007-05-08 15:24:52 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
#define INIT_TIMEOUT_MSECS 5000 /* wait up to 5s for device init ... */
|
|
|
|
#define INIT_WAIT_MSECS 50 /* ... in 50ms increments */
|
|
|
|
|
2007-09-26 12:01:35 +08:00
|
|
|
#define APPLESMC_POLL_INTERVAL 50 /* msecs */
|
2007-05-08 15:24:52 +08:00
|
|
|
#define APPLESMC_INPUT_FUZZ 4 /* input event threshold */
|
|
|
|
#define APPLESMC_INPUT_FLAT 4
|
|
|
|
|
2010-11-09 23:15:08 +08:00
|
|
|
#define to_index(attr) (to_sensor_dev_attr(attr)->index & 0xffff)
|
|
|
|
#define to_option(attr) (to_sensor_dev_attr(attr)->index >> 16)
|
2010-11-10 18:58:04 +08:00
|
|
|
|
|
|
|
/* Dynamic device node attributes */
|
|
|
|
struct applesmc_dev_attr {
|
|
|
|
struct sensor_device_attribute sda; /* hwmon attributes */
|
|
|
|
char name[32]; /* room for node file name */
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Dynamic device node group */
|
|
|
|
struct applesmc_node_group {
|
|
|
|
char *format; /* format string */
|
|
|
|
void *show; /* show function */
|
|
|
|
void *store; /* store function */
|
2010-11-09 23:15:08 +08:00
|
|
|
int option; /* function argument */
|
2010-11-10 18:58:04 +08:00
|
|
|
struct applesmc_dev_attr *nodes; /* dynamic node array */
|
|
|
|
};
|
|
|
|
|
2010-11-10 18:58:03 +08:00
|
|
|
/* AppleSMC entry - cached register information */
|
|
|
|
struct applesmc_entry {
|
|
|
|
char key[5]; /* four-letter key code */
|
|
|
|
u8 valid; /* set when entry is successfully read once */
|
|
|
|
u8 len; /* bounded by APPLESMC_MAX_DATA_LENGTH */
|
|
|
|
char type[5]; /* four-letter type code */
|
|
|
|
u8 flags; /* 0x10: func; 0x40: write; 0x80: read */
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Register lookup and registers common to all SMCs */
|
|
|
|
static struct applesmc_registers {
|
|
|
|
struct mutex mutex; /* register read/write mutex */
|
|
|
|
unsigned int key_count; /* number of SMC registers */
|
2010-11-09 23:15:08 +08:00
|
|
|
unsigned int fan_count; /* number of fans */
|
2010-11-10 18:58:04 +08:00
|
|
|
unsigned int temp_count; /* number of temperature registers */
|
|
|
|
unsigned int temp_begin; /* temperature lower index bound */
|
|
|
|
unsigned int temp_end; /* temperature upper index bound */
|
2012-07-16 15:18:11 +08:00
|
|
|
unsigned int index_count; /* size of temperature index array */
|
2010-11-10 18:58:05 +08:00
|
|
|
int num_light_sensors; /* number of light sensors */
|
|
|
|
bool has_accelerometer; /* has motion sensor */
|
|
|
|
bool has_key_backlight; /* has keyboard backlight */
|
2010-11-10 18:58:03 +08:00
|
|
|
bool init_complete; /* true when fully initialized */
|
|
|
|
struct applesmc_entry *cache; /* cached key entries */
|
2012-07-16 15:18:11 +08:00
|
|
|
const char **index; /* temperature key index */
|
2010-11-10 18:58:03 +08:00
|
|
|
} smcreg = {
|
|
|
|
.mutex = __MUTEX_INITIALIZER(smcreg.mutex),
|
|
|
|
};
|
|
|
|
|
2007-05-08 15:24:52 +08:00
|
|
|
static const int debug;
|
|
|
|
static struct platform_device *pdev;
|
|
|
|
static s16 rest_x;
|
|
|
|
static s16 rest_y;
|
2009-09-22 08:04:50 +08:00
|
|
|
static u8 backlight_state[2];
|
|
|
|
|
2007-08-21 04:46:20 +08:00
|
|
|
static struct device *hwmon_dev;
|
2007-09-26 12:01:35 +08:00
|
|
|
static struct input_polled_dev *applesmc_idev;
|
2007-05-08 15:24:52 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Last index written to key_at_index sysfs file, and value to use for all other
|
|
|
|
* key_at_index_* sysfs files.
|
|
|
|
*/
|
|
|
|
static unsigned int key_at_index;
|
|
|
|
|
|
|
|
static struct workqueue_struct *applesmc_led_wq;
|
|
|
|
|
|
|
|
/*
|
hwmon: (applesmc) Decode and act on read/write status codes
The behavior of the SMC has changed several times over the years,
causing read failures in the driver. It seems the problem can be
explained by a shift in SMC speed combined with improper action on
status codes.
We should first wait for the SMC to settle, which was the most
frequent response on the old slow machines. Then, if the SMC is busy,
we need to try again later by resending the command. This was the most
likely response until 2012. Now, with a shorter wait time, we are
again most likely to poll while the SMC is settling, and as a result
we see high failure rates on many old and new models.
With the distinction between busy and failure, we can also wait longer
before retrying, without sacrificing speed. This seems to bring
failures down to virtually zero on all models.
Tested on: MBA1,1 MBA3,1 MBA5,1 MBA5,2 MBP9,2
Tested-by: Adam Somerville <adamsomerville@gmail.com>
Tested-by: Hubert Eichner <hubert.georg.eichner@gmail.com>
Signed-off-by: Henrik Rydberg <rydberg@euromail.se>
Signed-off-by: Guenter Roeck <linux@roeck-us.net>
2012-07-28 02:12:46 +08:00
|
|
|
* wait_read - Wait for a byte to appear on SMC port. Callers must
|
2007-05-08 15:24:52 +08:00
|
|
|
* hold applesmc_lock.
|
|
|
|
*/
|
hwmon: (applesmc) Decode and act on read/write status codes
The behavior of the SMC has changed several times over the years,
causing read failures in the driver. It seems the problem can be
explained by a shift in SMC speed combined with improper action on
status codes.
We should first wait for the SMC to settle, which was the most
frequent response on the old slow machines. Then, if the SMC is busy,
we need to try again later by resending the command. This was the most
likely response until 2012. Now, with a shorter wait time, we are
again most likely to poll while the SMC is settling, and as a result
we see high failure rates on many old and new models.
With the distinction between busy and failure, we can also wait longer
before retrying, without sacrificing speed. This seems to bring
failures down to virtually zero on all models.
Tested on: MBA1,1 MBA3,1 MBA5,1 MBA5,2 MBP9,2
Tested-by: Adam Somerville <adamsomerville@gmail.com>
Tested-by: Hubert Eichner <hubert.georg.eichner@gmail.com>
Signed-off-by: Henrik Rydberg <rydberg@euromail.se>
Signed-off-by: Guenter Roeck <linux@roeck-us.net>
2012-07-28 02:12:46 +08:00
|
|
|
static int wait_read(void)
|
2007-05-08 15:24:52 +08:00
|
|
|
{
|
hwmon: (applesmc) Decode and act on read/write status codes
The behavior of the SMC has changed several times over the years,
causing read failures in the driver. It seems the problem can be
explained by a shift in SMC speed combined with improper action on
status codes.
We should first wait for the SMC to settle, which was the most
frequent response on the old slow machines. Then, if the SMC is busy,
we need to try again later by resending the command. This was the most
likely response until 2012. Now, with a shorter wait time, we are
again most likely to poll while the SMC is settling, and as a result
we see high failure rates on many old and new models.
With the distinction between busy and failure, we can also wait longer
before retrying, without sacrificing speed. This seems to bring
failures down to virtually zero on all models.
Tested on: MBA1,1 MBA3,1 MBA5,1 MBA5,2 MBP9,2
Tested-by: Adam Somerville <adamsomerville@gmail.com>
Tested-by: Hubert Eichner <hubert.georg.eichner@gmail.com>
Signed-off-by: Henrik Rydberg <rydberg@euromail.se>
Signed-off-by: Guenter Roeck <linux@roeck-us.net>
2012-07-28 02:12:46 +08:00
|
|
|
u8 status;
|
2008-10-19 11:27:43 +08:00
|
|
|
int us;
|
|
|
|
for (us = APPLESMC_MIN_WAIT; us < APPLESMC_MAX_WAIT; us <<= 1) {
|
|
|
|
udelay(us);
|
hwmon: (applesmc) Decode and act on read/write status codes
The behavior of the SMC has changed several times over the years,
causing read failures in the driver. It seems the problem can be
explained by a shift in SMC speed combined with improper action on
status codes.
We should first wait for the SMC to settle, which was the most
frequent response on the old slow machines. Then, if the SMC is busy,
we need to try again later by resending the command. This was the most
likely response until 2012. Now, with a shorter wait time, we are
again most likely to poll while the SMC is settling, and as a result
we see high failure rates on many old and new models.
With the distinction between busy and failure, we can also wait longer
before retrying, without sacrificing speed. This seems to bring
failures down to virtually zero on all models.
Tested on: MBA1,1 MBA3,1 MBA5,1 MBA5,2 MBP9,2
Tested-by: Adam Somerville <adamsomerville@gmail.com>
Tested-by: Hubert Eichner <hubert.georg.eichner@gmail.com>
Signed-off-by: Henrik Rydberg <rydberg@euromail.se>
Signed-off-by: Guenter Roeck <linux@roeck-us.net>
2012-07-28 02:12:46 +08:00
|
|
|
status = inb(APPLESMC_CMD_PORT);
|
|
|
|
/* read: wait for smc to settle */
|
|
|
|
if (status & 0x01)
|
2007-05-08 15:24:52 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
hwmon: (applesmc) Decode and act on read/write status codes
The behavior of the SMC has changed several times over the years,
causing read failures in the driver. It seems the problem can be
explained by a shift in SMC speed combined with improper action on
status codes.
We should first wait for the SMC to settle, which was the most
frequent response on the old slow machines. Then, if the SMC is busy,
we need to try again later by resending the command. This was the most
likely response until 2012. Now, with a shorter wait time, we are
again most likely to poll while the SMC is settling, and as a result
we see high failure rates on many old and new models.
With the distinction between busy and failure, we can also wait longer
before retrying, without sacrificing speed. This seems to bring
failures down to virtually zero on all models.
Tested on: MBA1,1 MBA3,1 MBA5,1 MBA5,2 MBP9,2
Tested-by: Adam Somerville <adamsomerville@gmail.com>
Tested-by: Hubert Eichner <hubert.georg.eichner@gmail.com>
Signed-off-by: Henrik Rydberg <rydberg@euromail.se>
Signed-off-by: Guenter Roeck <linux@roeck-us.net>
2012-07-28 02:12:46 +08:00
|
|
|
pr_warn("wait_read() fail: 0x%02x\n", status);
|
2007-05-08 15:24:52 +08:00
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
|
hwmon: applesmc: fix the 'wait status failed: c != 8' problem
On many Macbooks since mid 2007, the Pro, C2D and Air models, applesmc
fails to read some or all SMC ports. This problem has various effects,
such as flooded logfiles, malfunctioning temperature sensors,
accelerometers failing to initialize, and difficulties getting backlight
functionality to work properly.
The root of the problem seems to be the command protocol. The current
code sends out a command byte, then repeatedly polls for an ack before
continuing to send or recieve data. From experiments leading to this
patch, it seems the command protocol never quite worked or changed so that
one now sends a command byte, waits a little bit, polls for an ack, and if
it fails, repeats the whole thing by sending the command byte again.
This patch implements a send_command function according to the new
interpretation of the protocol, and should work also for earlier models.
Signed-off-by: Henrik Rydberg <rydberg@euromail.se>
Cc: Nicolas Boichat <nicolas@boichat.ch>
Cc: Riki Oktarianto <rkoktarianto@gmail.com>
Cc: Mark M. Hoffman <mhoffman@lightlink.com>
Cc: Jean Delvare <khali@linux-fr.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2008-10-19 11:27:38 +08:00
|
|
|
/*
|
hwmon: (applesmc) Decode and act on read/write status codes
The behavior of the SMC has changed several times over the years,
causing read failures in the driver. It seems the problem can be
explained by a shift in SMC speed combined with improper action on
status codes.
We should first wait for the SMC to settle, which was the most
frequent response on the old slow machines. Then, if the SMC is busy,
we need to try again later by resending the command. This was the most
likely response until 2012. Now, with a shorter wait time, we are
again most likely to poll while the SMC is settling, and as a result
we see high failure rates on many old and new models.
With the distinction between busy and failure, we can also wait longer
before retrying, without sacrificing speed. This seems to bring
failures down to virtually zero on all models.
Tested on: MBA1,1 MBA3,1 MBA5,1 MBA5,2 MBP9,2
Tested-by: Adam Somerville <adamsomerville@gmail.com>
Tested-by: Hubert Eichner <hubert.georg.eichner@gmail.com>
Signed-off-by: Henrik Rydberg <rydberg@euromail.se>
Signed-off-by: Guenter Roeck <linux@roeck-us.net>
2012-07-28 02:12:46 +08:00
|
|
|
* send_byte - Write to SMC port, retrying when necessary. Callers
|
|
|
|
* must hold applesmc_lock.
|
hwmon: applesmc: fix the 'wait status failed: c != 8' problem
On many Macbooks since mid 2007, the Pro, C2D and Air models, applesmc
fails to read some or all SMC ports. This problem has various effects,
such as flooded logfiles, malfunctioning temperature sensors,
accelerometers failing to initialize, and difficulties getting backlight
functionality to work properly.
The root of the problem seems to be the command protocol. The current
code sends out a command byte, then repeatedly polls for an ack before
continuing to send or recieve data. From experiments leading to this
patch, it seems the command protocol never quite worked or changed so that
one now sends a command byte, waits a little bit, polls for an ack, and if
it fails, repeats the whole thing by sending the command byte again.
This patch implements a send_command function according to the new
interpretation of the protocol, and should work also for earlier models.
Signed-off-by: Henrik Rydberg <rydberg@euromail.se>
Cc: Nicolas Boichat <nicolas@boichat.ch>
Cc: Riki Oktarianto <rkoktarianto@gmail.com>
Cc: Mark M. Hoffman <mhoffman@lightlink.com>
Cc: Jean Delvare <khali@linux-fr.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2008-10-19 11:27:38 +08:00
|
|
|
*/
|
hwmon: (applesmc) Decode and act on read/write status codes
The behavior of the SMC has changed several times over the years,
causing read failures in the driver. It seems the problem can be
explained by a shift in SMC speed combined with improper action on
status codes.
We should first wait for the SMC to settle, which was the most
frequent response on the old slow machines. Then, if the SMC is busy,
we need to try again later by resending the command. This was the most
likely response until 2012. Now, with a shorter wait time, we are
again most likely to poll while the SMC is settling, and as a result
we see high failure rates on many old and new models.
With the distinction between busy and failure, we can also wait longer
before retrying, without sacrificing speed. This seems to bring
failures down to virtually zero on all models.
Tested on: MBA1,1 MBA3,1 MBA5,1 MBA5,2 MBP9,2
Tested-by: Adam Somerville <adamsomerville@gmail.com>
Tested-by: Hubert Eichner <hubert.georg.eichner@gmail.com>
Signed-off-by: Henrik Rydberg <rydberg@euromail.se>
Signed-off-by: Guenter Roeck <linux@roeck-us.net>
2012-07-28 02:12:46 +08:00
|
|
|
static int send_byte(u8 cmd, u16 port)
|
hwmon: applesmc: fix the 'wait status failed: c != 8' problem
On many Macbooks since mid 2007, the Pro, C2D and Air models, applesmc
fails to read some or all SMC ports. This problem has various effects,
such as flooded logfiles, malfunctioning temperature sensors,
accelerometers failing to initialize, and difficulties getting backlight
functionality to work properly.
The root of the problem seems to be the command protocol. The current
code sends out a command byte, then repeatedly polls for an ack before
continuing to send or recieve data. From experiments leading to this
patch, it seems the command protocol never quite worked or changed so that
one now sends a command byte, waits a little bit, polls for an ack, and if
it fails, repeats the whole thing by sending the command byte again.
This patch implements a send_command function according to the new
interpretation of the protocol, and should work also for earlier models.
Signed-off-by: Henrik Rydberg <rydberg@euromail.se>
Cc: Nicolas Boichat <nicolas@boichat.ch>
Cc: Riki Oktarianto <rkoktarianto@gmail.com>
Cc: Mark M. Hoffman <mhoffman@lightlink.com>
Cc: Jean Delvare <khali@linux-fr.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2008-10-19 11:27:38 +08:00
|
|
|
{
|
hwmon: (applesmc) Decode and act on read/write status codes
The behavior of the SMC has changed several times over the years,
causing read failures in the driver. It seems the problem can be
explained by a shift in SMC speed combined with improper action on
status codes.
We should first wait for the SMC to settle, which was the most
frequent response on the old slow machines. Then, if the SMC is busy,
we need to try again later by resending the command. This was the most
likely response until 2012. Now, with a shorter wait time, we are
again most likely to poll while the SMC is settling, and as a result
we see high failure rates on many old and new models.
With the distinction between busy and failure, we can also wait longer
before retrying, without sacrificing speed. This seems to bring
failures down to virtually zero on all models.
Tested on: MBA1,1 MBA3,1 MBA5,1 MBA5,2 MBP9,2
Tested-by: Adam Somerville <adamsomerville@gmail.com>
Tested-by: Hubert Eichner <hubert.georg.eichner@gmail.com>
Signed-off-by: Henrik Rydberg <rydberg@euromail.se>
Signed-off-by: Guenter Roeck <linux@roeck-us.net>
2012-07-28 02:12:46 +08:00
|
|
|
u8 status;
|
2008-10-19 11:27:43 +08:00
|
|
|
int us;
|
hwmon: (applesmc) Decode and act on read/write status codes
The behavior of the SMC has changed several times over the years,
causing read failures in the driver. It seems the problem can be
explained by a shift in SMC speed combined with improper action on
status codes.
We should first wait for the SMC to settle, which was the most
frequent response on the old slow machines. Then, if the SMC is busy,
we need to try again later by resending the command. This was the most
likely response until 2012. Now, with a shorter wait time, we are
again most likely to poll while the SMC is settling, and as a result
we see high failure rates on many old and new models.
With the distinction between busy and failure, we can also wait longer
before retrying, without sacrificing speed. This seems to bring
failures down to virtually zero on all models.
Tested on: MBA1,1 MBA3,1 MBA5,1 MBA5,2 MBP9,2
Tested-by: Adam Somerville <adamsomerville@gmail.com>
Tested-by: Hubert Eichner <hubert.georg.eichner@gmail.com>
Signed-off-by: Henrik Rydberg <rydberg@euromail.se>
Signed-off-by: Guenter Roeck <linux@roeck-us.net>
2012-07-28 02:12:46 +08:00
|
|
|
|
|
|
|
outb(cmd, port);
|
2008-10-19 11:27:43 +08:00
|
|
|
for (us = APPLESMC_MIN_WAIT; us < APPLESMC_MAX_WAIT; us <<= 1) {
|
|
|
|
udelay(us);
|
hwmon: (applesmc) Decode and act on read/write status codes
The behavior of the SMC has changed several times over the years,
causing read failures in the driver. It seems the problem can be
explained by a shift in SMC speed combined with improper action on
status codes.
We should first wait for the SMC to settle, which was the most
frequent response on the old slow machines. Then, if the SMC is busy,
we need to try again later by resending the command. This was the most
likely response until 2012. Now, with a shorter wait time, we are
again most likely to poll while the SMC is settling, and as a result
we see high failure rates on many old and new models.
With the distinction between busy and failure, we can also wait longer
before retrying, without sacrificing speed. This seems to bring
failures down to virtually zero on all models.
Tested on: MBA1,1 MBA3,1 MBA5,1 MBA5,2 MBP9,2
Tested-by: Adam Somerville <adamsomerville@gmail.com>
Tested-by: Hubert Eichner <hubert.georg.eichner@gmail.com>
Signed-off-by: Henrik Rydberg <rydberg@euromail.se>
Signed-off-by: Guenter Roeck <linux@roeck-us.net>
2012-07-28 02:12:46 +08:00
|
|
|
status = inb(APPLESMC_CMD_PORT);
|
|
|
|
/* write: wait for smc to settle */
|
|
|
|
if (status & 0x02)
|
|
|
|
continue;
|
|
|
|
/* ready: cmd accepted, return */
|
|
|
|
if (status & 0x04)
|
hwmon: applesmc: fix the 'wait status failed: c != 8' problem
On many Macbooks since mid 2007, the Pro, C2D and Air models, applesmc
fails to read some or all SMC ports. This problem has various effects,
such as flooded logfiles, malfunctioning temperature sensors,
accelerometers failing to initialize, and difficulties getting backlight
functionality to work properly.
The root of the problem seems to be the command protocol. The current
code sends out a command byte, then repeatedly polls for an ack before
continuing to send or recieve data. From experiments leading to this
patch, it seems the command protocol never quite worked or changed so that
one now sends a command byte, waits a little bit, polls for an ack, and if
it fails, repeats the whole thing by sending the command byte again.
This patch implements a send_command function according to the new
interpretation of the protocol, and should work also for earlier models.
Signed-off-by: Henrik Rydberg <rydberg@euromail.se>
Cc: Nicolas Boichat <nicolas@boichat.ch>
Cc: Riki Oktarianto <rkoktarianto@gmail.com>
Cc: Mark M. Hoffman <mhoffman@lightlink.com>
Cc: Jean Delvare <khali@linux-fr.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2008-10-19 11:27:38 +08:00
|
|
|
return 0;
|
hwmon: (applesmc) Decode and act on read/write status codes
The behavior of the SMC has changed several times over the years,
causing read failures in the driver. It seems the problem can be
explained by a shift in SMC speed combined with improper action on
status codes.
We should first wait for the SMC to settle, which was the most
frequent response on the old slow machines. Then, if the SMC is busy,
we need to try again later by resending the command. This was the most
likely response until 2012. Now, with a shorter wait time, we are
again most likely to poll while the SMC is settling, and as a result
we see high failure rates on many old and new models.
With the distinction between busy and failure, we can also wait longer
before retrying, without sacrificing speed. This seems to bring
failures down to virtually zero on all models.
Tested on: MBA1,1 MBA3,1 MBA5,1 MBA5,2 MBP9,2
Tested-by: Adam Somerville <adamsomerville@gmail.com>
Tested-by: Hubert Eichner <hubert.georg.eichner@gmail.com>
Signed-off-by: Henrik Rydberg <rydberg@euromail.se>
Signed-off-by: Guenter Roeck <linux@roeck-us.net>
2012-07-28 02:12:46 +08:00
|
|
|
/* timeout: give up */
|
|
|
|
if (us << 1 == APPLESMC_MAX_WAIT)
|
|
|
|
break;
|
|
|
|
/* busy: long wait and resend */
|
|
|
|
udelay(APPLESMC_RETRY_WAIT);
|
|
|
|
outb(cmd, port);
|
hwmon: applesmc: fix the 'wait status failed: c != 8' problem
On many Macbooks since mid 2007, the Pro, C2D and Air models, applesmc
fails to read some or all SMC ports. This problem has various effects,
such as flooded logfiles, malfunctioning temperature sensors,
accelerometers failing to initialize, and difficulties getting backlight
functionality to work properly.
The root of the problem seems to be the command protocol. The current
code sends out a command byte, then repeatedly polls for an ack before
continuing to send or recieve data. From experiments leading to this
patch, it seems the command protocol never quite worked or changed so that
one now sends a command byte, waits a little bit, polls for an ack, and if
it fails, repeats the whole thing by sending the command byte again.
This patch implements a send_command function according to the new
interpretation of the protocol, and should work also for earlier models.
Signed-off-by: Henrik Rydberg <rydberg@euromail.se>
Cc: Nicolas Boichat <nicolas@boichat.ch>
Cc: Riki Oktarianto <rkoktarianto@gmail.com>
Cc: Mark M. Hoffman <mhoffman@lightlink.com>
Cc: Jean Delvare <khali@linux-fr.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2008-10-19 11:27:38 +08:00
|
|
|
}
|
hwmon: (applesmc) Decode and act on read/write status codes
The behavior of the SMC has changed several times over the years,
causing read failures in the driver. It seems the problem can be
explained by a shift in SMC speed combined with improper action on
status codes.
We should first wait for the SMC to settle, which was the most
frequent response on the old slow machines. Then, if the SMC is busy,
we need to try again later by resending the command. This was the most
likely response until 2012. Now, with a shorter wait time, we are
again most likely to poll while the SMC is settling, and as a result
we see high failure rates on many old and new models.
With the distinction between busy and failure, we can also wait longer
before retrying, without sacrificing speed. This seems to bring
failures down to virtually zero on all models.
Tested on: MBA1,1 MBA3,1 MBA5,1 MBA5,2 MBP9,2
Tested-by: Adam Somerville <adamsomerville@gmail.com>
Tested-by: Hubert Eichner <hubert.georg.eichner@gmail.com>
Signed-off-by: Henrik Rydberg <rydberg@euromail.se>
Signed-off-by: Guenter Roeck <linux@roeck-us.net>
2012-07-28 02:12:46 +08:00
|
|
|
|
|
|
|
pr_warn("send_byte(0x%02x, 0x%04x) fail: 0x%02x\n", cmd, port, status);
|
hwmon: applesmc: fix the 'wait status failed: c != 8' problem
On many Macbooks since mid 2007, the Pro, C2D and Air models, applesmc
fails to read some or all SMC ports. This problem has various effects,
such as flooded logfiles, malfunctioning temperature sensors,
accelerometers failing to initialize, and difficulties getting backlight
functionality to work properly.
The root of the problem seems to be the command protocol. The current
code sends out a command byte, then repeatedly polls for an ack before
continuing to send or recieve data. From experiments leading to this
patch, it seems the command protocol never quite worked or changed so that
one now sends a command byte, waits a little bit, polls for an ack, and if
it fails, repeats the whole thing by sending the command byte again.
This patch implements a send_command function according to the new
interpretation of the protocol, and should work also for earlier models.
Signed-off-by: Henrik Rydberg <rydberg@euromail.se>
Cc: Nicolas Boichat <nicolas@boichat.ch>
Cc: Riki Oktarianto <rkoktarianto@gmail.com>
Cc: Mark M. Hoffman <mhoffman@lightlink.com>
Cc: Jean Delvare <khali@linux-fr.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2008-10-19 11:27:38 +08:00
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
|
hwmon: (applesmc) Decode and act on read/write status codes
The behavior of the SMC has changed several times over the years,
causing read failures in the driver. It seems the problem can be
explained by a shift in SMC speed combined with improper action on
status codes.
We should first wait for the SMC to settle, which was the most
frequent response on the old slow machines. Then, if the SMC is busy,
we need to try again later by resending the command. This was the most
likely response until 2012. Now, with a shorter wait time, we are
again most likely to poll while the SMC is settling, and as a result
we see high failure rates on many old and new models.
With the distinction between busy and failure, we can also wait longer
before retrying, without sacrificing speed. This seems to bring
failures down to virtually zero on all models.
Tested on: MBA1,1 MBA3,1 MBA5,1 MBA5,2 MBP9,2
Tested-by: Adam Somerville <adamsomerville@gmail.com>
Tested-by: Hubert Eichner <hubert.georg.eichner@gmail.com>
Signed-off-by: Henrik Rydberg <rydberg@euromail.se>
Signed-off-by: Guenter Roeck <linux@roeck-us.net>
2012-07-28 02:12:46 +08:00
|
|
|
static int send_command(u8 cmd)
|
|
|
|
{
|
|
|
|
return send_byte(cmd, APPLESMC_CMD_PORT);
|
|
|
|
}
|
|
|
|
|
2010-11-10 18:58:03 +08:00
|
|
|
static int send_argument(const char *key)
|
2007-05-08 15:24:52 +08:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
hwmon: (applesmc) Decode and act on read/write status codes
The behavior of the SMC has changed several times over the years,
causing read failures in the driver. It seems the problem can be
explained by a shift in SMC speed combined with improper action on
status codes.
We should first wait for the SMC to settle, which was the most
frequent response on the old slow machines. Then, if the SMC is busy,
we need to try again later by resending the command. This was the most
likely response until 2012. Now, with a shorter wait time, we are
again most likely to poll while the SMC is settling, and as a result
we see high failure rates on many old and new models.
With the distinction between busy and failure, we can also wait longer
before retrying, without sacrificing speed. This seems to bring
failures down to virtually zero on all models.
Tested on: MBA1,1 MBA3,1 MBA5,1 MBA5,2 MBP9,2
Tested-by: Adam Somerville <adamsomerville@gmail.com>
Tested-by: Hubert Eichner <hubert.georg.eichner@gmail.com>
Signed-off-by: Henrik Rydberg <rydberg@euromail.se>
Signed-off-by: Guenter Roeck <linux@roeck-us.net>
2012-07-28 02:12:46 +08:00
|
|
|
for (i = 0; i < 4; i++)
|
|
|
|
if (send_byte(key[i], APPLESMC_DATA_PORT))
|
2007-05-08 15:24:52 +08:00
|
|
|
return -EIO;
|
2010-11-10 18:58:03 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int read_smc(u8 cmd, const char *key, u8 *buffer, u8 len)
|
|
|
|
{
|
hwmon: (applesmc) Always read until end of data
The crash reported and investigated in commit 5f4513 turned out to be
caused by a change to the read interface on newer (2012) SMCs.
Tests by Chris show that simply reading the data valid line is enough
for the problem to go away. Additional tests show that the newer SMCs
no longer wait for the number of requested bytes, but start sending
data right away. Apparently the number of bytes to read is no longer
specified as before, but instead found out by reading until end of
data. Failure to read until end of data confuses the state machine,
which eventually causes the crash.
As a remedy, assuming bit0 is the read valid line, make sure there is
nothing more to read before leaving the read function.
Tested to resolve the original problem, and runtested on MBA3,1,
MBP4,1, MBP8,2, MBP10,1, MBP10,2. The patch seems to have no effect on
machines before 2012.
Tested-by: Chris Murphy <chris@cmurf.com>
Cc: stable@vger.kernel.org
Signed-off-by: Henrik Rydberg <rydberg@euromail.se>
Signed-off-by: Guenter Roeck <linux@roeck-us.net>
2013-10-03 01:15:03 +08:00
|
|
|
u8 status, data = 0;
|
2010-11-10 18:58:03 +08:00
|
|
|
int i;
|
|
|
|
|
|
|
|
if (send_command(cmd) || send_argument(key)) {
|
2012-06-07 16:54:29 +08:00
|
|
|
pr_warn("%.4s: read arg fail\n", key);
|
2010-11-10 18:58:03 +08:00
|
|
|
return -EIO;
|
|
|
|
}
|
2007-05-08 15:24:52 +08:00
|
|
|
|
hwmon: (applesmc) Always read until end of data
The crash reported and investigated in commit 5f4513 turned out to be
caused by a change to the read interface on newer (2012) SMCs.
Tests by Chris show that simply reading the data valid line is enough
for the problem to go away. Additional tests show that the newer SMCs
no longer wait for the number of requested bytes, but start sending
data right away. Apparently the number of bytes to read is no longer
specified as before, but instead found out by reading until end of
data. Failure to read until end of data confuses the state machine,
which eventually causes the crash.
As a remedy, assuming bit0 is the read valid line, make sure there is
nothing more to read before leaving the read function.
Tested to resolve the original problem, and runtested on MBA3,1,
MBP4,1, MBP8,2, MBP10,1, MBP10,2. The patch seems to have no effect on
machines before 2012.
Tested-by: Chris Murphy <chris@cmurf.com>
Cc: stable@vger.kernel.org
Signed-off-by: Henrik Rydberg <rydberg@euromail.se>
Signed-off-by: Guenter Roeck <linux@roeck-us.net>
2013-10-03 01:15:03 +08:00
|
|
|
/* This has no effect on newer (2012) SMCs */
|
hwmon: (applesmc) Decode and act on read/write status codes
The behavior of the SMC has changed several times over the years,
causing read failures in the driver. It seems the problem can be
explained by a shift in SMC speed combined with improper action on
status codes.
We should first wait for the SMC to settle, which was the most
frequent response on the old slow machines. Then, if the SMC is busy,
we need to try again later by resending the command. This was the most
likely response until 2012. Now, with a shorter wait time, we are
again most likely to poll while the SMC is settling, and as a result
we see high failure rates on many old and new models.
With the distinction between busy and failure, we can also wait longer
before retrying, without sacrificing speed. This seems to bring
failures down to virtually zero on all models.
Tested on: MBA1,1 MBA3,1 MBA5,1 MBA5,2 MBP9,2
Tested-by: Adam Somerville <adamsomerville@gmail.com>
Tested-by: Hubert Eichner <hubert.georg.eichner@gmail.com>
Signed-off-by: Henrik Rydberg <rydberg@euromail.se>
Signed-off-by: Guenter Roeck <linux@roeck-us.net>
2012-07-28 02:12:46 +08:00
|
|
|
if (send_byte(len, APPLESMC_DATA_PORT)) {
|
|
|
|
pr_warn("%.4s: read len fail\n", key);
|
|
|
|
return -EIO;
|
|
|
|
}
|
2007-05-08 15:24:52 +08:00
|
|
|
|
|
|
|
for (i = 0; i < len; i++) {
|
hwmon: (applesmc) Decode and act on read/write status codes
The behavior of the SMC has changed several times over the years,
causing read failures in the driver. It seems the problem can be
explained by a shift in SMC speed combined with improper action on
status codes.
We should first wait for the SMC to settle, which was the most
frequent response on the old slow machines. Then, if the SMC is busy,
we need to try again later by resending the command. This was the most
likely response until 2012. Now, with a shorter wait time, we are
again most likely to poll while the SMC is settling, and as a result
we see high failure rates on many old and new models.
With the distinction between busy and failure, we can also wait longer
before retrying, without sacrificing speed. This seems to bring
failures down to virtually zero on all models.
Tested on: MBA1,1 MBA3,1 MBA5,1 MBA5,2 MBP9,2
Tested-by: Adam Somerville <adamsomerville@gmail.com>
Tested-by: Hubert Eichner <hubert.georg.eichner@gmail.com>
Signed-off-by: Henrik Rydberg <rydberg@euromail.se>
Signed-off-by: Guenter Roeck <linux@roeck-us.net>
2012-07-28 02:12:46 +08:00
|
|
|
if (wait_read()) {
|
|
|
|
pr_warn("%.4s: read data[%d] fail\n", key, i);
|
2007-05-08 15:24:52 +08:00
|
|
|
return -EIO;
|
2010-11-10 18:58:03 +08:00
|
|
|
}
|
2007-05-08 15:24:52 +08:00
|
|
|
buffer[i] = inb(APPLESMC_DATA_PORT);
|
|
|
|
}
|
|
|
|
|
hwmon: (applesmc) Always read until end of data
The crash reported and investigated in commit 5f4513 turned out to be
caused by a change to the read interface on newer (2012) SMCs.
Tests by Chris show that simply reading the data valid line is enough
for the problem to go away. Additional tests show that the newer SMCs
no longer wait for the number of requested bytes, but start sending
data right away. Apparently the number of bytes to read is no longer
specified as before, but instead found out by reading until end of
data. Failure to read until end of data confuses the state machine,
which eventually causes the crash.
As a remedy, assuming bit0 is the read valid line, make sure there is
nothing more to read before leaving the read function.
Tested to resolve the original problem, and runtested on MBA3,1,
MBP4,1, MBP8,2, MBP10,1, MBP10,2. The patch seems to have no effect on
machines before 2012.
Tested-by: Chris Murphy <chris@cmurf.com>
Cc: stable@vger.kernel.org
Signed-off-by: Henrik Rydberg <rydberg@euromail.se>
Signed-off-by: Guenter Roeck <linux@roeck-us.net>
2013-10-03 01:15:03 +08:00
|
|
|
/* Read the data port until bit0 is cleared */
|
|
|
|
for (i = 0; i < 16; i++) {
|
|
|
|
udelay(APPLESMC_MIN_WAIT);
|
|
|
|
status = inb(APPLESMC_CMD_PORT);
|
|
|
|
if (!(status & 0x01))
|
|
|
|
break;
|
|
|
|
data = inb(APPLESMC_DATA_PORT);
|
|
|
|
}
|
|
|
|
if (i)
|
|
|
|
pr_warn("flushed %d bytes, last value is: %d\n", i, data);
|
|
|
|
|
2007-05-08 15:24:52 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-11-10 18:58:03 +08:00
|
|
|
static int write_smc(u8 cmd, const char *key, const u8 *buffer, u8 len)
|
2007-05-08 15:24:52 +08:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2010-11-10 18:58:03 +08:00
|
|
|
if (send_command(cmd) || send_argument(key)) {
|
|
|
|
pr_warn("%s: write arg fail\n", key);
|
2007-05-08 15:24:52 +08:00
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
|
hwmon: (applesmc) Decode and act on read/write status codes
The behavior of the SMC has changed several times over the years,
causing read failures in the driver. It seems the problem can be
explained by a shift in SMC speed combined with improper action on
status codes.
We should first wait for the SMC to settle, which was the most
frequent response on the old slow machines. Then, if the SMC is busy,
we need to try again later by resending the command. This was the most
likely response until 2012. Now, with a shorter wait time, we are
again most likely to poll while the SMC is settling, and as a result
we see high failure rates on many old and new models.
With the distinction between busy and failure, we can also wait longer
before retrying, without sacrificing speed. This seems to bring
failures down to virtually zero on all models.
Tested on: MBA1,1 MBA3,1 MBA5,1 MBA5,2 MBP9,2
Tested-by: Adam Somerville <adamsomerville@gmail.com>
Tested-by: Hubert Eichner <hubert.georg.eichner@gmail.com>
Signed-off-by: Henrik Rydberg <rydberg@euromail.se>
Signed-off-by: Guenter Roeck <linux@roeck-us.net>
2012-07-28 02:12:46 +08:00
|
|
|
if (send_byte(len, APPLESMC_DATA_PORT)) {
|
|
|
|
pr_warn("%.4s: write len fail\n", key);
|
|
|
|
return -EIO;
|
|
|
|
}
|
2007-05-08 15:24:52 +08:00
|
|
|
|
|
|
|
for (i = 0; i < len; i++) {
|
hwmon: (applesmc) Decode and act on read/write status codes
The behavior of the SMC has changed several times over the years,
causing read failures in the driver. It seems the problem can be
explained by a shift in SMC speed combined with improper action on
status codes.
We should first wait for the SMC to settle, which was the most
frequent response on the old slow machines. Then, if the SMC is busy,
we need to try again later by resending the command. This was the most
likely response until 2012. Now, with a shorter wait time, we are
again most likely to poll while the SMC is settling, and as a result
we see high failure rates on many old and new models.
With the distinction between busy and failure, we can also wait longer
before retrying, without sacrificing speed. This seems to bring
failures down to virtually zero on all models.
Tested on: MBA1,1 MBA3,1 MBA5,1 MBA5,2 MBP9,2
Tested-by: Adam Somerville <adamsomerville@gmail.com>
Tested-by: Hubert Eichner <hubert.georg.eichner@gmail.com>
Signed-off-by: Henrik Rydberg <rydberg@euromail.se>
Signed-off-by: Guenter Roeck <linux@roeck-us.net>
2012-07-28 02:12:46 +08:00
|
|
|
if (send_byte(buffer[i], APPLESMC_DATA_PORT)) {
|
2010-11-10 18:58:03 +08:00
|
|
|
pr_warn("%s: write data fail\n", key);
|
2007-05-08 15:24:52 +08:00
|
|
|
return -EIO;
|
2010-11-10 18:58:03 +08:00
|
|
|
}
|
2007-05-08 15:24:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-11-10 18:58:03 +08:00
|
|
|
static int read_register_count(unsigned int *count)
|
|
|
|
{
|
|
|
|
__be32 be;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = read_smc(APPLESMC_READ_CMD, KEY_COUNT_KEY, (u8 *)&be, 4);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
*count = be32_to_cpu(be);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-05-08 15:24:52 +08:00
|
|
|
/*
|
2010-11-10 18:58:03 +08:00
|
|
|
* Serialized I/O
|
|
|
|
*
|
|
|
|
* Returns zero on success or a negative error on failure.
|
|
|
|
* All functions below are concurrency safe - callers should NOT hold lock.
|
2007-05-08 15:24:52 +08:00
|
|
|
*/
|
2010-11-10 18:58:03 +08:00
|
|
|
|
|
|
|
static int applesmc_read_entry(const struct applesmc_entry *entry,
|
|
|
|
u8 *buf, u8 len)
|
2007-05-08 15:24:52 +08:00
|
|
|
{
|
2010-11-10 18:58:03 +08:00
|
|
|
int ret;
|
2007-05-08 15:24:52 +08:00
|
|
|
|
2010-11-10 18:58:03 +08:00
|
|
|
if (entry->len != len)
|
|
|
|
return -EINVAL;
|
|
|
|
mutex_lock(&smcreg.mutex);
|
|
|
|
ret = read_smc(APPLESMC_READ_CMD, entry->key, buf, len);
|
|
|
|
mutex_unlock(&smcreg.mutex);
|
2007-05-08 15:24:52 +08:00
|
|
|
|
2010-11-10 18:58:03 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int applesmc_write_entry(const struct applesmc_entry *entry,
|
|
|
|
const u8 *buf, u8 len)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (entry->len != len)
|
|
|
|
return -EINVAL;
|
|
|
|
mutex_lock(&smcreg.mutex);
|
|
|
|
ret = write_smc(APPLESMC_WRITE_CMD, entry->key, buf, len);
|
|
|
|
mutex_unlock(&smcreg.mutex);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct applesmc_entry *applesmc_get_entry_by_index(int index)
|
|
|
|
{
|
|
|
|
struct applesmc_entry *cache = &smcreg.cache[index];
|
|
|
|
u8 key[4], info[6];
|
|
|
|
__be32 be;
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
if (cache->valid)
|
|
|
|
return cache;
|
|
|
|
|
|
|
|
mutex_lock(&smcreg.mutex);
|
|
|
|
|
|
|
|
if (cache->valid)
|
|
|
|
goto out;
|
|
|
|
be = cpu_to_be32(index);
|
|
|
|
ret = read_smc(APPLESMC_GET_KEY_BY_INDEX_CMD, (u8 *)&be, key, 4);
|
|
|
|
if (ret)
|
|
|
|
goto out;
|
|
|
|
ret = read_smc(APPLESMC_GET_KEY_TYPE_CMD, key, info, 6);
|
|
|
|
if (ret)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
memcpy(cache->key, key, 4);
|
|
|
|
cache->len = info[0];
|
|
|
|
memcpy(cache->type, &info[1], 4);
|
|
|
|
cache->flags = info[5];
|
|
|
|
cache->valid = 1;
|
|
|
|
|
|
|
|
out:
|
|
|
|
mutex_unlock(&smcreg.mutex);
|
|
|
|
if (ret)
|
|
|
|
return ERR_PTR(ret);
|
|
|
|
return cache;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int applesmc_get_lower_bound(unsigned int *lo, const char *key)
|
|
|
|
{
|
|
|
|
int begin = 0, end = smcreg.key_count;
|
|
|
|
const struct applesmc_entry *entry;
|
|
|
|
|
|
|
|
while (begin != end) {
|
|
|
|
int middle = begin + (end - begin) / 2;
|
|
|
|
entry = applesmc_get_entry_by_index(middle);
|
2012-01-26 19:08:41 +08:00
|
|
|
if (IS_ERR(entry)) {
|
|
|
|
*lo = 0;
|
2010-11-10 18:58:03 +08:00
|
|
|
return PTR_ERR(entry);
|
2012-01-26 19:08:41 +08:00
|
|
|
}
|
2010-11-10 18:58:03 +08:00
|
|
|
if (strcmp(entry->key, key) < 0)
|
|
|
|
begin = middle + 1;
|
|
|
|
else
|
|
|
|
end = middle;
|
2007-05-08 15:24:52 +08:00
|
|
|
}
|
|
|
|
|
2010-11-10 18:58:03 +08:00
|
|
|
*lo = begin;
|
|
|
|
return 0;
|
|
|
|
}
|
2007-05-08 15:24:52 +08:00
|
|
|
|
2010-11-10 18:58:03 +08:00
|
|
|
static int applesmc_get_upper_bound(unsigned int *hi, const char *key)
|
|
|
|
{
|
|
|
|
int begin = 0, end = smcreg.key_count;
|
|
|
|
const struct applesmc_entry *entry;
|
|
|
|
|
|
|
|
while (begin != end) {
|
|
|
|
int middle = begin + (end - begin) / 2;
|
|
|
|
entry = applesmc_get_entry_by_index(middle);
|
2012-01-26 19:08:41 +08:00
|
|
|
if (IS_ERR(entry)) {
|
|
|
|
*hi = smcreg.key_count;
|
2010-11-10 18:58:03 +08:00
|
|
|
return PTR_ERR(entry);
|
2012-01-26 19:08:41 +08:00
|
|
|
}
|
2010-11-10 18:58:03 +08:00
|
|
|
if (strcmp(key, entry->key) < 0)
|
|
|
|
end = middle;
|
|
|
|
else
|
|
|
|
begin = middle + 1;
|
2007-05-08 15:24:52 +08:00
|
|
|
}
|
|
|
|
|
2010-11-10 18:58:03 +08:00
|
|
|
*hi = begin;
|
2007-05-08 15:24:52 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-11-10 18:58:03 +08:00
|
|
|
static const struct applesmc_entry *applesmc_get_entry_by_key(const char *key)
|
2007-05-08 15:24:52 +08:00
|
|
|
{
|
2010-11-10 18:58:03 +08:00
|
|
|
int begin, end;
|
|
|
|
int ret;
|
2007-05-08 15:24:52 +08:00
|
|
|
|
2010-11-10 18:58:03 +08:00
|
|
|
ret = applesmc_get_lower_bound(&begin, key);
|
|
|
|
if (ret)
|
|
|
|
return ERR_PTR(ret);
|
|
|
|
ret = applesmc_get_upper_bound(&end, key);
|
|
|
|
if (ret)
|
|
|
|
return ERR_PTR(ret);
|
|
|
|
if (end - begin != 1)
|
|
|
|
return ERR_PTR(-EINVAL);
|
2007-05-08 15:24:52 +08:00
|
|
|
|
2010-11-10 18:58:03 +08:00
|
|
|
return applesmc_get_entry_by_index(begin);
|
|
|
|
}
|
2007-05-08 15:24:52 +08:00
|
|
|
|
2010-11-10 18:58:03 +08:00
|
|
|
static int applesmc_read_key(const char *key, u8 *buffer, u8 len)
|
|
|
|
{
|
|
|
|
const struct applesmc_entry *entry;
|
2007-05-08 15:24:52 +08:00
|
|
|
|
2010-11-10 18:58:03 +08:00
|
|
|
entry = applesmc_get_entry_by_key(key);
|
|
|
|
if (IS_ERR(entry))
|
|
|
|
return PTR_ERR(entry);
|
2007-05-08 15:24:52 +08:00
|
|
|
|
2010-11-10 18:58:03 +08:00
|
|
|
return applesmc_read_entry(entry, buffer, len);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int applesmc_write_key(const char *key, const u8 *buffer, u8 len)
|
|
|
|
{
|
|
|
|
const struct applesmc_entry *entry;
|
|
|
|
|
|
|
|
entry = applesmc_get_entry_by_key(key);
|
|
|
|
if (IS_ERR(entry))
|
|
|
|
return PTR_ERR(entry);
|
|
|
|
|
|
|
|
return applesmc_write_entry(entry, buffer, len);
|
2007-05-08 15:24:52 +08:00
|
|
|
}
|
|
|
|
|
2010-11-10 18:58:05 +08:00
|
|
|
static int applesmc_has_key(const char *key, bool *value)
|
|
|
|
{
|
|
|
|
const struct applesmc_entry *entry;
|
|
|
|
|
|
|
|
entry = applesmc_get_entry_by_key(key);
|
|
|
|
if (IS_ERR(entry) && PTR_ERR(entry) != -EINVAL)
|
|
|
|
return PTR_ERR(entry);
|
|
|
|
|
|
|
|
*value = !IS_ERR(entry);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-05-08 15:24:52 +08:00
|
|
|
/*
|
2012-06-21 00:00:06 +08:00
|
|
|
* applesmc_read_s16 - Read 16-bit signed big endian register
|
2007-05-08 15:24:52 +08:00
|
|
|
*/
|
2012-06-21 00:00:06 +08:00
|
|
|
static int applesmc_read_s16(const char *key, s16 *value)
|
2007-05-08 15:24:52 +08:00
|
|
|
{
|
|
|
|
u8 buffer[2];
|
|
|
|
int ret;
|
|
|
|
|
2012-06-21 00:00:06 +08:00
|
|
|
ret = applesmc_read_key(key, buffer, 2);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
2007-05-08 15:24:52 +08:00
|
|
|
|
|
|
|
*value = ((s16)buffer[0] << 8) | buffer[1];
|
2012-06-21 00:00:06 +08:00
|
|
|
return 0;
|
2007-05-08 15:24:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2010-11-09 23:15:02 +08:00
|
|
|
* applesmc_device_init - initialize the accelerometer. Can sleep.
|
2007-05-08 15:24:52 +08:00
|
|
|
*/
|
2010-11-09 23:15:02 +08:00
|
|
|
static void applesmc_device_init(void)
|
2007-05-08 15:24:52 +08:00
|
|
|
{
|
2010-11-09 23:15:02 +08:00
|
|
|
int total;
|
2007-05-08 15:24:52 +08:00
|
|
|
u8 buffer[2];
|
|
|
|
|
2010-11-10 18:58:05 +08:00
|
|
|
if (!smcreg.has_accelerometer)
|
2010-11-09 23:15:02 +08:00
|
|
|
return;
|
2007-05-08 15:24:52 +08:00
|
|
|
|
|
|
|
for (total = INIT_TIMEOUT_MSECS; total > 0; total -= INIT_WAIT_MSECS) {
|
|
|
|
if (!applesmc_read_key(MOTION_SENSOR_KEY, buffer, 2) &&
|
2010-11-09 23:15:02 +08:00
|
|
|
(buffer[0] != 0x00 || buffer[1] != 0x00))
|
2010-11-10 18:58:03 +08:00
|
|
|
return;
|
2007-05-08 15:24:52 +08:00
|
|
|
buffer[0] = 0xe0;
|
|
|
|
buffer[1] = 0x00;
|
|
|
|
applesmc_write_key(MOTION_SENSOR_KEY, buffer, 2);
|
|
|
|
msleep(INIT_WAIT_MSECS);
|
|
|
|
}
|
|
|
|
|
2010-11-09 23:15:03 +08:00
|
|
|
pr_warn("failed to init the device\n");
|
2007-05-08 15:24:52 +08:00
|
|
|
}
|
|
|
|
|
2012-07-16 15:18:11 +08:00
|
|
|
static int applesmc_init_index(struct applesmc_registers *s)
|
|
|
|
{
|
|
|
|
const struct applesmc_entry *entry;
|
|
|
|
unsigned int i;
|
|
|
|
|
|
|
|
if (s->index)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
s->index = kcalloc(s->temp_count, sizeof(s->index[0]), GFP_KERNEL);
|
|
|
|
if (!s->index)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
for (i = s->temp_begin; i < s->temp_end; i++) {
|
|
|
|
entry = applesmc_get_entry_by_index(i);
|
|
|
|
if (IS_ERR(entry))
|
|
|
|
continue;
|
|
|
|
if (strcmp(entry->type, TEMP_SENSOR_TYPE))
|
|
|
|
continue;
|
|
|
|
s->index[s->index_count++] = entry->key;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-11-10 18:58:03 +08:00
|
|
|
/*
|
|
|
|
* applesmc_init_smcreg_try - Try to initialize register cache. Idempotent.
|
|
|
|
*/
|
|
|
|
static int applesmc_init_smcreg_try(void)
|
|
|
|
{
|
|
|
|
struct applesmc_registers *s = &smcreg;
|
2010-11-10 18:58:05 +08:00
|
|
|
bool left_light_sensor, right_light_sensor;
|
2013-09-26 14:33:16 +08:00
|
|
|
unsigned int count;
|
2010-11-09 23:15:08 +08:00
|
|
|
u8 tmp[1];
|
2010-11-10 18:58:03 +08:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (s->init_complete)
|
|
|
|
return 0;
|
|
|
|
|
2013-09-26 14:33:16 +08:00
|
|
|
ret = read_register_count(&count);
|
2010-11-10 18:58:03 +08:00
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
2013-09-26 14:33:16 +08:00
|
|
|
if (s->cache && s->key_count != count) {
|
|
|
|
pr_warn("key count changed from %d to %d\n",
|
|
|
|
s->key_count, count);
|
|
|
|
kfree(s->cache);
|
|
|
|
s->cache = NULL;
|
|
|
|
}
|
|
|
|
s->key_count = count;
|
|
|
|
|
2010-11-10 18:58:03 +08:00
|
|
|
if (!s->cache)
|
|
|
|
s->cache = kcalloc(s->key_count, sizeof(*s->cache), GFP_KERNEL);
|
|
|
|
if (!s->cache)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
2010-11-09 23:15:08 +08:00
|
|
|
ret = applesmc_read_key(FANS_COUNT, tmp, 1);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
s->fan_count = tmp[0];
|
|
|
|
|
2010-11-10 18:58:04 +08:00
|
|
|
ret = applesmc_get_lower_bound(&s->temp_begin, "T");
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
ret = applesmc_get_lower_bound(&s->temp_end, "U");
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
s->temp_count = s->temp_end - s->temp_begin;
|
|
|
|
|
2012-07-16 15:18:11 +08:00
|
|
|
ret = applesmc_init_index(s);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
2010-11-10 18:58:05 +08:00
|
|
|
ret = applesmc_has_key(LIGHT_SENSOR_LEFT_KEY, &left_light_sensor);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
ret = applesmc_has_key(LIGHT_SENSOR_RIGHT_KEY, &right_light_sensor);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
ret = applesmc_has_key(MOTION_SENSOR_KEY, &s->has_accelerometer);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
ret = applesmc_has_key(BACKLIGHT_KEY, &s->has_key_backlight);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
s->num_light_sensors = left_light_sensor + right_light_sensor;
|
2010-11-10 18:58:03 +08:00
|
|
|
s->init_complete = true;
|
|
|
|
|
2012-07-16 15:18:11 +08:00
|
|
|
pr_info("key=%d fan=%d temp=%d index=%d acc=%d lux=%d kbd=%d\n",
|
|
|
|
s->key_count, s->fan_count, s->temp_count, s->index_count,
|
2010-11-10 18:58:05 +08:00
|
|
|
s->has_accelerometer,
|
|
|
|
s->num_light_sensors,
|
|
|
|
s->has_key_backlight);
|
2010-11-10 18:58:03 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-07-16 15:18:11 +08:00
|
|
|
static void applesmc_destroy_smcreg(void)
|
|
|
|
{
|
|
|
|
kfree(smcreg.index);
|
|
|
|
smcreg.index = NULL;
|
|
|
|
kfree(smcreg.cache);
|
|
|
|
smcreg.cache = NULL;
|
|
|
|
smcreg.init_complete = false;
|
|
|
|
}
|
|
|
|
|
2010-11-10 18:58:03 +08:00
|
|
|
/*
|
|
|
|
* applesmc_init_smcreg - Initialize register cache.
|
|
|
|
*
|
|
|
|
* Retries until initialization is successful, or the operation times out.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
static int applesmc_init_smcreg(void)
|
|
|
|
{
|
|
|
|
int ms, ret;
|
|
|
|
|
|
|
|
for (ms = 0; ms < INIT_TIMEOUT_MSECS; ms += INIT_WAIT_MSECS) {
|
|
|
|
ret = applesmc_init_smcreg_try();
|
|
|
|
if (!ret) {
|
|
|
|
if (ms)
|
|
|
|
pr_info("init_smcreg() took %d ms\n", ms);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
msleep(INIT_WAIT_MSECS);
|
|
|
|
}
|
|
|
|
|
2012-07-16 15:18:11 +08:00
|
|
|
applesmc_destroy_smcreg();
|
2010-11-10 18:58:03 +08:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2007-05-08 15:24:52 +08:00
|
|
|
/* Device model stuff */
|
|
|
|
static int applesmc_probe(struct platform_device *dev)
|
|
|
|
{
|
2010-11-10 18:58:03 +08:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = applesmc_init_smcreg();
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
2010-11-09 23:15:02 +08:00
|
|
|
applesmc_device_init();
|
2007-05-08 15:24:52 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-09-22 08:04:50 +08:00
|
|
|
/* Synchronize device with memorized backlight state */
|
|
|
|
static int applesmc_pm_resume(struct device *dev)
|
|
|
|
{
|
2010-11-10 18:58:05 +08:00
|
|
|
if (smcreg.has_key_backlight)
|
2009-09-22 08:04:50 +08:00
|
|
|
applesmc_write_key(BACKLIGHT_KEY, backlight_state, 2);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Reinitialize device on resume from hibernation */
|
|
|
|
static int applesmc_pm_restore(struct device *dev)
|
2007-05-08 15:24:52 +08:00
|
|
|
{
|
2010-11-09 23:15:02 +08:00
|
|
|
applesmc_device_init();
|
2009-09-22 08:04:50 +08:00
|
|
|
return applesmc_pm_resume(dev);
|
2007-05-08 15:24:52 +08:00
|
|
|
}
|
|
|
|
|
2009-12-15 10:00:08 +08:00
|
|
|
static const struct dev_pm_ops applesmc_pm_ops = {
|
2009-09-22 08:04:50 +08:00
|
|
|
.resume = applesmc_pm_resume,
|
|
|
|
.restore = applesmc_pm_restore,
|
|
|
|
};
|
|
|
|
|
2007-05-08 15:24:52 +08:00
|
|
|
static struct platform_driver applesmc_driver = {
|
|
|
|
.probe = applesmc_probe,
|
|
|
|
.driver = {
|
|
|
|
.name = "applesmc",
|
2009-09-22 08:04:50 +08:00
|
|
|
.pm = &applesmc_pm_ops,
|
2007-05-08 15:24:52 +08:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* applesmc_calibrate - Set our "resting" values. Callers must
|
|
|
|
* hold applesmc_lock.
|
|
|
|
*/
|
|
|
|
static void applesmc_calibrate(void)
|
|
|
|
{
|
2012-06-21 00:00:06 +08:00
|
|
|
applesmc_read_s16(MOTION_SENSOR_X_KEY, &rest_x);
|
|
|
|
applesmc_read_s16(MOTION_SENSOR_Y_KEY, &rest_y);
|
2007-05-08 15:24:52 +08:00
|
|
|
rest_x = -rest_x;
|
|
|
|
}
|
|
|
|
|
2007-09-26 12:01:35 +08:00
|
|
|
static void applesmc_idev_poll(struct input_polled_dev *dev)
|
2007-05-08 15:24:52 +08:00
|
|
|
{
|
2007-09-26 12:01:35 +08:00
|
|
|
struct input_dev *idev = dev->input;
|
2007-05-08 15:24:52 +08:00
|
|
|
s16 x, y;
|
|
|
|
|
2012-06-21 00:00:06 +08:00
|
|
|
if (applesmc_read_s16(MOTION_SENSOR_X_KEY, &x))
|
2010-11-10 18:58:03 +08:00
|
|
|
return;
|
2012-06-21 00:00:06 +08:00
|
|
|
if (applesmc_read_s16(MOTION_SENSOR_Y_KEY, &y))
|
2010-11-10 18:58:03 +08:00
|
|
|
return;
|
2007-05-08 15:24:52 +08:00
|
|
|
|
|
|
|
x = -x;
|
2007-09-26 12:01:35 +08:00
|
|
|
input_report_abs(idev, ABS_X, x - rest_x);
|
|
|
|
input_report_abs(idev, ABS_Y, y - rest_y);
|
|
|
|
input_sync(idev);
|
2007-05-08 15:24:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Sysfs Files */
|
|
|
|
|
2007-05-24 04:58:13 +08:00
|
|
|
static ssize_t applesmc_name_show(struct device *dev,
|
|
|
|
struct device_attribute *attr, char *buf)
|
|
|
|
{
|
|
|
|
return snprintf(buf, PAGE_SIZE, "applesmc\n");
|
|
|
|
}
|
|
|
|
|
2007-05-08 15:24:52 +08:00
|
|
|
static ssize_t applesmc_position_show(struct device *dev,
|
|
|
|
struct device_attribute *attr, char *buf)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
s16 x, y, z;
|
|
|
|
|
2012-06-21 00:00:06 +08:00
|
|
|
ret = applesmc_read_s16(MOTION_SENSOR_X_KEY, &x);
|
2007-05-08 15:24:52 +08:00
|
|
|
if (ret)
|
|
|
|
goto out;
|
2012-06-21 00:00:06 +08:00
|
|
|
ret = applesmc_read_s16(MOTION_SENSOR_Y_KEY, &y);
|
2007-05-08 15:24:52 +08:00
|
|
|
if (ret)
|
|
|
|
goto out;
|
2012-06-21 00:00:06 +08:00
|
|
|
ret = applesmc_read_s16(MOTION_SENSOR_Z_KEY, &z);
|
2007-05-08 15:24:52 +08:00
|
|
|
if (ret)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
out:
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
else
|
|
|
|
return snprintf(buf, PAGE_SIZE, "(%d,%d,%d)\n", x, y, z);
|
|
|
|
}
|
|
|
|
|
|
|
|
static ssize_t applesmc_light_show(struct device *dev,
|
|
|
|
struct device_attribute *attr, char *sysfsbuf)
|
|
|
|
{
|
2010-11-10 18:58:03 +08:00
|
|
|
const struct applesmc_entry *entry;
|
2008-10-19 11:27:39 +08:00
|
|
|
static int data_length;
|
2007-05-08 15:24:52 +08:00
|
|
|
int ret;
|
|
|
|
u8 left = 0, right = 0;
|
2010-11-10 18:58:03 +08:00
|
|
|
u8 buffer[10];
|
2007-05-08 15:24:52 +08:00
|
|
|
|
2008-10-19 11:27:39 +08:00
|
|
|
if (!data_length) {
|
2010-11-10 18:58:03 +08:00
|
|
|
entry = applesmc_get_entry_by_key(LIGHT_SENSOR_LEFT_KEY);
|
|
|
|
if (IS_ERR(entry))
|
|
|
|
return PTR_ERR(entry);
|
|
|
|
if (entry->len > 10)
|
|
|
|
return -ENXIO;
|
|
|
|
data_length = entry->len;
|
2010-11-09 23:15:03 +08:00
|
|
|
pr_info("light sensor data length set to %d\n", data_length);
|
2008-10-19 11:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
ret = applesmc_read_key(LIGHT_SENSOR_LEFT_KEY, buffer, data_length);
|
2009-01-16 05:51:08 +08:00
|
|
|
/* newer macbooks report a single 10-bit bigendian value */
|
|
|
|
if (data_length == 10) {
|
|
|
|
left = be16_to_cpu(*(__be16 *)(buffer + 6)) >> 2;
|
|
|
|
goto out;
|
|
|
|
}
|
2007-05-08 15:24:52 +08:00
|
|
|
left = buffer[2];
|
|
|
|
if (ret)
|
|
|
|
goto out;
|
2008-10-19 11:27:39 +08:00
|
|
|
ret = applesmc_read_key(LIGHT_SENSOR_RIGHT_KEY, buffer, data_length);
|
2007-05-08 15:24:52 +08:00
|
|
|
right = buffer[2];
|
|
|
|
|
|
|
|
out:
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
else
|
|
|
|
return snprintf(sysfsbuf, PAGE_SIZE, "(%d,%d)\n", left, right);
|
|
|
|
}
|
|
|
|
|
2010-05-28 01:58:54 +08:00
|
|
|
/* Displays sensor key as label */
|
|
|
|
static ssize_t applesmc_show_sensor_label(struct device *dev,
|
|
|
|
struct device_attribute *devattr, char *sysfsbuf)
|
|
|
|
{
|
2012-07-16 15:18:11 +08:00
|
|
|
const char *key = smcreg.index[to_index(devattr)];
|
2010-05-28 01:58:54 +08:00
|
|
|
|
2012-07-16 15:18:11 +08:00
|
|
|
return snprintf(sysfsbuf, PAGE_SIZE, "%s\n", key);
|
2010-05-28 01:58:54 +08:00
|
|
|
}
|
|
|
|
|
2007-05-08 15:24:52 +08:00
|
|
|
/* Displays degree Celsius * 1000 */
|
|
|
|
static ssize_t applesmc_show_temperature(struct device *dev,
|
|
|
|
struct device_attribute *devattr, char *sysfsbuf)
|
|
|
|
{
|
2012-07-16 15:18:11 +08:00
|
|
|
const char *key = smcreg.index[to_index(devattr)];
|
2007-05-08 15:24:52 +08:00
|
|
|
int ret;
|
2012-07-16 15:18:10 +08:00
|
|
|
s16 value;
|
|
|
|
int temp;
|
2007-05-08 15:24:52 +08:00
|
|
|
|
2012-07-16 15:18:11 +08:00
|
|
|
ret = applesmc_read_s16(key, &value);
|
2007-05-08 15:24:52 +08:00
|
|
|
if (ret)
|
|
|
|
return ret;
|
2010-11-10 18:58:04 +08:00
|
|
|
|
2012-07-16 15:18:10 +08:00
|
|
|
temp = 250 * (value >> 6);
|
2010-11-10 18:58:04 +08:00
|
|
|
|
2012-07-16 15:18:10 +08:00
|
|
|
return snprintf(sysfsbuf, PAGE_SIZE, "%d\n", temp);
|
2007-05-08 15:24:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static ssize_t applesmc_show_fan_speed(struct device *dev,
|
|
|
|
struct device_attribute *attr, char *sysfsbuf)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
unsigned int speed = 0;
|
|
|
|
char newkey[5];
|
|
|
|
u8 buffer[2];
|
|
|
|
|
2010-11-09 23:15:08 +08:00
|
|
|
sprintf(newkey, fan_speed_fmt[to_option(attr)], to_index(attr));
|
2007-05-08 15:24:52 +08:00
|
|
|
|
|
|
|
ret = applesmc_read_key(newkey, buffer, 2);
|
|
|
|
speed = ((buffer[0] << 8 | buffer[1]) >> 2);
|
|
|
|
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
else
|
|
|
|
return snprintf(sysfsbuf, PAGE_SIZE, "%u\n", speed);
|
|
|
|
}
|
|
|
|
|
|
|
|
static ssize_t applesmc_store_fan_speed(struct device *dev,
|
|
|
|
struct device_attribute *attr,
|
|
|
|
const char *sysfsbuf, size_t count)
|
|
|
|
{
|
|
|
|
int ret;
|
2010-11-11 02:42:21 +08:00
|
|
|
unsigned long speed;
|
2007-05-08 15:24:52 +08:00
|
|
|
char newkey[5];
|
|
|
|
u8 buffer[2];
|
|
|
|
|
2012-01-05 03:58:52 +08:00
|
|
|
if (kstrtoul(sysfsbuf, 10, &speed) < 0 || speed >= 0x4000)
|
2010-11-11 02:42:21 +08:00
|
|
|
return -EINVAL; /* Bigger than a 14-bit value */
|
2007-05-08 15:24:52 +08:00
|
|
|
|
2010-11-09 23:15:08 +08:00
|
|
|
sprintf(newkey, fan_speed_fmt[to_option(attr)], to_index(attr));
|
2007-05-08 15:24:52 +08:00
|
|
|
|
|
|
|
buffer[0] = (speed >> 6) & 0xff;
|
|
|
|
buffer[1] = (speed << 2) & 0xff;
|
|
|
|
ret = applesmc_write_key(newkey, buffer, 2);
|
|
|
|
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
else
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
static ssize_t applesmc_show_fan_manual(struct device *dev,
|
2010-11-09 23:15:08 +08:00
|
|
|
struct device_attribute *attr, char *sysfsbuf)
|
2007-05-08 15:24:52 +08:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
u16 manual = 0;
|
|
|
|
u8 buffer[2];
|
|
|
|
|
|
|
|
ret = applesmc_read_key(FANS_MANUAL, buffer, 2);
|
2010-11-09 23:15:08 +08:00
|
|
|
manual = ((buffer[0] << 8 | buffer[1]) >> to_index(attr)) & 0x01;
|
2007-05-08 15:24:52 +08:00
|
|
|
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
else
|
|
|
|
return snprintf(sysfsbuf, PAGE_SIZE, "%d\n", manual);
|
|
|
|
}
|
|
|
|
|
|
|
|
static ssize_t applesmc_store_fan_manual(struct device *dev,
|
2010-11-09 23:15:08 +08:00
|
|
|
struct device_attribute *attr,
|
2007-05-08 15:24:52 +08:00
|
|
|
const char *sysfsbuf, size_t count)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
u8 buffer[2];
|
2010-11-11 02:42:21 +08:00
|
|
|
unsigned long input;
|
2007-05-08 15:24:52 +08:00
|
|
|
u16 val;
|
|
|
|
|
2012-01-05 03:58:52 +08:00
|
|
|
if (kstrtoul(sysfsbuf, 10, &input) < 0)
|
2010-11-11 02:42:21 +08:00
|
|
|
return -EINVAL;
|
2007-05-08 15:24:52 +08:00
|
|
|
|
|
|
|
ret = applesmc_read_key(FANS_MANUAL, buffer, 2);
|
|
|
|
val = (buffer[0] << 8 | buffer[1]);
|
|
|
|
if (ret)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
if (input)
|
2010-11-09 23:15:08 +08:00
|
|
|
val = val | (0x01 << to_index(attr));
|
2007-05-08 15:24:52 +08:00
|
|
|
else
|
2010-11-09 23:15:08 +08:00
|
|
|
val = val & ~(0x01 << to_index(attr));
|
2007-05-08 15:24:52 +08:00
|
|
|
|
|
|
|
buffer[0] = (val >> 8) & 0xFF;
|
|
|
|
buffer[1] = val & 0xFF;
|
|
|
|
|
|
|
|
ret = applesmc_write_key(FANS_MANUAL, buffer, 2);
|
|
|
|
|
|
|
|
out:
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
else
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
static ssize_t applesmc_show_fan_position(struct device *dev,
|
|
|
|
struct device_attribute *attr, char *sysfsbuf)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
char newkey[5];
|
|
|
|
u8 buffer[17];
|
|
|
|
|
2010-11-09 23:15:08 +08:00
|
|
|
sprintf(newkey, FAN_ID_FMT, to_index(attr));
|
2007-05-08 15:24:52 +08:00
|
|
|
|
|
|
|
ret = applesmc_read_key(newkey, buffer, 16);
|
|
|
|
buffer[16] = 0;
|
|
|
|
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
else
|
|
|
|
return snprintf(sysfsbuf, PAGE_SIZE, "%s\n", buffer+4);
|
|
|
|
}
|
|
|
|
|
|
|
|
static ssize_t applesmc_calibrate_show(struct device *dev,
|
|
|
|
struct device_attribute *attr, char *sysfsbuf)
|
|
|
|
{
|
|
|
|
return snprintf(sysfsbuf, PAGE_SIZE, "(%d,%d)\n", rest_x, rest_y);
|
|
|
|
}
|
|
|
|
|
|
|
|
static ssize_t applesmc_calibrate_store(struct device *dev,
|
|
|
|
struct device_attribute *attr, const char *sysfsbuf, size_t count)
|
|
|
|
{
|
|
|
|
applesmc_calibrate();
|
|
|
|
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void applesmc_backlight_set(struct work_struct *work)
|
|
|
|
{
|
2009-09-22 08:04:50 +08:00
|
|
|
applesmc_write_key(BACKLIGHT_KEY, backlight_state, 2);
|
2007-05-08 15:24:52 +08:00
|
|
|
}
|
|
|
|
static DECLARE_WORK(backlight_work, &applesmc_backlight_set);
|
|
|
|
|
|
|
|
static void applesmc_brightness_set(struct led_classdev *led_cdev,
|
|
|
|
enum led_brightness value)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
2009-09-22 08:04:50 +08:00
|
|
|
backlight_state[0] = value;
|
2007-05-08 15:24:52 +08:00
|
|
|
ret = queue_work(applesmc_led_wq, &backlight_work);
|
|
|
|
|
|
|
|
if (debug && (!ret))
|
2013-01-10 21:49:55 +08:00
|
|
|
dev_dbg(led_cdev->dev, "work was already on the queue.\n");
|
2007-05-08 15:24:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static ssize_t applesmc_key_count_show(struct device *dev,
|
|
|
|
struct device_attribute *attr, char *sysfsbuf)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
u8 buffer[4];
|
|
|
|
u32 count;
|
|
|
|
|
|
|
|
ret = applesmc_read_key(KEY_COUNT_KEY, buffer, 4);
|
|
|
|
count = ((u32)buffer[0]<<24) + ((u32)buffer[1]<<16) +
|
|
|
|
((u32)buffer[2]<<8) + buffer[3];
|
|
|
|
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
else
|
|
|
|
return snprintf(sysfsbuf, PAGE_SIZE, "%d\n", count);
|
|
|
|
}
|
|
|
|
|
|
|
|
static ssize_t applesmc_key_at_index_read_show(struct device *dev,
|
|
|
|
struct device_attribute *attr, char *sysfsbuf)
|
|
|
|
{
|
2010-11-10 18:58:03 +08:00
|
|
|
const struct applesmc_entry *entry;
|
2007-05-08 15:24:52 +08:00
|
|
|
int ret;
|
|
|
|
|
2010-11-10 18:58:03 +08:00
|
|
|
entry = applesmc_get_entry_by_index(key_at_index);
|
|
|
|
if (IS_ERR(entry))
|
|
|
|
return PTR_ERR(entry);
|
|
|
|
ret = applesmc_read_entry(entry, sysfsbuf, entry->len);
|
|
|
|
if (ret)
|
2007-05-08 15:24:52 +08:00
|
|
|
return ret;
|
|
|
|
|
2010-11-10 18:58:03 +08:00
|
|
|
return entry->len;
|
2007-05-08 15:24:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static ssize_t applesmc_key_at_index_data_length_show(struct device *dev,
|
|
|
|
struct device_attribute *attr, char *sysfsbuf)
|
|
|
|
{
|
2010-11-10 18:58:03 +08:00
|
|
|
const struct applesmc_entry *entry;
|
2007-05-08 15:24:52 +08:00
|
|
|
|
2010-11-10 18:58:03 +08:00
|
|
|
entry = applesmc_get_entry_by_index(key_at_index);
|
|
|
|
if (IS_ERR(entry))
|
|
|
|
return PTR_ERR(entry);
|
2007-05-08 15:24:52 +08:00
|
|
|
|
2010-11-10 18:58:03 +08:00
|
|
|
return snprintf(sysfsbuf, PAGE_SIZE, "%d\n", entry->len);
|
2007-05-08 15:24:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static ssize_t applesmc_key_at_index_type_show(struct device *dev,
|
|
|
|
struct device_attribute *attr, char *sysfsbuf)
|
|
|
|
{
|
2010-11-10 18:58:03 +08:00
|
|
|
const struct applesmc_entry *entry;
|
2007-05-08 15:24:52 +08:00
|
|
|
|
2010-11-10 18:58:03 +08:00
|
|
|
entry = applesmc_get_entry_by_index(key_at_index);
|
|
|
|
if (IS_ERR(entry))
|
|
|
|
return PTR_ERR(entry);
|
2007-05-08 15:24:52 +08:00
|
|
|
|
2010-11-10 18:58:03 +08:00
|
|
|
return snprintf(sysfsbuf, PAGE_SIZE, "%s\n", entry->type);
|
2007-05-08 15:24:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static ssize_t applesmc_key_at_index_name_show(struct device *dev,
|
|
|
|
struct device_attribute *attr, char *sysfsbuf)
|
|
|
|
{
|
2010-11-10 18:58:03 +08:00
|
|
|
const struct applesmc_entry *entry;
|
2007-05-08 15:24:52 +08:00
|
|
|
|
2010-11-10 18:58:03 +08:00
|
|
|
entry = applesmc_get_entry_by_index(key_at_index);
|
|
|
|
if (IS_ERR(entry))
|
|
|
|
return PTR_ERR(entry);
|
2007-05-08 15:24:52 +08:00
|
|
|
|
2010-11-10 18:58:03 +08:00
|
|
|
return snprintf(sysfsbuf, PAGE_SIZE, "%s\n", entry->key);
|
2007-05-08 15:24:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static ssize_t applesmc_key_at_index_show(struct device *dev,
|
|
|
|
struct device_attribute *attr, char *sysfsbuf)
|
|
|
|
{
|
|
|
|
return snprintf(sysfsbuf, PAGE_SIZE, "%d\n", key_at_index);
|
|
|
|
}
|
|
|
|
|
|
|
|
static ssize_t applesmc_key_at_index_store(struct device *dev,
|
|
|
|
struct device_attribute *attr, const char *sysfsbuf, size_t count)
|
|
|
|
{
|
2010-11-10 18:58:03 +08:00
|
|
|
unsigned long newkey;
|
2007-05-08 15:24:52 +08:00
|
|
|
|
2012-01-05 03:58:52 +08:00
|
|
|
if (kstrtoul(sysfsbuf, 10, &newkey) < 0
|
2010-11-10 18:58:03 +08:00
|
|
|
|| newkey >= smcreg.key_count)
|
|
|
|
return -EINVAL;
|
2007-05-08 15:24:52 +08:00
|
|
|
|
2010-11-10 18:58:03 +08:00
|
|
|
key_at_index = newkey;
|
2007-05-08 15:24:52 +08:00
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct led_classdev applesmc_backlight = {
|
2007-10-31 22:00:07 +08:00
|
|
|
.name = "smc::kbd_backlight",
|
2007-05-08 15:24:52 +08:00
|
|
|
.default_trigger = "nand-disk",
|
|
|
|
.brightness_set = applesmc_brightness_set,
|
|
|
|
};
|
|
|
|
|
2010-11-09 23:15:09 +08:00
|
|
|
static struct applesmc_node_group info_group[] = {
|
|
|
|
{ "name", applesmc_name_show },
|
|
|
|
{ "key_count", applesmc_key_count_show },
|
|
|
|
{ "key_at_index", applesmc_key_at_index_show, applesmc_key_at_index_store },
|
|
|
|
{ "key_at_index_name", applesmc_key_at_index_name_show },
|
|
|
|
{ "key_at_index_type", applesmc_key_at_index_type_show },
|
|
|
|
{ "key_at_index_data_length", applesmc_key_at_index_data_length_show },
|
|
|
|
{ "key_at_index_data", applesmc_key_at_index_read_show },
|
|
|
|
{ }
|
2007-05-08 15:24:52 +08:00
|
|
|
};
|
|
|
|
|
2010-11-09 23:15:09 +08:00
|
|
|
static struct applesmc_node_group accelerometer_group[] = {
|
|
|
|
{ "position", applesmc_position_show },
|
|
|
|
{ "calibrate", applesmc_calibrate_show, applesmc_calibrate_store },
|
|
|
|
{ }
|
2007-05-08 15:24:52 +08:00
|
|
|
};
|
|
|
|
|
2010-11-09 23:15:09 +08:00
|
|
|
static struct applesmc_node_group light_sensor_group[] = {
|
|
|
|
{ "light", applesmc_light_show },
|
|
|
|
{ }
|
|
|
|
};
|
2007-05-08 15:24:52 +08:00
|
|
|
|
2010-11-09 23:15:08 +08:00
|
|
|
static struct applesmc_node_group fan_group[] = {
|
|
|
|
{ "fan%d_label", applesmc_show_fan_position },
|
|
|
|
{ "fan%d_input", applesmc_show_fan_speed, NULL, 0 },
|
|
|
|
{ "fan%d_min", applesmc_show_fan_speed, applesmc_store_fan_speed, 1 },
|
|
|
|
{ "fan%d_max", applesmc_show_fan_speed, NULL, 2 },
|
|
|
|
{ "fan%d_safe", applesmc_show_fan_speed, NULL, 3 },
|
|
|
|
{ "fan%d_output", applesmc_show_fan_speed, applesmc_store_fan_speed, 4 },
|
|
|
|
{ "fan%d_manual", applesmc_show_fan_manual, applesmc_store_fan_manual },
|
|
|
|
{ }
|
2007-05-08 15:24:52 +08:00
|
|
|
};
|
|
|
|
|
2010-11-10 18:58:04 +08:00
|
|
|
static struct applesmc_node_group temp_group[] = {
|
|
|
|
{ "temp%d_label", applesmc_show_sensor_label },
|
|
|
|
{ "temp%d_input", applesmc_show_temperature },
|
|
|
|
{ }
|
2010-05-28 01:58:54 +08:00
|
|
|
};
|
|
|
|
|
2007-05-08 15:24:52 +08:00
|
|
|
/* Module stuff */
|
|
|
|
|
2010-11-10 18:58:04 +08:00
|
|
|
/*
|
|
|
|
* applesmc_destroy_nodes - remove files and free associated memory
|
|
|
|
*/
|
|
|
|
static void applesmc_destroy_nodes(struct applesmc_node_group *groups)
|
|
|
|
{
|
|
|
|
struct applesmc_node_group *grp;
|
|
|
|
struct applesmc_dev_attr *node;
|
|
|
|
|
|
|
|
for (grp = groups; grp->nodes; grp++) {
|
|
|
|
for (node = grp->nodes; node->sda.dev_attr.attr.name; node++)
|
|
|
|
sysfs_remove_file(&pdev->dev.kobj,
|
|
|
|
&node->sda.dev_attr.attr);
|
|
|
|
kfree(grp->nodes);
|
|
|
|
grp->nodes = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* applesmc_create_nodes - create a two-dimensional group of sysfs files
|
|
|
|
*/
|
|
|
|
static int applesmc_create_nodes(struct applesmc_node_group *groups, int num)
|
|
|
|
{
|
|
|
|
struct applesmc_node_group *grp;
|
|
|
|
struct applesmc_dev_attr *node;
|
|
|
|
struct attribute *attr;
|
|
|
|
int ret, i;
|
|
|
|
|
|
|
|
for (grp = groups; grp->format; grp++) {
|
|
|
|
grp->nodes = kcalloc(num + 1, sizeof(*node), GFP_KERNEL);
|
|
|
|
if (!grp->nodes) {
|
|
|
|
ret = -ENOMEM;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
for (i = 0; i < num; i++) {
|
|
|
|
node = &grp->nodes[i];
|
|
|
|
sprintf(node->name, grp->format, i + 1);
|
2010-11-09 23:15:08 +08:00
|
|
|
node->sda.index = (grp->option << 16) | (i & 0xffff);
|
2010-11-10 18:58:04 +08:00
|
|
|
node->sda.dev_attr.show = grp->show;
|
|
|
|
node->sda.dev_attr.store = grp->store;
|
|
|
|
attr = &node->sda.dev_attr.attr;
|
2011-01-23 03:55:39 +08:00
|
|
|
sysfs_attr_init(attr);
|
2010-11-10 18:58:04 +08:00
|
|
|
attr->name = node->name;
|
|
|
|
attr->mode = S_IRUGO | (grp->store ? S_IWUSR : 0);
|
|
|
|
ret = sysfs_create_file(&pdev->dev.kobj, attr);
|
|
|
|
if (ret) {
|
|
|
|
attr->name = NULL;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
out:
|
|
|
|
applesmc_destroy_nodes(groups);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2007-05-08 15:24:52 +08:00
|
|
|
/* Create accelerometer ressources */
|
|
|
|
static int applesmc_create_accelerometer(void)
|
|
|
|
{
|
2007-09-26 12:01:35 +08:00
|
|
|
struct input_dev *idev;
|
2007-05-08 15:24:52 +08:00
|
|
|
int ret;
|
|
|
|
|
2010-11-09 23:15:09 +08:00
|
|
|
if (!smcreg.has_accelerometer)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
ret = applesmc_create_nodes(accelerometer_group, 1);
|
2007-05-08 15:24:52 +08:00
|
|
|
if (ret)
|
|
|
|
goto out;
|
|
|
|
|
2007-09-26 12:01:35 +08:00
|
|
|
applesmc_idev = input_allocate_polled_device();
|
2007-05-08 15:24:52 +08:00
|
|
|
if (!applesmc_idev) {
|
|
|
|
ret = -ENOMEM;
|
|
|
|
goto out_sysfs;
|
|
|
|
}
|
|
|
|
|
2007-09-26 12:01:35 +08:00
|
|
|
applesmc_idev->poll = applesmc_idev_poll;
|
|
|
|
applesmc_idev->poll_interval = APPLESMC_POLL_INTERVAL;
|
|
|
|
|
2007-05-08 15:24:52 +08:00
|
|
|
/* initial calibrate for the input device */
|
|
|
|
applesmc_calibrate();
|
|
|
|
|
2007-09-26 12:01:35 +08:00
|
|
|
/* initialize the input device */
|
|
|
|
idev = applesmc_idev->input;
|
|
|
|
idev->name = "applesmc";
|
|
|
|
idev->id.bustype = BUS_HOST;
|
|
|
|
idev->dev.parent = &pdev->dev;
|
2007-10-19 14:40:32 +08:00
|
|
|
idev->evbit[0] = BIT_MASK(EV_ABS);
|
2007-09-26 12:01:35 +08:00
|
|
|
input_set_abs_params(idev, ABS_X,
|
2007-05-08 15:24:52 +08:00
|
|
|
-256, 256, APPLESMC_INPUT_FUZZ, APPLESMC_INPUT_FLAT);
|
2007-09-26 12:01:35 +08:00
|
|
|
input_set_abs_params(idev, ABS_Y,
|
2007-05-08 15:24:52 +08:00
|
|
|
-256, 256, APPLESMC_INPUT_FUZZ, APPLESMC_INPUT_FLAT);
|
|
|
|
|
2007-09-26 12:01:35 +08:00
|
|
|
ret = input_register_polled_device(applesmc_idev);
|
2007-05-08 15:24:52 +08:00
|
|
|
if (ret)
|
|
|
|
goto out_idev;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
out_idev:
|
2007-09-26 12:01:35 +08:00
|
|
|
input_free_polled_device(applesmc_idev);
|
2007-05-08 15:24:52 +08:00
|
|
|
|
|
|
|
out_sysfs:
|
2010-11-09 23:15:09 +08:00
|
|
|
applesmc_destroy_nodes(accelerometer_group);
|
2007-05-08 15:24:52 +08:00
|
|
|
|
|
|
|
out:
|
2010-11-09 23:15:03 +08:00
|
|
|
pr_warn("driver init failed (ret=%d)!\n", ret);
|
2007-05-08 15:24:52 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Release all ressources used by the accelerometer */
|
|
|
|
static void applesmc_release_accelerometer(void)
|
|
|
|
{
|
2010-11-09 23:15:09 +08:00
|
|
|
if (!smcreg.has_accelerometer)
|
|
|
|
return;
|
2007-09-26 12:01:35 +08:00
|
|
|
input_unregister_polled_device(applesmc_idev);
|
|
|
|
input_free_polled_device(applesmc_idev);
|
2010-11-09 23:15:09 +08:00
|
|
|
applesmc_destroy_nodes(accelerometer_group);
|
2007-05-08 15:24:52 +08:00
|
|
|
}
|
2010-11-09 23:15:09 +08:00
|
|
|
|
|
|
|
static int applesmc_create_light_sensor(void)
|
|
|
|
{
|
|
|
|
if (!smcreg.num_light_sensors)
|
|
|
|
return 0;
|
|
|
|
return applesmc_create_nodes(light_sensor_group, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void applesmc_release_light_sensor(void)
|
|
|
|
{
|
|
|
|
if (!smcreg.num_light_sensors)
|
|
|
|
return;
|
|
|
|
applesmc_destroy_nodes(light_sensor_group);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int applesmc_create_key_backlight(void)
|
|
|
|
{
|
|
|
|
if (!smcreg.has_key_backlight)
|
|
|
|
return 0;
|
|
|
|
applesmc_led_wq = create_singlethread_workqueue("applesmc-led");
|
|
|
|
if (!applesmc_led_wq)
|
|
|
|
return -ENOMEM;
|
|
|
|
return led_classdev_register(&pdev->dev, &applesmc_backlight);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void applesmc_release_key_backlight(void)
|
|
|
|
{
|
|
|
|
if (!smcreg.has_key_backlight)
|
|
|
|
return;
|
|
|
|
led_classdev_unregister(&applesmc_backlight);
|
|
|
|
destroy_workqueue(applesmc_led_wq);
|
|
|
|
}
|
|
|
|
|
2010-11-10 18:58:05 +08:00
|
|
|
static int applesmc_dmi_match(const struct dmi_system_id *id)
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
2007-05-08 15:24:52 +08:00
|
|
|
|
2012-01-20 03:02:15 +08:00
|
|
|
/*
|
|
|
|
* Note that DMI_MATCH(...,"MacBook") will match "MacBookPro1,1".
|
|
|
|
* So we need to put "Apple MacBook Pro" before "Apple MacBook".
|
|
|
|
*/
|
2007-05-08 15:24:52 +08:00
|
|
|
static __initdata struct dmi_system_id applesmc_whitelist[] = {
|
2008-10-19 11:27:40 +08:00
|
|
|
{ applesmc_dmi_match, "Apple MacBook Air", {
|
|
|
|
DMI_MATCH(DMI_BOARD_VENDOR, "Apple"),
|
|
|
|
DMI_MATCH(DMI_PRODUCT_NAME, "MacBookAir") },
|
2010-11-10 18:58:05 +08:00
|
|
|
},
|
2007-05-08 15:24:52 +08:00
|
|
|
{ applesmc_dmi_match, "Apple MacBook Pro", {
|
2010-11-11 02:42:21 +08:00
|
|
|
DMI_MATCH(DMI_BOARD_VENDOR, "Apple"),
|
|
|
|
DMI_MATCH(DMI_PRODUCT_NAME, "MacBookPro") },
|
2010-11-10 18:58:05 +08:00
|
|
|
},
|
2008-02-05 15:41:58 +08:00
|
|
|
{ applesmc_dmi_match, "Apple MacBook", {
|
2010-11-11 02:42:21 +08:00
|
|
|
DMI_MATCH(DMI_BOARD_VENDOR, "Apple"),
|
|
|
|
DMI_MATCH(DMI_PRODUCT_NAME, "MacBook") },
|
2010-11-10 18:58:05 +08:00
|
|
|
},
|
2007-05-08 15:24:52 +08:00
|
|
|
{ applesmc_dmi_match, "Apple Macmini", {
|
2010-11-11 02:42:21 +08:00
|
|
|
DMI_MATCH(DMI_BOARD_VENDOR, "Apple"),
|
|
|
|
DMI_MATCH(DMI_PRODUCT_NAME, "Macmini") },
|
2010-11-10 18:58:05 +08:00
|
|
|
},
|
2008-11-20 07:36:42 +08:00
|
|
|
{ applesmc_dmi_match, "Apple MacPro", {
|
|
|
|
DMI_MATCH(DMI_BOARD_VENDOR, "Apple"),
|
|
|
|
DMI_MATCH(DMI_PRODUCT_NAME, "MacPro") },
|
2010-11-10 18:58:05 +08:00
|
|
|
},
|
2008-08-15 15:40:30 +08:00
|
|
|
{ applesmc_dmi_match, "Apple iMac", {
|
2010-11-11 02:42:21 +08:00
|
|
|
DMI_MATCH(DMI_BOARD_VENDOR, "Apple"),
|
|
|
|
DMI_MATCH(DMI_PRODUCT_NAME, "iMac") },
|
2010-11-10 18:58:05 +08:00
|
|
|
},
|
2007-05-08 15:24:52 +08:00
|
|
|
{ .ident = NULL }
|
|
|
|
};
|
|
|
|
|
|
|
|
static int __init applesmc_init(void)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (!dmi_check_system(applesmc_whitelist)) {
|
2010-11-09 23:15:03 +08:00
|
|
|
pr_warn("supported laptop not found!\n");
|
2007-05-08 15:24:52 +08:00
|
|
|
ret = -ENODEV;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!request_region(APPLESMC_DATA_PORT, APPLESMC_NR_PORTS,
|
|
|
|
"applesmc")) {
|
|
|
|
ret = -ENXIO;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = platform_driver_register(&applesmc_driver);
|
|
|
|
if (ret)
|
|
|
|
goto out_region;
|
|
|
|
|
2007-05-09 11:27:04 +08:00
|
|
|
pdev = platform_device_register_simple("applesmc", APPLESMC_DATA_PORT,
|
|
|
|
NULL, 0);
|
2007-05-08 15:24:52 +08:00
|
|
|
if (IS_ERR(pdev)) {
|
|
|
|
ret = PTR_ERR(pdev);
|
|
|
|
goto out_driver;
|
|
|
|
}
|
|
|
|
|
2010-11-10 18:58:03 +08:00
|
|
|
/* create register cache */
|
|
|
|
ret = applesmc_init_smcreg();
|
2007-05-28 04:17:43 +08:00
|
|
|
if (ret)
|
|
|
|
goto out_device;
|
2007-05-24 04:58:13 +08:00
|
|
|
|
2010-11-09 23:15:09 +08:00
|
|
|
ret = applesmc_create_nodes(info_group, 1);
|
2010-11-10 18:58:03 +08:00
|
|
|
if (ret)
|
|
|
|
goto out_smcreg;
|
|
|
|
|
2010-11-09 23:15:08 +08:00
|
|
|
ret = applesmc_create_nodes(fan_group, smcreg.fan_count);
|
|
|
|
if (ret)
|
|
|
|
goto out_info;
|
2007-05-08 15:24:52 +08:00
|
|
|
|
2012-07-16 15:18:11 +08:00
|
|
|
ret = applesmc_create_nodes(temp_group, smcreg.index_count);
|
2010-11-10 18:58:04 +08:00
|
|
|
if (ret)
|
|
|
|
goto out_fans;
|
2007-05-08 15:24:52 +08:00
|
|
|
|
2010-11-09 23:15:09 +08:00
|
|
|
ret = applesmc_create_accelerometer();
|
|
|
|
if (ret)
|
|
|
|
goto out_temperature;
|
2007-05-08 15:24:52 +08:00
|
|
|
|
2010-11-09 23:15:09 +08:00
|
|
|
ret = applesmc_create_light_sensor();
|
|
|
|
if (ret)
|
|
|
|
goto out_accelerometer;
|
2007-05-08 15:24:52 +08:00
|
|
|
|
2010-11-09 23:15:09 +08:00
|
|
|
ret = applesmc_create_key_backlight();
|
|
|
|
if (ret)
|
|
|
|
goto out_light_sysfs;
|
2007-05-08 15:24:52 +08:00
|
|
|
|
2007-08-21 04:46:20 +08:00
|
|
|
hwmon_dev = hwmon_device_register(&pdev->dev);
|
|
|
|
if (IS_ERR(hwmon_dev)) {
|
|
|
|
ret = PTR_ERR(hwmon_dev);
|
2007-05-08 15:24:52 +08:00
|
|
|
goto out_light_ledclass;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
out_light_ledclass:
|
2010-11-09 23:15:09 +08:00
|
|
|
applesmc_release_key_backlight();
|
2007-05-08 15:24:52 +08:00
|
|
|
out_light_sysfs:
|
2010-11-09 23:15:09 +08:00
|
|
|
applesmc_release_light_sensor();
|
2007-05-08 15:24:52 +08:00
|
|
|
out_accelerometer:
|
2010-11-09 23:15:09 +08:00
|
|
|
applesmc_release_accelerometer();
|
2007-05-08 15:24:52 +08:00
|
|
|
out_temperature:
|
2010-11-10 18:58:04 +08:00
|
|
|
applesmc_destroy_nodes(temp_group);
|
2010-05-11 15:17:47 +08:00
|
|
|
out_fans:
|
2010-11-09 23:15:08 +08:00
|
|
|
applesmc_destroy_nodes(fan_group);
|
|
|
|
out_info:
|
2010-11-09 23:15:09 +08:00
|
|
|
applesmc_destroy_nodes(info_group);
|
2010-11-10 18:58:03 +08:00
|
|
|
out_smcreg:
|
|
|
|
applesmc_destroy_smcreg();
|
2007-05-08 15:24:52 +08:00
|
|
|
out_device:
|
|
|
|
platform_device_unregister(pdev);
|
|
|
|
out_driver:
|
|
|
|
platform_driver_unregister(&applesmc_driver);
|
|
|
|
out_region:
|
|
|
|
release_region(APPLESMC_DATA_PORT, APPLESMC_NR_PORTS);
|
|
|
|
out:
|
2010-11-09 23:15:03 +08:00
|
|
|
pr_warn("driver init failed (ret=%d)!\n", ret);
|
2007-05-08 15:24:52 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void __exit applesmc_exit(void)
|
|
|
|
{
|
2007-08-21 04:46:20 +08:00
|
|
|
hwmon_device_unregister(hwmon_dev);
|
2010-11-09 23:15:09 +08:00
|
|
|
applesmc_release_key_backlight();
|
|
|
|
applesmc_release_light_sensor();
|
|
|
|
applesmc_release_accelerometer();
|
2010-11-10 18:58:04 +08:00
|
|
|
applesmc_destroy_nodes(temp_group);
|
2010-11-09 23:15:08 +08:00
|
|
|
applesmc_destroy_nodes(fan_group);
|
2010-11-09 23:15:09 +08:00
|
|
|
applesmc_destroy_nodes(info_group);
|
2010-11-10 18:58:03 +08:00
|
|
|
applesmc_destroy_smcreg();
|
2007-05-08 15:24:52 +08:00
|
|
|
platform_device_unregister(pdev);
|
|
|
|
platform_driver_unregister(&applesmc_driver);
|
|
|
|
release_region(APPLESMC_DATA_PORT, APPLESMC_NR_PORTS);
|
|
|
|
}
|
|
|
|
|
|
|
|
module_init(applesmc_init);
|
|
|
|
module_exit(applesmc_exit);
|
|
|
|
|
|
|
|
MODULE_AUTHOR("Nicolas Boichat");
|
|
|
|
MODULE_DESCRIPTION("Apple SMC");
|
|
|
|
MODULE_LICENSE("GPL v2");
|
2008-12-02 05:13:49 +08:00
|
|
|
MODULE_DEVICE_TABLE(dmi, applesmc_whitelist);
|