thinkpad_acpi.c 112 KB

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