applesmc.c 34 KB

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