thinkpad_acpi.c 99 KB

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