thinkpad_acpi.c 123 KB

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