serial: MPSC: Fix coding style and whitespace issues
[linux-2.6] / drivers / hwmon / applesmc.c
1 /*
2  * drivers/hwmon/applesmc.c - driver for Apple's SMC (accelerometer, temperature
3  * sensors, fan control, keyboard backlight control) used in Intel-based Apple
4  * computers.
5  *
6  * Copyright (C) 2007 Nicolas Boichat <nicolas@boichat.ch>
7  *
8  * Based on hdaps.c driver:
9  * Copyright (C) 2005 Robert Love <rml@novell.com>
10  * Copyright (C) 2005 Jesper Juhl <jesper.juhl@gmail.com>
11  *
12  * Fan control based on smcFanControl:
13  * Copyright (C) 2006 Hendrik Holtmann <holtmann@mac.com>
14  *
15  * This program is free software; you can redistribute it and/or modify it
16  * under the terms of the GNU General Public License v2 as published by the
17  * Free Software Foundation.
18  *
19  * This program is distributed in the hope that it will be useful, but WITHOUT
20  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
22  * more details.
23  *
24  * You should have received a copy of the GNU General Public License along with
25  * this program; if not, write to the Free Software Foundation, Inc.,
26  * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
27  */
28
29 #include <linux/delay.h>
30 #include <linux/platform_device.h>
31 #include <linux/input.h>
32 #include <linux/kernel.h>
33 #include <linux/module.h>
34 #include <linux/timer.h>
35 #include <linux/dmi.h>
36 #include <linux/mutex.h>
37 #include <linux/hwmon-sysfs.h>
38 #include <asm/io.h>
39 #include <linux/leds.h>
40 #include <linux/hwmon.h>
41 #include <linux/workqueue.h>
42
43 /* data port used by Apple SMC */
44 #define APPLESMC_DATA_PORT      0x300
45 /* command/status port used by Apple SMC */
46 #define APPLESMC_CMD_PORT       0x304
47
48 #define APPLESMC_NR_PORTS       32 /* 0x300-0x31f */
49
50 #define APPLESMC_MAX_DATA_LENGTH 32
51
52 #define APPLESMC_STATUS_MASK    0x0f
53 #define APPLESMC_READ_CMD       0x10
54 #define APPLESMC_WRITE_CMD      0x11
55 #define APPLESMC_GET_KEY_BY_INDEX_CMD   0x12
56 #define APPLESMC_GET_KEY_TYPE_CMD       0x13
57
58 #define KEY_COUNT_KEY           "#KEY" /* r-o ui32 */
59
60 #define LIGHT_SENSOR_LEFT_KEY   "ALV0" /* r-o {alv (6 bytes) */
61 #define LIGHT_SENSOR_RIGHT_KEY  "ALV1" /* r-o {alv (6 bytes) */
62 #define BACKLIGHT_KEY           "LKSB" /* w-o {lkb (2 bytes) */
63
64 #define CLAMSHELL_KEY           "MSLD" /* r-o ui8 (unused) */
65
66 #define MOTION_SENSOR_X_KEY     "MO_X" /* r-o sp78 (2 bytes) */
67 #define MOTION_SENSOR_Y_KEY     "MO_Y" /* r-o sp78 (2 bytes) */
68 #define MOTION_SENSOR_Z_KEY     "MO_Z" /* r-o sp78 (2 bytes) */
69 #define MOTION_SENSOR_KEY       "MOCN" /* r/w ui16 */
70
71 #define FANS_COUNT              "FNum" /* r-o ui8 */
72 #define FANS_MANUAL             "FS! " /* r-w ui16 */
73 #define FAN_ACTUAL_SPEED        "F0Ac" /* r-o fpe2 (2 bytes) */
74 #define FAN_MIN_SPEED           "F0Mn" /* r-o fpe2 (2 bytes) */
75 #define FAN_MAX_SPEED           "F0Mx" /* r-o fpe2 (2 bytes) */
76 #define FAN_SAFE_SPEED          "F0Sf" /* r-o fpe2 (2 bytes) */
77 #define FAN_TARGET_SPEED        "F0Tg" /* r-w fpe2 (2 bytes) */
78 #define FAN_POSITION            "F0ID" /* r-o char[16] */
79
80 /*
81  * Temperature sensors keys (sp78 - 2 bytes).
82  * First set for Macbook(Pro), second for Macmini.
83  */
84 static const char* temperature_sensors_sets[][13] = {
85         { "TA0P", "TB0T", "TC0D", "TC0P", "TG0H", "TG0P", "TG0T", "Th0H",
86           "Th1H", "Tm0P", "Ts0P", "Ts1P", NULL },
87         { "TC0D", "TC0P", NULL }
88 };
89
90 /* List of keys used to read/write fan speeds */
91 static const char* fan_speed_keys[] = {
92         FAN_ACTUAL_SPEED,
93         FAN_MIN_SPEED,
94         FAN_MAX_SPEED,
95         FAN_SAFE_SPEED,
96         FAN_TARGET_SPEED
97 };
98
99 #define INIT_TIMEOUT_MSECS      5000    /* wait up to 5s for device init ... */
100 #define INIT_WAIT_MSECS         50      /* ... in 50ms increments */
101
102 #define APPLESMC_POLL_PERIOD    (HZ/20) /* poll for input every 1/20s */
103 #define APPLESMC_INPUT_FUZZ     4       /* input event threshold */
104 #define APPLESMC_INPUT_FLAT     4
105
106 #define SENSOR_X 0
107 #define SENSOR_Y 1
108 #define SENSOR_Z 2
109
110 /* Structure to be passed to DMI_MATCH function */
111 struct dmi_match_data {
112 /* Indicates whether this computer has an accelerometer. */
113         int accelerometer;
114 /* Indicates whether this computer has light sensors and keyboard backlight. */
115         int light;
116 /* Indicates which temperature sensors set to use. */
117         int temperature_set;
118 };
119
120 static const int debug;
121 static struct platform_device *pdev;
122 static s16 rest_x;
123 static s16 rest_y;
124 static struct timer_list applesmc_timer;
125 static struct input_dev *applesmc_idev;
126 static struct class_device *hwmon_class_dev;
127
128 /* Indicates whether this computer has an accelerometer. */
129 static unsigned int applesmc_accelerometer;
130
131 /* Indicates whether this computer has light sensors and keyboard backlight. */
132 static unsigned int applesmc_light;
133
134 /* Indicates which temperature sensors set to use. */
135 static unsigned int applesmc_temperature_set;
136
137 static struct mutex applesmc_lock;
138
139 /*
140  * Last index written to key_at_index sysfs file, and value to use for all other
141  * key_at_index_* sysfs files.
142  */
143 static unsigned int key_at_index;
144
145 static struct workqueue_struct *applesmc_led_wq;
146
147 /*
148  * __wait_status - Wait up to 2ms for the status port to get a certain value
149  * (masked with 0x0f), returning zero if the value is obtained.  Callers must
150  * hold applesmc_lock.
151  */
152 static int __wait_status(u8 val)
153 {
154         unsigned int i;
155
156         val = val & APPLESMC_STATUS_MASK;
157
158         for (i = 0; i < 200; i++) {
159                 if ((inb(APPLESMC_CMD_PORT) & APPLESMC_STATUS_MASK) == val) {
160                         if (debug)
161                                 printk(KERN_DEBUG
162                                                 "Waited %d us for status %x\n",
163                                                 i*10, val);
164                         return 0;
165                 }
166                 udelay(10);
167         }
168
169         printk(KERN_WARNING "applesmc: wait status failed: %x != %x\n",
170                                                 val, inb(APPLESMC_CMD_PORT));
171
172         return -EIO;
173 }
174
175 /*
176  * applesmc_read_key - reads len bytes from a given key, and put them in buffer.
177  * Returns zero on success or a negative error on failure. Callers must
178  * hold applesmc_lock.
179  */
180 static int applesmc_read_key(const char* key, u8* buffer, u8 len)
181 {
182         int i;
183
184         if (len > APPLESMC_MAX_DATA_LENGTH) {
185                 printk(KERN_ERR "applesmc_read_key: cannot read more than "
186                                         "%d bytes\n", APPLESMC_MAX_DATA_LENGTH);
187                 return -EINVAL;
188         }
189
190         outb(APPLESMC_READ_CMD, APPLESMC_CMD_PORT);
191         if (__wait_status(0x0c))
192                 return -EIO;
193
194         for (i = 0; i < 4; i++) {
195                 outb(key[i], APPLESMC_DATA_PORT);
196                 if (__wait_status(0x04))
197                         return -EIO;
198         }
199         if (debug)
200                 printk(KERN_DEBUG "<%s", key);
201
202         outb(len, APPLESMC_DATA_PORT);
203         if (debug)
204                 printk(KERN_DEBUG ">%x", len);
205
206         for (i = 0; i < len; i++) {
207                 if (__wait_status(0x05))
208                         return -EIO;
209                 buffer[i] = inb(APPLESMC_DATA_PORT);
210                 if (debug)
211                         printk(KERN_DEBUG "<%x", buffer[i]);
212         }
213         if (debug)
214                 printk(KERN_DEBUG "\n");
215
216         return 0;
217 }
218
219 /*
220  * applesmc_write_key - writes len bytes from buffer to a given key.
221  * Returns zero on success or a negative error on failure. Callers must
222  * hold applesmc_lock.
223  */
224 static int applesmc_write_key(const char* key, u8* buffer, u8 len)
225 {
226         int i;
227
228         if (len > APPLESMC_MAX_DATA_LENGTH) {
229                 printk(KERN_ERR "applesmc_write_key: cannot write more than "
230                                         "%d bytes\n", APPLESMC_MAX_DATA_LENGTH);
231                 return -EINVAL;
232         }
233
234         outb(APPLESMC_WRITE_CMD, APPLESMC_CMD_PORT);
235         if (__wait_status(0x0c))
236                 return -EIO;
237
238         for (i = 0; i < 4; i++) {
239                 outb(key[i], APPLESMC_DATA_PORT);
240                 if (__wait_status(0x04))
241                         return -EIO;
242         }
243
244         outb(len, APPLESMC_DATA_PORT);
245
246         for (i = 0; i < len; i++) {
247                 if (__wait_status(0x04))
248                         return -EIO;
249                 outb(buffer[i], APPLESMC_DATA_PORT);
250         }
251
252         return 0;
253 }
254
255 /*
256  * applesmc_get_key_at_index - get key at index, and put the result in key
257  * (char[6]). Returns zero on success or a negative error on failure. Callers
258  * must hold applesmc_lock.
259  */
260 static int applesmc_get_key_at_index(int index, char* key)
261 {
262         int i;
263         u8 readkey[4];
264         readkey[0] = index >> 24;
265         readkey[1] = index >> 16;
266         readkey[2] = index >> 8;
267         readkey[3] = index;
268
269         outb(APPLESMC_GET_KEY_BY_INDEX_CMD, APPLESMC_CMD_PORT);
270         if (__wait_status(0x0c))
271                 return -EIO;
272
273         for (i = 0; i < 4; i++) {
274                 outb(readkey[i], APPLESMC_DATA_PORT);
275                 if (__wait_status(0x04))
276                         return -EIO;
277         }
278
279         outb(4, APPLESMC_DATA_PORT);
280
281         for (i = 0; i < 4; i++) {
282                 if (__wait_status(0x05))
283                         return -EIO;
284                 key[i] = inb(APPLESMC_DATA_PORT);
285         }
286         key[4] = 0;
287
288         return 0;
289 }
290
291 /*
292  * applesmc_get_key_type - get key type, and put the result in type (char[6]).
293  * Returns zero on success or a negative error on failure. Callers must
294  * hold applesmc_lock.
295  */
296 static int applesmc_get_key_type(char* key, char* type)
297 {
298         int i;
299
300         outb(APPLESMC_GET_KEY_TYPE_CMD, APPLESMC_CMD_PORT);
301         if (__wait_status(0x0c))
302                 return -EIO;
303
304         for (i = 0; i < 4; i++) {
305                 outb(key[i], APPLESMC_DATA_PORT);
306                 if (__wait_status(0x04))
307                         return -EIO;
308         }
309
310         outb(5, APPLESMC_DATA_PORT);
311
312         for (i = 0; i < 6; i++) {
313                 if (__wait_status(0x05))
314                         return -EIO;
315                 type[i] = inb(APPLESMC_DATA_PORT);
316         }
317         type[5] = 0;
318
319         return 0;
320 }
321
322 /*
323  * applesmc_read_motion_sensor - Read motion sensor (X, Y or Z). Callers must
324  * hold applesmc_lock.
325  */
326 static int applesmc_read_motion_sensor(int index, s16* value)
327 {
328         u8 buffer[2];
329         int ret;
330
331         switch (index) {
332         case SENSOR_X:
333                 ret = applesmc_read_key(MOTION_SENSOR_X_KEY, buffer, 2);
334                 break;
335         case SENSOR_Y:
336                 ret = applesmc_read_key(MOTION_SENSOR_Y_KEY, buffer, 2);
337                 break;
338         case SENSOR_Z:
339                 ret = applesmc_read_key(MOTION_SENSOR_Z_KEY, buffer, 2);
340                 break;
341         default:
342                 ret = -EINVAL;
343         }
344
345         *value = ((s16)buffer[0] << 8) | buffer[1];
346
347         return ret;
348 }
349
350 /*
351  * applesmc_device_init - initialize the accelerometer.  Returns zero on success
352  * and negative error code on failure.  Can sleep.
353  */
354 static int applesmc_device_init(void)
355 {
356         int total, ret = -ENXIO;
357         u8 buffer[2];
358
359         if (!applesmc_accelerometer)
360                 return 0;
361
362         mutex_lock(&applesmc_lock);
363
364         for (total = INIT_TIMEOUT_MSECS; total > 0; total -= INIT_WAIT_MSECS) {
365                 if (debug)
366                         printk(KERN_DEBUG "applesmc try %d\n", total);
367                 if (!applesmc_read_key(MOTION_SENSOR_KEY, buffer, 2) &&
368                                 (buffer[0] != 0x00 || buffer[1] != 0x00)) {
369                         if (total == INIT_TIMEOUT_MSECS) {
370                                 printk(KERN_DEBUG "applesmc: device has"
371                                                 " already been initialized"
372                                                 " (0x%02x, 0x%02x).\n",
373                                                 buffer[0], buffer[1]);
374                         } else {
375                                 printk(KERN_DEBUG "applesmc: device"
376                                                 " successfully initialized"
377                                                 " (0x%02x, 0x%02x).\n",
378                                                 buffer[0], buffer[1]);
379                         }
380                         ret = 0;
381                         goto out;
382                 }
383                 buffer[0] = 0xe0;
384                 buffer[1] = 0x00;
385                 applesmc_write_key(MOTION_SENSOR_KEY, buffer, 2);
386                 msleep(INIT_WAIT_MSECS);
387         }
388
389         printk(KERN_WARNING "applesmc: failed to init the device\n");
390
391 out:
392         mutex_unlock(&applesmc_lock);
393         return ret;
394 }
395
396 /*
397  * applesmc_get_fan_count - get the number of fans. Callers must NOT hold
398  * applesmc_lock.
399  */
400 static int applesmc_get_fan_count(void)
401 {
402         int ret;
403         u8 buffer[1];
404
405         mutex_lock(&applesmc_lock);
406
407         ret = applesmc_read_key(FANS_COUNT, buffer, 1);
408
409         mutex_unlock(&applesmc_lock);
410         if (ret)
411                 return ret;
412         else
413                 return buffer[0];
414 }
415
416 /* Device model stuff */
417 static int applesmc_probe(struct platform_device *dev)
418 {
419         int ret;
420
421         ret = applesmc_device_init();
422         if (ret)
423                 return ret;
424
425         printk(KERN_INFO "applesmc: device successfully initialized.\n");
426         return 0;
427 }
428
429 static int applesmc_resume(struct platform_device *dev)
430 {
431         return applesmc_device_init();
432 }
433
434 static struct platform_driver applesmc_driver = {
435         .probe = applesmc_probe,
436         .resume = applesmc_resume,
437         .driver = {
438                 .name = "applesmc",
439                 .owner = THIS_MODULE,
440         },
441 };
442
443 /*
444  * applesmc_calibrate - Set our "resting" values.  Callers must
445  * hold applesmc_lock.
446  */
447 static void applesmc_calibrate(void)
448 {
449         applesmc_read_motion_sensor(SENSOR_X, &rest_x);
450         applesmc_read_motion_sensor(SENSOR_Y, &rest_y);
451         rest_x = -rest_x;
452 }
453
454 static int applesmc_idev_open(struct input_dev *dev)
455 {
456         add_timer(&applesmc_timer);
457
458         return 0;
459 }
460
461 static void applesmc_idev_close(struct input_dev *dev)
462 {
463         del_timer_sync(&applesmc_timer);
464 }
465
466 static void applesmc_idev_poll(unsigned long unused)
467 {
468         s16 x, y;
469
470         /* Cannot sleep.  Try nonblockingly.  If we fail, try again later. */
471         if (!mutex_trylock(&applesmc_lock)) {
472                 mod_timer(&applesmc_timer, jiffies + APPLESMC_POLL_PERIOD);
473                 return;
474         }
475
476         if (applesmc_read_motion_sensor(SENSOR_X, &x))
477                 goto out;
478         if (applesmc_read_motion_sensor(SENSOR_Y, &y))
479                 goto out;
480
481         x = -x;
482         input_report_abs(applesmc_idev, ABS_X, x - rest_x);
483         input_report_abs(applesmc_idev, ABS_Y, y - rest_y);
484         input_sync(applesmc_idev);
485
486 out:
487         mod_timer(&applesmc_timer, jiffies + APPLESMC_POLL_PERIOD);
488
489         mutex_unlock(&applesmc_lock);
490 }
491
492 /* Sysfs Files */
493
494 static ssize_t applesmc_name_show(struct device *dev,
495                                    struct device_attribute *attr, char *buf)
496 {
497         return snprintf(buf, PAGE_SIZE, "applesmc\n");
498 }
499
500 static ssize_t applesmc_position_show(struct device *dev,
501                                    struct device_attribute *attr, char *buf)
502 {
503         int ret;
504         s16 x, y, z;
505
506         mutex_lock(&applesmc_lock);
507
508         ret = applesmc_read_motion_sensor(SENSOR_X, &x);
509         if (ret)
510                 goto out;
511         ret = applesmc_read_motion_sensor(SENSOR_Y, &y);
512         if (ret)
513                 goto out;
514         ret = applesmc_read_motion_sensor(SENSOR_Z, &z);
515         if (ret)
516                 goto out;
517
518 out:
519         mutex_unlock(&applesmc_lock);
520         if (ret)
521                 return ret;
522         else
523                 return snprintf(buf, PAGE_SIZE, "(%d,%d,%d)\n", x, y, z);
524 }
525
526 static ssize_t applesmc_light_show(struct device *dev,
527                                 struct device_attribute *attr, char *sysfsbuf)
528 {
529         int ret;
530         u8 left = 0, right = 0;
531         u8 buffer[6];
532
533         mutex_lock(&applesmc_lock);
534
535         ret = applesmc_read_key(LIGHT_SENSOR_LEFT_KEY, buffer, 6);
536         left = buffer[2];
537         if (ret)
538                 goto out;
539         ret = applesmc_read_key(LIGHT_SENSOR_RIGHT_KEY, buffer, 6);
540         right = buffer[2];
541
542 out:
543         mutex_unlock(&applesmc_lock);
544         if (ret)
545                 return ret;
546         else
547                 return snprintf(sysfsbuf, PAGE_SIZE, "(%d,%d)\n", left, right);
548 }
549
550 /* Displays degree Celsius * 1000 */
551 static ssize_t applesmc_show_temperature(struct device *dev,
552                         struct device_attribute *devattr, char *sysfsbuf)
553 {
554         int ret;
555         u8 buffer[2];
556         unsigned int temp;
557         struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
558         const char* key =
559                 temperature_sensors_sets[applesmc_temperature_set][attr->index];
560
561         mutex_lock(&applesmc_lock);
562
563         ret = applesmc_read_key(key, buffer, 2);
564         temp = buffer[0]*1000;
565         temp += (buffer[1] >> 6) * 250;
566
567         mutex_unlock(&applesmc_lock);
568
569         if (ret)
570                 return ret;
571         else
572                 return snprintf(sysfsbuf, PAGE_SIZE, "%u\n", temp);
573 }
574
575 static ssize_t applesmc_show_fan_speed(struct device *dev,
576                                 struct device_attribute *attr, char *sysfsbuf)
577 {
578         int ret;
579         unsigned int speed = 0;
580         char newkey[5];
581         u8 buffer[2];
582         struct sensor_device_attribute_2 *sensor_attr =
583                                                 to_sensor_dev_attr_2(attr);
584
585         newkey[0] = fan_speed_keys[sensor_attr->nr][0];
586         newkey[1] = '0' + sensor_attr->index;
587         newkey[2] = fan_speed_keys[sensor_attr->nr][2];
588         newkey[3] = fan_speed_keys[sensor_attr->nr][3];
589         newkey[4] = 0;
590
591         mutex_lock(&applesmc_lock);
592
593         ret = applesmc_read_key(newkey, buffer, 2);
594         speed = ((buffer[0] << 8 | buffer[1]) >> 2);
595
596         mutex_unlock(&applesmc_lock);
597         if (ret)
598                 return ret;
599         else
600                 return snprintf(sysfsbuf, PAGE_SIZE, "%u\n", speed);
601 }
602
603 static ssize_t applesmc_store_fan_speed(struct device *dev,
604                                         struct device_attribute *attr,
605                                         const char *sysfsbuf, size_t count)
606 {
607         int ret;
608         u32 speed;
609         char newkey[5];
610         u8 buffer[2];
611         struct sensor_device_attribute_2 *sensor_attr =
612                                                 to_sensor_dev_attr_2(attr);
613
614         speed = simple_strtoul(sysfsbuf, NULL, 10);
615
616         if (speed > 0x4000) /* Bigger than a 14-bit value */
617                 return -EINVAL;
618
619         newkey[0] = fan_speed_keys[sensor_attr->nr][0];
620         newkey[1] = '0' + sensor_attr->index;
621         newkey[2] = fan_speed_keys[sensor_attr->nr][2];
622         newkey[3] = fan_speed_keys[sensor_attr->nr][3];
623         newkey[4] = 0;
624
625         mutex_lock(&applesmc_lock);
626
627         buffer[0] = (speed >> 6) & 0xff;
628         buffer[1] = (speed << 2) & 0xff;
629         ret = applesmc_write_key(newkey, buffer, 2);
630
631         mutex_unlock(&applesmc_lock);
632         if (ret)
633                 return ret;
634         else
635                 return count;
636 }
637
638 static ssize_t applesmc_show_fan_manual(struct device *dev,
639                         struct device_attribute *devattr, char *sysfsbuf)
640 {
641         int ret;
642         u16 manual = 0;
643         u8 buffer[2];
644         struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
645
646         mutex_lock(&applesmc_lock);
647
648         ret = applesmc_read_key(FANS_MANUAL, buffer, 2);
649         manual = ((buffer[0] << 8 | buffer[1]) >> attr->index) & 0x01;
650
651         mutex_unlock(&applesmc_lock);
652         if (ret)
653                 return ret;
654         else
655                 return snprintf(sysfsbuf, PAGE_SIZE, "%d\n", manual);
656 }
657
658 static ssize_t applesmc_store_fan_manual(struct device *dev,
659                                          struct device_attribute *devattr,
660                                          const char *sysfsbuf, size_t count)
661 {
662         int ret;
663         u8 buffer[2];
664         u32 input;
665         u16 val;
666         struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
667
668         input = simple_strtoul(sysfsbuf, NULL, 10);
669
670         mutex_lock(&applesmc_lock);
671
672         ret = applesmc_read_key(FANS_MANUAL, buffer, 2);
673         val = (buffer[0] << 8 | buffer[1]);
674         if (ret)
675                 goto out;
676
677         if (input)
678                 val = val | (0x01 << attr->index);
679         else
680                 val = val & ~(0x01 << attr->index);
681
682         buffer[0] = (val >> 8) & 0xFF;
683         buffer[1] = val & 0xFF;
684
685         ret = applesmc_write_key(FANS_MANUAL, buffer, 2);
686
687 out:
688         mutex_unlock(&applesmc_lock);
689         if (ret)
690                 return ret;
691         else
692                 return count;
693 }
694
695 static ssize_t applesmc_show_fan_position(struct device *dev,
696                                 struct device_attribute *attr, char *sysfsbuf)
697 {
698         int ret;
699         char newkey[5];
700         u8 buffer[17];
701         struct sensor_device_attribute_2 *sensor_attr =
702                                                 to_sensor_dev_attr_2(attr);
703
704         newkey[0] = FAN_POSITION[0];
705         newkey[1] = '0' + sensor_attr->index;
706         newkey[2] = FAN_POSITION[2];
707         newkey[3] = FAN_POSITION[3];
708         newkey[4] = 0;
709
710         mutex_lock(&applesmc_lock);
711
712         ret = applesmc_read_key(newkey, buffer, 16);
713         buffer[16] = 0;
714
715         mutex_unlock(&applesmc_lock);
716         if (ret)
717                 return ret;
718         else
719                 return snprintf(sysfsbuf, PAGE_SIZE, "%s\n", buffer+4);
720 }
721
722 static ssize_t applesmc_calibrate_show(struct device *dev,
723                                 struct device_attribute *attr, char *sysfsbuf)
724 {
725         return snprintf(sysfsbuf, PAGE_SIZE, "(%d,%d)\n", rest_x, rest_y);
726 }
727
728 static ssize_t applesmc_calibrate_store(struct device *dev,
729         struct device_attribute *attr, const char *sysfsbuf, size_t count)
730 {
731         mutex_lock(&applesmc_lock);
732         applesmc_calibrate();
733         mutex_unlock(&applesmc_lock);
734
735         return count;
736 }
737
738 /* Store the next backlight value to be written by the work */
739 static unsigned int backlight_value;
740
741 static void applesmc_backlight_set(struct work_struct *work)
742 {
743         u8 buffer[2];
744
745         mutex_lock(&applesmc_lock);
746         buffer[0] = backlight_value;
747         buffer[1] = 0x00;
748         applesmc_write_key(BACKLIGHT_KEY, buffer, 2);
749         mutex_unlock(&applesmc_lock);
750 }
751 static DECLARE_WORK(backlight_work, &applesmc_backlight_set);
752
753 static void applesmc_brightness_set(struct led_classdev *led_cdev,
754                                                 enum led_brightness value)
755 {
756         int ret;
757
758         backlight_value = value;
759         ret = queue_work(applesmc_led_wq, &backlight_work);
760
761         if (debug && (!ret))
762                 printk(KERN_DEBUG "applesmc: work was already on the queue.\n");
763 }
764
765 static ssize_t applesmc_key_count_show(struct device *dev,
766                                 struct device_attribute *attr, char *sysfsbuf)
767 {
768         int ret;
769         u8 buffer[4];
770         u32 count;
771
772         mutex_lock(&applesmc_lock);
773
774         ret = applesmc_read_key(KEY_COUNT_KEY, buffer, 4);
775         count = ((u32)buffer[0]<<24) + ((u32)buffer[1]<<16) +
776                                                 ((u32)buffer[2]<<8) + buffer[3];
777
778         mutex_unlock(&applesmc_lock);
779         if (ret)
780                 return ret;
781         else
782                 return snprintf(sysfsbuf, PAGE_SIZE, "%d\n", count);
783 }
784
785 static ssize_t applesmc_key_at_index_read_show(struct device *dev,
786                                 struct device_attribute *attr, char *sysfsbuf)
787 {
788         char key[5];
789         char info[6];
790         int ret;
791
792         mutex_lock(&applesmc_lock);
793
794         ret = applesmc_get_key_at_index(key_at_index, key);
795
796         if (ret || !key[0]) {
797                 mutex_unlock(&applesmc_lock);
798
799                 return -EINVAL;
800         }
801
802         ret = applesmc_get_key_type(key, info);
803
804         if (ret) {
805                 mutex_unlock(&applesmc_lock);
806
807                 return ret;
808         }
809
810         /*
811          * info[0] maximum value (APPLESMC_MAX_DATA_LENGTH) is much lower than
812          * PAGE_SIZE, so we don't need any checks before writing to sysfsbuf.
813          */
814         ret = applesmc_read_key(key, sysfsbuf, info[0]);
815
816         mutex_unlock(&applesmc_lock);
817
818         if (!ret) {
819                 return info[0];
820         }
821         else {
822                 return ret;
823         }
824 }
825
826 static ssize_t applesmc_key_at_index_data_length_show(struct device *dev,
827                                 struct device_attribute *attr, char *sysfsbuf)
828 {
829         char key[5];
830         char info[6];
831         int ret;
832
833         mutex_lock(&applesmc_lock);
834
835         ret = applesmc_get_key_at_index(key_at_index, key);
836
837         if (ret || !key[0]) {
838                 mutex_unlock(&applesmc_lock);
839
840                 return -EINVAL;
841         }
842
843         ret = applesmc_get_key_type(key, info);
844
845         mutex_unlock(&applesmc_lock);
846
847         if (!ret)
848                 return snprintf(sysfsbuf, PAGE_SIZE, "%d\n", info[0]);
849         else
850                 return ret;
851 }
852
853 static ssize_t applesmc_key_at_index_type_show(struct device *dev,
854                                 struct device_attribute *attr, char *sysfsbuf)
855 {
856         char key[5];
857         char info[6];
858         int ret;
859
860         mutex_lock(&applesmc_lock);
861
862         ret = applesmc_get_key_at_index(key_at_index, key);
863
864         if (ret || !key[0]) {
865                 mutex_unlock(&applesmc_lock);
866
867                 return -EINVAL;
868         }
869
870         ret = applesmc_get_key_type(key, info);
871
872         mutex_unlock(&applesmc_lock);
873
874         if (!ret)
875                 return snprintf(sysfsbuf, PAGE_SIZE, "%s\n", info+1);
876         else
877                 return ret;
878 }
879
880 static ssize_t applesmc_key_at_index_name_show(struct device *dev,
881                                 struct device_attribute *attr, char *sysfsbuf)
882 {
883         char key[5];
884         int ret;
885
886         mutex_lock(&applesmc_lock);
887
888         ret = applesmc_get_key_at_index(key_at_index, key);
889
890         mutex_unlock(&applesmc_lock);
891
892         if (!ret && key[0])
893                 return snprintf(sysfsbuf, PAGE_SIZE, "%s\n", key);
894         else
895                 return -EINVAL;
896 }
897
898 static ssize_t applesmc_key_at_index_show(struct device *dev,
899                                 struct device_attribute *attr, char *sysfsbuf)
900 {
901         return snprintf(sysfsbuf, PAGE_SIZE, "%d\n", key_at_index);
902 }
903
904 static ssize_t applesmc_key_at_index_store(struct device *dev,
905         struct device_attribute *attr, const char *sysfsbuf, size_t count)
906 {
907         mutex_lock(&applesmc_lock);
908
909         key_at_index = simple_strtoul(sysfsbuf, NULL, 10);
910
911         mutex_unlock(&applesmc_lock);
912
913         return count;
914 }
915
916 static struct led_classdev applesmc_backlight = {
917         .name                   = "smc:kbd_backlight",
918         .default_trigger        = "nand-disk",
919         .brightness_set         = applesmc_brightness_set,
920 };
921
922 static DEVICE_ATTR(name, 0444, applesmc_name_show, NULL);
923
924 static DEVICE_ATTR(position, 0444, applesmc_position_show, NULL);
925 static DEVICE_ATTR(calibrate, 0644,
926                         applesmc_calibrate_show, applesmc_calibrate_store);
927
928 static struct attribute *accelerometer_attributes[] = {
929         &dev_attr_position.attr,
930         &dev_attr_calibrate.attr,
931         NULL
932 };
933
934 static const struct attribute_group accelerometer_attributes_group =
935         { .attrs = accelerometer_attributes };
936
937 static DEVICE_ATTR(light, 0444, applesmc_light_show, NULL);
938
939 static DEVICE_ATTR(key_count, 0444, applesmc_key_count_show, NULL);
940 static DEVICE_ATTR(key_at_index, 0644,
941                 applesmc_key_at_index_show, applesmc_key_at_index_store);
942 static DEVICE_ATTR(key_at_index_name, 0444,
943                                         applesmc_key_at_index_name_show, NULL);
944 static DEVICE_ATTR(key_at_index_type, 0444,
945                                         applesmc_key_at_index_type_show, NULL);
946 static DEVICE_ATTR(key_at_index_data_length, 0444,
947                                 applesmc_key_at_index_data_length_show, NULL);
948 static DEVICE_ATTR(key_at_index_data, 0444,
949                                 applesmc_key_at_index_read_show, NULL);
950
951 static struct attribute *key_enumeration_attributes[] = {
952         &dev_attr_key_count.attr,
953         &dev_attr_key_at_index.attr,
954         &dev_attr_key_at_index_name.attr,
955         &dev_attr_key_at_index_type.attr,
956         &dev_attr_key_at_index_data_length.attr,
957         &dev_attr_key_at_index_data.attr,
958         NULL
959 };
960
961 static const struct attribute_group key_enumeration_group =
962         { .attrs = key_enumeration_attributes };
963
964 /*
965  * Macro defining SENSOR_DEVICE_ATTR for a fan sysfs entries.
966  *  - show actual speed
967  *  - show/store minimum speed
968  *  - show maximum speed
969  *  - show safe speed
970  *  - show/store target speed
971  *  - show/store manual mode
972  */
973 #define sysfs_fan_speeds_offset(offset) \
974 static SENSOR_DEVICE_ATTR_2(fan##offset##_input, S_IRUGO, \
975                         applesmc_show_fan_speed, NULL, 0, offset-1); \
976 \
977 static SENSOR_DEVICE_ATTR_2(fan##offset##_min, S_IRUGO | S_IWUSR, \
978         applesmc_show_fan_speed, applesmc_store_fan_speed, 1, offset-1); \
979 \
980 static SENSOR_DEVICE_ATTR_2(fan##offset##_max, S_IRUGO, \
981                         applesmc_show_fan_speed, NULL, 2, offset-1); \
982 \
983 static SENSOR_DEVICE_ATTR_2(fan##offset##_safe, S_IRUGO, \
984                         applesmc_show_fan_speed, NULL, 3, offset-1); \
985 \
986 static SENSOR_DEVICE_ATTR_2(fan##offset##_output, S_IRUGO | S_IWUSR, \
987         applesmc_show_fan_speed, applesmc_store_fan_speed, 4, offset-1); \
988 \
989 static SENSOR_DEVICE_ATTR(fan##offset##_manual, S_IRUGO | S_IWUSR, \
990         applesmc_show_fan_manual, applesmc_store_fan_manual, offset-1); \
991 \
992 static SENSOR_DEVICE_ATTR(fan##offset##_label, S_IRUGO, \
993         applesmc_show_fan_position, NULL, offset-1); \
994 \
995 static struct attribute *fan##offset##_attributes[] = { \
996         &sensor_dev_attr_fan##offset##_input.dev_attr.attr, \
997         &sensor_dev_attr_fan##offset##_min.dev_attr.attr, \
998         &sensor_dev_attr_fan##offset##_max.dev_attr.attr, \
999         &sensor_dev_attr_fan##offset##_safe.dev_attr.attr, \
1000         &sensor_dev_attr_fan##offset##_output.dev_attr.attr, \
1001         &sensor_dev_attr_fan##offset##_manual.dev_attr.attr, \
1002         &sensor_dev_attr_fan##offset##_label.dev_attr.attr, \
1003         NULL \
1004 };
1005
1006 /*
1007  * Create the needed functions for each fan using the macro defined above
1008  * (2 fans are supported)
1009  */
1010 sysfs_fan_speeds_offset(1);
1011 sysfs_fan_speeds_offset(2);
1012
1013 static const struct attribute_group fan_attribute_groups[] = {
1014         { .attrs = fan1_attributes },
1015         { .attrs = fan2_attributes }
1016 };
1017
1018 /*
1019  * Temperature sensors sysfs entries.
1020  */
1021 static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO,
1022                                         applesmc_show_temperature, NULL, 0);
1023 static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO,
1024                                         applesmc_show_temperature, NULL, 1);
1025 static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO,
1026                                         applesmc_show_temperature, NULL, 2);
1027 static SENSOR_DEVICE_ATTR(temp4_input, S_IRUGO,
1028                                         applesmc_show_temperature, NULL, 3);
1029 static SENSOR_DEVICE_ATTR(temp5_input, S_IRUGO,
1030                                         applesmc_show_temperature, NULL, 4);
1031 static SENSOR_DEVICE_ATTR(temp6_input, S_IRUGO,
1032                                         applesmc_show_temperature, NULL, 5);
1033 static SENSOR_DEVICE_ATTR(temp7_input, S_IRUGO,
1034                                         applesmc_show_temperature, NULL, 6);
1035 static SENSOR_DEVICE_ATTR(temp8_input, S_IRUGO,
1036                                         applesmc_show_temperature, NULL, 7);
1037 static SENSOR_DEVICE_ATTR(temp9_input, S_IRUGO,
1038                                         applesmc_show_temperature, NULL, 8);
1039 static SENSOR_DEVICE_ATTR(temp10_input, S_IRUGO,
1040                                         applesmc_show_temperature, NULL, 9);
1041 static SENSOR_DEVICE_ATTR(temp11_input, S_IRUGO,
1042                                         applesmc_show_temperature, NULL, 10);
1043 static SENSOR_DEVICE_ATTR(temp12_input, S_IRUGO,
1044                                         applesmc_show_temperature, NULL, 11);
1045
1046 static struct attribute *temperature_attributes[] = {
1047         &sensor_dev_attr_temp1_input.dev_attr.attr,
1048         &sensor_dev_attr_temp2_input.dev_attr.attr,
1049         &sensor_dev_attr_temp3_input.dev_attr.attr,
1050         &sensor_dev_attr_temp4_input.dev_attr.attr,
1051         &sensor_dev_attr_temp5_input.dev_attr.attr,
1052         &sensor_dev_attr_temp6_input.dev_attr.attr,
1053         &sensor_dev_attr_temp7_input.dev_attr.attr,
1054         &sensor_dev_attr_temp8_input.dev_attr.attr,
1055         &sensor_dev_attr_temp9_input.dev_attr.attr,
1056         &sensor_dev_attr_temp10_input.dev_attr.attr,
1057         &sensor_dev_attr_temp11_input.dev_attr.attr,
1058         &sensor_dev_attr_temp12_input.dev_attr.attr,
1059         NULL
1060 };
1061
1062 static const struct attribute_group temperature_attributes_group =
1063         { .attrs = temperature_attributes };
1064
1065 /* Module stuff */
1066
1067 /*
1068  * applesmc_dmi_match - found a match.  return one, short-circuiting the hunt.
1069  */
1070 static int applesmc_dmi_match(struct dmi_system_id *id)
1071 {
1072         int i = 0;
1073         struct dmi_match_data* dmi_data = id->driver_data;
1074         printk(KERN_INFO "applesmc: %s detected:\n", id->ident);
1075         applesmc_accelerometer = dmi_data->accelerometer;
1076         printk(KERN_INFO "applesmc:  - Model %s accelerometer\n",
1077                                 applesmc_accelerometer ? "with" : "without");
1078         applesmc_light = dmi_data->light;
1079         printk(KERN_INFO "applesmc:  - Model %s light sensors and backlight\n",
1080                                         applesmc_light ? "with" : "without");
1081
1082         applesmc_temperature_set =  dmi_data->temperature_set;
1083         while (temperature_sensors_sets[applesmc_temperature_set][i] != NULL)
1084                 i++;
1085         printk(KERN_INFO "applesmc:  - Model with %d temperature sensors\n", i);
1086         return 1;
1087 }
1088
1089 /* Create accelerometer ressources */
1090 static int applesmc_create_accelerometer(void)
1091 {
1092         int ret;
1093
1094         ret = sysfs_create_group(&pdev->dev.kobj,
1095                                         &accelerometer_attributes_group);
1096         if (ret)
1097                 goto out;
1098
1099         applesmc_idev = input_allocate_device();
1100         if (!applesmc_idev) {
1101                 ret = -ENOMEM;
1102                 goto out_sysfs;
1103         }
1104
1105         /* initial calibrate for the input device */
1106         applesmc_calibrate();
1107
1108         /* initialize the input class */
1109         applesmc_idev->name = "applesmc";
1110         applesmc_idev->id.bustype = BUS_HOST;
1111         applesmc_idev->dev.parent = &pdev->dev;
1112         applesmc_idev->evbit[0] = BIT(EV_ABS);
1113         applesmc_idev->open = applesmc_idev_open;
1114         applesmc_idev->close = applesmc_idev_close;
1115         input_set_abs_params(applesmc_idev, ABS_X,
1116                         -256, 256, APPLESMC_INPUT_FUZZ, APPLESMC_INPUT_FLAT);
1117         input_set_abs_params(applesmc_idev, ABS_Y,
1118                         -256, 256, APPLESMC_INPUT_FUZZ, APPLESMC_INPUT_FLAT);
1119
1120         ret = input_register_device(applesmc_idev);
1121         if (ret)
1122                 goto out_idev;
1123
1124         /* start up our timer for the input device */
1125         init_timer(&applesmc_timer);
1126         applesmc_timer.function = applesmc_idev_poll;
1127         applesmc_timer.expires = jiffies + APPLESMC_POLL_PERIOD;
1128
1129         return 0;
1130
1131 out_idev:
1132         input_free_device(applesmc_idev);
1133
1134 out_sysfs:
1135         sysfs_remove_group(&pdev->dev.kobj, &accelerometer_attributes_group);
1136
1137 out:
1138         printk(KERN_WARNING "applesmc: driver init failed (ret=%d)!\n", ret);
1139         return ret;
1140 }
1141
1142 /* Release all ressources used by the accelerometer */
1143 static void applesmc_release_accelerometer(void)
1144 {
1145         del_timer_sync(&applesmc_timer);
1146         input_unregister_device(applesmc_idev);
1147         sysfs_remove_group(&pdev->dev.kobj, &accelerometer_attributes_group);
1148 }
1149
1150 static __initdata struct dmi_match_data applesmc_dmi_data[] = {
1151 /* MacBook Pro: accelerometer, backlight and temperature set 0 */
1152         { .accelerometer = 1, .light = 1, .temperature_set = 0 },
1153 /* MacBook: accelerometer and temperature set 0 */
1154         { .accelerometer = 1, .light = 0, .temperature_set = 0 },
1155 /* MacBook: temperature set 1 */
1156         { .accelerometer = 0, .light = 0, .temperature_set = 1 }
1157 };
1158
1159 /* Note that DMI_MATCH(...,"MacBook") will match "MacBookPro1,1".
1160  * So we need to put "Apple MacBook Pro" before "Apple MacBook". */
1161 static __initdata struct dmi_system_id applesmc_whitelist[] = {
1162         { applesmc_dmi_match, "Apple MacBook Pro", {
1163           DMI_MATCH(DMI_BOARD_VENDOR,"Apple"),
1164           DMI_MATCH(DMI_PRODUCT_NAME,"MacBookPro") },
1165                 (void*)&applesmc_dmi_data[0]},
1166         { applesmc_dmi_match, "Apple MacBook", {
1167           DMI_MATCH(DMI_BOARD_VENDOR,"Apple"),
1168           DMI_MATCH(DMI_PRODUCT_NAME,"MacBook") },
1169                 (void*)&applesmc_dmi_data[1]},
1170         { applesmc_dmi_match, "Apple Macmini", {
1171           DMI_MATCH(DMI_BOARD_VENDOR,"Apple"),
1172           DMI_MATCH(DMI_PRODUCT_NAME,"Macmini") },
1173                 (void*)&applesmc_dmi_data[2]},
1174         { .ident = NULL }
1175 };
1176
1177 static int __init applesmc_init(void)
1178 {
1179         int ret;
1180         int count;
1181         int i;
1182
1183         mutex_init(&applesmc_lock);
1184
1185         if (!dmi_check_system(applesmc_whitelist)) {
1186                 printk(KERN_WARNING "applesmc: supported laptop not found!\n");
1187                 ret = -ENODEV;
1188                 goto out;
1189         }
1190
1191         if (!request_region(APPLESMC_DATA_PORT, APPLESMC_NR_PORTS,
1192                                                                 "applesmc")) {
1193                 ret = -ENXIO;
1194                 goto out;
1195         }
1196
1197         ret = platform_driver_register(&applesmc_driver);
1198         if (ret)
1199                 goto out_region;
1200
1201         pdev = platform_device_register_simple("applesmc", APPLESMC_DATA_PORT,
1202                                                NULL, 0);
1203         if (IS_ERR(pdev)) {
1204                 ret = PTR_ERR(pdev);
1205                 goto out_driver;
1206         }
1207
1208         ret = sysfs_create_file(&pdev->dev.kobj, &dev_attr_name.attr);
1209         if (ret)
1210                 goto out_device;
1211
1212         /* Create key enumeration sysfs files */
1213         ret = sysfs_create_group(&pdev->dev.kobj, &key_enumeration_group);
1214         if (ret)
1215                 goto out_name;
1216
1217         /* create fan files */
1218         count = applesmc_get_fan_count();
1219         if (count < 0) {
1220                 printk(KERN_ERR "applesmc: Cannot get the number of fans.\n");
1221         } else {
1222                 printk(KERN_INFO "applesmc: %d fans found.\n", count);
1223
1224                 switch (count) {
1225                 default:
1226                         printk(KERN_WARNING "applesmc: More than 2 fans found,"
1227                                         " but at most 2 fans are supported"
1228                                                 " by the driver.\n");
1229                 case 2:
1230                         ret = sysfs_create_group(&pdev->dev.kobj,
1231                                                  &fan_attribute_groups[1]);
1232                         if (ret)
1233                                 goto out_key_enumeration;
1234                 case 1:
1235                         ret = sysfs_create_group(&pdev->dev.kobj,
1236                                                  &fan_attribute_groups[0]);
1237                         if (ret)
1238                                 goto out_fan_1;
1239                 case 0:
1240                         ;
1241                 }
1242         }
1243
1244         for (i = 0;
1245              temperature_sensors_sets[applesmc_temperature_set][i] != NULL;
1246              i++) {
1247                 if (temperature_attributes[i] == NULL) {
1248                         printk(KERN_ERR "applesmc: More temperature sensors "
1249                                 "in temperature_sensors_sets (at least %i)"
1250                                 "than available sysfs files in "
1251                                 "temperature_attributes (%i), please report "
1252                                 "this bug.\n", i, i-1);
1253                         goto out_temperature;
1254                 }
1255                 ret = sysfs_create_file(&pdev->dev.kobj,
1256                                                 temperature_attributes[i]);
1257                 if (ret)
1258                         goto out_temperature;
1259         }
1260
1261         if (applesmc_accelerometer) {
1262                 ret = applesmc_create_accelerometer();
1263                 if (ret)
1264                         goto out_temperature;
1265         }
1266
1267         if (applesmc_light) {
1268                 /* Add light sensor file */
1269                 ret = sysfs_create_file(&pdev->dev.kobj, &dev_attr_light.attr);
1270                 if (ret)
1271                         goto out_accelerometer;
1272
1273                 /* Create the workqueue */
1274                 applesmc_led_wq = create_singlethread_workqueue("applesmc-led");
1275                 if (!applesmc_led_wq) {
1276                         ret = -ENOMEM;
1277                         goto out_light_sysfs;
1278                 }
1279
1280                 /* register as a led device */
1281                 ret = led_classdev_register(&pdev->dev, &applesmc_backlight);
1282                 if (ret < 0)
1283                         goto out_light_wq;
1284         }
1285
1286         hwmon_class_dev = hwmon_device_register(&pdev->dev);
1287         if (IS_ERR(hwmon_class_dev)) {
1288                 ret = PTR_ERR(hwmon_class_dev);
1289                 goto out_light_ledclass;
1290         }
1291
1292         printk(KERN_INFO "applesmc: driver successfully loaded.\n");
1293
1294         return 0;
1295
1296 out_light_ledclass:
1297         if (applesmc_light)
1298                 led_classdev_unregister(&applesmc_backlight);
1299 out_light_wq:
1300         if (applesmc_light)
1301                 destroy_workqueue(applesmc_led_wq);
1302 out_light_sysfs:
1303         if (applesmc_light)
1304                 sysfs_remove_file(&pdev->dev.kobj, &dev_attr_light.attr);
1305 out_accelerometer:
1306         if (applesmc_accelerometer)
1307                 applesmc_release_accelerometer();
1308 out_temperature:
1309         sysfs_remove_group(&pdev->dev.kobj, &temperature_attributes_group);
1310         sysfs_remove_group(&pdev->dev.kobj, &fan_attribute_groups[0]);
1311 out_fan_1:
1312         sysfs_remove_group(&pdev->dev.kobj, &fan_attribute_groups[1]);
1313 out_key_enumeration:
1314         sysfs_remove_group(&pdev->dev.kobj, &key_enumeration_group);
1315 out_name:
1316         sysfs_remove_file(&pdev->dev.kobj, &dev_attr_name.attr);
1317 out_device:
1318         platform_device_unregister(pdev);
1319 out_driver:
1320         platform_driver_unregister(&applesmc_driver);
1321 out_region:
1322         release_region(APPLESMC_DATA_PORT, APPLESMC_NR_PORTS);
1323 out:
1324         printk(KERN_WARNING "applesmc: driver init failed (ret=%d)!\n", ret);
1325         return ret;
1326 }
1327
1328 static void __exit applesmc_exit(void)
1329 {
1330         hwmon_device_unregister(hwmon_class_dev);
1331         if (applesmc_light) {
1332                 led_classdev_unregister(&applesmc_backlight);
1333                 destroy_workqueue(applesmc_led_wq);
1334                 sysfs_remove_file(&pdev->dev.kobj, &dev_attr_light.attr);
1335         }
1336         if (applesmc_accelerometer)
1337                 applesmc_release_accelerometer();
1338         sysfs_remove_group(&pdev->dev.kobj, &temperature_attributes_group);
1339         sysfs_remove_group(&pdev->dev.kobj, &fan_attribute_groups[0]);
1340         sysfs_remove_group(&pdev->dev.kobj, &fan_attribute_groups[1]);
1341         sysfs_remove_group(&pdev->dev.kobj, &key_enumeration_group);
1342         sysfs_remove_file(&pdev->dev.kobj, &dev_attr_name.attr);
1343         platform_device_unregister(pdev);
1344         platform_driver_unregister(&applesmc_driver);
1345         release_region(APPLESMC_DATA_PORT, APPLESMC_NR_PORTS);
1346
1347         printk(KERN_INFO "applesmc: driver unloaded.\n");
1348 }
1349
1350 module_init(applesmc_init);
1351 module_exit(applesmc_exit);
1352
1353 MODULE_AUTHOR("Nicolas Boichat");
1354 MODULE_DESCRIPTION("Apple SMC");
1355 MODULE_LICENSE("GPL v2");