thinkpad_acpi.c 81 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496
  1. /*
  2. * thinkpad_acpi.c - ThinkPad ACPI Extras
  3. *
  4. *
  5. * Copyright (C) 2004-2005 Borislav Deianov <borislav@users.sf.net>
  6. * Copyright (C) 2006-2007 Henrique de Moraes Holschuh <hmh@hmh.eng.br>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  21. * 02110-1301, USA.
  22. */
  23. #define IBM_VERSION "0.14"
  24. #define TPACPI_SYSFS_VERSION 0x000100
  25. /*
  26. * Changelog:
  27. * 2007-03-27 0.14 renamed to thinkpad_acpi and moved to
  28. * drivers/misc.
  29. *
  30. * 2006-11-22 0.13 new maintainer
  31. * changelog now lives in git commit history, and will
  32. * not be updated further in-file.
  33. *
  34. * 2005-08-17 0.12 fix compilation on 2.6.13-rc kernels
  35. * 2005-03-17 0.11 support for 600e, 770x
  36. * thanks to Jamie Lentin <lentinj@dial.pipex.com>
  37. * support for 770e, G41
  38. * G40 and G41 don't have a thinklight
  39. * temperatures no longer experimental
  40. * experimental brightness control
  41. * experimental volume control
  42. * experimental fan enable/disable
  43. * 2005-01-16 0.10 fix module loading on R30, R31
  44. * 2005-01-16 0.9 support for 570, R30, R31
  45. * ultrabay support on A22p, A3x
  46. * limit arg for cmos, led, beep, drop experimental status
  47. * more capable led control on A21e, A22p, T20-22, X20
  48. * experimental temperatures and fan speed
  49. * experimental embedded controller register dump
  50. * mark more functions as __init, drop incorrect __exit
  51. * use MODULE_VERSION
  52. * thanks to Henrik Brix Andersen <brix@gentoo.org>
  53. * fix parameter passing on module loading
  54. * thanks to Rusty Russell <rusty@rustcorp.com.au>
  55. * thanks to Jim Radford <radford@blackbean.org>
  56. * 2004-11-08 0.8 fix init error case, don't return from a macro
  57. * thanks to Chris Wright <chrisw@osdl.org>
  58. * 2004-10-23 0.7 fix module loading on A21e, A22p, T20, T21, X20
  59. * fix led control on A21e
  60. * 2004-10-19 0.6 use acpi_bus_register_driver() to claim HKEY device
  61. * 2004-10-18 0.5 thinklight support on A21e, G40, R32, T20, T21, X20
  62. * proc file format changed
  63. * video_switch command
  64. * experimental cmos control
  65. * experimental led control
  66. * experimental acpi sounds
  67. * 2004-09-16 0.4 support for module parameters
  68. * hotkey mask can be prefixed by 0x
  69. * video output switching
  70. * video expansion control
  71. * ultrabay eject support
  72. * removed lcd brightness/on/off control, didn't work
  73. * 2004-08-17 0.3 support for R40
  74. * lcd off, brightness control
  75. * thinklight on/off
  76. * 2004-08-14 0.2 support for T series, X20
  77. * bluetooth enable/disable
  78. * hotkey events disabled by default
  79. * removed fan control, currently useless
  80. * 2004-08-09 0.1 initial release, support for X series
  81. */
  82. #include "thinkpad_acpi.h"
  83. MODULE_AUTHOR("Borislav Deianov, Henrique de Moraes Holschuh");
  84. MODULE_DESCRIPTION(IBM_DESC);
  85. MODULE_VERSION(IBM_VERSION);
  86. MODULE_LICENSE("GPL");
  87. /* Please remove this in year 2009 */
  88. MODULE_ALIAS("ibm_acpi");
  89. #define __unused __attribute__ ((unused))
  90. /****************************************************************************
  91. ****************************************************************************
  92. *
  93. * ACPI Helpers and device model
  94. *
  95. ****************************************************************************
  96. ****************************************************************************/
  97. /*************************************************************************
  98. * ACPI basic handles
  99. */
  100. static acpi_handle root_handle = NULL;
  101. #define IBM_HANDLE(object, parent, paths...) \
  102. static acpi_handle object##_handle; \
  103. static acpi_handle *object##_parent = &parent##_handle; \
  104. static char *object##_path; \
  105. static char *object##_paths[] = { paths }
  106. IBM_HANDLE(ec, root, "\\_SB.PCI0.ISA.EC0", /* 240, 240x */
  107. "\\_SB.PCI.ISA.EC", /* 570 */
  108. "\\_SB.PCI0.ISA0.EC0", /* 600e/x, 770e, 770x */
  109. "\\_SB.PCI0.ISA.EC", /* A21e, A2xm/p, T20-22, X20-21 */
  110. "\\_SB.PCI0.AD4S.EC0", /* i1400, R30 */
  111. "\\_SB.PCI0.ICH3.EC0", /* R31 */
  112. "\\_SB.PCI0.LPC.EC", /* all others */
  113. );
  114. IBM_HANDLE(ecrd, ec, "ECRD"); /* 570 */
  115. IBM_HANDLE(ecwr, ec, "ECWR"); /* 570 */
  116. /*************************************************************************
  117. * Misc ACPI handles
  118. */
  119. IBM_HANDLE(cmos, root, "\\UCMS", /* R50, R50e, R50p, R51, T4x, X31, X40 */
  120. "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
  121. "\\CMS", /* R40, R40e */
  122. ); /* all others */
  123. IBM_HANDLE(hkey, ec, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
  124. "^HKEY", /* R30, R31 */
  125. "HKEY", /* all others */
  126. ); /* 570 */
  127. /*************************************************************************
  128. * ACPI helpers
  129. */
  130. static int acpi_evalf(acpi_handle handle,
  131. void *res, char *method, char *fmt, ...)
  132. {
  133. char *fmt0 = fmt;
  134. struct acpi_object_list params;
  135. union acpi_object in_objs[IBM_MAX_ACPI_ARGS];
  136. struct acpi_buffer result, *resultp;
  137. union acpi_object out_obj;
  138. acpi_status status;
  139. va_list ap;
  140. char res_type;
  141. int success;
  142. int quiet;
  143. if (!*fmt) {
  144. printk(IBM_ERR "acpi_evalf() called with empty format\n");
  145. return 0;
  146. }
  147. if (*fmt == 'q') {
  148. quiet = 1;
  149. fmt++;
  150. } else
  151. quiet = 0;
  152. res_type = *(fmt++);
  153. params.count = 0;
  154. params.pointer = &in_objs[0];
  155. va_start(ap, fmt);
  156. while (*fmt) {
  157. char c = *(fmt++);
  158. switch (c) {
  159. case 'd': /* int */
  160. in_objs[params.count].integer.value = va_arg(ap, int);
  161. in_objs[params.count++].type = ACPI_TYPE_INTEGER;
  162. break;
  163. /* add more types as needed */
  164. default:
  165. printk(IBM_ERR "acpi_evalf() called "
  166. "with invalid format character '%c'\n", c);
  167. return 0;
  168. }
  169. }
  170. va_end(ap);
  171. if (res_type != 'v') {
  172. result.length = sizeof(out_obj);
  173. result.pointer = &out_obj;
  174. resultp = &result;
  175. } else
  176. resultp = NULL;
  177. status = acpi_evaluate_object(handle, method, &params, resultp);
  178. switch (res_type) {
  179. case 'd': /* int */
  180. if (res)
  181. *(int *)res = out_obj.integer.value;
  182. success = status == AE_OK && out_obj.type == ACPI_TYPE_INTEGER;
  183. break;
  184. case 'v': /* void */
  185. success = status == AE_OK;
  186. break;
  187. /* add more types as needed */
  188. default:
  189. printk(IBM_ERR "acpi_evalf() called "
  190. "with invalid format character '%c'\n", res_type);
  191. return 0;
  192. }
  193. if (!success && !quiet)
  194. printk(IBM_ERR "acpi_evalf(%s, %s, ...) failed: %d\n",
  195. method, fmt0, status);
  196. return success;
  197. }
  198. static void __unused acpi_print_int(acpi_handle handle, char *method)
  199. {
  200. int i;
  201. if (acpi_evalf(handle, &i, method, "d"))
  202. printk(IBM_INFO "%s = 0x%x\n", method, i);
  203. else
  204. printk(IBM_ERR "error calling %s\n", method);
  205. }
  206. static int acpi_ec_read(int i, u8 * p)
  207. {
  208. int v;
  209. if (ecrd_handle) {
  210. if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
  211. return 0;
  212. *p = v;
  213. } else {
  214. if (ec_read(i, p) < 0)
  215. return 0;
  216. }
  217. return 1;
  218. }
  219. static int acpi_ec_write(int i, u8 v)
  220. {
  221. if (ecwr_handle) {
  222. if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
  223. return 0;
  224. } else {
  225. if (ec_write(i, v) < 0)
  226. return 0;
  227. }
  228. return 1;
  229. }
  230. static int _sta(acpi_handle handle)
  231. {
  232. int status;
  233. if (!handle || !acpi_evalf(handle, &status, "_STA", "d"))
  234. status = 0;
  235. return status;
  236. }
  237. static int issue_thinkpad_cmos_command(int cmos_cmd)
  238. {
  239. if (!cmos_handle)
  240. return -ENXIO;
  241. if (!acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd))
  242. return -EIO;
  243. return 0;
  244. }
  245. /*************************************************************************
  246. * ACPI device model
  247. */
  248. static void drv_acpi_handle_init(char *name,
  249. acpi_handle *handle, acpi_handle parent,
  250. char **paths, int num_paths, char **path)
  251. {
  252. int i;
  253. acpi_status status;
  254. for (i = 0; i < num_paths; i++) {
  255. status = acpi_get_handle(parent, paths[i], handle);
  256. if (ACPI_SUCCESS(status)) {
  257. *path = paths[i];
  258. return;
  259. }
  260. }
  261. *handle = NULL;
  262. }
  263. static void dispatch_acpi_notify(acpi_handle handle, u32 event, void *data)
  264. {
  265. struct ibm_struct *ibm = data;
  266. if (!ibm || !ibm->acpi || !ibm->acpi->notify)
  267. return;
  268. ibm->acpi->notify(ibm, event);
  269. }
  270. static int __init setup_acpi_notify(struct ibm_struct *ibm)
  271. {
  272. acpi_status status;
  273. int ret;
  274. BUG_ON(!ibm->acpi);
  275. if (!*ibm->acpi->handle)
  276. return 0;
  277. dbg_printk(TPACPI_DBG_INIT,
  278. "setting up ACPI notify for %s\n", ibm->name);
  279. ret = acpi_bus_get_device(*ibm->acpi->handle, &ibm->acpi->device);
  280. if (ret < 0) {
  281. printk(IBM_ERR "%s device not present\n", ibm->name);
  282. return -ENODEV;
  283. }
  284. acpi_driver_data(ibm->acpi->device) = ibm;
  285. sprintf(acpi_device_class(ibm->acpi->device), "%s/%s",
  286. IBM_ACPI_EVENT_PREFIX,
  287. ibm->name);
  288. status = acpi_install_notify_handler(*ibm->acpi->handle,
  289. ibm->acpi->type, dispatch_acpi_notify, ibm);
  290. if (ACPI_FAILURE(status)) {
  291. if (status == AE_ALREADY_EXISTS) {
  292. printk(IBM_NOTICE "another device driver is already handling %s events\n",
  293. ibm->name);
  294. } else {
  295. printk(IBM_ERR "acpi_install_notify_handler(%s) failed: %d\n",
  296. ibm->name, status);
  297. }
  298. return -ENODEV;
  299. }
  300. ibm->flags.acpi_notify_installed = 1;
  301. return 0;
  302. }
  303. static int __init tpacpi_device_add(struct acpi_device *device)
  304. {
  305. return 0;
  306. }
  307. static int __init register_tpacpi_subdriver(struct ibm_struct *ibm)
  308. {
  309. int ret;
  310. dbg_printk(TPACPI_DBG_INIT,
  311. "registering %s as an ACPI driver\n", ibm->name);
  312. BUG_ON(!ibm->acpi);
  313. ibm->acpi->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
  314. if (!ibm->acpi->driver) {
  315. printk(IBM_ERR "kzalloc(ibm->driver) failed\n");
  316. return -ENOMEM;
  317. }
  318. sprintf(ibm->acpi->driver->name, "%s_%s", IBM_NAME, ibm->name);
  319. ibm->acpi->driver->ids = ibm->acpi->hid;
  320. ibm->acpi->driver->ops.add = &tpacpi_device_add;
  321. ret = acpi_bus_register_driver(ibm->acpi->driver);
  322. if (ret < 0) {
  323. printk(IBM_ERR "acpi_bus_register_driver(%s) failed: %d\n",
  324. ibm->acpi->hid, ret);
  325. kfree(ibm->acpi->driver);
  326. ibm->acpi->driver = NULL;
  327. } else if (!ret)
  328. ibm->flags.acpi_driver_registered = 1;
  329. return ret;
  330. }
  331. /****************************************************************************
  332. ****************************************************************************
  333. *
  334. * Procfs Helpers
  335. *
  336. ****************************************************************************
  337. ****************************************************************************/
  338. static int dispatch_procfs_read(char *page, char **start, off_t off,
  339. int count, int *eof, void *data)
  340. {
  341. struct ibm_struct *ibm = data;
  342. int len;
  343. if (!ibm || !ibm->read)
  344. return -EINVAL;
  345. len = ibm->read(page);
  346. if (len < 0)
  347. return len;
  348. if (len <= off + count)
  349. *eof = 1;
  350. *start = page + off;
  351. len -= off;
  352. if (len > count)
  353. len = count;
  354. if (len < 0)
  355. len = 0;
  356. return len;
  357. }
  358. static int dispatch_procfs_write(struct file *file,
  359. const char __user * userbuf,
  360. unsigned long count, void *data)
  361. {
  362. struct ibm_struct *ibm = data;
  363. char *kernbuf;
  364. int ret;
  365. if (!ibm || !ibm->write)
  366. return -EINVAL;
  367. kernbuf = kmalloc(count + 2, GFP_KERNEL);
  368. if (!kernbuf)
  369. return -ENOMEM;
  370. if (copy_from_user(kernbuf, userbuf, count)) {
  371. kfree(kernbuf);
  372. return -EFAULT;
  373. }
  374. kernbuf[count] = 0;
  375. strcat(kernbuf, ",");
  376. ret = ibm->write(kernbuf);
  377. if (ret == 0)
  378. ret = count;
  379. kfree(kernbuf);
  380. return ret;
  381. }
  382. static char *next_cmd(char **cmds)
  383. {
  384. char *start = *cmds;
  385. char *end;
  386. while ((end = strchr(start, ',')) && end == start)
  387. start = end + 1;
  388. if (!end)
  389. return NULL;
  390. *end = 0;
  391. *cmds = end + 1;
  392. return start;
  393. }
  394. /****************************************************************************
  395. ****************************************************************************
  396. *
  397. * Device model: hwmon and platform
  398. *
  399. ****************************************************************************
  400. ****************************************************************************/
  401. static struct platform_device *tpacpi_pdev = NULL;
  402. static struct class_device *tpacpi_hwmon = NULL;
  403. static struct platform_driver tpacpi_pdriver = {
  404. .driver = {
  405. .name = IBM_DRVR_NAME,
  406. .owner = THIS_MODULE,
  407. },
  408. };
  409. /*************************************************************************
  410. * thinkpad-acpi driver attributes
  411. */
  412. /* interface_version --------------------------------------------------- */
  413. static ssize_t tpacpi_driver_interface_version_show(
  414. struct device_driver *drv,
  415. char *buf)
  416. {
  417. return snprintf(buf, PAGE_SIZE, "0x%08x\n", TPACPI_SYSFS_VERSION);
  418. }
  419. static DRIVER_ATTR(interface_version, S_IRUGO,
  420. tpacpi_driver_interface_version_show, NULL);
  421. /* debug_level --------------------------------------------------------- */
  422. static ssize_t tpacpi_driver_debug_show(struct device_driver *drv,
  423. char *buf)
  424. {
  425. return snprintf(buf, PAGE_SIZE, "0x%04x\n", dbg_level);
  426. }
  427. static ssize_t tpacpi_driver_debug_store(struct device_driver *drv,
  428. const char *buf, size_t count)
  429. {
  430. unsigned long t;
  431. if (parse_strtoul(buf, 0xffff, &t))
  432. return -EINVAL;
  433. dbg_level = t;
  434. return count;
  435. }
  436. static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
  437. tpacpi_driver_debug_show, tpacpi_driver_debug_store);
  438. /* version ------------------------------------------------------------- */
  439. static ssize_t tpacpi_driver_version_show(struct device_driver *drv,
  440. char *buf)
  441. {
  442. return snprintf(buf, PAGE_SIZE, "%s v%s\n", IBM_DESC, IBM_VERSION);
  443. }
  444. static DRIVER_ATTR(version, S_IRUGO,
  445. tpacpi_driver_version_show, NULL);
  446. /* --------------------------------------------------------------------- */
  447. static struct driver_attribute* tpacpi_driver_attributes[] = {
  448. &driver_attr_debug_level, &driver_attr_version,
  449. &driver_attr_interface_version,
  450. };
  451. static int __init tpacpi_create_driver_attributes(struct device_driver *drv)
  452. {
  453. int i, res;
  454. i = 0;
  455. res = 0;
  456. while (!res && i < ARRAY_SIZE(tpacpi_driver_attributes)) {
  457. res = driver_create_file(drv, tpacpi_driver_attributes[i]);
  458. i++;
  459. }
  460. return res;
  461. }
  462. static void tpacpi_remove_driver_attributes(struct device_driver *drv)
  463. {
  464. int i;
  465. for(i = 0; i < ARRAY_SIZE(tpacpi_driver_attributes); i++)
  466. driver_remove_file(drv, tpacpi_driver_attributes[i]);
  467. }
  468. /*************************************************************************
  469. * sysfs support helpers
  470. */
  471. struct attribute_set_obj {
  472. struct attribute_set s;
  473. struct attribute *a;
  474. } __attribute__((packed));
  475. static struct attribute_set *create_attr_set(unsigned int max_members,
  476. const char* name)
  477. {
  478. struct attribute_set_obj *sobj;
  479. if (max_members == 0)
  480. return NULL;
  481. /* Allocates space for implicit NULL at the end too */
  482. sobj = kzalloc(sizeof(struct attribute_set_obj) +
  483. max_members * sizeof(struct attribute *),
  484. GFP_KERNEL);
  485. if (!sobj)
  486. return NULL;
  487. sobj->s.max_members = max_members;
  488. sobj->s.group.attrs = &sobj->a;
  489. sobj->s.group.name = name;
  490. return &sobj->s;
  491. }
  492. /* not multi-threaded safe, use it in a single thread per set */
  493. static int add_to_attr_set(struct attribute_set* s, struct attribute *attr)
  494. {
  495. if (!s || !attr)
  496. return -EINVAL;
  497. if (s->members >= s->max_members)
  498. return -ENOMEM;
  499. s->group.attrs[s->members] = attr;
  500. s->members++;
  501. return 0;
  502. }
  503. static int add_many_to_attr_set(struct attribute_set* s,
  504. struct attribute **attr,
  505. unsigned int count)
  506. {
  507. int i, res;
  508. for (i = 0; i < count; i++) {
  509. res = add_to_attr_set(s, attr[i]);
  510. if (res)
  511. return res;
  512. }
  513. return 0;
  514. }
  515. static void delete_attr_set(struct attribute_set* s, struct kobject *kobj)
  516. {
  517. sysfs_remove_group(kobj, &s->group);
  518. destroy_attr_set(s);
  519. }
  520. static int parse_strtoul(const char *buf,
  521. unsigned long max, unsigned long *value)
  522. {
  523. char *endp;
  524. *value = simple_strtoul(buf, &endp, 0);
  525. while (*endp && isspace(*endp))
  526. endp++;
  527. if (*endp || *value > max)
  528. return -EINVAL;
  529. return 0;
  530. }
  531. /****************************************************************************
  532. ****************************************************************************
  533. *
  534. * Subdrivers
  535. *
  536. ****************************************************************************
  537. ****************************************************************************/
  538. /*************************************************************************
  539. * thinkpad-acpi init subdriver
  540. */
  541. static int __init thinkpad_acpi_driver_init(struct ibm_init_struct *iibm)
  542. {
  543. printk(IBM_INFO "%s v%s\n", IBM_DESC, IBM_VERSION);
  544. printk(IBM_INFO "%s\n", IBM_URL);
  545. if (ibm_thinkpad_ec_found)
  546. printk(IBM_INFO "ThinkPad EC firmware %s\n",
  547. ibm_thinkpad_ec_found);
  548. return 0;
  549. }
  550. static int thinkpad_acpi_driver_read(char *p)
  551. {
  552. int len = 0;
  553. len += sprintf(p + len, "driver:\t\t%s\n", IBM_DESC);
  554. len += sprintf(p + len, "version:\t%s\n", IBM_VERSION);
  555. return len;
  556. }
  557. static struct ibm_struct thinkpad_acpi_driver_data = {
  558. .name = "driver",
  559. .read = thinkpad_acpi_driver_read,
  560. };
  561. /*************************************************************************
  562. * Hotkey subdriver
  563. */
  564. static int hotkey_orig_status;
  565. static int hotkey_orig_mask;
  566. static int __init hotkey_init(struct ibm_init_struct *iibm)
  567. {
  568. int res;
  569. vdbg_printk(TPACPI_DBG_INIT, "initializing hotkey subdriver\n");
  570. IBM_ACPIHANDLE_INIT(hkey);
  571. /* hotkey not supported on 570 */
  572. tp_features.hotkey = hkey_handle != NULL;
  573. vdbg_printk(TPACPI_DBG_INIT, "hotkeys are %s\n",
  574. str_supported(tp_features.hotkey));
  575. if (tp_features.hotkey) {
  576. /* mask not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
  577. A30, R30, R31, T20-22, X20-21, X22-24 */
  578. tp_features.hotkey_mask =
  579. acpi_evalf(hkey_handle, NULL, "DHKN", "qv");
  580. vdbg_printk(TPACPI_DBG_INIT, "hotkey masks are %s\n",
  581. str_supported(tp_features.hotkey_mask));
  582. res = hotkey_get(&hotkey_orig_status, &hotkey_orig_mask);
  583. if (res)
  584. return res;
  585. }
  586. return (tp_features.hotkey)? 0 : 1;
  587. }
  588. static void hotkey_exit(void)
  589. {
  590. int res;
  591. if (tp_features.hotkey) {
  592. dbg_printk(TPACPI_DBG_EXIT, "restoring original hotkey mask\n");
  593. res = hotkey_set(hotkey_orig_status, hotkey_orig_mask);
  594. if (res)
  595. printk(IBM_ERR "failed to restore hotkey to BIOS defaults\n");
  596. }
  597. }
  598. static void hotkey_notify(struct ibm_struct *ibm, u32 event)
  599. {
  600. int hkey;
  601. if (acpi_evalf(hkey_handle, &hkey, "MHKP", "d"))
  602. acpi_bus_generate_event(ibm->acpi->device, event, hkey);
  603. else {
  604. printk(IBM_ERR "unknown hotkey event %d\n", event);
  605. acpi_bus_generate_event(ibm->acpi->device, event, 0);
  606. }
  607. }
  608. static int hotkey_get(int *status, int *mask)
  609. {
  610. if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
  611. return -EIO;
  612. if (tp_features.hotkey_mask)
  613. if (!acpi_evalf(hkey_handle, mask, "DHKN", "d"))
  614. return -EIO;
  615. return 0;
  616. }
  617. static int hotkey_set(int status, int mask)
  618. {
  619. int i;
  620. if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", status))
  621. return -EIO;
  622. if (tp_features.hotkey_mask)
  623. for (i = 0; i < 32; i++) {
  624. int bit = ((1 << i) & mask) != 0;
  625. if (!acpi_evalf(hkey_handle,
  626. NULL, "MHKM", "vdd", i + 1, bit))
  627. return -EIO;
  628. }
  629. return 0;
  630. }
  631. static int hotkey_read(char *p)
  632. {
  633. int res, status, mask;
  634. int len = 0;
  635. if (!tp_features.hotkey) {
  636. len += sprintf(p + len, "status:\t\tnot supported\n");
  637. return len;
  638. }
  639. res = hotkey_get(&status, &mask);
  640. if (res)
  641. return res;
  642. len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 0));
  643. if (tp_features.hotkey_mask) {
  644. len += sprintf(p + len, "mask:\t\t0x%04x\n", mask);
  645. len += sprintf(p + len,
  646. "commands:\tenable, disable, reset, <mask>\n");
  647. } else {
  648. len += sprintf(p + len, "mask:\t\tnot supported\n");
  649. len += sprintf(p + len, "commands:\tenable, disable, reset\n");
  650. }
  651. return len;
  652. }
  653. static int hotkey_write(char *buf)
  654. {
  655. int res, status, mask;
  656. char *cmd;
  657. int do_cmd = 0;
  658. if (!tp_features.hotkey)
  659. return -ENODEV;
  660. res = hotkey_get(&status, &mask);
  661. if (res)
  662. return res;
  663. while ((cmd = next_cmd(&buf))) {
  664. if (strlencmp(cmd, "enable") == 0) {
  665. status = 1;
  666. } else if (strlencmp(cmd, "disable") == 0) {
  667. status = 0;
  668. } else if (strlencmp(cmd, "reset") == 0) {
  669. status = hotkey_orig_status;
  670. mask = hotkey_orig_mask;
  671. } else if (sscanf(cmd, "0x%x", &mask) == 1) {
  672. /* mask set */
  673. } else if (sscanf(cmd, "%x", &mask) == 1) {
  674. /* mask set */
  675. } else
  676. return -EINVAL;
  677. do_cmd = 1;
  678. }
  679. if (do_cmd) {
  680. res = hotkey_set(status, mask);
  681. if (res)
  682. return res;
  683. }
  684. return 0;
  685. }
  686. static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
  687. .hid = IBM_HKEY_HID,
  688. .notify = hotkey_notify,
  689. .handle = &hkey_handle,
  690. .type = ACPI_DEVICE_NOTIFY,
  691. };
  692. static struct ibm_struct hotkey_driver_data = {
  693. .name = "hotkey",
  694. .read = hotkey_read,
  695. .write = hotkey_write,
  696. .exit = hotkey_exit,
  697. .acpi = &ibm_hotkey_acpidriver,
  698. };
  699. /*************************************************************************
  700. * Bluetooth subdriver
  701. */
  702. static int __init bluetooth_init(struct ibm_init_struct *iibm)
  703. {
  704. int status = 0;
  705. vdbg_printk(TPACPI_DBG_INIT, "initializing bluetooth subdriver\n");
  706. IBM_ACPIHANDLE_INIT(hkey);
  707. /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
  708. G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
  709. tp_features.bluetooth = hkey_handle &&
  710. acpi_evalf(hkey_handle, &status, "GBDC", "qd");
  711. vdbg_printk(TPACPI_DBG_INIT, "bluetooth is %s, status 0x%02x\n",
  712. str_supported(tp_features.bluetooth),
  713. status);
  714. if (tp_features.bluetooth &&
  715. !(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
  716. /* no bluetooth hardware present in system */
  717. tp_features.bluetooth = 0;
  718. dbg_printk(TPACPI_DBG_INIT,
  719. "bluetooth hardware not installed\n");
  720. }
  721. return (tp_features.bluetooth)? 0 : 1;
  722. }
  723. static int bluetooth_get_radiosw(void)
  724. {
  725. int status;
  726. if (!tp_features.bluetooth)
  727. return -ENODEV;
  728. if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
  729. return -EIO;
  730. return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0);
  731. }
  732. static int bluetooth_set_radiosw(int radio_on)
  733. {
  734. int status;
  735. if (!tp_features.bluetooth)
  736. return -ENODEV;
  737. if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
  738. return -EIO;
  739. if (radio_on)
  740. status |= TP_ACPI_BLUETOOTH_RADIOSSW;
  741. else
  742. status &= ~TP_ACPI_BLUETOOTH_RADIOSSW;
  743. if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
  744. return -EIO;
  745. return 0;
  746. }
  747. static int bluetooth_read(char *p)
  748. {
  749. int len = 0;
  750. int status = bluetooth_get_radiosw();
  751. if (!tp_features.bluetooth)
  752. len += sprintf(p + len, "status:\t\tnot supported\n");
  753. else {
  754. len += sprintf(p + len, "status:\t\t%s\n",
  755. (status)? "enabled" : "disabled");
  756. len += sprintf(p + len, "commands:\tenable, disable\n");
  757. }
  758. return len;
  759. }
  760. static int bluetooth_write(char *buf)
  761. {
  762. char *cmd;
  763. if (!tp_features.bluetooth)
  764. return -ENODEV;
  765. while ((cmd = next_cmd(&buf))) {
  766. if (strlencmp(cmd, "enable") == 0) {
  767. bluetooth_set_radiosw(1);
  768. } else if (strlencmp(cmd, "disable") == 0) {
  769. bluetooth_set_radiosw(0);
  770. } else
  771. return -EINVAL;
  772. }
  773. return 0;
  774. }
  775. static struct ibm_struct bluetooth_driver_data = {
  776. .name = "bluetooth",
  777. .read = bluetooth_read,
  778. .write = bluetooth_write,
  779. };
  780. /*************************************************************************
  781. * Wan subdriver
  782. */
  783. static int __init wan_init(struct ibm_init_struct *iibm)
  784. {
  785. int status = 0;
  786. vdbg_printk(TPACPI_DBG_INIT, "initializing wan subdriver\n");
  787. IBM_ACPIHANDLE_INIT(hkey);
  788. tp_features.wan = hkey_handle &&
  789. acpi_evalf(hkey_handle, &status, "GWAN", "qd");
  790. vdbg_printk(TPACPI_DBG_INIT, "wan is %s, status 0x%02x\n",
  791. str_supported(tp_features.wan),
  792. status);
  793. if (tp_features.wan &&
  794. !(status & TP_ACPI_WANCARD_HWPRESENT)) {
  795. /* no wan hardware present in system */
  796. tp_features.wan = 0;
  797. dbg_printk(TPACPI_DBG_INIT,
  798. "wan hardware not installed\n");
  799. }
  800. return (tp_features.wan)? 0 : 1;
  801. }
  802. static int wan_get_radiosw(void)
  803. {
  804. int status;
  805. if (!tp_features.wan)
  806. return -ENODEV;
  807. if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
  808. return -EIO;
  809. return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0);
  810. }
  811. static int wan_set_radiosw(int radio_on)
  812. {
  813. int status;
  814. if (!tp_features.wan)
  815. return -ENODEV;
  816. if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
  817. return -EIO;
  818. if (radio_on)
  819. status |= TP_ACPI_WANCARD_RADIOSSW;
  820. else
  821. status &= ~TP_ACPI_WANCARD_RADIOSSW;
  822. if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
  823. return -EIO;
  824. return 0;
  825. }
  826. static int wan_read(char *p)
  827. {
  828. int len = 0;
  829. int status = wan_get_radiosw();
  830. if (!tp_features.wan)
  831. len += sprintf(p + len, "status:\t\tnot supported\n");
  832. else {
  833. len += sprintf(p + len, "status:\t\t%s\n",
  834. (status)? "enabled" : "disabled");
  835. len += sprintf(p + len, "commands:\tenable, disable\n");
  836. }
  837. return len;
  838. }
  839. static int wan_write(char *buf)
  840. {
  841. char *cmd;
  842. if (!tp_features.wan)
  843. return -ENODEV;
  844. while ((cmd = next_cmd(&buf))) {
  845. if (strlencmp(cmd, "enable") == 0) {
  846. wan_set_radiosw(1);
  847. } else if (strlencmp(cmd, "disable") == 0) {
  848. wan_set_radiosw(0);
  849. } else
  850. return -EINVAL;
  851. }
  852. return 0;
  853. }
  854. static struct ibm_struct wan_driver_data = {
  855. .name = "wan",
  856. .read = wan_read,
  857. .write = wan_write,
  858. .flags.experimental = 1,
  859. };
  860. /*************************************************************************
  861. * Video subdriver
  862. */
  863. static enum video_access_mode video_supported;
  864. static int video_orig_autosw;
  865. IBM_HANDLE(vid, root, "\\_SB.PCI.AGP.VGA", /* 570 */
  866. "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
  867. "\\_SB.PCI0.VID0", /* 770e */
  868. "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
  869. "\\_SB.PCI0.AGP.VID", /* all others */
  870. ); /* R30, R31 */
  871. IBM_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
  872. static int __init video_init(struct ibm_init_struct *iibm)
  873. {
  874. int ivga;
  875. vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
  876. IBM_ACPIHANDLE_INIT(vid);
  877. IBM_ACPIHANDLE_INIT(vid2);
  878. if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
  879. /* G41, assume IVGA doesn't change */
  880. vid_handle = vid2_handle;
  881. if (!vid_handle)
  882. /* video switching not supported on R30, R31 */
  883. video_supported = TPACPI_VIDEO_NONE;
  884. else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
  885. /* 570 */
  886. video_supported = TPACPI_VIDEO_570;
  887. else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
  888. /* 600e/x, 770e, 770x */
  889. video_supported = TPACPI_VIDEO_770;
  890. else
  891. /* all others */
  892. video_supported = TPACPI_VIDEO_NEW;
  893. vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
  894. str_supported(video_supported != TPACPI_VIDEO_NONE),
  895. video_supported);
  896. return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
  897. }
  898. static void video_exit(void)
  899. {
  900. dbg_printk(TPACPI_DBG_EXIT,
  901. "restoring original video autoswitch mode\n");
  902. if (video_autosw_set(video_orig_autosw))
  903. printk(IBM_ERR "error while trying to restore original "
  904. "video autoswitch mode\n");
  905. }
  906. static int video_outputsw_get(void)
  907. {
  908. int status = 0;
  909. int i;
  910. switch (video_supported) {
  911. case TPACPI_VIDEO_570:
  912. if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
  913. TP_ACPI_VIDEO_570_PHSCMD))
  914. return -EIO;
  915. status = i & TP_ACPI_VIDEO_570_PHSMASK;
  916. break;
  917. case TPACPI_VIDEO_770:
  918. if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
  919. return -EIO;
  920. if (i)
  921. status |= TP_ACPI_VIDEO_S_LCD;
  922. if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
  923. return -EIO;
  924. if (i)
  925. status |= TP_ACPI_VIDEO_S_CRT;
  926. break;
  927. case TPACPI_VIDEO_NEW:
  928. if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
  929. !acpi_evalf(NULL, &i, "\\VCDC", "d"))
  930. return -EIO;
  931. if (i)
  932. status |= TP_ACPI_VIDEO_S_CRT;
  933. if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
  934. !acpi_evalf(NULL, &i, "\\VCDL", "d"))
  935. return -EIO;
  936. if (i)
  937. status |= TP_ACPI_VIDEO_S_LCD;
  938. if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
  939. return -EIO;
  940. if (i)
  941. status |= TP_ACPI_VIDEO_S_DVI;
  942. break;
  943. default:
  944. return -ENOSYS;
  945. }
  946. return status;
  947. }
  948. static int video_outputsw_set(int status)
  949. {
  950. int autosw;
  951. int res = 0;
  952. switch (video_supported) {
  953. case TPACPI_VIDEO_570:
  954. res = acpi_evalf(NULL, NULL,
  955. "\\_SB.PHS2", "vdd",
  956. TP_ACPI_VIDEO_570_PHS2CMD,
  957. status | TP_ACPI_VIDEO_570_PHS2SET);
  958. break;
  959. case TPACPI_VIDEO_770:
  960. autosw = video_autosw_get();
  961. if (autosw < 0)
  962. return autosw;
  963. res = video_autosw_set(1);
  964. if (res)
  965. return res;
  966. res = acpi_evalf(vid_handle, NULL,
  967. "ASWT", "vdd", status * 0x100, 0);
  968. if (!autosw && video_autosw_set(autosw)) {
  969. printk(IBM_ERR "video auto-switch left enabled due to error\n");
  970. return -EIO;
  971. }
  972. break;
  973. case TPACPI_VIDEO_NEW:
  974. res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
  975. acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
  976. break;
  977. default:
  978. return -ENOSYS;
  979. }
  980. return (res)? 0 : -EIO;
  981. }
  982. static int video_autosw_get(void)
  983. {
  984. int autosw = 0;
  985. switch (video_supported) {
  986. case TPACPI_VIDEO_570:
  987. if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
  988. return -EIO;
  989. break;
  990. case TPACPI_VIDEO_770:
  991. case TPACPI_VIDEO_NEW:
  992. if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
  993. return -EIO;
  994. break;
  995. default:
  996. return -ENOSYS;
  997. }
  998. return autosw & 1;
  999. }
  1000. static int video_autosw_set(int enable)
  1001. {
  1002. if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
  1003. return -EIO;
  1004. return 0;
  1005. }
  1006. static int video_outputsw_cycle(void)
  1007. {
  1008. int autosw = video_autosw_get();
  1009. int res;
  1010. if (autosw < 0)
  1011. return autosw;
  1012. switch (video_supported) {
  1013. case TPACPI_VIDEO_570:
  1014. res = video_autosw_set(1);
  1015. if (res)
  1016. return res;
  1017. res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
  1018. break;
  1019. case TPACPI_VIDEO_770:
  1020. case TPACPI_VIDEO_NEW:
  1021. res = video_autosw_set(1);
  1022. if (res)
  1023. return res;
  1024. res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
  1025. break;
  1026. default:
  1027. return -ENOSYS;
  1028. }
  1029. if (!autosw && video_autosw_set(autosw)) {
  1030. printk(IBM_ERR "video auto-switch left enabled due to error\n");
  1031. return -EIO;
  1032. }
  1033. return (res)? 0 : -EIO;
  1034. }
  1035. static int video_expand_toggle(void)
  1036. {
  1037. switch (video_supported) {
  1038. case TPACPI_VIDEO_570:
  1039. return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
  1040. 0 : -EIO;
  1041. case TPACPI_VIDEO_770:
  1042. return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
  1043. 0 : -EIO;
  1044. case TPACPI_VIDEO_NEW:
  1045. return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
  1046. 0 : -EIO;
  1047. default:
  1048. return -ENOSYS;
  1049. }
  1050. /* not reached */
  1051. }
  1052. static int video_read(char *p)
  1053. {
  1054. int status, autosw;
  1055. int len = 0;
  1056. if (video_supported == TPACPI_VIDEO_NONE) {
  1057. len += sprintf(p + len, "status:\t\tnot supported\n");
  1058. return len;
  1059. }
  1060. status = video_outputsw_get();
  1061. if (status < 0)
  1062. return status;
  1063. autosw = video_autosw_get();
  1064. if (autosw < 0)
  1065. return autosw;
  1066. len += sprintf(p + len, "status:\t\tsupported\n");
  1067. len += sprintf(p + len, "lcd:\t\t%s\n", enabled(status, 0));
  1068. len += sprintf(p + len, "crt:\t\t%s\n", enabled(status, 1));
  1069. if (video_supported == TPACPI_VIDEO_NEW)
  1070. len += sprintf(p + len, "dvi:\t\t%s\n", enabled(status, 3));
  1071. len += sprintf(p + len, "auto:\t\t%s\n", enabled(autosw, 0));
  1072. len += sprintf(p + len, "commands:\tlcd_enable, lcd_disable\n");
  1073. len += sprintf(p + len, "commands:\tcrt_enable, crt_disable\n");
  1074. if (video_supported == TPACPI_VIDEO_NEW)
  1075. len += sprintf(p + len, "commands:\tdvi_enable, dvi_disable\n");
  1076. len += sprintf(p + len, "commands:\tauto_enable, auto_disable\n");
  1077. len += sprintf(p + len, "commands:\tvideo_switch, expand_toggle\n");
  1078. return len;
  1079. }
  1080. static int video_write(char *buf)
  1081. {
  1082. char *cmd;
  1083. int enable, disable, status;
  1084. int res;
  1085. if (video_supported == TPACPI_VIDEO_NONE)
  1086. return -ENODEV;
  1087. enable = 0;
  1088. disable = 0;
  1089. while ((cmd = next_cmd(&buf))) {
  1090. if (strlencmp(cmd, "lcd_enable") == 0) {
  1091. enable |= TP_ACPI_VIDEO_S_LCD;
  1092. } else if (strlencmp(cmd, "lcd_disable") == 0) {
  1093. disable |= TP_ACPI_VIDEO_S_LCD;
  1094. } else if (strlencmp(cmd, "crt_enable") == 0) {
  1095. enable |= TP_ACPI_VIDEO_S_CRT;
  1096. } else if (strlencmp(cmd, "crt_disable") == 0) {
  1097. disable |= TP_ACPI_VIDEO_S_CRT;
  1098. } else if (video_supported == TPACPI_VIDEO_NEW &&
  1099. strlencmp(cmd, "dvi_enable") == 0) {
  1100. enable |= TP_ACPI_VIDEO_S_DVI;
  1101. } else if (video_supported == TPACPI_VIDEO_NEW &&
  1102. strlencmp(cmd, "dvi_disable") == 0) {
  1103. disable |= TP_ACPI_VIDEO_S_DVI;
  1104. } else if (strlencmp(cmd, "auto_enable") == 0) {
  1105. res = video_autosw_set(1);
  1106. if (res)
  1107. return res;
  1108. } else if (strlencmp(cmd, "auto_disable") == 0) {
  1109. res = video_autosw_set(0);
  1110. if (res)
  1111. return res;
  1112. } else if (strlencmp(cmd, "video_switch") == 0) {
  1113. res = video_outputsw_cycle();
  1114. if (res)
  1115. return res;
  1116. } else if (strlencmp(cmd, "expand_toggle") == 0) {
  1117. res = video_expand_toggle();
  1118. if (res)
  1119. return res;
  1120. } else
  1121. return -EINVAL;
  1122. }
  1123. if (enable || disable) {
  1124. status = video_outputsw_get();
  1125. if (status < 0)
  1126. return status;
  1127. res = video_outputsw_set((status & ~disable) | enable);
  1128. if (res)
  1129. return res;
  1130. }
  1131. return 0;
  1132. }
  1133. static struct ibm_struct video_driver_data = {
  1134. .name = "video",
  1135. .read = video_read,
  1136. .write = video_write,
  1137. .exit = video_exit,
  1138. };
  1139. /*************************************************************************
  1140. * Light (thinklight) subdriver
  1141. */
  1142. IBM_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
  1143. IBM_HANDLE(ledb, ec, "LEDB"); /* G4x */
  1144. static int __init light_init(struct ibm_init_struct *iibm)
  1145. {
  1146. vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
  1147. IBM_ACPIHANDLE_INIT(ledb);
  1148. IBM_ACPIHANDLE_INIT(lght);
  1149. IBM_ACPIHANDLE_INIT(cmos);
  1150. /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
  1151. tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
  1152. if (tp_features.light)
  1153. /* light status not supported on
  1154. 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
  1155. tp_features.light_status =
  1156. acpi_evalf(ec_handle, NULL, "KBLT", "qv");
  1157. vdbg_printk(TPACPI_DBG_INIT, "light is %s\n",
  1158. str_supported(tp_features.light));
  1159. return (tp_features.light)? 0 : 1;
  1160. }
  1161. static int light_read(char *p)
  1162. {
  1163. int len = 0;
  1164. int status = 0;
  1165. if (!tp_features.light) {
  1166. len += sprintf(p + len, "status:\t\tnot supported\n");
  1167. } else if (!tp_features.light_status) {
  1168. len += sprintf(p + len, "status:\t\tunknown\n");
  1169. len += sprintf(p + len, "commands:\ton, off\n");
  1170. } else {
  1171. if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
  1172. return -EIO;
  1173. len += sprintf(p + len, "status:\t\t%s\n", onoff(status, 0));
  1174. len += sprintf(p + len, "commands:\ton, off\n");
  1175. }
  1176. return len;
  1177. }
  1178. static int light_write(char *buf)
  1179. {
  1180. int cmos_cmd, lght_cmd;
  1181. char *cmd;
  1182. int success;
  1183. if (!tp_features.light)
  1184. return -ENODEV;
  1185. while ((cmd = next_cmd(&buf))) {
  1186. if (strlencmp(cmd, "on") == 0) {
  1187. cmos_cmd = 0x0c;
  1188. lght_cmd = 1;
  1189. } else if (strlencmp(cmd, "off") == 0) {
  1190. cmos_cmd = 0x0d;
  1191. lght_cmd = 0;
  1192. } else
  1193. return -EINVAL;
  1194. success = cmos_handle ?
  1195. acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd) :
  1196. acpi_evalf(lght_handle, NULL, NULL, "vd", lght_cmd);
  1197. if (!success)
  1198. return -EIO;
  1199. }
  1200. return 0;
  1201. }
  1202. static struct ibm_struct light_driver_data = {
  1203. .name = "light",
  1204. .read = light_read,
  1205. .write = light_write,
  1206. };
  1207. /*************************************************************************
  1208. * Dock subdriver
  1209. */
  1210. #ifdef CONFIG_THINKPAD_ACPI_DOCK
  1211. IBM_HANDLE(dock, root, "\\_SB.GDCK", /* X30, X31, X40 */
  1212. "\\_SB.PCI0.DOCK", /* 600e/x,770e,770x,A2xm/p,T20-22,X20-21 */
  1213. "\\_SB.PCI0.PCI1.DOCK", /* all others */
  1214. "\\_SB.PCI.ISA.SLCE", /* 570 */
  1215. ); /* A21e,G4x,R30,R31,R32,R40,R40e,R50e */
  1216. /* don't list other alternatives as we install a notify handler on the 570 */
  1217. IBM_HANDLE(pci, root, "\\_SB.PCI"); /* 570 */
  1218. #define dock_docked() (_sta(dock_handle) & 1)
  1219. static int __init dock_init(struct ibm_init_struct *iibm)
  1220. {
  1221. vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver\n");
  1222. IBM_ACPIHANDLE_INIT(dock);
  1223. IBM_ACPIHANDLE_INIT(pci);
  1224. vdbg_printk(TPACPI_DBG_INIT, "dock is %s\n",
  1225. str_supported(dock_handle != NULL));
  1226. return (dock_handle)? 0 : 1;
  1227. }
  1228. static void dock_notify(struct ibm_struct *ibm, u32 event)
  1229. {
  1230. int docked = dock_docked();
  1231. int pci = ibm->acpi->hid && strstr(ibm->acpi->hid, IBM_PCI_HID);
  1232. if (event == 1 && !pci) /* 570 */
  1233. acpi_bus_generate_event(ibm->acpi->device, event, 1); /* button */
  1234. else if (event == 1 && pci) /* 570 */
  1235. acpi_bus_generate_event(ibm->acpi->device, event, 3); /* dock */
  1236. else if (event == 3 && docked)
  1237. acpi_bus_generate_event(ibm->acpi->device, event, 1); /* button */
  1238. else if (event == 3 && !docked)
  1239. acpi_bus_generate_event(ibm->acpi->device, event, 2); /* undock */
  1240. else if (event == 0 && docked)
  1241. acpi_bus_generate_event(ibm->acpi->device, event, 3); /* dock */
  1242. else {
  1243. printk(IBM_ERR "unknown dock event %d, status %d\n",
  1244. event, _sta(dock_handle));
  1245. acpi_bus_generate_event(ibm->acpi->device, event, 0); /* unknown */
  1246. }
  1247. }
  1248. static int dock_read(char *p)
  1249. {
  1250. int len = 0;
  1251. int docked = dock_docked();
  1252. if (!dock_handle)
  1253. len += sprintf(p + len, "status:\t\tnot supported\n");
  1254. else if (!docked)
  1255. len += sprintf(p + len, "status:\t\tundocked\n");
  1256. else {
  1257. len += sprintf(p + len, "status:\t\tdocked\n");
  1258. len += sprintf(p + len, "commands:\tdock, undock\n");
  1259. }
  1260. return len;
  1261. }
  1262. static int dock_write(char *buf)
  1263. {
  1264. char *cmd;
  1265. if (!dock_docked())
  1266. return -ENODEV;
  1267. while ((cmd = next_cmd(&buf))) {
  1268. if (strlencmp(cmd, "undock") == 0) {
  1269. if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 0) ||
  1270. !acpi_evalf(dock_handle, NULL, "_EJ0", "vd", 1))
  1271. return -EIO;
  1272. } else if (strlencmp(cmd, "dock") == 0) {
  1273. if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 1))
  1274. return -EIO;
  1275. } else
  1276. return -EINVAL;
  1277. }
  1278. return 0;
  1279. }
  1280. static struct tp_acpi_drv_struct ibm_dock_acpidriver[2] = {
  1281. {
  1282. .notify = dock_notify,
  1283. .handle = &dock_handle,
  1284. .type = ACPI_SYSTEM_NOTIFY,
  1285. },
  1286. {
  1287. .hid = IBM_PCI_HID,
  1288. .notify = dock_notify,
  1289. .handle = &pci_handle,
  1290. .type = ACPI_SYSTEM_NOTIFY,
  1291. },
  1292. };
  1293. static struct ibm_struct dock_driver_data[2] = {
  1294. {
  1295. .name = "dock",
  1296. .read = dock_read,
  1297. .write = dock_write,
  1298. .acpi = &ibm_dock_acpidriver[0],
  1299. },
  1300. {
  1301. .name = "dock",
  1302. .acpi = &ibm_dock_acpidriver[1],
  1303. },
  1304. };
  1305. #endif /* CONFIG_THINKPAD_ACPI_DOCK */
  1306. /*************************************************************************
  1307. * Bay subdriver
  1308. */
  1309. #ifdef CONFIG_THINKPAD_ACPI_BAY
  1310. IBM_HANDLE(bay, root, "\\_SB.PCI.IDE.SECN.MAST", /* 570 */
  1311. "\\_SB.PCI0.IDE0.IDES.IDSM", /* 600e/x, 770e, 770x */
  1312. "\\_SB.PCI0.SATA.SCND.MSTR", /* T60, X60, Z60 */
  1313. "\\_SB.PCI0.IDE0.SCND.MSTR", /* all others */
  1314. ); /* A21e, R30, R31 */
  1315. IBM_HANDLE(bay_ej, bay, "_EJ3", /* 600e/x, A2xm/p, A3x */
  1316. "_EJ0", /* all others */
  1317. ); /* 570,A21e,G4x,R30,R31,R32,R40e,R50e */
  1318. IBM_HANDLE(bay2, root, "\\_SB.PCI0.IDE0.PRIM.SLAV", /* A3x, R32 */
  1319. "\\_SB.PCI0.IDE0.IDEP.IDPS", /* 600e/x, 770e, 770x */
  1320. ); /* all others */
  1321. IBM_HANDLE(bay2_ej, bay2, "_EJ3", /* 600e/x, 770e, A3x */
  1322. "_EJ0", /* 770x */
  1323. ); /* all others */
  1324. static int __init bay_init(struct ibm_init_struct *iibm)
  1325. {
  1326. vdbg_printk(TPACPI_DBG_INIT, "initializing bay subdriver\n");
  1327. IBM_ACPIHANDLE_INIT(bay);
  1328. if (bay_handle)
  1329. IBM_ACPIHANDLE_INIT(bay_ej);
  1330. IBM_ACPIHANDLE_INIT(bay2);
  1331. if (bay2_handle)
  1332. IBM_ACPIHANDLE_INIT(bay2_ej);
  1333. tp_features.bay_status = bay_handle &&
  1334. acpi_evalf(bay_handle, NULL, "_STA", "qv");
  1335. tp_features.bay_status2 = bay2_handle &&
  1336. acpi_evalf(bay2_handle, NULL, "_STA", "qv");
  1337. tp_features.bay_eject = bay_handle && bay_ej_handle &&
  1338. (strlencmp(bay_ej_path, "_EJ0") == 0 || experimental);
  1339. tp_features.bay_eject2 = bay2_handle && bay2_ej_handle &&
  1340. (strlencmp(bay2_ej_path, "_EJ0") == 0 || experimental);
  1341. vdbg_printk(TPACPI_DBG_INIT,
  1342. "bay 1: status %s, eject %s; bay 2: status %s, eject %s\n",
  1343. str_supported(tp_features.bay_status),
  1344. str_supported(tp_features.bay_eject),
  1345. str_supported(tp_features.bay_status2),
  1346. str_supported(tp_features.bay_eject2));
  1347. return (tp_features.bay_status || tp_features.bay_eject ||
  1348. tp_features.bay_status2 || tp_features.bay_eject2)? 0 : 1;
  1349. }
  1350. static void bay_notify(struct ibm_struct *ibm, u32 event)
  1351. {
  1352. acpi_bus_generate_event(ibm->acpi->device, event, 0);
  1353. }
  1354. #define bay_occupied(b) (_sta(b##_handle) & 1)
  1355. static int bay_read(char *p)
  1356. {
  1357. int len = 0;
  1358. int occupied = bay_occupied(bay);
  1359. int occupied2 = bay_occupied(bay2);
  1360. int eject, eject2;
  1361. len += sprintf(p + len, "status:\t\t%s\n",
  1362. tp_features.bay_status ?
  1363. (occupied ? "occupied" : "unoccupied") :
  1364. "not supported");
  1365. if (tp_features.bay_status2)
  1366. len += sprintf(p + len, "status2:\t%s\n", occupied2 ?
  1367. "occupied" : "unoccupied");
  1368. eject = tp_features.bay_eject && occupied;
  1369. eject2 = tp_features.bay_eject2 && occupied2;
  1370. if (eject && eject2)
  1371. len += sprintf(p + len, "commands:\teject, eject2\n");
  1372. else if (eject)
  1373. len += sprintf(p + len, "commands:\teject\n");
  1374. else if (eject2)
  1375. len += sprintf(p + len, "commands:\teject2\n");
  1376. return len;
  1377. }
  1378. static int bay_write(char *buf)
  1379. {
  1380. char *cmd;
  1381. if (!tp_features.bay_eject && !tp_features.bay_eject2)
  1382. return -ENODEV;
  1383. while ((cmd = next_cmd(&buf))) {
  1384. if (tp_features.bay_eject && strlencmp(cmd, "eject") == 0) {
  1385. if (!acpi_evalf(bay_ej_handle, NULL, NULL, "vd", 1))
  1386. return -EIO;
  1387. } else if (tp_features.bay_eject2 &&
  1388. strlencmp(cmd, "eject2") == 0) {
  1389. if (!acpi_evalf(bay2_ej_handle, NULL, NULL, "vd", 1))
  1390. return -EIO;
  1391. } else
  1392. return -EINVAL;
  1393. }
  1394. return 0;
  1395. }
  1396. static struct tp_acpi_drv_struct ibm_bay_acpidriver = {
  1397. .notify = bay_notify,
  1398. .handle = &bay_handle,
  1399. .type = ACPI_SYSTEM_NOTIFY,
  1400. };
  1401. static struct ibm_struct bay_driver_data = {
  1402. .name = "bay",
  1403. .read = bay_read,
  1404. .write = bay_write,
  1405. .acpi = &ibm_bay_acpidriver,
  1406. };
  1407. #endif /* CONFIG_THINKPAD_ACPI_BAY */
  1408. /*************************************************************************
  1409. * CMOS subdriver
  1410. */
  1411. static int __init cmos_init(struct ibm_init_struct *iibm)
  1412. {
  1413. vdbg_printk(TPACPI_DBG_INIT,
  1414. "initializing cmos commands subdriver\n");
  1415. IBM_ACPIHANDLE_INIT(cmos);
  1416. vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
  1417. str_supported(cmos_handle != NULL));
  1418. return (cmos_handle)? 0 : 1;
  1419. }
  1420. static int cmos_read(char *p)
  1421. {
  1422. int len = 0;
  1423. /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
  1424. R30, R31, T20-22, X20-21 */
  1425. if (!cmos_handle)
  1426. len += sprintf(p + len, "status:\t\tnot supported\n");
  1427. else {
  1428. len += sprintf(p + len, "status:\t\tsupported\n");
  1429. len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-21)\n");
  1430. }
  1431. return len;
  1432. }
  1433. static int cmos_write(char *buf)
  1434. {
  1435. char *cmd;
  1436. int cmos_cmd, res;
  1437. while ((cmd = next_cmd(&buf))) {
  1438. if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
  1439. cmos_cmd >= 0 && cmos_cmd <= 21) {
  1440. /* cmos_cmd set */
  1441. } else
  1442. return -EINVAL;
  1443. res = issue_thinkpad_cmos_command(cmos_cmd);
  1444. if (res)
  1445. return res;
  1446. }
  1447. return 0;
  1448. }
  1449. static struct ibm_struct cmos_driver_data = {
  1450. .name = "cmos",
  1451. .read = cmos_read,
  1452. .write = cmos_write,
  1453. };
  1454. /*************************************************************************
  1455. * LED subdriver
  1456. */
  1457. static enum led_access_mode led_supported;
  1458. IBM_HANDLE(led, ec, "SLED", /* 570 */
  1459. "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
  1460. "LED", /* all others */
  1461. ); /* R30, R31 */
  1462. static int __init led_init(struct ibm_init_struct *iibm)
  1463. {
  1464. vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
  1465. IBM_ACPIHANDLE_INIT(led);
  1466. if (!led_handle)
  1467. /* led not supported on R30, R31 */
  1468. led_supported = TPACPI_LED_NONE;
  1469. else if (strlencmp(led_path, "SLED") == 0)
  1470. /* 570 */
  1471. led_supported = TPACPI_LED_570;
  1472. else if (strlencmp(led_path, "SYSL") == 0)
  1473. /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
  1474. led_supported = TPACPI_LED_OLD;
  1475. else
  1476. /* all others */
  1477. led_supported = TPACPI_LED_NEW;
  1478. vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
  1479. str_supported(led_supported), led_supported);
  1480. return (led_supported != TPACPI_LED_NONE)? 0 : 1;
  1481. }
  1482. #define led_status(s) ((s) == 0 ? "off" : ((s) == 1 ? "on" : "blinking"))
  1483. static int led_read(char *p)
  1484. {
  1485. int len = 0;
  1486. if (!led_supported) {
  1487. len += sprintf(p + len, "status:\t\tnot supported\n");
  1488. return len;
  1489. }
  1490. len += sprintf(p + len, "status:\t\tsupported\n");
  1491. if (led_supported == TPACPI_LED_570) {
  1492. /* 570 */
  1493. int i, status;
  1494. for (i = 0; i < 8; i++) {
  1495. if (!acpi_evalf(ec_handle,
  1496. &status, "GLED", "dd", 1 << i))
  1497. return -EIO;
  1498. len += sprintf(p + len, "%d:\t\t%s\n",
  1499. i, led_status(status));
  1500. }
  1501. }
  1502. len += sprintf(p + len, "commands:\t"
  1503. "<led> on, <led> off, <led> blink (<led> is 0-7)\n");
  1504. return len;
  1505. }
  1506. /* off, on, blink */
  1507. static const int led_sled_arg1[] = { 0, 1, 3 };
  1508. static const int led_exp_hlbl[] = { 0, 0, 1 }; /* led# * */
  1509. static const int led_exp_hlcl[] = { 0, 1, 1 }; /* led# * */
  1510. static const int led_led_arg1[] = { 0, 0x80, 0xc0 };
  1511. static int led_write(char *buf)
  1512. {
  1513. char *cmd;
  1514. int led, ind, ret;
  1515. if (!led_supported)
  1516. return -ENODEV;
  1517. while ((cmd = next_cmd(&buf))) {
  1518. if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 7)
  1519. return -EINVAL;
  1520. if (strstr(cmd, "off")) {
  1521. ind = 0;
  1522. } else if (strstr(cmd, "on")) {
  1523. ind = 1;
  1524. } else if (strstr(cmd, "blink")) {
  1525. ind = 2;
  1526. } else
  1527. return -EINVAL;
  1528. if (led_supported == TPACPI_LED_570) {
  1529. /* 570 */
  1530. led = 1 << led;
  1531. if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
  1532. led, led_sled_arg1[ind]))
  1533. return -EIO;
  1534. } else if (led_supported == TPACPI_LED_OLD) {
  1535. /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
  1536. led = 1 << led;
  1537. ret = ec_write(TPACPI_LED_EC_HLMS, led);
  1538. if (ret >= 0)
  1539. ret =
  1540. ec_write(TPACPI_LED_EC_HLBL,
  1541. led * led_exp_hlbl[ind]);
  1542. if (ret >= 0)
  1543. ret =
  1544. ec_write(TPACPI_LED_EC_HLCL,
  1545. led * led_exp_hlcl[ind]);
  1546. if (ret < 0)
  1547. return ret;
  1548. } else {
  1549. /* all others */
  1550. if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
  1551. led, led_led_arg1[ind]))
  1552. return -EIO;
  1553. }
  1554. }
  1555. return 0;
  1556. }
  1557. static struct ibm_struct led_driver_data = {
  1558. .name = "led",
  1559. .read = led_read,
  1560. .write = led_write,
  1561. };
  1562. /*************************************************************************
  1563. * Beep subdriver
  1564. */
  1565. IBM_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
  1566. static int __init beep_init(struct ibm_init_struct *iibm)
  1567. {
  1568. vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
  1569. IBM_ACPIHANDLE_INIT(beep);
  1570. vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
  1571. str_supported(beep_handle != NULL));
  1572. return (beep_handle)? 0 : 1;
  1573. }
  1574. static int beep_read(char *p)
  1575. {
  1576. int len = 0;
  1577. if (!beep_handle)
  1578. len += sprintf(p + len, "status:\t\tnot supported\n");
  1579. else {
  1580. len += sprintf(p + len, "status:\t\tsupported\n");
  1581. len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-17)\n");
  1582. }
  1583. return len;
  1584. }
  1585. static int beep_write(char *buf)
  1586. {
  1587. char *cmd;
  1588. int beep_cmd;
  1589. if (!beep_handle)
  1590. return -ENODEV;
  1591. while ((cmd = next_cmd(&buf))) {
  1592. if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
  1593. beep_cmd >= 0 && beep_cmd <= 17) {
  1594. /* beep_cmd set */
  1595. } else
  1596. return -EINVAL;
  1597. if (!acpi_evalf(beep_handle, NULL, NULL, "vdd", beep_cmd, 0))
  1598. return -EIO;
  1599. }
  1600. return 0;
  1601. }
  1602. static struct ibm_struct beep_driver_data = {
  1603. .name = "beep",
  1604. .read = beep_read,
  1605. .write = beep_write,
  1606. };
  1607. /*************************************************************************
  1608. * Thermal subdriver
  1609. */
  1610. static enum thermal_access_mode thermal_read_mode;
  1611. static int __init thermal_init(struct ibm_init_struct *iibm)
  1612. {
  1613. u8 t, ta1, ta2;
  1614. int i;
  1615. int acpi_tmp7;
  1616. vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
  1617. acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
  1618. if (ibm_thinkpad_ec_found && experimental) {
  1619. /*
  1620. * Direct EC access mode: sensors at registers
  1621. * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
  1622. * non-implemented, thermal sensors return 0x80 when
  1623. * not available
  1624. */
  1625. ta1 = ta2 = 0;
  1626. for (i = 0; i < 8; i++) {
  1627. if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
  1628. ta1 |= t;
  1629. } else {
  1630. ta1 = 0;
  1631. break;
  1632. }
  1633. if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
  1634. ta2 |= t;
  1635. } else {
  1636. ta1 = 0;
  1637. break;
  1638. }
  1639. }
  1640. if (ta1 == 0) {
  1641. /* This is sheer paranoia, but we handle it anyway */
  1642. if (acpi_tmp7) {
  1643. printk(IBM_ERR
  1644. "ThinkPad ACPI EC access misbehaving, "
  1645. "falling back to ACPI TMPx access mode\n");
  1646. thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
  1647. } else {
  1648. printk(IBM_ERR
  1649. "ThinkPad ACPI EC access misbehaving, "
  1650. "disabling thermal sensors access\n");
  1651. thermal_read_mode = TPACPI_THERMAL_NONE;
  1652. }
  1653. } else {
  1654. thermal_read_mode =
  1655. (ta2 != 0) ?
  1656. TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
  1657. }
  1658. } else if (acpi_tmp7) {
  1659. if (acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
  1660. /* 600e/x, 770e, 770x */
  1661. thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
  1662. } else {
  1663. /* Standard ACPI TMPx access, max 8 sensors */
  1664. thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
  1665. }
  1666. } else {
  1667. /* temperatures not supported on 570, G4x, R30, R31, R32 */
  1668. thermal_read_mode = TPACPI_THERMAL_NONE;
  1669. }
  1670. vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
  1671. str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
  1672. thermal_read_mode);
  1673. return (thermal_read_mode != TPACPI_THERMAL_NONE)? 0 : 1;
  1674. }
  1675. /* idx is zero-based */
  1676. static int thermal_get_sensor(int idx, s32 *value)
  1677. {
  1678. int t;
  1679. s8 tmp;
  1680. char tmpi[5];
  1681. t = TP_EC_THERMAL_TMP0;
  1682. switch (thermal_read_mode) {
  1683. #if TPACPI_MAX_THERMAL_SENSORS >= 16
  1684. case TPACPI_THERMAL_TPEC_16:
  1685. if (idx >= 8 && idx <= 15) {
  1686. t = TP_EC_THERMAL_TMP8;
  1687. idx -= 8;
  1688. }
  1689. /* fallthrough */
  1690. #endif
  1691. case TPACPI_THERMAL_TPEC_8:
  1692. if (idx <= 7) {
  1693. if (!acpi_ec_read(t + idx, &tmp))
  1694. return -EIO;
  1695. *value = tmp * 1000;
  1696. return 0;
  1697. }
  1698. break;
  1699. case TPACPI_THERMAL_ACPI_UPDT:
  1700. if (idx <= 7) {
  1701. snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
  1702. if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
  1703. return -EIO;
  1704. if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
  1705. return -EIO;
  1706. *value = (t - 2732) * 100;
  1707. return 0;
  1708. }
  1709. break;
  1710. case TPACPI_THERMAL_ACPI_TMP07:
  1711. if (idx <= 7) {
  1712. snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
  1713. if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
  1714. return -EIO;
  1715. *value = t * 1000;
  1716. return 0;
  1717. }
  1718. break;
  1719. case TPACPI_THERMAL_NONE:
  1720. default:
  1721. return -ENOSYS;
  1722. }
  1723. return -EINVAL;
  1724. }
  1725. static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
  1726. {
  1727. int res, i;
  1728. int n;
  1729. n = 8;
  1730. i = 0;
  1731. if (!s)
  1732. return -EINVAL;
  1733. if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
  1734. n = 16;
  1735. for(i = 0 ; i < n; i++) {
  1736. res = thermal_get_sensor(i, &s->temp[i]);
  1737. if (res)
  1738. return res;
  1739. }
  1740. return n;
  1741. }
  1742. static int thermal_read(char *p)
  1743. {
  1744. int len = 0;
  1745. int n, i;
  1746. struct ibm_thermal_sensors_struct t;
  1747. n = thermal_get_sensors(&t);
  1748. if (unlikely(n < 0))
  1749. return n;
  1750. len += sprintf(p + len, "temperatures:\t");
  1751. if (n > 0) {
  1752. for (i = 0; i < (n - 1); i++)
  1753. len += sprintf(p + len, "%d ", t.temp[i] / 1000);
  1754. len += sprintf(p + len, "%d\n", t.temp[i] / 1000);
  1755. } else
  1756. len += sprintf(p + len, "not supported\n");
  1757. return len;
  1758. }
  1759. static struct ibm_struct thermal_driver_data = {
  1760. .name = "thermal",
  1761. .read = thermal_read,
  1762. };
  1763. /*************************************************************************
  1764. * EC Dump subdriver
  1765. */
  1766. static u8 ecdump_regs[256];
  1767. static int ecdump_read(char *p)
  1768. {
  1769. int len = 0;
  1770. int i, j;
  1771. u8 v;
  1772. len += sprintf(p + len, "EC "
  1773. " +00 +01 +02 +03 +04 +05 +06 +07"
  1774. " +08 +09 +0a +0b +0c +0d +0e +0f\n");
  1775. for (i = 0; i < 256; i += 16) {
  1776. len += sprintf(p + len, "EC 0x%02x:", i);
  1777. for (j = 0; j < 16; j++) {
  1778. if (!acpi_ec_read(i + j, &v))
  1779. break;
  1780. if (v != ecdump_regs[i + j])
  1781. len += sprintf(p + len, " *%02x", v);
  1782. else
  1783. len += sprintf(p + len, " %02x", v);
  1784. ecdump_regs[i + j] = v;
  1785. }
  1786. len += sprintf(p + len, "\n");
  1787. if (j != 16)
  1788. break;
  1789. }
  1790. /* These are way too dangerous to advertise openly... */
  1791. #if 0
  1792. len += sprintf(p + len, "commands:\t0x<offset> 0x<value>"
  1793. " (<offset> is 00-ff, <value> is 00-ff)\n");
  1794. len += sprintf(p + len, "commands:\t0x<offset> <value> "
  1795. " (<offset> is 00-ff, <value> is 0-255)\n");
  1796. #endif
  1797. return len;
  1798. }
  1799. static int ecdump_write(char *buf)
  1800. {
  1801. char *cmd;
  1802. int i, v;
  1803. while ((cmd = next_cmd(&buf))) {
  1804. if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
  1805. /* i and v set */
  1806. } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
  1807. /* i and v set */
  1808. } else
  1809. return -EINVAL;
  1810. if (i >= 0 && i < 256 && v >= 0 && v < 256) {
  1811. if (!acpi_ec_write(i, v))
  1812. return -EIO;
  1813. } else
  1814. return -EINVAL;
  1815. }
  1816. return 0;
  1817. }
  1818. static struct ibm_struct ecdump_driver_data = {
  1819. .name = "ecdump",
  1820. .read = ecdump_read,
  1821. .write = ecdump_write,
  1822. .flags.experimental = 1,
  1823. };
  1824. /*************************************************************************
  1825. * Backlight/brightness subdriver
  1826. */
  1827. static struct backlight_device *ibm_backlight_device = NULL;
  1828. static struct backlight_ops ibm_backlight_data = {
  1829. .get_brightness = brightness_get,
  1830. .update_status = brightness_update_status,
  1831. };
  1832. static int __init brightness_init(struct ibm_init_struct *iibm)
  1833. {
  1834. int b;
  1835. vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
  1836. b = brightness_get(NULL);
  1837. if (b < 0)
  1838. return b;
  1839. ibm_backlight_device = backlight_device_register("ibm", NULL, NULL,
  1840. &ibm_backlight_data);
  1841. if (IS_ERR(ibm_backlight_device)) {
  1842. printk(IBM_ERR "Could not register backlight device\n");
  1843. return PTR_ERR(ibm_backlight_device);
  1844. }
  1845. vdbg_printk(TPACPI_DBG_INIT, "brightness is supported\n");
  1846. ibm_backlight_device->props.max_brightness = 7;
  1847. ibm_backlight_device->props.brightness = b;
  1848. backlight_update_status(ibm_backlight_device);
  1849. return 0;
  1850. }
  1851. static void brightness_exit(void)
  1852. {
  1853. if (ibm_backlight_device) {
  1854. vdbg_printk(TPACPI_DBG_EXIT,
  1855. "calling backlight_device_unregister()\n");
  1856. backlight_device_unregister(ibm_backlight_device);
  1857. ibm_backlight_device = NULL;
  1858. }
  1859. }
  1860. static int brightness_update_status(struct backlight_device *bd)
  1861. {
  1862. return brightness_set(
  1863. (bd->props.fb_blank == FB_BLANK_UNBLANK &&
  1864. bd->props.power == FB_BLANK_UNBLANK) ?
  1865. bd->props.brightness : 0);
  1866. }
  1867. static int brightness_get(struct backlight_device *bd)
  1868. {
  1869. u8 level;
  1870. if (!acpi_ec_read(brightness_offset, &level))
  1871. return -EIO;
  1872. level &= 0x7;
  1873. return level;
  1874. }
  1875. static int brightness_set(int value)
  1876. {
  1877. int cmos_cmd, inc, i;
  1878. int current_value = brightness_get(NULL);
  1879. value &= 7;
  1880. cmos_cmd = value > current_value ? TP_CMOS_BRIGHTNESS_UP : TP_CMOS_BRIGHTNESS_DOWN;
  1881. inc = value > current_value ? 1 : -1;
  1882. for (i = current_value; i != value; i += inc) {
  1883. if (issue_thinkpad_cmos_command(cmos_cmd))
  1884. return -EIO;
  1885. if (!acpi_ec_write(brightness_offset, i + inc))
  1886. return -EIO;
  1887. }
  1888. return 0;
  1889. }
  1890. static int brightness_read(char *p)
  1891. {
  1892. int len = 0;
  1893. int level;
  1894. if ((level = brightness_get(NULL)) < 0) {
  1895. len += sprintf(p + len, "level:\t\tunreadable\n");
  1896. } else {
  1897. len += sprintf(p + len, "level:\t\t%d\n", level & 0x7);
  1898. len += sprintf(p + len, "commands:\tup, down\n");
  1899. len += sprintf(p + len, "commands:\tlevel <level>"
  1900. " (<level> is 0-7)\n");
  1901. }
  1902. return len;
  1903. }
  1904. static int brightness_write(char *buf)
  1905. {
  1906. int level;
  1907. int new_level;
  1908. char *cmd;
  1909. while ((cmd = next_cmd(&buf))) {
  1910. if ((level = brightness_get(NULL)) < 0)
  1911. return level;
  1912. level &= 7;
  1913. if (strlencmp(cmd, "up") == 0) {
  1914. new_level = level == 7 ? 7 : level + 1;
  1915. } else if (strlencmp(cmd, "down") == 0) {
  1916. new_level = level == 0 ? 0 : level - 1;
  1917. } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
  1918. new_level >= 0 && new_level <= 7) {
  1919. /* new_level set */
  1920. } else
  1921. return -EINVAL;
  1922. brightness_set(new_level);
  1923. }
  1924. return 0;
  1925. }
  1926. static struct ibm_struct brightness_driver_data = {
  1927. .name = "brightness",
  1928. .read = brightness_read,
  1929. .write = brightness_write,
  1930. .exit = brightness_exit,
  1931. };
  1932. /*************************************************************************
  1933. * Volume subdriver
  1934. */
  1935. static int volume_read(char *p)
  1936. {
  1937. int len = 0;
  1938. u8 level;
  1939. if (!acpi_ec_read(volume_offset, &level)) {
  1940. len += sprintf(p + len, "level:\t\tunreadable\n");
  1941. } else {
  1942. len += sprintf(p + len, "level:\t\t%d\n", level & 0xf);
  1943. len += sprintf(p + len, "mute:\t\t%s\n", onoff(level, 6));
  1944. len += sprintf(p + len, "commands:\tup, down, mute\n");
  1945. len += sprintf(p + len, "commands:\tlevel <level>"
  1946. " (<level> is 0-15)\n");
  1947. }
  1948. return len;
  1949. }
  1950. static int volume_write(char *buf)
  1951. {
  1952. int cmos_cmd, inc, i;
  1953. u8 level, mute;
  1954. int new_level, new_mute;
  1955. char *cmd;
  1956. while ((cmd = next_cmd(&buf))) {
  1957. if (!acpi_ec_read(volume_offset, &level))
  1958. return -EIO;
  1959. new_mute = mute = level & 0x40;
  1960. new_level = level = level & 0xf;
  1961. if (strlencmp(cmd, "up") == 0) {
  1962. if (mute)
  1963. new_mute = 0;
  1964. else
  1965. new_level = level == 15 ? 15 : level + 1;
  1966. } else if (strlencmp(cmd, "down") == 0) {
  1967. if (mute)
  1968. new_mute = 0;
  1969. else
  1970. new_level = level == 0 ? 0 : level - 1;
  1971. } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
  1972. new_level >= 0 && new_level <= 15) {
  1973. /* new_level set */
  1974. } else if (strlencmp(cmd, "mute") == 0) {
  1975. new_mute = 0x40;
  1976. } else
  1977. return -EINVAL;
  1978. if (new_level != level) { /* mute doesn't change */
  1979. cmos_cmd = new_level > level ? TP_CMOS_VOLUME_UP : TP_CMOS_VOLUME_DOWN;
  1980. inc = new_level > level ? 1 : -1;
  1981. if (mute && (issue_thinkpad_cmos_command(cmos_cmd) ||
  1982. !acpi_ec_write(volume_offset, level)))
  1983. return -EIO;
  1984. for (i = level; i != new_level; i += inc)
  1985. if (issue_thinkpad_cmos_command(cmos_cmd) ||
  1986. !acpi_ec_write(volume_offset, i + inc))
  1987. return -EIO;
  1988. if (mute && (issue_thinkpad_cmos_command(TP_CMOS_VOLUME_MUTE) ||
  1989. !acpi_ec_write(volume_offset,
  1990. new_level + mute)))
  1991. return -EIO;
  1992. }
  1993. if (new_mute != mute) { /* level doesn't change */
  1994. cmos_cmd = new_mute ? TP_CMOS_VOLUME_MUTE : TP_CMOS_VOLUME_UP;
  1995. if (issue_thinkpad_cmos_command(cmos_cmd) ||
  1996. !acpi_ec_write(volume_offset, level + new_mute))
  1997. return -EIO;
  1998. }
  1999. }
  2000. return 0;
  2001. }
  2002. static struct ibm_struct volume_driver_data = {
  2003. .name = "volume",
  2004. .read = volume_read,
  2005. .write = volume_write,
  2006. };
  2007. /*************************************************************************
  2008. * Fan subdriver
  2009. */
  2010. /*
  2011. * FAN ACCESS MODES
  2012. *
  2013. * TPACPI_FAN_RD_ACPI_GFAN:
  2014. * ACPI GFAN method: returns fan level
  2015. *
  2016. * see TPACPI_FAN_WR_ACPI_SFAN
  2017. * EC 0x2f (HFSP) not available if GFAN exists
  2018. *
  2019. * TPACPI_FAN_WR_ACPI_SFAN:
  2020. * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
  2021. *
  2022. * EC 0x2f (HFSP) might be available *for reading*, but do not use
  2023. * it for writing.
  2024. *
  2025. * TPACPI_FAN_WR_TPEC:
  2026. * ThinkPad EC register 0x2f (HFSP): fan control loop mode
  2027. * Supported on almost all ThinkPads
  2028. *
  2029. * Fan speed changes of any sort (including those caused by the
  2030. * disengaged mode) are usually done slowly by the firmware as the
  2031. * maximum ammount of fan duty cycle change per second seems to be
  2032. * limited.
  2033. *
  2034. * Reading is not available if GFAN exists.
  2035. * Writing is not available if SFAN exists.
  2036. *
  2037. * Bits
  2038. * 7 automatic mode engaged;
  2039. * (default operation mode of the ThinkPad)
  2040. * fan level is ignored in this mode.
  2041. * 6 full speed mode (takes precedence over bit 7);
  2042. * not available on all thinkpads. May disable
  2043. * the tachometer while the fan controller ramps up
  2044. * the speed (which can take up to a few *minutes*).
  2045. * Speeds up fan to 100% duty-cycle, which is far above
  2046. * the standard RPM levels. It is not impossible that
  2047. * it could cause hardware damage.
  2048. * 5-3 unused in some models. Extra bits for fan level
  2049. * in others, but still useless as all values above
  2050. * 7 map to the same speed as level 7 in these models.
  2051. * 2-0 fan level (0..7 usually)
  2052. * 0x00 = stop
  2053. * 0x07 = max (set when temperatures critical)
  2054. * Some ThinkPads may have other levels, see
  2055. * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
  2056. *
  2057. * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
  2058. * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
  2059. * does so, its initial value is meaningless (0x07).
  2060. *
  2061. * For firmware bugs, refer to:
  2062. * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
  2063. *
  2064. * ----
  2065. *
  2066. * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
  2067. * Main fan tachometer reading (in RPM)
  2068. *
  2069. * This register is present on all ThinkPads with a new-style EC, and
  2070. * it is known not to be present on the A21m/e, and T22, as there is
  2071. * something else in offset 0x84 according to the ACPI DSDT. Other
  2072. * ThinkPads from this same time period (and earlier) probably lack the
  2073. * tachometer as well.
  2074. *
  2075. * Unfortunately a lot of ThinkPads with new-style ECs but whose firwmare
  2076. * was never fixed by IBM to report the EC firmware version string
  2077. * probably support the tachometer (like the early X models), so
  2078. * detecting it is quite hard. We need more data to know for sure.
  2079. *
  2080. * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
  2081. * might result.
  2082. *
  2083. * FIRMWARE BUG: may go stale while the EC is switching to full speed
  2084. * mode.
  2085. *
  2086. * For firmware bugs, refer to:
  2087. * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
  2088. *
  2089. * TPACPI_FAN_WR_ACPI_FANS:
  2090. * ThinkPad X31, X40, X41. Not available in the X60.
  2091. *
  2092. * FANS ACPI handle: takes three arguments: low speed, medium speed,
  2093. * high speed. ACPI DSDT seems to map these three speeds to levels
  2094. * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
  2095. * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
  2096. *
  2097. * The speeds are stored on handles
  2098. * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
  2099. *
  2100. * There are three default speed sets, acessible as handles:
  2101. * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
  2102. *
  2103. * ACPI DSDT switches which set is in use depending on various
  2104. * factors.
  2105. *
  2106. * TPACPI_FAN_WR_TPEC is also available and should be used to
  2107. * command the fan. The X31/X40/X41 seems to have 8 fan levels,
  2108. * but the ACPI tables just mention level 7.
  2109. */
  2110. static enum fan_status_access_mode fan_status_access_mode;
  2111. static enum fan_control_access_mode fan_control_access_mode;
  2112. static enum fan_control_commands fan_control_commands;
  2113. static u8 fan_control_initial_status;
  2114. static void fan_watchdog_fire(struct work_struct *ignored);
  2115. static int fan_watchdog_maxinterval;
  2116. static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
  2117. IBM_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
  2118. IBM_HANDLE(gfan, ec, "GFAN", /* 570 */
  2119. "\\FSPD", /* 600e/x, 770e, 770x */
  2120. ); /* all others */
  2121. IBM_HANDLE(sfan, ec, "SFAN", /* 570 */
  2122. "JFNS", /* 770x-JL */
  2123. ); /* all others */
  2124. static int __init fan_init(struct ibm_init_struct *iibm)
  2125. {
  2126. vdbg_printk(TPACPI_DBG_INIT, "initializing fan subdriver\n");
  2127. fan_status_access_mode = TPACPI_FAN_NONE;
  2128. fan_control_access_mode = TPACPI_FAN_WR_NONE;
  2129. fan_control_commands = 0;
  2130. fan_watchdog_maxinterval = 0;
  2131. tp_features.fan_ctrl_status_undef = 0;
  2132. IBM_ACPIHANDLE_INIT(fans);
  2133. IBM_ACPIHANDLE_INIT(gfan);
  2134. IBM_ACPIHANDLE_INIT(sfan);
  2135. if (gfan_handle) {
  2136. /* 570, 600e/x, 770e, 770x */
  2137. fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
  2138. } else {
  2139. /* all other ThinkPads: note that even old-style
  2140. * ThinkPad ECs supports the fan control register */
  2141. if (likely(acpi_ec_read(fan_status_offset,
  2142. &fan_control_initial_status))) {
  2143. fan_status_access_mode = TPACPI_FAN_RD_TPEC;
  2144. /* In some ThinkPads, neither the EC nor the ACPI
  2145. * DSDT initialize the fan status, and it ends up
  2146. * being set to 0x07 when it *could* be either
  2147. * 0x07 or 0x80.
  2148. *
  2149. * Enable for TP-1Y (T43), TP-78 (R51e),
  2150. * TP-76 (R52), TP-70 (T43, R52), which are known
  2151. * to be buggy. */
  2152. if (fan_control_initial_status == 0x07 &&
  2153. ibm_thinkpad_ec_found &&
  2154. ((ibm_thinkpad_ec_found[0] == '1' &&
  2155. ibm_thinkpad_ec_found[1] == 'Y') ||
  2156. (ibm_thinkpad_ec_found[0] == '7' &&
  2157. (ibm_thinkpad_ec_found[1] == '6' ||
  2158. ibm_thinkpad_ec_found[1] == '8' ||
  2159. ibm_thinkpad_ec_found[1] == '0'))
  2160. )) {
  2161. printk(IBM_NOTICE
  2162. "fan_init: initial fan status is "
  2163. "unknown, assuming it is in auto "
  2164. "mode\n");
  2165. tp_features.fan_ctrl_status_undef = 1;
  2166. }
  2167. } else {
  2168. printk(IBM_ERR
  2169. "ThinkPad ACPI EC access misbehaving, "
  2170. "fan status and control unavailable\n");
  2171. return 1;
  2172. }
  2173. }
  2174. if (sfan_handle) {
  2175. /* 570, 770x-JL */
  2176. fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
  2177. fan_control_commands |=
  2178. TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
  2179. } else {
  2180. if (!gfan_handle) {
  2181. /* gfan without sfan means no fan control */
  2182. /* all other models implement TP EC 0x2f control */
  2183. if (fans_handle) {
  2184. /* X31, X40, X41 */
  2185. fan_control_access_mode =
  2186. TPACPI_FAN_WR_ACPI_FANS;
  2187. fan_control_commands |=
  2188. TPACPI_FAN_CMD_SPEED |
  2189. TPACPI_FAN_CMD_LEVEL |
  2190. TPACPI_FAN_CMD_ENABLE;
  2191. } else {
  2192. fan_control_access_mode = TPACPI_FAN_WR_TPEC;
  2193. fan_control_commands |=
  2194. TPACPI_FAN_CMD_LEVEL |
  2195. TPACPI_FAN_CMD_ENABLE;
  2196. }
  2197. }
  2198. }
  2199. vdbg_printk(TPACPI_DBG_INIT, "fan is %s, modes %d, %d\n",
  2200. str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
  2201. fan_control_access_mode != TPACPI_FAN_WR_NONE),
  2202. fan_status_access_mode, fan_control_access_mode);
  2203. return (fan_status_access_mode != TPACPI_FAN_NONE ||
  2204. fan_control_access_mode != TPACPI_FAN_WR_NONE)?
  2205. 0 : 1;
  2206. }
  2207. static int fan_get_status(u8 *status)
  2208. {
  2209. u8 s;
  2210. /* TODO:
  2211. * Add TPACPI_FAN_RD_ACPI_FANS ? */
  2212. switch (fan_status_access_mode) {
  2213. case TPACPI_FAN_RD_ACPI_GFAN:
  2214. /* 570, 600e/x, 770e, 770x */
  2215. if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
  2216. return -EIO;
  2217. if (likely(status))
  2218. *status = s & 0x07;
  2219. break;
  2220. case TPACPI_FAN_RD_TPEC:
  2221. /* all except 570, 600e/x, 770e, 770x */
  2222. if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
  2223. return -EIO;
  2224. if (likely(status))
  2225. *status = s;
  2226. break;
  2227. default:
  2228. return -ENXIO;
  2229. }
  2230. return 0;
  2231. }
  2232. static void fan_exit(void)
  2233. {
  2234. vdbg_printk(TPACPI_DBG_EXIT, "cancelling any pending fan watchdog tasks\n");
  2235. cancel_delayed_work(&fan_watchdog_task);
  2236. flush_scheduled_work();
  2237. }
  2238. static int fan_get_speed(unsigned int *speed)
  2239. {
  2240. u8 hi, lo;
  2241. switch (fan_status_access_mode) {
  2242. case TPACPI_FAN_RD_TPEC:
  2243. /* all except 570, 600e/x, 770e, 770x */
  2244. if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
  2245. !acpi_ec_read(fan_rpm_offset + 1, &hi)))
  2246. return -EIO;
  2247. if (likely(speed))
  2248. *speed = (hi << 8) | lo;
  2249. break;
  2250. default:
  2251. return -ENXIO;
  2252. }
  2253. return 0;
  2254. }
  2255. static void fan_watchdog_fire(struct work_struct *ignored)
  2256. {
  2257. int rc;
  2258. printk(IBM_NOTICE "fan watchdog: enabling fan\n");
  2259. rc = fan_set_enable();
  2260. if (rc < 0) {
  2261. printk(IBM_ERR "fan watchdog: error %d while enabling fan, "
  2262. "will try again later...\n", -rc);
  2263. /* reschedule for later */
  2264. fan_watchdog_reset();
  2265. }
  2266. }
  2267. static void fan_watchdog_reset(void)
  2268. {
  2269. static int fan_watchdog_active = 0;
  2270. if (fan_watchdog_active)
  2271. cancel_delayed_work(&fan_watchdog_task);
  2272. if (fan_watchdog_maxinterval > 0) {
  2273. fan_watchdog_active = 1;
  2274. if (!schedule_delayed_work(&fan_watchdog_task,
  2275. msecs_to_jiffies(fan_watchdog_maxinterval
  2276. * 1000))) {
  2277. printk(IBM_ERR "failed to schedule the fan watchdog, "
  2278. "watchdog will not trigger\n");
  2279. }
  2280. } else
  2281. fan_watchdog_active = 0;
  2282. }
  2283. static int fan_set_level(int level)
  2284. {
  2285. switch (fan_control_access_mode) {
  2286. case TPACPI_FAN_WR_ACPI_SFAN:
  2287. if (level >= 0 && level <= 7) {
  2288. if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
  2289. return -EIO;
  2290. } else
  2291. return -EINVAL;
  2292. break;
  2293. case TPACPI_FAN_WR_ACPI_FANS:
  2294. case TPACPI_FAN_WR_TPEC:
  2295. if ((level != TP_EC_FAN_AUTO) &&
  2296. (level != TP_EC_FAN_FULLSPEED) &&
  2297. ((level < 0) || (level > 7)))
  2298. return -EINVAL;
  2299. if (!acpi_ec_write(fan_status_offset, level))
  2300. return -EIO;
  2301. else
  2302. tp_features.fan_ctrl_status_undef = 0;
  2303. break;
  2304. default:
  2305. return -ENXIO;
  2306. }
  2307. return 0;
  2308. }
  2309. static int fan_set_enable(void)
  2310. {
  2311. u8 s;
  2312. int rc;
  2313. switch (fan_control_access_mode) {
  2314. case TPACPI_FAN_WR_ACPI_FANS:
  2315. case TPACPI_FAN_WR_TPEC:
  2316. if ((rc = fan_get_status(&s)) < 0)
  2317. return rc;
  2318. /* Don't go out of emergency fan mode */
  2319. if (s != 7)
  2320. s = TP_EC_FAN_AUTO;
  2321. if (!acpi_ec_write(fan_status_offset, s))
  2322. return -EIO;
  2323. else
  2324. tp_features.fan_ctrl_status_undef = 0;
  2325. break;
  2326. case TPACPI_FAN_WR_ACPI_SFAN:
  2327. if ((rc = fan_get_status(&s)) < 0)
  2328. return rc;
  2329. s &= 0x07;
  2330. /* Set fan to at least level 4 */
  2331. if (s < 4)
  2332. s = 4;
  2333. if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
  2334. return -EIO;
  2335. break;
  2336. default:
  2337. return -ENXIO;
  2338. }
  2339. return 0;
  2340. }
  2341. static int fan_set_disable(void)
  2342. {
  2343. switch (fan_control_access_mode) {
  2344. case TPACPI_FAN_WR_ACPI_FANS:
  2345. case TPACPI_FAN_WR_TPEC:
  2346. if (!acpi_ec_write(fan_status_offset, 0x00))
  2347. return -EIO;
  2348. else
  2349. tp_features.fan_ctrl_status_undef = 0;
  2350. break;
  2351. case TPACPI_FAN_WR_ACPI_SFAN:
  2352. if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
  2353. return -EIO;
  2354. break;
  2355. default:
  2356. return -ENXIO;
  2357. }
  2358. return 0;
  2359. }
  2360. static int fan_set_speed(int speed)
  2361. {
  2362. switch (fan_control_access_mode) {
  2363. case TPACPI_FAN_WR_ACPI_FANS:
  2364. if (speed >= 0 && speed <= 65535) {
  2365. if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
  2366. speed, speed, speed))
  2367. return -EIO;
  2368. } else
  2369. return -EINVAL;
  2370. break;
  2371. default:
  2372. return -ENXIO;
  2373. }
  2374. return 0;
  2375. }
  2376. static int fan_read(char *p)
  2377. {
  2378. int len = 0;
  2379. int rc;
  2380. u8 status;
  2381. unsigned int speed = 0;
  2382. switch (fan_status_access_mode) {
  2383. case TPACPI_FAN_RD_ACPI_GFAN:
  2384. /* 570, 600e/x, 770e, 770x */
  2385. if ((rc = fan_get_status(&status)) < 0)
  2386. return rc;
  2387. len += sprintf(p + len, "status:\t\t%s\n"
  2388. "level:\t\t%d\n",
  2389. (status != 0) ? "enabled" : "disabled", status);
  2390. break;
  2391. case TPACPI_FAN_RD_TPEC:
  2392. /* all except 570, 600e/x, 770e, 770x */
  2393. if ((rc = fan_get_status(&status)) < 0)
  2394. return rc;
  2395. if (unlikely(tp_features.fan_ctrl_status_undef)) {
  2396. if (status != fan_control_initial_status)
  2397. tp_features.fan_ctrl_status_undef = 0;
  2398. else
  2399. /* Return most likely status. In fact, it
  2400. * might be the only possible status */
  2401. status = TP_EC_FAN_AUTO;
  2402. }
  2403. len += sprintf(p + len, "status:\t\t%s\n",
  2404. (status != 0) ? "enabled" : "disabled");
  2405. if ((rc = fan_get_speed(&speed)) < 0)
  2406. return rc;
  2407. len += sprintf(p + len, "speed:\t\t%d\n", speed);
  2408. if (status & TP_EC_FAN_FULLSPEED)
  2409. /* Disengaged mode takes precedence */
  2410. len += sprintf(p + len, "level:\t\tdisengaged\n");
  2411. else if (status & TP_EC_FAN_AUTO)
  2412. len += sprintf(p + len, "level:\t\tauto\n");
  2413. else
  2414. len += sprintf(p + len, "level:\t\t%d\n", status);
  2415. break;
  2416. case TPACPI_FAN_NONE:
  2417. default:
  2418. len += sprintf(p + len, "status:\t\tnot supported\n");
  2419. }
  2420. if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
  2421. len += sprintf(p + len, "commands:\tlevel <level>");
  2422. switch (fan_control_access_mode) {
  2423. case TPACPI_FAN_WR_ACPI_SFAN:
  2424. len += sprintf(p + len, " (<level> is 0-7)\n");
  2425. break;
  2426. default:
  2427. len += sprintf(p + len, " (<level> is 0-7, "
  2428. "auto, disengaged)\n");
  2429. break;
  2430. }
  2431. }
  2432. if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
  2433. len += sprintf(p + len, "commands:\tenable, disable\n"
  2434. "commands:\twatchdog <timeout> (<timeout> is 0 (off), "
  2435. "1-120 (seconds))\n");
  2436. if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
  2437. len += sprintf(p + len, "commands:\tspeed <speed>"
  2438. " (<speed> is 0-65535)\n");
  2439. return len;
  2440. }
  2441. static int fan_write_cmd_level(const char *cmd, int *rc)
  2442. {
  2443. int level;
  2444. if (strlencmp(cmd, "level auto") == 0)
  2445. level = TP_EC_FAN_AUTO;
  2446. else if (strlencmp(cmd, "level disengaged") == 0)
  2447. level = TP_EC_FAN_FULLSPEED;
  2448. else if (sscanf(cmd, "level %d", &level) != 1)
  2449. return 0;
  2450. if ((*rc = fan_set_level(level)) == -ENXIO)
  2451. printk(IBM_ERR "level command accepted for unsupported "
  2452. "access mode %d", fan_control_access_mode);
  2453. return 1;
  2454. }
  2455. static int fan_write_cmd_enable(const char *cmd, int *rc)
  2456. {
  2457. if (strlencmp(cmd, "enable") != 0)
  2458. return 0;
  2459. if ((*rc = fan_set_enable()) == -ENXIO)
  2460. printk(IBM_ERR "enable command accepted for unsupported "
  2461. "access mode %d", fan_control_access_mode);
  2462. return 1;
  2463. }
  2464. static int fan_write_cmd_disable(const char *cmd, int *rc)
  2465. {
  2466. if (strlencmp(cmd, "disable") != 0)
  2467. return 0;
  2468. if ((*rc = fan_set_disable()) == -ENXIO)
  2469. printk(IBM_ERR "disable command accepted for unsupported "
  2470. "access mode %d", fan_control_access_mode);
  2471. return 1;
  2472. }
  2473. static int fan_write_cmd_speed(const char *cmd, int *rc)
  2474. {
  2475. int speed;
  2476. /* TODO:
  2477. * Support speed <low> <medium> <high> ? */
  2478. if (sscanf(cmd, "speed %d", &speed) != 1)
  2479. return 0;
  2480. if ((*rc = fan_set_speed(speed)) == -ENXIO)
  2481. printk(IBM_ERR "speed command accepted for unsupported "
  2482. "access mode %d", fan_control_access_mode);
  2483. return 1;
  2484. }
  2485. static int fan_write_cmd_watchdog(const char *cmd, int *rc)
  2486. {
  2487. int interval;
  2488. if (sscanf(cmd, "watchdog %d", &interval) != 1)
  2489. return 0;
  2490. if (interval < 0 || interval > 120)
  2491. *rc = -EINVAL;
  2492. else
  2493. fan_watchdog_maxinterval = interval;
  2494. return 1;
  2495. }
  2496. static int fan_write(char *buf)
  2497. {
  2498. char *cmd;
  2499. int rc = 0;
  2500. while (!rc && (cmd = next_cmd(&buf))) {
  2501. if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
  2502. fan_write_cmd_level(cmd, &rc)) &&
  2503. !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
  2504. (fan_write_cmd_enable(cmd, &rc) ||
  2505. fan_write_cmd_disable(cmd, &rc) ||
  2506. fan_write_cmd_watchdog(cmd, &rc))) &&
  2507. !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
  2508. fan_write_cmd_speed(cmd, &rc))
  2509. )
  2510. rc = -EINVAL;
  2511. else if (!rc)
  2512. fan_watchdog_reset();
  2513. }
  2514. return rc;
  2515. }
  2516. static struct ibm_struct fan_driver_data = {
  2517. .name = "fan",
  2518. .read = fan_read,
  2519. .write = fan_write,
  2520. .exit = fan_exit,
  2521. .flags.experimental = 1,
  2522. };
  2523. /****************************************************************************
  2524. ****************************************************************************
  2525. *
  2526. * Infrastructure
  2527. *
  2528. ****************************************************************************
  2529. ****************************************************************************/
  2530. /* /proc support */
  2531. static struct proc_dir_entry *proc_dir = NULL;
  2532. /* Subdriver registry */
  2533. static LIST_HEAD(tpacpi_all_drivers);
  2534. /*
  2535. * Module and infrastructure proble, init and exit handling
  2536. */
  2537. #ifdef CONFIG_THINKPAD_ACPI_DEBUG
  2538. static const char * __init str_supported(int is_supported)
  2539. {
  2540. static char text_unsupported[] __initdata = "not supported";
  2541. return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
  2542. }
  2543. #endif /* CONFIG_THINKPAD_ACPI_DEBUG */
  2544. static int __init ibm_init(struct ibm_init_struct *iibm)
  2545. {
  2546. int ret;
  2547. struct ibm_struct *ibm = iibm->data;
  2548. struct proc_dir_entry *entry;
  2549. BUG_ON(ibm == NULL);
  2550. INIT_LIST_HEAD(&ibm->all_drivers);
  2551. if (ibm->flags.experimental && !experimental)
  2552. return 0;
  2553. dbg_printk(TPACPI_DBG_INIT,
  2554. "probing for %s\n", ibm->name);
  2555. if (iibm->init) {
  2556. ret = iibm->init(iibm);
  2557. if (ret > 0)
  2558. return 0; /* probe failed */
  2559. if (ret)
  2560. return ret;
  2561. ibm->flags.init_called = 1;
  2562. }
  2563. if (ibm->acpi) {
  2564. if (ibm->acpi->hid) {
  2565. ret = register_tpacpi_subdriver(ibm);
  2566. if (ret)
  2567. goto err_out;
  2568. }
  2569. if (ibm->acpi->notify) {
  2570. ret = setup_acpi_notify(ibm);
  2571. if (ret == -ENODEV) {
  2572. printk(IBM_NOTICE "disabling subdriver %s\n",
  2573. ibm->name);
  2574. ret = 0;
  2575. goto err_out;
  2576. }
  2577. if (ret < 0)
  2578. goto err_out;
  2579. }
  2580. }
  2581. dbg_printk(TPACPI_DBG_INIT,
  2582. "%s installed\n", ibm->name);
  2583. if (ibm->read) {
  2584. entry = create_proc_entry(ibm->name,
  2585. S_IFREG | S_IRUGO | S_IWUSR,
  2586. proc_dir);
  2587. if (!entry) {
  2588. printk(IBM_ERR "unable to create proc entry %s\n",
  2589. ibm->name);
  2590. ret = -ENODEV;
  2591. goto err_out;
  2592. }
  2593. entry->owner = THIS_MODULE;
  2594. entry->data = ibm;
  2595. entry->read_proc = &dispatch_procfs_read;
  2596. if (ibm->write)
  2597. entry->write_proc = &dispatch_procfs_write;
  2598. ibm->flags.proc_created = 1;
  2599. }
  2600. list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
  2601. return 0;
  2602. err_out:
  2603. dbg_printk(TPACPI_DBG_INIT,
  2604. "%s: at error exit path with result %d\n",
  2605. ibm->name, ret);
  2606. ibm_exit(ibm);
  2607. return (ret < 0)? ret : 0;
  2608. }
  2609. static void ibm_exit(struct ibm_struct *ibm)
  2610. {
  2611. dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
  2612. list_del_init(&ibm->all_drivers);
  2613. if (ibm->flags.acpi_notify_installed) {
  2614. dbg_printk(TPACPI_DBG_EXIT,
  2615. "%s: acpi_remove_notify_handler\n", ibm->name);
  2616. BUG_ON(!ibm->acpi);
  2617. acpi_remove_notify_handler(*ibm->acpi->handle,
  2618. ibm->acpi->type,
  2619. dispatch_acpi_notify);
  2620. ibm->flags.acpi_notify_installed = 0;
  2621. ibm->flags.acpi_notify_installed = 0;
  2622. }
  2623. if (ibm->flags.proc_created) {
  2624. dbg_printk(TPACPI_DBG_EXIT,
  2625. "%s: remove_proc_entry\n", ibm->name);
  2626. remove_proc_entry(ibm->name, proc_dir);
  2627. ibm->flags.proc_created = 0;
  2628. }
  2629. if (ibm->flags.acpi_driver_registered) {
  2630. dbg_printk(TPACPI_DBG_EXIT,
  2631. "%s: acpi_bus_unregister_driver\n", ibm->name);
  2632. BUG_ON(!ibm->acpi);
  2633. acpi_bus_unregister_driver(ibm->acpi->driver);
  2634. kfree(ibm->acpi->driver);
  2635. ibm->acpi->driver = NULL;
  2636. ibm->flags.acpi_driver_registered = 0;
  2637. }
  2638. if (ibm->flags.init_called && ibm->exit) {
  2639. ibm->exit();
  2640. ibm->flags.init_called = 0;
  2641. }
  2642. dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
  2643. }
  2644. /* Probing */
  2645. static char *ibm_thinkpad_ec_found = NULL;
  2646. static char* __init check_dmi_for_ec(void)
  2647. {
  2648. struct dmi_device *dev = NULL;
  2649. char ec_fw_string[18];
  2650. /*
  2651. * ThinkPad T23 or newer, A31 or newer, R50e or newer,
  2652. * X32 or newer, all Z series; Some models must have an
  2653. * up-to-date BIOS or they will not be detected.
  2654. *
  2655. * See http://thinkwiki.org/wiki/List_of_DMI_IDs
  2656. */
  2657. while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
  2658. if (sscanf(dev->name,
  2659. "IBM ThinkPad Embedded Controller -[%17c",
  2660. ec_fw_string) == 1) {
  2661. ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
  2662. ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
  2663. return kstrdup(ec_fw_string, GFP_KERNEL);
  2664. }
  2665. }
  2666. return NULL;
  2667. }
  2668. static int __init probe_for_thinkpad(void)
  2669. {
  2670. int is_thinkpad;
  2671. if (acpi_disabled)
  2672. return -ENODEV;
  2673. /*
  2674. * Non-ancient models have better DMI tagging, but very old models
  2675. * don't.
  2676. */
  2677. is_thinkpad = dmi_name_in_vendors("ThinkPad");
  2678. /* ec is required because many other handles are relative to it */
  2679. IBM_ACPIHANDLE_INIT(ec);
  2680. if (!ec_handle) {
  2681. if (is_thinkpad)
  2682. printk(IBM_ERR
  2683. "Not yet supported ThinkPad detected!\n");
  2684. return -ENODEV;
  2685. }
  2686. /*
  2687. * Risks a regression on very old machines, but reduces potential
  2688. * false positives a damn great deal
  2689. */
  2690. if (!is_thinkpad)
  2691. is_thinkpad = dmi_name_in_vendors("IBM");
  2692. if (!is_thinkpad && !force_load)
  2693. return -ENODEV;
  2694. return 0;
  2695. }
  2696. /* Module init, exit, parameters */
  2697. static struct ibm_init_struct ibms_init[] __initdata = {
  2698. {
  2699. .init = thinkpad_acpi_driver_init,
  2700. .data = &thinkpad_acpi_driver_data,
  2701. },
  2702. {
  2703. .init = hotkey_init,
  2704. .data = &hotkey_driver_data,
  2705. },
  2706. {
  2707. .init = bluetooth_init,
  2708. .data = &bluetooth_driver_data,
  2709. },
  2710. {
  2711. .init = wan_init,
  2712. .data = &wan_driver_data,
  2713. },
  2714. {
  2715. .init = video_init,
  2716. .data = &video_driver_data,
  2717. },
  2718. {
  2719. .init = light_init,
  2720. .data = &light_driver_data,
  2721. },
  2722. #ifdef CONFIG_THINKPAD_ACPI_DOCK
  2723. {
  2724. .init = dock_init,
  2725. .data = &dock_driver_data[0],
  2726. },
  2727. {
  2728. .data = &dock_driver_data[1],
  2729. },
  2730. #endif
  2731. #ifdef CONFIG_THINKPAD_ACPI_BAY
  2732. {
  2733. .init = bay_init,
  2734. .data = &bay_driver_data,
  2735. },
  2736. #endif
  2737. {
  2738. .init = cmos_init,
  2739. .data = &cmos_driver_data,
  2740. },
  2741. {
  2742. .init = led_init,
  2743. .data = &led_driver_data,
  2744. },
  2745. {
  2746. .init = beep_init,
  2747. .data = &beep_driver_data,
  2748. },
  2749. {
  2750. .init = thermal_init,
  2751. .data = &thermal_driver_data,
  2752. },
  2753. {
  2754. .data = &ecdump_driver_data,
  2755. },
  2756. {
  2757. .init = brightness_init,
  2758. .data = &brightness_driver_data,
  2759. },
  2760. {
  2761. .data = &volume_driver_data,
  2762. },
  2763. {
  2764. .init = fan_init,
  2765. .data = &fan_driver_data,
  2766. },
  2767. };
  2768. static int __init set_ibm_param(const char *val, struct kernel_param *kp)
  2769. {
  2770. unsigned int i;
  2771. struct ibm_struct *ibm;
  2772. for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
  2773. ibm = ibms_init[i].data;
  2774. BUG_ON(ibm == NULL);
  2775. if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
  2776. if (strlen(val) > sizeof(ibms_init[i].param) - 2)
  2777. return -ENOSPC;
  2778. strcpy(ibms_init[i].param, val);
  2779. strcat(ibms_init[i].param, ",");
  2780. return 0;
  2781. }
  2782. }
  2783. return -EINVAL;
  2784. }
  2785. static int experimental;
  2786. module_param(experimental, int, 0);
  2787. static u32 dbg_level;
  2788. module_param_named(debug, dbg_level, uint, 0);
  2789. static int force_load;
  2790. module_param(force_load, int, 0);
  2791. #define IBM_PARAM(feature) \
  2792. module_param_call(feature, set_ibm_param, NULL, NULL, 0)
  2793. IBM_PARAM(hotkey);
  2794. IBM_PARAM(bluetooth);
  2795. IBM_PARAM(video);
  2796. IBM_PARAM(light);
  2797. #ifdef CONFIG_THINKPAD_ACPI_DOCK
  2798. IBM_PARAM(dock);
  2799. #endif
  2800. #ifdef CONFIG_THINKPAD_ACPI_BAY
  2801. IBM_PARAM(bay);
  2802. #endif /* CONFIG_THINKPAD_ACPI_BAY */
  2803. IBM_PARAM(cmos);
  2804. IBM_PARAM(led);
  2805. IBM_PARAM(beep);
  2806. IBM_PARAM(ecdump);
  2807. IBM_PARAM(brightness);
  2808. IBM_PARAM(volume);
  2809. IBM_PARAM(fan);
  2810. static int __init thinkpad_acpi_module_init(void)
  2811. {
  2812. int ret, i;
  2813. /* Driver-level probe */
  2814. ret = probe_for_thinkpad();
  2815. if (ret)
  2816. return ret;
  2817. /* Driver initialization */
  2818. ibm_thinkpad_ec_found = check_dmi_for_ec();
  2819. IBM_ACPIHANDLE_INIT(ecrd);
  2820. IBM_ACPIHANDLE_INIT(ecwr);
  2821. proc_dir = proc_mkdir(IBM_PROC_DIR, acpi_root_dir);
  2822. if (!proc_dir) {
  2823. printk(IBM_ERR "unable to create proc dir " IBM_PROC_DIR);
  2824. thinkpad_acpi_module_exit();
  2825. return -ENODEV;
  2826. }
  2827. proc_dir->owner = THIS_MODULE;
  2828. ret = platform_driver_register(&tpacpi_pdriver);
  2829. if (ret) {
  2830. printk(IBM_ERR "unable to register platform driver\n");
  2831. thinkpad_acpi_module_exit();
  2832. return ret;
  2833. }
  2834. ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
  2835. if (ret) {
  2836. printk(IBM_ERR "unable to create sysfs driver attributes\n");
  2837. thinkpad_acpi_module_exit();
  2838. return ret;
  2839. }
  2840. /* Device initialization */
  2841. tpacpi_pdev = platform_device_register_simple(IBM_DRVR_NAME, -1,
  2842. NULL, 0);
  2843. if (IS_ERR(tpacpi_pdev)) {
  2844. ret = PTR_ERR(tpacpi_pdev);
  2845. tpacpi_pdev = NULL;
  2846. printk(IBM_ERR "unable to register platform device\n");
  2847. thinkpad_acpi_module_exit();
  2848. return ret;
  2849. }
  2850. tpacpi_hwmon = hwmon_device_register(&tpacpi_pdev->dev);
  2851. if (IS_ERR(tpacpi_hwmon)) {
  2852. ret = PTR_ERR(tpacpi_hwmon);
  2853. tpacpi_hwmon = NULL;
  2854. printk(IBM_ERR "unable to register hwmon device\n");
  2855. thinkpad_acpi_module_exit();
  2856. return ret;
  2857. }
  2858. for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
  2859. ret = ibm_init(&ibms_init[i]);
  2860. if (ret >= 0 && *ibms_init[i].param)
  2861. ret = ibms_init[i].data->write(ibms_init[i].param);
  2862. if (ret < 0) {
  2863. thinkpad_acpi_module_exit();
  2864. return ret;
  2865. }
  2866. }
  2867. return 0;
  2868. }
  2869. static void thinkpad_acpi_module_exit(void)
  2870. {
  2871. struct ibm_struct *ibm, *itmp;
  2872. list_for_each_entry_safe_reverse(ibm, itmp,
  2873. &tpacpi_all_drivers,
  2874. all_drivers) {
  2875. ibm_exit(ibm);
  2876. }
  2877. dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
  2878. if (tpacpi_hwmon)
  2879. hwmon_device_unregister(tpacpi_hwmon);
  2880. if (tpacpi_pdev)
  2881. platform_device_unregister(tpacpi_pdev);
  2882. tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
  2883. platform_driver_unregister(&tpacpi_pdriver);
  2884. if (proc_dir)
  2885. remove_proc_entry(IBM_PROC_DIR, acpi_root_dir);
  2886. kfree(ibm_thinkpad_ec_found);
  2887. }
  2888. module_init(thinkpad_acpi_module_init);
  2889. module_exit(thinkpad_acpi_module_exit);