applesmc.c 38 KB

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