applesmc.c 34 KB

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