summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHenrik Rydberg <rydberg@euromail.se>2010-11-01 08:58:01 +0100
committerHenrik Rydberg <rydberg@euromail.se>2010-11-01 08:58:01 +0100
commitcab7602198201b8515484e7aa4a4039e6bd8c659 (patch)
tree1f0c38493f9b3202b7f07490d0421c78ae7e0e31
parent9cf0f8e5b28d3c9b53eea70a8ed7aede6500c4d6 (diff)
Refactored upstream driver
Generic register mapping for all models. Signed-off-by: Henrik Rydberg <rydberg@euromail.se>
-rw-r--r--usr/src/dkms_source_tree/applesmc.c1577
1 files changed, 554 insertions, 1023 deletions
diff --git a/usr/src/dkms_source_tree/applesmc.c b/usr/src/dkms_source_tree/applesmc.c
index b6598aa..2eb78e3 100644
--- a/usr/src/dkms_source_tree/applesmc.c
+++ b/usr/src/dkms_source_tree/applesmc.c
@@ -4,6 +4,7 @@
4 * computers. 4 * computers.
5 * 5 *
6 * Copyright (C) 2007 Nicolas Boichat <nicolas@boichat.ch> 6 * Copyright (C) 2007 Nicolas Boichat <nicolas@boichat.ch>
7 * Copyright (C) 2010 Henrik Rydberg <rydberg@euromail.se>
7 * 8 *
8 * Based on hdaps.c driver: 9 * Based on hdaps.c driver:
9 * Copyright (C) 2005 Robert Love <rml@novell.com> 10 * Copyright (C) 2005 Robert Love <rml@novell.com>
@@ -26,10 +27,13 @@
26 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA 27 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
27 */ 28 */
28 29
30#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
31
29#include <linux/delay.h> 32#include <linux/delay.h>
30#include <linux/platform_device.h> 33#include <linux/platform_device.h>
31#include <linux/input-polldev.h> 34#include <linux/input-polldev.h>
32#include <linux/kernel.h> 35#include <linux/kernel.h>
36#include <linux/slab.h>
33#include <linux/module.h> 37#include <linux/module.h>
34#include <linux/timer.h> 38#include <linux/timer.h>
35#include <linux/dmi.h> 39#include <linux/dmi.h>
@@ -40,6 +44,16 @@
40#include <linux/hwmon.h> 44#include <linux/hwmon.h>
41#include <linux/workqueue.h> 45#include <linux/workqueue.h>
42 46
47#ifndef pr_info
48#define pr_info(fmt, ...) printk(KERN_INFO pr_fmt(fmt), ##__VA_ARGS__)
49#endif
50#ifndef pr_err
51#define pr_err(fmt, ...) printk(KERN_ERR pr_fmt(fmt), ##__VA_ARGS__)
52#endif
53#ifndef pr_warn
54#define pr_warn(fmt, ...) printk(KERN_WARNING pr_fmt(fmt), ##__VA_ARGS__)
55#endif
56
43/* data port used by Apple SMC */ 57/* data port used by Apple SMC */
44#define APPLESMC_DATA_PORT 0x300 58#define APPLESMC_DATA_PORT 0x300
45/* command/status port used by Apple SMC */ 59/* command/status port used by Apple SMC */
@@ -49,6 +63,7 @@
49 63
50#define APPLESMC_MAX_DATA_LENGTH 32 64#define APPLESMC_MAX_DATA_LENGTH 32
51 65
66/* wait up to 32 ms for a status change. */
52#define APPLESMC_MIN_WAIT 0x0040 67#define APPLESMC_MIN_WAIT 0x0040
53#define APPLESMC_MAX_WAIT 0x8000 68#define APPLESMC_MAX_WAIT 0x8000
54 69
@@ -73,104 +88,15 @@
73 88
74#define FANS_COUNT "FNum" /* r-o ui8 */ 89#define FANS_COUNT "FNum" /* r-o ui8 */
75#define FANS_MANUAL "FS! " /* r-w ui16 */ 90#define FANS_MANUAL "FS! " /* r-w ui16 */
76#define FAN_ACTUAL_SPEED "F0Ac" /* r-o fpe2 (2 bytes) */ 91#define FAN_ID_FMT "F%dID" /* r-o char[16] */
77#define FAN_MIN_SPEED "F0Mn" /* r-o fpe2 (2 bytes) */
78#define FAN_MAX_SPEED "F0Mx" /* r-o fpe2 (2 bytes) */
79#define FAN_SAFE_SPEED "F0Sf" /* r-o fpe2 (2 bytes) */
80#define FAN_TARGET_SPEED "F0Tg" /* r-w fpe2 (2 bytes) */
81#define FAN_POSITION "F0ID" /* r-o char[16] */
82
83/*
84 * Temperature sensors keys (sp78 - 2 bytes).
85 */
86static const char *temperature_sensors_sets[][41] = {
87/* Set 0: Macbook Pro */
88 { "TA0P", "TB0T", "TC0D", "TC0P", "TG0H", "TG0P", "TG0T", "Th0H",
89 "Th1H", "Tm0P", "Ts0P", "Ts1P", NULL },
90/* Set 1: Macbook2 set */
91 { "TB0T", "TC0D", "TC0P", "TM0P", "TN0P", "TN1P", "TTF0", "Th0H",
92 "Th0S", "Th1H", NULL },
93/* Set 2: Macbook set */
94 { "TB0T", "TC0D", "TC0P", "TM0P", "TN0P", "TN1P", "Th0H", "Th0S",
95 "Th1H", "Ts0P", NULL },
96/* Set 3: Macmini set */
97 { "TC0D", "TC0P", NULL },
98/* Set 4: Mac Pro (2 x Quad-Core) */
99 { "TA0P", "TCAG", "TCAH", "TCBG", "TCBH", "TC0C", "TC0D", "TC0P",
100 "TC1C", "TC1D", "TC2C", "TC2D", "TC3C", "TC3D", "THTG", "TH0P",
101 "TH1P", "TH2P", "TH3P", "TMAP", "TMAS", "TMBS", "TM0P", "TM0S",
102 "TM1P", "TM1S", "TM2P", "TM2S", "TM3S", "TM8P", "TM8S", "TM9P",
103 "TM9S", "TN0H", "TS0C", NULL },
104/* Set 5: iMac */
105 { "TC0D", "TA0P", "TG0P", "TG0D", "TG0H", "TH0P", "Tm0P", "TO0P",
106 "Tp0C", NULL },
107/* Set 6: Macbook3 set */
108 { "TB0T", "TC0D", "TC0P", "TM0P", "TN0P", "TTF0", "TW0P", "Th0H",
109 "Th0S", "Th1H", NULL },
110/* Set 7: Macbook Air */
111 { "TB0T", "TB1S", "TB1T", "TB2S", "TB2T", "TC0D", "TC0P", "TCFP",
112 "TTF0", "TW0P", "Th0H", "Tp0P", "TpFP", "Ts0P", "Ts0S", NULL },
113/* Set 8: Macbook Pro 4,1 (Penryn) */
114 { "TB0T", "TC0D", "TC0P", "TG0D", "TG0H", "TTF0", "TW0P", "Th0H",
115 "Th1H", "Th2H", "Tm0P", "Ts0P", NULL },
116/* Set 9: Macbook Pro 3,1 (Santa Rosa) */
117 { "TALP", "TB0T", "TC0D", "TC0P", "TG0D", "TG0H", "TTF0", "TW0P",
118 "Th0H", "Th1H", "Th2H", "Tm0P", "Ts0P", NULL },
119/* Set 10: iMac 5,1 */
120 { "TA0P", "TC0D", "TC0P", "TG0D", "TH0P", "TO0P", "Tm0P", NULL },
121/* Set 11: Macbook 5,1 */
122 { "TB0T", "TB1T", "TB2T", "TB3T", "TC0D", "TC0P", "TN0D", "TN0P",
123 "TTF0", "Th0H", "Th1H", "ThFH", "Ts0P", "Ts0S", NULL },
124/* Set 12: Macbook Pro 5,1 */
125 { "TB0T", "TB1T", "TB2T", "TB3T", "TC0D", "TC0F", "TC0P", "TG0D",
126 "TG0F", "TG0H", "TG0P", "TG0T", "TG1H", "TN0D", "TN0P", "TTF0",
127 "Th2H", "Tm0P", "Ts0P", "Ts0S", NULL },
128/* Set 13: iMac 8,1 */
129 { "TA0P", "TC0D", "TC0H", "TC0P", "TG0D", "TG0H", "TG0P", "TH0P",
130 "TL0P", "TO0P", "TW0P", "Tm0P", "Tp0P", NULL },
131/* Set 14: iMac 6,1 */
132 { "TA0P", "TC0D", "TC0H", "TC0P", "TG0D", "TG0H", "TG0P", "TH0P",
133 "TO0P", "Tp0P", NULL },
134/* Set 15: MacBook Air 2,1 */
135 { "TB0T", "TB1S", "TB1T", "TB2S", "TB2T", "TC0D", "TN0D", "TTF0",
136 "TV0P", "TVFP", "TW0P", "Th0P", "Tp0P", "Tp1P", "TpFP", "Ts0P",
137 "Ts0S", NULL },
138/* Set 16: Mac Pro 3,1 (2 x Quad-Core) */
139 { "TA0P", "TCAG", "TCAH", "TCBG", "TCBH", "TC0C", "TC0D", "TC0P",
140 "TC1C", "TC1D", "TC2C", "TC2D", "TC3C", "TC3D", "TH0P", "TH1P",
141 "TH2P", "TH3P", "TMAP", "TMAS", "TMBS", "TM0P", "TM0S", "TM1P",
142 "TM1S", "TM2P", "TM2S", "TM3S", "TM8P", "TM8S", "TM9P", "TM9S",
143 "TN0C", "TN0D", "TN0H", "TS0C", "Tp0C", "Tp1C", "Tv0S", "Tv1S",
144 NULL },
145/* Set 17: iMac 9,1 */
146 { "TA0P", "TC0D", "TC0H", "TC0P", "TG0D", "TG0H", "TH0P", "TL0P",
147 "TN0D", "TN0H", "TN0P", "TO0P", "Tm0P", "Tp0P", NULL },
148/* Set 18: MacBook Pro 2,2 */
149 { "TB0T", "TC0D", "TC0P", "TG0H", "TG0P", "TG0T", "TM0P", "TTF0",
150 "Th0H", "Th1H", "Tm0P", "Ts0P", NULL },
151/* Set 19: Macbook Pro 5,3 */
152 { "TB0T", "TB1T", "TB2T", "TB3T", "TC0D", "TC0F", "TC0P", "TG0D",
153 "TG0F", "TG0H", "TG0P", "TG0T", "TN0D", "TN0P", "TTF0", "Th2H",
154 "Tm0P", "Ts0P", "Ts0S", NULL },
155/* Set 20: MacBook Pro 5,4 */
156 { "TB0T", "TB1T", "TB2T", "TB3T", "TC0D", "TC0F", "TC0P", "TN0D",
157 "TN0P", "TTF0", "Th2H", "Ts0P", "Ts0S", NULL },
158/* Set 21: MacBook Pro 6,2 */
159 { "TB0T", "TB1T", "TB2T", "TC0C", "TC0D", "TC0P", "TC1C", "TG0D",
160 "TG0P", "TG0T", "TMCD", "TP0P", "TPCD", "Th1H", "Th2H", "Tm0P",
161 "Ts0P", "Ts0S", NULL },
162/* Set 22: MacBook Pro 7,1 */
163 { "TB0T", "TB1T", "TB2T", "TC0D", "TC0P", "TN0D", "TN0P", "TN0S",
164 "TN1D", "TN1F", "TN1G", "TN1S", "Th1H", "Ts0P", "Ts0S", NULL },
165};
166 92
167/* List of keys used to read/write fan speeds */ 93/* List of keys used to read/write fan speeds */
168static const char* fan_speed_keys[] = { 94static const char *fan_speed_fmt[] = {
169 FAN_ACTUAL_SPEED, 95 "F%dAc", /* actual speed */
170 FAN_MIN_SPEED, 96 "F%dMn", /* minimum speed (rw) */
171 FAN_MAX_SPEED, 97 "F%dMx", /* maximum speed */
172 FAN_SAFE_SPEED, 98 "F%dSf", /* safe speed - not all models */
173 FAN_TARGET_SPEED 99 "F%dTg", /* target speed (manual: rw) */
174}; 100};
175 101
176#define INIT_TIMEOUT_MSECS 5000 /* wait up to 5s for device init ... */ 102#define INIT_TIMEOUT_MSECS 5000 /* wait up to 5s for device init ... */
@@ -184,16 +110,48 @@ static const char* fan_speed_keys[] = {
184#define SENSOR_Y 1 110#define SENSOR_Y 1
185#define SENSOR_Z 2 111#define SENSOR_Z 2
186 112
187/* Structure to be passed to DMI_MATCH function */ 113#define to_index(attr) (to_sensor_dev_attr(attr)->index & 0xffff)
188struct dmi_match_data { 114#define to_option(attr) (to_sensor_dev_attr(attr)->index >> 16)
189/* Indicates whether this computer has an accelerometer. */ 115
190 int accelerometer; 116/* Dynamic device node attributes */
191/* Indicates whether this computer has light sensors and keyboard backlight. */ 117struct applesmc_dev_attr {
192 int light; 118 struct sensor_device_attribute sda; /* hwmon attributes */
193/* Indicates which temperature sensors set to use. */ 119 char name[32]; /* room for node file name */
194 int temperature_set; 120};
121
122/* Dynamic device node group */
123struct applesmc_node_group {
124 char *format; /* format string */
125 void *show; /* show function */
126 void *store; /* store function */
127 int option; /* function argument */
128 struct applesmc_dev_attr *nodes; /* dynamic node array */
195}; 129};
196 130
131/* AppleSMC entry - cached register information */
132struct applesmc_entry {
133 char key[5]; /* four-letter key code */
134 u8 valid; /* set when entry is successfully read once */
135 u8 len; /* bounded by APPLESMC_MAX_DATA_LENGTH */
136 char type[5]; /* four-letter type code (padded) */
137};
138
139/* Register lookup and registers common to all SMCs */
140static struct applesmc_registers {
141 struct mutex mutex; /* register read/write mutex */
142 unsigned int key_count; /* number of SMC registers */
143 unsigned int fan_count; /* number of fans */
144 unsigned int temp_count; /* number of temperature registers */
145 unsigned int temp_begin; /* temperature lower index bound */
146 unsigned int temp_end; /* temperature upper index bound */
147 bool has_accelerometer; /* has motion sensor */
148 bool has_left_light; /* has left light sensor */
149 bool has_right_light; /* has right light sensor */
150 bool has_key_light; /* has keyboard backlight */
151 bool init_complete; /* true when fully initialized */
152 struct applesmc_entry *entry; /* key entries */
153} smcreg;
154
197static const int debug; 155static const int debug;
198static struct platform_device *pdev; 156static struct platform_device *pdev;
199static s16 rest_x; 157static s16 rest_x;
@@ -203,26 +161,13 @@ static u8 backlight_state[2];
203static struct device *hwmon_dev; 161static struct device *hwmon_dev;
204static struct input_polled_dev *applesmc_idev; 162static struct input_polled_dev *applesmc_idev;
205 163
206/* Indicates whether this computer has an accelerometer. */
207static unsigned int applesmc_accelerometer;
208
209/* Indicates whether this computer has light sensors and keyboard backlight. */
210static unsigned int applesmc_light;
211
212/* The number of fans handled by the driver */
213static unsigned int fans_handled;
214
215/* Indicates which temperature sensors set to use. */
216static unsigned int applesmc_temperature_set;
217
218static DEFINE_MUTEX(applesmc_lock);
219
220/* 164/*
221 * Last index written to key_at_index sysfs file, and value to use for all other 165 * Last index written to key_at_index sysfs file, and value to use for all other
222 * key_at_index_* sysfs files. 166 * key_at_index_* sysfs files.
223 */ 167 */
224static unsigned int key_at_index; 168static unsigned int key_at_index;
225 169
170
226static struct workqueue_struct *applesmc_led_wq; 171static struct workqueue_struct *applesmc_led_wq;
227 172
228/* 173/*
@@ -239,17 +184,10 @@ static int __wait_status(u8 val)
239 for (us = APPLESMC_MIN_WAIT; us < APPLESMC_MAX_WAIT; us <<= 1) { 184 for (us = APPLESMC_MIN_WAIT; us < APPLESMC_MAX_WAIT; us <<= 1) {
240 udelay(us); 185 udelay(us);
241 if ((inb(APPLESMC_CMD_PORT) & APPLESMC_STATUS_MASK) == val) { 186 if ((inb(APPLESMC_CMD_PORT) & APPLESMC_STATUS_MASK) == val) {
242 if (debug)
243 printk(KERN_DEBUG
244 "Waited %d us for status %x\n",
245 2 * us - APPLESMC_MIN_WAIT, val);
246 return 0; 187 return 0;
247 } 188 }
248 } 189 }
249 190
250 printk(KERN_WARNING "applesmc: wait status failed: %x != %x\n",
251 val, inb(APPLESMC_CMD_PORT));
252
253 return -EIO; 191 return -EIO;
254} 192}
255 193
@@ -267,157 +205,241 @@ static int send_command(u8 cmd)
267 if ((inb(APPLESMC_CMD_PORT) & APPLESMC_STATUS_MASK) == 0x0c) 205 if ((inb(APPLESMC_CMD_PORT) & APPLESMC_STATUS_MASK) == 0x0c)
268 return 0; 206 return 0;
269 } 207 }
270 printk(KERN_WARNING "applesmc: command failed: %x -> %x\n",
271 cmd, inb(APPLESMC_CMD_PORT));
272 return -EIO; 208 return -EIO;
273} 209}
274 210
275/* 211static int send_argument(const char *key)
276 * applesmc_read_key - reads len bytes from a given key, and put them in buffer.
277 * Returns zero on success or a negative error on failure. Callers must
278 * hold applesmc_lock.
279 */
280static int applesmc_read_key(const char* key, u8* buffer, u8 len)
281{ 212{
282 int i; 213 int i;
283 214
284 if (len > APPLESMC_MAX_DATA_LENGTH) {
285 printk(KERN_ERR "applesmc_read_key: cannot read more than "
286 "%d bytes\n", APPLESMC_MAX_DATA_LENGTH);
287 return -EINVAL;
288 }
289
290 if (send_command(APPLESMC_READ_CMD))
291 return -EIO;
292
293 for (i = 0; i < 4; i++) { 215 for (i = 0; i < 4; i++) {
294 outb(key[i], APPLESMC_DATA_PORT); 216 outb(key[i], APPLESMC_DATA_PORT);
295 if (__wait_status(0x04)) 217 if (__wait_status(0x04))
296 return -EIO; 218 return -EIO;
297 } 219 }
298 if (debug) 220 return 0;
299 printk(KERN_DEBUG "<%s", key); 221}
222
223static int read_smc(u8 cmd, const char *key, u8 *buffer, u8 len)
224{
225 int i;
226
227 if (send_command(cmd) || send_argument(key)) {
228 pr_warn("%s: read arg fail\n", key);
229 return -EIO;
230 }
300 231
301 outb(len, APPLESMC_DATA_PORT); 232 outb(len, APPLESMC_DATA_PORT);
302 if (debug)
303 printk(KERN_DEBUG ">%x", len);
304 233
305 for (i = 0; i < len; i++) { 234 for (i = 0; i < len; i++) {
306 if (__wait_status(0x05)) 235 if (__wait_status(0x05)) {
236 pr_warn("%s: read data fail\n", key);
307 return -EIO; 237 return -EIO;
238 }
308 buffer[i] = inb(APPLESMC_DATA_PORT); 239 buffer[i] = inb(APPLESMC_DATA_PORT);
309 if (debug)
310 printk(KERN_DEBUG "<%x", buffer[i]);
311 } 240 }
312 if (debug)
313 printk(KERN_DEBUG "\n");
314 241
315 return 0; 242 return 0;
316} 243}
317 244
318/* 245static int write_smc(u8 cmd, const char *key, const u8 *buffer, u8 len)
319 * applesmc_write_key - writes len bytes from buffer to a given key.
320 * Returns zero on success or a negative error on failure. Callers must
321 * hold applesmc_lock.
322 */
323static int applesmc_write_key(const char* key, u8* buffer, u8 len)
324{ 246{
325 int i; 247 int i;
326 248
327 if (len > APPLESMC_MAX_DATA_LENGTH) { 249 if (send_command(cmd) || send_argument(key)) {
328 printk(KERN_ERR "applesmc_write_key: cannot write more than " 250 pr_warn("%s: write arg fail\n", key);
329 "%d bytes\n", APPLESMC_MAX_DATA_LENGTH);
330 return -EINVAL;
331 }
332
333 if (send_command(APPLESMC_WRITE_CMD))
334 return -EIO; 251 return -EIO;
335
336 for (i = 0; i < 4; i++) {
337 outb(key[i], APPLESMC_DATA_PORT);
338 if (__wait_status(0x04))
339 return -EIO;
340 } 252 }
341 253
342 outb(len, APPLESMC_DATA_PORT); 254 outb(len, APPLESMC_DATA_PORT);
343 255
344 for (i = 0; i < len; i++) { 256 for (i = 0; i < len; i++) {
345 if (__wait_status(0x04)) 257 if (__wait_status(0x04)) {
258 pr_warn("%s: write data fail\n", key);
346 return -EIO; 259 return -EIO;
260 }
347 outb(buffer[i], APPLESMC_DATA_PORT); 261 outb(buffer[i], APPLESMC_DATA_PORT);
348 } 262 }
349 263
350 return 0; 264 return 0;
351} 265}
352 266
267static int read_register_count(unsigned int *count)
268{
269 __be32 be;
270 int ret;
271
272 ret = read_smc(APPLESMC_READ_CMD, KEY_COUNT_KEY, (u8 *)&be, 4);
273 if (ret)
274 return ret;
275
276 *count = be32_to_cpu(be);
277 return 0;
278}
279
353/* 280/*
354 * applesmc_get_key_at_index - get key at index, and put the result in key 281 * Serialized I/O
355 * (char[6]). Returns zero on success or a negative error on failure. Callers 282 *
356 * must hold applesmc_lock. 283 * Returns zero on success or a negative error on failure.
284 * All functions below are concurrency safe - callers should NOT hold lock.
357 */ 285 */
358static int applesmc_get_key_at_index(int index, char* key) 286
287static int applesmc_read_entry(const struct applesmc_entry *entry,
288 u8 *buf, u8 len)
359{ 289{
360 int i; 290 int ret;
361 u8 readkey[4];
362 readkey[0] = index >> 24;
363 readkey[1] = index >> 16;
364 readkey[2] = index >> 8;
365 readkey[3] = index;
366 291
367 if (send_command(APPLESMC_GET_KEY_BY_INDEX_CMD)) 292 if (entry->len != len)
368 return -EIO; 293 return -EINVAL;
294 mutex_lock(&smcreg.mutex);
295 ret = read_smc(APPLESMC_READ_CMD, entry->key, buf, len);
296 mutex_unlock(&smcreg.mutex);
369 297
370 for (i = 0; i < 4; i++) { 298 return ret;
371 outb(readkey[i], APPLESMC_DATA_PORT); 299}
372 if (__wait_status(0x04)) 300
373 return -EIO; 301static int applesmc_write_entry(const struct applesmc_entry *entry,
302 const u8 *buf, u8 len)
303{
304 int ret;
305
306 if (entry->len != len)
307 return -EINVAL;
308 mutex_lock(&smcreg.mutex);
309 ret = write_smc(APPLESMC_WRITE_CMD, entry->key, buf, len);
310 mutex_unlock(&smcreg.mutex);
311 return ret;
312}
313
314static int applesmc_get_entry_by_index(int index, struct applesmc_entry *entry)
315{
316 struct applesmc_entry *cache = &smcreg.entry[index];
317 __be32 be;
318 int ret;
319
320 if (cache->valid) {
321 memcpy(entry, cache, sizeof(*entry));
322 return 0;
374 } 323 }
375 324
376 outb(4, APPLESMC_DATA_PORT); 325 mutex_lock(&smcreg.mutex);
377 326
378 for (i = 0; i < 4; i++) { 327 be = cpu_to_be32(index);
379 if (__wait_status(0x05)) 328 ret = read_smc(APPLESMC_GET_KEY_BY_INDEX_CMD, (u8 *)&be, cache->key, 4);
380 return -EIO; 329 if (ret)
381 key[i] = inb(APPLESMC_DATA_PORT); 330 goto out;
331 ret = read_smc(APPLESMC_GET_KEY_TYPE_CMD, cache->key, &cache->len, 6);
332 if (ret)
333 goto out;
334
335 cache->type[4] = 0;
336 cache->valid = 1;
337 memcpy(entry, cache, sizeof(*entry));
338
339out:
340 mutex_unlock(&smcreg.mutex);
341 return ret;
342}
343
344static int applesmc_get_lower_bound(unsigned int *lo, const char *key)
345{
346 int begin = 0, end = smcreg.key_count;
347 struct applesmc_entry entry;
348 int ret;
349
350 while (begin != end) {
351 int middle = begin + (end - begin) / 2;
352 ret = applesmc_get_entry_by_index(middle, &entry);
353 if (ret)
354 return ret;
355 if (strcmp(entry.key, key) < 0)
356 begin = middle + 1;
357 else
358 end = middle;
382 } 359 }
383 key[4] = 0;
384 360
361 *lo = begin;
385 return 0; 362 return 0;
386} 363}
387 364
388/* 365static int applesmc_get_upper_bound(unsigned int *hi, const char *key)
389 * applesmc_get_key_type - get key type, and put the result in type (char[6]).
390 * Returns zero on success or a negative error on failure. Callers must
391 * hold applesmc_lock.
392 */
393static int applesmc_get_key_type(char* key, char* type)
394{ 366{
395 int i; 367 int begin = 0, end = smcreg.key_count;
396 368 struct applesmc_entry entry;
397 if (send_command(APPLESMC_GET_KEY_TYPE_CMD)) 369 int ret;
398 return -EIO;
399 370
400 for (i = 0; i < 4; i++) { 371 while (begin != end) {
401 outb(key[i], APPLESMC_DATA_PORT); 372 int middle = begin + (end - begin) / 2;
402 if (__wait_status(0x04)) 373 ret = applesmc_get_entry_by_index(middle, &entry);
403 return -EIO; 374 if (ret)
375 return ret;
376 if (strcmp(key, entry.key) < 0)
377 end = middle;
378 else
379 begin = middle + 1;
404 } 380 }
405 381
406 outb(6, APPLESMC_DATA_PORT); 382 *hi = begin;
383 return 0;
384}
407 385
408 for (i = 0; i < 6; i++) { 386static int applesmc_get_entry_by_key(const char *key,
409 if (__wait_status(0x05)) 387 struct applesmc_entry *entry)
410 return -EIO; 388{
411 type[i] = inb(APPLESMC_DATA_PORT); 389 int begin, end;
412 } 390 int ret;
413 type[5] = 0;
414 391
392 ret = applesmc_get_lower_bound(&begin, key);
393 if (ret)
394 return ret;
395 ret = applesmc_get_upper_bound(&end, key);
396 if (ret)
397 return ret;
398 if (end - begin != 1)
399 return -EINVAL;
400
401 return applesmc_get_entry_by_index(begin, entry);
402}
403
404static int applesmc_read_key(const char *key, u8 *buffer, u8 len)
405{
406 struct applesmc_entry entry;
407 int ret;
408
409 ret = applesmc_get_entry_by_key(key, &entry);
410 if (ret)
411 return ret;
412
413 return applesmc_read_entry(&entry, buffer, len);
414}
415
416static int applesmc_write_key(const char *key, const u8 *buffer, u8 len)
417{
418 struct applesmc_entry entry;
419 int ret;
420
421 ret = applesmc_get_entry_by_key(key, &entry);
422 if (ret)
423 return ret;
424
425 return applesmc_write_entry(&entry, buffer, len);
426}
427
428static int applesmc_has_key(const char *key, bool *value)
429{
430 struct applesmc_entry entry;
431 int ret;
432
433 ret = applesmc_get_entry_by_key(key, &entry);
434 if (ret && ret != -EINVAL)
435 return ret;
436
437 *value = ret == 0 && entry.len;
415 return 0; 438 return 0;
416} 439}
417 440
418/* 441/*
419 * applesmc_read_motion_sensor - Read motion sensor (X, Y or Z). Callers must 442 * applesmc_read_motion_sensor - Read motion sensor (X, Y or Z).
420 * hold applesmc_lock.
421 */ 443 */
422static int applesmc_read_motion_sensor(int index, s16* value) 444static int applesmc_read_motion_sensor(int index, s16* value)
423{ 445{
@@ -452,27 +474,12 @@ static int applesmc_device_init(void)
452 int total, ret = -ENXIO; 474 int total, ret = -ENXIO;
453 u8 buffer[2]; 475 u8 buffer[2];
454 476
455 if (!applesmc_accelerometer) 477 if (!smcreg.has_accelerometer)
456 return 0; 478 return 0;
457 479
458 mutex_lock(&applesmc_lock);
459
460 for (total = INIT_TIMEOUT_MSECS; total > 0; total -= INIT_WAIT_MSECS) { 480 for (total = INIT_TIMEOUT_MSECS; total > 0; total -= INIT_WAIT_MSECS) {
461 if (debug)
462 printk(KERN_DEBUG "applesmc try %d\n", total);
463 if (!applesmc_read_key(MOTION_SENSOR_KEY, buffer, 2) && 481 if (!applesmc_read_key(MOTION_SENSOR_KEY, buffer, 2) &&
464 (buffer[0] != 0x00 || buffer[1] != 0x00)) { 482 (buffer[0] != 0x00 || buffer[1] != 0x00)) {
465 if (total == INIT_TIMEOUT_MSECS) {
466 printk(KERN_DEBUG "applesmc: device has"
467 " already been initialized"
468 " (0x%02x, 0x%02x).\n",
469 buffer[0], buffer[1]);
470 } else {
471 printk(KERN_DEBUG "applesmc: device"
472 " successfully initialized"
473 " (0x%02x, 0x%02x).\n",
474 buffer[0], buffer[1]);
475 }
476 ret = 0; 483 ret = 0;
477 goto out; 484 goto out;
478 } 485 }
@@ -482,31 +489,97 @@ static int applesmc_device_init(void)
482 msleep(INIT_WAIT_MSECS); 489 msleep(INIT_WAIT_MSECS);
483 } 490 }
484 491
485 printk(KERN_WARNING "applesmc: failed to init the device\n"); 492 pr_warn("failed to init the device\n");
486 493
487out: 494out:
488 mutex_unlock(&applesmc_lock);
489 return ret; 495 return ret;
490} 496}
491 497
492/* 498/*
493 * applesmc_get_fan_count - get the number of fans. Callers must NOT hold 499 * applesmc_init_smcreg_try - Try to initialize register cache. Idempotent.
494 * applesmc_lock.
495 */ 500 */
496static int applesmc_get_fan_count(void) 501static int applesmc_init_smcreg_try(void)
497{ 502{
503 struct applesmc_registers *s = &smcreg;
504 u8 tmp[4];
498 int ret; 505 int ret;
499 u8 buffer[1];
500 506
501 mutex_lock(&applesmc_lock); 507 if (s->init_complete)
508 return 0;
502 509
503 ret = applesmc_read_key(FANS_COUNT, buffer, 1); 510 mutex_init(&s->mutex);
504 511
505 mutex_unlock(&applesmc_lock); 512 ret = read_register_count(&s->key_count);
506 if (ret) 513 if (ret)
507 return ret; 514 return ret;
508 else 515
509 return buffer[0]; 516 if (!s->entry)
517 s->entry = kcalloc(s->key_count, sizeof(*s->entry), GFP_KERNEL);
518 if (!s->entry)
519 return -ENOMEM;
520
521 ret = applesmc_read_key(FANS_COUNT, tmp, 1);
522 if (ret)
523 return ret;
524 s->fan_count = tmp[0];
525
526 ret = applesmc_get_lower_bound(&s->temp_begin, "T");
527 if (ret)
528 return ret;
529 ret = applesmc_get_lower_bound(&s->temp_end, "U");
530 if (ret)
531 return ret;
532 s->temp_count = s->temp_end - s->temp_begin;
533
534 ret = applesmc_has_key(LIGHT_SENSOR_LEFT_KEY, &s->has_left_light);
535 if (ret)
536 return ret;
537 ret = applesmc_has_key(LIGHT_SENSOR_RIGHT_KEY, &s->has_right_light);
538 if (ret)
539 return ret;
540 ret = applesmc_has_key(MOTION_SENSOR_KEY, &s->has_accelerometer);
541 if (ret)
542 return ret;
543 ret = applesmc_has_key(BACKLIGHT_KEY, &s->has_key_light);
544 if (ret)
545 return ret;
546
547 s->init_complete = true;
548
549 pr_info("key=%d fan=%d temp=%d acc=%d, light=%d, keyb=%d\n",
550 s->key_count, s->fan_count, s->temp_count,
551 s->has_accelerometer,
552 s->has_left_light + s->has_right_light,
553 s->has_key_light);
554
555 return 0;
556}
557
558/*
559 * applesmc_init_smcreg - Initialize register cache.
560 *
561 * Retries until initialization is successful, or the operation times out.
562 *
563 */
564static int applesmc_init_smcreg(void)
565{
566 int ms, ret;
567
568 for (ms = 0; ms < INIT_TIMEOUT_MSECS; ms += INIT_WAIT_MSECS) {
569 ret = applesmc_init_smcreg_try();
570 if (!ret)
571 return 0;
572 pr_warn("slow init, retrying\n");
573 msleep(INIT_WAIT_MSECS);
574 }
575
576 return ret;
577}
578
579static void applesmc_destroy_smcreg(void)
580{
581 kfree(smcreg.entry);
582 smcreg.entry = NULL;
510} 583}
511 584
512/* Device model stuff */ 585/* Device model stuff */
@@ -514,30 +587,27 @@ static int applesmc_probe(struct platform_device *dev)
514{ 587{
515 int ret; 588 int ret;
516 589
517 ret = applesmc_device_init(); 590 ret = applesmc_init_smcreg();
518 if (ret) 591 if (ret)
519 return ret; 592 return ret;
520 593
521 printk(KERN_INFO "applesmc: device successfully initialized.\n"); 594 applesmc_device_init();
595
522 return 0; 596 return 0;
523} 597}
524 598
525/* Synchronize device with memorized backlight state */ 599/* Synchronize device with memorized backlight state */
526static int applesmc_pm_resume(struct device *dev) 600static int applesmc_pm_resume(struct device *dev)
527{ 601{
528 mutex_lock(&applesmc_lock); 602 if (smcreg.has_key_light)
529 if (applesmc_light)
530 applesmc_write_key(BACKLIGHT_KEY, backlight_state, 2); 603 applesmc_write_key(BACKLIGHT_KEY, backlight_state, 2);
531 mutex_unlock(&applesmc_lock);
532 return 0; 604 return 0;
533} 605}
534 606
535/* Reinitialize device on resume from hibernation */ 607/* Reinitialize device on resume from hibernation */
536static int applesmc_pm_restore(struct device *dev) 608static int applesmc_pm_restore(struct device *dev)
537{ 609{
538 int ret = applesmc_device_init(); 610 applesmc_device_init();
539 if (ret)
540 return ret;
541 return applesmc_pm_resume(dev); 611 return applesmc_pm_resume(dev);
542} 612}
543 613
@@ -571,20 +641,15 @@ static void applesmc_idev_poll(struct input_polled_dev *dev)
571 struct input_dev *idev = dev->input; 641 struct input_dev *idev = dev->input;
572 s16 x, y; 642 s16 x, y;
573 643
574 mutex_lock(&applesmc_lock);
575
576 if (applesmc_read_motion_sensor(SENSOR_X, &x)) 644 if (applesmc_read_motion_sensor(SENSOR_X, &x))
577 goto out; 645 return;
578 if (applesmc_read_motion_sensor(SENSOR_Y, &y)) 646 if (applesmc_read_motion_sensor(SENSOR_Y, &y))
579 goto out; 647 return;
580 648
581 x = -x; 649 x = -x;
582 input_report_abs(idev, ABS_X, x - rest_x); 650 input_report_abs(idev, ABS_X, x - rest_x);
583 input_report_abs(idev, ABS_Y, y - rest_y); 651 input_report_abs(idev, ABS_Y, y - rest_y);
584 input_sync(idev); 652 input_sync(idev);
585
586out:
587 mutex_unlock(&applesmc_lock);
588} 653}
589 654
590/* Sysfs Files */ 655/* Sysfs Files */
@@ -601,8 +666,6 @@ static ssize_t applesmc_position_show(struct device *dev,
601 int ret; 666 int ret;
602 s16 x, y, z; 667 s16 x, y, z;
603 668
604 mutex_lock(&applesmc_lock);
605
606 ret = applesmc_read_motion_sensor(SENSOR_X, &x); 669 ret = applesmc_read_motion_sensor(SENSOR_X, &x);
607 if (ret) 670 if (ret)
608 goto out; 671 goto out;
@@ -614,7 +677,6 @@ static ssize_t applesmc_position_show(struct device *dev,
614 goto out; 677 goto out;
615 678
616out: 679out:
617 mutex_unlock(&applesmc_lock);
618 if (ret) 680 if (ret)
619 return ret; 681 return ret;
620 else 682 else
@@ -624,20 +686,18 @@ out:
624static ssize_t applesmc_light_show(struct device *dev, 686static ssize_t applesmc_light_show(struct device *dev,
625 struct device_attribute *attr, char *sysfsbuf) 687 struct device_attribute *attr, char *sysfsbuf)
626{ 688{
689 struct applesmc_entry entry;
627 static int data_length; 690 static int data_length;
628 int ret; 691 int ret;
629 u8 left = 0, right = 0; 692 u8 left = 0, right = 0;
630 u8 buffer[10], query[6]; 693 u8 buffer[10];
631
632 mutex_lock(&applesmc_lock);
633 694
634 if (!data_length) { 695 if (!data_length) {
635 ret = applesmc_get_key_type(LIGHT_SENSOR_LEFT_KEY, query); 696 ret = applesmc_get_entry_by_key(LIGHT_SENSOR_LEFT_KEY, &entry);
636 if (ret) 697 if (ret)
637 goto out; 698 goto out;
638 data_length = clamp_val(query[0], 0, 10); 699 data_length = clamp_val(entry.len, 0, 10);
639 printk(KERN_INFO "applesmc: light sensor data length set to " 700 pr_info("light sensor data length set to %d\n", data_length);
640 "%d\n", data_length);
641 } 701 }
642 702
643 ret = applesmc_read_key(LIGHT_SENSOR_LEFT_KEY, buffer, data_length); 703 ret = applesmc_read_key(LIGHT_SENSOR_LEFT_KEY, buffer, data_length);
@@ -653,7 +713,6 @@ static ssize_t applesmc_light_show(struct device *dev,
653 right = buffer[2]; 713 right = buffer[2];
654 714
655out: 715out:
656 mutex_unlock(&applesmc_lock);
657 if (ret) 716 if (ret)
658 return ret; 717 return ret;
659 else 718 else
@@ -664,36 +723,46 @@ out:
664static ssize_t applesmc_show_sensor_label(struct device *dev, 723static ssize_t applesmc_show_sensor_label(struct device *dev,
665 struct device_attribute *devattr, char *sysfsbuf) 724 struct device_attribute *devattr, char *sysfsbuf)
666{ 725{
667 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); 726 int index = smcreg.temp_begin + to_index(devattr);
668 const char *key = 727 struct applesmc_entry entry;
669 temperature_sensors_sets[applesmc_temperature_set][attr->index]; 728 int ret;
729
730 ret = applesmc_get_entry_by_index(index, &entry);
731 if (ret)
732 return ret;
670 733
671 return snprintf(sysfsbuf, PAGE_SIZE, "%s\n", key); 734 return snprintf(sysfsbuf, PAGE_SIZE, "%s\n", entry.key);
672} 735}
673 736
674/* Displays degree Celsius * 1000 */ 737/* Displays degree Celsius * 1000 */
675static ssize_t applesmc_show_temperature(struct device *dev, 738static ssize_t applesmc_show_temperature(struct device *dev,
676 struct device_attribute *devattr, char *sysfsbuf) 739 struct device_attribute *devattr, char *sysfsbuf)
677{ 740{
741 int index = smcreg.temp_begin + to_index(devattr);
742 struct applesmc_entry entry;
678 int ret; 743 int ret;
679 u8 buffer[2]; 744 u8 buffer[2];
680 unsigned int temp; 745 unsigned int temp;
681 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
682 const char* key =
683 temperature_sensors_sets[applesmc_temperature_set][attr->index];
684 746
685 mutex_lock(&applesmc_lock); 747 ret = applesmc_get_entry_by_index(index, &entry);
686 748 if (ret)
687 ret = applesmc_read_key(key, buffer, 2); 749 return ret;
688 temp = buffer[0]*1000; 750 if (entry.len > 2)
689 temp += (buffer[1] >> 6) * 250; 751 return -EINVAL;
690
691 mutex_unlock(&applesmc_lock);
692 752
753 ret = applesmc_read_entry(&entry, buffer, entry.len);
693 if (ret) 754 if (ret)
694 return ret; 755 return ret;
695 else 756
696 return snprintf(sysfsbuf, PAGE_SIZE, "%u\n", temp); 757 if (entry.len == 2) {
758 temp = buffer[0]*1000;
759 temp += (buffer[1] >> 6) * 250;
760 } else {
761 /* wild guess */
762 temp = buffer[0] * 4000;
763 }
764
765 return snprintf(sysfsbuf, PAGE_SIZE, "%u\n", temp);
697} 766}
698 767
699static ssize_t applesmc_show_fan_speed(struct device *dev, 768static ssize_t applesmc_show_fan_speed(struct device *dev,
@@ -703,21 +772,12 @@ static ssize_t applesmc_show_fan_speed(struct device *dev,
703 unsigned int speed = 0; 772 unsigned int speed = 0;
704 char newkey[5]; 773 char newkey[5];
705 u8 buffer[2]; 774 u8 buffer[2];
706 struct sensor_device_attribute_2 *sensor_attr =
707 to_sensor_dev_attr_2(attr);
708
709 newkey[0] = fan_speed_keys[sensor_attr->nr][0];
710 newkey[1] = '0' + sensor_attr->index;
711 newkey[2] = fan_speed_keys[sensor_attr->nr][2];
712 newkey[3] = fan_speed_keys[sensor_attr->nr][3];
713 newkey[4] = 0;
714 775
715 mutex_lock(&applesmc_lock); 776 sprintf(newkey, fan_speed_fmt[to_option(attr)], to_index(attr));
716 777
717 ret = applesmc_read_key(newkey, buffer, 2); 778 ret = applesmc_read_key(newkey, buffer, 2);
718 speed = ((buffer[0] << 8 | buffer[1]) >> 2); 779 speed = ((buffer[0] << 8 | buffer[1]) >> 2);
719 780
720 mutex_unlock(&applesmc_lock);
721 if (ret) 781 if (ret)
722 return ret; 782 return ret;
723 else 783 else
@@ -732,27 +792,18 @@ static ssize_t applesmc_store_fan_speed(struct device *dev,
732 u32 speed; 792 u32 speed;
733 char newkey[5]; 793 char newkey[5];
734 u8 buffer[2]; 794 u8 buffer[2];
735 struct sensor_device_attribute_2 *sensor_attr =
736 to_sensor_dev_attr_2(attr);
737 795
738 speed = simple_strtoul(sysfsbuf, NULL, 10); 796 speed = simple_strtoul(sysfsbuf, NULL, 10);
739 797
740 if (speed > 0x4000) /* Bigger than a 14-bit value */ 798 if (speed > 0x4000) /* Bigger than a 14-bit value */
741 return -EINVAL; 799 return -EINVAL;
742 800
743 newkey[0] = fan_speed_keys[sensor_attr->nr][0]; 801 sprintf(newkey, fan_speed_fmt[to_option(attr)], to_index(attr));
744 newkey[1] = '0' + sensor_attr->index;
745 newkey[2] = fan_speed_keys[sensor_attr->nr][2];
746 newkey[3] = fan_speed_keys[sensor_attr->nr][3];
747 newkey[4] = 0;
748
749 mutex_lock(&applesmc_lock);
750 802
751 buffer[0] = (speed >> 6) & 0xff; 803 buffer[0] = (speed >> 6) & 0xff;
752 buffer[1] = (speed << 2) & 0xff; 804 buffer[1] = (speed << 2) & 0xff;
753 ret = applesmc_write_key(newkey, buffer, 2); 805 ret = applesmc_write_key(newkey, buffer, 2);
754 806
755 mutex_unlock(&applesmc_lock);
756 if (ret) 807 if (ret)
757 return ret; 808 return ret;
758 else 809 else
@@ -760,19 +811,15 @@ static ssize_t applesmc_store_fan_speed(struct device *dev,
760} 811}
761 812
762static ssize_t applesmc_show_fan_manual(struct device *dev, 813static ssize_t applesmc_show_fan_manual(struct device *dev,
763 struct device_attribute *devattr, char *sysfsbuf) 814 struct device_attribute *attr, char *sysfsbuf)
764{ 815{
765 int ret; 816 int ret;
766 u16 manual = 0; 817 u16 manual = 0;
767 u8 buffer[2]; 818 u8 buffer[2];
768 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
769
770 mutex_lock(&applesmc_lock);
771 819
772 ret = applesmc_read_key(FANS_MANUAL, buffer, 2); 820 ret = applesmc_read_key(FANS_MANUAL, buffer, 2);
773 manual = ((buffer[0] << 8 | buffer[1]) >> attr->index) & 0x01; 821 manual = ((buffer[0] << 8 | buffer[1]) >> to_index(attr)) & 0x01;
774 822
775 mutex_unlock(&applesmc_lock);
776 if (ret) 823 if (ret)
777 return ret; 824 return ret;
778 else 825 else
@@ -780,28 +827,25 @@ static ssize_t applesmc_show_fan_manual(struct device *dev,
780} 827}
781 828
782static ssize_t applesmc_store_fan_manual(struct device *dev, 829static ssize_t applesmc_store_fan_manual(struct device *dev,
783 struct device_attribute *devattr, 830 struct device_attribute *attr,
784 const char *sysfsbuf, size_t count) 831 const char *sysfsbuf, size_t count)
785{ 832{
786 int ret; 833 int ret;
787 u8 buffer[2]; 834 u8 buffer[2];
788 u32 input; 835 u32 input;
789 u16 val; 836 u16 val;
790 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
791 837
792 input = simple_strtoul(sysfsbuf, NULL, 10); 838 input = simple_strtoul(sysfsbuf, NULL, 10);
793 839
794 mutex_lock(&applesmc_lock);
795
796 ret = applesmc_read_key(FANS_MANUAL, buffer, 2); 840 ret = applesmc_read_key(FANS_MANUAL, buffer, 2);
797 val = (buffer[0] << 8 | buffer[1]); 841 val = (buffer[0] << 8 | buffer[1]);
798 if (ret) 842 if (ret)
799 goto out; 843 goto out;
800 844
801 if (input) 845 if (input)
802 val = val | (0x01 << attr->index); 846 val = val | (0x01 << to_index(attr));
803 else 847 else
804 val = val & ~(0x01 << attr->index); 848 val = val & ~(0x01 << to_index(attr));
805 849
806 buffer[0] = (val >> 8) & 0xFF; 850 buffer[0] = (val >> 8) & 0xFF;
807 buffer[1] = val & 0xFF; 851 buffer[1] = val & 0xFF;
@@ -809,7 +853,6 @@ static ssize_t applesmc_store_fan_manual(struct device *dev,
809 ret = applesmc_write_key(FANS_MANUAL, buffer, 2); 853 ret = applesmc_write_key(FANS_MANUAL, buffer, 2);
810 854
811out: 855out:
812 mutex_unlock(&applesmc_lock);
813 if (ret) 856 if (ret)
814 return ret; 857 return ret;
815 else 858 else
@@ -822,21 +865,12 @@ static ssize_t applesmc_show_fan_position(struct device *dev,
822 int ret; 865 int ret;
823 char newkey[5]; 866 char newkey[5];
824 u8 buffer[17]; 867 u8 buffer[17];
825 struct sensor_device_attribute_2 *sensor_attr =
826 to_sensor_dev_attr_2(attr);
827
828 newkey[0] = FAN_POSITION[0];
829 newkey[1] = '0' + sensor_attr->index;
830 newkey[2] = FAN_POSITION[2];
831 newkey[3] = FAN_POSITION[3];
832 newkey[4] = 0;
833 868
834 mutex_lock(&applesmc_lock); 869 sprintf(newkey, FAN_ID_FMT, to_index(attr));
835 870
836 ret = applesmc_read_key(newkey, buffer, 16); 871 ret = applesmc_read_key(newkey, buffer, 16);
837 buffer[16] = 0; 872 buffer[16] = 0;
838 873
839 mutex_unlock(&applesmc_lock);
840 if (ret) 874 if (ret)
841 return ret; 875 return ret;
842 else 876 else
@@ -852,18 +886,14 @@ static ssize_t applesmc_calibrate_show(struct device *dev,
852static ssize_t applesmc_calibrate_store(struct device *dev, 886static ssize_t applesmc_calibrate_store(struct device *dev,
853 struct device_attribute *attr, const char *sysfsbuf, size_t count) 887 struct device_attribute *attr, const char *sysfsbuf, size_t count)
854{ 888{
855 mutex_lock(&applesmc_lock);
856 applesmc_calibrate(); 889 applesmc_calibrate();
857 mutex_unlock(&applesmc_lock);
858 890
859 return count; 891 return count;
860} 892}
861 893
862static void applesmc_backlight_set(struct work_struct *work) 894static void applesmc_backlight_set(struct work_struct *work)
863{ 895{
864 mutex_lock(&applesmc_lock);
865 applesmc_write_key(BACKLIGHT_KEY, backlight_state, 2); 896 applesmc_write_key(BACKLIGHT_KEY, backlight_state, 2);
866 mutex_unlock(&applesmc_lock);
867} 897}
868static DECLARE_WORK(backlight_work, &applesmc_backlight_set); 898static DECLARE_WORK(backlight_work, &applesmc_backlight_set);
869 899
@@ -886,13 +916,10 @@ static ssize_t applesmc_key_count_show(struct device *dev,
886 u8 buffer[4]; 916 u8 buffer[4];
887 u32 count; 917 u32 count;
888 918
889 mutex_lock(&applesmc_lock);
890
891 ret = applesmc_read_key(KEY_COUNT_KEY, buffer, 4); 919 ret = applesmc_read_key(KEY_COUNT_KEY, buffer, 4);
892 count = ((u32)buffer[0]<<24) + ((u32)buffer[1]<<16) + 920 count = ((u32)buffer[0]<<24) + ((u32)buffer[1]<<16) +
893 ((u32)buffer[2]<<8) + buffer[3]; 921 ((u32)buffer[2]<<8) + buffer[3];
894 922
895 mutex_unlock(&applesmc_lock);
896 if (ret) 923 if (ret)
897 return ret; 924 return ret;
898 else 925 else
@@ -902,113 +929,56 @@ static ssize_t applesmc_key_count_show(struct device *dev,
902static ssize_t applesmc_key_at_index_read_show(struct device *dev, 929static ssize_t applesmc_key_at_index_read_show(struct device *dev,
903 struct device_attribute *attr, char *sysfsbuf) 930 struct device_attribute *attr, char *sysfsbuf)
904{ 931{
905 char key[5]; 932 struct applesmc_entry entry;
906 char info[6];
907 int ret; 933 int ret;
908 934
909 mutex_lock(&applesmc_lock); 935 ret = applesmc_get_entry_by_index(key_at_index, &entry);
910 936 if (ret)
911 ret = applesmc_get_key_at_index(key_at_index, key);
912
913 if (ret || !key[0]) {
914 mutex_unlock(&applesmc_lock);
915
916 return -EINVAL;
917 }
918
919 ret = applesmc_get_key_type(key, info);
920
921 if (ret) {
922 mutex_unlock(&applesmc_lock);
923
924 return ret; 937 return ret;
925 } 938 ret = applesmc_read_entry(&entry, sysfsbuf, entry.len);
926 939 if (ret)
927 /*
928 * info[0] maximum value (APPLESMC_MAX_DATA_LENGTH) is much lower than
929 * PAGE_SIZE, so we don't need any checks before writing to sysfsbuf.
930 */
931 ret = applesmc_read_key(key, sysfsbuf, info[0]);
932
933 mutex_unlock(&applesmc_lock);
934
935 if (!ret) {
936 return info[0];
937 } else {
938 return ret; 940 return ret;
939 } 941
942 return entry.len;
940} 943}
941 944
942static ssize_t applesmc_key_at_index_data_length_show(struct device *dev, 945static ssize_t applesmc_key_at_index_data_length_show(struct device *dev,
943 struct device_attribute *attr, char *sysfsbuf) 946 struct device_attribute *attr, char *sysfsbuf)
944{ 947{
945 char key[5]; 948 struct applesmc_entry entry;
946 char info[6];
947 int ret; 949 int ret;
948 950
949 mutex_lock(&applesmc_lock); 951 ret = applesmc_get_entry_by_index(key_at_index, &entry);
950 952 if (ret)
951 ret = applesmc_get_key_at_index(key_at_index, key);
952
953 if (ret || !key[0]) {
954 mutex_unlock(&applesmc_lock);
955
956 return -EINVAL;
957 }
958
959 ret = applesmc_get_key_type(key, info);
960
961 mutex_unlock(&applesmc_lock);
962
963 if (!ret)
964 return snprintf(sysfsbuf, PAGE_SIZE, "%d\n", info[0]);
965 else
966 return ret; 953 return ret;
954
955 return snprintf(sysfsbuf, PAGE_SIZE, "%d\n", entry.len);
967} 956}
968 957
969static ssize_t applesmc_key_at_index_type_show(struct device *dev, 958static ssize_t applesmc_key_at_index_type_show(struct device *dev,
970 struct device_attribute *attr, char *sysfsbuf) 959 struct device_attribute *attr, char *sysfsbuf)
971{ 960{
972 char key[5]; 961 struct applesmc_entry entry;
973 char info[6];
974 int ret; 962 int ret;
975 963
976 mutex_lock(&applesmc_lock); 964 ret = applesmc_get_entry_by_index(key_at_index, &entry);
977 965 if (ret)
978 ret = applesmc_get_key_at_index(key_at_index, key);
979
980 if (ret || !key[0]) {
981 mutex_unlock(&applesmc_lock);
982
983 return -EINVAL;
984 }
985
986 ret = applesmc_get_key_type(key, info);
987
988 mutex_unlock(&applesmc_lock);
989
990 if (!ret)
991 return snprintf(sysfsbuf, PAGE_SIZE, "%s\n", info+1);
992 else
993 return ret; 966 return ret;
967
968 return snprintf(sysfsbuf, PAGE_SIZE, "%s\n", entry.type);
994} 969}
995 970
996static ssize_t applesmc_key_at_index_name_show(struct device *dev, 971static ssize_t applesmc_key_at_index_name_show(struct device *dev,
997 struct device_attribute *attr, char *sysfsbuf) 972 struct device_attribute *attr, char *sysfsbuf)
998{ 973{
999 char key[5]; 974 struct applesmc_entry entry;
1000 int ret; 975 int ret;
1001 976
1002 mutex_lock(&applesmc_lock); 977 ret = applesmc_get_entry_by_index(key_at_index, &entry);
1003 978 if (ret)
1004 ret = applesmc_get_key_at_index(key_at_index, key); 979 return ret;
1005
1006 mutex_unlock(&applesmc_lock);
1007 980
1008 if (!ret && key[0]) 981 return snprintf(sysfsbuf, PAGE_SIZE, "%s\n", entry.key);
1009 return snprintf(sysfsbuf, PAGE_SIZE, "%s\n", key);
1010 else
1011 return -EINVAL;
1012} 982}
1013 983
1014static ssize_t applesmc_key_at_index_show(struct device *dev, 984static ssize_t applesmc_key_at_index_show(struct device *dev,
@@ -1020,12 +990,8 @@ static ssize_t applesmc_key_at_index_show(struct device *dev,
1020static ssize_t applesmc_key_at_index_store(struct device *dev, 990static ssize_t applesmc_key_at_index_store(struct device *dev,
1021 struct device_attribute *attr, const char *sysfsbuf, size_t count) 991 struct device_attribute *attr, const char *sysfsbuf, size_t count)
1022{ 992{
1023 mutex_lock(&applesmc_lock);
1024
1025 key_at_index = simple_strtoul(sysfsbuf, NULL, 10); 993 key_at_index = simple_strtoul(sysfsbuf, NULL, 10);
1026 994
1027 mutex_unlock(&applesmc_lock);
1028
1029 return count; 995 return count;
1030} 996}
1031 997
@@ -1035,387 +1001,101 @@ static struct led_classdev applesmc_backlight = {
1035 .brightness_set = applesmc_brightness_set, 1001 .brightness_set = applesmc_brightness_set,
1036}; 1002};
1037 1003
1038static DEVICE_ATTR(name, 0444, applesmc_name_show, NULL); 1004static struct applesmc_node_group info_group[] = {
1039 1005 { "name", applesmc_name_show },
1040static DEVICE_ATTR(position, 0444, applesmc_position_show, NULL); 1006 { "key_count", applesmc_key_count_show },
1041static DEVICE_ATTR(calibrate, 0644, 1007 { "key_at_index", applesmc_key_at_index_show, applesmc_key_at_index_store },
1042 applesmc_calibrate_show, applesmc_calibrate_store); 1008 { "key_at_index_name", applesmc_key_at_index_name_show },
1043 1009 { "key_at_index_type", applesmc_key_at_index_type_show },
1044static struct attribute *accelerometer_attributes[] = { 1010 { "key_at_index_data_length", applesmc_key_at_index_data_length_show },
1045 &dev_attr_position.attr, 1011 { "key_at_index_data", applesmc_key_at_index_read_show },
1046 &dev_attr_calibrate.attr, 1012 { }
1047 NULL
1048};
1049
1050static const struct attribute_group accelerometer_attributes_group =
1051 { .attrs = accelerometer_attributes };
1052
1053static DEVICE_ATTR(light, 0444, applesmc_light_show, NULL);
1054
1055static DEVICE_ATTR(key_count, 0444, applesmc_key_count_show, NULL);
1056static DEVICE_ATTR(key_at_index, 0644,
1057 applesmc_key_at_index_show, applesmc_key_at_index_store);
1058static DEVICE_ATTR(key_at_index_name, 0444,
1059 applesmc_key_at_index_name_show, NULL);
1060static DEVICE_ATTR(key_at_index_type, 0444,
1061 applesmc_key_at_index_type_show, NULL);
1062static DEVICE_ATTR(key_at_index_data_length, 0444,
1063 applesmc_key_at_index_data_length_show, NULL);
1064static DEVICE_ATTR(key_at_index_data, 0444,
1065 applesmc_key_at_index_read_show, NULL);
1066
1067static struct attribute *key_enumeration_attributes[] = {
1068 &dev_attr_key_count.attr,
1069 &dev_attr_key_at_index.attr,
1070 &dev_attr_key_at_index_name.attr,
1071 &dev_attr_key_at_index_type.attr,
1072 &dev_attr_key_at_index_data_length.attr,
1073 &dev_attr_key_at_index_data.attr,
1074 NULL
1075};
1076
1077static const struct attribute_group key_enumeration_group =
1078 { .attrs = key_enumeration_attributes };
1079
1080/*
1081 * Macro defining SENSOR_DEVICE_ATTR for a fan sysfs entries.
1082 * - show actual speed
1083 * - show/store minimum speed
1084 * - show maximum speed
1085 * - show safe speed
1086 * - show/store target speed
1087 * - show/store manual mode
1088 */
1089#define sysfs_fan_speeds_offset(offset) \
1090static SENSOR_DEVICE_ATTR_2(fan##offset##_input, S_IRUGO, \
1091 applesmc_show_fan_speed, NULL, 0, offset-1); \
1092\
1093static SENSOR_DEVICE_ATTR_2(fan##offset##_min, S_IRUGO | S_IWUSR, \
1094 applesmc_show_fan_speed, applesmc_store_fan_speed, 1, offset-1); \
1095\
1096static SENSOR_DEVICE_ATTR_2(fan##offset##_max, S_IRUGO, \
1097 applesmc_show_fan_speed, NULL, 2, offset-1); \
1098\
1099static SENSOR_DEVICE_ATTR_2(fan##offset##_safe, S_IRUGO, \
1100 applesmc_show_fan_speed, NULL, 3, offset-1); \
1101\
1102static SENSOR_DEVICE_ATTR_2(fan##offset##_output, S_IRUGO | S_IWUSR, \
1103 applesmc_show_fan_speed, applesmc_store_fan_speed, 4, offset-1); \
1104\
1105static SENSOR_DEVICE_ATTR(fan##offset##_manual, S_IRUGO | S_IWUSR, \
1106 applesmc_show_fan_manual, applesmc_store_fan_manual, offset-1); \
1107\
1108static SENSOR_DEVICE_ATTR(fan##offset##_label, S_IRUGO, \
1109 applesmc_show_fan_position, NULL, offset-1); \
1110\
1111static struct attribute *fan##offset##_attributes[] = { \
1112 &sensor_dev_attr_fan##offset##_input.dev_attr.attr, \
1113 &sensor_dev_attr_fan##offset##_min.dev_attr.attr, \
1114 &sensor_dev_attr_fan##offset##_max.dev_attr.attr, \
1115 &sensor_dev_attr_fan##offset##_safe.dev_attr.attr, \
1116 &sensor_dev_attr_fan##offset##_output.dev_attr.attr, \
1117 &sensor_dev_attr_fan##offset##_manual.dev_attr.attr, \
1118 &sensor_dev_attr_fan##offset##_label.dev_attr.attr, \
1119 NULL \
1120}; 1013};
1121 1014
1122/* 1015static struct applesmc_node_group accelerometer_group[] = {
1123 * Create the needed functions for each fan using the macro defined above 1016 { "position", applesmc_position_show },
1124 * (4 fans are supported) 1017 { "calibrate", applesmc_calibrate_show, applesmc_calibrate_store },
1125 */ 1018 { }
1126sysfs_fan_speeds_offset(1);
1127sysfs_fan_speeds_offset(2);
1128sysfs_fan_speeds_offset(3);
1129sysfs_fan_speeds_offset(4);
1130
1131static const struct attribute_group fan_attribute_groups[] = {
1132 { .attrs = fan1_attributes },
1133 { .attrs = fan2_attributes },
1134 { .attrs = fan3_attributes },
1135 { .attrs = fan4_attributes },
1136}; 1019};
1137 1020
1138/* 1021static struct applesmc_node_group light_sensor_group[] = {
1139 * Temperature sensors sysfs entries. 1022 { "light", applesmc_light_show },
1140 */ 1023 { }
1141static SENSOR_DEVICE_ATTR(temp1_label, S_IRUGO,
1142 applesmc_show_sensor_label, NULL, 0);
1143static SENSOR_DEVICE_ATTR(temp2_label, S_IRUGO,
1144 applesmc_show_sensor_label, NULL, 1);
1145static SENSOR_DEVICE_ATTR(temp3_label, S_IRUGO,
1146 applesmc_show_sensor_label, NULL, 2);
1147static SENSOR_DEVICE_ATTR(temp4_label, S_IRUGO,
1148 applesmc_show_sensor_label, NULL, 3);
1149static SENSOR_DEVICE_ATTR(temp5_label, S_IRUGO,
1150 applesmc_show_sensor_label, NULL, 4);
1151static SENSOR_DEVICE_ATTR(temp6_label, S_IRUGO,
1152 applesmc_show_sensor_label, NULL, 5);
1153static SENSOR_DEVICE_ATTR(temp7_label, S_IRUGO,
1154 applesmc_show_sensor_label, NULL, 6);
1155static SENSOR_DEVICE_ATTR(temp8_label, S_IRUGO,
1156 applesmc_show_sensor_label, NULL, 7);
1157static SENSOR_DEVICE_ATTR(temp9_label, S_IRUGO,
1158 applesmc_show_sensor_label, NULL, 8);
1159static SENSOR_DEVICE_ATTR(temp10_label, S_IRUGO,
1160 applesmc_show_sensor_label, NULL, 9);
1161static SENSOR_DEVICE_ATTR(temp11_label, S_IRUGO,
1162 applesmc_show_sensor_label, NULL, 10);
1163static SENSOR_DEVICE_ATTR(temp12_label, S_IRUGO,
1164 applesmc_show_sensor_label, NULL, 11);
1165static SENSOR_DEVICE_ATTR(temp13_label, S_IRUGO,
1166 applesmc_show_sensor_label, NULL, 12);
1167static SENSOR_DEVICE_ATTR(temp14_label, S_IRUGO,
1168 applesmc_show_sensor_label, NULL, 13);
1169static SENSOR_DEVICE_ATTR(temp15_label, S_IRUGO,
1170 applesmc_show_sensor_label, NULL, 14);
1171static SENSOR_DEVICE_ATTR(temp16_label, S_IRUGO,
1172 applesmc_show_sensor_label, NULL, 15);
1173static SENSOR_DEVICE_ATTR(temp17_label, S_IRUGO,
1174 applesmc_show_sensor_label, NULL, 16);
1175static SENSOR_DEVICE_ATTR(temp18_label, S_IRUGO,
1176 applesmc_show_sensor_label, NULL, 17);
1177static SENSOR_DEVICE_ATTR(temp19_label, S_IRUGO,
1178 applesmc_show_sensor_label, NULL, 18);
1179static SENSOR_DEVICE_ATTR(temp20_label, S_IRUGO,
1180 applesmc_show_sensor_label, NULL, 19);
1181static SENSOR_DEVICE_ATTR(temp21_label, S_IRUGO,
1182 applesmc_show_sensor_label, NULL, 20);
1183static SENSOR_DEVICE_ATTR(temp22_label, S_IRUGO,
1184 applesmc_show_sensor_label, NULL, 21);
1185static SENSOR_DEVICE_ATTR(temp23_label, S_IRUGO,
1186 applesmc_show_sensor_label, NULL, 22);
1187static SENSOR_DEVICE_ATTR(temp24_label, S_IRUGO,
1188 applesmc_show_sensor_label, NULL, 23);
1189static SENSOR_DEVICE_ATTR(temp25_label, S_IRUGO,
1190 applesmc_show_sensor_label, NULL, 24);
1191static SENSOR_DEVICE_ATTR(temp26_label, S_IRUGO,
1192 applesmc_show_sensor_label, NULL, 25);
1193static SENSOR_DEVICE_ATTR(temp27_label, S_IRUGO,
1194 applesmc_show_sensor_label, NULL, 26);
1195static SENSOR_DEVICE_ATTR(temp28_label, S_IRUGO,
1196 applesmc_show_sensor_label, NULL, 27);
1197static SENSOR_DEVICE_ATTR(temp29_label, S_IRUGO,
1198 applesmc_show_sensor_label, NULL, 28);
1199static SENSOR_DEVICE_ATTR(temp30_label, S_IRUGO,
1200 applesmc_show_sensor_label, NULL, 29);
1201static SENSOR_DEVICE_ATTR(temp31_label, S_IRUGO,
1202 applesmc_show_sensor_label, NULL, 30);
1203static SENSOR_DEVICE_ATTR(temp32_label, S_IRUGO,
1204 applesmc_show_sensor_label, NULL, 31);
1205static SENSOR_DEVICE_ATTR(temp33_label, S_IRUGO,
1206 applesmc_show_sensor_label, NULL, 32);
1207static SENSOR_DEVICE_ATTR(temp34_label, S_IRUGO,
1208 applesmc_show_sensor_label, NULL, 33);
1209static SENSOR_DEVICE_ATTR(temp35_label, S_IRUGO,
1210 applesmc_show_sensor_label, NULL, 34);
1211static SENSOR_DEVICE_ATTR(temp36_label, S_IRUGO,
1212 applesmc_show_sensor_label, NULL, 35);
1213static SENSOR_DEVICE_ATTR(temp37_label, S_IRUGO,
1214 applesmc_show_sensor_label, NULL, 36);
1215static SENSOR_DEVICE_ATTR(temp38_label, S_IRUGO,
1216 applesmc_show_sensor_label, NULL, 37);
1217static SENSOR_DEVICE_ATTR(temp39_label, S_IRUGO,
1218 applesmc_show_sensor_label, NULL, 38);
1219static SENSOR_DEVICE_ATTR(temp40_label, S_IRUGO,
1220 applesmc_show_sensor_label, NULL, 39);
1221static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO,
1222 applesmc_show_temperature, NULL, 0);
1223static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO,
1224 applesmc_show_temperature, NULL, 1);
1225static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO,
1226 applesmc_show_temperature, NULL, 2);
1227static SENSOR_DEVICE_ATTR(temp4_input, S_IRUGO,
1228 applesmc_show_temperature, NULL, 3);
1229static SENSOR_DEVICE_ATTR(temp5_input, S_IRUGO,
1230 applesmc_show_temperature, NULL, 4);
1231static SENSOR_DEVICE_ATTR(temp6_input, S_IRUGO,
1232 applesmc_show_temperature, NULL, 5);
1233static SENSOR_DEVICE_ATTR(temp7_input, S_IRUGO,
1234 applesmc_show_temperature, NULL, 6);
1235static SENSOR_DEVICE_ATTR(temp8_input, S_IRUGO,
1236 applesmc_show_temperature, NULL, 7);
1237static SENSOR_DEVICE_ATTR(temp9_input, S_IRUGO,
1238 applesmc_show_temperature, NULL, 8);
1239static SENSOR_DEVICE_ATTR(temp10_input, S_IRUGO,
1240 applesmc_show_temperature, NULL, 9);
1241static SENSOR_DEVICE_ATTR(temp11_input, S_IRUGO,
1242 applesmc_show_temperature, NULL, 10);
1243static SENSOR_DEVICE_ATTR(temp12_input, S_IRUGO,
1244 applesmc_show_temperature, NULL, 11);
1245static SENSOR_DEVICE_ATTR(temp13_input, S_IRUGO,
1246 applesmc_show_temperature, NULL, 12);
1247static SENSOR_DEVICE_ATTR(temp14_input, S_IRUGO,
1248 applesmc_show_temperature, NULL, 13);
1249static SENSOR_DEVICE_ATTR(temp15_input, S_IRUGO,
1250 applesmc_show_temperature, NULL, 14);
1251static SENSOR_DEVICE_ATTR(temp16_input, S_IRUGO,
1252 applesmc_show_temperature, NULL, 15);
1253static SENSOR_DEVICE_ATTR(temp17_input, S_IRUGO,
1254 applesmc_show_temperature, NULL, 16);
1255static SENSOR_DEVICE_ATTR(temp18_input, S_IRUGO,
1256 applesmc_show_temperature, NULL, 17);
1257static SENSOR_DEVICE_ATTR(temp19_input, S_IRUGO,
1258 applesmc_show_temperature, NULL, 18);
1259static SENSOR_DEVICE_ATTR(temp20_input, S_IRUGO,
1260 applesmc_show_temperature, NULL, 19);
1261static SENSOR_DEVICE_ATTR(temp21_input, S_IRUGO,
1262 applesmc_show_temperature, NULL, 20);
1263static SENSOR_DEVICE_ATTR(temp22_input, S_IRUGO,
1264 applesmc_show_temperature, NULL, 21);
1265static SENSOR_DEVICE_ATTR(temp23_input, S_IRUGO,
1266 applesmc_show_temperature, NULL, 22);
1267static SENSOR_DEVICE_ATTR(temp24_input, S_IRUGO,
1268 applesmc_show_temperature, NULL, 23);
1269static SENSOR_DEVICE_ATTR(temp25_input, S_IRUGO,
1270 applesmc_show_temperature, NULL, 24);
1271static SENSOR_DEVICE_ATTR(temp26_input, S_IRUGO,
1272 applesmc_show_temperature, NULL, 25);
1273static SENSOR_DEVICE_ATTR(temp27_input, S_IRUGO,
1274 applesmc_show_temperature, NULL, 26);
1275static SENSOR_DEVICE_ATTR(temp28_input, S_IRUGO,
1276 applesmc_show_temperature, NULL, 27);
1277static SENSOR_DEVICE_ATTR(temp29_input, S_IRUGO,
1278 applesmc_show_temperature, NULL, 28);
1279static SENSOR_DEVICE_ATTR(temp30_input, S_IRUGO,
1280 applesmc_show_temperature, NULL, 29);
1281static SENSOR_DEVICE_ATTR(temp31_input, S_IRUGO,
1282 applesmc_show_temperature, NULL, 30);
1283static SENSOR_DEVICE_ATTR(temp32_input, S_IRUGO,
1284 applesmc_show_temperature, NULL, 31);
1285static SENSOR_DEVICE_ATTR(temp33_input, S_IRUGO,
1286 applesmc_show_temperature, NULL, 32);
1287static SENSOR_DEVICE_ATTR(temp34_input, S_IRUGO,
1288 applesmc_show_temperature, NULL, 33);
1289static SENSOR_DEVICE_ATTR(temp35_input, S_IRUGO,
1290 applesmc_show_temperature, NULL, 34);
1291static SENSOR_DEVICE_ATTR(temp36_input, S_IRUGO,
1292 applesmc_show_temperature, NULL, 35);
1293static SENSOR_DEVICE_ATTR(temp37_input, S_IRUGO,
1294 applesmc_show_temperature, NULL, 36);
1295static SENSOR_DEVICE_ATTR(temp38_input, S_IRUGO,
1296 applesmc_show_temperature, NULL, 37);
1297static SENSOR_DEVICE_ATTR(temp39_input, S_IRUGO,
1298 applesmc_show_temperature, NULL, 38);
1299static SENSOR_DEVICE_ATTR(temp40_input, S_IRUGO,
1300 applesmc_show_temperature, NULL, 39);
1301
1302static struct attribute *label_attributes[] = {
1303 &sensor_dev_attr_temp1_label.dev_attr.attr,
1304 &sensor_dev_attr_temp2_label.dev_attr.attr,
1305 &sensor_dev_attr_temp3_label.dev_attr.attr,
1306 &sensor_dev_attr_temp4_label.dev_attr.attr,
1307 &sensor_dev_attr_temp5_label.dev_attr.attr,
1308 &sensor_dev_attr_temp6_label.dev_attr.attr,
1309 &sensor_dev_attr_temp7_label.dev_attr.attr,
1310 &sensor_dev_attr_temp8_label.dev_attr.attr,
1311 &sensor_dev_attr_temp9_label.dev_attr.attr,
1312 &sensor_dev_attr_temp10_label.dev_attr.attr,
1313 &sensor_dev_attr_temp11_label.dev_attr.attr,
1314 &sensor_dev_attr_temp12_label.dev_attr.attr,
1315 &sensor_dev_attr_temp13_label.dev_attr.attr,
1316 &sensor_dev_attr_temp14_label.dev_attr.attr,
1317 &sensor_dev_attr_temp15_label.dev_attr.attr,
1318 &sensor_dev_attr_temp16_label.dev_attr.attr,
1319 &sensor_dev_attr_temp17_label.dev_attr.attr,
1320 &sensor_dev_attr_temp18_label.dev_attr.attr,
1321 &sensor_dev_attr_temp19_label.dev_attr.attr,
1322 &sensor_dev_attr_temp20_label.dev_attr.attr,
1323 &sensor_dev_attr_temp21_label.dev_attr.attr,
1324 &sensor_dev_attr_temp22_label.dev_attr.attr,
1325 &sensor_dev_attr_temp23_label.dev_attr.attr,
1326 &sensor_dev_attr_temp24_label.dev_attr.attr,
1327 &sensor_dev_attr_temp25_label.dev_attr.attr,
1328 &sensor_dev_attr_temp26_label.dev_attr.attr,
1329 &sensor_dev_attr_temp27_label.dev_attr.attr,
1330 &sensor_dev_attr_temp28_label.dev_attr.attr,
1331 &sensor_dev_attr_temp29_label.dev_attr.attr,
1332 &sensor_dev_attr_temp30_label.dev_attr.attr,
1333 &sensor_dev_attr_temp31_label.dev_attr.attr,
1334 &sensor_dev_attr_temp32_label.dev_attr.attr,
1335 &sensor_dev_attr_temp33_label.dev_attr.attr,
1336 &sensor_dev_attr_temp34_label.dev_attr.attr,
1337 &sensor_dev_attr_temp35_label.dev_attr.attr,
1338 &sensor_dev_attr_temp36_label.dev_attr.attr,
1339 &sensor_dev_attr_temp37_label.dev_attr.attr,
1340 &sensor_dev_attr_temp38_label.dev_attr.attr,
1341 &sensor_dev_attr_temp39_label.dev_attr.attr,
1342 &sensor_dev_attr_temp40_label.dev_attr.attr,
1343 NULL
1344}; 1024};
1345 1025
1346static struct attribute *temperature_attributes[] = { 1026static struct applesmc_node_group fan_group[] = {
1347 &sensor_dev_attr_temp1_input.dev_attr.attr, 1027 { "fan%d_label", applesmc_show_fan_position },
1348 &sensor_dev_attr_temp2_input.dev_attr.attr, 1028 { "fan%d_input", applesmc_show_fan_speed, NULL, 0 },
1349 &sensor_dev_attr_temp3_input.dev_attr.attr, 1029 { "fan%d_min", applesmc_show_fan_speed, applesmc_store_fan_speed, 1 },
1350 &sensor_dev_attr_temp4_input.dev_attr.attr, 1030 { "fan%d_max", applesmc_show_fan_speed, NULL, 2 },
1351 &sensor_dev_attr_temp5_input.dev_attr.attr, 1031 { "fan%d_safe", applesmc_show_fan_speed, NULL, 3 },
1352 &sensor_dev_attr_temp6_input.dev_attr.attr, 1032 { "fan%d_output", applesmc_show_fan_speed, applesmc_store_fan_speed, 4 },
1353 &sensor_dev_attr_temp7_input.dev_attr.attr, 1033 { "fan%d_manual", applesmc_show_fan_manual, applesmc_store_fan_manual },
1354 &sensor_dev_attr_temp8_input.dev_attr.attr, 1034 { }
1355 &sensor_dev_attr_temp9_input.dev_attr.attr,
1356 &sensor_dev_attr_temp10_input.dev_attr.attr,
1357 &sensor_dev_attr_temp11_input.dev_attr.attr,
1358 &sensor_dev_attr_temp12_input.dev_attr.attr,
1359 &sensor_dev_attr_temp13_input.dev_attr.attr,
1360 &sensor_dev_attr_temp14_input.dev_attr.attr,
1361 &sensor_dev_attr_temp15_input.dev_attr.attr,
1362 &sensor_dev_attr_temp16_input.dev_attr.attr,
1363 &sensor_dev_attr_temp17_input.dev_attr.attr,
1364 &sensor_dev_attr_temp18_input.dev_attr.attr,
1365 &sensor_dev_attr_temp19_input.dev_attr.attr,
1366 &sensor_dev_attr_temp20_input.dev_attr.attr,
1367 &sensor_dev_attr_temp21_input.dev_attr.attr,
1368 &sensor_dev_attr_temp22_input.dev_attr.attr,
1369 &sensor_dev_attr_temp23_input.dev_attr.attr,
1370 &sensor_dev_attr_temp24_input.dev_attr.attr,
1371 &sensor_dev_attr_temp25_input.dev_attr.attr,
1372 &sensor_dev_attr_temp26_input.dev_attr.attr,
1373 &sensor_dev_attr_temp27_input.dev_attr.attr,
1374 &sensor_dev_attr_temp28_input.dev_attr.attr,
1375 &sensor_dev_attr_temp29_input.dev_attr.attr,
1376 &sensor_dev_attr_temp30_input.dev_attr.attr,
1377 &sensor_dev_attr_temp31_input.dev_attr.attr,
1378 &sensor_dev_attr_temp32_input.dev_attr.attr,
1379 &sensor_dev_attr_temp33_input.dev_attr.attr,
1380 &sensor_dev_attr_temp34_input.dev_attr.attr,
1381 &sensor_dev_attr_temp35_input.dev_attr.attr,
1382 &sensor_dev_attr_temp36_input.dev_attr.attr,
1383 &sensor_dev_attr_temp37_input.dev_attr.attr,
1384 &sensor_dev_attr_temp38_input.dev_attr.attr,
1385 &sensor_dev_attr_temp39_input.dev_attr.attr,
1386 &sensor_dev_attr_temp40_input.dev_attr.attr,
1387 NULL
1388}; 1035};
1389 1036
1390static const struct attribute_group temperature_attributes_group = 1037static struct applesmc_node_group temp_group[] = {
1391 { .attrs = temperature_attributes }; 1038 { "temp%d_label", applesmc_show_sensor_label },
1392 1039 { "temp%d_input", applesmc_show_temperature },
1393static const struct attribute_group label_attributes_group = { 1040 { }
1394 .attrs = label_attributes
1395}; 1041};
1396 1042
1397/* Module stuff */ 1043/* Module stuff */
1398 1044
1399/* 1045/*
1400 * applesmc_dmi_match - found a match. return one, short-circuiting the hunt. 1046 * applesmc_destroy_nodes - remove files and free associated memory
1401 */ 1047 */
1402static int applesmc_dmi_match(const struct dmi_system_id *id) 1048static void applesmc_destroy_nodes(struct applesmc_node_group *groups)
1403{ 1049{
1404 int i = 0; 1050 struct applesmc_node_group *grp;
1405 struct dmi_match_data* dmi_data = id->driver_data; 1051 struct applesmc_dev_attr *node;
1406 printk(KERN_INFO "applesmc: %s detected:\n", id->ident); 1052
1407 applesmc_accelerometer = dmi_data->accelerometer; 1053 for (grp = groups; grp->nodes; grp++) {
1408 printk(KERN_INFO "applesmc: - Model %s accelerometer\n", 1054 for (node = grp->nodes; node->sda.dev_attr.attr.name; node++)
1409 applesmc_accelerometer ? "with" : "without"); 1055 sysfs_remove_file(&pdev->dev.kobj,
1410 applesmc_light = dmi_data->light; 1056 &node->sda.dev_attr.attr);
1411 printk(KERN_INFO "applesmc: - Model %s light sensors and backlight\n", 1057 kfree(grp->nodes);
1412 applesmc_light ? "with" : "without"); 1058 grp->nodes = NULL;
1413 1059 }
1414 applesmc_temperature_set = dmi_data->temperature_set; 1060}
1415 while (temperature_sensors_sets[applesmc_temperature_set][i] != NULL) 1061
1416 i++; 1062/*
1417 printk(KERN_INFO "applesmc: - Model with %d temperature sensors\n", i); 1063 * applesmc_create_nodes - create a two-dimensional group of sysfs files
1418 return 1; 1064 */
1065static int applesmc_create_nodes(struct applesmc_node_group *groups, int num)
1066{
1067 struct applesmc_node_group *grp;
1068 struct applesmc_dev_attr *node;
1069 struct attribute *attr;
1070 int ret, i;
1071
1072 for (grp = groups; grp->format; grp++) {
1073 grp->nodes = kcalloc(num + 1, sizeof(*node), GFP_KERNEL);
1074 if (!grp->nodes) {
1075 ret = -ENOMEM;
1076 goto out;
1077 }
1078 for (i = 0; i < num; i++) {
1079 node = &grp->nodes[i];
1080 sprintf(node->name, grp->format, i + 1);
1081 node->sda.index = (grp->option << 16) | (i & 0xffff);
1082 node->sda.dev_attr.show = grp->show;
1083 node->sda.dev_attr.store = grp->store;
1084 attr = &node->sda.dev_attr.attr;
1085 attr->name = node->name;
1086 attr->mode = S_IRUGO | (grp->store ? S_IWUSR : 0);
1087 ret = sysfs_create_file(&pdev->dev.kobj, attr);
1088 if (ret) {
1089 attr->name = NULL;
1090 goto out;
1091 }
1092 }
1093 }
1094
1095 return 0;
1096out:
1097 applesmc_destroy_nodes(groups);
1098 return ret;
1419} 1099}
1420 1100
1421/* Create accelerometer ressources */ 1101/* Create accelerometer ressources */
@@ -1424,8 +1104,10 @@ static int applesmc_create_accelerometer(void)
1424 struct input_dev *idev; 1104 struct input_dev *idev;
1425 int ret; 1105 int ret;
1426 1106
1427 ret = sysfs_create_group(&pdev->dev.kobj, 1107 if (!smcreg.has_accelerometer)
1428 &accelerometer_attributes_group); 1108 return 0;
1109
1110 ret = applesmc_create_nodes(accelerometer_group, 1);
1429 if (ret) 1111 if (ret)
1430 goto out; 1112 goto out;
1431 1113
@@ -1462,184 +1144,97 @@ out_idev:
1462 input_free_polled_device(applesmc_idev); 1144 input_free_polled_device(applesmc_idev);
1463 1145
1464out_sysfs: 1146out_sysfs:
1465 sysfs_remove_group(&pdev->dev.kobj, &accelerometer_attributes_group); 1147 applesmc_destroy_nodes(accelerometer_group);
1466 1148
1467out: 1149out:
1468 printk(KERN_WARNING "applesmc: driver init failed (ret=%d)!\n", ret); 1150 pr_warn("driver init failed (ret=%d)!\n", ret);
1469 return ret; 1151 return ret;
1470} 1152}
1471 1153
1472/* Release all ressources used by the accelerometer */ 1154/* Release all ressources used by the accelerometer */
1473static void applesmc_release_accelerometer(void) 1155static void applesmc_release_accelerometer(void)
1474{ 1156{
1157 if (!smcreg.has_accelerometer)
1158 return;
1475 input_unregister_polled_device(applesmc_idev); 1159 input_unregister_polled_device(applesmc_idev);
1476 input_free_polled_device(applesmc_idev); 1160 input_free_polled_device(applesmc_idev);
1477 sysfs_remove_group(&pdev->dev.kobj, &accelerometer_attributes_group); 1161 applesmc_destroy_nodes(accelerometer_group);
1478} 1162}
1479 1163
1480static __initdata struct dmi_match_data applesmc_dmi_data[] = { 1164static int applesmc_create_light_sensor(void)
1481/* MacBook Pro: accelerometer, backlight and temperature set 0 */ 1165{
1482 { .accelerometer = 1, .light = 1, .temperature_set = 0 }, 1166 if (!smcreg.has_left_light)
1483/* MacBook2: accelerometer and temperature set 1 */ 1167 return 0;
1484 { .accelerometer = 1, .light = 0, .temperature_set = 1 }, 1168 return applesmc_create_nodes(light_sensor_group, 1);
1485/* MacBook: accelerometer and temperature set 2 */ 1169}
1486 { .accelerometer = 1, .light = 0, .temperature_set = 2 }, 1170
1487/* MacMini: temperature set 3 */ 1171static void applesmc_release_light_sensor(void)
1488 { .accelerometer = 0, .light = 0, .temperature_set = 3 }, 1172{
1489/* MacPro: temperature set 4 */ 1173 if (!smcreg.has_left_light)
1490 { .accelerometer = 0, .light = 0, .temperature_set = 4 }, 1174 return;
1491/* iMac: temperature set 5 */ 1175 applesmc_destroy_nodes(light_sensor_group);
1492 { .accelerometer = 0, .light = 0, .temperature_set = 5 }, 1176}
1493/* MacBook3, MacBook4: accelerometer and temperature set 6 */ 1177
1494 { .accelerometer = 1, .light = 0, .temperature_set = 6 }, 1178static int applesmc_create_key_backlight(void)
1495/* MacBook Air: accelerometer, backlight and temperature set 7 */ 1179{
1496 { .accelerometer = 1, .light = 1, .temperature_set = 7 }, 1180 if (!smcreg.has_key_light)
1497/* MacBook Pro 4: accelerometer, backlight and temperature set 8 */ 1181 return 0;
1498 { .accelerometer = 1, .light = 1, .temperature_set = 8 }, 1182 applesmc_led_wq = create_singlethread_workqueue("applesmc-led");
1499/* MacBook Pro 3: accelerometer, backlight and temperature set 9 */ 1183 if (!applesmc_led_wq)
1500 { .accelerometer = 1, .light = 1, .temperature_set = 9 }, 1184 return -ENOMEM;
1501/* iMac 5: light sensor only, temperature set 10 */ 1185 return led_classdev_register(&pdev->dev, &applesmc_backlight);
1502 { .accelerometer = 0, .light = 0, .temperature_set = 10 }, 1186}
1503/* MacBook 5: accelerometer, backlight and temperature set 11 */ 1187
1504 { .accelerometer = 1, .light = 1, .temperature_set = 11 }, 1188static void applesmc_release_key_backlight(void)
1505/* MacBook Pro 5: accelerometer, backlight and temperature set 12 */ 1189{
1506 { .accelerometer = 1, .light = 1, .temperature_set = 12 }, 1190 if (!smcreg.has_key_light)
1507/* iMac 8: light sensor only, temperature set 13 */ 1191 return;
1508 { .accelerometer = 0, .light = 0, .temperature_set = 13 }, 1192 led_classdev_unregister(&applesmc_backlight);
1509/* iMac 6: light sensor only, temperature set 14 */ 1193 destroy_workqueue(applesmc_led_wq);
1510 { .accelerometer = 0, .light = 0, .temperature_set = 14 }, 1194}
1511/* MacBook Air 2,1: accelerometer, backlight and temperature set 15 */ 1195
1512 { .accelerometer = 1, .light = 1, .temperature_set = 15 }, 1196static int applesmc_dmi_match(const struct dmi_system_id *id)
1513/* MacPro3,1: temperature set 16 */ 1197{
1514 { .accelerometer = 0, .light = 0, .temperature_set = 16 }, 1198 pr_info("%s detected\n", id->ident);
1515/* iMac 9,1: light sensor only, temperature set 17 */ 1199 return 1;
1516 { .accelerometer = 0, .light = 0, .temperature_set = 17 }, 1200}
1517/* MacBook Pro 2,2: accelerometer, backlight and temperature set 18 */
1518 { .accelerometer = 1, .light = 1, .temperature_set = 18 },
1519/* MacBook Pro 5,3: accelerometer, backlight and temperature set 19 */
1520 { .accelerometer = 1, .light = 1, .temperature_set = 19 },
1521/* MacBook Pro 5,4: accelerometer, backlight and temperature set 20 */
1522 { .accelerometer = 1, .light = 1, .temperature_set = 20 },
1523/* MacBook Pro 6,2: accelerometer, backlight and temperature set 21 */
1524 { .accelerometer = 1, .light = 1, .temperature_set = 21 },
1525/* MacBook Pro 7,1: accelerometer, backlight and temperature set 22 */
1526 { .accelerometer = 1, .light = 1, .temperature_set = 22 },
1527};
1528 1201
1529/* Note that DMI_MATCH(...,"MacBook") will match "MacBookPro1,1". 1202/* Note that DMI_MATCH(...,"MacBook") will match "MacBookPro1,1".
1530 * So we need to put "Apple MacBook Pro" before "Apple MacBook". */ 1203 * So we need to put "Apple MacBook Pro" before "Apple MacBook". */
1531static __initdata struct dmi_system_id applesmc_whitelist[] = { 1204static __initdata struct dmi_system_id applesmc_whitelist[] = {
1532 { applesmc_dmi_match, "Apple MacBook Air 2", {
1533 DMI_MATCH(DMI_BOARD_VENDOR, "Apple"),
1534 DMI_MATCH(DMI_PRODUCT_NAME, "MacBookAir2") },
1535 &applesmc_dmi_data[15]},
1536 { applesmc_dmi_match, "Apple MacBook Air", { 1205 { applesmc_dmi_match, "Apple MacBook Air", {
1537 DMI_MATCH(DMI_BOARD_VENDOR, "Apple"), 1206 DMI_MATCH(DMI_BOARD_VENDOR, "Apple"),
1538 DMI_MATCH(DMI_PRODUCT_NAME, "MacBookAir") }, 1207 DMI_MATCH(DMI_PRODUCT_NAME, "MacBookAir") },
1539 &applesmc_dmi_data[7]}, 1208 },
1540 { applesmc_dmi_match, "Apple MacBook Pro 7", {
1541 DMI_MATCH(DMI_BOARD_VENDOR, "Apple"),
1542 DMI_MATCH(DMI_PRODUCT_NAME, "MacBookPro7") },
1543 &applesmc_dmi_data[22]},
1544 { applesmc_dmi_match, "Apple MacBook Pro 5,4", {
1545 DMI_MATCH(DMI_BOARD_VENDOR, "Apple"),
1546 DMI_MATCH(DMI_PRODUCT_NAME, "MacBookPro5,4") },
1547 &applesmc_dmi_data[20]},
1548 { applesmc_dmi_match, "Apple MacBook Pro 5,3", {
1549 DMI_MATCH(DMI_BOARD_VENDOR, "Apple"),
1550 DMI_MATCH(DMI_PRODUCT_NAME, "MacBookPro5,3") },
1551 &applesmc_dmi_data[19]},
1552 { applesmc_dmi_match, "Apple MacBook Pro 6", {
1553 DMI_MATCH(DMI_BOARD_VENDOR, "Apple"),
1554 DMI_MATCH(DMI_PRODUCT_NAME, "MacBookPro6") },
1555 &applesmc_dmi_data[21]},
1556 { applesmc_dmi_match, "Apple MacBook Pro 5", {
1557 DMI_MATCH(DMI_BOARD_VENDOR, "Apple"),
1558 DMI_MATCH(DMI_PRODUCT_NAME, "MacBookPro5") },
1559 &applesmc_dmi_data[12]},
1560 { applesmc_dmi_match, "Apple MacBook Pro 4", {
1561 DMI_MATCH(DMI_BOARD_VENDOR, "Apple"),
1562 DMI_MATCH(DMI_PRODUCT_NAME, "MacBookPro4") },
1563 &applesmc_dmi_data[8]},
1564 { applesmc_dmi_match, "Apple MacBook Pro 3", {
1565 DMI_MATCH(DMI_BOARD_VENDOR, "Apple"),
1566 DMI_MATCH(DMI_PRODUCT_NAME, "MacBookPro3") },
1567 &applesmc_dmi_data[9]},
1568 { applesmc_dmi_match, "Apple MacBook Pro 2,2", {
1569 DMI_MATCH(DMI_BOARD_VENDOR, "Apple Computer, Inc."),
1570 DMI_MATCH(DMI_PRODUCT_NAME, "MacBookPro2,2") },
1571 &applesmc_dmi_data[18]},
1572 { applesmc_dmi_match, "Apple MacBook Pro", { 1209 { applesmc_dmi_match, "Apple MacBook Pro", {
1573 DMI_MATCH(DMI_BOARD_VENDOR,"Apple"), 1210 DMI_MATCH(DMI_BOARD_VENDOR,"Apple"),
1574 DMI_MATCH(DMI_PRODUCT_NAME,"MacBookPro") }, 1211 DMI_MATCH(DMI_PRODUCT_NAME,"MacBookPro") },
1575 &applesmc_dmi_data[0]}, 1212 },
1576 { applesmc_dmi_match, "Apple MacBook (v2)", {
1577 DMI_MATCH(DMI_BOARD_VENDOR,"Apple"),
1578 DMI_MATCH(DMI_PRODUCT_NAME,"MacBook2") },
1579 &applesmc_dmi_data[1]},
1580 { applesmc_dmi_match, "Apple MacBook (v3)", {
1581 DMI_MATCH(DMI_BOARD_VENDOR,"Apple"),
1582 DMI_MATCH(DMI_PRODUCT_NAME,"MacBook3") },
1583 &applesmc_dmi_data[6]},
1584 { applesmc_dmi_match, "Apple MacBook 4", {
1585 DMI_MATCH(DMI_BOARD_VENDOR, "Apple"),
1586 DMI_MATCH(DMI_PRODUCT_NAME, "MacBook4") },
1587 &applesmc_dmi_data[6]},
1588 { applesmc_dmi_match, "Apple MacBook 5", {
1589 DMI_MATCH(DMI_BOARD_VENDOR, "Apple"),
1590 DMI_MATCH(DMI_PRODUCT_NAME, "MacBook5") },
1591 &applesmc_dmi_data[11]},
1592 { applesmc_dmi_match, "Apple MacBook", { 1213 { applesmc_dmi_match, "Apple MacBook", {
1593 DMI_MATCH(DMI_BOARD_VENDOR,"Apple"), 1214 DMI_MATCH(DMI_BOARD_VENDOR,"Apple"),
1594 DMI_MATCH(DMI_PRODUCT_NAME,"MacBook") }, 1215 DMI_MATCH(DMI_PRODUCT_NAME,"MacBook") },
1595 &applesmc_dmi_data[2]}, 1216 },
1596 { applesmc_dmi_match, "Apple Macmini", { 1217 { applesmc_dmi_match, "Apple Macmini", {
1597 DMI_MATCH(DMI_BOARD_VENDOR,"Apple"), 1218 DMI_MATCH(DMI_BOARD_VENDOR,"Apple"),
1598 DMI_MATCH(DMI_PRODUCT_NAME,"Macmini") }, 1219 DMI_MATCH(DMI_PRODUCT_NAME,"Macmini") },
1599 &applesmc_dmi_data[3]}, 1220 },
1600 { applesmc_dmi_match, "Apple MacPro2", {
1601 DMI_MATCH(DMI_BOARD_VENDOR,"Apple"),
1602 DMI_MATCH(DMI_PRODUCT_NAME,"MacPro2") },
1603 &applesmc_dmi_data[4]},
1604 { applesmc_dmi_match, "Apple MacPro3", {
1605 DMI_MATCH(DMI_BOARD_VENDOR, "Apple"),
1606 DMI_MATCH(DMI_PRODUCT_NAME, "MacPro3") },
1607 &applesmc_dmi_data[16]},
1608 { applesmc_dmi_match, "Apple MacPro", { 1221 { applesmc_dmi_match, "Apple MacPro", {
1609 DMI_MATCH(DMI_BOARD_VENDOR, "Apple"), 1222 DMI_MATCH(DMI_BOARD_VENDOR, "Apple"),
1610 DMI_MATCH(DMI_PRODUCT_NAME, "MacPro") }, 1223 DMI_MATCH(DMI_PRODUCT_NAME, "MacPro") },
1611 &applesmc_dmi_data[4]}, 1224 },
1612 { applesmc_dmi_match, "Apple iMac 9,1", {
1613 DMI_MATCH(DMI_BOARD_VENDOR, "Apple Inc."),
1614 DMI_MATCH(DMI_PRODUCT_NAME, "iMac9,1") },
1615 &applesmc_dmi_data[17]},
1616 { applesmc_dmi_match, "Apple iMac 8", {
1617 DMI_MATCH(DMI_BOARD_VENDOR, "Apple"),
1618 DMI_MATCH(DMI_PRODUCT_NAME, "iMac8") },
1619 &applesmc_dmi_data[13]},
1620 { applesmc_dmi_match, "Apple iMac 6", {
1621 DMI_MATCH(DMI_BOARD_VENDOR, "Apple"),
1622 DMI_MATCH(DMI_PRODUCT_NAME, "iMac6") },
1623 &applesmc_dmi_data[14]},
1624 { applesmc_dmi_match, "Apple iMac 5", {
1625 DMI_MATCH(DMI_BOARD_VENDOR, "Apple"),
1626 DMI_MATCH(DMI_PRODUCT_NAME, "iMac5") },
1627 &applesmc_dmi_data[10]},
1628 { applesmc_dmi_match, "Apple iMac", { 1225 { applesmc_dmi_match, "Apple iMac", {
1629 DMI_MATCH(DMI_BOARD_VENDOR,"Apple"), 1226 DMI_MATCH(DMI_BOARD_VENDOR,"Apple"),
1630 DMI_MATCH(DMI_PRODUCT_NAME,"iMac") }, 1227 DMI_MATCH(DMI_PRODUCT_NAME,"iMac") },
1631 &applesmc_dmi_data[5]}, 1228 },
1632 { .ident = NULL } 1229 { .ident = NULL }
1633}; 1230};
1634 1231
1635static int __init applesmc_init(void) 1232static int __init applesmc_init(void)
1636{ 1233{
1637 int ret; 1234 int ret;
1638 int count;
1639 int i;
1640 1235
1641 if (!dmi_check_system(applesmc_whitelist)) { 1236 if (!dmi_check_system(applesmc_whitelist)) {
1642 printk(KERN_WARNING "applesmc: supported laptop not found!\n"); 1237 pr_warn("supported laptop not found!\n");
1643 ret = -ENODEV; 1238 ret = -ENODEV;
1644 goto out; 1239 goto out;
1645 } 1240 }
@@ -1661,83 +1256,34 @@ static int __init applesmc_init(void)
1661 goto out_driver; 1256 goto out_driver;
1662 } 1257 }
1663 1258
1664 ret = sysfs_create_file(&pdev->dev.kobj, &dev_attr_name.attr); 1259 /* create register cache */
1260 ret = applesmc_init_smcreg();
1665 if (ret) 1261 if (ret)
1666 goto out_device; 1262 goto out_device;
1667 1263
1668 /* Create key enumeration sysfs files */ 1264 ret = applesmc_create_nodes(info_group, 1);
1669 ret = sysfs_create_group(&pdev->dev.kobj, &key_enumeration_group);
1670 if (ret) 1265 if (ret)
1671 goto out_name; 1266 goto out_smcreg;
1672 1267
1673 /* create fan files */ 1268 ret = applesmc_create_nodes(fan_group, smcreg.fan_count);
1674 count = applesmc_get_fan_count(); 1269 if (ret)
1675 if (count < 0) 1270 goto out_info;
1676 printk(KERN_ERR "applesmc: Cannot get the number of fans.\n");
1677 else
1678 printk(KERN_INFO "applesmc: %d fans found.\n", count);
1679
1680 if (count > 4) {
1681 count = 4;
1682 printk(KERN_WARNING "applesmc: More than 4 fans found,"
1683 " but at most 4 fans are supported"
1684 " by the driver.\n");
1685 }
1686
1687 while (fans_handled < count) {
1688 ret = sysfs_create_group(&pdev->dev.kobj,
1689 &fan_attribute_groups[fans_handled]);
1690 if (ret)
1691 goto out_fans;
1692 fans_handled++;
1693 }
1694
1695 for (i = 0;
1696 temperature_sensors_sets[applesmc_temperature_set][i] != NULL;
1697 i++) {
1698 if (temperature_attributes[i] == NULL ||
1699 label_attributes[i] == NULL) {
1700 printk(KERN_ERR "applesmc: More temperature sensors "
1701 "in temperature_sensors_sets (at least %i)"
1702 "than available sysfs files in "
1703 "temperature_attributes (%i), please report "
1704 "this bug.\n", i, i-1);
1705 goto out_temperature;
1706 }
1707 ret = sysfs_create_file(&pdev->dev.kobj,
1708 temperature_attributes[i]);
1709 if (ret)
1710 goto out_temperature;
1711 ret = sysfs_create_file(&pdev->dev.kobj,
1712 label_attributes[i]);
1713 if (ret)
1714 goto out_temperature;
1715 }
1716 1271
1717 if (applesmc_accelerometer) { 1272 ret = applesmc_create_nodes(temp_group, smcreg.temp_count);
1718 ret = applesmc_create_accelerometer(); 1273 if (ret)
1719 if (ret) 1274 goto out_fans;
1720 goto out_temperature;
1721 }
1722 1275
1723 if (applesmc_light) { 1276 ret = applesmc_create_accelerometer();
1724 /* Add light sensor file */ 1277 if (ret)
1725 ret = sysfs_create_file(&pdev->dev.kobj, &dev_attr_light.attr); 1278 goto out_temperature;
1726 if (ret)
1727 goto out_accelerometer;
1728 1279
1729 /* Create the workqueue */ 1280 ret = applesmc_create_light_sensor();
1730 applesmc_led_wq = create_singlethread_workqueue("applesmc-led"); 1281 if (ret)
1731 if (!applesmc_led_wq) { 1282 goto out_accelerometer;
1732 ret = -ENOMEM;
1733 goto out_light_sysfs;
1734 }
1735 1283
1736 /* register as a led device */ 1284 ret = applesmc_create_key_backlight();
1737 ret = led_classdev_register(&pdev->dev, &applesmc_backlight); 1285 if (ret)
1738 if (ret < 0) 1286 goto out_light_sysfs;
1739 goto out_light_wq;
1740 }
1741 1287
1742 hwmon_dev = hwmon_device_register(&pdev->dev); 1288 hwmon_dev = hwmon_device_register(&pdev->dev);
1743 if (IS_ERR(hwmon_dev)) { 1289 if (IS_ERR(hwmon_dev)) {
@@ -1745,32 +1291,24 @@ static int __init applesmc_init(void)
1745 goto out_light_ledclass; 1291 goto out_light_ledclass;
1746 } 1292 }
1747 1293
1748 printk(KERN_INFO "applesmc: driver successfully loaded.\n"); 1294 pr_info("driver successfully loaded\n");
1749 1295
1750 return 0; 1296 return 0;
1751 1297
1752out_light_ledclass: 1298out_light_ledclass:
1753 if (applesmc_light) 1299 applesmc_release_key_backlight();
1754 led_classdev_unregister(&applesmc_backlight);
1755out_light_wq:
1756 if (applesmc_light)
1757 destroy_workqueue(applesmc_led_wq);
1758out_light_sysfs: 1300out_light_sysfs:
1759 if (applesmc_light) 1301 applesmc_release_light_sensor();
1760 sysfs_remove_file(&pdev->dev.kobj, &dev_attr_light.attr);
1761out_accelerometer: 1302out_accelerometer:
1762 if (applesmc_accelerometer) 1303 applesmc_release_accelerometer();
1763 applesmc_release_accelerometer();
1764out_temperature: 1304out_temperature:
1765 sysfs_remove_group(&pdev->dev.kobj, &label_attributes_group); 1305 applesmc_destroy_nodes(temp_group);
1766 sysfs_remove_group(&pdev->dev.kobj, &temperature_attributes_group);
1767out_fans: 1306out_fans:
1768 while (fans_handled) 1307 applesmc_destroy_nodes(fan_group);
1769 sysfs_remove_group(&pdev->dev.kobj, 1308out_info:
1770 &fan_attribute_groups[--fans_handled]); 1309 applesmc_destroy_nodes(info_group);
1771 sysfs_remove_group(&pdev->dev.kobj, &key_enumeration_group); 1310out_smcreg:
1772out_name: 1311 applesmc_destroy_smcreg();
1773 sysfs_remove_file(&pdev->dev.kobj, &dev_attr_name.attr);
1774out_device: 1312out_device:
1775 platform_device_unregister(pdev); 1313 platform_device_unregister(pdev);
1776out_driver: 1314out_driver:
@@ -1778,32 +1316,25 @@ out_driver:
1778out_region: 1316out_region:
1779 release_region(APPLESMC_DATA_PORT, APPLESMC_NR_PORTS); 1317 release_region(APPLESMC_DATA_PORT, APPLESMC_NR_PORTS);
1780out: 1318out:
1781 printk(KERN_WARNING "applesmc: driver init failed (ret=%d)!\n", ret); 1319 pr_warn("driver init failed (ret=%d)!\n", ret);
1782 return ret; 1320 return ret;
1783} 1321}
1784 1322
1785static void __exit applesmc_exit(void) 1323static void __exit applesmc_exit(void)
1786{ 1324{
1787 hwmon_device_unregister(hwmon_dev); 1325 hwmon_device_unregister(hwmon_dev);
1788 if (applesmc_light) { 1326 applesmc_release_key_backlight();
1789 led_classdev_unregister(&applesmc_backlight); 1327 applesmc_release_light_sensor();
1790 destroy_workqueue(applesmc_led_wq); 1328 applesmc_release_accelerometer();
1791 sysfs_remove_file(&pdev->dev.kobj, &dev_attr_light.attr); 1329 applesmc_destroy_nodes(temp_group);
1792 } 1330 applesmc_destroy_nodes(fan_group);
1793 if (applesmc_accelerometer) 1331 applesmc_destroy_nodes(info_group);
1794 applesmc_release_accelerometer(); 1332 applesmc_destroy_smcreg();
1795 sysfs_remove_group(&pdev->dev.kobj, &label_attributes_group);
1796 sysfs_remove_group(&pdev->dev.kobj, &temperature_attributes_group);
1797 while (fans_handled)
1798 sysfs_remove_group(&pdev->dev.kobj,
1799 &fan_attribute_groups[--fans_handled]);
1800 sysfs_remove_group(&pdev->dev.kobj, &key_enumeration_group);
1801 sysfs_remove_file(&pdev->dev.kobj, &dev_attr_name.attr);
1802 platform_device_unregister(pdev); 1333 platform_device_unregister(pdev);
1803 platform_driver_unregister(&applesmc_driver); 1334 platform_driver_unregister(&applesmc_driver);
1804 release_region(APPLESMC_DATA_PORT, APPLESMC_NR_PORTS); 1335 release_region(APPLESMC_DATA_PORT, APPLESMC_NR_PORTS);
1805 1336
1806 printk(KERN_INFO "applesmc: driver unloaded.\n"); 1337 pr_info("driver unloaded\n");
1807} 1338}
1808 1339
1809module_init(applesmc_init); 1340module_init(applesmc_init);