applesmc.c 46 KB

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