thinkpad_acpi.c 91 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964
  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. mutex_init(&hotkey_mutex);
  572. /* hotkey not supported on 570 */
  573. tp_features.hotkey = hkey_handle != NULL;
  574. vdbg_printk(TPACPI_DBG_INIT, "hotkeys are %s\n",
  575. str_supported(tp_features.hotkey));
  576. if (tp_features.hotkey) {
  577. /* mask not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
  578. A30, R30, R31, T20-22, X20-21, X22-24 */
  579. tp_features.hotkey_mask =
  580. acpi_evalf(hkey_handle, NULL, "DHKN", "qv");
  581. vdbg_printk(TPACPI_DBG_INIT, "hotkey masks are %s\n",
  582. str_supported(tp_features.hotkey_mask));
  583. res = hotkey_get(&hotkey_orig_status, &hotkey_orig_mask);
  584. if (res)
  585. return res;
  586. }
  587. return (tp_features.hotkey)? 0 : 1;
  588. }
  589. static void hotkey_exit(void)
  590. {
  591. int res;
  592. if (tp_features.hotkey) {
  593. dbg_printk(TPACPI_DBG_EXIT, "restoring original hotkey mask\n");
  594. res = hotkey_set(hotkey_orig_status, hotkey_orig_mask);
  595. if (res)
  596. printk(IBM_ERR "failed to restore hotkey to BIOS defaults\n");
  597. }
  598. }
  599. static void hotkey_notify(struct ibm_struct *ibm, u32 event)
  600. {
  601. int hkey;
  602. if (acpi_evalf(hkey_handle, &hkey, "MHKP", "d"))
  603. acpi_bus_generate_event(ibm->acpi->device, event, hkey);
  604. else {
  605. printk(IBM_ERR "unknown hotkey event %d\n", event);
  606. acpi_bus_generate_event(ibm->acpi->device, event, 0);
  607. }
  608. }
  609. /*
  610. * Call with hotkey_mutex held
  611. */
  612. static int hotkey_get(int *status, int *mask)
  613. {
  614. if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
  615. return -EIO;
  616. if (tp_features.hotkey_mask)
  617. if (!acpi_evalf(hkey_handle, mask, "DHKN", "d"))
  618. return -EIO;
  619. return 0;
  620. }
  621. /*
  622. * Call with hotkey_mutex held
  623. */
  624. static int hotkey_set(int status, int mask)
  625. {
  626. int i;
  627. if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", status))
  628. return -EIO;
  629. if (tp_features.hotkey_mask)
  630. for (i = 0; i < 32; i++) {
  631. int bit = ((1 << i) & mask) != 0;
  632. if (!acpi_evalf(hkey_handle,
  633. NULL, "MHKM", "vdd", i + 1, bit))
  634. return -EIO;
  635. }
  636. return 0;
  637. }
  638. static int hotkey_read(char *p)
  639. {
  640. int res, status, mask;
  641. int len = 0;
  642. if (!tp_features.hotkey) {
  643. len += sprintf(p + len, "status:\t\tnot supported\n");
  644. return len;
  645. }
  646. res = mutex_lock_interruptible(&hotkey_mutex);
  647. if (res < 0)
  648. return res;
  649. res = hotkey_get(&status, &mask);
  650. mutex_unlock(&hotkey_mutex);
  651. if (res)
  652. return res;
  653. len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 0));
  654. if (tp_features.hotkey_mask) {
  655. len += sprintf(p + len, "mask:\t\t0x%04x\n", mask);
  656. len += sprintf(p + len,
  657. "commands:\tenable, disable, reset, <mask>\n");
  658. } else {
  659. len += sprintf(p + len, "mask:\t\tnot supported\n");
  660. len += sprintf(p + len, "commands:\tenable, disable, reset\n");
  661. }
  662. return len;
  663. }
  664. static int hotkey_write(char *buf)
  665. {
  666. int res, status, mask;
  667. char *cmd;
  668. int do_cmd = 0;
  669. if (!tp_features.hotkey)
  670. return -ENODEV;
  671. res = mutex_lock_interruptible(&hotkey_mutex);
  672. if (res < 0)
  673. return res;
  674. res = hotkey_get(&status, &mask);
  675. if (res)
  676. goto errexit;
  677. res = 0;
  678. while ((cmd = next_cmd(&buf))) {
  679. if (strlencmp(cmd, "enable") == 0) {
  680. status = 1;
  681. } else if (strlencmp(cmd, "disable") == 0) {
  682. status = 0;
  683. } else if (strlencmp(cmd, "reset") == 0) {
  684. status = hotkey_orig_status;
  685. mask = hotkey_orig_mask;
  686. } else if (sscanf(cmd, "0x%x", &mask) == 1) {
  687. /* mask set */
  688. } else if (sscanf(cmd, "%x", &mask) == 1) {
  689. /* mask set */
  690. } else {
  691. res = -EINVAL;
  692. goto errexit;
  693. }
  694. do_cmd = 1;
  695. }
  696. if (do_cmd)
  697. res = hotkey_set(status, mask);
  698. errexit:
  699. mutex_unlock(&hotkey_mutex);
  700. return res;
  701. }
  702. static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
  703. .hid = IBM_HKEY_HID,
  704. .notify = hotkey_notify,
  705. .handle = &hkey_handle,
  706. .type = ACPI_DEVICE_NOTIFY,
  707. };
  708. static struct ibm_struct hotkey_driver_data = {
  709. .name = "hotkey",
  710. .read = hotkey_read,
  711. .write = hotkey_write,
  712. .exit = hotkey_exit,
  713. .acpi = &ibm_hotkey_acpidriver,
  714. };
  715. /*************************************************************************
  716. * Bluetooth subdriver
  717. */
  718. static int __init bluetooth_init(struct ibm_init_struct *iibm)
  719. {
  720. int status = 0;
  721. vdbg_printk(TPACPI_DBG_INIT, "initializing bluetooth subdriver\n");
  722. IBM_ACPIHANDLE_INIT(hkey);
  723. /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
  724. G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
  725. tp_features.bluetooth = hkey_handle &&
  726. acpi_evalf(hkey_handle, &status, "GBDC", "qd");
  727. vdbg_printk(TPACPI_DBG_INIT, "bluetooth is %s, status 0x%02x\n",
  728. str_supported(tp_features.bluetooth),
  729. status);
  730. if (tp_features.bluetooth &&
  731. !(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
  732. /* no bluetooth hardware present in system */
  733. tp_features.bluetooth = 0;
  734. dbg_printk(TPACPI_DBG_INIT,
  735. "bluetooth hardware not installed\n");
  736. }
  737. return (tp_features.bluetooth)? 0 : 1;
  738. }
  739. static int bluetooth_get_radiosw(void)
  740. {
  741. int status;
  742. if (!tp_features.bluetooth)
  743. return -ENODEV;
  744. if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
  745. return -EIO;
  746. return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0);
  747. }
  748. static int bluetooth_set_radiosw(int radio_on)
  749. {
  750. int status;
  751. if (!tp_features.bluetooth)
  752. return -ENODEV;
  753. if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
  754. return -EIO;
  755. if (radio_on)
  756. status |= TP_ACPI_BLUETOOTH_RADIOSSW;
  757. else
  758. status &= ~TP_ACPI_BLUETOOTH_RADIOSSW;
  759. if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
  760. return -EIO;
  761. return 0;
  762. }
  763. static int bluetooth_read(char *p)
  764. {
  765. int len = 0;
  766. int status = bluetooth_get_radiosw();
  767. if (!tp_features.bluetooth)
  768. len += sprintf(p + len, "status:\t\tnot supported\n");
  769. else {
  770. len += sprintf(p + len, "status:\t\t%s\n",
  771. (status)? "enabled" : "disabled");
  772. len += sprintf(p + len, "commands:\tenable, disable\n");
  773. }
  774. return len;
  775. }
  776. static int bluetooth_write(char *buf)
  777. {
  778. char *cmd;
  779. if (!tp_features.bluetooth)
  780. return -ENODEV;
  781. while ((cmd = next_cmd(&buf))) {
  782. if (strlencmp(cmd, "enable") == 0) {
  783. bluetooth_set_radiosw(1);
  784. } else if (strlencmp(cmd, "disable") == 0) {
  785. bluetooth_set_radiosw(0);
  786. } else
  787. return -EINVAL;
  788. }
  789. return 0;
  790. }
  791. static struct ibm_struct bluetooth_driver_data = {
  792. .name = "bluetooth",
  793. .read = bluetooth_read,
  794. .write = bluetooth_write,
  795. };
  796. /*************************************************************************
  797. * Wan subdriver
  798. */
  799. static int __init wan_init(struct ibm_init_struct *iibm)
  800. {
  801. int status = 0;
  802. vdbg_printk(TPACPI_DBG_INIT, "initializing wan subdriver\n");
  803. IBM_ACPIHANDLE_INIT(hkey);
  804. tp_features.wan = hkey_handle &&
  805. acpi_evalf(hkey_handle, &status, "GWAN", "qd");
  806. vdbg_printk(TPACPI_DBG_INIT, "wan is %s, status 0x%02x\n",
  807. str_supported(tp_features.wan),
  808. status);
  809. if (tp_features.wan &&
  810. !(status & TP_ACPI_WANCARD_HWPRESENT)) {
  811. /* no wan hardware present in system */
  812. tp_features.wan = 0;
  813. dbg_printk(TPACPI_DBG_INIT,
  814. "wan hardware not installed\n");
  815. }
  816. return (tp_features.wan)? 0 : 1;
  817. }
  818. static int wan_get_radiosw(void)
  819. {
  820. int status;
  821. if (!tp_features.wan)
  822. return -ENODEV;
  823. if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
  824. return -EIO;
  825. return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0);
  826. }
  827. static int wan_set_radiosw(int radio_on)
  828. {
  829. int status;
  830. if (!tp_features.wan)
  831. return -ENODEV;
  832. if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
  833. return -EIO;
  834. if (radio_on)
  835. status |= TP_ACPI_WANCARD_RADIOSSW;
  836. else
  837. status &= ~TP_ACPI_WANCARD_RADIOSSW;
  838. if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
  839. return -EIO;
  840. return 0;
  841. }
  842. static int wan_read(char *p)
  843. {
  844. int len = 0;
  845. int status = wan_get_radiosw();
  846. if (!tp_features.wan)
  847. len += sprintf(p + len, "status:\t\tnot supported\n");
  848. else {
  849. len += sprintf(p + len, "status:\t\t%s\n",
  850. (status)? "enabled" : "disabled");
  851. len += sprintf(p + len, "commands:\tenable, disable\n");
  852. }
  853. return len;
  854. }
  855. static int wan_write(char *buf)
  856. {
  857. char *cmd;
  858. if (!tp_features.wan)
  859. return -ENODEV;
  860. while ((cmd = next_cmd(&buf))) {
  861. if (strlencmp(cmd, "enable") == 0) {
  862. wan_set_radiosw(1);
  863. } else if (strlencmp(cmd, "disable") == 0) {
  864. wan_set_radiosw(0);
  865. } else
  866. return -EINVAL;
  867. }
  868. return 0;
  869. }
  870. static struct ibm_struct wan_driver_data = {
  871. .name = "wan",
  872. .read = wan_read,
  873. .write = wan_write,
  874. .flags.experimental = 1,
  875. };
  876. /*************************************************************************
  877. * Video subdriver
  878. */
  879. static enum video_access_mode video_supported;
  880. static int video_orig_autosw;
  881. IBM_HANDLE(vid, root, "\\_SB.PCI.AGP.VGA", /* 570 */
  882. "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
  883. "\\_SB.PCI0.VID0", /* 770e */
  884. "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
  885. "\\_SB.PCI0.AGP.VID", /* all others */
  886. ); /* R30, R31 */
  887. IBM_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
  888. static int __init video_init(struct ibm_init_struct *iibm)
  889. {
  890. int ivga;
  891. vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
  892. IBM_ACPIHANDLE_INIT(vid);
  893. IBM_ACPIHANDLE_INIT(vid2);
  894. if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
  895. /* G41, assume IVGA doesn't change */
  896. vid_handle = vid2_handle;
  897. if (!vid_handle)
  898. /* video switching not supported on R30, R31 */
  899. video_supported = TPACPI_VIDEO_NONE;
  900. else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
  901. /* 570 */
  902. video_supported = TPACPI_VIDEO_570;
  903. else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
  904. /* 600e/x, 770e, 770x */
  905. video_supported = TPACPI_VIDEO_770;
  906. else
  907. /* all others */
  908. video_supported = TPACPI_VIDEO_NEW;
  909. vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
  910. str_supported(video_supported != TPACPI_VIDEO_NONE),
  911. video_supported);
  912. return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
  913. }
  914. static void video_exit(void)
  915. {
  916. dbg_printk(TPACPI_DBG_EXIT,
  917. "restoring original video autoswitch mode\n");
  918. if (video_autosw_set(video_orig_autosw))
  919. printk(IBM_ERR "error while trying to restore original "
  920. "video autoswitch mode\n");
  921. }
  922. static int video_outputsw_get(void)
  923. {
  924. int status = 0;
  925. int i;
  926. switch (video_supported) {
  927. case TPACPI_VIDEO_570:
  928. if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
  929. TP_ACPI_VIDEO_570_PHSCMD))
  930. return -EIO;
  931. status = i & TP_ACPI_VIDEO_570_PHSMASK;
  932. break;
  933. case TPACPI_VIDEO_770:
  934. if (!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, "\\VCDC", "d"))
  939. return -EIO;
  940. if (i)
  941. status |= TP_ACPI_VIDEO_S_CRT;
  942. break;
  943. case TPACPI_VIDEO_NEW:
  944. if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
  945. !acpi_evalf(NULL, &i, "\\VCDC", "d"))
  946. return -EIO;
  947. if (i)
  948. status |= TP_ACPI_VIDEO_S_CRT;
  949. if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
  950. !acpi_evalf(NULL, &i, "\\VCDL", "d"))
  951. return -EIO;
  952. if (i)
  953. status |= TP_ACPI_VIDEO_S_LCD;
  954. if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
  955. return -EIO;
  956. if (i)
  957. status |= TP_ACPI_VIDEO_S_DVI;
  958. break;
  959. default:
  960. return -ENOSYS;
  961. }
  962. return status;
  963. }
  964. static int video_outputsw_set(int status)
  965. {
  966. int autosw;
  967. int res = 0;
  968. switch (video_supported) {
  969. case TPACPI_VIDEO_570:
  970. res = acpi_evalf(NULL, NULL,
  971. "\\_SB.PHS2", "vdd",
  972. TP_ACPI_VIDEO_570_PHS2CMD,
  973. status | TP_ACPI_VIDEO_570_PHS2SET);
  974. break;
  975. case TPACPI_VIDEO_770:
  976. autosw = video_autosw_get();
  977. if (autosw < 0)
  978. return autosw;
  979. res = video_autosw_set(1);
  980. if (res)
  981. return res;
  982. res = acpi_evalf(vid_handle, NULL,
  983. "ASWT", "vdd", status * 0x100, 0);
  984. if (!autosw && video_autosw_set(autosw)) {
  985. printk(IBM_ERR "video auto-switch left enabled due to error\n");
  986. return -EIO;
  987. }
  988. break;
  989. case TPACPI_VIDEO_NEW:
  990. res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
  991. acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
  992. break;
  993. default:
  994. return -ENOSYS;
  995. }
  996. return (res)? 0 : -EIO;
  997. }
  998. static int video_autosw_get(void)
  999. {
  1000. int autosw = 0;
  1001. switch (video_supported) {
  1002. case TPACPI_VIDEO_570:
  1003. if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
  1004. return -EIO;
  1005. break;
  1006. case TPACPI_VIDEO_770:
  1007. case TPACPI_VIDEO_NEW:
  1008. if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
  1009. return -EIO;
  1010. break;
  1011. default:
  1012. return -ENOSYS;
  1013. }
  1014. return autosw & 1;
  1015. }
  1016. static int video_autosw_set(int enable)
  1017. {
  1018. if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
  1019. return -EIO;
  1020. return 0;
  1021. }
  1022. static int video_outputsw_cycle(void)
  1023. {
  1024. int autosw = video_autosw_get();
  1025. int res;
  1026. if (autosw < 0)
  1027. return autosw;
  1028. switch (video_supported) {
  1029. case TPACPI_VIDEO_570:
  1030. res = video_autosw_set(1);
  1031. if (res)
  1032. return res;
  1033. res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
  1034. break;
  1035. case TPACPI_VIDEO_770:
  1036. case TPACPI_VIDEO_NEW:
  1037. res = video_autosw_set(1);
  1038. if (res)
  1039. return res;
  1040. res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
  1041. break;
  1042. default:
  1043. return -ENOSYS;
  1044. }
  1045. if (!autosw && video_autosw_set(autosw)) {
  1046. printk(IBM_ERR "video auto-switch left enabled due to error\n");
  1047. return -EIO;
  1048. }
  1049. return (res)? 0 : -EIO;
  1050. }
  1051. static int video_expand_toggle(void)
  1052. {
  1053. switch (video_supported) {
  1054. case TPACPI_VIDEO_570:
  1055. return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
  1056. 0 : -EIO;
  1057. case TPACPI_VIDEO_770:
  1058. return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
  1059. 0 : -EIO;
  1060. case TPACPI_VIDEO_NEW:
  1061. return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
  1062. 0 : -EIO;
  1063. default:
  1064. return -ENOSYS;
  1065. }
  1066. /* not reached */
  1067. }
  1068. static int video_read(char *p)
  1069. {
  1070. int status, autosw;
  1071. int len = 0;
  1072. if (video_supported == TPACPI_VIDEO_NONE) {
  1073. len += sprintf(p + len, "status:\t\tnot supported\n");
  1074. return len;
  1075. }
  1076. status = video_outputsw_get();
  1077. if (status < 0)
  1078. return status;
  1079. autosw = video_autosw_get();
  1080. if (autosw < 0)
  1081. return autosw;
  1082. len += sprintf(p + len, "status:\t\tsupported\n");
  1083. len += sprintf(p + len, "lcd:\t\t%s\n", enabled(status, 0));
  1084. len += sprintf(p + len, "crt:\t\t%s\n", enabled(status, 1));
  1085. if (video_supported == TPACPI_VIDEO_NEW)
  1086. len += sprintf(p + len, "dvi:\t\t%s\n", enabled(status, 3));
  1087. len += sprintf(p + len, "auto:\t\t%s\n", enabled(autosw, 0));
  1088. len += sprintf(p + len, "commands:\tlcd_enable, lcd_disable\n");
  1089. len += sprintf(p + len, "commands:\tcrt_enable, crt_disable\n");
  1090. if (video_supported == TPACPI_VIDEO_NEW)
  1091. len += sprintf(p + len, "commands:\tdvi_enable, dvi_disable\n");
  1092. len += sprintf(p + len, "commands:\tauto_enable, auto_disable\n");
  1093. len += sprintf(p + len, "commands:\tvideo_switch, expand_toggle\n");
  1094. return len;
  1095. }
  1096. static int video_write(char *buf)
  1097. {
  1098. char *cmd;
  1099. int enable, disable, status;
  1100. int res;
  1101. if (video_supported == TPACPI_VIDEO_NONE)
  1102. return -ENODEV;
  1103. enable = 0;
  1104. disable = 0;
  1105. while ((cmd = next_cmd(&buf))) {
  1106. if (strlencmp(cmd, "lcd_enable") == 0) {
  1107. enable |= TP_ACPI_VIDEO_S_LCD;
  1108. } else if (strlencmp(cmd, "lcd_disable") == 0) {
  1109. disable |= TP_ACPI_VIDEO_S_LCD;
  1110. } else if (strlencmp(cmd, "crt_enable") == 0) {
  1111. enable |= TP_ACPI_VIDEO_S_CRT;
  1112. } else if (strlencmp(cmd, "crt_disable") == 0) {
  1113. disable |= TP_ACPI_VIDEO_S_CRT;
  1114. } else if (video_supported == TPACPI_VIDEO_NEW &&
  1115. strlencmp(cmd, "dvi_enable") == 0) {
  1116. enable |= TP_ACPI_VIDEO_S_DVI;
  1117. } else if (video_supported == TPACPI_VIDEO_NEW &&
  1118. strlencmp(cmd, "dvi_disable") == 0) {
  1119. disable |= TP_ACPI_VIDEO_S_DVI;
  1120. } else if (strlencmp(cmd, "auto_enable") == 0) {
  1121. res = video_autosw_set(1);
  1122. if (res)
  1123. return res;
  1124. } else if (strlencmp(cmd, "auto_disable") == 0) {
  1125. res = video_autosw_set(0);
  1126. if (res)
  1127. return res;
  1128. } else if (strlencmp(cmd, "video_switch") == 0) {
  1129. res = video_outputsw_cycle();
  1130. if (res)
  1131. return res;
  1132. } else if (strlencmp(cmd, "expand_toggle") == 0) {
  1133. res = video_expand_toggle();
  1134. if (res)
  1135. return res;
  1136. } else
  1137. return -EINVAL;
  1138. }
  1139. if (enable || disable) {
  1140. status = video_outputsw_get();
  1141. if (status < 0)
  1142. return status;
  1143. res = video_outputsw_set((status & ~disable) | enable);
  1144. if (res)
  1145. return res;
  1146. }
  1147. return 0;
  1148. }
  1149. static struct ibm_struct video_driver_data = {
  1150. .name = "video",
  1151. .read = video_read,
  1152. .write = video_write,
  1153. .exit = video_exit,
  1154. };
  1155. /*************************************************************************
  1156. * Light (thinklight) subdriver
  1157. */
  1158. IBM_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
  1159. IBM_HANDLE(ledb, ec, "LEDB"); /* G4x */
  1160. static int __init light_init(struct ibm_init_struct *iibm)
  1161. {
  1162. vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
  1163. IBM_ACPIHANDLE_INIT(ledb);
  1164. IBM_ACPIHANDLE_INIT(lght);
  1165. IBM_ACPIHANDLE_INIT(cmos);
  1166. /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
  1167. tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
  1168. if (tp_features.light)
  1169. /* light status not supported on
  1170. 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
  1171. tp_features.light_status =
  1172. acpi_evalf(ec_handle, NULL, "KBLT", "qv");
  1173. vdbg_printk(TPACPI_DBG_INIT, "light is %s\n",
  1174. str_supported(tp_features.light));
  1175. return (tp_features.light)? 0 : 1;
  1176. }
  1177. static int light_read(char *p)
  1178. {
  1179. int len = 0;
  1180. int status = 0;
  1181. if (!tp_features.light) {
  1182. len += sprintf(p + len, "status:\t\tnot supported\n");
  1183. } else if (!tp_features.light_status) {
  1184. len += sprintf(p + len, "status:\t\tunknown\n");
  1185. len += sprintf(p + len, "commands:\ton, off\n");
  1186. } else {
  1187. if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
  1188. return -EIO;
  1189. len += sprintf(p + len, "status:\t\t%s\n", onoff(status, 0));
  1190. len += sprintf(p + len, "commands:\ton, off\n");
  1191. }
  1192. return len;
  1193. }
  1194. static int light_write(char *buf)
  1195. {
  1196. int cmos_cmd, lght_cmd;
  1197. char *cmd;
  1198. int success;
  1199. if (!tp_features.light)
  1200. return -ENODEV;
  1201. while ((cmd = next_cmd(&buf))) {
  1202. if (strlencmp(cmd, "on") == 0) {
  1203. cmos_cmd = 0x0c;
  1204. lght_cmd = 1;
  1205. } else if (strlencmp(cmd, "off") == 0) {
  1206. cmos_cmd = 0x0d;
  1207. lght_cmd = 0;
  1208. } else
  1209. return -EINVAL;
  1210. success = cmos_handle ?
  1211. acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd) :
  1212. acpi_evalf(lght_handle, NULL, NULL, "vd", lght_cmd);
  1213. if (!success)
  1214. return -EIO;
  1215. }
  1216. return 0;
  1217. }
  1218. static struct ibm_struct light_driver_data = {
  1219. .name = "light",
  1220. .read = light_read,
  1221. .write = light_write,
  1222. };
  1223. /*************************************************************************
  1224. * Dock subdriver
  1225. */
  1226. #ifdef CONFIG_THINKPAD_ACPI_DOCK
  1227. IBM_HANDLE(dock, root, "\\_SB.GDCK", /* X30, X31, X40 */
  1228. "\\_SB.PCI0.DOCK", /* 600e/x,770e,770x,A2xm/p,T20-22,X20-21 */
  1229. "\\_SB.PCI0.PCI1.DOCK", /* all others */
  1230. "\\_SB.PCI.ISA.SLCE", /* 570 */
  1231. ); /* A21e,G4x,R30,R31,R32,R40,R40e,R50e */
  1232. /* don't list other alternatives as we install a notify handler on the 570 */
  1233. IBM_HANDLE(pci, root, "\\_SB.PCI"); /* 570 */
  1234. #define dock_docked() (_sta(dock_handle) & 1)
  1235. static int __init dock_init(struct ibm_init_struct *iibm)
  1236. {
  1237. vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver\n");
  1238. IBM_ACPIHANDLE_INIT(dock);
  1239. IBM_ACPIHANDLE_INIT(pci);
  1240. vdbg_printk(TPACPI_DBG_INIT, "dock is %s\n",
  1241. str_supported(dock_handle != NULL));
  1242. return (dock_handle)? 0 : 1;
  1243. }
  1244. static void dock_notify(struct ibm_struct *ibm, u32 event)
  1245. {
  1246. int docked = dock_docked();
  1247. int pci = ibm->acpi->hid && strstr(ibm->acpi->hid, IBM_PCI_HID);
  1248. if (event == 1 && !pci) /* 570 */
  1249. acpi_bus_generate_event(ibm->acpi->device, event, 1); /* button */
  1250. else if (event == 1 && pci) /* 570 */
  1251. acpi_bus_generate_event(ibm->acpi->device, event, 3); /* dock */
  1252. else if (event == 3 && docked)
  1253. acpi_bus_generate_event(ibm->acpi->device, event, 1); /* button */
  1254. else if (event == 3 && !docked)
  1255. acpi_bus_generate_event(ibm->acpi->device, event, 2); /* undock */
  1256. else if (event == 0 && docked)
  1257. acpi_bus_generate_event(ibm->acpi->device, event, 3); /* dock */
  1258. else {
  1259. printk(IBM_ERR "unknown dock event %d, status %d\n",
  1260. event, _sta(dock_handle));
  1261. acpi_bus_generate_event(ibm->acpi->device, event, 0); /* unknown */
  1262. }
  1263. }
  1264. static int dock_read(char *p)
  1265. {
  1266. int len = 0;
  1267. int docked = dock_docked();
  1268. if (!dock_handle)
  1269. len += sprintf(p + len, "status:\t\tnot supported\n");
  1270. else if (!docked)
  1271. len += sprintf(p + len, "status:\t\tundocked\n");
  1272. else {
  1273. len += sprintf(p + len, "status:\t\tdocked\n");
  1274. len += sprintf(p + len, "commands:\tdock, undock\n");
  1275. }
  1276. return len;
  1277. }
  1278. static int dock_write(char *buf)
  1279. {
  1280. char *cmd;
  1281. if (!dock_docked())
  1282. return -ENODEV;
  1283. while ((cmd = next_cmd(&buf))) {
  1284. if (strlencmp(cmd, "undock") == 0) {
  1285. if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 0) ||
  1286. !acpi_evalf(dock_handle, NULL, "_EJ0", "vd", 1))
  1287. return -EIO;
  1288. } else if (strlencmp(cmd, "dock") == 0) {
  1289. if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 1))
  1290. return -EIO;
  1291. } else
  1292. return -EINVAL;
  1293. }
  1294. return 0;
  1295. }
  1296. static struct tp_acpi_drv_struct ibm_dock_acpidriver[2] = {
  1297. {
  1298. .notify = dock_notify,
  1299. .handle = &dock_handle,
  1300. .type = ACPI_SYSTEM_NOTIFY,
  1301. },
  1302. {
  1303. .hid = IBM_PCI_HID,
  1304. .notify = dock_notify,
  1305. .handle = &pci_handle,
  1306. .type = ACPI_SYSTEM_NOTIFY,
  1307. },
  1308. };
  1309. static struct ibm_struct dock_driver_data[2] = {
  1310. {
  1311. .name = "dock",
  1312. .read = dock_read,
  1313. .write = dock_write,
  1314. .acpi = &ibm_dock_acpidriver[0],
  1315. },
  1316. {
  1317. .name = "dock",
  1318. .acpi = &ibm_dock_acpidriver[1],
  1319. },
  1320. };
  1321. #endif /* CONFIG_THINKPAD_ACPI_DOCK */
  1322. /*************************************************************************
  1323. * Bay subdriver
  1324. */
  1325. #ifdef CONFIG_THINKPAD_ACPI_BAY
  1326. IBM_HANDLE(bay, root, "\\_SB.PCI.IDE.SECN.MAST", /* 570 */
  1327. "\\_SB.PCI0.IDE0.IDES.IDSM", /* 600e/x, 770e, 770x */
  1328. "\\_SB.PCI0.SATA.SCND.MSTR", /* T60, X60, Z60 */
  1329. "\\_SB.PCI0.IDE0.SCND.MSTR", /* all others */
  1330. ); /* A21e, R30, R31 */
  1331. IBM_HANDLE(bay_ej, bay, "_EJ3", /* 600e/x, A2xm/p, A3x */
  1332. "_EJ0", /* all others */
  1333. ); /* 570,A21e,G4x,R30,R31,R32,R40e,R50e */
  1334. IBM_HANDLE(bay2, root, "\\_SB.PCI0.IDE0.PRIM.SLAV", /* A3x, R32 */
  1335. "\\_SB.PCI0.IDE0.IDEP.IDPS", /* 600e/x, 770e, 770x */
  1336. ); /* all others */
  1337. IBM_HANDLE(bay2_ej, bay2, "_EJ3", /* 600e/x, 770e, A3x */
  1338. "_EJ0", /* 770x */
  1339. ); /* all others */
  1340. static int __init bay_init(struct ibm_init_struct *iibm)
  1341. {
  1342. vdbg_printk(TPACPI_DBG_INIT, "initializing bay subdriver\n");
  1343. IBM_ACPIHANDLE_INIT(bay);
  1344. if (bay_handle)
  1345. IBM_ACPIHANDLE_INIT(bay_ej);
  1346. IBM_ACPIHANDLE_INIT(bay2);
  1347. if (bay2_handle)
  1348. IBM_ACPIHANDLE_INIT(bay2_ej);
  1349. tp_features.bay_status = bay_handle &&
  1350. acpi_evalf(bay_handle, NULL, "_STA", "qv");
  1351. tp_features.bay_status2 = bay2_handle &&
  1352. acpi_evalf(bay2_handle, NULL, "_STA", "qv");
  1353. tp_features.bay_eject = bay_handle && bay_ej_handle &&
  1354. (strlencmp(bay_ej_path, "_EJ0") == 0 || experimental);
  1355. tp_features.bay_eject2 = bay2_handle && bay2_ej_handle &&
  1356. (strlencmp(bay2_ej_path, "_EJ0") == 0 || experimental);
  1357. vdbg_printk(TPACPI_DBG_INIT,
  1358. "bay 1: status %s, eject %s; bay 2: status %s, eject %s\n",
  1359. str_supported(tp_features.bay_status),
  1360. str_supported(tp_features.bay_eject),
  1361. str_supported(tp_features.bay_status2),
  1362. str_supported(tp_features.bay_eject2));
  1363. return (tp_features.bay_status || tp_features.bay_eject ||
  1364. tp_features.bay_status2 || tp_features.bay_eject2)? 0 : 1;
  1365. }
  1366. static void bay_notify(struct ibm_struct *ibm, u32 event)
  1367. {
  1368. acpi_bus_generate_event(ibm->acpi->device, event, 0);
  1369. }
  1370. #define bay_occupied(b) (_sta(b##_handle) & 1)
  1371. static int bay_read(char *p)
  1372. {
  1373. int len = 0;
  1374. int occupied = bay_occupied(bay);
  1375. int occupied2 = bay_occupied(bay2);
  1376. int eject, eject2;
  1377. len += sprintf(p + len, "status:\t\t%s\n",
  1378. tp_features.bay_status ?
  1379. (occupied ? "occupied" : "unoccupied") :
  1380. "not supported");
  1381. if (tp_features.bay_status2)
  1382. len += sprintf(p + len, "status2:\t%s\n", occupied2 ?
  1383. "occupied" : "unoccupied");
  1384. eject = tp_features.bay_eject && occupied;
  1385. eject2 = tp_features.bay_eject2 && occupied2;
  1386. if (eject && eject2)
  1387. len += sprintf(p + len, "commands:\teject, eject2\n");
  1388. else if (eject)
  1389. len += sprintf(p + len, "commands:\teject\n");
  1390. else if (eject2)
  1391. len += sprintf(p + len, "commands:\teject2\n");
  1392. return len;
  1393. }
  1394. static int bay_write(char *buf)
  1395. {
  1396. char *cmd;
  1397. if (!tp_features.bay_eject && !tp_features.bay_eject2)
  1398. return -ENODEV;
  1399. while ((cmd = next_cmd(&buf))) {
  1400. if (tp_features.bay_eject && strlencmp(cmd, "eject") == 0) {
  1401. if (!acpi_evalf(bay_ej_handle, NULL, NULL, "vd", 1))
  1402. return -EIO;
  1403. } else if (tp_features.bay_eject2 &&
  1404. strlencmp(cmd, "eject2") == 0) {
  1405. if (!acpi_evalf(bay2_ej_handle, NULL, NULL, "vd", 1))
  1406. return -EIO;
  1407. } else
  1408. return -EINVAL;
  1409. }
  1410. return 0;
  1411. }
  1412. static struct tp_acpi_drv_struct ibm_bay_acpidriver = {
  1413. .notify = bay_notify,
  1414. .handle = &bay_handle,
  1415. .type = ACPI_SYSTEM_NOTIFY,
  1416. };
  1417. static struct ibm_struct bay_driver_data = {
  1418. .name = "bay",
  1419. .read = bay_read,
  1420. .write = bay_write,
  1421. .acpi = &ibm_bay_acpidriver,
  1422. };
  1423. #endif /* CONFIG_THINKPAD_ACPI_BAY */
  1424. /*************************************************************************
  1425. * CMOS subdriver
  1426. */
  1427. static int __init cmos_init(struct ibm_init_struct *iibm)
  1428. {
  1429. vdbg_printk(TPACPI_DBG_INIT,
  1430. "initializing cmos commands subdriver\n");
  1431. IBM_ACPIHANDLE_INIT(cmos);
  1432. vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
  1433. str_supported(cmos_handle != NULL));
  1434. return (cmos_handle)? 0 : 1;
  1435. }
  1436. static int cmos_read(char *p)
  1437. {
  1438. int len = 0;
  1439. /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
  1440. R30, R31, T20-22, X20-21 */
  1441. if (!cmos_handle)
  1442. len += sprintf(p + len, "status:\t\tnot supported\n");
  1443. else {
  1444. len += sprintf(p + len, "status:\t\tsupported\n");
  1445. len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-21)\n");
  1446. }
  1447. return len;
  1448. }
  1449. static int cmos_write(char *buf)
  1450. {
  1451. char *cmd;
  1452. int cmos_cmd, res;
  1453. while ((cmd = next_cmd(&buf))) {
  1454. if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
  1455. cmos_cmd >= 0 && cmos_cmd <= 21) {
  1456. /* cmos_cmd set */
  1457. } else
  1458. return -EINVAL;
  1459. res = issue_thinkpad_cmos_command(cmos_cmd);
  1460. if (res)
  1461. return res;
  1462. }
  1463. return 0;
  1464. }
  1465. static struct ibm_struct cmos_driver_data = {
  1466. .name = "cmos",
  1467. .read = cmos_read,
  1468. .write = cmos_write,
  1469. };
  1470. /*************************************************************************
  1471. * LED subdriver
  1472. */
  1473. static enum led_access_mode led_supported;
  1474. IBM_HANDLE(led, ec, "SLED", /* 570 */
  1475. "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
  1476. "LED", /* all others */
  1477. ); /* R30, R31 */
  1478. static int __init led_init(struct ibm_init_struct *iibm)
  1479. {
  1480. vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
  1481. IBM_ACPIHANDLE_INIT(led);
  1482. if (!led_handle)
  1483. /* led not supported on R30, R31 */
  1484. led_supported = TPACPI_LED_NONE;
  1485. else if (strlencmp(led_path, "SLED") == 0)
  1486. /* 570 */
  1487. led_supported = TPACPI_LED_570;
  1488. else if (strlencmp(led_path, "SYSL") == 0)
  1489. /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
  1490. led_supported = TPACPI_LED_OLD;
  1491. else
  1492. /* all others */
  1493. led_supported = TPACPI_LED_NEW;
  1494. vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
  1495. str_supported(led_supported), led_supported);
  1496. return (led_supported != TPACPI_LED_NONE)? 0 : 1;
  1497. }
  1498. #define led_status(s) ((s) == 0 ? "off" : ((s) == 1 ? "on" : "blinking"))
  1499. static int led_read(char *p)
  1500. {
  1501. int len = 0;
  1502. if (!led_supported) {
  1503. len += sprintf(p + len, "status:\t\tnot supported\n");
  1504. return len;
  1505. }
  1506. len += sprintf(p + len, "status:\t\tsupported\n");
  1507. if (led_supported == TPACPI_LED_570) {
  1508. /* 570 */
  1509. int i, status;
  1510. for (i = 0; i < 8; i++) {
  1511. if (!acpi_evalf(ec_handle,
  1512. &status, "GLED", "dd", 1 << i))
  1513. return -EIO;
  1514. len += sprintf(p + len, "%d:\t\t%s\n",
  1515. i, led_status(status));
  1516. }
  1517. }
  1518. len += sprintf(p + len, "commands:\t"
  1519. "<led> on, <led> off, <led> blink (<led> is 0-7)\n");
  1520. return len;
  1521. }
  1522. /* off, on, blink */
  1523. static const int led_sled_arg1[] = { 0, 1, 3 };
  1524. static const int led_exp_hlbl[] = { 0, 0, 1 }; /* led# * */
  1525. static const int led_exp_hlcl[] = { 0, 1, 1 }; /* led# * */
  1526. static const int led_led_arg1[] = { 0, 0x80, 0xc0 };
  1527. static int led_write(char *buf)
  1528. {
  1529. char *cmd;
  1530. int led, ind, ret;
  1531. if (!led_supported)
  1532. return -ENODEV;
  1533. while ((cmd = next_cmd(&buf))) {
  1534. if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 7)
  1535. return -EINVAL;
  1536. if (strstr(cmd, "off")) {
  1537. ind = 0;
  1538. } else if (strstr(cmd, "on")) {
  1539. ind = 1;
  1540. } else if (strstr(cmd, "blink")) {
  1541. ind = 2;
  1542. } else
  1543. return -EINVAL;
  1544. if (led_supported == TPACPI_LED_570) {
  1545. /* 570 */
  1546. led = 1 << led;
  1547. if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
  1548. led, led_sled_arg1[ind]))
  1549. return -EIO;
  1550. } else if (led_supported == TPACPI_LED_OLD) {
  1551. /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
  1552. led = 1 << led;
  1553. ret = ec_write(TPACPI_LED_EC_HLMS, led);
  1554. if (ret >= 0)
  1555. ret =
  1556. ec_write(TPACPI_LED_EC_HLBL,
  1557. led * led_exp_hlbl[ind]);
  1558. if (ret >= 0)
  1559. ret =
  1560. ec_write(TPACPI_LED_EC_HLCL,
  1561. led * led_exp_hlcl[ind]);
  1562. if (ret < 0)
  1563. return ret;
  1564. } else {
  1565. /* all others */
  1566. if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
  1567. led, led_led_arg1[ind]))
  1568. return -EIO;
  1569. }
  1570. }
  1571. return 0;
  1572. }
  1573. static struct ibm_struct led_driver_data = {
  1574. .name = "led",
  1575. .read = led_read,
  1576. .write = led_write,
  1577. };
  1578. /*************************************************************************
  1579. * Beep subdriver
  1580. */
  1581. IBM_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
  1582. static int __init beep_init(struct ibm_init_struct *iibm)
  1583. {
  1584. vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
  1585. IBM_ACPIHANDLE_INIT(beep);
  1586. vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
  1587. str_supported(beep_handle != NULL));
  1588. return (beep_handle)? 0 : 1;
  1589. }
  1590. static int beep_read(char *p)
  1591. {
  1592. int len = 0;
  1593. if (!beep_handle)
  1594. len += sprintf(p + len, "status:\t\tnot supported\n");
  1595. else {
  1596. len += sprintf(p + len, "status:\t\tsupported\n");
  1597. len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-17)\n");
  1598. }
  1599. return len;
  1600. }
  1601. static int beep_write(char *buf)
  1602. {
  1603. char *cmd;
  1604. int beep_cmd;
  1605. if (!beep_handle)
  1606. return -ENODEV;
  1607. while ((cmd = next_cmd(&buf))) {
  1608. if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
  1609. beep_cmd >= 0 && beep_cmd <= 17) {
  1610. /* beep_cmd set */
  1611. } else
  1612. return -EINVAL;
  1613. if (!acpi_evalf(beep_handle, NULL, NULL, "vdd", beep_cmd, 0))
  1614. return -EIO;
  1615. }
  1616. return 0;
  1617. }
  1618. static struct ibm_struct beep_driver_data = {
  1619. .name = "beep",
  1620. .read = beep_read,
  1621. .write = beep_write,
  1622. };
  1623. /*************************************************************************
  1624. * Thermal subdriver
  1625. */
  1626. static enum thermal_access_mode thermal_read_mode;
  1627. /* sysfs temp##_input -------------------------------------------------- */
  1628. static ssize_t thermal_temp_input_show(struct device *dev,
  1629. struct device_attribute *attr,
  1630. char *buf)
  1631. {
  1632. struct sensor_device_attribute *sensor_attr =
  1633. to_sensor_dev_attr(attr);
  1634. int idx = sensor_attr->index;
  1635. s32 value;
  1636. int res;
  1637. res = thermal_get_sensor(idx, &value);
  1638. if (res)
  1639. return res;
  1640. if (value == TP_EC_THERMAL_TMP_NA * 1000)
  1641. return -ENXIO;
  1642. return snprintf(buf, PAGE_SIZE, "%d\n", value);
  1643. }
  1644. #define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
  1645. SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, thermal_temp_input_show, NULL, _idxB)
  1646. static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
  1647. THERMAL_SENSOR_ATTR_TEMP(1, 0),
  1648. THERMAL_SENSOR_ATTR_TEMP(2, 1),
  1649. THERMAL_SENSOR_ATTR_TEMP(3, 2),
  1650. THERMAL_SENSOR_ATTR_TEMP(4, 3),
  1651. THERMAL_SENSOR_ATTR_TEMP(5, 4),
  1652. THERMAL_SENSOR_ATTR_TEMP(6, 5),
  1653. THERMAL_SENSOR_ATTR_TEMP(7, 6),
  1654. THERMAL_SENSOR_ATTR_TEMP(8, 7),
  1655. THERMAL_SENSOR_ATTR_TEMP(9, 8),
  1656. THERMAL_SENSOR_ATTR_TEMP(10, 9),
  1657. THERMAL_SENSOR_ATTR_TEMP(11, 10),
  1658. THERMAL_SENSOR_ATTR_TEMP(12, 11),
  1659. THERMAL_SENSOR_ATTR_TEMP(13, 12),
  1660. THERMAL_SENSOR_ATTR_TEMP(14, 13),
  1661. THERMAL_SENSOR_ATTR_TEMP(15, 14),
  1662. THERMAL_SENSOR_ATTR_TEMP(16, 15),
  1663. };
  1664. #define THERMAL_ATTRS(X) \
  1665. &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
  1666. static struct attribute *thermal_temp_input_attr[] = {
  1667. THERMAL_ATTRS(8),
  1668. THERMAL_ATTRS(9),
  1669. THERMAL_ATTRS(10),
  1670. THERMAL_ATTRS(11),
  1671. THERMAL_ATTRS(12),
  1672. THERMAL_ATTRS(13),
  1673. THERMAL_ATTRS(14),
  1674. THERMAL_ATTRS(15),
  1675. THERMAL_ATTRS(0),
  1676. THERMAL_ATTRS(1),
  1677. THERMAL_ATTRS(2),
  1678. THERMAL_ATTRS(3),
  1679. THERMAL_ATTRS(4),
  1680. THERMAL_ATTRS(5),
  1681. THERMAL_ATTRS(6),
  1682. THERMAL_ATTRS(7),
  1683. NULL
  1684. };
  1685. static const struct attribute_group thermal_temp_input16_group = {
  1686. .attrs = thermal_temp_input_attr
  1687. };
  1688. static const struct attribute_group thermal_temp_input8_group = {
  1689. .attrs = &thermal_temp_input_attr[8]
  1690. };
  1691. #undef THERMAL_SENSOR_ATTR_TEMP
  1692. #undef THERMAL_ATTRS
  1693. /* --------------------------------------------------------------------- */
  1694. static int __init thermal_init(struct ibm_init_struct *iibm)
  1695. {
  1696. u8 t, ta1, ta2;
  1697. int i;
  1698. int acpi_tmp7;
  1699. int res;
  1700. vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
  1701. acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
  1702. if (ibm_thinkpad_ec_found && experimental) {
  1703. /*
  1704. * Direct EC access mode: sensors at registers
  1705. * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
  1706. * non-implemented, thermal sensors return 0x80 when
  1707. * not available
  1708. */
  1709. ta1 = ta2 = 0;
  1710. for (i = 0; i < 8; i++) {
  1711. if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
  1712. ta1 |= t;
  1713. } else {
  1714. ta1 = 0;
  1715. break;
  1716. }
  1717. if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
  1718. ta2 |= t;
  1719. } else {
  1720. ta1 = 0;
  1721. break;
  1722. }
  1723. }
  1724. if (ta1 == 0) {
  1725. /* This is sheer paranoia, but we handle it anyway */
  1726. if (acpi_tmp7) {
  1727. printk(IBM_ERR
  1728. "ThinkPad ACPI EC access misbehaving, "
  1729. "falling back to ACPI TMPx access mode\n");
  1730. thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
  1731. } else {
  1732. printk(IBM_ERR
  1733. "ThinkPad ACPI EC access misbehaving, "
  1734. "disabling thermal sensors access\n");
  1735. thermal_read_mode = TPACPI_THERMAL_NONE;
  1736. }
  1737. } else {
  1738. thermal_read_mode =
  1739. (ta2 != 0) ?
  1740. TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
  1741. }
  1742. } else if (acpi_tmp7) {
  1743. if (acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
  1744. /* 600e/x, 770e, 770x */
  1745. thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
  1746. } else {
  1747. /* Standard ACPI TMPx access, max 8 sensors */
  1748. thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
  1749. }
  1750. } else {
  1751. /* temperatures not supported on 570, G4x, R30, R31, R32 */
  1752. thermal_read_mode = TPACPI_THERMAL_NONE;
  1753. }
  1754. vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
  1755. str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
  1756. thermal_read_mode);
  1757. switch(thermal_read_mode) {
  1758. case TPACPI_THERMAL_TPEC_16:
  1759. res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
  1760. &thermal_temp_input16_group);
  1761. if (res)
  1762. return res;
  1763. break;
  1764. case TPACPI_THERMAL_TPEC_8:
  1765. case TPACPI_THERMAL_ACPI_TMP07:
  1766. case TPACPI_THERMAL_ACPI_UPDT:
  1767. res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
  1768. &thermal_temp_input8_group);
  1769. if (res)
  1770. return res;
  1771. break;
  1772. case TPACPI_THERMAL_NONE:
  1773. default:
  1774. return 1;
  1775. }
  1776. return 0;
  1777. }
  1778. static void thermal_exit(void)
  1779. {
  1780. switch(thermal_read_mode) {
  1781. case TPACPI_THERMAL_TPEC_16:
  1782. sysfs_remove_group(&tpacpi_pdev->dev.kobj,
  1783. &thermal_temp_input16_group);
  1784. break;
  1785. case TPACPI_THERMAL_TPEC_8:
  1786. case TPACPI_THERMAL_ACPI_TMP07:
  1787. case TPACPI_THERMAL_ACPI_UPDT:
  1788. sysfs_remove_group(&tpacpi_pdev->dev.kobj,
  1789. &thermal_temp_input16_group);
  1790. break;
  1791. case TPACPI_THERMAL_NONE:
  1792. default:
  1793. break;
  1794. }
  1795. }
  1796. /* idx is zero-based */
  1797. static int thermal_get_sensor(int idx, s32 *value)
  1798. {
  1799. int t;
  1800. s8 tmp;
  1801. char tmpi[5];
  1802. t = TP_EC_THERMAL_TMP0;
  1803. switch (thermal_read_mode) {
  1804. #if TPACPI_MAX_THERMAL_SENSORS >= 16
  1805. case TPACPI_THERMAL_TPEC_16:
  1806. if (idx >= 8 && idx <= 15) {
  1807. t = TP_EC_THERMAL_TMP8;
  1808. idx -= 8;
  1809. }
  1810. /* fallthrough */
  1811. #endif
  1812. case TPACPI_THERMAL_TPEC_8:
  1813. if (idx <= 7) {
  1814. if (!acpi_ec_read(t + idx, &tmp))
  1815. return -EIO;
  1816. *value = tmp * 1000;
  1817. return 0;
  1818. }
  1819. break;
  1820. case TPACPI_THERMAL_ACPI_UPDT:
  1821. if (idx <= 7) {
  1822. snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
  1823. if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
  1824. return -EIO;
  1825. if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
  1826. return -EIO;
  1827. *value = (t - 2732) * 100;
  1828. return 0;
  1829. }
  1830. break;
  1831. case TPACPI_THERMAL_ACPI_TMP07:
  1832. if (idx <= 7) {
  1833. snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
  1834. if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
  1835. return -EIO;
  1836. *value = t * 1000;
  1837. return 0;
  1838. }
  1839. break;
  1840. case TPACPI_THERMAL_NONE:
  1841. default:
  1842. return -ENOSYS;
  1843. }
  1844. return -EINVAL;
  1845. }
  1846. static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
  1847. {
  1848. int res, i;
  1849. int n;
  1850. n = 8;
  1851. i = 0;
  1852. if (!s)
  1853. return -EINVAL;
  1854. if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
  1855. n = 16;
  1856. for(i = 0 ; i < n; i++) {
  1857. res = thermal_get_sensor(i, &s->temp[i]);
  1858. if (res)
  1859. return res;
  1860. }
  1861. return n;
  1862. }
  1863. static int thermal_read(char *p)
  1864. {
  1865. int len = 0;
  1866. int n, i;
  1867. struct ibm_thermal_sensors_struct t;
  1868. n = thermal_get_sensors(&t);
  1869. if (unlikely(n < 0))
  1870. return n;
  1871. len += sprintf(p + len, "temperatures:\t");
  1872. if (n > 0) {
  1873. for (i = 0; i < (n - 1); i++)
  1874. len += sprintf(p + len, "%d ", t.temp[i] / 1000);
  1875. len += sprintf(p + len, "%d\n", t.temp[i] / 1000);
  1876. } else
  1877. len += sprintf(p + len, "not supported\n");
  1878. return len;
  1879. }
  1880. static struct ibm_struct thermal_driver_data = {
  1881. .name = "thermal",
  1882. .read = thermal_read,
  1883. .exit = thermal_exit,
  1884. };
  1885. /*************************************************************************
  1886. * EC Dump subdriver
  1887. */
  1888. static u8 ecdump_regs[256];
  1889. static int ecdump_read(char *p)
  1890. {
  1891. int len = 0;
  1892. int i, j;
  1893. u8 v;
  1894. len += sprintf(p + len, "EC "
  1895. " +00 +01 +02 +03 +04 +05 +06 +07"
  1896. " +08 +09 +0a +0b +0c +0d +0e +0f\n");
  1897. for (i = 0; i < 256; i += 16) {
  1898. len += sprintf(p + len, "EC 0x%02x:", i);
  1899. for (j = 0; j < 16; j++) {
  1900. if (!acpi_ec_read(i + j, &v))
  1901. break;
  1902. if (v != ecdump_regs[i + j])
  1903. len += sprintf(p + len, " *%02x", v);
  1904. else
  1905. len += sprintf(p + len, " %02x", v);
  1906. ecdump_regs[i + j] = v;
  1907. }
  1908. len += sprintf(p + len, "\n");
  1909. if (j != 16)
  1910. break;
  1911. }
  1912. /* These are way too dangerous to advertise openly... */
  1913. #if 0
  1914. len += sprintf(p + len, "commands:\t0x<offset> 0x<value>"
  1915. " (<offset> is 00-ff, <value> is 00-ff)\n");
  1916. len += sprintf(p + len, "commands:\t0x<offset> <value> "
  1917. " (<offset> is 00-ff, <value> is 0-255)\n");
  1918. #endif
  1919. return len;
  1920. }
  1921. static int ecdump_write(char *buf)
  1922. {
  1923. char *cmd;
  1924. int i, v;
  1925. while ((cmd = next_cmd(&buf))) {
  1926. if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
  1927. /* i and v set */
  1928. } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
  1929. /* i and v set */
  1930. } else
  1931. return -EINVAL;
  1932. if (i >= 0 && i < 256 && v >= 0 && v < 256) {
  1933. if (!acpi_ec_write(i, v))
  1934. return -EIO;
  1935. } else
  1936. return -EINVAL;
  1937. }
  1938. return 0;
  1939. }
  1940. static struct ibm_struct ecdump_driver_data = {
  1941. .name = "ecdump",
  1942. .read = ecdump_read,
  1943. .write = ecdump_write,
  1944. .flags.experimental = 1,
  1945. };
  1946. /*************************************************************************
  1947. * Backlight/brightness subdriver
  1948. */
  1949. static struct backlight_device *ibm_backlight_device = NULL;
  1950. static struct backlight_ops ibm_backlight_data = {
  1951. .get_brightness = brightness_get,
  1952. .update_status = brightness_update_status,
  1953. };
  1954. static int __init brightness_init(struct ibm_init_struct *iibm)
  1955. {
  1956. int b;
  1957. vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
  1958. b = brightness_get(NULL);
  1959. if (b < 0)
  1960. return b;
  1961. ibm_backlight_device = backlight_device_register("ibm", NULL, NULL,
  1962. &ibm_backlight_data);
  1963. if (IS_ERR(ibm_backlight_device)) {
  1964. printk(IBM_ERR "Could not register backlight device\n");
  1965. return PTR_ERR(ibm_backlight_device);
  1966. }
  1967. vdbg_printk(TPACPI_DBG_INIT, "brightness is supported\n");
  1968. ibm_backlight_device->props.max_brightness = 7;
  1969. ibm_backlight_device->props.brightness = b;
  1970. backlight_update_status(ibm_backlight_device);
  1971. return 0;
  1972. }
  1973. static void brightness_exit(void)
  1974. {
  1975. if (ibm_backlight_device) {
  1976. vdbg_printk(TPACPI_DBG_EXIT,
  1977. "calling backlight_device_unregister()\n");
  1978. backlight_device_unregister(ibm_backlight_device);
  1979. ibm_backlight_device = NULL;
  1980. }
  1981. }
  1982. static int brightness_update_status(struct backlight_device *bd)
  1983. {
  1984. return brightness_set(
  1985. (bd->props.fb_blank == FB_BLANK_UNBLANK &&
  1986. bd->props.power == FB_BLANK_UNBLANK) ?
  1987. bd->props.brightness : 0);
  1988. }
  1989. static int brightness_get(struct backlight_device *bd)
  1990. {
  1991. u8 level;
  1992. if (!acpi_ec_read(brightness_offset, &level))
  1993. return -EIO;
  1994. level &= 0x7;
  1995. return level;
  1996. }
  1997. static int brightness_set(int value)
  1998. {
  1999. int cmos_cmd, inc, i;
  2000. int current_value = brightness_get(NULL);
  2001. value &= 7;
  2002. cmos_cmd = value > current_value ? TP_CMOS_BRIGHTNESS_UP : TP_CMOS_BRIGHTNESS_DOWN;
  2003. inc = value > current_value ? 1 : -1;
  2004. for (i = current_value; i != value; i += inc) {
  2005. if (issue_thinkpad_cmos_command(cmos_cmd))
  2006. return -EIO;
  2007. if (!acpi_ec_write(brightness_offset, i + inc))
  2008. return -EIO;
  2009. }
  2010. return 0;
  2011. }
  2012. static int brightness_read(char *p)
  2013. {
  2014. int len = 0;
  2015. int level;
  2016. if ((level = brightness_get(NULL)) < 0) {
  2017. len += sprintf(p + len, "level:\t\tunreadable\n");
  2018. } else {
  2019. len += sprintf(p + len, "level:\t\t%d\n", level & 0x7);
  2020. len += sprintf(p + len, "commands:\tup, down\n");
  2021. len += sprintf(p + len, "commands:\tlevel <level>"
  2022. " (<level> is 0-7)\n");
  2023. }
  2024. return len;
  2025. }
  2026. static int brightness_write(char *buf)
  2027. {
  2028. int level;
  2029. int new_level;
  2030. char *cmd;
  2031. while ((cmd = next_cmd(&buf))) {
  2032. if ((level = brightness_get(NULL)) < 0)
  2033. return level;
  2034. level &= 7;
  2035. if (strlencmp(cmd, "up") == 0) {
  2036. new_level = level == 7 ? 7 : level + 1;
  2037. } else if (strlencmp(cmd, "down") == 0) {
  2038. new_level = level == 0 ? 0 : level - 1;
  2039. } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
  2040. new_level >= 0 && new_level <= 7) {
  2041. /* new_level set */
  2042. } else
  2043. return -EINVAL;
  2044. brightness_set(new_level);
  2045. }
  2046. return 0;
  2047. }
  2048. static struct ibm_struct brightness_driver_data = {
  2049. .name = "brightness",
  2050. .read = brightness_read,
  2051. .write = brightness_write,
  2052. .exit = brightness_exit,
  2053. };
  2054. /*************************************************************************
  2055. * Volume subdriver
  2056. */
  2057. static int volume_read(char *p)
  2058. {
  2059. int len = 0;
  2060. u8 level;
  2061. if (!acpi_ec_read(volume_offset, &level)) {
  2062. len += sprintf(p + len, "level:\t\tunreadable\n");
  2063. } else {
  2064. len += sprintf(p + len, "level:\t\t%d\n", level & 0xf);
  2065. len += sprintf(p + len, "mute:\t\t%s\n", onoff(level, 6));
  2066. len += sprintf(p + len, "commands:\tup, down, mute\n");
  2067. len += sprintf(p + len, "commands:\tlevel <level>"
  2068. " (<level> is 0-15)\n");
  2069. }
  2070. return len;
  2071. }
  2072. static int volume_write(char *buf)
  2073. {
  2074. int cmos_cmd, inc, i;
  2075. u8 level, mute;
  2076. int new_level, new_mute;
  2077. char *cmd;
  2078. while ((cmd = next_cmd(&buf))) {
  2079. if (!acpi_ec_read(volume_offset, &level))
  2080. return -EIO;
  2081. new_mute = mute = level & 0x40;
  2082. new_level = level = level & 0xf;
  2083. if (strlencmp(cmd, "up") == 0) {
  2084. if (mute)
  2085. new_mute = 0;
  2086. else
  2087. new_level = level == 15 ? 15 : level + 1;
  2088. } else if (strlencmp(cmd, "down") == 0) {
  2089. if (mute)
  2090. new_mute = 0;
  2091. else
  2092. new_level = level == 0 ? 0 : level - 1;
  2093. } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
  2094. new_level >= 0 && new_level <= 15) {
  2095. /* new_level set */
  2096. } else if (strlencmp(cmd, "mute") == 0) {
  2097. new_mute = 0x40;
  2098. } else
  2099. return -EINVAL;
  2100. if (new_level != level) { /* mute doesn't change */
  2101. cmos_cmd = new_level > level ? TP_CMOS_VOLUME_UP : TP_CMOS_VOLUME_DOWN;
  2102. inc = new_level > level ? 1 : -1;
  2103. if (mute && (issue_thinkpad_cmos_command(cmos_cmd) ||
  2104. !acpi_ec_write(volume_offset, level)))
  2105. return -EIO;
  2106. for (i = level; i != new_level; i += inc)
  2107. if (issue_thinkpad_cmos_command(cmos_cmd) ||
  2108. !acpi_ec_write(volume_offset, i + inc))
  2109. return -EIO;
  2110. if (mute && (issue_thinkpad_cmos_command(TP_CMOS_VOLUME_MUTE) ||
  2111. !acpi_ec_write(volume_offset,
  2112. new_level + mute)))
  2113. return -EIO;
  2114. }
  2115. if (new_mute != mute) { /* level doesn't change */
  2116. cmos_cmd = new_mute ? TP_CMOS_VOLUME_MUTE : TP_CMOS_VOLUME_UP;
  2117. if (issue_thinkpad_cmos_command(cmos_cmd) ||
  2118. !acpi_ec_write(volume_offset, level + new_mute))
  2119. return -EIO;
  2120. }
  2121. }
  2122. return 0;
  2123. }
  2124. static struct ibm_struct volume_driver_data = {
  2125. .name = "volume",
  2126. .read = volume_read,
  2127. .write = volume_write,
  2128. };
  2129. /*************************************************************************
  2130. * Fan subdriver
  2131. */
  2132. /*
  2133. * FAN ACCESS MODES
  2134. *
  2135. * TPACPI_FAN_RD_ACPI_GFAN:
  2136. * ACPI GFAN method: returns fan level
  2137. *
  2138. * see TPACPI_FAN_WR_ACPI_SFAN
  2139. * EC 0x2f (HFSP) not available if GFAN exists
  2140. *
  2141. * TPACPI_FAN_WR_ACPI_SFAN:
  2142. * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
  2143. *
  2144. * EC 0x2f (HFSP) might be available *for reading*, but do not use
  2145. * it for writing.
  2146. *
  2147. * TPACPI_FAN_WR_TPEC:
  2148. * ThinkPad EC register 0x2f (HFSP): fan control loop mode
  2149. * Supported on almost all ThinkPads
  2150. *
  2151. * Fan speed changes of any sort (including those caused by the
  2152. * disengaged mode) are usually done slowly by the firmware as the
  2153. * maximum ammount of fan duty cycle change per second seems to be
  2154. * limited.
  2155. *
  2156. * Reading is not available if GFAN exists.
  2157. * Writing is not available if SFAN exists.
  2158. *
  2159. * Bits
  2160. * 7 automatic mode engaged;
  2161. * (default operation mode of the ThinkPad)
  2162. * fan level is ignored in this mode.
  2163. * 6 full speed mode (takes precedence over bit 7);
  2164. * not available on all thinkpads. May disable
  2165. * the tachometer while the fan controller ramps up
  2166. * the speed (which can take up to a few *minutes*).
  2167. * Speeds up fan to 100% duty-cycle, which is far above
  2168. * the standard RPM levels. It is not impossible that
  2169. * it could cause hardware damage.
  2170. * 5-3 unused in some models. Extra bits for fan level
  2171. * in others, but still useless as all values above
  2172. * 7 map to the same speed as level 7 in these models.
  2173. * 2-0 fan level (0..7 usually)
  2174. * 0x00 = stop
  2175. * 0x07 = max (set when temperatures critical)
  2176. * Some ThinkPads may have other levels, see
  2177. * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
  2178. *
  2179. * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
  2180. * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
  2181. * does so, its initial value is meaningless (0x07).
  2182. *
  2183. * For firmware bugs, refer to:
  2184. * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
  2185. *
  2186. * ----
  2187. *
  2188. * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
  2189. * Main fan tachometer reading (in RPM)
  2190. *
  2191. * This register is present on all ThinkPads with a new-style EC, and
  2192. * it is known not to be present on the A21m/e, and T22, as there is
  2193. * something else in offset 0x84 according to the ACPI DSDT. Other
  2194. * ThinkPads from this same time period (and earlier) probably lack the
  2195. * tachometer as well.
  2196. *
  2197. * Unfortunately a lot of ThinkPads with new-style ECs but whose firwmare
  2198. * was never fixed by IBM to report the EC firmware version string
  2199. * probably support the tachometer (like the early X models), so
  2200. * detecting it is quite hard. We need more data to know for sure.
  2201. *
  2202. * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
  2203. * might result.
  2204. *
  2205. * FIRMWARE BUG: may go stale while the EC is switching to full speed
  2206. * mode.
  2207. *
  2208. * For firmware bugs, refer to:
  2209. * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
  2210. *
  2211. * TPACPI_FAN_WR_ACPI_FANS:
  2212. * ThinkPad X31, X40, X41. Not available in the X60.
  2213. *
  2214. * FANS ACPI handle: takes three arguments: low speed, medium speed,
  2215. * high speed. ACPI DSDT seems to map these three speeds to levels
  2216. * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
  2217. * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
  2218. *
  2219. * The speeds are stored on handles
  2220. * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
  2221. *
  2222. * There are three default speed sets, acessible as handles:
  2223. * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
  2224. *
  2225. * ACPI DSDT switches which set is in use depending on various
  2226. * factors.
  2227. *
  2228. * TPACPI_FAN_WR_TPEC is also available and should be used to
  2229. * command the fan. The X31/X40/X41 seems to have 8 fan levels,
  2230. * but the ACPI tables just mention level 7.
  2231. */
  2232. static enum fan_status_access_mode fan_status_access_mode;
  2233. static enum fan_control_access_mode fan_control_access_mode;
  2234. static enum fan_control_commands fan_control_commands;
  2235. static u8 fan_control_initial_status;
  2236. static u8 fan_control_desired_level;
  2237. static void fan_watchdog_fire(struct work_struct *ignored);
  2238. static int fan_watchdog_maxinterval;
  2239. static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
  2240. IBM_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
  2241. IBM_HANDLE(gfan, ec, "GFAN", /* 570 */
  2242. "\\FSPD", /* 600e/x, 770e, 770x */
  2243. ); /* all others */
  2244. IBM_HANDLE(sfan, ec, "SFAN", /* 570 */
  2245. "JFNS", /* 770x-JL */
  2246. ); /* all others */
  2247. /*
  2248. * SYSFS fan layout: hwmon compatible (device)
  2249. *
  2250. * pwm*_enable:
  2251. * 0: "disengaged" mode
  2252. * 1: manual mode
  2253. * 2: native EC "auto" mode (recommended, hardware default)
  2254. *
  2255. * pwm*: set speed in manual mode, ignored otherwise.
  2256. * 0 is level 0; 255 is level 7. Intermediate points done with linear
  2257. * interpolation.
  2258. *
  2259. * fan*_input: tachometer reading, RPM
  2260. *
  2261. *
  2262. * SYSFS fan layout: extensions
  2263. *
  2264. * fan_watchdog (driver):
  2265. * fan watchdog interval in seconds, 0 disables (default), max 120
  2266. */
  2267. /* sysfs fan pwm1_enable ----------------------------------------------- */
  2268. static ssize_t fan_pwm1_enable_show(struct device *dev,
  2269. struct device_attribute *attr,
  2270. char *buf)
  2271. {
  2272. int res, mode;
  2273. u8 status;
  2274. res = fan_get_status_safe(&status);
  2275. if (res)
  2276. return res;
  2277. if (unlikely(tp_features.fan_ctrl_status_undef)) {
  2278. if (status != fan_control_initial_status) {
  2279. tp_features.fan_ctrl_status_undef = 0;
  2280. } else {
  2281. /* Return most likely status. In fact, it
  2282. * might be the only possible status */
  2283. status = TP_EC_FAN_AUTO;
  2284. }
  2285. }
  2286. if (status & TP_EC_FAN_FULLSPEED) {
  2287. mode = 0;
  2288. } else if (status & TP_EC_FAN_AUTO) {
  2289. mode = 2;
  2290. } else
  2291. mode = 1;
  2292. return snprintf(buf, PAGE_SIZE, "%d\n", mode);
  2293. }
  2294. static ssize_t fan_pwm1_enable_store(struct device *dev,
  2295. struct device_attribute *attr,
  2296. const char *buf, size_t count)
  2297. {
  2298. unsigned long t;
  2299. int res, level;
  2300. if (parse_strtoul(buf, 2, &t))
  2301. return -EINVAL;
  2302. switch (t) {
  2303. case 0:
  2304. level = TP_EC_FAN_FULLSPEED;
  2305. break;
  2306. case 1:
  2307. level = TPACPI_FAN_LAST_LEVEL;
  2308. break;
  2309. case 2:
  2310. level = TP_EC_FAN_AUTO;
  2311. break;
  2312. case 3:
  2313. /* reserved for software-controlled auto mode */
  2314. return -ENOSYS;
  2315. default:
  2316. return -EINVAL;
  2317. }
  2318. res = fan_set_level_safe(level);
  2319. if (res < 0)
  2320. return res;
  2321. fan_watchdog_reset();
  2322. return count;
  2323. }
  2324. static struct device_attribute dev_attr_fan_pwm1_enable =
  2325. __ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
  2326. fan_pwm1_enable_show, fan_pwm1_enable_store);
  2327. /* sysfs fan pwm1 ------------------------------------------------------ */
  2328. static ssize_t fan_pwm1_show(struct device *dev,
  2329. struct device_attribute *attr,
  2330. char *buf)
  2331. {
  2332. int res;
  2333. u8 status;
  2334. res = fan_get_status_safe(&status);
  2335. if (res)
  2336. return res;
  2337. if (unlikely(tp_features.fan_ctrl_status_undef)) {
  2338. if (status != fan_control_initial_status) {
  2339. tp_features.fan_ctrl_status_undef = 0;
  2340. } else {
  2341. status = TP_EC_FAN_AUTO;
  2342. }
  2343. }
  2344. if ((status &
  2345. (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
  2346. status = fan_control_desired_level;
  2347. if (status > 7)
  2348. status = 7;
  2349. return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
  2350. }
  2351. static ssize_t fan_pwm1_store(struct device *dev,
  2352. struct device_attribute *attr,
  2353. const char *buf, size_t count)
  2354. {
  2355. unsigned long s;
  2356. int rc;
  2357. u8 status, newlevel;
  2358. if (parse_strtoul(buf, 255, &s))
  2359. return -EINVAL;
  2360. /* scale down from 0-255 to 0-7 */
  2361. newlevel = (s >> 5) & 0x07;
  2362. rc = mutex_lock_interruptible(&fan_mutex);
  2363. if (rc < 0)
  2364. return rc;
  2365. rc = fan_get_status(&status);
  2366. if (!rc && (status &
  2367. (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
  2368. rc = fan_set_level(newlevel);
  2369. if (!rc)
  2370. fan_update_desired_level(newlevel);
  2371. fan_watchdog_reset();
  2372. }
  2373. mutex_unlock(&fan_mutex);
  2374. return (rc)? rc : count;
  2375. }
  2376. static struct device_attribute dev_attr_fan_pwm1 =
  2377. __ATTR(pwm1, S_IWUSR | S_IRUGO,
  2378. fan_pwm1_show, fan_pwm1_store);
  2379. /* sysfs fan fan1_input ------------------------------------------------ */
  2380. static ssize_t fan_fan1_input_show(struct device *dev,
  2381. struct device_attribute *attr,
  2382. char *buf)
  2383. {
  2384. int res;
  2385. unsigned int speed;
  2386. res = fan_get_speed(&speed);
  2387. if (res < 0)
  2388. return res;
  2389. return snprintf(buf, PAGE_SIZE, "%u\n", speed);
  2390. }
  2391. static struct device_attribute dev_attr_fan_fan1_input =
  2392. __ATTR(fan1_input, S_IRUGO,
  2393. fan_fan1_input_show, NULL);
  2394. /* sysfs fan fan_watchdog (driver) ------------------------------------- */
  2395. static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
  2396. char *buf)
  2397. {
  2398. return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
  2399. }
  2400. static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
  2401. const char *buf, size_t count)
  2402. {
  2403. unsigned long t;
  2404. if (parse_strtoul(buf, 120, &t))
  2405. return -EINVAL;
  2406. fan_watchdog_maxinterval = t;
  2407. fan_watchdog_reset();
  2408. return count;
  2409. }
  2410. static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
  2411. fan_fan_watchdog_show, fan_fan_watchdog_store);
  2412. /* --------------------------------------------------------------------- */
  2413. static struct attribute *fan_attributes[] = {
  2414. &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr,
  2415. &dev_attr_fan_fan1_input.attr,
  2416. NULL
  2417. };
  2418. static const struct attribute_group fan_attr_group = {
  2419. .attrs = fan_attributes,
  2420. };
  2421. static int __init fan_init(struct ibm_init_struct *iibm)
  2422. {
  2423. int rc;
  2424. vdbg_printk(TPACPI_DBG_INIT, "initializing fan subdriver\n");
  2425. mutex_init(&fan_mutex);
  2426. fan_status_access_mode = TPACPI_FAN_NONE;
  2427. fan_control_access_mode = TPACPI_FAN_WR_NONE;
  2428. fan_control_commands = 0;
  2429. fan_watchdog_maxinterval = 0;
  2430. tp_features.fan_ctrl_status_undef = 0;
  2431. fan_control_desired_level = 7;
  2432. IBM_ACPIHANDLE_INIT(fans);
  2433. IBM_ACPIHANDLE_INIT(gfan);
  2434. IBM_ACPIHANDLE_INIT(sfan);
  2435. if (gfan_handle) {
  2436. /* 570, 600e/x, 770e, 770x */
  2437. fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
  2438. } else {
  2439. /* all other ThinkPads: note that even old-style
  2440. * ThinkPad ECs supports the fan control register */
  2441. if (likely(acpi_ec_read(fan_status_offset,
  2442. &fan_control_initial_status))) {
  2443. fan_status_access_mode = TPACPI_FAN_RD_TPEC;
  2444. /* In some ThinkPads, neither the EC nor the ACPI
  2445. * DSDT initialize the fan status, and it ends up
  2446. * being set to 0x07 when it *could* be either
  2447. * 0x07 or 0x80.
  2448. *
  2449. * Enable for TP-1Y (T43), TP-78 (R51e),
  2450. * TP-76 (R52), TP-70 (T43, R52), which are known
  2451. * to be buggy. */
  2452. if (fan_control_initial_status == 0x07 &&
  2453. ibm_thinkpad_ec_found &&
  2454. ((ibm_thinkpad_ec_found[0] == '1' &&
  2455. ibm_thinkpad_ec_found[1] == 'Y') ||
  2456. (ibm_thinkpad_ec_found[0] == '7' &&
  2457. (ibm_thinkpad_ec_found[1] == '6' ||
  2458. ibm_thinkpad_ec_found[1] == '8' ||
  2459. ibm_thinkpad_ec_found[1] == '0'))
  2460. )) {
  2461. printk(IBM_NOTICE
  2462. "fan_init: initial fan status is "
  2463. "unknown, assuming it is in auto "
  2464. "mode\n");
  2465. tp_features.fan_ctrl_status_undef = 1;
  2466. }
  2467. } else {
  2468. printk(IBM_ERR
  2469. "ThinkPad ACPI EC access misbehaving, "
  2470. "fan status and control unavailable\n");
  2471. return 1;
  2472. }
  2473. }
  2474. if (sfan_handle) {
  2475. /* 570, 770x-JL */
  2476. fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
  2477. fan_control_commands |=
  2478. TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
  2479. } else {
  2480. if (!gfan_handle) {
  2481. /* gfan without sfan means no fan control */
  2482. /* all other models implement TP EC 0x2f control */
  2483. if (fans_handle) {
  2484. /* X31, X40, X41 */
  2485. fan_control_access_mode =
  2486. TPACPI_FAN_WR_ACPI_FANS;
  2487. fan_control_commands |=
  2488. TPACPI_FAN_CMD_SPEED |
  2489. TPACPI_FAN_CMD_LEVEL |
  2490. TPACPI_FAN_CMD_ENABLE;
  2491. } else {
  2492. fan_control_access_mode = TPACPI_FAN_WR_TPEC;
  2493. fan_control_commands |=
  2494. TPACPI_FAN_CMD_LEVEL |
  2495. TPACPI_FAN_CMD_ENABLE;
  2496. }
  2497. }
  2498. }
  2499. vdbg_printk(TPACPI_DBG_INIT, "fan is %s, modes %d, %d\n",
  2500. str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
  2501. fan_control_access_mode != TPACPI_FAN_WR_NONE),
  2502. fan_status_access_mode, fan_control_access_mode);
  2503. /* update fan_control_desired_level */
  2504. if (fan_status_access_mode != TPACPI_FAN_NONE)
  2505. fan_get_status_safe(NULL);
  2506. if (fan_status_access_mode != TPACPI_FAN_NONE ||
  2507. fan_control_access_mode != TPACPI_FAN_WR_NONE) {
  2508. rc = sysfs_create_group(&tpacpi_pdev->dev.kobj,
  2509. &fan_attr_group);
  2510. if (!(rc < 0))
  2511. rc = driver_create_file(&tpacpi_pdriver.driver,
  2512. &driver_attr_fan_watchdog);
  2513. if (rc < 0)
  2514. return rc;
  2515. return 0;
  2516. } else
  2517. return 1;
  2518. }
  2519. /*
  2520. * Call with fan_mutex held
  2521. */
  2522. static void fan_update_desired_level(u8 status)
  2523. {
  2524. if ((status &
  2525. (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
  2526. if (status > 7)
  2527. fan_control_desired_level = 7;
  2528. else
  2529. fan_control_desired_level = status;
  2530. }
  2531. }
  2532. static int fan_get_status(u8 *status)
  2533. {
  2534. u8 s;
  2535. /* TODO:
  2536. * Add TPACPI_FAN_RD_ACPI_FANS ? */
  2537. switch (fan_status_access_mode) {
  2538. case TPACPI_FAN_RD_ACPI_GFAN:
  2539. /* 570, 600e/x, 770e, 770x */
  2540. if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
  2541. return -EIO;
  2542. if (likely(status))
  2543. *status = s & 0x07;
  2544. break;
  2545. case TPACPI_FAN_RD_TPEC:
  2546. /* all except 570, 600e/x, 770e, 770x */
  2547. if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
  2548. return -EIO;
  2549. if (likely(status))
  2550. *status = s;
  2551. break;
  2552. default:
  2553. return -ENXIO;
  2554. }
  2555. return 0;
  2556. }
  2557. static int fan_get_status_safe(u8 *status)
  2558. {
  2559. int rc;
  2560. u8 s;
  2561. rc = mutex_lock_interruptible(&fan_mutex);
  2562. if (rc < 0)
  2563. return rc;
  2564. rc = fan_get_status(&s);
  2565. if (!rc)
  2566. fan_update_desired_level(s);
  2567. mutex_unlock(&fan_mutex);
  2568. if (status)
  2569. *status = s;
  2570. return rc;
  2571. }
  2572. static void fan_exit(void)
  2573. {
  2574. vdbg_printk(TPACPI_DBG_EXIT, "cancelling any pending fan watchdog tasks\n");
  2575. /* FIXME: can we really do this unconditionally? */
  2576. sysfs_remove_group(&tpacpi_pdev->dev.kobj, &fan_attr_group);
  2577. driver_remove_file(&tpacpi_pdriver.driver, &driver_attr_fan_watchdog);
  2578. cancel_delayed_work(&fan_watchdog_task);
  2579. flush_scheduled_work();
  2580. }
  2581. static int fan_get_speed(unsigned int *speed)
  2582. {
  2583. u8 hi, lo;
  2584. switch (fan_status_access_mode) {
  2585. case TPACPI_FAN_RD_TPEC:
  2586. /* all except 570, 600e/x, 770e, 770x */
  2587. if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
  2588. !acpi_ec_read(fan_rpm_offset + 1, &hi)))
  2589. return -EIO;
  2590. if (likely(speed))
  2591. *speed = (hi << 8) | lo;
  2592. break;
  2593. default:
  2594. return -ENXIO;
  2595. }
  2596. return 0;
  2597. }
  2598. static void fan_watchdog_fire(struct work_struct *ignored)
  2599. {
  2600. int rc;
  2601. printk(IBM_NOTICE "fan watchdog: enabling fan\n");
  2602. rc = fan_set_enable();
  2603. if (rc < 0) {
  2604. printk(IBM_ERR "fan watchdog: error %d while enabling fan, "
  2605. "will try again later...\n", -rc);
  2606. /* reschedule for later */
  2607. fan_watchdog_reset();
  2608. }
  2609. }
  2610. static void fan_watchdog_reset(void)
  2611. {
  2612. static int fan_watchdog_active = 0;
  2613. if (fan_watchdog_active)
  2614. cancel_delayed_work(&fan_watchdog_task);
  2615. if (fan_watchdog_maxinterval > 0) {
  2616. fan_watchdog_active = 1;
  2617. if (!schedule_delayed_work(&fan_watchdog_task,
  2618. msecs_to_jiffies(fan_watchdog_maxinterval
  2619. * 1000))) {
  2620. printk(IBM_ERR "failed to schedule the fan watchdog, "
  2621. "watchdog will not trigger\n");
  2622. }
  2623. } else
  2624. fan_watchdog_active = 0;
  2625. }
  2626. static int fan_set_level(int level)
  2627. {
  2628. switch (fan_control_access_mode) {
  2629. case TPACPI_FAN_WR_ACPI_SFAN:
  2630. if (level >= 0 && level <= 7) {
  2631. if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
  2632. return -EIO;
  2633. } else
  2634. return -EINVAL;
  2635. break;
  2636. case TPACPI_FAN_WR_ACPI_FANS:
  2637. case TPACPI_FAN_WR_TPEC:
  2638. if ((level != TP_EC_FAN_AUTO) &&
  2639. (level != TP_EC_FAN_FULLSPEED) &&
  2640. ((level < 0) || (level > 7)))
  2641. return -EINVAL;
  2642. /* safety net should the EC not support AUTO
  2643. * or FULLSPEED mode bits and just ignore them */
  2644. if (level & TP_EC_FAN_FULLSPEED)
  2645. level |= 7; /* safety min speed 7 */
  2646. else if (level & TP_EC_FAN_FULLSPEED)
  2647. level |= 4; /* safety min speed 4 */
  2648. if (!acpi_ec_write(fan_status_offset, level))
  2649. return -EIO;
  2650. else
  2651. tp_features.fan_ctrl_status_undef = 0;
  2652. break;
  2653. default:
  2654. return -ENXIO;
  2655. }
  2656. return 0;
  2657. }
  2658. static int fan_set_level_safe(int level)
  2659. {
  2660. int rc;
  2661. rc = mutex_lock_interruptible(&fan_mutex);
  2662. if (rc < 0)
  2663. return rc;
  2664. if (level == TPACPI_FAN_LAST_LEVEL)
  2665. level = fan_control_desired_level;
  2666. rc = fan_set_level(level);
  2667. if (!rc)
  2668. fan_update_desired_level(level);
  2669. mutex_unlock(&fan_mutex);
  2670. return rc;
  2671. }
  2672. static int fan_set_enable(void)
  2673. {
  2674. u8 s;
  2675. int rc;
  2676. rc = mutex_lock_interruptible(&fan_mutex);
  2677. if (rc < 0)
  2678. return rc;
  2679. switch (fan_control_access_mode) {
  2680. case TPACPI_FAN_WR_ACPI_FANS:
  2681. case TPACPI_FAN_WR_TPEC:
  2682. rc = fan_get_status(&s);
  2683. if (rc < 0)
  2684. break;
  2685. /* Don't go out of emergency fan mode */
  2686. if (s != 7) {
  2687. s &= 0x07;
  2688. s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
  2689. }
  2690. if (!acpi_ec_write(fan_status_offset, s))
  2691. rc = -EIO;
  2692. else {
  2693. tp_features.fan_ctrl_status_undef = 0;
  2694. rc = 0;
  2695. }
  2696. break;
  2697. case TPACPI_FAN_WR_ACPI_SFAN:
  2698. rc = fan_get_status(&s);
  2699. if (rc < 0)
  2700. break;
  2701. s &= 0x07;
  2702. /* Set fan to at least level 4 */
  2703. s |= 4;
  2704. if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
  2705. rc= -EIO;
  2706. else
  2707. rc = 0;
  2708. break;
  2709. default:
  2710. rc = -ENXIO;
  2711. }
  2712. mutex_unlock(&fan_mutex);
  2713. return rc;
  2714. }
  2715. static int fan_set_disable(void)
  2716. {
  2717. int rc;
  2718. rc = mutex_lock_interruptible(&fan_mutex);
  2719. if (rc < 0)
  2720. return rc;
  2721. rc = 0;
  2722. switch (fan_control_access_mode) {
  2723. case TPACPI_FAN_WR_ACPI_FANS:
  2724. case TPACPI_FAN_WR_TPEC:
  2725. if (!acpi_ec_write(fan_status_offset, 0x00))
  2726. rc = -EIO;
  2727. else {
  2728. fan_control_desired_level = 0;
  2729. tp_features.fan_ctrl_status_undef = 0;
  2730. }
  2731. break;
  2732. case TPACPI_FAN_WR_ACPI_SFAN:
  2733. if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
  2734. rc = -EIO;
  2735. else
  2736. fan_control_desired_level = 0;
  2737. break;
  2738. default:
  2739. rc = -ENXIO;
  2740. }
  2741. mutex_unlock(&fan_mutex);
  2742. return rc;
  2743. }
  2744. static int fan_set_speed(int speed)
  2745. {
  2746. int rc;
  2747. rc = mutex_lock_interruptible(&fan_mutex);
  2748. if (rc < 0)
  2749. return rc;
  2750. rc = 0;
  2751. switch (fan_control_access_mode) {
  2752. case TPACPI_FAN_WR_ACPI_FANS:
  2753. if (speed >= 0 && speed <= 65535) {
  2754. if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
  2755. speed, speed, speed))
  2756. rc = -EIO;
  2757. } else
  2758. rc = -EINVAL;
  2759. break;
  2760. default:
  2761. rc = -ENXIO;
  2762. }
  2763. mutex_unlock(&fan_mutex);
  2764. return rc;
  2765. }
  2766. static int fan_read(char *p)
  2767. {
  2768. int len = 0;
  2769. int rc;
  2770. u8 status;
  2771. unsigned int speed = 0;
  2772. switch (fan_status_access_mode) {
  2773. case TPACPI_FAN_RD_ACPI_GFAN:
  2774. /* 570, 600e/x, 770e, 770x */
  2775. if ((rc = fan_get_status_safe(&status)) < 0)
  2776. return rc;
  2777. len += sprintf(p + len, "status:\t\t%s\n"
  2778. "level:\t\t%d\n",
  2779. (status != 0) ? "enabled" : "disabled", status);
  2780. break;
  2781. case TPACPI_FAN_RD_TPEC:
  2782. /* all except 570, 600e/x, 770e, 770x */
  2783. if ((rc = fan_get_status_safe(&status)) < 0)
  2784. return rc;
  2785. if (unlikely(tp_features.fan_ctrl_status_undef)) {
  2786. if (status != fan_control_initial_status)
  2787. tp_features.fan_ctrl_status_undef = 0;
  2788. else
  2789. /* Return most likely status. In fact, it
  2790. * might be the only possible status */
  2791. status = TP_EC_FAN_AUTO;
  2792. }
  2793. len += sprintf(p + len, "status:\t\t%s\n",
  2794. (status != 0) ? "enabled" : "disabled");
  2795. if ((rc = fan_get_speed(&speed)) < 0)
  2796. return rc;
  2797. len += sprintf(p + len, "speed:\t\t%d\n", speed);
  2798. if (status & TP_EC_FAN_FULLSPEED)
  2799. /* Disengaged mode takes precedence */
  2800. len += sprintf(p + len, "level:\t\tdisengaged\n");
  2801. else if (status & TP_EC_FAN_AUTO)
  2802. len += sprintf(p + len, "level:\t\tauto\n");
  2803. else
  2804. len += sprintf(p + len, "level:\t\t%d\n", status);
  2805. break;
  2806. case TPACPI_FAN_NONE:
  2807. default:
  2808. len += sprintf(p + len, "status:\t\tnot supported\n");
  2809. }
  2810. if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
  2811. len += sprintf(p + len, "commands:\tlevel <level>");
  2812. switch (fan_control_access_mode) {
  2813. case TPACPI_FAN_WR_ACPI_SFAN:
  2814. len += sprintf(p + len, " (<level> is 0-7)\n");
  2815. break;
  2816. default:
  2817. len += sprintf(p + len, " (<level> is 0-7, "
  2818. "auto, disengaged, full-speed)\n");
  2819. break;
  2820. }
  2821. }
  2822. if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
  2823. len += sprintf(p + len, "commands:\tenable, disable\n"
  2824. "commands:\twatchdog <timeout> (<timeout> is 0 (off), "
  2825. "1-120 (seconds))\n");
  2826. if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
  2827. len += sprintf(p + len, "commands:\tspeed <speed>"
  2828. " (<speed> is 0-65535)\n");
  2829. return len;
  2830. }
  2831. static int fan_write_cmd_level(const char *cmd, int *rc)
  2832. {
  2833. int level;
  2834. if (strlencmp(cmd, "level auto") == 0)
  2835. level = TP_EC_FAN_AUTO;
  2836. else if ((strlencmp(cmd, "level disengaged") == 0) |
  2837. (strlencmp(cmd, "level full-speed") == 0))
  2838. level = TP_EC_FAN_FULLSPEED;
  2839. else if (sscanf(cmd, "level %d", &level) != 1)
  2840. return 0;
  2841. if ((*rc = fan_set_level_safe(level)) == -ENXIO)
  2842. printk(IBM_ERR "level command accepted for unsupported "
  2843. "access mode %d", fan_control_access_mode);
  2844. return 1;
  2845. }
  2846. static int fan_write_cmd_enable(const char *cmd, int *rc)
  2847. {
  2848. if (strlencmp(cmd, "enable") != 0)
  2849. return 0;
  2850. if ((*rc = fan_set_enable()) == -ENXIO)
  2851. printk(IBM_ERR "enable command accepted for unsupported "
  2852. "access mode %d", fan_control_access_mode);
  2853. return 1;
  2854. }
  2855. static int fan_write_cmd_disable(const char *cmd, int *rc)
  2856. {
  2857. if (strlencmp(cmd, "disable") != 0)
  2858. return 0;
  2859. if ((*rc = fan_set_disable()) == -ENXIO)
  2860. printk(IBM_ERR "disable command accepted for unsupported "
  2861. "access mode %d", fan_control_access_mode);
  2862. return 1;
  2863. }
  2864. static int fan_write_cmd_speed(const char *cmd, int *rc)
  2865. {
  2866. int speed;
  2867. /* TODO:
  2868. * Support speed <low> <medium> <high> ? */
  2869. if (sscanf(cmd, "speed %d", &speed) != 1)
  2870. return 0;
  2871. if ((*rc = fan_set_speed(speed)) == -ENXIO)
  2872. printk(IBM_ERR "speed command accepted for unsupported "
  2873. "access mode %d", fan_control_access_mode);
  2874. return 1;
  2875. }
  2876. static int fan_write_cmd_watchdog(const char *cmd, int *rc)
  2877. {
  2878. int interval;
  2879. if (sscanf(cmd, "watchdog %d", &interval) != 1)
  2880. return 0;
  2881. if (interval < 0 || interval > 120)
  2882. *rc = -EINVAL;
  2883. else
  2884. fan_watchdog_maxinterval = interval;
  2885. return 1;
  2886. }
  2887. static int fan_write(char *buf)
  2888. {
  2889. char *cmd;
  2890. int rc = 0;
  2891. while (!rc && (cmd = next_cmd(&buf))) {
  2892. if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
  2893. fan_write_cmd_level(cmd, &rc)) &&
  2894. !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
  2895. (fan_write_cmd_enable(cmd, &rc) ||
  2896. fan_write_cmd_disable(cmd, &rc) ||
  2897. fan_write_cmd_watchdog(cmd, &rc))) &&
  2898. !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
  2899. fan_write_cmd_speed(cmd, &rc))
  2900. )
  2901. rc = -EINVAL;
  2902. else if (!rc)
  2903. fan_watchdog_reset();
  2904. }
  2905. return rc;
  2906. }
  2907. static struct ibm_struct fan_driver_data = {
  2908. .name = "fan",
  2909. .read = fan_read,
  2910. .write = fan_write,
  2911. .exit = fan_exit,
  2912. .flags.experimental = 1,
  2913. };
  2914. /****************************************************************************
  2915. ****************************************************************************
  2916. *
  2917. * Infrastructure
  2918. *
  2919. ****************************************************************************
  2920. ****************************************************************************/
  2921. /* /proc support */
  2922. static struct proc_dir_entry *proc_dir = NULL;
  2923. /* Subdriver registry */
  2924. static LIST_HEAD(tpacpi_all_drivers);
  2925. /*
  2926. * Module and infrastructure proble, init and exit handling
  2927. */
  2928. #ifdef CONFIG_THINKPAD_ACPI_DEBUG
  2929. static const char * __init str_supported(int is_supported)
  2930. {
  2931. static char text_unsupported[] __initdata = "not supported";
  2932. return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
  2933. }
  2934. #endif /* CONFIG_THINKPAD_ACPI_DEBUG */
  2935. static int __init ibm_init(struct ibm_init_struct *iibm)
  2936. {
  2937. int ret;
  2938. struct ibm_struct *ibm = iibm->data;
  2939. struct proc_dir_entry *entry;
  2940. BUG_ON(ibm == NULL);
  2941. INIT_LIST_HEAD(&ibm->all_drivers);
  2942. if (ibm->flags.experimental && !experimental)
  2943. return 0;
  2944. dbg_printk(TPACPI_DBG_INIT,
  2945. "probing for %s\n", ibm->name);
  2946. if (iibm->init) {
  2947. ret = iibm->init(iibm);
  2948. if (ret > 0)
  2949. return 0; /* probe failed */
  2950. if (ret)
  2951. return ret;
  2952. ibm->flags.init_called = 1;
  2953. }
  2954. if (ibm->acpi) {
  2955. if (ibm->acpi->hid) {
  2956. ret = register_tpacpi_subdriver(ibm);
  2957. if (ret)
  2958. goto err_out;
  2959. }
  2960. if (ibm->acpi->notify) {
  2961. ret = setup_acpi_notify(ibm);
  2962. if (ret == -ENODEV) {
  2963. printk(IBM_NOTICE "disabling subdriver %s\n",
  2964. ibm->name);
  2965. ret = 0;
  2966. goto err_out;
  2967. }
  2968. if (ret < 0)
  2969. goto err_out;
  2970. }
  2971. }
  2972. dbg_printk(TPACPI_DBG_INIT,
  2973. "%s installed\n", ibm->name);
  2974. if (ibm->read) {
  2975. entry = create_proc_entry(ibm->name,
  2976. S_IFREG | S_IRUGO | S_IWUSR,
  2977. proc_dir);
  2978. if (!entry) {
  2979. printk(IBM_ERR "unable to create proc entry %s\n",
  2980. ibm->name);
  2981. ret = -ENODEV;
  2982. goto err_out;
  2983. }
  2984. entry->owner = THIS_MODULE;
  2985. entry->data = ibm;
  2986. entry->read_proc = &dispatch_procfs_read;
  2987. if (ibm->write)
  2988. entry->write_proc = &dispatch_procfs_write;
  2989. ibm->flags.proc_created = 1;
  2990. }
  2991. list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
  2992. return 0;
  2993. err_out:
  2994. dbg_printk(TPACPI_DBG_INIT,
  2995. "%s: at error exit path with result %d\n",
  2996. ibm->name, ret);
  2997. ibm_exit(ibm);
  2998. return (ret < 0)? ret : 0;
  2999. }
  3000. static void ibm_exit(struct ibm_struct *ibm)
  3001. {
  3002. dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
  3003. list_del_init(&ibm->all_drivers);
  3004. if (ibm->flags.acpi_notify_installed) {
  3005. dbg_printk(TPACPI_DBG_EXIT,
  3006. "%s: acpi_remove_notify_handler\n", ibm->name);
  3007. BUG_ON(!ibm->acpi);
  3008. acpi_remove_notify_handler(*ibm->acpi->handle,
  3009. ibm->acpi->type,
  3010. dispatch_acpi_notify);
  3011. ibm->flags.acpi_notify_installed = 0;
  3012. ibm->flags.acpi_notify_installed = 0;
  3013. }
  3014. if (ibm->flags.proc_created) {
  3015. dbg_printk(TPACPI_DBG_EXIT,
  3016. "%s: remove_proc_entry\n", ibm->name);
  3017. remove_proc_entry(ibm->name, proc_dir);
  3018. ibm->flags.proc_created = 0;
  3019. }
  3020. if (ibm->flags.acpi_driver_registered) {
  3021. dbg_printk(TPACPI_DBG_EXIT,
  3022. "%s: acpi_bus_unregister_driver\n", ibm->name);
  3023. BUG_ON(!ibm->acpi);
  3024. acpi_bus_unregister_driver(ibm->acpi->driver);
  3025. kfree(ibm->acpi->driver);
  3026. ibm->acpi->driver = NULL;
  3027. ibm->flags.acpi_driver_registered = 0;
  3028. }
  3029. if (ibm->flags.init_called && ibm->exit) {
  3030. ibm->exit();
  3031. ibm->flags.init_called = 0;
  3032. }
  3033. dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
  3034. }
  3035. /* Probing */
  3036. static char *ibm_thinkpad_ec_found = NULL;
  3037. static char* __init check_dmi_for_ec(void)
  3038. {
  3039. struct dmi_device *dev = NULL;
  3040. char ec_fw_string[18];
  3041. /*
  3042. * ThinkPad T23 or newer, A31 or newer, R50e or newer,
  3043. * X32 or newer, all Z series; Some models must have an
  3044. * up-to-date BIOS or they will not be detected.
  3045. *
  3046. * See http://thinkwiki.org/wiki/List_of_DMI_IDs
  3047. */
  3048. while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
  3049. if (sscanf(dev->name,
  3050. "IBM ThinkPad Embedded Controller -[%17c",
  3051. ec_fw_string) == 1) {
  3052. ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
  3053. ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
  3054. return kstrdup(ec_fw_string, GFP_KERNEL);
  3055. }
  3056. }
  3057. return NULL;
  3058. }
  3059. static int __init probe_for_thinkpad(void)
  3060. {
  3061. int is_thinkpad;
  3062. if (acpi_disabled)
  3063. return -ENODEV;
  3064. /*
  3065. * Non-ancient models have better DMI tagging, but very old models
  3066. * don't.
  3067. */
  3068. is_thinkpad = dmi_name_in_vendors("ThinkPad");
  3069. /* ec is required because many other handles are relative to it */
  3070. IBM_ACPIHANDLE_INIT(ec);
  3071. if (!ec_handle) {
  3072. if (is_thinkpad)
  3073. printk(IBM_ERR
  3074. "Not yet supported ThinkPad detected!\n");
  3075. return -ENODEV;
  3076. }
  3077. /*
  3078. * Risks a regression on very old machines, but reduces potential
  3079. * false positives a damn great deal
  3080. */
  3081. if (!is_thinkpad)
  3082. is_thinkpad = dmi_name_in_vendors("IBM");
  3083. if (!is_thinkpad && !force_load)
  3084. return -ENODEV;
  3085. return 0;
  3086. }
  3087. /* Module init, exit, parameters */
  3088. static struct ibm_init_struct ibms_init[] __initdata = {
  3089. {
  3090. .init = thinkpad_acpi_driver_init,
  3091. .data = &thinkpad_acpi_driver_data,
  3092. },
  3093. {
  3094. .init = hotkey_init,
  3095. .data = &hotkey_driver_data,
  3096. },
  3097. {
  3098. .init = bluetooth_init,
  3099. .data = &bluetooth_driver_data,
  3100. },
  3101. {
  3102. .init = wan_init,
  3103. .data = &wan_driver_data,
  3104. },
  3105. {
  3106. .init = video_init,
  3107. .data = &video_driver_data,
  3108. },
  3109. {
  3110. .init = light_init,
  3111. .data = &light_driver_data,
  3112. },
  3113. #ifdef CONFIG_THINKPAD_ACPI_DOCK
  3114. {
  3115. .init = dock_init,
  3116. .data = &dock_driver_data[0],
  3117. },
  3118. {
  3119. .data = &dock_driver_data[1],
  3120. },
  3121. #endif
  3122. #ifdef CONFIG_THINKPAD_ACPI_BAY
  3123. {
  3124. .init = bay_init,
  3125. .data = &bay_driver_data,
  3126. },
  3127. #endif
  3128. {
  3129. .init = cmos_init,
  3130. .data = &cmos_driver_data,
  3131. },
  3132. {
  3133. .init = led_init,
  3134. .data = &led_driver_data,
  3135. },
  3136. {
  3137. .init = beep_init,
  3138. .data = &beep_driver_data,
  3139. },
  3140. {
  3141. .init = thermal_init,
  3142. .data = &thermal_driver_data,
  3143. },
  3144. {
  3145. .data = &ecdump_driver_data,
  3146. },
  3147. {
  3148. .init = brightness_init,
  3149. .data = &brightness_driver_data,
  3150. },
  3151. {
  3152. .data = &volume_driver_data,
  3153. },
  3154. {
  3155. .init = fan_init,
  3156. .data = &fan_driver_data,
  3157. },
  3158. };
  3159. static int __init set_ibm_param(const char *val, struct kernel_param *kp)
  3160. {
  3161. unsigned int i;
  3162. struct ibm_struct *ibm;
  3163. for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
  3164. ibm = ibms_init[i].data;
  3165. BUG_ON(ibm == NULL);
  3166. if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
  3167. if (strlen(val) > sizeof(ibms_init[i].param) - 2)
  3168. return -ENOSPC;
  3169. strcpy(ibms_init[i].param, val);
  3170. strcat(ibms_init[i].param, ",");
  3171. return 0;
  3172. }
  3173. }
  3174. return -EINVAL;
  3175. }
  3176. static int experimental;
  3177. module_param(experimental, int, 0);
  3178. static u32 dbg_level;
  3179. module_param_named(debug, dbg_level, uint, 0);
  3180. static int force_load;
  3181. module_param(force_load, int, 0);
  3182. #define IBM_PARAM(feature) \
  3183. module_param_call(feature, set_ibm_param, NULL, NULL, 0)
  3184. IBM_PARAM(hotkey);
  3185. IBM_PARAM(bluetooth);
  3186. IBM_PARAM(video);
  3187. IBM_PARAM(light);
  3188. #ifdef CONFIG_THINKPAD_ACPI_DOCK
  3189. IBM_PARAM(dock);
  3190. #endif
  3191. #ifdef CONFIG_THINKPAD_ACPI_BAY
  3192. IBM_PARAM(bay);
  3193. #endif /* CONFIG_THINKPAD_ACPI_BAY */
  3194. IBM_PARAM(cmos);
  3195. IBM_PARAM(led);
  3196. IBM_PARAM(beep);
  3197. IBM_PARAM(ecdump);
  3198. IBM_PARAM(brightness);
  3199. IBM_PARAM(volume);
  3200. IBM_PARAM(fan);
  3201. static int __init thinkpad_acpi_module_init(void)
  3202. {
  3203. int ret, i;
  3204. /* Driver-level probe */
  3205. ret = probe_for_thinkpad();
  3206. if (ret)
  3207. return ret;
  3208. /* Driver initialization */
  3209. ibm_thinkpad_ec_found = check_dmi_for_ec();
  3210. IBM_ACPIHANDLE_INIT(ecrd);
  3211. IBM_ACPIHANDLE_INIT(ecwr);
  3212. proc_dir = proc_mkdir(IBM_PROC_DIR, acpi_root_dir);
  3213. if (!proc_dir) {
  3214. printk(IBM_ERR "unable to create proc dir " IBM_PROC_DIR);
  3215. thinkpad_acpi_module_exit();
  3216. return -ENODEV;
  3217. }
  3218. proc_dir->owner = THIS_MODULE;
  3219. ret = platform_driver_register(&tpacpi_pdriver);
  3220. if (ret) {
  3221. printk(IBM_ERR "unable to register platform driver\n");
  3222. thinkpad_acpi_module_exit();
  3223. return ret;
  3224. }
  3225. ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
  3226. if (ret) {
  3227. printk(IBM_ERR "unable to create sysfs driver attributes\n");
  3228. thinkpad_acpi_module_exit();
  3229. return ret;
  3230. }
  3231. /* Device initialization */
  3232. tpacpi_pdev = platform_device_register_simple(IBM_DRVR_NAME, -1,
  3233. NULL, 0);
  3234. if (IS_ERR(tpacpi_pdev)) {
  3235. ret = PTR_ERR(tpacpi_pdev);
  3236. tpacpi_pdev = NULL;
  3237. printk(IBM_ERR "unable to register platform device\n");
  3238. thinkpad_acpi_module_exit();
  3239. return ret;
  3240. }
  3241. tpacpi_hwmon = hwmon_device_register(&tpacpi_pdev->dev);
  3242. if (IS_ERR(tpacpi_hwmon)) {
  3243. ret = PTR_ERR(tpacpi_hwmon);
  3244. tpacpi_hwmon = NULL;
  3245. printk(IBM_ERR "unable to register hwmon device\n");
  3246. thinkpad_acpi_module_exit();
  3247. return ret;
  3248. }
  3249. for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
  3250. ret = ibm_init(&ibms_init[i]);
  3251. if (ret >= 0 && *ibms_init[i].param)
  3252. ret = ibms_init[i].data->write(ibms_init[i].param);
  3253. if (ret < 0) {
  3254. thinkpad_acpi_module_exit();
  3255. return ret;
  3256. }
  3257. }
  3258. return 0;
  3259. }
  3260. static void thinkpad_acpi_module_exit(void)
  3261. {
  3262. struct ibm_struct *ibm, *itmp;
  3263. list_for_each_entry_safe_reverse(ibm, itmp,
  3264. &tpacpi_all_drivers,
  3265. all_drivers) {
  3266. ibm_exit(ibm);
  3267. }
  3268. dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
  3269. if (tpacpi_hwmon)
  3270. hwmon_device_unregister(tpacpi_hwmon);
  3271. if (tpacpi_pdev)
  3272. platform_device_unregister(tpacpi_pdev);
  3273. tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
  3274. platform_driver_unregister(&tpacpi_pdriver);
  3275. if (proc_dir)
  3276. remove_proc_entry(IBM_PROC_DIR, acpi_root_dir);
  3277. kfree(ibm_thinkpad_ec_found);
  3278. }
  3279. module_init(thinkpad_acpi_module_init);
  3280. module_exit(thinkpad_acpi_module_exit);