thinkpad_acpi.c 99 KB

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