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