thinkpad_acpi.c 108 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597
  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 0x010000
  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. /*
  90. * DMI matching for module autoloading
  91. *
  92. * See http://thinkwiki.org/wiki/List_of_DMI_IDs
  93. * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
  94. *
  95. * Only models listed in thinkwiki will be supported, so add yours
  96. * if it is not there yet.
  97. */
  98. #define IBM_BIOS_MODULE_ALIAS(__type) \
  99. MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW")
  100. /* Non-ancient thinkpads */
  101. MODULE_ALIAS("dmi:bvnIBM:*:svnIBM:*:pvrThinkPad*:rvnIBM:*");
  102. MODULE_ALIAS("dmi:bvnLENOVO:*:svnLENOVO:*:pvrThinkPad*:rvnLENOVO:*");
  103. /* Ancient thinkpad BIOSes have to be identified by
  104. * BIOS type or model number, and there are far less
  105. * BIOS types than model numbers... */
  106. IBM_BIOS_MODULE_ALIAS("I[B,D,H,I,M,N,O,T,W,V,Y,Z]");
  107. IBM_BIOS_MODULE_ALIAS("1[0,3,6,8,A-G,I,K,M-P,S,T]");
  108. IBM_BIOS_MODULE_ALIAS("K[U,X-Z]");
  109. #define __unused __attribute__ ((unused))
  110. /****************************************************************************
  111. ****************************************************************************
  112. *
  113. * ACPI Helpers and device model
  114. *
  115. ****************************************************************************
  116. ****************************************************************************/
  117. /*************************************************************************
  118. * ACPI basic handles
  119. */
  120. static acpi_handle root_handle;
  121. #define IBM_HANDLE(object, parent, paths...) \
  122. static acpi_handle object##_handle; \
  123. static acpi_handle *object##_parent = &parent##_handle; \
  124. static char *object##_path; \
  125. static char *object##_paths[] = { paths }
  126. IBM_HANDLE(ec, root, "\\_SB.PCI0.ISA.EC0", /* 240, 240x */
  127. "\\_SB.PCI.ISA.EC", /* 570 */
  128. "\\_SB.PCI0.ISA0.EC0", /* 600e/x, 770e, 770x */
  129. "\\_SB.PCI0.ISA.EC", /* A21e, A2xm/p, T20-22, X20-21 */
  130. "\\_SB.PCI0.AD4S.EC0", /* i1400, R30 */
  131. "\\_SB.PCI0.ICH3.EC0", /* R31 */
  132. "\\_SB.PCI0.LPC.EC", /* all others */
  133. );
  134. IBM_HANDLE(ecrd, ec, "ECRD"); /* 570 */
  135. IBM_HANDLE(ecwr, ec, "ECWR"); /* 570 */
  136. /*************************************************************************
  137. * Misc ACPI handles
  138. */
  139. IBM_HANDLE(cmos, root, "\\UCMS", /* R50, R50e, R50p, R51, T4x, X31, X40 */
  140. "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
  141. "\\CMS", /* R40, R40e */
  142. ); /* all others */
  143. IBM_HANDLE(hkey, ec, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
  144. "^HKEY", /* R30, R31 */
  145. "HKEY", /* all others */
  146. ); /* 570 */
  147. /*************************************************************************
  148. * ACPI helpers
  149. */
  150. static int acpi_evalf(acpi_handle handle,
  151. void *res, char *method, char *fmt, ...)
  152. {
  153. char *fmt0 = fmt;
  154. struct acpi_object_list params;
  155. union acpi_object in_objs[IBM_MAX_ACPI_ARGS];
  156. struct acpi_buffer result, *resultp;
  157. union acpi_object out_obj;
  158. acpi_status status;
  159. va_list ap;
  160. char res_type;
  161. int success;
  162. int quiet;
  163. if (!*fmt) {
  164. printk(IBM_ERR "acpi_evalf() called with empty format\n");
  165. return 0;
  166. }
  167. if (*fmt == 'q') {
  168. quiet = 1;
  169. fmt++;
  170. } else
  171. quiet = 0;
  172. res_type = *(fmt++);
  173. params.count = 0;
  174. params.pointer = &in_objs[0];
  175. va_start(ap, fmt);
  176. while (*fmt) {
  177. char c = *(fmt++);
  178. switch (c) {
  179. case 'd': /* int */
  180. in_objs[params.count].integer.value = va_arg(ap, int);
  181. in_objs[params.count++].type = ACPI_TYPE_INTEGER;
  182. break;
  183. /* add more types as needed */
  184. default:
  185. printk(IBM_ERR "acpi_evalf() called "
  186. "with invalid format character '%c'\n", c);
  187. return 0;
  188. }
  189. }
  190. va_end(ap);
  191. if (res_type != 'v') {
  192. result.length = sizeof(out_obj);
  193. result.pointer = &out_obj;
  194. resultp = &result;
  195. } else
  196. resultp = NULL;
  197. status = acpi_evaluate_object(handle, method, &params, resultp);
  198. switch (res_type) {
  199. case 'd': /* int */
  200. if (res)
  201. *(int *)res = out_obj.integer.value;
  202. success = status == AE_OK && out_obj.type == ACPI_TYPE_INTEGER;
  203. break;
  204. case 'v': /* void */
  205. success = status == AE_OK;
  206. break;
  207. /* add more types as needed */
  208. default:
  209. printk(IBM_ERR "acpi_evalf() called "
  210. "with invalid format character '%c'\n", res_type);
  211. return 0;
  212. }
  213. if (!success && !quiet)
  214. printk(IBM_ERR "acpi_evalf(%s, %s, ...) failed: %d\n",
  215. method, fmt0, status);
  216. return success;
  217. }
  218. static void __unused acpi_print_int(acpi_handle handle, char *method)
  219. {
  220. int i;
  221. if (acpi_evalf(handle, &i, method, "d"))
  222. printk(IBM_INFO "%s = 0x%x\n", method, i);
  223. else
  224. printk(IBM_ERR "error calling %s\n", method);
  225. }
  226. static int acpi_ec_read(int i, u8 * p)
  227. {
  228. int v;
  229. if (ecrd_handle) {
  230. if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
  231. return 0;
  232. *p = v;
  233. } else {
  234. if (ec_read(i, p) < 0)
  235. return 0;
  236. }
  237. return 1;
  238. }
  239. static int acpi_ec_write(int i, u8 v)
  240. {
  241. if (ecwr_handle) {
  242. if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
  243. return 0;
  244. } else {
  245. if (ec_write(i, v) < 0)
  246. return 0;
  247. }
  248. return 1;
  249. }
  250. static int _sta(acpi_handle handle)
  251. {
  252. int status;
  253. if (!handle || !acpi_evalf(handle, &status, "_STA", "d"))
  254. status = 0;
  255. return status;
  256. }
  257. static int issue_thinkpad_cmos_command(int cmos_cmd)
  258. {
  259. if (!cmos_handle)
  260. return -ENXIO;
  261. if (!acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd))
  262. return -EIO;
  263. return 0;
  264. }
  265. /*************************************************************************
  266. * ACPI device model
  267. */
  268. static void drv_acpi_handle_init(char *name,
  269. acpi_handle *handle, acpi_handle parent,
  270. char **paths, int num_paths, char **path)
  271. {
  272. int i;
  273. acpi_status status;
  274. vdbg_printk(TPACPI_DBG_INIT, "trying to locate ACPI handle for %s\n",
  275. name);
  276. for (i = 0; i < num_paths; i++) {
  277. status = acpi_get_handle(parent, paths[i], handle);
  278. if (ACPI_SUCCESS(status)) {
  279. *path = paths[i];
  280. dbg_printk(TPACPI_DBG_INIT,
  281. "Found ACPI handle %s for %s\n",
  282. *path, name);
  283. return;
  284. }
  285. }
  286. vdbg_printk(TPACPI_DBG_INIT, "ACPI handle for %s not found\n",
  287. name);
  288. *handle = NULL;
  289. }
  290. static void dispatch_acpi_notify(acpi_handle handle, u32 event, void *data)
  291. {
  292. struct ibm_struct *ibm = data;
  293. if (!ibm || !ibm->acpi || !ibm->acpi->notify)
  294. return;
  295. ibm->acpi->notify(ibm, event);
  296. }
  297. static int __init setup_acpi_notify(struct ibm_struct *ibm)
  298. {
  299. acpi_status status;
  300. int rc;
  301. BUG_ON(!ibm->acpi);
  302. if (!*ibm->acpi->handle)
  303. return 0;
  304. vdbg_printk(TPACPI_DBG_INIT,
  305. "setting up ACPI notify for %s\n", ibm->name);
  306. rc = acpi_bus_get_device(*ibm->acpi->handle, &ibm->acpi->device);
  307. if (rc < 0) {
  308. printk(IBM_ERR "acpi_bus_get_device(%s) failed: %d\n",
  309. ibm->name, rc);
  310. return -ENODEV;
  311. }
  312. acpi_driver_data(ibm->acpi->device) = ibm;
  313. sprintf(acpi_device_class(ibm->acpi->device), "%s/%s",
  314. IBM_ACPI_EVENT_PREFIX,
  315. ibm->name);
  316. status = acpi_install_notify_handler(*ibm->acpi->handle,
  317. ibm->acpi->type, dispatch_acpi_notify, ibm);
  318. if (ACPI_FAILURE(status)) {
  319. if (status == AE_ALREADY_EXISTS) {
  320. printk(IBM_NOTICE "another device driver is already handling %s events\n",
  321. ibm->name);
  322. } else {
  323. printk(IBM_ERR "acpi_install_notify_handler(%s) failed: %d\n",
  324. ibm->name, status);
  325. }
  326. return -ENODEV;
  327. }
  328. ibm->flags.acpi_notify_installed = 1;
  329. return 0;
  330. }
  331. static int __init tpacpi_device_add(struct acpi_device *device)
  332. {
  333. return 0;
  334. }
  335. static int __init register_tpacpi_subdriver(struct ibm_struct *ibm)
  336. {
  337. int rc;
  338. dbg_printk(TPACPI_DBG_INIT,
  339. "registering %s as an ACPI driver\n", ibm->name);
  340. BUG_ON(!ibm->acpi);
  341. ibm->acpi->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
  342. if (!ibm->acpi->driver) {
  343. printk(IBM_ERR "kzalloc(ibm->driver) failed\n");
  344. return -ENOMEM;
  345. }
  346. sprintf(ibm->acpi->driver->name, "%s_%s", IBM_NAME, ibm->name);
  347. ibm->acpi->driver->ids = ibm->acpi->hid;
  348. ibm->acpi->driver->ops.add = &tpacpi_device_add;
  349. rc = acpi_bus_register_driver(ibm->acpi->driver);
  350. if (rc < 0) {
  351. printk(IBM_ERR "acpi_bus_register_driver(%s) failed: %d\n",
  352. ibm->acpi->hid, rc);
  353. kfree(ibm->acpi->driver);
  354. ibm->acpi->driver = NULL;
  355. } else if (!rc)
  356. ibm->flags.acpi_driver_registered = 1;
  357. return rc;
  358. }
  359. /****************************************************************************
  360. ****************************************************************************
  361. *
  362. * Procfs Helpers
  363. *
  364. ****************************************************************************
  365. ****************************************************************************/
  366. static int dispatch_procfs_read(char *page, char **start, off_t off,
  367. int count, int *eof, void *data)
  368. {
  369. struct ibm_struct *ibm = data;
  370. int len;
  371. if (!ibm || !ibm->read)
  372. return -EINVAL;
  373. len = ibm->read(page);
  374. if (len < 0)
  375. return len;
  376. if (len <= off + count)
  377. *eof = 1;
  378. *start = page + off;
  379. len -= off;
  380. if (len > count)
  381. len = count;
  382. if (len < 0)
  383. len = 0;
  384. return len;
  385. }
  386. static int dispatch_procfs_write(struct file *file,
  387. const char __user * userbuf,
  388. unsigned long count, void *data)
  389. {
  390. struct ibm_struct *ibm = data;
  391. char *kernbuf;
  392. int ret;
  393. if (!ibm || !ibm->write)
  394. return -EINVAL;
  395. kernbuf = kmalloc(count + 2, GFP_KERNEL);
  396. if (!kernbuf)
  397. return -ENOMEM;
  398. if (copy_from_user(kernbuf, userbuf, count)) {
  399. kfree(kernbuf);
  400. return -EFAULT;
  401. }
  402. kernbuf[count] = 0;
  403. strcat(kernbuf, ",");
  404. ret = ibm->write(kernbuf);
  405. if (ret == 0)
  406. ret = count;
  407. kfree(kernbuf);
  408. return ret;
  409. }
  410. static char *next_cmd(char **cmds)
  411. {
  412. char *start = *cmds;
  413. char *end;
  414. while ((end = strchr(start, ',')) && end == start)
  415. start = end + 1;
  416. if (!end)
  417. return NULL;
  418. *end = 0;
  419. *cmds = end + 1;
  420. return start;
  421. }
  422. /****************************************************************************
  423. ****************************************************************************
  424. *
  425. * Device model: input, hwmon and platform
  426. *
  427. ****************************************************************************
  428. ****************************************************************************/
  429. static struct platform_device *tpacpi_pdev;
  430. static struct class_device *tpacpi_hwmon;
  431. static struct input_dev *tpacpi_inputdev;
  432. static int tpacpi_resume_handler(struct platform_device *pdev)
  433. {
  434. struct ibm_struct *ibm, *itmp;
  435. list_for_each_entry_safe(ibm, itmp,
  436. &tpacpi_all_drivers,
  437. all_drivers) {
  438. if (ibm->resume)
  439. (ibm->resume)();
  440. }
  441. return 0;
  442. }
  443. static struct platform_driver tpacpi_pdriver = {
  444. .driver = {
  445. .name = IBM_DRVR_NAME,
  446. .owner = THIS_MODULE,
  447. },
  448. .resume = tpacpi_resume_handler,
  449. };
  450. /*************************************************************************
  451. * thinkpad-acpi driver attributes
  452. */
  453. /* interface_version --------------------------------------------------- */
  454. static ssize_t tpacpi_driver_interface_version_show(
  455. struct device_driver *drv,
  456. char *buf)
  457. {
  458. return snprintf(buf, PAGE_SIZE, "0x%08x\n", TPACPI_SYSFS_VERSION);
  459. }
  460. static DRIVER_ATTR(interface_version, S_IRUGO,
  461. tpacpi_driver_interface_version_show, NULL);
  462. /* debug_level --------------------------------------------------------- */
  463. static ssize_t tpacpi_driver_debug_show(struct device_driver *drv,
  464. char *buf)
  465. {
  466. return snprintf(buf, PAGE_SIZE, "0x%04x\n", dbg_level);
  467. }
  468. static ssize_t tpacpi_driver_debug_store(struct device_driver *drv,
  469. const char *buf, size_t count)
  470. {
  471. unsigned long t;
  472. if (parse_strtoul(buf, 0xffff, &t))
  473. return -EINVAL;
  474. dbg_level = t;
  475. return count;
  476. }
  477. static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
  478. tpacpi_driver_debug_show, tpacpi_driver_debug_store);
  479. /* version ------------------------------------------------------------- */
  480. static ssize_t tpacpi_driver_version_show(struct device_driver *drv,
  481. char *buf)
  482. {
  483. return snprintf(buf, PAGE_SIZE, "%s v%s\n", IBM_DESC, IBM_VERSION);
  484. }
  485. static DRIVER_ATTR(version, S_IRUGO,
  486. tpacpi_driver_version_show, NULL);
  487. /* --------------------------------------------------------------------- */
  488. static struct driver_attribute* tpacpi_driver_attributes[] = {
  489. &driver_attr_debug_level, &driver_attr_version,
  490. &driver_attr_interface_version,
  491. };
  492. static int __init tpacpi_create_driver_attributes(struct device_driver *drv)
  493. {
  494. int i, res;
  495. i = 0;
  496. res = 0;
  497. while (!res && i < ARRAY_SIZE(tpacpi_driver_attributes)) {
  498. res = driver_create_file(drv, tpacpi_driver_attributes[i]);
  499. i++;
  500. }
  501. return res;
  502. }
  503. static void tpacpi_remove_driver_attributes(struct device_driver *drv)
  504. {
  505. int i;
  506. for(i = 0; i < ARRAY_SIZE(tpacpi_driver_attributes); i++)
  507. driver_remove_file(drv, tpacpi_driver_attributes[i]);
  508. }
  509. /*************************************************************************
  510. * sysfs support helpers
  511. */
  512. struct attribute_set_obj {
  513. struct attribute_set s;
  514. struct attribute *a;
  515. } __attribute__((packed));
  516. static struct attribute_set *create_attr_set(unsigned int max_members,
  517. const char* name)
  518. {
  519. struct attribute_set_obj *sobj;
  520. if (max_members == 0)
  521. return NULL;
  522. /* Allocates space for implicit NULL at the end too */
  523. sobj = kzalloc(sizeof(struct attribute_set_obj) +
  524. max_members * sizeof(struct attribute *),
  525. GFP_KERNEL);
  526. if (!sobj)
  527. return NULL;
  528. sobj->s.max_members = max_members;
  529. sobj->s.group.attrs = &sobj->a;
  530. sobj->s.group.name = name;
  531. return &sobj->s;
  532. }
  533. /* not multi-threaded safe, use it in a single thread per set */
  534. static int add_to_attr_set(struct attribute_set* s, struct attribute *attr)
  535. {
  536. if (!s || !attr)
  537. return -EINVAL;
  538. if (s->members >= s->max_members)
  539. return -ENOMEM;
  540. s->group.attrs[s->members] = attr;
  541. s->members++;
  542. return 0;
  543. }
  544. static int add_many_to_attr_set(struct attribute_set* s,
  545. struct attribute **attr,
  546. unsigned int count)
  547. {
  548. int i, res;
  549. for (i = 0; i < count; i++) {
  550. res = add_to_attr_set(s, attr[i]);
  551. if (res)
  552. return res;
  553. }
  554. return 0;
  555. }
  556. static void delete_attr_set(struct attribute_set* s, struct kobject *kobj)
  557. {
  558. sysfs_remove_group(kobj, &s->group);
  559. destroy_attr_set(s);
  560. }
  561. static int parse_strtoul(const char *buf,
  562. unsigned long max, unsigned long *value)
  563. {
  564. char *endp;
  565. *value = simple_strtoul(buf, &endp, 0);
  566. while (*endp && isspace(*endp))
  567. endp++;
  568. if (*endp || *value > max)
  569. return -EINVAL;
  570. return 0;
  571. }
  572. /****************************************************************************
  573. ****************************************************************************
  574. *
  575. * Subdrivers
  576. *
  577. ****************************************************************************
  578. ****************************************************************************/
  579. /*************************************************************************
  580. * thinkpad-acpi init subdriver
  581. */
  582. static int __init thinkpad_acpi_driver_init(struct ibm_init_struct *iibm)
  583. {
  584. printk(IBM_INFO "%s v%s\n", IBM_DESC, IBM_VERSION);
  585. printk(IBM_INFO "%s\n", IBM_URL);
  586. if (ibm_thinkpad_ec_found)
  587. printk(IBM_INFO "ThinkPad EC firmware %s\n",
  588. ibm_thinkpad_ec_found);
  589. return 0;
  590. }
  591. static int thinkpad_acpi_driver_read(char *p)
  592. {
  593. int len = 0;
  594. len += sprintf(p + len, "driver:\t\t%s\n", IBM_DESC);
  595. len += sprintf(p + len, "version:\t%s\n", IBM_VERSION);
  596. return len;
  597. }
  598. static struct ibm_struct thinkpad_acpi_driver_data = {
  599. .name = "driver",
  600. .read = thinkpad_acpi_driver_read,
  601. };
  602. /*************************************************************************
  603. * Hotkey subdriver
  604. */
  605. static int hotkey_orig_status;
  606. static u32 hotkey_orig_mask;
  607. static u32 hotkey_all_mask;
  608. static u32 hotkey_reserved_mask;
  609. static u16 hotkey_keycode_map[] = {
  610. /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
  611. KEY_FN_F1, KEY_FN_F2, KEY_FN_F3, KEY_SLEEP,
  612. KEY_FN_F5, KEY_FN_F6, KEY_FN_F7, KEY_FN_F8,
  613. KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
  614. /* Scan codes 0x0C to 0x0F: Other ACPI HKEY hot keys */
  615. KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
  616. KEY_UNKNOWN, /* 0x0D: FN+INSERT */
  617. KEY_UNKNOWN, /* 0x0E: FN+DELETE */
  618. KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
  619. /* Scan codes 0x10 to 0x1F: Extended ACPI HKEY hot keys */
  620. KEY_RESERVED, /* 0x10: FN+END (brightness down) */
  621. KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
  622. KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
  623. KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
  624. KEY_RESERVED, /* 0x14: VOLUME UP */
  625. KEY_RESERVED, /* 0x15: VOLUME DOWN */
  626. KEY_RESERVED, /* 0x16: MUTE */
  627. KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
  628. /* (assignments unknown, please report if found) */
  629. KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
  630. KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
  631. };
  632. static struct attribute_set *hotkey_dev_attributes;
  633. static int hotkey_get_wlsw(int *status)
  634. {
  635. if (!acpi_evalf(hkey_handle, status, "WLSW", "d"))
  636. return -EIO;
  637. return 0;
  638. }
  639. /* sysfs hotkey enable ------------------------------------------------- */
  640. static ssize_t hotkey_enable_show(struct device *dev,
  641. struct device_attribute *attr,
  642. char *buf)
  643. {
  644. int res, status;
  645. u32 mask;
  646. res = hotkey_get(&status, &mask);
  647. if (res)
  648. return res;
  649. return snprintf(buf, PAGE_SIZE, "%d\n", status);
  650. }
  651. static ssize_t hotkey_enable_store(struct device *dev,
  652. struct device_attribute *attr,
  653. const char *buf, size_t count)
  654. {
  655. unsigned long t;
  656. int res, status;
  657. u32 mask;
  658. if (parse_strtoul(buf, 1, &t))
  659. return -EINVAL;
  660. res = hotkey_get(&status, &mask);
  661. if (!res)
  662. res = hotkey_set(t, mask);
  663. return (res) ? res : count;
  664. }
  665. static struct device_attribute dev_attr_hotkey_enable =
  666. __ATTR(hotkey_enable, S_IWUSR | S_IRUGO,
  667. hotkey_enable_show, hotkey_enable_store);
  668. /* sysfs hotkey mask --------------------------------------------------- */
  669. static ssize_t hotkey_mask_show(struct device *dev,
  670. struct device_attribute *attr,
  671. char *buf)
  672. {
  673. int res, status;
  674. u32 mask;
  675. res = hotkey_get(&status, &mask);
  676. if (res)
  677. return res;
  678. return snprintf(buf, PAGE_SIZE, "0x%08x\n", mask);
  679. }
  680. static ssize_t hotkey_mask_store(struct device *dev,
  681. struct device_attribute *attr,
  682. const char *buf, size_t count)
  683. {
  684. unsigned long t;
  685. int res, status;
  686. u32 mask;
  687. if (parse_strtoul(buf, 0xffffffffUL, &t))
  688. return -EINVAL;
  689. res = hotkey_get(&status, &mask);
  690. if (!res)
  691. hotkey_set(status, t);
  692. return (res) ? res : count;
  693. }
  694. static struct device_attribute dev_attr_hotkey_mask =
  695. __ATTR(hotkey_mask, S_IWUSR | S_IRUGO,
  696. hotkey_mask_show, hotkey_mask_store);
  697. /* sysfs hotkey bios_enabled ------------------------------------------- */
  698. static ssize_t hotkey_bios_enabled_show(struct device *dev,
  699. struct device_attribute *attr,
  700. char *buf)
  701. {
  702. return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_orig_status);
  703. }
  704. static struct device_attribute dev_attr_hotkey_bios_enabled =
  705. __ATTR(hotkey_bios_enabled, S_IRUGO, hotkey_bios_enabled_show, NULL);
  706. /* sysfs hotkey bios_mask ---------------------------------------------- */
  707. static ssize_t hotkey_bios_mask_show(struct device *dev,
  708. struct device_attribute *attr,
  709. char *buf)
  710. {
  711. return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
  712. }
  713. static struct device_attribute dev_attr_hotkey_bios_mask =
  714. __ATTR(hotkey_bios_mask, S_IRUGO, hotkey_bios_mask_show, NULL);
  715. /* sysfs hotkey all_mask ----------------------------------------------- */
  716. static ssize_t hotkey_all_mask_show(struct device *dev,
  717. struct device_attribute *attr,
  718. char *buf)
  719. {
  720. return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_all_mask);
  721. }
  722. static struct device_attribute dev_attr_hotkey_all_mask =
  723. __ATTR(hotkey_all_mask, S_IRUGO, hotkey_all_mask_show, NULL);
  724. /* sysfs hotkey recommended_mask --------------------------------------- */
  725. static ssize_t hotkey_recommended_mask_show(struct device *dev,
  726. struct device_attribute *attr,
  727. char *buf)
  728. {
  729. return snprintf(buf, PAGE_SIZE, "0x%08x\n",
  730. hotkey_all_mask & ~hotkey_reserved_mask);
  731. }
  732. static struct device_attribute dev_attr_hotkey_recommended_mask =
  733. __ATTR(hotkey_recommended_mask, S_IRUGO,
  734. hotkey_recommended_mask_show, NULL);
  735. /* sysfs hotkey radio_sw ----------------------------------------------- */
  736. static ssize_t hotkey_radio_sw_show(struct device *dev,
  737. struct device_attribute *attr,
  738. char *buf)
  739. {
  740. int res, s;
  741. res = hotkey_get_wlsw(&s);
  742. if (res < 0)
  743. return res;
  744. return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
  745. }
  746. static struct device_attribute dev_attr_hotkey_radio_sw =
  747. __ATTR(hotkey_radio_sw, S_IRUGO, hotkey_radio_sw_show, NULL);
  748. /* --------------------------------------------------------------------- */
  749. static struct attribute *hotkey_mask_attributes[] = {
  750. &dev_attr_hotkey_mask.attr,
  751. &dev_attr_hotkey_bios_enabled.attr,
  752. &dev_attr_hotkey_bios_mask.attr,
  753. &dev_attr_hotkey_all_mask.attr,
  754. &dev_attr_hotkey_recommended_mask.attr,
  755. };
  756. static int __init hotkey_init(struct ibm_init_struct *iibm)
  757. {
  758. int res, i;
  759. int status;
  760. vdbg_printk(TPACPI_DBG_INIT, "initializing hotkey subdriver\n");
  761. BUG_ON(!tpacpi_inputdev);
  762. IBM_ACPIHANDLE_INIT(hkey);
  763. mutex_init(&hotkey_mutex);
  764. /* hotkey not supported on 570 */
  765. tp_features.hotkey = hkey_handle != NULL;
  766. vdbg_printk(TPACPI_DBG_INIT, "hotkeys are %s\n",
  767. str_supported(tp_features.hotkey));
  768. if (tp_features.hotkey) {
  769. hotkey_dev_attributes = create_attr_set(7, NULL);
  770. if (!hotkey_dev_attributes)
  771. return -ENOMEM;
  772. res = add_to_attr_set(hotkey_dev_attributes,
  773. &dev_attr_hotkey_enable.attr);
  774. if (res)
  775. return res;
  776. /* mask not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
  777. A30, R30, R31, T20-22, X20-21, X22-24 */
  778. tp_features.hotkey_mask =
  779. acpi_evalf(hkey_handle, NULL, "DHKN", "qv");
  780. vdbg_printk(TPACPI_DBG_INIT, "hotkey masks are %s\n",
  781. str_supported(tp_features.hotkey_mask));
  782. if (tp_features.hotkey_mask) {
  783. /* MHKA available in A31, R40, R40e, T4x, X31, and later */
  784. if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
  785. "MHKA", "qd"))
  786. hotkey_all_mask = 0x080cU; /* FN+F12, FN+F4, FN+F3 */
  787. }
  788. res = hotkey_get(&hotkey_orig_status, &hotkey_orig_mask);
  789. if (!res && tp_features.hotkey_mask) {
  790. res = add_many_to_attr_set(hotkey_dev_attributes,
  791. hotkey_mask_attributes,
  792. ARRAY_SIZE(hotkey_mask_attributes));
  793. }
  794. /* Not all thinkpads have a hardware radio switch */
  795. if (!res && acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
  796. tp_features.hotkey_wlsw = 1;
  797. printk(IBM_INFO
  798. "radio switch found; radios are %s\n",
  799. enabled(status, 0));
  800. res = add_to_attr_set(hotkey_dev_attributes,
  801. &dev_attr_hotkey_radio_sw.attr);
  802. }
  803. if (!res)
  804. res = register_attr_set_with_sysfs(
  805. hotkey_dev_attributes,
  806. &tpacpi_pdev->dev.kobj);
  807. if (res)
  808. return res;
  809. #ifndef CONFIG_THINKPAD_ACPI_INPUT_ENABLED
  810. for (i = 0; i < 12; i++)
  811. hotkey_keycode_map[i] = KEY_UNKNOWN;
  812. #endif /* ! CONFIG_THINKPAD_ACPI_INPUT_ENABLED */
  813. set_bit(EV_KEY, tpacpi_inputdev->evbit);
  814. set_bit(EV_MSC, tpacpi_inputdev->evbit);
  815. set_bit(MSC_SCAN, tpacpi_inputdev->mscbit);
  816. tpacpi_inputdev->keycodesize = sizeof(hotkey_keycode_map[0]);
  817. tpacpi_inputdev->keycodemax = ARRAY_SIZE(hotkey_keycode_map);
  818. tpacpi_inputdev->keycode = &hotkey_keycode_map;
  819. for (i = 0; i < ARRAY_SIZE(hotkey_keycode_map); i++) {
  820. if (hotkey_keycode_map[i] != KEY_RESERVED) {
  821. set_bit(hotkey_keycode_map[i],
  822. tpacpi_inputdev->keybit);
  823. } else {
  824. if (i < sizeof(hotkey_reserved_mask)*8)
  825. hotkey_reserved_mask |= 1 << i;
  826. }
  827. }
  828. if (tp_features.hotkey_wlsw) {
  829. set_bit(EV_SW, tpacpi_inputdev->evbit);
  830. set_bit(SW_RADIO, tpacpi_inputdev->swbit);
  831. }
  832. #ifdef CONFIG_THINKPAD_ACPI_INPUT_ENABLED
  833. dbg_printk(TPACPI_DBG_INIT,
  834. "enabling hot key handling\n");
  835. res = hotkey_set(1, (hotkey_all_mask & ~hotkey_reserved_mask)
  836. | hotkey_orig_mask);
  837. if (res)
  838. return res;
  839. #endif /* CONFIG_THINKPAD_ACPI_INPUT_ENABLED */
  840. }
  841. return (tp_features.hotkey)? 0 : 1;
  842. }
  843. static void hotkey_exit(void)
  844. {
  845. int res;
  846. if (tp_features.hotkey) {
  847. dbg_printk(TPACPI_DBG_EXIT, "restoring original hotkey mask\n");
  848. res = hotkey_set(hotkey_orig_status, hotkey_orig_mask);
  849. if (res)
  850. printk(IBM_ERR "failed to restore hotkey to BIOS defaults\n");
  851. }
  852. if (hotkey_dev_attributes) {
  853. delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
  854. hotkey_dev_attributes = NULL;
  855. }
  856. }
  857. static void tpacpi_input_send_key(unsigned int scancode,
  858. unsigned int keycode)
  859. {
  860. if (keycode != KEY_RESERVED) {
  861. input_report_key(tpacpi_inputdev, keycode, 1);
  862. if (keycode == KEY_UNKNOWN)
  863. input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
  864. scancode);
  865. input_sync(tpacpi_inputdev);
  866. input_report_key(tpacpi_inputdev, keycode, 0);
  867. if (keycode == KEY_UNKNOWN)
  868. input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
  869. scancode);
  870. input_sync(tpacpi_inputdev);
  871. }
  872. }
  873. static void tpacpi_input_send_radiosw(void)
  874. {
  875. int wlsw;
  876. if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&wlsw))
  877. input_report_switch(tpacpi_inputdev,
  878. SW_RADIO, !!wlsw);
  879. }
  880. static void hotkey_notify(struct ibm_struct *ibm, u32 event)
  881. {
  882. u32 hkey;
  883. unsigned int keycode, scancode;
  884. int sendacpi = 1;
  885. if (event == 0x80 && acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
  886. if (tpacpi_inputdev->users > 0) {
  887. switch (hkey >> 12) {
  888. case 1:
  889. /* 0x1000-0x1FFF: key presses */
  890. scancode = hkey & 0xfff;
  891. if (scancode > 0 && scancode < 0x21) {
  892. scancode--;
  893. keycode = hotkey_keycode_map[scancode];
  894. tpacpi_input_send_key(scancode, keycode);
  895. sendacpi = (keycode == KEY_RESERVED
  896. || keycode == KEY_UNKNOWN);
  897. } else {
  898. printk(IBM_ERR
  899. "hotkey 0x%04x out of range for keyboard map\n",
  900. hkey);
  901. }
  902. break;
  903. case 5:
  904. /* 0x5000-0x5FFF: LID */
  905. /* we don't handle it through this path, just
  906. * eat up known LID events */
  907. if (hkey != 0x5001 && hkey != 0x5002) {
  908. printk(IBM_ERR
  909. "unknown LID-related hotkey event: 0x%04x\n",
  910. hkey);
  911. }
  912. break;
  913. case 7:
  914. /* 0x7000-0x7FFF: misc */
  915. if (tp_features.hotkey_wlsw && hkey == 0x7000) {
  916. tpacpi_input_send_radiosw();
  917. sendacpi = 0;
  918. break;
  919. }
  920. /* fallthrough to default */
  921. default:
  922. /* case 2: dock-related */
  923. /* 0x2305 - T43 waking up due to bay lever eject while aslept */
  924. /* case 3: ultra-bay related. maybe bay in dock? */
  925. /* 0x3003 - T43 after wake up by bay lever eject (0x2305) */
  926. printk(IBM_NOTICE "unhandled hotkey event 0x%04x\n", hkey);
  927. }
  928. }
  929. if (sendacpi)
  930. acpi_bus_generate_event(ibm->acpi->device, event, hkey);
  931. } else {
  932. printk(IBM_ERR "unknown hotkey notification event %d\n", event);
  933. acpi_bus_generate_event(ibm->acpi->device, event, 0);
  934. }
  935. }
  936. static void hotkey_resume(void)
  937. {
  938. tpacpi_input_send_radiosw();
  939. }
  940. /*
  941. * Call with hotkey_mutex held
  942. */
  943. static int hotkey_get(int *status, u32 *mask)
  944. {
  945. if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
  946. return -EIO;
  947. if (tp_features.hotkey_mask)
  948. if (!acpi_evalf(hkey_handle, mask, "DHKN", "d"))
  949. return -EIO;
  950. return 0;
  951. }
  952. /*
  953. * Call with hotkey_mutex held
  954. */
  955. static int hotkey_set(int status, u32 mask)
  956. {
  957. int i;
  958. if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", status))
  959. return -EIO;
  960. if (tp_features.hotkey_mask)
  961. for (i = 0; i < 32; i++) {
  962. int bit = ((1 << i) & mask) != 0;
  963. if (!acpi_evalf(hkey_handle,
  964. NULL, "MHKM", "vdd", i + 1, bit))
  965. return -EIO;
  966. }
  967. return 0;
  968. }
  969. /* procfs -------------------------------------------------------------- */
  970. static int hotkey_read(char *p)
  971. {
  972. int res, status;
  973. u32 mask;
  974. int len = 0;
  975. if (!tp_features.hotkey) {
  976. len += sprintf(p + len, "status:\t\tnot supported\n");
  977. return len;
  978. }
  979. res = mutex_lock_interruptible(&hotkey_mutex);
  980. if (res < 0)
  981. return res;
  982. res = hotkey_get(&status, &mask);
  983. mutex_unlock(&hotkey_mutex);
  984. if (res)
  985. return res;
  986. len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 0));
  987. if (tp_features.hotkey_mask) {
  988. len += sprintf(p + len, "mask:\t\t0x%08x\n", mask);
  989. len += sprintf(p + len,
  990. "commands:\tenable, disable, reset, <mask>\n");
  991. } else {
  992. len += sprintf(p + len, "mask:\t\tnot supported\n");
  993. len += sprintf(p + len, "commands:\tenable, disable, reset\n");
  994. }
  995. return len;
  996. }
  997. static int hotkey_write(char *buf)
  998. {
  999. int res, status;
  1000. u32 mask;
  1001. char *cmd;
  1002. int do_cmd = 0;
  1003. if (!tp_features.hotkey)
  1004. return -ENODEV;
  1005. res = mutex_lock_interruptible(&hotkey_mutex);
  1006. if (res < 0)
  1007. return res;
  1008. res = hotkey_get(&status, &mask);
  1009. if (res)
  1010. goto errexit;
  1011. res = 0;
  1012. while ((cmd = next_cmd(&buf))) {
  1013. if (strlencmp(cmd, "enable") == 0) {
  1014. status = 1;
  1015. } else if (strlencmp(cmd, "disable") == 0) {
  1016. status = 0;
  1017. } else if (strlencmp(cmd, "reset") == 0) {
  1018. status = hotkey_orig_status;
  1019. mask = hotkey_orig_mask;
  1020. } else if (sscanf(cmd, "0x%x", &mask) == 1) {
  1021. /* mask set */
  1022. } else if (sscanf(cmd, "%x", &mask) == 1) {
  1023. /* mask set */
  1024. } else {
  1025. res = -EINVAL;
  1026. goto errexit;
  1027. }
  1028. do_cmd = 1;
  1029. }
  1030. if (do_cmd)
  1031. res = hotkey_set(status, mask);
  1032. errexit:
  1033. mutex_unlock(&hotkey_mutex);
  1034. return res;
  1035. }
  1036. static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
  1037. .hid = IBM_HKEY_HID,
  1038. .notify = hotkey_notify,
  1039. .handle = &hkey_handle,
  1040. .type = ACPI_DEVICE_NOTIFY,
  1041. };
  1042. static struct ibm_struct hotkey_driver_data = {
  1043. .name = "hotkey",
  1044. .read = hotkey_read,
  1045. .write = hotkey_write,
  1046. .exit = hotkey_exit,
  1047. .resume = hotkey_resume,
  1048. .acpi = &ibm_hotkey_acpidriver,
  1049. };
  1050. /*************************************************************************
  1051. * Bluetooth subdriver
  1052. */
  1053. /* sysfs bluetooth enable ---------------------------------------------- */
  1054. static ssize_t bluetooth_enable_show(struct device *dev,
  1055. struct device_attribute *attr,
  1056. char *buf)
  1057. {
  1058. int status;
  1059. status = bluetooth_get_radiosw();
  1060. if (status < 0)
  1061. return status;
  1062. return snprintf(buf, PAGE_SIZE, "%d\n", status ? 1 : 0);
  1063. }
  1064. static ssize_t bluetooth_enable_store(struct device *dev,
  1065. struct device_attribute *attr,
  1066. const char *buf, size_t count)
  1067. {
  1068. unsigned long t;
  1069. int res;
  1070. if (parse_strtoul(buf, 1, &t))
  1071. return -EINVAL;
  1072. res = bluetooth_set_radiosw(t);
  1073. return (res) ? res : count;
  1074. }
  1075. static struct device_attribute dev_attr_bluetooth_enable =
  1076. __ATTR(bluetooth_enable, S_IWUSR | S_IRUGO,
  1077. bluetooth_enable_show, bluetooth_enable_store);
  1078. /* --------------------------------------------------------------------- */
  1079. static struct attribute *bluetooth_attributes[] = {
  1080. &dev_attr_bluetooth_enable.attr,
  1081. NULL
  1082. };
  1083. static const struct attribute_group bluetooth_attr_group = {
  1084. .attrs = bluetooth_attributes,
  1085. };
  1086. static int __init bluetooth_init(struct ibm_init_struct *iibm)
  1087. {
  1088. int res;
  1089. int status = 0;
  1090. vdbg_printk(TPACPI_DBG_INIT, "initializing bluetooth subdriver\n");
  1091. IBM_ACPIHANDLE_INIT(hkey);
  1092. /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
  1093. G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
  1094. tp_features.bluetooth = hkey_handle &&
  1095. acpi_evalf(hkey_handle, &status, "GBDC", "qd");
  1096. vdbg_printk(TPACPI_DBG_INIT, "bluetooth is %s, status 0x%02x\n",
  1097. str_supported(tp_features.bluetooth),
  1098. status);
  1099. if (tp_features.bluetooth) {
  1100. if (!(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
  1101. /* no bluetooth hardware present in system */
  1102. tp_features.bluetooth = 0;
  1103. dbg_printk(TPACPI_DBG_INIT,
  1104. "bluetooth hardware not installed\n");
  1105. } else {
  1106. res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
  1107. &bluetooth_attr_group);
  1108. if (res)
  1109. return res;
  1110. }
  1111. }
  1112. return (tp_features.bluetooth)? 0 : 1;
  1113. }
  1114. static void bluetooth_exit(void)
  1115. {
  1116. sysfs_remove_group(&tpacpi_pdev->dev.kobj,
  1117. &bluetooth_attr_group);
  1118. }
  1119. static int bluetooth_get_radiosw(void)
  1120. {
  1121. int status;
  1122. if (!tp_features.bluetooth)
  1123. return -ENODEV;
  1124. if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
  1125. return -EIO;
  1126. return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0);
  1127. }
  1128. static int bluetooth_set_radiosw(int radio_on)
  1129. {
  1130. int status;
  1131. if (!tp_features.bluetooth)
  1132. return -ENODEV;
  1133. if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
  1134. return -EIO;
  1135. if (radio_on)
  1136. status |= TP_ACPI_BLUETOOTH_RADIOSSW;
  1137. else
  1138. status &= ~TP_ACPI_BLUETOOTH_RADIOSSW;
  1139. if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
  1140. return -EIO;
  1141. return 0;
  1142. }
  1143. /* procfs -------------------------------------------------------------- */
  1144. static int bluetooth_read(char *p)
  1145. {
  1146. int len = 0;
  1147. int status = bluetooth_get_radiosw();
  1148. if (!tp_features.bluetooth)
  1149. len += sprintf(p + len, "status:\t\tnot supported\n");
  1150. else {
  1151. len += sprintf(p + len, "status:\t\t%s\n",
  1152. (status)? "enabled" : "disabled");
  1153. len += sprintf(p + len, "commands:\tenable, disable\n");
  1154. }
  1155. return len;
  1156. }
  1157. static int bluetooth_write(char *buf)
  1158. {
  1159. char *cmd;
  1160. if (!tp_features.bluetooth)
  1161. return -ENODEV;
  1162. while ((cmd = next_cmd(&buf))) {
  1163. if (strlencmp(cmd, "enable") == 0) {
  1164. bluetooth_set_radiosw(1);
  1165. } else if (strlencmp(cmd, "disable") == 0) {
  1166. bluetooth_set_radiosw(0);
  1167. } else
  1168. return -EINVAL;
  1169. }
  1170. return 0;
  1171. }
  1172. static struct ibm_struct bluetooth_driver_data = {
  1173. .name = "bluetooth",
  1174. .read = bluetooth_read,
  1175. .write = bluetooth_write,
  1176. .exit = bluetooth_exit,
  1177. };
  1178. /*************************************************************************
  1179. * Wan subdriver
  1180. */
  1181. /* sysfs wan enable ---------------------------------------------------- */
  1182. static ssize_t wan_enable_show(struct device *dev,
  1183. struct device_attribute *attr,
  1184. char *buf)
  1185. {
  1186. int status;
  1187. status = wan_get_radiosw();
  1188. if (status < 0)
  1189. return status;
  1190. return snprintf(buf, PAGE_SIZE, "%d\n", status ? 1 : 0);
  1191. }
  1192. static ssize_t wan_enable_store(struct device *dev,
  1193. struct device_attribute *attr,
  1194. const char *buf, size_t count)
  1195. {
  1196. unsigned long t;
  1197. int res;
  1198. if (parse_strtoul(buf, 1, &t))
  1199. return -EINVAL;
  1200. res = wan_set_radiosw(t);
  1201. return (res) ? res : count;
  1202. }
  1203. static struct device_attribute dev_attr_wan_enable =
  1204. __ATTR(wwan_enable, S_IWUSR | S_IRUGO,
  1205. wan_enable_show, wan_enable_store);
  1206. /* --------------------------------------------------------------------- */
  1207. static struct attribute *wan_attributes[] = {
  1208. &dev_attr_wan_enable.attr,
  1209. NULL
  1210. };
  1211. static const struct attribute_group wan_attr_group = {
  1212. .attrs = wan_attributes,
  1213. };
  1214. static int __init wan_init(struct ibm_init_struct *iibm)
  1215. {
  1216. int res;
  1217. int status = 0;
  1218. vdbg_printk(TPACPI_DBG_INIT, "initializing wan subdriver\n");
  1219. IBM_ACPIHANDLE_INIT(hkey);
  1220. tp_features.wan = hkey_handle &&
  1221. acpi_evalf(hkey_handle, &status, "GWAN", "qd");
  1222. vdbg_printk(TPACPI_DBG_INIT, "wan is %s, status 0x%02x\n",
  1223. str_supported(tp_features.wan),
  1224. status);
  1225. if (tp_features.wan) {
  1226. if (!(status & TP_ACPI_WANCARD_HWPRESENT)) {
  1227. /* no wan hardware present in system */
  1228. tp_features.wan = 0;
  1229. dbg_printk(TPACPI_DBG_INIT,
  1230. "wan hardware not installed\n");
  1231. } else {
  1232. res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
  1233. &wan_attr_group);
  1234. if (res)
  1235. return res;
  1236. }
  1237. }
  1238. return (tp_features.wan)? 0 : 1;
  1239. }
  1240. static void wan_exit(void)
  1241. {
  1242. sysfs_remove_group(&tpacpi_pdev->dev.kobj,
  1243. &wan_attr_group);
  1244. }
  1245. static int wan_get_radiosw(void)
  1246. {
  1247. int status;
  1248. if (!tp_features.wan)
  1249. return -ENODEV;
  1250. if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
  1251. return -EIO;
  1252. return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0);
  1253. }
  1254. static int wan_set_radiosw(int radio_on)
  1255. {
  1256. int status;
  1257. if (!tp_features.wan)
  1258. return -ENODEV;
  1259. if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
  1260. return -EIO;
  1261. if (radio_on)
  1262. status |= TP_ACPI_WANCARD_RADIOSSW;
  1263. else
  1264. status &= ~TP_ACPI_WANCARD_RADIOSSW;
  1265. if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
  1266. return -EIO;
  1267. return 0;
  1268. }
  1269. /* procfs -------------------------------------------------------------- */
  1270. static int wan_read(char *p)
  1271. {
  1272. int len = 0;
  1273. int status = wan_get_radiosw();
  1274. if (!tp_features.wan)
  1275. len += sprintf(p + len, "status:\t\tnot supported\n");
  1276. else {
  1277. len += sprintf(p + len, "status:\t\t%s\n",
  1278. (status)? "enabled" : "disabled");
  1279. len += sprintf(p + len, "commands:\tenable, disable\n");
  1280. }
  1281. return len;
  1282. }
  1283. static int wan_write(char *buf)
  1284. {
  1285. char *cmd;
  1286. if (!tp_features.wan)
  1287. return -ENODEV;
  1288. while ((cmd = next_cmd(&buf))) {
  1289. if (strlencmp(cmd, "enable") == 0) {
  1290. wan_set_radiosw(1);
  1291. } else if (strlencmp(cmd, "disable") == 0) {
  1292. wan_set_radiosw(0);
  1293. } else
  1294. return -EINVAL;
  1295. }
  1296. return 0;
  1297. }
  1298. static struct ibm_struct wan_driver_data = {
  1299. .name = "wan",
  1300. .read = wan_read,
  1301. .write = wan_write,
  1302. .exit = wan_exit,
  1303. .flags.experimental = 1,
  1304. };
  1305. /*************************************************************************
  1306. * Video subdriver
  1307. */
  1308. static enum video_access_mode video_supported;
  1309. static int video_orig_autosw;
  1310. IBM_HANDLE(vid, root, "\\_SB.PCI.AGP.VGA", /* 570 */
  1311. "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
  1312. "\\_SB.PCI0.VID0", /* 770e */
  1313. "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
  1314. "\\_SB.PCI0.AGP.VID", /* all others */
  1315. ); /* R30, R31 */
  1316. IBM_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
  1317. static int __init video_init(struct ibm_init_struct *iibm)
  1318. {
  1319. int ivga;
  1320. vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
  1321. IBM_ACPIHANDLE_INIT(vid);
  1322. IBM_ACPIHANDLE_INIT(vid2);
  1323. if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
  1324. /* G41, assume IVGA doesn't change */
  1325. vid_handle = vid2_handle;
  1326. if (!vid_handle)
  1327. /* video switching not supported on R30, R31 */
  1328. video_supported = TPACPI_VIDEO_NONE;
  1329. else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
  1330. /* 570 */
  1331. video_supported = TPACPI_VIDEO_570;
  1332. else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
  1333. /* 600e/x, 770e, 770x */
  1334. video_supported = TPACPI_VIDEO_770;
  1335. else
  1336. /* all others */
  1337. video_supported = TPACPI_VIDEO_NEW;
  1338. vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
  1339. str_supported(video_supported != TPACPI_VIDEO_NONE),
  1340. video_supported);
  1341. return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
  1342. }
  1343. static void video_exit(void)
  1344. {
  1345. dbg_printk(TPACPI_DBG_EXIT,
  1346. "restoring original video autoswitch mode\n");
  1347. if (video_autosw_set(video_orig_autosw))
  1348. printk(IBM_ERR "error while trying to restore original "
  1349. "video autoswitch mode\n");
  1350. }
  1351. static int video_outputsw_get(void)
  1352. {
  1353. int status = 0;
  1354. int i;
  1355. switch (video_supported) {
  1356. case TPACPI_VIDEO_570:
  1357. if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
  1358. TP_ACPI_VIDEO_570_PHSCMD))
  1359. return -EIO;
  1360. status = i & TP_ACPI_VIDEO_570_PHSMASK;
  1361. break;
  1362. case TPACPI_VIDEO_770:
  1363. if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
  1364. return -EIO;
  1365. if (i)
  1366. status |= TP_ACPI_VIDEO_S_LCD;
  1367. if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
  1368. return -EIO;
  1369. if (i)
  1370. status |= TP_ACPI_VIDEO_S_CRT;
  1371. break;
  1372. case TPACPI_VIDEO_NEW:
  1373. if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
  1374. !acpi_evalf(NULL, &i, "\\VCDC", "d"))
  1375. return -EIO;
  1376. if (i)
  1377. status |= TP_ACPI_VIDEO_S_CRT;
  1378. if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
  1379. !acpi_evalf(NULL, &i, "\\VCDL", "d"))
  1380. return -EIO;
  1381. if (i)
  1382. status |= TP_ACPI_VIDEO_S_LCD;
  1383. if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
  1384. return -EIO;
  1385. if (i)
  1386. status |= TP_ACPI_VIDEO_S_DVI;
  1387. break;
  1388. default:
  1389. return -ENOSYS;
  1390. }
  1391. return status;
  1392. }
  1393. static int video_outputsw_set(int status)
  1394. {
  1395. int autosw;
  1396. int res = 0;
  1397. switch (video_supported) {
  1398. case TPACPI_VIDEO_570:
  1399. res = acpi_evalf(NULL, NULL,
  1400. "\\_SB.PHS2", "vdd",
  1401. TP_ACPI_VIDEO_570_PHS2CMD,
  1402. status | TP_ACPI_VIDEO_570_PHS2SET);
  1403. break;
  1404. case TPACPI_VIDEO_770:
  1405. autosw = video_autosw_get();
  1406. if (autosw < 0)
  1407. return autosw;
  1408. res = video_autosw_set(1);
  1409. if (res)
  1410. return res;
  1411. res = acpi_evalf(vid_handle, NULL,
  1412. "ASWT", "vdd", status * 0x100, 0);
  1413. if (!autosw && video_autosw_set(autosw)) {
  1414. printk(IBM_ERR "video auto-switch left enabled due to error\n");
  1415. return -EIO;
  1416. }
  1417. break;
  1418. case TPACPI_VIDEO_NEW:
  1419. res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
  1420. acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
  1421. break;
  1422. default:
  1423. return -ENOSYS;
  1424. }
  1425. return (res)? 0 : -EIO;
  1426. }
  1427. static int video_autosw_get(void)
  1428. {
  1429. int autosw = 0;
  1430. switch (video_supported) {
  1431. case TPACPI_VIDEO_570:
  1432. if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
  1433. return -EIO;
  1434. break;
  1435. case TPACPI_VIDEO_770:
  1436. case TPACPI_VIDEO_NEW:
  1437. if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
  1438. return -EIO;
  1439. break;
  1440. default:
  1441. return -ENOSYS;
  1442. }
  1443. return autosw & 1;
  1444. }
  1445. static int video_autosw_set(int enable)
  1446. {
  1447. if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
  1448. return -EIO;
  1449. return 0;
  1450. }
  1451. static int video_outputsw_cycle(void)
  1452. {
  1453. int autosw = video_autosw_get();
  1454. int res;
  1455. if (autosw < 0)
  1456. return autosw;
  1457. switch (video_supported) {
  1458. case TPACPI_VIDEO_570:
  1459. res = video_autosw_set(1);
  1460. if (res)
  1461. return res;
  1462. res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
  1463. break;
  1464. case TPACPI_VIDEO_770:
  1465. case TPACPI_VIDEO_NEW:
  1466. res = video_autosw_set(1);
  1467. if (res)
  1468. return res;
  1469. res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
  1470. break;
  1471. default:
  1472. return -ENOSYS;
  1473. }
  1474. if (!autosw && video_autosw_set(autosw)) {
  1475. printk(IBM_ERR "video auto-switch left enabled due to error\n");
  1476. return -EIO;
  1477. }
  1478. return (res)? 0 : -EIO;
  1479. }
  1480. static int video_expand_toggle(void)
  1481. {
  1482. switch (video_supported) {
  1483. case TPACPI_VIDEO_570:
  1484. return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
  1485. 0 : -EIO;
  1486. case TPACPI_VIDEO_770:
  1487. return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
  1488. 0 : -EIO;
  1489. case TPACPI_VIDEO_NEW:
  1490. return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
  1491. 0 : -EIO;
  1492. default:
  1493. return -ENOSYS;
  1494. }
  1495. /* not reached */
  1496. }
  1497. static int video_read(char *p)
  1498. {
  1499. int status, autosw;
  1500. int len = 0;
  1501. if (video_supported == TPACPI_VIDEO_NONE) {
  1502. len += sprintf(p + len, "status:\t\tnot supported\n");
  1503. return len;
  1504. }
  1505. status = video_outputsw_get();
  1506. if (status < 0)
  1507. return status;
  1508. autosw = video_autosw_get();
  1509. if (autosw < 0)
  1510. return autosw;
  1511. len += sprintf(p + len, "status:\t\tsupported\n");
  1512. len += sprintf(p + len, "lcd:\t\t%s\n", enabled(status, 0));
  1513. len += sprintf(p + len, "crt:\t\t%s\n", enabled(status, 1));
  1514. if (video_supported == TPACPI_VIDEO_NEW)
  1515. len += sprintf(p + len, "dvi:\t\t%s\n", enabled(status, 3));
  1516. len += sprintf(p + len, "auto:\t\t%s\n", enabled(autosw, 0));
  1517. len += sprintf(p + len, "commands:\tlcd_enable, lcd_disable\n");
  1518. len += sprintf(p + len, "commands:\tcrt_enable, crt_disable\n");
  1519. if (video_supported == TPACPI_VIDEO_NEW)
  1520. len += sprintf(p + len, "commands:\tdvi_enable, dvi_disable\n");
  1521. len += sprintf(p + len, "commands:\tauto_enable, auto_disable\n");
  1522. len += sprintf(p + len, "commands:\tvideo_switch, expand_toggle\n");
  1523. return len;
  1524. }
  1525. static int video_write(char *buf)
  1526. {
  1527. char *cmd;
  1528. int enable, disable, status;
  1529. int res;
  1530. if (video_supported == TPACPI_VIDEO_NONE)
  1531. return -ENODEV;
  1532. enable = 0;
  1533. disable = 0;
  1534. while ((cmd = next_cmd(&buf))) {
  1535. if (strlencmp(cmd, "lcd_enable") == 0) {
  1536. enable |= TP_ACPI_VIDEO_S_LCD;
  1537. } else if (strlencmp(cmd, "lcd_disable") == 0) {
  1538. disable |= TP_ACPI_VIDEO_S_LCD;
  1539. } else if (strlencmp(cmd, "crt_enable") == 0) {
  1540. enable |= TP_ACPI_VIDEO_S_CRT;
  1541. } else if (strlencmp(cmd, "crt_disable") == 0) {
  1542. disable |= TP_ACPI_VIDEO_S_CRT;
  1543. } else if (video_supported == TPACPI_VIDEO_NEW &&
  1544. strlencmp(cmd, "dvi_enable") == 0) {
  1545. enable |= TP_ACPI_VIDEO_S_DVI;
  1546. } else if (video_supported == TPACPI_VIDEO_NEW &&
  1547. strlencmp(cmd, "dvi_disable") == 0) {
  1548. disable |= TP_ACPI_VIDEO_S_DVI;
  1549. } else if (strlencmp(cmd, "auto_enable") == 0) {
  1550. res = video_autosw_set(1);
  1551. if (res)
  1552. return res;
  1553. } else if (strlencmp(cmd, "auto_disable") == 0) {
  1554. res = video_autosw_set(0);
  1555. if (res)
  1556. return res;
  1557. } else if (strlencmp(cmd, "video_switch") == 0) {
  1558. res = video_outputsw_cycle();
  1559. if (res)
  1560. return res;
  1561. } else if (strlencmp(cmd, "expand_toggle") == 0) {
  1562. res = video_expand_toggle();
  1563. if (res)
  1564. return res;
  1565. } else
  1566. return -EINVAL;
  1567. }
  1568. if (enable || disable) {
  1569. status = video_outputsw_get();
  1570. if (status < 0)
  1571. return status;
  1572. res = video_outputsw_set((status & ~disable) | enable);
  1573. if (res)
  1574. return res;
  1575. }
  1576. return 0;
  1577. }
  1578. static struct ibm_struct video_driver_data = {
  1579. .name = "video",
  1580. .read = video_read,
  1581. .write = video_write,
  1582. .exit = video_exit,
  1583. };
  1584. /*************************************************************************
  1585. * Light (thinklight) subdriver
  1586. */
  1587. IBM_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
  1588. IBM_HANDLE(ledb, ec, "LEDB"); /* G4x */
  1589. static int __init light_init(struct ibm_init_struct *iibm)
  1590. {
  1591. vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
  1592. IBM_ACPIHANDLE_INIT(ledb);
  1593. IBM_ACPIHANDLE_INIT(lght);
  1594. IBM_ACPIHANDLE_INIT(cmos);
  1595. /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
  1596. tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
  1597. if (tp_features.light)
  1598. /* light status not supported on
  1599. 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
  1600. tp_features.light_status =
  1601. acpi_evalf(ec_handle, NULL, "KBLT", "qv");
  1602. vdbg_printk(TPACPI_DBG_INIT, "light is %s\n",
  1603. str_supported(tp_features.light));
  1604. return (tp_features.light)? 0 : 1;
  1605. }
  1606. static int light_read(char *p)
  1607. {
  1608. int len = 0;
  1609. int status = 0;
  1610. if (!tp_features.light) {
  1611. len += sprintf(p + len, "status:\t\tnot supported\n");
  1612. } else if (!tp_features.light_status) {
  1613. len += sprintf(p + len, "status:\t\tunknown\n");
  1614. len += sprintf(p + len, "commands:\ton, off\n");
  1615. } else {
  1616. if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
  1617. return -EIO;
  1618. len += sprintf(p + len, "status:\t\t%s\n", onoff(status, 0));
  1619. len += sprintf(p + len, "commands:\ton, off\n");
  1620. }
  1621. return len;
  1622. }
  1623. static int light_write(char *buf)
  1624. {
  1625. int cmos_cmd, lght_cmd;
  1626. char *cmd;
  1627. int success;
  1628. if (!tp_features.light)
  1629. return -ENODEV;
  1630. while ((cmd = next_cmd(&buf))) {
  1631. if (strlencmp(cmd, "on") == 0) {
  1632. cmos_cmd = 0x0c;
  1633. lght_cmd = 1;
  1634. } else if (strlencmp(cmd, "off") == 0) {
  1635. cmos_cmd = 0x0d;
  1636. lght_cmd = 0;
  1637. } else
  1638. return -EINVAL;
  1639. success = cmos_handle ?
  1640. acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd) :
  1641. acpi_evalf(lght_handle, NULL, NULL, "vd", lght_cmd);
  1642. if (!success)
  1643. return -EIO;
  1644. }
  1645. return 0;
  1646. }
  1647. static struct ibm_struct light_driver_data = {
  1648. .name = "light",
  1649. .read = light_read,
  1650. .write = light_write,
  1651. };
  1652. /*************************************************************************
  1653. * Dock subdriver
  1654. */
  1655. #ifdef CONFIG_THINKPAD_ACPI_DOCK
  1656. IBM_HANDLE(dock, root, "\\_SB.GDCK", /* X30, X31, X40 */
  1657. "\\_SB.PCI0.DOCK", /* 600e/x,770e,770x,A2xm/p,T20-22,X20-21 */
  1658. "\\_SB.PCI0.PCI1.DOCK", /* all others */
  1659. "\\_SB.PCI.ISA.SLCE", /* 570 */
  1660. ); /* A21e,G4x,R30,R31,R32,R40,R40e,R50e */
  1661. /* don't list other alternatives as we install a notify handler on the 570 */
  1662. IBM_HANDLE(pci, root, "\\_SB.PCI"); /* 570 */
  1663. static struct tp_acpi_drv_struct ibm_dock_acpidriver[2] = {
  1664. {
  1665. .notify = dock_notify,
  1666. .handle = &dock_handle,
  1667. .type = ACPI_SYSTEM_NOTIFY,
  1668. },
  1669. {
  1670. /* THIS ONE MUST NEVER BE USED FOR DRIVER AUTOLOADING.
  1671. * We just use it to get notifications of dock hotplug
  1672. * in very old thinkpads */
  1673. .hid = PCI_ROOT_HID_STRING,
  1674. .notify = dock_notify,
  1675. .handle = &pci_handle,
  1676. .type = ACPI_SYSTEM_NOTIFY,
  1677. },
  1678. };
  1679. static struct ibm_struct dock_driver_data[2] = {
  1680. {
  1681. .name = "dock",
  1682. .read = dock_read,
  1683. .write = dock_write,
  1684. .acpi = &ibm_dock_acpidriver[0],
  1685. },
  1686. {
  1687. .name = "dock",
  1688. .acpi = &ibm_dock_acpidriver[1],
  1689. },
  1690. };
  1691. #define dock_docked() (_sta(dock_handle) & 1)
  1692. static int __init dock_init(struct ibm_init_struct *iibm)
  1693. {
  1694. vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver\n");
  1695. IBM_ACPIHANDLE_INIT(dock);
  1696. vdbg_printk(TPACPI_DBG_INIT, "dock is %s\n",
  1697. str_supported(dock_handle != NULL));
  1698. return (dock_handle)? 0 : 1;
  1699. }
  1700. static int __init dock_init2(struct ibm_init_struct *iibm)
  1701. {
  1702. int dock2_needed;
  1703. vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver part 2\n");
  1704. if (dock_driver_data[0].flags.acpi_driver_registered &&
  1705. dock_driver_data[0].flags.acpi_notify_installed) {
  1706. IBM_ACPIHANDLE_INIT(pci);
  1707. dock2_needed = (pci_handle != NULL);
  1708. vdbg_printk(TPACPI_DBG_INIT,
  1709. "dock PCI handler for the TP 570 is %s\n",
  1710. str_supported(dock2_needed));
  1711. } else {
  1712. vdbg_printk(TPACPI_DBG_INIT,
  1713. "dock subdriver part 2 not required\n");
  1714. dock2_needed = 0;
  1715. }
  1716. return (dock2_needed)? 0 : 1;
  1717. }
  1718. static void dock_notify(struct ibm_struct *ibm, u32 event)
  1719. {
  1720. int docked = dock_docked();
  1721. int pci = ibm->acpi->hid && strstr(ibm->acpi->hid, PCI_ROOT_HID_STRING);
  1722. if (event == 1 && !pci) /* 570 */
  1723. acpi_bus_generate_event(ibm->acpi->device, event, 1); /* button */
  1724. else if (event == 1 && pci) /* 570 */
  1725. acpi_bus_generate_event(ibm->acpi->device, event, 3); /* dock */
  1726. else if (event == 3 && docked)
  1727. acpi_bus_generate_event(ibm->acpi->device, event, 1); /* button */
  1728. else if (event == 3 && !docked)
  1729. acpi_bus_generate_event(ibm->acpi->device, event, 2); /* undock */
  1730. else if (event == 0 && docked)
  1731. acpi_bus_generate_event(ibm->acpi->device, event, 3); /* dock */
  1732. else {
  1733. printk(IBM_ERR "unknown dock event %d, status %d\n",
  1734. event, _sta(dock_handle));
  1735. acpi_bus_generate_event(ibm->acpi->device, event, 0); /* unknown */
  1736. }
  1737. }
  1738. static int dock_read(char *p)
  1739. {
  1740. int len = 0;
  1741. int docked = dock_docked();
  1742. if (!dock_handle)
  1743. len += sprintf(p + len, "status:\t\tnot supported\n");
  1744. else if (!docked)
  1745. len += sprintf(p + len, "status:\t\tundocked\n");
  1746. else {
  1747. len += sprintf(p + len, "status:\t\tdocked\n");
  1748. len += sprintf(p + len, "commands:\tdock, undock\n");
  1749. }
  1750. return len;
  1751. }
  1752. static int dock_write(char *buf)
  1753. {
  1754. char *cmd;
  1755. if (!dock_docked())
  1756. return -ENODEV;
  1757. while ((cmd = next_cmd(&buf))) {
  1758. if (strlencmp(cmd, "undock") == 0) {
  1759. if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 0) ||
  1760. !acpi_evalf(dock_handle, NULL, "_EJ0", "vd", 1))
  1761. return -EIO;
  1762. } else if (strlencmp(cmd, "dock") == 0) {
  1763. if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 1))
  1764. return -EIO;
  1765. } else
  1766. return -EINVAL;
  1767. }
  1768. return 0;
  1769. }
  1770. #endif /* CONFIG_THINKPAD_ACPI_DOCK */
  1771. /*************************************************************************
  1772. * Bay subdriver
  1773. */
  1774. #ifdef CONFIG_THINKPAD_ACPI_BAY
  1775. IBM_HANDLE(bay, root, "\\_SB.PCI.IDE.SECN.MAST", /* 570 */
  1776. "\\_SB.PCI0.IDE0.IDES.IDSM", /* 600e/x, 770e, 770x */
  1777. "\\_SB.PCI0.SATA.SCND.MSTR", /* T60, X60, Z60 */
  1778. "\\_SB.PCI0.IDE0.SCND.MSTR", /* all others */
  1779. ); /* A21e, R30, R31 */
  1780. IBM_HANDLE(bay_ej, bay, "_EJ3", /* 600e/x, A2xm/p, A3x */
  1781. "_EJ0", /* all others */
  1782. ); /* 570,A21e,G4x,R30,R31,R32,R40e,R50e */
  1783. IBM_HANDLE(bay2, root, "\\_SB.PCI0.IDE0.PRIM.SLAV", /* A3x, R32 */
  1784. "\\_SB.PCI0.IDE0.IDEP.IDPS", /* 600e/x, 770e, 770x */
  1785. ); /* all others */
  1786. IBM_HANDLE(bay2_ej, bay2, "_EJ3", /* 600e/x, 770e, A3x */
  1787. "_EJ0", /* 770x */
  1788. ); /* all others */
  1789. static int __init bay_init(struct ibm_init_struct *iibm)
  1790. {
  1791. vdbg_printk(TPACPI_DBG_INIT, "initializing bay subdriver\n");
  1792. IBM_ACPIHANDLE_INIT(bay);
  1793. if (bay_handle)
  1794. IBM_ACPIHANDLE_INIT(bay_ej);
  1795. IBM_ACPIHANDLE_INIT(bay2);
  1796. if (bay2_handle)
  1797. IBM_ACPIHANDLE_INIT(bay2_ej);
  1798. tp_features.bay_status = bay_handle &&
  1799. acpi_evalf(bay_handle, NULL, "_STA", "qv");
  1800. tp_features.bay_status2 = bay2_handle &&
  1801. acpi_evalf(bay2_handle, NULL, "_STA", "qv");
  1802. tp_features.bay_eject = bay_handle && bay_ej_handle &&
  1803. (strlencmp(bay_ej_path, "_EJ0") == 0 || experimental);
  1804. tp_features.bay_eject2 = bay2_handle && bay2_ej_handle &&
  1805. (strlencmp(bay2_ej_path, "_EJ0") == 0 || experimental);
  1806. vdbg_printk(TPACPI_DBG_INIT,
  1807. "bay 1: status %s, eject %s; bay 2: status %s, eject %s\n",
  1808. str_supported(tp_features.bay_status),
  1809. str_supported(tp_features.bay_eject),
  1810. str_supported(tp_features.bay_status2),
  1811. str_supported(tp_features.bay_eject2));
  1812. return (tp_features.bay_status || tp_features.bay_eject ||
  1813. tp_features.bay_status2 || tp_features.bay_eject2)? 0 : 1;
  1814. }
  1815. static void bay_notify(struct ibm_struct *ibm, u32 event)
  1816. {
  1817. acpi_bus_generate_event(ibm->acpi->device, event, 0);
  1818. }
  1819. #define bay_occupied(b) (_sta(b##_handle) & 1)
  1820. static int bay_read(char *p)
  1821. {
  1822. int len = 0;
  1823. int occupied = bay_occupied(bay);
  1824. int occupied2 = bay_occupied(bay2);
  1825. int eject, eject2;
  1826. len += sprintf(p + len, "status:\t\t%s\n",
  1827. tp_features.bay_status ?
  1828. (occupied ? "occupied" : "unoccupied") :
  1829. "not supported");
  1830. if (tp_features.bay_status2)
  1831. len += sprintf(p + len, "status2:\t%s\n", occupied2 ?
  1832. "occupied" : "unoccupied");
  1833. eject = tp_features.bay_eject && occupied;
  1834. eject2 = tp_features.bay_eject2 && occupied2;
  1835. if (eject && eject2)
  1836. len += sprintf(p + len, "commands:\teject, eject2\n");
  1837. else if (eject)
  1838. len += sprintf(p + len, "commands:\teject\n");
  1839. else if (eject2)
  1840. len += sprintf(p + len, "commands:\teject2\n");
  1841. return len;
  1842. }
  1843. static int bay_write(char *buf)
  1844. {
  1845. char *cmd;
  1846. if (!tp_features.bay_eject && !tp_features.bay_eject2)
  1847. return -ENODEV;
  1848. while ((cmd = next_cmd(&buf))) {
  1849. if (tp_features.bay_eject && strlencmp(cmd, "eject") == 0) {
  1850. if (!acpi_evalf(bay_ej_handle, NULL, NULL, "vd", 1))
  1851. return -EIO;
  1852. } else if (tp_features.bay_eject2 &&
  1853. strlencmp(cmd, "eject2") == 0) {
  1854. if (!acpi_evalf(bay2_ej_handle, NULL, NULL, "vd", 1))
  1855. return -EIO;
  1856. } else
  1857. return -EINVAL;
  1858. }
  1859. return 0;
  1860. }
  1861. static struct tp_acpi_drv_struct ibm_bay_acpidriver = {
  1862. .notify = bay_notify,
  1863. .handle = &bay_handle,
  1864. .type = ACPI_SYSTEM_NOTIFY,
  1865. };
  1866. static struct ibm_struct bay_driver_data = {
  1867. .name = "bay",
  1868. .read = bay_read,
  1869. .write = bay_write,
  1870. .acpi = &ibm_bay_acpidriver,
  1871. };
  1872. #endif /* CONFIG_THINKPAD_ACPI_BAY */
  1873. /*************************************************************************
  1874. * CMOS subdriver
  1875. */
  1876. /* sysfs cmos_command -------------------------------------------------- */
  1877. static ssize_t cmos_command_store(struct device *dev,
  1878. struct device_attribute *attr,
  1879. const char *buf, size_t count)
  1880. {
  1881. unsigned long cmos_cmd;
  1882. int res;
  1883. if (parse_strtoul(buf, 21, &cmos_cmd))
  1884. return -EINVAL;
  1885. res = issue_thinkpad_cmos_command(cmos_cmd);
  1886. return (res)? res : count;
  1887. }
  1888. static struct device_attribute dev_attr_cmos_command =
  1889. __ATTR(cmos_command, S_IWUSR, NULL, cmos_command_store);
  1890. /* --------------------------------------------------------------------- */
  1891. static int __init cmos_init(struct ibm_init_struct *iibm)
  1892. {
  1893. int res;
  1894. vdbg_printk(TPACPI_DBG_INIT,
  1895. "initializing cmos commands subdriver\n");
  1896. IBM_ACPIHANDLE_INIT(cmos);
  1897. vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
  1898. str_supported(cmos_handle != NULL));
  1899. res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
  1900. if (res)
  1901. return res;
  1902. return (cmos_handle)? 0 : 1;
  1903. }
  1904. static void cmos_exit(void)
  1905. {
  1906. device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
  1907. }
  1908. static int cmos_read(char *p)
  1909. {
  1910. int len = 0;
  1911. /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
  1912. R30, R31, T20-22, X20-21 */
  1913. if (!cmos_handle)
  1914. len += sprintf(p + len, "status:\t\tnot supported\n");
  1915. else {
  1916. len += sprintf(p + len, "status:\t\tsupported\n");
  1917. len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-21)\n");
  1918. }
  1919. return len;
  1920. }
  1921. static int cmos_write(char *buf)
  1922. {
  1923. char *cmd;
  1924. int cmos_cmd, res;
  1925. while ((cmd = next_cmd(&buf))) {
  1926. if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
  1927. cmos_cmd >= 0 && cmos_cmd <= 21) {
  1928. /* cmos_cmd set */
  1929. } else
  1930. return -EINVAL;
  1931. res = issue_thinkpad_cmos_command(cmos_cmd);
  1932. if (res)
  1933. return res;
  1934. }
  1935. return 0;
  1936. }
  1937. static struct ibm_struct cmos_driver_data = {
  1938. .name = "cmos",
  1939. .read = cmos_read,
  1940. .write = cmos_write,
  1941. .exit = cmos_exit,
  1942. };
  1943. /*************************************************************************
  1944. * LED subdriver
  1945. */
  1946. static enum led_access_mode led_supported;
  1947. IBM_HANDLE(led, ec, "SLED", /* 570 */
  1948. "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
  1949. "LED", /* all others */
  1950. ); /* R30, R31 */
  1951. static int __init led_init(struct ibm_init_struct *iibm)
  1952. {
  1953. vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
  1954. IBM_ACPIHANDLE_INIT(led);
  1955. if (!led_handle)
  1956. /* led not supported on R30, R31 */
  1957. led_supported = TPACPI_LED_NONE;
  1958. else if (strlencmp(led_path, "SLED") == 0)
  1959. /* 570 */
  1960. led_supported = TPACPI_LED_570;
  1961. else if (strlencmp(led_path, "SYSL") == 0)
  1962. /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
  1963. led_supported = TPACPI_LED_OLD;
  1964. else
  1965. /* all others */
  1966. led_supported = TPACPI_LED_NEW;
  1967. vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
  1968. str_supported(led_supported), led_supported);
  1969. return (led_supported != TPACPI_LED_NONE)? 0 : 1;
  1970. }
  1971. #define led_status(s) ((s) == 0 ? "off" : ((s) == 1 ? "on" : "blinking"))
  1972. static int led_read(char *p)
  1973. {
  1974. int len = 0;
  1975. if (!led_supported) {
  1976. len += sprintf(p + len, "status:\t\tnot supported\n");
  1977. return len;
  1978. }
  1979. len += sprintf(p + len, "status:\t\tsupported\n");
  1980. if (led_supported == TPACPI_LED_570) {
  1981. /* 570 */
  1982. int i, status;
  1983. for (i = 0; i < 8; i++) {
  1984. if (!acpi_evalf(ec_handle,
  1985. &status, "GLED", "dd", 1 << i))
  1986. return -EIO;
  1987. len += sprintf(p + len, "%d:\t\t%s\n",
  1988. i, led_status(status));
  1989. }
  1990. }
  1991. len += sprintf(p + len, "commands:\t"
  1992. "<led> on, <led> off, <led> blink (<led> is 0-7)\n");
  1993. return len;
  1994. }
  1995. /* off, on, blink */
  1996. static const int led_sled_arg1[] = { 0, 1, 3 };
  1997. static const int led_exp_hlbl[] = { 0, 0, 1 }; /* led# * */
  1998. static const int led_exp_hlcl[] = { 0, 1, 1 }; /* led# * */
  1999. static const int led_led_arg1[] = { 0, 0x80, 0xc0 };
  2000. static int led_write(char *buf)
  2001. {
  2002. char *cmd;
  2003. int led, ind, ret;
  2004. if (!led_supported)
  2005. return -ENODEV;
  2006. while ((cmd = next_cmd(&buf))) {
  2007. if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 7)
  2008. return -EINVAL;
  2009. if (strstr(cmd, "off")) {
  2010. ind = 0;
  2011. } else if (strstr(cmd, "on")) {
  2012. ind = 1;
  2013. } else if (strstr(cmd, "blink")) {
  2014. ind = 2;
  2015. } else
  2016. return -EINVAL;
  2017. if (led_supported == TPACPI_LED_570) {
  2018. /* 570 */
  2019. led = 1 << led;
  2020. if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
  2021. led, led_sled_arg1[ind]))
  2022. return -EIO;
  2023. } else if (led_supported == TPACPI_LED_OLD) {
  2024. /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
  2025. led = 1 << led;
  2026. ret = ec_write(TPACPI_LED_EC_HLMS, led);
  2027. if (ret >= 0)
  2028. ret =
  2029. ec_write(TPACPI_LED_EC_HLBL,
  2030. led * led_exp_hlbl[ind]);
  2031. if (ret >= 0)
  2032. ret =
  2033. ec_write(TPACPI_LED_EC_HLCL,
  2034. led * led_exp_hlcl[ind]);
  2035. if (ret < 0)
  2036. return ret;
  2037. } else {
  2038. /* all others */
  2039. if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
  2040. led, led_led_arg1[ind]))
  2041. return -EIO;
  2042. }
  2043. }
  2044. return 0;
  2045. }
  2046. static struct ibm_struct led_driver_data = {
  2047. .name = "led",
  2048. .read = led_read,
  2049. .write = led_write,
  2050. };
  2051. /*************************************************************************
  2052. * Beep subdriver
  2053. */
  2054. IBM_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
  2055. static int __init beep_init(struct ibm_init_struct *iibm)
  2056. {
  2057. vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
  2058. IBM_ACPIHANDLE_INIT(beep);
  2059. vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
  2060. str_supported(beep_handle != NULL));
  2061. return (beep_handle)? 0 : 1;
  2062. }
  2063. static int beep_read(char *p)
  2064. {
  2065. int len = 0;
  2066. if (!beep_handle)
  2067. len += sprintf(p + len, "status:\t\tnot supported\n");
  2068. else {
  2069. len += sprintf(p + len, "status:\t\tsupported\n");
  2070. len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-17)\n");
  2071. }
  2072. return len;
  2073. }
  2074. static int beep_write(char *buf)
  2075. {
  2076. char *cmd;
  2077. int beep_cmd;
  2078. if (!beep_handle)
  2079. return -ENODEV;
  2080. while ((cmd = next_cmd(&buf))) {
  2081. if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
  2082. beep_cmd >= 0 && beep_cmd <= 17) {
  2083. /* beep_cmd set */
  2084. } else
  2085. return -EINVAL;
  2086. if (!acpi_evalf(beep_handle, NULL, NULL, "vdd", beep_cmd, 0))
  2087. return -EIO;
  2088. }
  2089. return 0;
  2090. }
  2091. static struct ibm_struct beep_driver_data = {
  2092. .name = "beep",
  2093. .read = beep_read,
  2094. .write = beep_write,
  2095. };
  2096. /*************************************************************************
  2097. * Thermal subdriver
  2098. */
  2099. static enum thermal_access_mode thermal_read_mode;
  2100. /* sysfs temp##_input -------------------------------------------------- */
  2101. static ssize_t thermal_temp_input_show(struct device *dev,
  2102. struct device_attribute *attr,
  2103. char *buf)
  2104. {
  2105. struct sensor_device_attribute *sensor_attr =
  2106. to_sensor_dev_attr(attr);
  2107. int idx = sensor_attr->index;
  2108. s32 value;
  2109. int res;
  2110. res = thermal_get_sensor(idx, &value);
  2111. if (res)
  2112. return res;
  2113. if (value == TP_EC_THERMAL_TMP_NA * 1000)
  2114. return -ENXIO;
  2115. return snprintf(buf, PAGE_SIZE, "%d\n", value);
  2116. }
  2117. #define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
  2118. SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, thermal_temp_input_show, NULL, _idxB)
  2119. static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
  2120. THERMAL_SENSOR_ATTR_TEMP(1, 0),
  2121. THERMAL_SENSOR_ATTR_TEMP(2, 1),
  2122. THERMAL_SENSOR_ATTR_TEMP(3, 2),
  2123. THERMAL_SENSOR_ATTR_TEMP(4, 3),
  2124. THERMAL_SENSOR_ATTR_TEMP(5, 4),
  2125. THERMAL_SENSOR_ATTR_TEMP(6, 5),
  2126. THERMAL_SENSOR_ATTR_TEMP(7, 6),
  2127. THERMAL_SENSOR_ATTR_TEMP(8, 7),
  2128. THERMAL_SENSOR_ATTR_TEMP(9, 8),
  2129. THERMAL_SENSOR_ATTR_TEMP(10, 9),
  2130. THERMAL_SENSOR_ATTR_TEMP(11, 10),
  2131. THERMAL_SENSOR_ATTR_TEMP(12, 11),
  2132. THERMAL_SENSOR_ATTR_TEMP(13, 12),
  2133. THERMAL_SENSOR_ATTR_TEMP(14, 13),
  2134. THERMAL_SENSOR_ATTR_TEMP(15, 14),
  2135. THERMAL_SENSOR_ATTR_TEMP(16, 15),
  2136. };
  2137. #define THERMAL_ATTRS(X) \
  2138. &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
  2139. static struct attribute *thermal_temp_input_attr[] = {
  2140. THERMAL_ATTRS(8),
  2141. THERMAL_ATTRS(9),
  2142. THERMAL_ATTRS(10),
  2143. THERMAL_ATTRS(11),
  2144. THERMAL_ATTRS(12),
  2145. THERMAL_ATTRS(13),
  2146. THERMAL_ATTRS(14),
  2147. THERMAL_ATTRS(15),
  2148. THERMAL_ATTRS(0),
  2149. THERMAL_ATTRS(1),
  2150. THERMAL_ATTRS(2),
  2151. THERMAL_ATTRS(3),
  2152. THERMAL_ATTRS(4),
  2153. THERMAL_ATTRS(5),
  2154. THERMAL_ATTRS(6),
  2155. THERMAL_ATTRS(7),
  2156. NULL
  2157. };
  2158. static const struct attribute_group thermal_temp_input16_group = {
  2159. .attrs = thermal_temp_input_attr
  2160. };
  2161. static const struct attribute_group thermal_temp_input8_group = {
  2162. .attrs = &thermal_temp_input_attr[8]
  2163. };
  2164. #undef THERMAL_SENSOR_ATTR_TEMP
  2165. #undef THERMAL_ATTRS
  2166. /* --------------------------------------------------------------------- */
  2167. static int __init thermal_init(struct ibm_init_struct *iibm)
  2168. {
  2169. u8 t, ta1, ta2;
  2170. int i;
  2171. int acpi_tmp7;
  2172. int res;
  2173. vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
  2174. acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
  2175. if (ibm_thinkpad_ec_found && experimental) {
  2176. /*
  2177. * Direct EC access mode: sensors at registers
  2178. * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
  2179. * non-implemented, thermal sensors return 0x80 when
  2180. * not available
  2181. */
  2182. ta1 = ta2 = 0;
  2183. for (i = 0; i < 8; i++) {
  2184. if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
  2185. ta1 |= t;
  2186. } else {
  2187. ta1 = 0;
  2188. break;
  2189. }
  2190. if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
  2191. ta2 |= t;
  2192. } else {
  2193. ta1 = 0;
  2194. break;
  2195. }
  2196. }
  2197. if (ta1 == 0) {
  2198. /* This is sheer paranoia, but we handle it anyway */
  2199. if (acpi_tmp7) {
  2200. printk(IBM_ERR
  2201. "ThinkPad ACPI EC access misbehaving, "
  2202. "falling back to ACPI TMPx access mode\n");
  2203. thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
  2204. } else {
  2205. printk(IBM_ERR
  2206. "ThinkPad ACPI EC access misbehaving, "
  2207. "disabling thermal sensors access\n");
  2208. thermal_read_mode = TPACPI_THERMAL_NONE;
  2209. }
  2210. } else {
  2211. thermal_read_mode =
  2212. (ta2 != 0) ?
  2213. TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
  2214. }
  2215. } else if (acpi_tmp7) {
  2216. if (acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
  2217. /* 600e/x, 770e, 770x */
  2218. thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
  2219. } else {
  2220. /* Standard ACPI TMPx access, max 8 sensors */
  2221. thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
  2222. }
  2223. } else {
  2224. /* temperatures not supported on 570, G4x, R30, R31, R32 */
  2225. thermal_read_mode = TPACPI_THERMAL_NONE;
  2226. }
  2227. vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
  2228. str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
  2229. thermal_read_mode);
  2230. switch(thermal_read_mode) {
  2231. case TPACPI_THERMAL_TPEC_16:
  2232. res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
  2233. &thermal_temp_input16_group);
  2234. if (res)
  2235. return res;
  2236. break;
  2237. case TPACPI_THERMAL_TPEC_8:
  2238. case TPACPI_THERMAL_ACPI_TMP07:
  2239. case TPACPI_THERMAL_ACPI_UPDT:
  2240. res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
  2241. &thermal_temp_input8_group);
  2242. if (res)
  2243. return res;
  2244. break;
  2245. case TPACPI_THERMAL_NONE:
  2246. default:
  2247. return 1;
  2248. }
  2249. return 0;
  2250. }
  2251. static void thermal_exit(void)
  2252. {
  2253. switch(thermal_read_mode) {
  2254. case TPACPI_THERMAL_TPEC_16:
  2255. sysfs_remove_group(&tpacpi_pdev->dev.kobj,
  2256. &thermal_temp_input16_group);
  2257. break;
  2258. case TPACPI_THERMAL_TPEC_8:
  2259. case TPACPI_THERMAL_ACPI_TMP07:
  2260. case TPACPI_THERMAL_ACPI_UPDT:
  2261. sysfs_remove_group(&tpacpi_pdev->dev.kobj,
  2262. &thermal_temp_input16_group);
  2263. break;
  2264. case TPACPI_THERMAL_NONE:
  2265. default:
  2266. break;
  2267. }
  2268. }
  2269. /* idx is zero-based */
  2270. static int thermal_get_sensor(int idx, s32 *value)
  2271. {
  2272. int t;
  2273. s8 tmp;
  2274. char tmpi[5];
  2275. t = TP_EC_THERMAL_TMP0;
  2276. switch (thermal_read_mode) {
  2277. #if TPACPI_MAX_THERMAL_SENSORS >= 16
  2278. case TPACPI_THERMAL_TPEC_16:
  2279. if (idx >= 8 && idx <= 15) {
  2280. t = TP_EC_THERMAL_TMP8;
  2281. idx -= 8;
  2282. }
  2283. /* fallthrough */
  2284. #endif
  2285. case TPACPI_THERMAL_TPEC_8:
  2286. if (idx <= 7) {
  2287. if (!acpi_ec_read(t + idx, &tmp))
  2288. return -EIO;
  2289. *value = tmp * 1000;
  2290. return 0;
  2291. }
  2292. break;
  2293. case TPACPI_THERMAL_ACPI_UPDT:
  2294. if (idx <= 7) {
  2295. snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
  2296. if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
  2297. return -EIO;
  2298. if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
  2299. return -EIO;
  2300. *value = (t - 2732) * 100;
  2301. return 0;
  2302. }
  2303. break;
  2304. case TPACPI_THERMAL_ACPI_TMP07:
  2305. if (idx <= 7) {
  2306. snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
  2307. if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
  2308. return -EIO;
  2309. *value = t * 1000;
  2310. return 0;
  2311. }
  2312. break;
  2313. case TPACPI_THERMAL_NONE:
  2314. default:
  2315. return -ENOSYS;
  2316. }
  2317. return -EINVAL;
  2318. }
  2319. static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
  2320. {
  2321. int res, i;
  2322. int n;
  2323. n = 8;
  2324. i = 0;
  2325. if (!s)
  2326. return -EINVAL;
  2327. if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
  2328. n = 16;
  2329. for(i = 0 ; i < n; i++) {
  2330. res = thermal_get_sensor(i, &s->temp[i]);
  2331. if (res)
  2332. return res;
  2333. }
  2334. return n;
  2335. }
  2336. static int thermal_read(char *p)
  2337. {
  2338. int len = 0;
  2339. int n, i;
  2340. struct ibm_thermal_sensors_struct t;
  2341. n = thermal_get_sensors(&t);
  2342. if (unlikely(n < 0))
  2343. return n;
  2344. len += sprintf(p + len, "temperatures:\t");
  2345. if (n > 0) {
  2346. for (i = 0; i < (n - 1); i++)
  2347. len += sprintf(p + len, "%d ", t.temp[i] / 1000);
  2348. len += sprintf(p + len, "%d\n", t.temp[i] / 1000);
  2349. } else
  2350. len += sprintf(p + len, "not supported\n");
  2351. return len;
  2352. }
  2353. static struct ibm_struct thermal_driver_data = {
  2354. .name = "thermal",
  2355. .read = thermal_read,
  2356. .exit = thermal_exit,
  2357. };
  2358. /*************************************************************************
  2359. * EC Dump subdriver
  2360. */
  2361. static u8 ecdump_regs[256];
  2362. static int ecdump_read(char *p)
  2363. {
  2364. int len = 0;
  2365. int i, j;
  2366. u8 v;
  2367. len += sprintf(p + len, "EC "
  2368. " +00 +01 +02 +03 +04 +05 +06 +07"
  2369. " +08 +09 +0a +0b +0c +0d +0e +0f\n");
  2370. for (i = 0; i < 256; i += 16) {
  2371. len += sprintf(p + len, "EC 0x%02x:", i);
  2372. for (j = 0; j < 16; j++) {
  2373. if (!acpi_ec_read(i + j, &v))
  2374. break;
  2375. if (v != ecdump_regs[i + j])
  2376. len += sprintf(p + len, " *%02x", v);
  2377. else
  2378. len += sprintf(p + len, " %02x", v);
  2379. ecdump_regs[i + j] = v;
  2380. }
  2381. len += sprintf(p + len, "\n");
  2382. if (j != 16)
  2383. break;
  2384. }
  2385. /* These are way too dangerous to advertise openly... */
  2386. #if 0
  2387. len += sprintf(p + len, "commands:\t0x<offset> 0x<value>"
  2388. " (<offset> is 00-ff, <value> is 00-ff)\n");
  2389. len += sprintf(p + len, "commands:\t0x<offset> <value> "
  2390. " (<offset> is 00-ff, <value> is 0-255)\n");
  2391. #endif
  2392. return len;
  2393. }
  2394. static int ecdump_write(char *buf)
  2395. {
  2396. char *cmd;
  2397. int i, v;
  2398. while ((cmd = next_cmd(&buf))) {
  2399. if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
  2400. /* i and v set */
  2401. } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
  2402. /* i and v set */
  2403. } else
  2404. return -EINVAL;
  2405. if (i >= 0 && i < 256 && v >= 0 && v < 256) {
  2406. if (!acpi_ec_write(i, v))
  2407. return -EIO;
  2408. } else
  2409. return -EINVAL;
  2410. }
  2411. return 0;
  2412. }
  2413. static struct ibm_struct ecdump_driver_data = {
  2414. .name = "ecdump",
  2415. .read = ecdump_read,
  2416. .write = ecdump_write,
  2417. .flags.experimental = 1,
  2418. };
  2419. /*************************************************************************
  2420. * Backlight/brightness subdriver
  2421. */
  2422. static struct backlight_device *ibm_backlight_device;
  2423. static struct backlight_ops ibm_backlight_data = {
  2424. .get_brightness = brightness_get,
  2425. .update_status = brightness_update_status,
  2426. };
  2427. static int __init brightness_init(struct ibm_init_struct *iibm)
  2428. {
  2429. int b;
  2430. vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
  2431. b = brightness_get(NULL);
  2432. if (b < 0)
  2433. return b;
  2434. ibm_backlight_device = backlight_device_register(
  2435. TPACPI_BACKLIGHT_DEV_NAME, NULL, NULL,
  2436. &ibm_backlight_data);
  2437. if (IS_ERR(ibm_backlight_device)) {
  2438. printk(IBM_ERR "Could not register backlight device\n");
  2439. return PTR_ERR(ibm_backlight_device);
  2440. }
  2441. vdbg_printk(TPACPI_DBG_INIT, "brightness is supported\n");
  2442. ibm_backlight_device->props.max_brightness = 7;
  2443. ibm_backlight_device->props.brightness = b;
  2444. backlight_update_status(ibm_backlight_device);
  2445. return 0;
  2446. }
  2447. static void brightness_exit(void)
  2448. {
  2449. if (ibm_backlight_device) {
  2450. vdbg_printk(TPACPI_DBG_EXIT,
  2451. "calling backlight_device_unregister()\n");
  2452. backlight_device_unregister(ibm_backlight_device);
  2453. ibm_backlight_device = NULL;
  2454. }
  2455. }
  2456. static int brightness_update_status(struct backlight_device *bd)
  2457. {
  2458. return brightness_set(
  2459. (bd->props.fb_blank == FB_BLANK_UNBLANK &&
  2460. bd->props.power == FB_BLANK_UNBLANK) ?
  2461. bd->props.brightness : 0);
  2462. }
  2463. static int brightness_get(struct backlight_device *bd)
  2464. {
  2465. u8 level;
  2466. if (!acpi_ec_read(brightness_offset, &level))
  2467. return -EIO;
  2468. level &= 0x7;
  2469. return level;
  2470. }
  2471. static int brightness_set(int value)
  2472. {
  2473. int cmos_cmd, inc, i;
  2474. int current_value = brightness_get(NULL);
  2475. value &= 7;
  2476. cmos_cmd = value > current_value ? TP_CMOS_BRIGHTNESS_UP : TP_CMOS_BRIGHTNESS_DOWN;
  2477. inc = value > current_value ? 1 : -1;
  2478. for (i = current_value; i != value; i += inc) {
  2479. if (issue_thinkpad_cmos_command(cmos_cmd))
  2480. return -EIO;
  2481. if (!acpi_ec_write(brightness_offset, i + inc))
  2482. return -EIO;
  2483. }
  2484. return 0;
  2485. }
  2486. static int brightness_read(char *p)
  2487. {
  2488. int len = 0;
  2489. int level;
  2490. if ((level = brightness_get(NULL)) < 0) {
  2491. len += sprintf(p + len, "level:\t\tunreadable\n");
  2492. } else {
  2493. len += sprintf(p + len, "level:\t\t%d\n", level & 0x7);
  2494. len += sprintf(p + len, "commands:\tup, down\n");
  2495. len += sprintf(p + len, "commands:\tlevel <level>"
  2496. " (<level> is 0-7)\n");
  2497. }
  2498. return len;
  2499. }
  2500. static int brightness_write(char *buf)
  2501. {
  2502. int level;
  2503. int new_level;
  2504. char *cmd;
  2505. while ((cmd = next_cmd(&buf))) {
  2506. if ((level = brightness_get(NULL)) < 0)
  2507. return level;
  2508. level &= 7;
  2509. if (strlencmp(cmd, "up") == 0) {
  2510. new_level = level == 7 ? 7 : level + 1;
  2511. } else if (strlencmp(cmd, "down") == 0) {
  2512. new_level = level == 0 ? 0 : level - 1;
  2513. } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
  2514. new_level >= 0 && new_level <= 7) {
  2515. /* new_level set */
  2516. } else
  2517. return -EINVAL;
  2518. brightness_set(new_level);
  2519. }
  2520. return 0;
  2521. }
  2522. static struct ibm_struct brightness_driver_data = {
  2523. .name = "brightness",
  2524. .read = brightness_read,
  2525. .write = brightness_write,
  2526. .exit = brightness_exit,
  2527. };
  2528. /*************************************************************************
  2529. * Volume subdriver
  2530. */
  2531. static int volume_read(char *p)
  2532. {
  2533. int len = 0;
  2534. u8 level;
  2535. if (!acpi_ec_read(volume_offset, &level)) {
  2536. len += sprintf(p + len, "level:\t\tunreadable\n");
  2537. } else {
  2538. len += sprintf(p + len, "level:\t\t%d\n", level & 0xf);
  2539. len += sprintf(p + len, "mute:\t\t%s\n", onoff(level, 6));
  2540. len += sprintf(p + len, "commands:\tup, down, mute\n");
  2541. len += sprintf(p + len, "commands:\tlevel <level>"
  2542. " (<level> is 0-15)\n");
  2543. }
  2544. return len;
  2545. }
  2546. static int volume_write(char *buf)
  2547. {
  2548. int cmos_cmd, inc, i;
  2549. u8 level, mute;
  2550. int new_level, new_mute;
  2551. char *cmd;
  2552. while ((cmd = next_cmd(&buf))) {
  2553. if (!acpi_ec_read(volume_offset, &level))
  2554. return -EIO;
  2555. new_mute = mute = level & 0x40;
  2556. new_level = level = level & 0xf;
  2557. if (strlencmp(cmd, "up") == 0) {
  2558. if (mute)
  2559. new_mute = 0;
  2560. else
  2561. new_level = level == 15 ? 15 : level + 1;
  2562. } else if (strlencmp(cmd, "down") == 0) {
  2563. if (mute)
  2564. new_mute = 0;
  2565. else
  2566. new_level = level == 0 ? 0 : level - 1;
  2567. } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
  2568. new_level >= 0 && new_level <= 15) {
  2569. /* new_level set */
  2570. } else if (strlencmp(cmd, "mute") == 0) {
  2571. new_mute = 0x40;
  2572. } else
  2573. return -EINVAL;
  2574. if (new_level != level) { /* mute doesn't change */
  2575. cmos_cmd = new_level > level ? TP_CMOS_VOLUME_UP : TP_CMOS_VOLUME_DOWN;
  2576. inc = new_level > level ? 1 : -1;
  2577. if (mute && (issue_thinkpad_cmos_command(cmos_cmd) ||
  2578. !acpi_ec_write(volume_offset, level)))
  2579. return -EIO;
  2580. for (i = level; i != new_level; i += inc)
  2581. if (issue_thinkpad_cmos_command(cmos_cmd) ||
  2582. !acpi_ec_write(volume_offset, i + inc))
  2583. return -EIO;
  2584. if (mute && (issue_thinkpad_cmos_command(TP_CMOS_VOLUME_MUTE) ||
  2585. !acpi_ec_write(volume_offset,
  2586. new_level + mute)))
  2587. return -EIO;
  2588. }
  2589. if (new_mute != mute) { /* level doesn't change */
  2590. cmos_cmd = new_mute ? TP_CMOS_VOLUME_MUTE : TP_CMOS_VOLUME_UP;
  2591. if (issue_thinkpad_cmos_command(cmos_cmd) ||
  2592. !acpi_ec_write(volume_offset, level + new_mute))
  2593. return -EIO;
  2594. }
  2595. }
  2596. return 0;
  2597. }
  2598. static struct ibm_struct volume_driver_data = {
  2599. .name = "volume",
  2600. .read = volume_read,
  2601. .write = volume_write,
  2602. };
  2603. /*************************************************************************
  2604. * Fan subdriver
  2605. */
  2606. /*
  2607. * FAN ACCESS MODES
  2608. *
  2609. * TPACPI_FAN_RD_ACPI_GFAN:
  2610. * ACPI GFAN method: returns fan level
  2611. *
  2612. * see TPACPI_FAN_WR_ACPI_SFAN
  2613. * EC 0x2f (HFSP) not available if GFAN exists
  2614. *
  2615. * TPACPI_FAN_WR_ACPI_SFAN:
  2616. * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
  2617. *
  2618. * EC 0x2f (HFSP) might be available *for reading*, but do not use
  2619. * it for writing.
  2620. *
  2621. * TPACPI_FAN_WR_TPEC:
  2622. * ThinkPad EC register 0x2f (HFSP): fan control loop mode
  2623. * Supported on almost all ThinkPads
  2624. *
  2625. * Fan speed changes of any sort (including those caused by the
  2626. * disengaged mode) are usually done slowly by the firmware as the
  2627. * maximum ammount of fan duty cycle change per second seems to be
  2628. * limited.
  2629. *
  2630. * Reading is not available if GFAN exists.
  2631. * Writing is not available if SFAN exists.
  2632. *
  2633. * Bits
  2634. * 7 automatic mode engaged;
  2635. * (default operation mode of the ThinkPad)
  2636. * fan level is ignored in this mode.
  2637. * 6 full speed mode (takes precedence over bit 7);
  2638. * not available on all thinkpads. May disable
  2639. * the tachometer while the fan controller ramps up
  2640. * the speed (which can take up to a few *minutes*).
  2641. * Speeds up fan to 100% duty-cycle, which is far above
  2642. * the standard RPM levels. It is not impossible that
  2643. * it could cause hardware damage.
  2644. * 5-3 unused in some models. Extra bits for fan level
  2645. * in others, but still useless as all values above
  2646. * 7 map to the same speed as level 7 in these models.
  2647. * 2-0 fan level (0..7 usually)
  2648. * 0x00 = stop
  2649. * 0x07 = max (set when temperatures critical)
  2650. * Some ThinkPads may have other levels, see
  2651. * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
  2652. *
  2653. * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
  2654. * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
  2655. * does so, its initial value is meaningless (0x07).
  2656. *
  2657. * For firmware bugs, refer to:
  2658. * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
  2659. *
  2660. * ----
  2661. *
  2662. * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
  2663. * Main fan tachometer reading (in RPM)
  2664. *
  2665. * This register is present on all ThinkPads with a new-style EC, and
  2666. * it is known not to be present on the A21m/e, and T22, as there is
  2667. * something else in offset 0x84 according to the ACPI DSDT. Other
  2668. * ThinkPads from this same time period (and earlier) probably lack the
  2669. * tachometer as well.
  2670. *
  2671. * Unfortunately a lot of ThinkPads with new-style ECs but whose firwmare
  2672. * was never fixed by IBM to report the EC firmware version string
  2673. * probably support the tachometer (like the early X models), so
  2674. * detecting it is quite hard. We need more data to know for sure.
  2675. *
  2676. * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
  2677. * might result.
  2678. *
  2679. * FIRMWARE BUG: may go stale while the EC is switching to full speed
  2680. * mode.
  2681. *
  2682. * For firmware bugs, refer to:
  2683. * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
  2684. *
  2685. * TPACPI_FAN_WR_ACPI_FANS:
  2686. * ThinkPad X31, X40, X41. Not available in the X60.
  2687. *
  2688. * FANS ACPI handle: takes three arguments: low speed, medium speed,
  2689. * high speed. ACPI DSDT seems to map these three speeds to levels
  2690. * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
  2691. * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
  2692. *
  2693. * The speeds are stored on handles
  2694. * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
  2695. *
  2696. * There are three default speed sets, acessible as handles:
  2697. * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
  2698. *
  2699. * ACPI DSDT switches which set is in use depending on various
  2700. * factors.
  2701. *
  2702. * TPACPI_FAN_WR_TPEC is also available and should be used to
  2703. * command the fan. The X31/X40/X41 seems to have 8 fan levels,
  2704. * but the ACPI tables just mention level 7.
  2705. */
  2706. static enum fan_status_access_mode fan_status_access_mode;
  2707. static enum fan_control_access_mode fan_control_access_mode;
  2708. static enum fan_control_commands fan_control_commands;
  2709. static u8 fan_control_initial_status;
  2710. static u8 fan_control_desired_level;
  2711. static void fan_watchdog_fire(struct work_struct *ignored);
  2712. static int fan_watchdog_maxinterval;
  2713. static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
  2714. IBM_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
  2715. IBM_HANDLE(gfan, ec, "GFAN", /* 570 */
  2716. "\\FSPD", /* 600e/x, 770e, 770x */
  2717. ); /* all others */
  2718. IBM_HANDLE(sfan, ec, "SFAN", /* 570 */
  2719. "JFNS", /* 770x-JL */
  2720. ); /* all others */
  2721. /*
  2722. * SYSFS fan layout: hwmon compatible (device)
  2723. *
  2724. * pwm*_enable:
  2725. * 0: "disengaged" mode
  2726. * 1: manual mode
  2727. * 2: native EC "auto" mode (recommended, hardware default)
  2728. *
  2729. * pwm*: set speed in manual mode, ignored otherwise.
  2730. * 0 is level 0; 255 is level 7. Intermediate points done with linear
  2731. * interpolation.
  2732. *
  2733. * fan*_input: tachometer reading, RPM
  2734. *
  2735. *
  2736. * SYSFS fan layout: extensions
  2737. *
  2738. * fan_watchdog (driver):
  2739. * fan watchdog interval in seconds, 0 disables (default), max 120
  2740. */
  2741. /* sysfs fan pwm1_enable ----------------------------------------------- */
  2742. static ssize_t fan_pwm1_enable_show(struct device *dev,
  2743. struct device_attribute *attr,
  2744. char *buf)
  2745. {
  2746. int res, mode;
  2747. u8 status;
  2748. res = fan_get_status_safe(&status);
  2749. if (res)
  2750. return res;
  2751. if (unlikely(tp_features.fan_ctrl_status_undef)) {
  2752. if (status != fan_control_initial_status) {
  2753. tp_features.fan_ctrl_status_undef = 0;
  2754. } else {
  2755. /* Return most likely status. In fact, it
  2756. * might be the only possible status */
  2757. status = TP_EC_FAN_AUTO;
  2758. }
  2759. }
  2760. if (status & TP_EC_FAN_FULLSPEED) {
  2761. mode = 0;
  2762. } else if (status & TP_EC_FAN_AUTO) {
  2763. mode = 2;
  2764. } else
  2765. mode = 1;
  2766. return snprintf(buf, PAGE_SIZE, "%d\n", mode);
  2767. }
  2768. static ssize_t fan_pwm1_enable_store(struct device *dev,
  2769. struct device_attribute *attr,
  2770. const char *buf, size_t count)
  2771. {
  2772. unsigned long t;
  2773. int res, level;
  2774. if (parse_strtoul(buf, 2, &t))
  2775. return -EINVAL;
  2776. switch (t) {
  2777. case 0:
  2778. level = TP_EC_FAN_FULLSPEED;
  2779. break;
  2780. case 1:
  2781. level = TPACPI_FAN_LAST_LEVEL;
  2782. break;
  2783. case 2:
  2784. level = TP_EC_FAN_AUTO;
  2785. break;
  2786. case 3:
  2787. /* reserved for software-controlled auto mode */
  2788. return -ENOSYS;
  2789. default:
  2790. return -EINVAL;
  2791. }
  2792. res = fan_set_level_safe(level);
  2793. if (res == -ENXIO)
  2794. return -EINVAL;
  2795. else if (res < 0)
  2796. return res;
  2797. fan_watchdog_reset();
  2798. return count;
  2799. }
  2800. static struct device_attribute dev_attr_fan_pwm1_enable =
  2801. __ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
  2802. fan_pwm1_enable_show, fan_pwm1_enable_store);
  2803. /* sysfs fan pwm1 ------------------------------------------------------ */
  2804. static ssize_t fan_pwm1_show(struct device *dev,
  2805. struct device_attribute *attr,
  2806. char *buf)
  2807. {
  2808. int res;
  2809. u8 status;
  2810. res = fan_get_status_safe(&status);
  2811. if (res)
  2812. return res;
  2813. if (unlikely(tp_features.fan_ctrl_status_undef)) {
  2814. if (status != fan_control_initial_status) {
  2815. tp_features.fan_ctrl_status_undef = 0;
  2816. } else {
  2817. status = TP_EC_FAN_AUTO;
  2818. }
  2819. }
  2820. if ((status &
  2821. (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
  2822. status = fan_control_desired_level;
  2823. if (status > 7)
  2824. status = 7;
  2825. return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
  2826. }
  2827. static ssize_t fan_pwm1_store(struct device *dev,
  2828. struct device_attribute *attr,
  2829. const char *buf, size_t count)
  2830. {
  2831. unsigned long s;
  2832. int rc;
  2833. u8 status, newlevel;
  2834. if (parse_strtoul(buf, 255, &s))
  2835. return -EINVAL;
  2836. /* scale down from 0-255 to 0-7 */
  2837. newlevel = (s >> 5) & 0x07;
  2838. rc = mutex_lock_interruptible(&fan_mutex);
  2839. if (rc < 0)
  2840. return rc;
  2841. rc = fan_get_status(&status);
  2842. if (!rc && (status &
  2843. (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
  2844. rc = fan_set_level(newlevel);
  2845. if (rc == -ENXIO)
  2846. rc = -EINVAL;
  2847. else if (!rc) {
  2848. fan_update_desired_level(newlevel);
  2849. fan_watchdog_reset();
  2850. }
  2851. }
  2852. mutex_unlock(&fan_mutex);
  2853. return (rc)? rc : count;
  2854. }
  2855. static struct device_attribute dev_attr_fan_pwm1 =
  2856. __ATTR(pwm1, S_IWUSR | S_IRUGO,
  2857. fan_pwm1_show, fan_pwm1_store);
  2858. /* sysfs fan fan1_input ------------------------------------------------ */
  2859. static ssize_t fan_fan1_input_show(struct device *dev,
  2860. struct device_attribute *attr,
  2861. char *buf)
  2862. {
  2863. int res;
  2864. unsigned int speed;
  2865. res = fan_get_speed(&speed);
  2866. if (res < 0)
  2867. return res;
  2868. return snprintf(buf, PAGE_SIZE, "%u\n", speed);
  2869. }
  2870. static struct device_attribute dev_attr_fan_fan1_input =
  2871. __ATTR(fan1_input, S_IRUGO,
  2872. fan_fan1_input_show, NULL);
  2873. /* sysfs fan fan_watchdog (driver) ------------------------------------- */
  2874. static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
  2875. char *buf)
  2876. {
  2877. return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
  2878. }
  2879. static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
  2880. const char *buf, size_t count)
  2881. {
  2882. unsigned long t;
  2883. if (parse_strtoul(buf, 120, &t))
  2884. return -EINVAL;
  2885. if (!fan_control_allowed)
  2886. return -EPERM;
  2887. fan_watchdog_maxinterval = t;
  2888. fan_watchdog_reset();
  2889. return count;
  2890. }
  2891. static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
  2892. fan_fan_watchdog_show, fan_fan_watchdog_store);
  2893. /* --------------------------------------------------------------------- */
  2894. static struct attribute *fan_attributes[] = {
  2895. &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr,
  2896. &dev_attr_fan_fan1_input.attr,
  2897. NULL
  2898. };
  2899. static const struct attribute_group fan_attr_group = {
  2900. .attrs = fan_attributes,
  2901. };
  2902. static int __init fan_init(struct ibm_init_struct *iibm)
  2903. {
  2904. int rc;
  2905. vdbg_printk(TPACPI_DBG_INIT, "initializing fan subdriver\n");
  2906. mutex_init(&fan_mutex);
  2907. fan_status_access_mode = TPACPI_FAN_NONE;
  2908. fan_control_access_mode = TPACPI_FAN_WR_NONE;
  2909. fan_control_commands = 0;
  2910. fan_watchdog_maxinterval = 0;
  2911. tp_features.fan_ctrl_status_undef = 0;
  2912. fan_control_desired_level = 7;
  2913. IBM_ACPIHANDLE_INIT(fans);
  2914. IBM_ACPIHANDLE_INIT(gfan);
  2915. IBM_ACPIHANDLE_INIT(sfan);
  2916. if (gfan_handle) {
  2917. /* 570, 600e/x, 770e, 770x */
  2918. fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
  2919. } else {
  2920. /* all other ThinkPads: note that even old-style
  2921. * ThinkPad ECs supports the fan control register */
  2922. if (likely(acpi_ec_read(fan_status_offset,
  2923. &fan_control_initial_status))) {
  2924. fan_status_access_mode = TPACPI_FAN_RD_TPEC;
  2925. /* In some ThinkPads, neither the EC nor the ACPI
  2926. * DSDT initialize the fan status, and it ends up
  2927. * being set to 0x07 when it *could* be either
  2928. * 0x07 or 0x80.
  2929. *
  2930. * Enable for TP-1Y (T43), TP-78 (R51e),
  2931. * TP-76 (R52), TP-70 (T43, R52), which are known
  2932. * to be buggy. */
  2933. if (fan_control_initial_status == 0x07 &&
  2934. ibm_thinkpad_ec_found &&
  2935. ((ibm_thinkpad_ec_found[0] == '1' &&
  2936. ibm_thinkpad_ec_found[1] == 'Y') ||
  2937. (ibm_thinkpad_ec_found[0] == '7' &&
  2938. (ibm_thinkpad_ec_found[1] == '6' ||
  2939. ibm_thinkpad_ec_found[1] == '8' ||
  2940. ibm_thinkpad_ec_found[1] == '0'))
  2941. )) {
  2942. printk(IBM_NOTICE
  2943. "fan_init: initial fan status is "
  2944. "unknown, assuming it is in auto "
  2945. "mode\n");
  2946. tp_features.fan_ctrl_status_undef = 1;
  2947. }
  2948. } else {
  2949. printk(IBM_ERR
  2950. "ThinkPad ACPI EC access misbehaving, "
  2951. "fan status and control unavailable\n");
  2952. return 1;
  2953. }
  2954. }
  2955. if (sfan_handle) {
  2956. /* 570, 770x-JL */
  2957. fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
  2958. fan_control_commands |=
  2959. TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
  2960. } else {
  2961. if (!gfan_handle) {
  2962. /* gfan without sfan means no fan control */
  2963. /* all other models implement TP EC 0x2f control */
  2964. if (fans_handle) {
  2965. /* X31, X40, X41 */
  2966. fan_control_access_mode =
  2967. TPACPI_FAN_WR_ACPI_FANS;
  2968. fan_control_commands |=
  2969. TPACPI_FAN_CMD_SPEED |
  2970. TPACPI_FAN_CMD_LEVEL |
  2971. TPACPI_FAN_CMD_ENABLE;
  2972. } else {
  2973. fan_control_access_mode = TPACPI_FAN_WR_TPEC;
  2974. fan_control_commands |=
  2975. TPACPI_FAN_CMD_LEVEL |
  2976. TPACPI_FAN_CMD_ENABLE;
  2977. }
  2978. }
  2979. }
  2980. vdbg_printk(TPACPI_DBG_INIT, "fan is %s, modes %d, %d\n",
  2981. str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
  2982. fan_control_access_mode != TPACPI_FAN_WR_NONE),
  2983. fan_status_access_mode, fan_control_access_mode);
  2984. /* fan control master switch */
  2985. if (!fan_control_allowed) {
  2986. fan_control_access_mode = TPACPI_FAN_WR_NONE;
  2987. fan_control_commands = 0;
  2988. dbg_printk(TPACPI_DBG_INIT,
  2989. "fan control features disabled by parameter\n");
  2990. }
  2991. /* update fan_control_desired_level */
  2992. if (fan_status_access_mode != TPACPI_FAN_NONE)
  2993. fan_get_status_safe(NULL);
  2994. if (fan_status_access_mode != TPACPI_FAN_NONE ||
  2995. fan_control_access_mode != TPACPI_FAN_WR_NONE) {
  2996. rc = sysfs_create_group(&tpacpi_pdev->dev.kobj,
  2997. &fan_attr_group);
  2998. if (!(rc < 0))
  2999. rc = driver_create_file(&tpacpi_pdriver.driver,
  3000. &driver_attr_fan_watchdog);
  3001. if (rc < 0)
  3002. return rc;
  3003. return 0;
  3004. } else
  3005. return 1;
  3006. }
  3007. /*
  3008. * Call with fan_mutex held
  3009. */
  3010. static void fan_update_desired_level(u8 status)
  3011. {
  3012. if ((status &
  3013. (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
  3014. if (status > 7)
  3015. fan_control_desired_level = 7;
  3016. else
  3017. fan_control_desired_level = status;
  3018. }
  3019. }
  3020. static int fan_get_status(u8 *status)
  3021. {
  3022. u8 s;
  3023. /* TODO:
  3024. * Add TPACPI_FAN_RD_ACPI_FANS ? */
  3025. switch (fan_status_access_mode) {
  3026. case TPACPI_FAN_RD_ACPI_GFAN:
  3027. /* 570, 600e/x, 770e, 770x */
  3028. if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
  3029. return -EIO;
  3030. if (likely(status))
  3031. *status = s & 0x07;
  3032. break;
  3033. case TPACPI_FAN_RD_TPEC:
  3034. /* all except 570, 600e/x, 770e, 770x */
  3035. if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
  3036. return -EIO;
  3037. if (likely(status))
  3038. *status = s;
  3039. break;
  3040. default:
  3041. return -ENXIO;
  3042. }
  3043. return 0;
  3044. }
  3045. static int fan_get_status_safe(u8 *status)
  3046. {
  3047. int rc;
  3048. u8 s;
  3049. rc = mutex_lock_interruptible(&fan_mutex);
  3050. if (rc < 0)
  3051. return rc;
  3052. rc = fan_get_status(&s);
  3053. if (!rc)
  3054. fan_update_desired_level(s);
  3055. mutex_unlock(&fan_mutex);
  3056. if (status)
  3057. *status = s;
  3058. return rc;
  3059. }
  3060. static void fan_exit(void)
  3061. {
  3062. vdbg_printk(TPACPI_DBG_EXIT, "cancelling any pending fan watchdog tasks\n");
  3063. /* FIXME: can we really do this unconditionally? */
  3064. sysfs_remove_group(&tpacpi_pdev->dev.kobj, &fan_attr_group);
  3065. driver_remove_file(&tpacpi_pdriver.driver, &driver_attr_fan_watchdog);
  3066. cancel_delayed_work(&fan_watchdog_task);
  3067. flush_scheduled_work();
  3068. }
  3069. static int fan_get_speed(unsigned int *speed)
  3070. {
  3071. u8 hi, lo;
  3072. switch (fan_status_access_mode) {
  3073. case TPACPI_FAN_RD_TPEC:
  3074. /* all except 570, 600e/x, 770e, 770x */
  3075. if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
  3076. !acpi_ec_read(fan_rpm_offset + 1, &hi)))
  3077. return -EIO;
  3078. if (likely(speed))
  3079. *speed = (hi << 8) | lo;
  3080. break;
  3081. default:
  3082. return -ENXIO;
  3083. }
  3084. return 0;
  3085. }
  3086. static void fan_watchdog_fire(struct work_struct *ignored)
  3087. {
  3088. int rc;
  3089. printk(IBM_NOTICE "fan watchdog: enabling fan\n");
  3090. rc = fan_set_enable();
  3091. if (rc < 0) {
  3092. printk(IBM_ERR "fan watchdog: error %d while enabling fan, "
  3093. "will try again later...\n", -rc);
  3094. /* reschedule for later */
  3095. fan_watchdog_reset();
  3096. }
  3097. }
  3098. static void fan_watchdog_reset(void)
  3099. {
  3100. static int fan_watchdog_active;
  3101. if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
  3102. return;
  3103. if (fan_watchdog_active)
  3104. cancel_delayed_work(&fan_watchdog_task);
  3105. if (fan_watchdog_maxinterval > 0) {
  3106. fan_watchdog_active = 1;
  3107. if (!schedule_delayed_work(&fan_watchdog_task,
  3108. msecs_to_jiffies(fan_watchdog_maxinterval
  3109. * 1000))) {
  3110. printk(IBM_ERR "failed to schedule the fan watchdog, "
  3111. "watchdog will not trigger\n");
  3112. }
  3113. } else
  3114. fan_watchdog_active = 0;
  3115. }
  3116. static int fan_set_level(int level)
  3117. {
  3118. if (!fan_control_allowed)
  3119. return -EPERM;
  3120. switch (fan_control_access_mode) {
  3121. case TPACPI_FAN_WR_ACPI_SFAN:
  3122. if (level >= 0 && level <= 7) {
  3123. if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
  3124. return -EIO;
  3125. } else
  3126. return -EINVAL;
  3127. break;
  3128. case TPACPI_FAN_WR_ACPI_FANS:
  3129. case TPACPI_FAN_WR_TPEC:
  3130. if ((level != TP_EC_FAN_AUTO) &&
  3131. (level != TP_EC_FAN_FULLSPEED) &&
  3132. ((level < 0) || (level > 7)))
  3133. return -EINVAL;
  3134. /* safety net should the EC not support AUTO
  3135. * or FULLSPEED mode bits and just ignore them */
  3136. if (level & TP_EC_FAN_FULLSPEED)
  3137. level |= 7; /* safety min speed 7 */
  3138. else if (level & TP_EC_FAN_FULLSPEED)
  3139. level |= 4; /* safety min speed 4 */
  3140. if (!acpi_ec_write(fan_status_offset, level))
  3141. return -EIO;
  3142. else
  3143. tp_features.fan_ctrl_status_undef = 0;
  3144. break;
  3145. default:
  3146. return -ENXIO;
  3147. }
  3148. return 0;
  3149. }
  3150. static int fan_set_level_safe(int level)
  3151. {
  3152. int rc;
  3153. if (!fan_control_allowed)
  3154. return -EPERM;
  3155. rc = mutex_lock_interruptible(&fan_mutex);
  3156. if (rc < 0)
  3157. return rc;
  3158. if (level == TPACPI_FAN_LAST_LEVEL)
  3159. level = fan_control_desired_level;
  3160. rc = fan_set_level(level);
  3161. if (!rc)
  3162. fan_update_desired_level(level);
  3163. mutex_unlock(&fan_mutex);
  3164. return rc;
  3165. }
  3166. static int fan_set_enable(void)
  3167. {
  3168. u8 s;
  3169. int rc;
  3170. if (!fan_control_allowed)
  3171. return -EPERM;
  3172. rc = mutex_lock_interruptible(&fan_mutex);
  3173. if (rc < 0)
  3174. return rc;
  3175. switch (fan_control_access_mode) {
  3176. case TPACPI_FAN_WR_ACPI_FANS:
  3177. case TPACPI_FAN_WR_TPEC:
  3178. rc = fan_get_status(&s);
  3179. if (rc < 0)
  3180. break;
  3181. /* Don't go out of emergency fan mode */
  3182. if (s != 7) {
  3183. s &= 0x07;
  3184. s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
  3185. }
  3186. if (!acpi_ec_write(fan_status_offset, s))
  3187. rc = -EIO;
  3188. else {
  3189. tp_features.fan_ctrl_status_undef = 0;
  3190. rc = 0;
  3191. }
  3192. break;
  3193. case TPACPI_FAN_WR_ACPI_SFAN:
  3194. rc = fan_get_status(&s);
  3195. if (rc < 0)
  3196. break;
  3197. s &= 0x07;
  3198. /* Set fan to at least level 4 */
  3199. s |= 4;
  3200. if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
  3201. rc= -EIO;
  3202. else
  3203. rc = 0;
  3204. break;
  3205. default:
  3206. rc = -ENXIO;
  3207. }
  3208. mutex_unlock(&fan_mutex);
  3209. return rc;
  3210. }
  3211. static int fan_set_disable(void)
  3212. {
  3213. int rc;
  3214. if (!fan_control_allowed)
  3215. return -EPERM;
  3216. rc = mutex_lock_interruptible(&fan_mutex);
  3217. if (rc < 0)
  3218. return rc;
  3219. rc = 0;
  3220. switch (fan_control_access_mode) {
  3221. case TPACPI_FAN_WR_ACPI_FANS:
  3222. case TPACPI_FAN_WR_TPEC:
  3223. if (!acpi_ec_write(fan_status_offset, 0x00))
  3224. rc = -EIO;
  3225. else {
  3226. fan_control_desired_level = 0;
  3227. tp_features.fan_ctrl_status_undef = 0;
  3228. }
  3229. break;
  3230. case TPACPI_FAN_WR_ACPI_SFAN:
  3231. if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
  3232. rc = -EIO;
  3233. else
  3234. fan_control_desired_level = 0;
  3235. break;
  3236. default:
  3237. rc = -ENXIO;
  3238. }
  3239. mutex_unlock(&fan_mutex);
  3240. return rc;
  3241. }
  3242. static int fan_set_speed(int speed)
  3243. {
  3244. int rc;
  3245. if (!fan_control_allowed)
  3246. return -EPERM;
  3247. rc = mutex_lock_interruptible(&fan_mutex);
  3248. if (rc < 0)
  3249. return rc;
  3250. rc = 0;
  3251. switch (fan_control_access_mode) {
  3252. case TPACPI_FAN_WR_ACPI_FANS:
  3253. if (speed >= 0 && speed <= 65535) {
  3254. if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
  3255. speed, speed, speed))
  3256. rc = -EIO;
  3257. } else
  3258. rc = -EINVAL;
  3259. break;
  3260. default:
  3261. rc = -ENXIO;
  3262. }
  3263. mutex_unlock(&fan_mutex);
  3264. return rc;
  3265. }
  3266. static int fan_read(char *p)
  3267. {
  3268. int len = 0;
  3269. int rc;
  3270. u8 status;
  3271. unsigned int speed = 0;
  3272. switch (fan_status_access_mode) {
  3273. case TPACPI_FAN_RD_ACPI_GFAN:
  3274. /* 570, 600e/x, 770e, 770x */
  3275. if ((rc = fan_get_status_safe(&status)) < 0)
  3276. return rc;
  3277. len += sprintf(p + len, "status:\t\t%s\n"
  3278. "level:\t\t%d\n",
  3279. (status != 0) ? "enabled" : "disabled", status);
  3280. break;
  3281. case TPACPI_FAN_RD_TPEC:
  3282. /* all except 570, 600e/x, 770e, 770x */
  3283. if ((rc = fan_get_status_safe(&status)) < 0)
  3284. return rc;
  3285. if (unlikely(tp_features.fan_ctrl_status_undef)) {
  3286. if (status != fan_control_initial_status)
  3287. tp_features.fan_ctrl_status_undef = 0;
  3288. else
  3289. /* Return most likely status. In fact, it
  3290. * might be the only possible status */
  3291. status = TP_EC_FAN_AUTO;
  3292. }
  3293. len += sprintf(p + len, "status:\t\t%s\n",
  3294. (status != 0) ? "enabled" : "disabled");
  3295. if ((rc = fan_get_speed(&speed)) < 0)
  3296. return rc;
  3297. len += sprintf(p + len, "speed:\t\t%d\n", speed);
  3298. if (status & TP_EC_FAN_FULLSPEED)
  3299. /* Disengaged mode takes precedence */
  3300. len += sprintf(p + len, "level:\t\tdisengaged\n");
  3301. else if (status & TP_EC_FAN_AUTO)
  3302. len += sprintf(p + len, "level:\t\tauto\n");
  3303. else
  3304. len += sprintf(p + len, "level:\t\t%d\n", status);
  3305. break;
  3306. case TPACPI_FAN_NONE:
  3307. default:
  3308. len += sprintf(p + len, "status:\t\tnot supported\n");
  3309. }
  3310. if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
  3311. len += sprintf(p + len, "commands:\tlevel <level>");
  3312. switch (fan_control_access_mode) {
  3313. case TPACPI_FAN_WR_ACPI_SFAN:
  3314. len += sprintf(p + len, " (<level> is 0-7)\n");
  3315. break;
  3316. default:
  3317. len += sprintf(p + len, " (<level> is 0-7, "
  3318. "auto, disengaged, full-speed)\n");
  3319. break;
  3320. }
  3321. }
  3322. if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
  3323. len += sprintf(p + len, "commands:\tenable, disable\n"
  3324. "commands:\twatchdog <timeout> (<timeout> is 0 (off), "
  3325. "1-120 (seconds))\n");
  3326. if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
  3327. len += sprintf(p + len, "commands:\tspeed <speed>"
  3328. " (<speed> is 0-65535)\n");
  3329. return len;
  3330. }
  3331. static int fan_write_cmd_level(const char *cmd, int *rc)
  3332. {
  3333. int level;
  3334. if (strlencmp(cmd, "level auto") == 0)
  3335. level = TP_EC_FAN_AUTO;
  3336. else if ((strlencmp(cmd, "level disengaged") == 0) |
  3337. (strlencmp(cmd, "level full-speed") == 0))
  3338. level = TP_EC_FAN_FULLSPEED;
  3339. else if (sscanf(cmd, "level %d", &level) != 1)
  3340. return 0;
  3341. if ((*rc = fan_set_level_safe(level)) == -ENXIO)
  3342. printk(IBM_ERR "level command accepted for unsupported "
  3343. "access mode %d", fan_control_access_mode);
  3344. return 1;
  3345. }
  3346. static int fan_write_cmd_enable(const char *cmd, int *rc)
  3347. {
  3348. if (strlencmp(cmd, "enable") != 0)
  3349. return 0;
  3350. if ((*rc = fan_set_enable()) == -ENXIO)
  3351. printk(IBM_ERR "enable command accepted for unsupported "
  3352. "access mode %d", fan_control_access_mode);
  3353. return 1;
  3354. }
  3355. static int fan_write_cmd_disable(const char *cmd, int *rc)
  3356. {
  3357. if (strlencmp(cmd, "disable") != 0)
  3358. return 0;
  3359. if ((*rc = fan_set_disable()) == -ENXIO)
  3360. printk(IBM_ERR "disable command accepted for unsupported "
  3361. "access mode %d", fan_control_access_mode);
  3362. return 1;
  3363. }
  3364. static int fan_write_cmd_speed(const char *cmd, int *rc)
  3365. {
  3366. int speed;
  3367. /* TODO:
  3368. * Support speed <low> <medium> <high> ? */
  3369. if (sscanf(cmd, "speed %d", &speed) != 1)
  3370. return 0;
  3371. if ((*rc = fan_set_speed(speed)) == -ENXIO)
  3372. printk(IBM_ERR "speed command accepted for unsupported "
  3373. "access mode %d", fan_control_access_mode);
  3374. return 1;
  3375. }
  3376. static int fan_write_cmd_watchdog(const char *cmd, int *rc)
  3377. {
  3378. int interval;
  3379. if (sscanf(cmd, "watchdog %d", &interval) != 1)
  3380. return 0;
  3381. if (interval < 0 || interval > 120)
  3382. *rc = -EINVAL;
  3383. else
  3384. fan_watchdog_maxinterval = interval;
  3385. return 1;
  3386. }
  3387. static int fan_write(char *buf)
  3388. {
  3389. char *cmd;
  3390. int rc = 0;
  3391. while (!rc && (cmd = next_cmd(&buf))) {
  3392. if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
  3393. fan_write_cmd_level(cmd, &rc)) &&
  3394. !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
  3395. (fan_write_cmd_enable(cmd, &rc) ||
  3396. fan_write_cmd_disable(cmd, &rc) ||
  3397. fan_write_cmd_watchdog(cmd, &rc))) &&
  3398. !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
  3399. fan_write_cmd_speed(cmd, &rc))
  3400. )
  3401. rc = -EINVAL;
  3402. else if (!rc)
  3403. fan_watchdog_reset();
  3404. }
  3405. return rc;
  3406. }
  3407. static struct ibm_struct fan_driver_data = {
  3408. .name = "fan",
  3409. .read = fan_read,
  3410. .write = fan_write,
  3411. .exit = fan_exit,
  3412. };
  3413. /****************************************************************************
  3414. ****************************************************************************
  3415. *
  3416. * Infrastructure
  3417. *
  3418. ****************************************************************************
  3419. ****************************************************************************/
  3420. /* /proc support */
  3421. static struct proc_dir_entry *proc_dir;
  3422. /* Subdriver registry */
  3423. static LIST_HEAD(tpacpi_all_drivers);
  3424. /*
  3425. * Module and infrastructure proble, init and exit handling
  3426. */
  3427. #ifdef CONFIG_THINKPAD_ACPI_DEBUG
  3428. static const char * __init str_supported(int is_supported)
  3429. {
  3430. static char text_unsupported[] __initdata = "not supported";
  3431. return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
  3432. }
  3433. #endif /* CONFIG_THINKPAD_ACPI_DEBUG */
  3434. static int __init ibm_init(struct ibm_init_struct *iibm)
  3435. {
  3436. int ret;
  3437. struct ibm_struct *ibm = iibm->data;
  3438. struct proc_dir_entry *entry;
  3439. BUG_ON(ibm == NULL);
  3440. INIT_LIST_HEAD(&ibm->all_drivers);
  3441. if (ibm->flags.experimental && !experimental)
  3442. return 0;
  3443. dbg_printk(TPACPI_DBG_INIT,
  3444. "probing for %s\n", ibm->name);
  3445. if (iibm->init) {
  3446. ret = iibm->init(iibm);
  3447. if (ret > 0)
  3448. return 0; /* probe failed */
  3449. if (ret)
  3450. return ret;
  3451. ibm->flags.init_called = 1;
  3452. }
  3453. if (ibm->acpi) {
  3454. if (ibm->acpi->hid) {
  3455. ret = register_tpacpi_subdriver(ibm);
  3456. if (ret)
  3457. goto err_out;
  3458. }
  3459. if (ibm->acpi->notify) {
  3460. ret = setup_acpi_notify(ibm);
  3461. if (ret == -ENODEV) {
  3462. printk(IBM_NOTICE "disabling subdriver %s\n",
  3463. ibm->name);
  3464. ret = 0;
  3465. goto err_out;
  3466. }
  3467. if (ret < 0)
  3468. goto err_out;
  3469. }
  3470. }
  3471. dbg_printk(TPACPI_DBG_INIT,
  3472. "%s installed\n", ibm->name);
  3473. if (ibm->read) {
  3474. entry = create_proc_entry(ibm->name,
  3475. S_IFREG | S_IRUGO | S_IWUSR,
  3476. proc_dir);
  3477. if (!entry) {
  3478. printk(IBM_ERR "unable to create proc entry %s\n",
  3479. ibm->name);
  3480. ret = -ENODEV;
  3481. goto err_out;
  3482. }
  3483. entry->owner = THIS_MODULE;
  3484. entry->data = ibm;
  3485. entry->read_proc = &dispatch_procfs_read;
  3486. if (ibm->write)
  3487. entry->write_proc = &dispatch_procfs_write;
  3488. ibm->flags.proc_created = 1;
  3489. }
  3490. list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
  3491. return 0;
  3492. err_out:
  3493. dbg_printk(TPACPI_DBG_INIT,
  3494. "%s: at error exit path with result %d\n",
  3495. ibm->name, ret);
  3496. ibm_exit(ibm);
  3497. return (ret < 0)? ret : 0;
  3498. }
  3499. static void ibm_exit(struct ibm_struct *ibm)
  3500. {
  3501. dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
  3502. list_del_init(&ibm->all_drivers);
  3503. if (ibm->flags.acpi_notify_installed) {
  3504. dbg_printk(TPACPI_DBG_EXIT,
  3505. "%s: acpi_remove_notify_handler\n", ibm->name);
  3506. BUG_ON(!ibm->acpi);
  3507. acpi_remove_notify_handler(*ibm->acpi->handle,
  3508. ibm->acpi->type,
  3509. dispatch_acpi_notify);
  3510. ibm->flags.acpi_notify_installed = 0;
  3511. ibm->flags.acpi_notify_installed = 0;
  3512. }
  3513. if (ibm->flags.proc_created) {
  3514. dbg_printk(TPACPI_DBG_EXIT,
  3515. "%s: remove_proc_entry\n", ibm->name);
  3516. remove_proc_entry(ibm->name, proc_dir);
  3517. ibm->flags.proc_created = 0;
  3518. }
  3519. if (ibm->flags.acpi_driver_registered) {
  3520. dbg_printk(TPACPI_DBG_EXIT,
  3521. "%s: acpi_bus_unregister_driver\n", ibm->name);
  3522. BUG_ON(!ibm->acpi);
  3523. acpi_bus_unregister_driver(ibm->acpi->driver);
  3524. kfree(ibm->acpi->driver);
  3525. ibm->acpi->driver = NULL;
  3526. ibm->flags.acpi_driver_registered = 0;
  3527. }
  3528. if (ibm->flags.init_called && ibm->exit) {
  3529. ibm->exit();
  3530. ibm->flags.init_called = 0;
  3531. }
  3532. dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
  3533. }
  3534. /* Probing */
  3535. static char *ibm_thinkpad_ec_found;
  3536. static char* __init check_dmi_for_ec(void)
  3537. {
  3538. struct dmi_device *dev = NULL;
  3539. char ec_fw_string[18];
  3540. /*
  3541. * ThinkPad T23 or newer, A31 or newer, R50e or newer,
  3542. * X32 or newer, all Z series; Some models must have an
  3543. * up-to-date BIOS or they will not be detected.
  3544. *
  3545. * See http://thinkwiki.org/wiki/List_of_DMI_IDs
  3546. */
  3547. while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
  3548. if (sscanf(dev->name,
  3549. "IBM ThinkPad Embedded Controller -[%17c",
  3550. ec_fw_string) == 1) {
  3551. ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
  3552. ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
  3553. return kstrdup(ec_fw_string, GFP_KERNEL);
  3554. }
  3555. }
  3556. return NULL;
  3557. }
  3558. static int __init probe_for_thinkpad(void)
  3559. {
  3560. int is_thinkpad;
  3561. if (acpi_disabled)
  3562. return -ENODEV;
  3563. /*
  3564. * Non-ancient models have better DMI tagging, but very old models
  3565. * don't.
  3566. */
  3567. is_thinkpad = dmi_name_in_vendors("ThinkPad");
  3568. /* ec is required because many other handles are relative to it */
  3569. IBM_ACPIHANDLE_INIT(ec);
  3570. if (!ec_handle) {
  3571. if (is_thinkpad)
  3572. printk(IBM_ERR
  3573. "Not yet supported ThinkPad detected!\n");
  3574. return -ENODEV;
  3575. }
  3576. /*
  3577. * Risks a regression on very old machines, but reduces potential
  3578. * false positives a damn great deal
  3579. */
  3580. if (!is_thinkpad)
  3581. is_thinkpad = dmi_name_in_vendors("IBM");
  3582. if (!is_thinkpad && !force_load)
  3583. return -ENODEV;
  3584. return 0;
  3585. }
  3586. /* Module init, exit, parameters */
  3587. static struct ibm_init_struct ibms_init[] __initdata = {
  3588. {
  3589. .init = thinkpad_acpi_driver_init,
  3590. .data = &thinkpad_acpi_driver_data,
  3591. },
  3592. {
  3593. .init = hotkey_init,
  3594. .data = &hotkey_driver_data,
  3595. },
  3596. {
  3597. .init = bluetooth_init,
  3598. .data = &bluetooth_driver_data,
  3599. },
  3600. {
  3601. .init = wan_init,
  3602. .data = &wan_driver_data,
  3603. },
  3604. {
  3605. .init = video_init,
  3606. .data = &video_driver_data,
  3607. },
  3608. {
  3609. .init = light_init,
  3610. .data = &light_driver_data,
  3611. },
  3612. #ifdef CONFIG_THINKPAD_ACPI_DOCK
  3613. {
  3614. .init = dock_init,
  3615. .data = &dock_driver_data[0],
  3616. },
  3617. {
  3618. .init = dock_init2,
  3619. .data = &dock_driver_data[1],
  3620. },
  3621. #endif
  3622. #ifdef CONFIG_THINKPAD_ACPI_BAY
  3623. {
  3624. .init = bay_init,
  3625. .data = &bay_driver_data,
  3626. },
  3627. #endif
  3628. {
  3629. .init = cmos_init,
  3630. .data = &cmos_driver_data,
  3631. },
  3632. {
  3633. .init = led_init,
  3634. .data = &led_driver_data,
  3635. },
  3636. {
  3637. .init = beep_init,
  3638. .data = &beep_driver_data,
  3639. },
  3640. {
  3641. .init = thermal_init,
  3642. .data = &thermal_driver_data,
  3643. },
  3644. {
  3645. .data = &ecdump_driver_data,
  3646. },
  3647. {
  3648. .init = brightness_init,
  3649. .data = &brightness_driver_data,
  3650. },
  3651. {
  3652. .data = &volume_driver_data,
  3653. },
  3654. {
  3655. .init = fan_init,
  3656. .data = &fan_driver_data,
  3657. },
  3658. };
  3659. static int __init set_ibm_param(const char *val, struct kernel_param *kp)
  3660. {
  3661. unsigned int i;
  3662. struct ibm_struct *ibm;
  3663. for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
  3664. ibm = ibms_init[i].data;
  3665. BUG_ON(ibm == NULL);
  3666. if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
  3667. if (strlen(val) > sizeof(ibms_init[i].param) - 2)
  3668. return -ENOSPC;
  3669. strcpy(ibms_init[i].param, val);
  3670. strcat(ibms_init[i].param, ",");
  3671. return 0;
  3672. }
  3673. }
  3674. return -EINVAL;
  3675. }
  3676. static int experimental;
  3677. module_param(experimental, int, 0);
  3678. static u32 dbg_level;
  3679. module_param_named(debug, dbg_level, uint, 0);
  3680. static int force_load;
  3681. module_param(force_load, bool, 0);
  3682. static int fan_control_allowed;
  3683. module_param_named(fan_control, fan_control_allowed, bool, 0);
  3684. #define IBM_PARAM(feature) \
  3685. module_param_call(feature, set_ibm_param, NULL, NULL, 0)
  3686. IBM_PARAM(hotkey);
  3687. IBM_PARAM(bluetooth);
  3688. IBM_PARAM(video);
  3689. IBM_PARAM(light);
  3690. #ifdef CONFIG_THINKPAD_ACPI_DOCK
  3691. IBM_PARAM(dock);
  3692. #endif
  3693. #ifdef CONFIG_THINKPAD_ACPI_BAY
  3694. IBM_PARAM(bay);
  3695. #endif /* CONFIG_THINKPAD_ACPI_BAY */
  3696. IBM_PARAM(cmos);
  3697. IBM_PARAM(led);
  3698. IBM_PARAM(beep);
  3699. IBM_PARAM(ecdump);
  3700. IBM_PARAM(brightness);
  3701. IBM_PARAM(volume);
  3702. IBM_PARAM(fan);
  3703. static int __init thinkpad_acpi_module_init(void)
  3704. {
  3705. int ret, i;
  3706. /* Driver-level probe */
  3707. ret = probe_for_thinkpad();
  3708. if (ret)
  3709. return ret;
  3710. /* Driver initialization */
  3711. ibm_thinkpad_ec_found = check_dmi_for_ec();
  3712. IBM_ACPIHANDLE_INIT(ecrd);
  3713. IBM_ACPIHANDLE_INIT(ecwr);
  3714. proc_dir = proc_mkdir(IBM_PROC_DIR, acpi_root_dir);
  3715. if (!proc_dir) {
  3716. printk(IBM_ERR "unable to create proc dir " IBM_PROC_DIR);
  3717. thinkpad_acpi_module_exit();
  3718. return -ENODEV;
  3719. }
  3720. proc_dir->owner = THIS_MODULE;
  3721. ret = platform_driver_register(&tpacpi_pdriver);
  3722. if (ret) {
  3723. printk(IBM_ERR "unable to register platform driver\n");
  3724. thinkpad_acpi_module_exit();
  3725. return ret;
  3726. }
  3727. ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
  3728. if (ret) {
  3729. printk(IBM_ERR "unable to create sysfs driver attributes\n");
  3730. thinkpad_acpi_module_exit();
  3731. return ret;
  3732. }
  3733. /* Device initialization */
  3734. tpacpi_pdev = platform_device_register_simple(IBM_DRVR_NAME, -1,
  3735. NULL, 0);
  3736. if (IS_ERR(tpacpi_pdev)) {
  3737. ret = PTR_ERR(tpacpi_pdev);
  3738. tpacpi_pdev = NULL;
  3739. printk(IBM_ERR "unable to register platform device\n");
  3740. thinkpad_acpi_module_exit();
  3741. return ret;
  3742. }
  3743. tpacpi_hwmon = hwmon_device_register(&tpacpi_pdev->dev);
  3744. if (IS_ERR(tpacpi_hwmon)) {
  3745. ret = PTR_ERR(tpacpi_hwmon);
  3746. tpacpi_hwmon = NULL;
  3747. printk(IBM_ERR "unable to register hwmon device\n");
  3748. thinkpad_acpi_module_exit();
  3749. return ret;
  3750. }
  3751. tpacpi_inputdev = input_allocate_device();
  3752. if (!tpacpi_inputdev) {
  3753. printk(IBM_ERR "unable to allocate input device\n");
  3754. thinkpad_acpi_module_exit();
  3755. return -ENOMEM;
  3756. } else {
  3757. /* Prepare input device, but don't register */
  3758. tpacpi_inputdev->name = "ThinkPad Extra Buttons";
  3759. tpacpi_inputdev->phys = IBM_DRVR_NAME "/input0";
  3760. tpacpi_inputdev->id.bustype = BUS_HOST;
  3761. tpacpi_inputdev->id.vendor = TPACPI_HKEY_INPUT_VENDOR;
  3762. tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
  3763. tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
  3764. }
  3765. for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
  3766. ret = ibm_init(&ibms_init[i]);
  3767. if (ret >= 0 && *ibms_init[i].param)
  3768. ret = ibms_init[i].data->write(ibms_init[i].param);
  3769. if (ret < 0) {
  3770. thinkpad_acpi_module_exit();
  3771. return ret;
  3772. }
  3773. }
  3774. ret = input_register_device(tpacpi_inputdev);
  3775. if (ret < 0) {
  3776. printk(IBM_ERR "unable to register input device\n");
  3777. thinkpad_acpi_module_exit();
  3778. return ret;
  3779. } else {
  3780. tp_features.input_device_registered = 1;
  3781. }
  3782. return 0;
  3783. }
  3784. static void thinkpad_acpi_module_exit(void)
  3785. {
  3786. struct ibm_struct *ibm, *itmp;
  3787. list_for_each_entry_safe_reverse(ibm, itmp,
  3788. &tpacpi_all_drivers,
  3789. all_drivers) {
  3790. ibm_exit(ibm);
  3791. }
  3792. dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
  3793. if (tpacpi_inputdev) {
  3794. if (tp_features.input_device_registered)
  3795. input_unregister_device(tpacpi_inputdev);
  3796. else
  3797. input_free_device(tpacpi_inputdev);
  3798. }
  3799. if (tpacpi_hwmon)
  3800. hwmon_device_unregister(tpacpi_hwmon);
  3801. if (tpacpi_pdev)
  3802. platform_device_unregister(tpacpi_pdev);
  3803. tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
  3804. platform_driver_unregister(&tpacpi_pdriver);
  3805. if (proc_dir)
  3806. remove_proc_entry(IBM_PROC_DIR, acpi_root_dir);
  3807. kfree(ibm_thinkpad_ec_found);
  3808. }
  3809. module_init(thinkpad_acpi_module_init);
  3810. module_exit(thinkpad_acpi_module_exit);