applesmc.c 39 KB

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