thinkpad_acpi.c 120 KB

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