applesmc.c 45 KB

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