sony-laptop.c 120 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941
  1. /*
  2. * ACPI Sony Notebook Control Driver (SNC and SPIC)
  3. *
  4. * Copyright (C) 2004-2005 Stelian Pop <stelian@popies.net>
  5. * Copyright (C) 2007-2009 Mattia Dongili <malattia@linux.it>
  6. *
  7. * Parts of this driver inspired from asus_acpi.c and ibm_acpi.c
  8. * which are copyrighted by their respective authors.
  9. *
  10. * The SNY6001 driver part is based on the sonypi driver which includes
  11. * material from:
  12. *
  13. * Copyright (C) 2001-2005 Stelian Pop <stelian@popies.net>
  14. *
  15. * Copyright (C) 2005 Narayanan R S <nars@kadamba.org>
  16. *
  17. * Copyright (C) 2001-2002 Alcôve <www.alcove.com>
  18. *
  19. * Copyright (C) 2001 Michael Ashley <m.ashley@unsw.edu.au>
  20. *
  21. * Copyright (C) 2001 Junichi Morita <jun1m@mars.dti.ne.jp>
  22. *
  23. * Copyright (C) 2000 Takaya Kinjo <t-kinjo@tc4.so-net.ne.jp>
  24. *
  25. * Copyright (C) 2000 Andrew Tridgell <tridge@valinux.com>
  26. *
  27. * Earlier work by Werner Almesberger, Paul `Rusty' Russell and Paul Mackerras.
  28. *
  29. * This program is free software; you can redistribute it and/or modify
  30. * it under the terms of the GNU General Public License as published by
  31. * the Free Software Foundation; either version 2 of the License, or
  32. * (at your option) any later version.
  33. *
  34. * This program is distributed in the hope that it will be useful,
  35. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  36. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  37. * GNU General Public License for more details.
  38. *
  39. * You should have received a copy of the GNU General Public License
  40. * along with this program; if not, write to the Free Software
  41. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  42. *
  43. */
  44. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  45. #include <linux/kernel.h>
  46. #include <linux/module.h>
  47. #include <linux/moduleparam.h>
  48. #include <linux/init.h>
  49. #include <linux/types.h>
  50. #include <linux/backlight.h>
  51. #include <linux/platform_device.h>
  52. #include <linux/err.h>
  53. #include <linux/dmi.h>
  54. #include <linux/pci.h>
  55. #include <linux/interrupt.h>
  56. #include <linux/delay.h>
  57. #include <linux/input.h>
  58. #include <linux/kfifo.h>
  59. #include <linux/workqueue.h>
  60. #include <linux/acpi.h>
  61. #include <linux/slab.h>
  62. #include <linux/sonypi.h>
  63. #include <linux/sony-laptop.h>
  64. #include <linux/rfkill.h>
  65. #ifdef CONFIG_SONYPI_COMPAT
  66. #include <linux/poll.h>
  67. #include <linux/miscdevice.h>
  68. #endif
  69. #include <linux/uaccess.h>
  70. #include <acpi/video.h>
  71. #define dprintk(fmt, ...) \
  72. do { \
  73. if (debug) \
  74. pr_warn(fmt, ##__VA_ARGS__); \
  75. } while (0)
  76. #define SONY_NC_CLASS "sony-nc"
  77. #define SONY_NC_HID "SNY5001"
  78. #define SONY_NC_DRIVER_NAME "Sony Notebook Control Driver"
  79. #define SONY_PIC_CLASS "sony-pic"
  80. #define SONY_PIC_HID "SNY6001"
  81. #define SONY_PIC_DRIVER_NAME "Sony Programmable IO Control Driver"
  82. MODULE_AUTHOR("Stelian Pop, Mattia Dongili");
  83. MODULE_DESCRIPTION("Sony laptop extras driver (SPIC and SNC ACPI device)");
  84. MODULE_LICENSE("GPL");
  85. static int debug;
  86. module_param(debug, int, 0);
  87. MODULE_PARM_DESC(debug, "set this to 1 (and RTFM) if you want to help "
  88. "the development of this driver");
  89. static int no_spic; /* = 0 */
  90. module_param(no_spic, int, 0444);
  91. MODULE_PARM_DESC(no_spic,
  92. "set this if you don't want to enable the SPIC device");
  93. static int compat; /* = 0 */
  94. module_param(compat, int, 0444);
  95. MODULE_PARM_DESC(compat,
  96. "set this if you want to enable backward compatibility mode");
  97. static unsigned long mask = 0xffffffff;
  98. module_param(mask, ulong, 0644);
  99. MODULE_PARM_DESC(mask,
  100. "set this to the mask of event you want to enable (see doc)");
  101. static int camera; /* = 0 */
  102. module_param(camera, int, 0444);
  103. MODULE_PARM_DESC(camera,
  104. "set this to 1 to enable Motion Eye camera controls "
  105. "(only use it if you have a C1VE or C1VN model)");
  106. #ifdef CONFIG_SONYPI_COMPAT
  107. static int minor = -1;
  108. module_param(minor, int, 0);
  109. MODULE_PARM_DESC(minor,
  110. "minor number of the misc device for the SPIC compatibility code, "
  111. "default is -1 (automatic)");
  112. #endif
  113. static int kbd_backlight = -1;
  114. module_param(kbd_backlight, int, 0444);
  115. MODULE_PARM_DESC(kbd_backlight,
  116. "set this to 0 to disable keyboard backlight, "
  117. "1 to enable it with automatic control and 2 to have it always "
  118. "on (default: no change from current value)");
  119. static int kbd_backlight_timeout = -1;
  120. module_param(kbd_backlight_timeout, int, 0444);
  121. MODULE_PARM_DESC(kbd_backlight_timeout,
  122. "meaningful values vary from 0 to 3 and their meaning depends "
  123. "on the model (default: no change from current value)");
  124. #ifdef CONFIG_PM_SLEEP
  125. static void sony_nc_thermal_resume(void);
  126. #endif
  127. static int sony_nc_kbd_backlight_setup(struct platform_device *pd,
  128. unsigned int handle);
  129. static void sony_nc_kbd_backlight_cleanup(struct platform_device *pd,
  130. unsigned int handle);
  131. static int sony_nc_battery_care_setup(struct platform_device *pd,
  132. unsigned int handle);
  133. static void sony_nc_battery_care_cleanup(struct platform_device *pd);
  134. static int sony_nc_thermal_setup(struct platform_device *pd);
  135. static void sony_nc_thermal_cleanup(struct platform_device *pd);
  136. static int sony_nc_lid_resume_setup(struct platform_device *pd,
  137. unsigned int handle);
  138. static void sony_nc_lid_resume_cleanup(struct platform_device *pd);
  139. static int sony_nc_gfx_switch_setup(struct platform_device *pd,
  140. unsigned int handle);
  141. static void sony_nc_gfx_switch_cleanup(struct platform_device *pd);
  142. static int __sony_nc_gfx_switch_status_get(void);
  143. static int sony_nc_highspeed_charging_setup(struct platform_device *pd);
  144. static void sony_nc_highspeed_charging_cleanup(struct platform_device *pd);
  145. static int sony_nc_lowbatt_setup(struct platform_device *pd);
  146. static void sony_nc_lowbatt_cleanup(struct platform_device *pd);
  147. static int sony_nc_fanspeed_setup(struct platform_device *pd);
  148. static void sony_nc_fanspeed_cleanup(struct platform_device *pd);
  149. static int sony_nc_usb_charge_setup(struct platform_device *pd);
  150. static void sony_nc_usb_charge_cleanup(struct platform_device *pd);
  151. static int sony_nc_panelid_setup(struct platform_device *pd);
  152. static void sony_nc_panelid_cleanup(struct platform_device *pd);
  153. static int sony_nc_smart_conn_setup(struct platform_device *pd);
  154. static void sony_nc_smart_conn_cleanup(struct platform_device *pd);
  155. static int sony_nc_touchpad_setup(struct platform_device *pd,
  156. unsigned int handle);
  157. static void sony_nc_touchpad_cleanup(struct platform_device *pd);
  158. enum sony_nc_rfkill {
  159. SONY_WIFI,
  160. SONY_BLUETOOTH,
  161. SONY_WWAN,
  162. SONY_WIMAX,
  163. N_SONY_RFKILL,
  164. };
  165. static int sony_rfkill_handle;
  166. static struct rfkill *sony_rfkill_devices[N_SONY_RFKILL];
  167. static int sony_rfkill_address[N_SONY_RFKILL] = {0x300, 0x500, 0x700, 0x900};
  168. static int sony_nc_rfkill_setup(struct acpi_device *device,
  169. unsigned int handle);
  170. static void sony_nc_rfkill_cleanup(void);
  171. static void sony_nc_rfkill_update(void);
  172. /*********** Input Devices ***********/
  173. #define SONY_LAPTOP_BUF_SIZE 128
  174. struct sony_laptop_input_s {
  175. atomic_t users;
  176. struct input_dev *jog_dev;
  177. struct input_dev *key_dev;
  178. struct kfifo fifo;
  179. spinlock_t fifo_lock;
  180. struct timer_list release_key_timer;
  181. };
  182. static struct sony_laptop_input_s sony_laptop_input = {
  183. .users = ATOMIC_INIT(0),
  184. };
  185. struct sony_laptop_keypress {
  186. struct input_dev *dev;
  187. int key;
  188. };
  189. /* Correspondance table between sonypi events
  190. * and input layer indexes in the keymap
  191. */
  192. static const int sony_laptop_input_index[] = {
  193. -1, /* 0 no event */
  194. -1, /* 1 SONYPI_EVENT_JOGDIAL_DOWN */
  195. -1, /* 2 SONYPI_EVENT_JOGDIAL_UP */
  196. -1, /* 3 SONYPI_EVENT_JOGDIAL_DOWN_PRESSED */
  197. -1, /* 4 SONYPI_EVENT_JOGDIAL_UP_PRESSED */
  198. -1, /* 5 SONYPI_EVENT_JOGDIAL_PRESSED */
  199. -1, /* 6 SONYPI_EVENT_JOGDIAL_RELEASED */
  200. 0, /* 7 SONYPI_EVENT_CAPTURE_PRESSED */
  201. 1, /* 8 SONYPI_EVENT_CAPTURE_RELEASED */
  202. 2, /* 9 SONYPI_EVENT_CAPTURE_PARTIALPRESSED */
  203. 3, /* 10 SONYPI_EVENT_CAPTURE_PARTIALRELEASED */
  204. 4, /* 11 SONYPI_EVENT_FNKEY_ESC */
  205. 5, /* 12 SONYPI_EVENT_FNKEY_F1 */
  206. 6, /* 13 SONYPI_EVENT_FNKEY_F2 */
  207. 7, /* 14 SONYPI_EVENT_FNKEY_F3 */
  208. 8, /* 15 SONYPI_EVENT_FNKEY_F4 */
  209. 9, /* 16 SONYPI_EVENT_FNKEY_F5 */
  210. 10, /* 17 SONYPI_EVENT_FNKEY_F6 */
  211. 11, /* 18 SONYPI_EVENT_FNKEY_F7 */
  212. 12, /* 19 SONYPI_EVENT_FNKEY_F8 */
  213. 13, /* 20 SONYPI_EVENT_FNKEY_F9 */
  214. 14, /* 21 SONYPI_EVENT_FNKEY_F10 */
  215. 15, /* 22 SONYPI_EVENT_FNKEY_F11 */
  216. 16, /* 23 SONYPI_EVENT_FNKEY_F12 */
  217. 17, /* 24 SONYPI_EVENT_FNKEY_1 */
  218. 18, /* 25 SONYPI_EVENT_FNKEY_2 */
  219. 19, /* 26 SONYPI_EVENT_FNKEY_D */
  220. 20, /* 27 SONYPI_EVENT_FNKEY_E */
  221. 21, /* 28 SONYPI_EVENT_FNKEY_F */
  222. 22, /* 29 SONYPI_EVENT_FNKEY_S */
  223. 23, /* 30 SONYPI_EVENT_FNKEY_B */
  224. 24, /* 31 SONYPI_EVENT_BLUETOOTH_PRESSED */
  225. 25, /* 32 SONYPI_EVENT_PKEY_P1 */
  226. 26, /* 33 SONYPI_EVENT_PKEY_P2 */
  227. 27, /* 34 SONYPI_EVENT_PKEY_P3 */
  228. 28, /* 35 SONYPI_EVENT_BACK_PRESSED */
  229. -1, /* 36 SONYPI_EVENT_LID_CLOSED */
  230. -1, /* 37 SONYPI_EVENT_LID_OPENED */
  231. 29, /* 38 SONYPI_EVENT_BLUETOOTH_ON */
  232. 30, /* 39 SONYPI_EVENT_BLUETOOTH_OFF */
  233. 31, /* 40 SONYPI_EVENT_HELP_PRESSED */
  234. 32, /* 41 SONYPI_EVENT_FNKEY_ONLY */
  235. 33, /* 42 SONYPI_EVENT_JOGDIAL_FAST_DOWN */
  236. 34, /* 43 SONYPI_EVENT_JOGDIAL_FAST_UP */
  237. 35, /* 44 SONYPI_EVENT_JOGDIAL_FAST_DOWN_PRESSED */
  238. 36, /* 45 SONYPI_EVENT_JOGDIAL_FAST_UP_PRESSED */
  239. 37, /* 46 SONYPI_EVENT_JOGDIAL_VFAST_DOWN */
  240. 38, /* 47 SONYPI_EVENT_JOGDIAL_VFAST_UP */
  241. 39, /* 48 SONYPI_EVENT_JOGDIAL_VFAST_DOWN_PRESSED */
  242. 40, /* 49 SONYPI_EVENT_JOGDIAL_VFAST_UP_PRESSED */
  243. 41, /* 50 SONYPI_EVENT_ZOOM_PRESSED */
  244. 42, /* 51 SONYPI_EVENT_THUMBPHRASE_PRESSED */
  245. 43, /* 52 SONYPI_EVENT_MEYE_FACE */
  246. 44, /* 53 SONYPI_EVENT_MEYE_OPPOSITE */
  247. 45, /* 54 SONYPI_EVENT_MEMORYSTICK_INSERT */
  248. 46, /* 55 SONYPI_EVENT_MEMORYSTICK_EJECT */
  249. -1, /* 56 SONYPI_EVENT_ANYBUTTON_RELEASED */
  250. -1, /* 57 SONYPI_EVENT_BATTERY_INSERT */
  251. -1, /* 58 SONYPI_EVENT_BATTERY_REMOVE */
  252. -1, /* 59 SONYPI_EVENT_FNKEY_RELEASED */
  253. 47, /* 60 SONYPI_EVENT_WIRELESS_ON */
  254. 48, /* 61 SONYPI_EVENT_WIRELESS_OFF */
  255. 49, /* 62 SONYPI_EVENT_ZOOM_IN_PRESSED */
  256. 50, /* 63 SONYPI_EVENT_ZOOM_OUT_PRESSED */
  257. 51, /* 64 SONYPI_EVENT_CD_EJECT_PRESSED */
  258. 52, /* 65 SONYPI_EVENT_MODEKEY_PRESSED */
  259. 53, /* 66 SONYPI_EVENT_PKEY_P4 */
  260. 54, /* 67 SONYPI_EVENT_PKEY_P5 */
  261. 55, /* 68 SONYPI_EVENT_SETTINGKEY_PRESSED */
  262. 56, /* 69 SONYPI_EVENT_VOLUME_INC_PRESSED */
  263. 57, /* 70 SONYPI_EVENT_VOLUME_DEC_PRESSED */
  264. -1, /* 71 SONYPI_EVENT_BRIGHTNESS_PRESSED */
  265. 58, /* 72 SONYPI_EVENT_MEDIA_PRESSED */
  266. 59, /* 72 SONYPI_EVENT_VENDOR_PRESSED */
  267. };
  268. static int sony_laptop_input_keycode_map[] = {
  269. KEY_CAMERA, /* 0 SONYPI_EVENT_CAPTURE_PRESSED */
  270. KEY_RESERVED, /* 1 SONYPI_EVENT_CAPTURE_RELEASED */
  271. KEY_RESERVED, /* 2 SONYPI_EVENT_CAPTURE_PARTIALPRESSED */
  272. KEY_RESERVED, /* 3 SONYPI_EVENT_CAPTURE_PARTIALRELEASED */
  273. KEY_FN_ESC, /* 4 SONYPI_EVENT_FNKEY_ESC */
  274. KEY_FN_F1, /* 5 SONYPI_EVENT_FNKEY_F1 */
  275. KEY_FN_F2, /* 6 SONYPI_EVENT_FNKEY_F2 */
  276. KEY_FN_F3, /* 7 SONYPI_EVENT_FNKEY_F3 */
  277. KEY_FN_F4, /* 8 SONYPI_EVENT_FNKEY_F4 */
  278. KEY_FN_F5, /* 9 SONYPI_EVENT_FNKEY_F5 */
  279. KEY_FN_F6, /* 10 SONYPI_EVENT_FNKEY_F6 */
  280. KEY_FN_F7, /* 11 SONYPI_EVENT_FNKEY_F7 */
  281. KEY_FN_F8, /* 12 SONYPI_EVENT_FNKEY_F8 */
  282. KEY_FN_F9, /* 13 SONYPI_EVENT_FNKEY_F9 */
  283. KEY_FN_F10, /* 14 SONYPI_EVENT_FNKEY_F10 */
  284. KEY_FN_F11, /* 15 SONYPI_EVENT_FNKEY_F11 */
  285. KEY_FN_F12, /* 16 SONYPI_EVENT_FNKEY_F12 */
  286. KEY_FN_1, /* 17 SONYPI_EVENT_FNKEY_1 */
  287. KEY_FN_2, /* 18 SONYPI_EVENT_FNKEY_2 */
  288. KEY_FN_D, /* 19 SONYPI_EVENT_FNKEY_D */
  289. KEY_FN_E, /* 20 SONYPI_EVENT_FNKEY_E */
  290. KEY_FN_F, /* 21 SONYPI_EVENT_FNKEY_F */
  291. KEY_FN_S, /* 22 SONYPI_EVENT_FNKEY_S */
  292. KEY_FN_B, /* 23 SONYPI_EVENT_FNKEY_B */
  293. KEY_BLUETOOTH, /* 24 SONYPI_EVENT_BLUETOOTH_PRESSED */
  294. KEY_PROG1, /* 25 SONYPI_EVENT_PKEY_P1 */
  295. KEY_PROG2, /* 26 SONYPI_EVENT_PKEY_P2 */
  296. KEY_PROG3, /* 27 SONYPI_EVENT_PKEY_P3 */
  297. KEY_BACK, /* 28 SONYPI_EVENT_BACK_PRESSED */
  298. KEY_BLUETOOTH, /* 29 SONYPI_EVENT_BLUETOOTH_ON */
  299. KEY_BLUETOOTH, /* 30 SONYPI_EVENT_BLUETOOTH_OFF */
  300. KEY_HELP, /* 31 SONYPI_EVENT_HELP_PRESSED */
  301. KEY_FN, /* 32 SONYPI_EVENT_FNKEY_ONLY */
  302. KEY_RESERVED, /* 33 SONYPI_EVENT_JOGDIAL_FAST_DOWN */
  303. KEY_RESERVED, /* 34 SONYPI_EVENT_JOGDIAL_FAST_UP */
  304. KEY_RESERVED, /* 35 SONYPI_EVENT_JOGDIAL_FAST_DOWN_PRESSED */
  305. KEY_RESERVED, /* 36 SONYPI_EVENT_JOGDIAL_FAST_UP_PRESSED */
  306. KEY_RESERVED, /* 37 SONYPI_EVENT_JOGDIAL_VFAST_DOWN */
  307. KEY_RESERVED, /* 38 SONYPI_EVENT_JOGDIAL_VFAST_UP */
  308. KEY_RESERVED, /* 39 SONYPI_EVENT_JOGDIAL_VFAST_DOWN_PRESSED */
  309. KEY_RESERVED, /* 40 SONYPI_EVENT_JOGDIAL_VFAST_UP_PRESSED */
  310. KEY_ZOOM, /* 41 SONYPI_EVENT_ZOOM_PRESSED */
  311. BTN_THUMB, /* 42 SONYPI_EVENT_THUMBPHRASE_PRESSED */
  312. KEY_RESERVED, /* 43 SONYPI_EVENT_MEYE_FACE */
  313. KEY_RESERVED, /* 44 SONYPI_EVENT_MEYE_OPPOSITE */
  314. KEY_RESERVED, /* 45 SONYPI_EVENT_MEMORYSTICK_INSERT */
  315. KEY_RESERVED, /* 46 SONYPI_EVENT_MEMORYSTICK_EJECT */
  316. KEY_WLAN, /* 47 SONYPI_EVENT_WIRELESS_ON */
  317. KEY_WLAN, /* 48 SONYPI_EVENT_WIRELESS_OFF */
  318. KEY_ZOOMIN, /* 49 SONYPI_EVENT_ZOOM_IN_PRESSED */
  319. KEY_ZOOMOUT, /* 50 SONYPI_EVENT_ZOOM_OUT_PRESSED */
  320. KEY_EJECTCD, /* 51 SONYPI_EVENT_CD_EJECT_PRESSED */
  321. KEY_F13, /* 52 SONYPI_EVENT_MODEKEY_PRESSED */
  322. KEY_PROG4, /* 53 SONYPI_EVENT_PKEY_P4 */
  323. KEY_F14, /* 54 SONYPI_EVENT_PKEY_P5 */
  324. KEY_F15, /* 55 SONYPI_EVENT_SETTINGKEY_PRESSED */
  325. KEY_VOLUMEUP, /* 56 SONYPI_EVENT_VOLUME_INC_PRESSED */
  326. KEY_VOLUMEDOWN, /* 57 SONYPI_EVENT_VOLUME_DEC_PRESSED */
  327. KEY_MEDIA, /* 58 SONYPI_EVENT_MEDIA_PRESSED */
  328. KEY_VENDOR, /* 59 SONYPI_EVENT_VENDOR_PRESSED */
  329. };
  330. /* release buttons after a short delay if pressed */
  331. static void do_sony_laptop_release_key(struct timer_list *unused)
  332. {
  333. struct sony_laptop_keypress kp;
  334. unsigned long flags;
  335. spin_lock_irqsave(&sony_laptop_input.fifo_lock, flags);
  336. if (kfifo_out(&sony_laptop_input.fifo,
  337. (unsigned char *)&kp, sizeof(kp)) == sizeof(kp)) {
  338. input_report_key(kp.dev, kp.key, 0);
  339. input_sync(kp.dev);
  340. }
  341. /* If there is something in the fifo schedule next release. */
  342. if (kfifo_len(&sony_laptop_input.fifo) != 0)
  343. mod_timer(&sony_laptop_input.release_key_timer,
  344. jiffies + msecs_to_jiffies(10));
  345. spin_unlock_irqrestore(&sony_laptop_input.fifo_lock, flags);
  346. }
  347. /* forward event to the input subsystem */
  348. static void sony_laptop_report_input_event(u8 event)
  349. {
  350. struct input_dev *jog_dev = sony_laptop_input.jog_dev;
  351. struct input_dev *key_dev = sony_laptop_input.key_dev;
  352. struct sony_laptop_keypress kp = { NULL };
  353. int scancode = -1;
  354. if (event == SONYPI_EVENT_FNKEY_RELEASED ||
  355. event == SONYPI_EVENT_ANYBUTTON_RELEASED) {
  356. /* Nothing, not all VAIOs generate this event */
  357. return;
  358. }
  359. /* report events */
  360. switch (event) {
  361. /* jog_dev events */
  362. case SONYPI_EVENT_JOGDIAL_UP:
  363. case SONYPI_EVENT_JOGDIAL_UP_PRESSED:
  364. input_report_rel(jog_dev, REL_WHEEL, 1);
  365. input_sync(jog_dev);
  366. return;
  367. case SONYPI_EVENT_JOGDIAL_DOWN:
  368. case SONYPI_EVENT_JOGDIAL_DOWN_PRESSED:
  369. input_report_rel(jog_dev, REL_WHEEL, -1);
  370. input_sync(jog_dev);
  371. return;
  372. /* key_dev events */
  373. case SONYPI_EVENT_JOGDIAL_PRESSED:
  374. kp.key = BTN_MIDDLE;
  375. kp.dev = jog_dev;
  376. break;
  377. default:
  378. if (event >= ARRAY_SIZE(sony_laptop_input_index)) {
  379. dprintk("sony_laptop_report_input_event, event not known: %d\n", event);
  380. break;
  381. }
  382. if ((scancode = sony_laptop_input_index[event]) != -1) {
  383. kp.key = sony_laptop_input_keycode_map[scancode];
  384. if (kp.key != KEY_UNKNOWN)
  385. kp.dev = key_dev;
  386. }
  387. break;
  388. }
  389. if (kp.dev) {
  390. /* if we have a scancode we emit it so we can always
  391. remap the key */
  392. if (scancode != -1)
  393. input_event(kp.dev, EV_MSC, MSC_SCAN, scancode);
  394. input_report_key(kp.dev, kp.key, 1);
  395. input_sync(kp.dev);
  396. /* schedule key release */
  397. kfifo_in_locked(&sony_laptop_input.fifo,
  398. (unsigned char *)&kp, sizeof(kp),
  399. &sony_laptop_input.fifo_lock);
  400. mod_timer(&sony_laptop_input.release_key_timer,
  401. jiffies + msecs_to_jiffies(10));
  402. } else
  403. dprintk("unknown input event %.2x\n", event);
  404. }
  405. static int sony_laptop_setup_input(struct acpi_device *acpi_device)
  406. {
  407. struct input_dev *jog_dev;
  408. struct input_dev *key_dev;
  409. int i;
  410. int error;
  411. /* don't run again if already initialized */
  412. if (atomic_add_return(1, &sony_laptop_input.users) > 1)
  413. return 0;
  414. /* kfifo */
  415. spin_lock_init(&sony_laptop_input.fifo_lock);
  416. error = kfifo_alloc(&sony_laptop_input.fifo,
  417. SONY_LAPTOP_BUF_SIZE, GFP_KERNEL);
  418. if (error) {
  419. pr_err("kfifo_alloc failed\n");
  420. goto err_dec_users;
  421. }
  422. timer_setup(&sony_laptop_input.release_key_timer,
  423. do_sony_laptop_release_key, 0);
  424. /* input keys */
  425. key_dev = input_allocate_device();
  426. if (!key_dev) {
  427. error = -ENOMEM;
  428. goto err_free_kfifo;
  429. }
  430. key_dev->name = "Sony Vaio Keys";
  431. key_dev->id.bustype = BUS_ISA;
  432. key_dev->id.vendor = PCI_VENDOR_ID_SONY;
  433. key_dev->dev.parent = &acpi_device->dev;
  434. /* Initialize the Input Drivers: special keys */
  435. input_set_capability(key_dev, EV_MSC, MSC_SCAN);
  436. __set_bit(EV_KEY, key_dev->evbit);
  437. key_dev->keycodesize = sizeof(sony_laptop_input_keycode_map[0]);
  438. key_dev->keycodemax = ARRAY_SIZE(sony_laptop_input_keycode_map);
  439. key_dev->keycode = &sony_laptop_input_keycode_map;
  440. for (i = 0; i < ARRAY_SIZE(sony_laptop_input_keycode_map); i++)
  441. __set_bit(sony_laptop_input_keycode_map[i], key_dev->keybit);
  442. __clear_bit(KEY_RESERVED, key_dev->keybit);
  443. error = input_register_device(key_dev);
  444. if (error)
  445. goto err_free_keydev;
  446. sony_laptop_input.key_dev = key_dev;
  447. /* jogdial */
  448. jog_dev = input_allocate_device();
  449. if (!jog_dev) {
  450. error = -ENOMEM;
  451. goto err_unregister_keydev;
  452. }
  453. jog_dev->name = "Sony Vaio Jogdial";
  454. jog_dev->id.bustype = BUS_ISA;
  455. jog_dev->id.vendor = PCI_VENDOR_ID_SONY;
  456. jog_dev->dev.parent = &acpi_device->dev;
  457. input_set_capability(jog_dev, EV_KEY, BTN_MIDDLE);
  458. input_set_capability(jog_dev, EV_REL, REL_WHEEL);
  459. error = input_register_device(jog_dev);
  460. if (error)
  461. goto err_free_jogdev;
  462. sony_laptop_input.jog_dev = jog_dev;
  463. return 0;
  464. err_free_jogdev:
  465. input_free_device(jog_dev);
  466. err_unregister_keydev:
  467. input_unregister_device(key_dev);
  468. /* to avoid kref underflow below at input_free_device */
  469. key_dev = NULL;
  470. err_free_keydev:
  471. input_free_device(key_dev);
  472. err_free_kfifo:
  473. kfifo_free(&sony_laptop_input.fifo);
  474. err_dec_users:
  475. atomic_dec(&sony_laptop_input.users);
  476. return error;
  477. }
  478. static void sony_laptop_remove_input(void)
  479. {
  480. struct sony_laptop_keypress kp = { NULL };
  481. /* Cleanup only after the last user has gone */
  482. if (!atomic_dec_and_test(&sony_laptop_input.users))
  483. return;
  484. del_timer_sync(&sony_laptop_input.release_key_timer);
  485. /*
  486. * Generate key-up events for remaining keys. Note that we don't
  487. * need locking since nobody is adding new events to the kfifo.
  488. */
  489. while (kfifo_out(&sony_laptop_input.fifo,
  490. (unsigned char *)&kp, sizeof(kp)) == sizeof(kp)) {
  491. input_report_key(kp.dev, kp.key, 0);
  492. input_sync(kp.dev);
  493. }
  494. /* destroy input devs */
  495. input_unregister_device(sony_laptop_input.key_dev);
  496. sony_laptop_input.key_dev = NULL;
  497. if (sony_laptop_input.jog_dev) {
  498. input_unregister_device(sony_laptop_input.jog_dev);
  499. sony_laptop_input.jog_dev = NULL;
  500. }
  501. kfifo_free(&sony_laptop_input.fifo);
  502. }
  503. /*********** Platform Device ***********/
  504. static atomic_t sony_pf_users = ATOMIC_INIT(0);
  505. static struct platform_driver sony_pf_driver = {
  506. .driver = {
  507. .name = "sony-laptop",
  508. }
  509. };
  510. static struct platform_device *sony_pf_device;
  511. static int sony_pf_add(void)
  512. {
  513. int ret = 0;
  514. /* don't run again if already initialized */
  515. if (atomic_add_return(1, &sony_pf_users) > 1)
  516. return 0;
  517. ret = platform_driver_register(&sony_pf_driver);
  518. if (ret)
  519. goto out;
  520. sony_pf_device = platform_device_alloc("sony-laptop", -1);
  521. if (!sony_pf_device) {
  522. ret = -ENOMEM;
  523. goto out_platform_registered;
  524. }
  525. ret = platform_device_add(sony_pf_device);
  526. if (ret)
  527. goto out_platform_alloced;
  528. return 0;
  529. out_platform_alloced:
  530. platform_device_put(sony_pf_device);
  531. sony_pf_device = NULL;
  532. out_platform_registered:
  533. platform_driver_unregister(&sony_pf_driver);
  534. out:
  535. atomic_dec(&sony_pf_users);
  536. return ret;
  537. }
  538. static void sony_pf_remove(void)
  539. {
  540. /* deregister only after the last user has gone */
  541. if (!atomic_dec_and_test(&sony_pf_users))
  542. return;
  543. platform_device_unregister(sony_pf_device);
  544. platform_driver_unregister(&sony_pf_driver);
  545. }
  546. /*********** SNC (SNY5001) Device ***********/
  547. /* the device uses 1-based values, while the backlight subsystem uses
  548. 0-based values */
  549. #define SONY_MAX_BRIGHTNESS 8
  550. #define SNC_VALIDATE_IN 0
  551. #define SNC_VALIDATE_OUT 1
  552. static ssize_t sony_nc_sysfs_show(struct device *, struct device_attribute *,
  553. char *);
  554. static ssize_t sony_nc_sysfs_store(struct device *, struct device_attribute *,
  555. const char *, size_t);
  556. static int boolean_validate(const int, const int);
  557. static int brightness_default_validate(const int, const int);
  558. struct sony_nc_value {
  559. char *name; /* name of the entry */
  560. char **acpiget; /* names of the ACPI get function */
  561. char **acpiset; /* names of the ACPI set function */
  562. int (*validate)(const int, const int); /* input/output validation */
  563. int value; /* current setting */
  564. int valid; /* Has ever been set */
  565. int debug; /* active only in debug mode ? */
  566. struct device_attribute devattr; /* sysfs attribute */
  567. };
  568. #define SNC_HANDLE_NAMES(_name, _values...) \
  569. static char *snc_##_name[] = { _values, NULL }
  570. #define SNC_HANDLE(_name, _getters, _setters, _validate, _debug) \
  571. { \
  572. .name = __stringify(_name), \
  573. .acpiget = _getters, \
  574. .acpiset = _setters, \
  575. .validate = _validate, \
  576. .debug = _debug, \
  577. .devattr = __ATTR(_name, 0, sony_nc_sysfs_show, sony_nc_sysfs_store), \
  578. }
  579. #define SNC_HANDLE_NULL { .name = NULL }
  580. SNC_HANDLE_NAMES(fnkey_get, "GHKE");
  581. SNC_HANDLE_NAMES(brightness_def_get, "GPBR");
  582. SNC_HANDLE_NAMES(brightness_def_set, "SPBR");
  583. SNC_HANDLE_NAMES(cdpower_get, "GCDP");
  584. SNC_HANDLE_NAMES(cdpower_set, "SCDP", "CDPW");
  585. SNC_HANDLE_NAMES(audiopower_get, "GAZP");
  586. SNC_HANDLE_NAMES(audiopower_set, "AZPW");
  587. SNC_HANDLE_NAMES(lanpower_get, "GLNP");
  588. SNC_HANDLE_NAMES(lanpower_set, "LNPW");
  589. SNC_HANDLE_NAMES(lidstate_get, "GLID");
  590. SNC_HANDLE_NAMES(indicatorlamp_get, "GILS");
  591. SNC_HANDLE_NAMES(indicatorlamp_set, "SILS");
  592. SNC_HANDLE_NAMES(gainbass_get, "GMGB");
  593. SNC_HANDLE_NAMES(gainbass_set, "CMGB");
  594. SNC_HANDLE_NAMES(PID_get, "GPID");
  595. SNC_HANDLE_NAMES(CTR_get, "GCTR");
  596. SNC_HANDLE_NAMES(CTR_set, "SCTR");
  597. SNC_HANDLE_NAMES(PCR_get, "GPCR");
  598. SNC_HANDLE_NAMES(PCR_set, "SPCR");
  599. SNC_HANDLE_NAMES(CMI_get, "GCMI");
  600. SNC_HANDLE_NAMES(CMI_set, "SCMI");
  601. static struct sony_nc_value sony_nc_values[] = {
  602. SNC_HANDLE(brightness_default, snc_brightness_def_get,
  603. snc_brightness_def_set, brightness_default_validate, 0),
  604. SNC_HANDLE(fnkey, snc_fnkey_get, NULL, NULL, 0),
  605. SNC_HANDLE(cdpower, snc_cdpower_get, snc_cdpower_set, boolean_validate, 0),
  606. SNC_HANDLE(audiopower, snc_audiopower_get, snc_audiopower_set,
  607. boolean_validate, 0),
  608. SNC_HANDLE(lanpower, snc_lanpower_get, snc_lanpower_set,
  609. boolean_validate, 1),
  610. SNC_HANDLE(lidstate, snc_lidstate_get, NULL,
  611. boolean_validate, 0),
  612. SNC_HANDLE(indicatorlamp, snc_indicatorlamp_get, snc_indicatorlamp_set,
  613. boolean_validate, 0),
  614. SNC_HANDLE(gainbass, snc_gainbass_get, snc_gainbass_set,
  615. boolean_validate, 0),
  616. /* unknown methods */
  617. SNC_HANDLE(PID, snc_PID_get, NULL, NULL, 1),
  618. SNC_HANDLE(CTR, snc_CTR_get, snc_CTR_set, NULL, 1),
  619. SNC_HANDLE(PCR, snc_PCR_get, snc_PCR_set, NULL, 1),
  620. SNC_HANDLE(CMI, snc_CMI_get, snc_CMI_set, NULL, 1),
  621. SNC_HANDLE_NULL
  622. };
  623. static acpi_handle sony_nc_acpi_handle;
  624. static struct acpi_device *sony_nc_acpi_device = NULL;
  625. /*
  626. * acpi_evaluate_object wrappers
  627. * all useful calls into SNC methods take one or zero parameters and return
  628. * integers or arrays.
  629. */
  630. static union acpi_object *__call_snc_method(acpi_handle handle, char *method,
  631. u64 *value)
  632. {
  633. union acpi_object *result = NULL;
  634. struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
  635. acpi_status status;
  636. if (value) {
  637. struct acpi_object_list params;
  638. union acpi_object in;
  639. in.type = ACPI_TYPE_INTEGER;
  640. in.integer.value = *value;
  641. params.count = 1;
  642. params.pointer = &in;
  643. status = acpi_evaluate_object(handle, method, &params, &output);
  644. dprintk("__call_snc_method: [%s:0x%.8x%.8x]\n", method,
  645. (unsigned int)(*value >> 32),
  646. (unsigned int)*value & 0xffffffff);
  647. } else {
  648. status = acpi_evaluate_object(handle, method, NULL, &output);
  649. dprintk("__call_snc_method: [%s]\n", method);
  650. }
  651. if (ACPI_FAILURE(status)) {
  652. pr_err("Failed to evaluate [%s]\n", method);
  653. return NULL;
  654. }
  655. result = (union acpi_object *) output.pointer;
  656. if (!result)
  657. dprintk("No return object [%s]\n", method);
  658. return result;
  659. }
  660. static int sony_nc_int_call(acpi_handle handle, char *name, int *value,
  661. int *result)
  662. {
  663. union acpi_object *object = NULL;
  664. if (value) {
  665. u64 v = *value;
  666. object = __call_snc_method(handle, name, &v);
  667. } else
  668. object = __call_snc_method(handle, name, NULL);
  669. if (!object)
  670. return -EINVAL;
  671. if (object->type != ACPI_TYPE_INTEGER) {
  672. pr_warn("Invalid acpi_object: expected 0x%x got 0x%x\n",
  673. ACPI_TYPE_INTEGER, object->type);
  674. kfree(object);
  675. return -EINVAL;
  676. }
  677. if (result)
  678. *result = object->integer.value;
  679. kfree(object);
  680. return 0;
  681. }
  682. #define MIN(a, b) (a > b ? b : a)
  683. static int sony_nc_buffer_call(acpi_handle handle, char *name, u64 *value,
  684. void *buffer, size_t buflen)
  685. {
  686. int ret = 0;
  687. size_t len;
  688. union acpi_object *object = __call_snc_method(handle, name, value);
  689. if (!object)
  690. return -EINVAL;
  691. if (object->type == ACPI_TYPE_BUFFER) {
  692. len = MIN(buflen, object->buffer.length);
  693. memcpy(buffer, object->buffer.pointer, len);
  694. } else if (object->type == ACPI_TYPE_INTEGER) {
  695. len = MIN(buflen, sizeof(object->integer.value));
  696. memcpy(buffer, &object->integer.value, len);
  697. } else {
  698. pr_warn("Invalid acpi_object: expected 0x%x got 0x%x\n",
  699. ACPI_TYPE_BUFFER, object->type);
  700. ret = -EINVAL;
  701. }
  702. kfree(object);
  703. return ret;
  704. }
  705. struct sony_nc_handles {
  706. u16 cap[0x10];
  707. struct device_attribute devattr;
  708. };
  709. static struct sony_nc_handles *handles;
  710. static ssize_t sony_nc_handles_show(struct device *dev,
  711. struct device_attribute *attr, char *buffer)
  712. {
  713. ssize_t len = 0;
  714. int i;
  715. for (i = 0; i < ARRAY_SIZE(handles->cap); i++) {
  716. len += snprintf(buffer + len, PAGE_SIZE - len, "0x%.4x ",
  717. handles->cap[i]);
  718. }
  719. len += snprintf(buffer + len, PAGE_SIZE - len, "\n");
  720. return len;
  721. }
  722. static int sony_nc_handles_setup(struct platform_device *pd)
  723. {
  724. int i, r, result, arg;
  725. handles = kzalloc(sizeof(*handles), GFP_KERNEL);
  726. if (!handles)
  727. return -ENOMEM;
  728. for (i = 0; i < ARRAY_SIZE(handles->cap); i++) {
  729. arg = i + 0x20;
  730. r = sony_nc_int_call(sony_nc_acpi_handle, "SN00", &arg,
  731. &result);
  732. if (!r) {
  733. dprintk("caching handle 0x%.4x (offset: 0x%.2x)\n",
  734. result, i);
  735. handles->cap[i] = result;
  736. }
  737. }
  738. if (debug) {
  739. sysfs_attr_init(&handles->devattr.attr);
  740. handles->devattr.attr.name = "handles";
  741. handles->devattr.attr.mode = S_IRUGO;
  742. handles->devattr.show = sony_nc_handles_show;
  743. /* allow reading capabilities via sysfs */
  744. if (device_create_file(&pd->dev, &handles->devattr)) {
  745. kfree(handles);
  746. handles = NULL;
  747. return -1;
  748. }
  749. }
  750. return 0;
  751. }
  752. static int sony_nc_handles_cleanup(struct platform_device *pd)
  753. {
  754. if (handles) {
  755. if (debug)
  756. device_remove_file(&pd->dev, &handles->devattr);
  757. kfree(handles);
  758. handles = NULL;
  759. }
  760. return 0;
  761. }
  762. static int sony_find_snc_handle(int handle)
  763. {
  764. int i;
  765. /* not initialized yet, return early */
  766. if (!handles || !handle)
  767. return -EINVAL;
  768. for (i = 0; i < 0x10; i++) {
  769. if (handles->cap[i] == handle) {
  770. dprintk("found handle 0x%.4x (offset: 0x%.2x)\n",
  771. handle, i);
  772. return i;
  773. }
  774. }
  775. dprintk("handle 0x%.4x not found\n", handle);
  776. return -EINVAL;
  777. }
  778. static int sony_call_snc_handle(int handle, int argument, int *result)
  779. {
  780. int arg, ret = 0;
  781. int offset = sony_find_snc_handle(handle);
  782. if (offset < 0)
  783. return offset;
  784. arg = offset | argument;
  785. ret = sony_nc_int_call(sony_nc_acpi_handle, "SN07", &arg, result);
  786. dprintk("called SN07 with 0x%.4x (result: 0x%.4x)\n", arg, *result);
  787. return ret;
  788. }
  789. /*
  790. * sony_nc_values input/output validate functions
  791. */
  792. /* brightness_default_validate:
  793. *
  794. * manipulate input output values to keep consistency with the
  795. * backlight framework for which brightness values are 0-based.
  796. */
  797. static int brightness_default_validate(const int direction, const int value)
  798. {
  799. switch (direction) {
  800. case SNC_VALIDATE_OUT:
  801. return value - 1;
  802. case SNC_VALIDATE_IN:
  803. if (value >= 0 && value < SONY_MAX_BRIGHTNESS)
  804. return value + 1;
  805. }
  806. return -EINVAL;
  807. }
  808. /* boolean_validate:
  809. *
  810. * on input validate boolean values 0/1, on output just pass the
  811. * received value.
  812. */
  813. static int boolean_validate(const int direction, const int value)
  814. {
  815. if (direction == SNC_VALIDATE_IN) {
  816. if (value != 0 && value != 1)
  817. return -EINVAL;
  818. }
  819. return value;
  820. }
  821. /*
  822. * Sysfs show/store common to all sony_nc_values
  823. */
  824. static ssize_t sony_nc_sysfs_show(struct device *dev, struct device_attribute *attr,
  825. char *buffer)
  826. {
  827. int value, ret = 0;
  828. struct sony_nc_value *item =
  829. container_of(attr, struct sony_nc_value, devattr);
  830. if (!*item->acpiget)
  831. return -EIO;
  832. ret = sony_nc_int_call(sony_nc_acpi_handle, *item->acpiget, NULL,
  833. &value);
  834. if (ret < 0)
  835. return -EIO;
  836. if (item->validate)
  837. value = item->validate(SNC_VALIDATE_OUT, value);
  838. return snprintf(buffer, PAGE_SIZE, "%d\n", value);
  839. }
  840. static ssize_t sony_nc_sysfs_store(struct device *dev,
  841. struct device_attribute *attr,
  842. const char *buffer, size_t count)
  843. {
  844. int value;
  845. int ret = 0;
  846. struct sony_nc_value *item =
  847. container_of(attr, struct sony_nc_value, devattr);
  848. if (!item->acpiset)
  849. return -EIO;
  850. if (count > 31)
  851. return -EINVAL;
  852. if (kstrtoint(buffer, 10, &value))
  853. return -EINVAL;
  854. if (item->validate)
  855. value = item->validate(SNC_VALIDATE_IN, value);
  856. if (value < 0)
  857. return value;
  858. ret = sony_nc_int_call(sony_nc_acpi_handle, *item->acpiset,
  859. &value, NULL);
  860. if (ret < 0)
  861. return -EIO;
  862. item->value = value;
  863. item->valid = 1;
  864. return count;
  865. }
  866. /*
  867. * Backlight device
  868. */
  869. struct sony_backlight_props {
  870. struct backlight_device *dev;
  871. int handle;
  872. int cmd_base;
  873. u8 offset;
  874. u8 maxlvl;
  875. };
  876. static struct sony_backlight_props sony_bl_props;
  877. static int sony_backlight_update_status(struct backlight_device *bd)
  878. {
  879. int arg = bd->props.brightness + 1;
  880. return sony_nc_int_call(sony_nc_acpi_handle, "SBRT", &arg, NULL);
  881. }
  882. static int sony_backlight_get_brightness(struct backlight_device *bd)
  883. {
  884. int value;
  885. if (sony_nc_int_call(sony_nc_acpi_handle, "GBRT", NULL, &value))
  886. return 0;
  887. /* brightness levels are 1-based, while backlight ones are 0-based */
  888. return value - 1;
  889. }
  890. static int sony_nc_get_brightness_ng(struct backlight_device *bd)
  891. {
  892. int result;
  893. struct sony_backlight_props *sdev =
  894. (struct sony_backlight_props *)bl_get_data(bd);
  895. sony_call_snc_handle(sdev->handle, sdev->cmd_base + 0x100, &result);
  896. return (result & 0xff) - sdev->offset;
  897. }
  898. static int sony_nc_update_status_ng(struct backlight_device *bd)
  899. {
  900. int value, result;
  901. struct sony_backlight_props *sdev =
  902. (struct sony_backlight_props *)bl_get_data(bd);
  903. value = bd->props.brightness + sdev->offset;
  904. if (sony_call_snc_handle(sdev->handle, sdev->cmd_base | (value << 0x10),
  905. &result))
  906. return -EIO;
  907. return value;
  908. }
  909. static const struct backlight_ops sony_backlight_ops = {
  910. .options = BL_CORE_SUSPENDRESUME,
  911. .update_status = sony_backlight_update_status,
  912. .get_brightness = sony_backlight_get_brightness,
  913. };
  914. static const struct backlight_ops sony_backlight_ng_ops = {
  915. .options = BL_CORE_SUSPENDRESUME,
  916. .update_status = sony_nc_update_status_ng,
  917. .get_brightness = sony_nc_get_brightness_ng,
  918. };
  919. /*
  920. * New SNC-only Vaios event mapping to driver known keys
  921. */
  922. struct sony_nc_event {
  923. u8 data;
  924. u8 event;
  925. };
  926. static struct sony_nc_event sony_100_events[] = {
  927. { 0x90, SONYPI_EVENT_PKEY_P1 },
  928. { 0x10, SONYPI_EVENT_ANYBUTTON_RELEASED },
  929. { 0x91, SONYPI_EVENT_PKEY_P2 },
  930. { 0x11, SONYPI_EVENT_ANYBUTTON_RELEASED },
  931. { 0x81, SONYPI_EVENT_FNKEY_F1 },
  932. { 0x01, SONYPI_EVENT_FNKEY_RELEASED },
  933. { 0x82, SONYPI_EVENT_FNKEY_F2 },
  934. { 0x02, SONYPI_EVENT_FNKEY_RELEASED },
  935. { 0x83, SONYPI_EVENT_FNKEY_F3 },
  936. { 0x03, SONYPI_EVENT_FNKEY_RELEASED },
  937. { 0x84, SONYPI_EVENT_FNKEY_F4 },
  938. { 0x04, SONYPI_EVENT_FNKEY_RELEASED },
  939. { 0x85, SONYPI_EVENT_FNKEY_F5 },
  940. { 0x05, SONYPI_EVENT_FNKEY_RELEASED },
  941. { 0x86, SONYPI_EVENT_FNKEY_F6 },
  942. { 0x06, SONYPI_EVENT_FNKEY_RELEASED },
  943. { 0x87, SONYPI_EVENT_FNKEY_F7 },
  944. { 0x07, SONYPI_EVENT_FNKEY_RELEASED },
  945. { 0x88, SONYPI_EVENT_FNKEY_F8 },
  946. { 0x08, SONYPI_EVENT_FNKEY_RELEASED },
  947. { 0x89, SONYPI_EVENT_FNKEY_F9 },
  948. { 0x09, SONYPI_EVENT_FNKEY_RELEASED },
  949. { 0x8A, SONYPI_EVENT_FNKEY_F10 },
  950. { 0x0A, SONYPI_EVENT_FNKEY_RELEASED },
  951. { 0x8B, SONYPI_EVENT_FNKEY_F11 },
  952. { 0x0B, SONYPI_EVENT_FNKEY_RELEASED },
  953. { 0x8C, SONYPI_EVENT_FNKEY_F12 },
  954. { 0x0C, SONYPI_EVENT_FNKEY_RELEASED },
  955. { 0x9d, SONYPI_EVENT_ZOOM_PRESSED },
  956. { 0x1d, SONYPI_EVENT_ANYBUTTON_RELEASED },
  957. { 0x9f, SONYPI_EVENT_CD_EJECT_PRESSED },
  958. { 0x1f, SONYPI_EVENT_ANYBUTTON_RELEASED },
  959. { 0xa1, SONYPI_EVENT_MEDIA_PRESSED },
  960. { 0x21, SONYPI_EVENT_ANYBUTTON_RELEASED },
  961. { 0xa4, SONYPI_EVENT_CD_EJECT_PRESSED },
  962. { 0x24, SONYPI_EVENT_ANYBUTTON_RELEASED },
  963. { 0xa5, SONYPI_EVENT_VENDOR_PRESSED },
  964. { 0x25, SONYPI_EVENT_ANYBUTTON_RELEASED },
  965. { 0xa6, SONYPI_EVENT_HELP_PRESSED },
  966. { 0x26, SONYPI_EVENT_ANYBUTTON_RELEASED },
  967. { 0xa8, SONYPI_EVENT_FNKEY_1 },
  968. { 0x28, SONYPI_EVENT_ANYBUTTON_RELEASED },
  969. { 0, 0 },
  970. };
  971. static struct sony_nc_event sony_127_events[] = {
  972. { 0x81, SONYPI_EVENT_MODEKEY_PRESSED },
  973. { 0x01, SONYPI_EVENT_ANYBUTTON_RELEASED },
  974. { 0x82, SONYPI_EVENT_PKEY_P1 },
  975. { 0x02, SONYPI_EVENT_ANYBUTTON_RELEASED },
  976. { 0x83, SONYPI_EVENT_PKEY_P2 },
  977. { 0x03, SONYPI_EVENT_ANYBUTTON_RELEASED },
  978. { 0x84, SONYPI_EVENT_PKEY_P3 },
  979. { 0x04, SONYPI_EVENT_ANYBUTTON_RELEASED },
  980. { 0x85, SONYPI_EVENT_PKEY_P4 },
  981. { 0x05, SONYPI_EVENT_ANYBUTTON_RELEASED },
  982. { 0x86, SONYPI_EVENT_PKEY_P5 },
  983. { 0x06, SONYPI_EVENT_ANYBUTTON_RELEASED },
  984. { 0x87, SONYPI_EVENT_SETTINGKEY_PRESSED },
  985. { 0x07, SONYPI_EVENT_ANYBUTTON_RELEASED },
  986. { 0, 0 },
  987. };
  988. static int sony_nc_hotkeys_decode(u32 event, unsigned int handle)
  989. {
  990. int ret = -EINVAL;
  991. unsigned int result = 0;
  992. struct sony_nc_event *key_event;
  993. if (sony_call_snc_handle(handle, 0x200, &result)) {
  994. dprintk("Unable to decode event 0x%.2x 0x%.2x\n", handle,
  995. event);
  996. return -EINVAL;
  997. }
  998. result &= 0xFF;
  999. if (handle == 0x0100)
  1000. key_event = sony_100_events;
  1001. else
  1002. key_event = sony_127_events;
  1003. for (; key_event->data; key_event++) {
  1004. if (key_event->data == result) {
  1005. ret = key_event->event;
  1006. break;
  1007. }
  1008. }
  1009. if (!key_event->data)
  1010. pr_info("Unknown hotkey 0x%.2x/0x%.2x (handle 0x%.2x)\n",
  1011. event, result, handle);
  1012. return ret;
  1013. }
  1014. /*
  1015. * ACPI callbacks
  1016. */
  1017. enum event_types {
  1018. HOTKEY = 1,
  1019. KILLSWITCH,
  1020. GFX_SWITCH
  1021. };
  1022. static void sony_nc_notify(struct acpi_device *device, u32 event)
  1023. {
  1024. u32 real_ev = event;
  1025. u8 ev_type = 0;
  1026. int ret;
  1027. dprintk("sony_nc_notify, event: 0x%.2x\n", event);
  1028. if (event >= 0x90) {
  1029. unsigned int result = 0;
  1030. unsigned int arg = 0;
  1031. unsigned int handle = 0;
  1032. unsigned int offset = event - 0x90;
  1033. if (offset >= ARRAY_SIZE(handles->cap)) {
  1034. pr_err("Event 0x%x outside of capabilities list\n",
  1035. event);
  1036. return;
  1037. }
  1038. handle = handles->cap[offset];
  1039. /* list of handles known for generating events */
  1040. switch (handle) {
  1041. /* hotkey event */
  1042. case 0x0100:
  1043. case 0x0127:
  1044. ev_type = HOTKEY;
  1045. ret = sony_nc_hotkeys_decode(event, handle);
  1046. if (ret > 0) {
  1047. sony_laptop_report_input_event(ret);
  1048. real_ev = ret;
  1049. }
  1050. break;
  1051. /* wlan switch */
  1052. case 0x0124:
  1053. case 0x0135:
  1054. /* events on this handle are reported when the
  1055. * switch changes position or for battery
  1056. * events. We'll notify both of them but only
  1057. * update the rfkill device status when the
  1058. * switch is moved.
  1059. */
  1060. ev_type = KILLSWITCH;
  1061. sony_call_snc_handle(handle, 0x0100, &result);
  1062. real_ev = result & 0x03;
  1063. /* hw switch event */
  1064. if (real_ev == 1)
  1065. sony_nc_rfkill_update();
  1066. break;
  1067. case 0x0128:
  1068. case 0x0146:
  1069. /* Hybrid GFX switching */
  1070. sony_call_snc_handle(handle, 0x0000, &result);
  1071. dprintk("GFX switch event received (reason: %s)\n",
  1072. (result == 0x1) ? "switch change" :
  1073. (result == 0x2) ? "output switch" :
  1074. (result == 0x3) ? "output switch" :
  1075. "");
  1076. ev_type = GFX_SWITCH;
  1077. real_ev = __sony_nc_gfx_switch_status_get();
  1078. break;
  1079. case 0x015B:
  1080. /* Hybrid GFX switching SVS151290S */
  1081. ev_type = GFX_SWITCH;
  1082. real_ev = __sony_nc_gfx_switch_status_get();
  1083. break;
  1084. default:
  1085. dprintk("Unknown event 0x%x for handle 0x%x\n",
  1086. event, handle);
  1087. break;
  1088. }
  1089. /* clear the event (and the event reason when present) */
  1090. arg = 1 << offset;
  1091. sony_nc_int_call(sony_nc_acpi_handle, "SN05", &arg, &result);
  1092. } else {
  1093. /* old style event */
  1094. ev_type = HOTKEY;
  1095. sony_laptop_report_input_event(real_ev);
  1096. }
  1097. acpi_bus_generate_netlink_event(sony_nc_acpi_device->pnp.device_class,
  1098. dev_name(&sony_nc_acpi_device->dev), ev_type, real_ev);
  1099. }
  1100. static acpi_status sony_walk_callback(acpi_handle handle, u32 level,
  1101. void *context, void **return_value)
  1102. {
  1103. struct acpi_device_info *info;
  1104. if (ACPI_SUCCESS(acpi_get_object_info(handle, &info))) {
  1105. pr_warn("method: name: %4.4s, args %X\n",
  1106. (char *)&info->name, info->param_count);
  1107. kfree(info);
  1108. }
  1109. return AE_OK;
  1110. }
  1111. /*
  1112. * ACPI device
  1113. */
  1114. static void sony_nc_function_setup(struct acpi_device *device,
  1115. struct platform_device *pf_device)
  1116. {
  1117. unsigned int i, result, bitmask, arg;
  1118. if (!handles)
  1119. return;
  1120. /* setup found handles here */
  1121. for (i = 0; i < ARRAY_SIZE(handles->cap); i++) {
  1122. unsigned int handle = handles->cap[i];
  1123. if (!handle)
  1124. continue;
  1125. dprintk("setting up handle 0x%.4x\n", handle);
  1126. switch (handle) {
  1127. case 0x0100:
  1128. case 0x0101:
  1129. case 0x0127:
  1130. /* setup hotkeys */
  1131. sony_call_snc_handle(handle, 0, &result);
  1132. break;
  1133. case 0x0102:
  1134. /* setup hotkeys */
  1135. sony_call_snc_handle(handle, 0x100, &result);
  1136. break;
  1137. case 0x0105:
  1138. case 0x0148:
  1139. /* touchpad enable/disable */
  1140. result = sony_nc_touchpad_setup(pf_device, handle);
  1141. if (result)
  1142. pr_err("couldn't set up touchpad control function (%d)\n",
  1143. result);
  1144. break;
  1145. case 0x0115:
  1146. case 0x0136:
  1147. case 0x013f:
  1148. result = sony_nc_battery_care_setup(pf_device, handle);
  1149. if (result)
  1150. pr_err("couldn't set up battery care function (%d)\n",
  1151. result);
  1152. break;
  1153. case 0x0119:
  1154. case 0x015D:
  1155. result = sony_nc_lid_resume_setup(pf_device, handle);
  1156. if (result)
  1157. pr_err("couldn't set up lid resume function (%d)\n",
  1158. result);
  1159. break;
  1160. case 0x0122:
  1161. result = sony_nc_thermal_setup(pf_device);
  1162. if (result)
  1163. pr_err("couldn't set up thermal profile function (%d)\n",
  1164. result);
  1165. break;
  1166. case 0x0128:
  1167. case 0x0146:
  1168. case 0x015B:
  1169. result = sony_nc_gfx_switch_setup(pf_device, handle);
  1170. if (result)
  1171. pr_err("couldn't set up GFX Switch status (%d)\n",
  1172. result);
  1173. break;
  1174. case 0x0131:
  1175. result = sony_nc_highspeed_charging_setup(pf_device);
  1176. if (result)
  1177. pr_err("couldn't set up high speed charging function (%d)\n",
  1178. result);
  1179. break;
  1180. case 0x0124:
  1181. case 0x0135:
  1182. result = sony_nc_rfkill_setup(device, handle);
  1183. if (result)
  1184. pr_err("couldn't set up rfkill support (%d)\n",
  1185. result);
  1186. break;
  1187. case 0x0137:
  1188. case 0x0143:
  1189. case 0x014b:
  1190. case 0x014c:
  1191. case 0x0153:
  1192. case 0x0163:
  1193. result = sony_nc_kbd_backlight_setup(pf_device, handle);
  1194. if (result)
  1195. pr_err("couldn't set up keyboard backlight function (%d)\n",
  1196. result);
  1197. break;
  1198. case 0x0121:
  1199. result = sony_nc_lowbatt_setup(pf_device);
  1200. if (result)
  1201. pr_err("couldn't set up low battery function (%d)\n",
  1202. result);
  1203. break;
  1204. case 0x0149:
  1205. result = sony_nc_fanspeed_setup(pf_device);
  1206. if (result)
  1207. pr_err("couldn't set up fan speed function (%d)\n",
  1208. result);
  1209. break;
  1210. case 0x0155:
  1211. result = sony_nc_usb_charge_setup(pf_device);
  1212. if (result)
  1213. pr_err("couldn't set up USB charge support (%d)\n",
  1214. result);
  1215. break;
  1216. case 0x011D:
  1217. result = sony_nc_panelid_setup(pf_device);
  1218. if (result)
  1219. pr_err("couldn't set up panel ID function (%d)\n",
  1220. result);
  1221. break;
  1222. case 0x0168:
  1223. result = sony_nc_smart_conn_setup(pf_device);
  1224. if (result)
  1225. pr_err("couldn't set up smart connect support (%d)\n",
  1226. result);
  1227. break;
  1228. default:
  1229. continue;
  1230. }
  1231. }
  1232. /* Enable all events */
  1233. arg = 0x10;
  1234. if (!sony_nc_int_call(sony_nc_acpi_handle, "SN00", &arg, &bitmask))
  1235. sony_nc_int_call(sony_nc_acpi_handle, "SN02", &bitmask,
  1236. &result);
  1237. }
  1238. static void sony_nc_function_cleanup(struct platform_device *pd)
  1239. {
  1240. unsigned int i, result, bitmask, handle;
  1241. if (!handles)
  1242. return;
  1243. /* get enabled events and disable them */
  1244. sony_nc_int_call(sony_nc_acpi_handle, "SN01", NULL, &bitmask);
  1245. sony_nc_int_call(sony_nc_acpi_handle, "SN03", &bitmask, &result);
  1246. /* cleanup handles here */
  1247. for (i = 0; i < ARRAY_SIZE(handles->cap); i++) {
  1248. handle = handles->cap[i];
  1249. if (!handle)
  1250. continue;
  1251. switch (handle) {
  1252. case 0x0105:
  1253. case 0x0148:
  1254. sony_nc_touchpad_cleanup(pd);
  1255. break;
  1256. case 0x0115:
  1257. case 0x0136:
  1258. case 0x013f:
  1259. sony_nc_battery_care_cleanup(pd);
  1260. break;
  1261. case 0x0119:
  1262. case 0x015D:
  1263. sony_nc_lid_resume_cleanup(pd);
  1264. break;
  1265. case 0x0122:
  1266. sony_nc_thermal_cleanup(pd);
  1267. break;
  1268. case 0x0128:
  1269. case 0x0146:
  1270. case 0x015B:
  1271. sony_nc_gfx_switch_cleanup(pd);
  1272. break;
  1273. case 0x0131:
  1274. sony_nc_highspeed_charging_cleanup(pd);
  1275. break;
  1276. case 0x0124:
  1277. case 0x0135:
  1278. sony_nc_rfkill_cleanup();
  1279. break;
  1280. case 0x0137:
  1281. case 0x0143:
  1282. case 0x014b:
  1283. case 0x014c:
  1284. case 0x0153:
  1285. case 0x0163:
  1286. sony_nc_kbd_backlight_cleanup(pd, handle);
  1287. break;
  1288. case 0x0121:
  1289. sony_nc_lowbatt_cleanup(pd);
  1290. break;
  1291. case 0x0149:
  1292. sony_nc_fanspeed_cleanup(pd);
  1293. break;
  1294. case 0x0155:
  1295. sony_nc_usb_charge_cleanup(pd);
  1296. break;
  1297. case 0x011D:
  1298. sony_nc_panelid_cleanup(pd);
  1299. break;
  1300. case 0x0168:
  1301. sony_nc_smart_conn_cleanup(pd);
  1302. break;
  1303. default:
  1304. continue;
  1305. }
  1306. }
  1307. /* finally cleanup the handles list */
  1308. sony_nc_handles_cleanup(pd);
  1309. }
  1310. #ifdef CONFIG_PM_SLEEP
  1311. static void sony_nc_function_resume(void)
  1312. {
  1313. unsigned int i, result, bitmask, arg;
  1314. dprintk("Resuming SNC device\n");
  1315. for (i = 0; i < ARRAY_SIZE(handles->cap); i++) {
  1316. unsigned int handle = handles->cap[i];
  1317. if (!handle)
  1318. continue;
  1319. switch (handle) {
  1320. case 0x0100:
  1321. case 0x0101:
  1322. case 0x0127:
  1323. /* re-enable hotkeys */
  1324. sony_call_snc_handle(handle, 0, &result);
  1325. break;
  1326. case 0x0102:
  1327. /* re-enable hotkeys */
  1328. sony_call_snc_handle(handle, 0x100, &result);
  1329. break;
  1330. case 0x0122:
  1331. sony_nc_thermal_resume();
  1332. break;
  1333. case 0x0124:
  1334. case 0x0135:
  1335. sony_nc_rfkill_update();
  1336. break;
  1337. default:
  1338. continue;
  1339. }
  1340. }
  1341. /* Enable all events */
  1342. arg = 0x10;
  1343. if (!sony_nc_int_call(sony_nc_acpi_handle, "SN00", &arg, &bitmask))
  1344. sony_nc_int_call(sony_nc_acpi_handle, "SN02", &bitmask,
  1345. &result);
  1346. }
  1347. static int sony_nc_resume(struct device *dev)
  1348. {
  1349. struct sony_nc_value *item;
  1350. for (item = sony_nc_values; item->name; item++) {
  1351. int ret;
  1352. if (!item->valid)
  1353. continue;
  1354. ret = sony_nc_int_call(sony_nc_acpi_handle, *item->acpiset,
  1355. &item->value, NULL);
  1356. if (ret < 0) {
  1357. pr_err("%s: %d\n", __func__, ret);
  1358. break;
  1359. }
  1360. }
  1361. if (acpi_has_method(sony_nc_acpi_handle, "ECON")) {
  1362. int arg = 1;
  1363. if (sony_nc_int_call(sony_nc_acpi_handle, "ECON", &arg, NULL))
  1364. dprintk("ECON Method failed\n");
  1365. }
  1366. if (acpi_has_method(sony_nc_acpi_handle, "SN00"))
  1367. sony_nc_function_resume();
  1368. return 0;
  1369. }
  1370. #endif
  1371. static SIMPLE_DEV_PM_OPS(sony_nc_pm, NULL, sony_nc_resume);
  1372. static void sony_nc_rfkill_cleanup(void)
  1373. {
  1374. int i;
  1375. for (i = 0; i < N_SONY_RFKILL; i++) {
  1376. if (sony_rfkill_devices[i]) {
  1377. rfkill_unregister(sony_rfkill_devices[i]);
  1378. rfkill_destroy(sony_rfkill_devices[i]);
  1379. }
  1380. }
  1381. }
  1382. static int sony_nc_rfkill_set(void *data, bool blocked)
  1383. {
  1384. int result;
  1385. int argument = sony_rfkill_address[(long) data] + 0x100;
  1386. if (!blocked)
  1387. argument |= 0x070000;
  1388. return sony_call_snc_handle(sony_rfkill_handle, argument, &result);
  1389. }
  1390. static const struct rfkill_ops sony_rfkill_ops = {
  1391. .set_block = sony_nc_rfkill_set,
  1392. };
  1393. static int sony_nc_setup_rfkill(struct acpi_device *device,
  1394. enum sony_nc_rfkill nc_type)
  1395. {
  1396. int err;
  1397. struct rfkill *rfk;
  1398. enum rfkill_type type;
  1399. const char *name;
  1400. int result;
  1401. bool hwblock, swblock;
  1402. switch (nc_type) {
  1403. case SONY_WIFI:
  1404. type = RFKILL_TYPE_WLAN;
  1405. name = "sony-wifi";
  1406. break;
  1407. case SONY_BLUETOOTH:
  1408. type = RFKILL_TYPE_BLUETOOTH;
  1409. name = "sony-bluetooth";
  1410. break;
  1411. case SONY_WWAN:
  1412. type = RFKILL_TYPE_WWAN;
  1413. name = "sony-wwan";
  1414. break;
  1415. case SONY_WIMAX:
  1416. type = RFKILL_TYPE_WIMAX;
  1417. name = "sony-wimax";
  1418. break;
  1419. default:
  1420. return -EINVAL;
  1421. }
  1422. rfk = rfkill_alloc(name, &device->dev, type,
  1423. &sony_rfkill_ops, (void *)nc_type);
  1424. if (!rfk)
  1425. return -ENOMEM;
  1426. err = sony_call_snc_handle(sony_rfkill_handle, 0x200, &result);
  1427. if (err < 0) {
  1428. rfkill_destroy(rfk);
  1429. return err;
  1430. }
  1431. hwblock = !(result & 0x1);
  1432. err = sony_call_snc_handle(sony_rfkill_handle,
  1433. sony_rfkill_address[nc_type],
  1434. &result);
  1435. if (err < 0) {
  1436. rfkill_destroy(rfk);
  1437. return err;
  1438. }
  1439. swblock = !(result & 0x2);
  1440. rfkill_init_sw_state(rfk, swblock);
  1441. rfkill_set_hw_state(rfk, hwblock);
  1442. err = rfkill_register(rfk);
  1443. if (err) {
  1444. rfkill_destroy(rfk);
  1445. return err;
  1446. }
  1447. sony_rfkill_devices[nc_type] = rfk;
  1448. return err;
  1449. }
  1450. static void sony_nc_rfkill_update(void)
  1451. {
  1452. enum sony_nc_rfkill i;
  1453. int result;
  1454. bool hwblock;
  1455. sony_call_snc_handle(sony_rfkill_handle, 0x200, &result);
  1456. hwblock = !(result & 0x1);
  1457. for (i = 0; i < N_SONY_RFKILL; i++) {
  1458. int argument = sony_rfkill_address[i];
  1459. if (!sony_rfkill_devices[i])
  1460. continue;
  1461. if (hwblock) {
  1462. if (rfkill_set_hw_state(sony_rfkill_devices[i], true)) {
  1463. /* we already know we're blocked */
  1464. }
  1465. continue;
  1466. }
  1467. sony_call_snc_handle(sony_rfkill_handle, argument, &result);
  1468. rfkill_set_states(sony_rfkill_devices[i],
  1469. !(result & 0x2), false);
  1470. }
  1471. }
  1472. static int sony_nc_rfkill_setup(struct acpi_device *device,
  1473. unsigned int handle)
  1474. {
  1475. u64 offset;
  1476. int i;
  1477. unsigned char buffer[32] = { 0 };
  1478. offset = sony_find_snc_handle(handle);
  1479. sony_rfkill_handle = handle;
  1480. i = sony_nc_buffer_call(sony_nc_acpi_handle, "SN06", &offset, buffer,
  1481. 32);
  1482. if (i < 0)
  1483. return i;
  1484. /* The buffer is filled with magic numbers describing the devices
  1485. * available, 0xff terminates the enumeration.
  1486. * Known codes:
  1487. * 0x00 WLAN
  1488. * 0x10 BLUETOOTH
  1489. * 0x20 WWAN GPRS-EDGE
  1490. * 0x21 WWAN HSDPA
  1491. * 0x22 WWAN EV-DO
  1492. * 0x23 WWAN GPS
  1493. * 0x25 Gobi WWAN no GPS
  1494. * 0x26 Gobi WWAN + GPS
  1495. * 0x28 Gobi WWAN no GPS
  1496. * 0x29 Gobi WWAN + GPS
  1497. * 0x30 WIMAX
  1498. * 0x50 Gobi WWAN no GPS
  1499. * 0x51 Gobi WWAN + GPS
  1500. * 0x70 no SIM card slot
  1501. * 0x71 SIM card slot
  1502. */
  1503. for (i = 0; i < ARRAY_SIZE(buffer); i++) {
  1504. if (buffer[i] == 0xff)
  1505. break;
  1506. dprintk("Radio devices, found 0x%.2x\n", buffer[i]);
  1507. if (buffer[i] == 0 && !sony_rfkill_devices[SONY_WIFI])
  1508. sony_nc_setup_rfkill(device, SONY_WIFI);
  1509. if (buffer[i] == 0x10 && !sony_rfkill_devices[SONY_BLUETOOTH])
  1510. sony_nc_setup_rfkill(device, SONY_BLUETOOTH);
  1511. if (((0xf0 & buffer[i]) == 0x20 ||
  1512. (0xf0 & buffer[i]) == 0x50) &&
  1513. !sony_rfkill_devices[SONY_WWAN])
  1514. sony_nc_setup_rfkill(device, SONY_WWAN);
  1515. if (buffer[i] == 0x30 && !sony_rfkill_devices[SONY_WIMAX])
  1516. sony_nc_setup_rfkill(device, SONY_WIMAX);
  1517. }
  1518. return 0;
  1519. }
  1520. /* Keyboard backlight feature */
  1521. struct kbd_backlight {
  1522. unsigned int handle;
  1523. unsigned int base;
  1524. unsigned int mode;
  1525. unsigned int timeout;
  1526. unsigned int has_timeout;
  1527. struct device_attribute mode_attr;
  1528. struct device_attribute timeout_attr;
  1529. };
  1530. static struct kbd_backlight *kbdbl_ctl;
  1531. static ssize_t __sony_nc_kbd_backlight_mode_set(u8 value)
  1532. {
  1533. int result;
  1534. if (value > 2)
  1535. return -EINVAL;
  1536. if (sony_call_snc_handle(kbdbl_ctl->handle,
  1537. (value << 0x10) | (kbdbl_ctl->base), &result))
  1538. return -EIO;
  1539. /* Try to turn the light on/off immediately */
  1540. if (value != 1)
  1541. sony_call_snc_handle(kbdbl_ctl->handle,
  1542. (value << 0x0f) | (kbdbl_ctl->base + 0x100),
  1543. &result);
  1544. kbdbl_ctl->mode = value;
  1545. return 0;
  1546. }
  1547. static ssize_t sony_nc_kbd_backlight_mode_store(struct device *dev,
  1548. struct device_attribute *attr,
  1549. const char *buffer, size_t count)
  1550. {
  1551. int ret = 0;
  1552. unsigned long value;
  1553. if (count > 31)
  1554. return -EINVAL;
  1555. if (kstrtoul(buffer, 10, &value))
  1556. return -EINVAL;
  1557. ret = __sony_nc_kbd_backlight_mode_set(value);
  1558. if (ret < 0)
  1559. return ret;
  1560. return count;
  1561. }
  1562. static ssize_t sony_nc_kbd_backlight_mode_show(struct device *dev,
  1563. struct device_attribute *attr, char *buffer)
  1564. {
  1565. ssize_t count = 0;
  1566. count = snprintf(buffer, PAGE_SIZE, "%d\n", kbdbl_ctl->mode);
  1567. return count;
  1568. }
  1569. static int __sony_nc_kbd_backlight_timeout_set(u8 value)
  1570. {
  1571. int result;
  1572. if (value > 3)
  1573. return -EINVAL;
  1574. if (sony_call_snc_handle(kbdbl_ctl->handle, (value << 0x10) |
  1575. (kbdbl_ctl->base + 0x200), &result))
  1576. return -EIO;
  1577. kbdbl_ctl->timeout = value;
  1578. return 0;
  1579. }
  1580. static ssize_t sony_nc_kbd_backlight_timeout_store(struct device *dev,
  1581. struct device_attribute *attr,
  1582. const char *buffer, size_t count)
  1583. {
  1584. int ret = 0;
  1585. unsigned long value;
  1586. if (count > 31)
  1587. return -EINVAL;
  1588. if (kstrtoul(buffer, 10, &value))
  1589. return -EINVAL;
  1590. ret = __sony_nc_kbd_backlight_timeout_set(value);
  1591. if (ret < 0)
  1592. return ret;
  1593. return count;
  1594. }
  1595. static ssize_t sony_nc_kbd_backlight_timeout_show(struct device *dev,
  1596. struct device_attribute *attr, char *buffer)
  1597. {
  1598. ssize_t count = 0;
  1599. count = snprintf(buffer, PAGE_SIZE, "%d\n", kbdbl_ctl->timeout);
  1600. return count;
  1601. }
  1602. static int sony_nc_kbd_backlight_setup(struct platform_device *pd,
  1603. unsigned int handle)
  1604. {
  1605. int result;
  1606. int probe_base = 0;
  1607. int ctl_base = 0;
  1608. int ret = 0;
  1609. if (kbdbl_ctl) {
  1610. pr_warn("handle 0x%.4x: keyboard backlight setup already done for 0x%.4x\n",
  1611. handle, kbdbl_ctl->handle);
  1612. return -EBUSY;
  1613. }
  1614. /* verify the kbd backlight presence, some of these handles are not used
  1615. * for keyboard backlight only
  1616. */
  1617. switch (handle) {
  1618. case 0x0153:
  1619. probe_base = 0x0;
  1620. ctl_base = 0x0;
  1621. break;
  1622. case 0x0137:
  1623. probe_base = 0x0B00;
  1624. ctl_base = 0x0C00;
  1625. break;
  1626. default:
  1627. probe_base = 0x0100;
  1628. ctl_base = 0x4000;
  1629. break;
  1630. }
  1631. ret = sony_call_snc_handle(handle, probe_base, &result);
  1632. if (ret)
  1633. return ret;
  1634. if ((handle == 0x0137 && !(result & 0x02)) ||
  1635. !(result & 0x01)) {
  1636. dprintk("no backlight keyboard found\n");
  1637. return 0;
  1638. }
  1639. kbdbl_ctl = kzalloc(sizeof(*kbdbl_ctl), GFP_KERNEL);
  1640. if (!kbdbl_ctl)
  1641. return -ENOMEM;
  1642. kbdbl_ctl->mode = kbd_backlight;
  1643. kbdbl_ctl->timeout = kbd_backlight_timeout;
  1644. kbdbl_ctl->handle = handle;
  1645. kbdbl_ctl->base = ctl_base;
  1646. /* Some models do not allow timeout control */
  1647. kbdbl_ctl->has_timeout = handle != 0x0153;
  1648. sysfs_attr_init(&kbdbl_ctl->mode_attr.attr);
  1649. kbdbl_ctl->mode_attr.attr.name = "kbd_backlight";
  1650. kbdbl_ctl->mode_attr.attr.mode = S_IRUGO | S_IWUSR;
  1651. kbdbl_ctl->mode_attr.show = sony_nc_kbd_backlight_mode_show;
  1652. kbdbl_ctl->mode_attr.store = sony_nc_kbd_backlight_mode_store;
  1653. ret = device_create_file(&pd->dev, &kbdbl_ctl->mode_attr);
  1654. if (ret)
  1655. goto outkzalloc;
  1656. __sony_nc_kbd_backlight_mode_set(kbdbl_ctl->mode);
  1657. if (kbdbl_ctl->has_timeout) {
  1658. sysfs_attr_init(&kbdbl_ctl->timeout_attr.attr);
  1659. kbdbl_ctl->timeout_attr.attr.name = "kbd_backlight_timeout";
  1660. kbdbl_ctl->timeout_attr.attr.mode = S_IRUGO | S_IWUSR;
  1661. kbdbl_ctl->timeout_attr.show =
  1662. sony_nc_kbd_backlight_timeout_show;
  1663. kbdbl_ctl->timeout_attr.store =
  1664. sony_nc_kbd_backlight_timeout_store;
  1665. ret = device_create_file(&pd->dev, &kbdbl_ctl->timeout_attr);
  1666. if (ret)
  1667. goto outmode;
  1668. __sony_nc_kbd_backlight_timeout_set(kbdbl_ctl->timeout);
  1669. }
  1670. return 0;
  1671. outmode:
  1672. device_remove_file(&pd->dev, &kbdbl_ctl->mode_attr);
  1673. outkzalloc:
  1674. kfree(kbdbl_ctl);
  1675. kbdbl_ctl = NULL;
  1676. return ret;
  1677. }
  1678. static void sony_nc_kbd_backlight_cleanup(struct platform_device *pd,
  1679. unsigned int handle)
  1680. {
  1681. if (kbdbl_ctl && handle == kbdbl_ctl->handle) {
  1682. device_remove_file(&pd->dev, &kbdbl_ctl->mode_attr);
  1683. if (kbdbl_ctl->has_timeout)
  1684. device_remove_file(&pd->dev, &kbdbl_ctl->timeout_attr);
  1685. kfree(kbdbl_ctl);
  1686. kbdbl_ctl = NULL;
  1687. }
  1688. }
  1689. struct battery_care_control {
  1690. struct device_attribute attrs[2];
  1691. unsigned int handle;
  1692. };
  1693. static struct battery_care_control *bcare_ctl;
  1694. static ssize_t sony_nc_battery_care_limit_store(struct device *dev,
  1695. struct device_attribute *attr,
  1696. const char *buffer, size_t count)
  1697. {
  1698. unsigned int result, cmd;
  1699. unsigned long value;
  1700. if (count > 31)
  1701. return -EINVAL;
  1702. if (kstrtoul(buffer, 10, &value))
  1703. return -EINVAL;
  1704. /* limit values (2 bits):
  1705. * 00 - none
  1706. * 01 - 80%
  1707. * 10 - 50%
  1708. * 11 - 100%
  1709. *
  1710. * bit 0: 0 disable BCL, 1 enable BCL
  1711. * bit 1: 1 tell to store the battery limit (see bits 6,7) too
  1712. * bits 2,3: reserved
  1713. * bits 4,5: store the limit into the EC
  1714. * bits 6,7: store the limit into the battery
  1715. */
  1716. cmd = 0;
  1717. if (value > 0) {
  1718. if (value <= 50)
  1719. cmd = 0x20;
  1720. else if (value <= 80)
  1721. cmd = 0x10;
  1722. else if (value <= 100)
  1723. cmd = 0x30;
  1724. else
  1725. return -EINVAL;
  1726. /*
  1727. * handle 0x0115 should allow storing on battery too;
  1728. * handle 0x0136 same as 0x0115 + health status;
  1729. * handle 0x013f, same as 0x0136 but no storing on the battery
  1730. */
  1731. if (bcare_ctl->handle != 0x013f)
  1732. cmd = cmd | (cmd << 2);
  1733. cmd = (cmd | 0x1) << 0x10;
  1734. }
  1735. if (sony_call_snc_handle(bcare_ctl->handle, cmd | 0x0100, &result))
  1736. return -EIO;
  1737. return count;
  1738. }
  1739. static ssize_t sony_nc_battery_care_limit_show(struct device *dev,
  1740. struct device_attribute *attr, char *buffer)
  1741. {
  1742. unsigned int result, status;
  1743. if (sony_call_snc_handle(bcare_ctl->handle, 0x0000, &result))
  1744. return -EIO;
  1745. status = (result & 0x01) ? ((result & 0x30) >> 0x04) : 0;
  1746. switch (status) {
  1747. case 1:
  1748. status = 80;
  1749. break;
  1750. case 2:
  1751. status = 50;
  1752. break;
  1753. case 3:
  1754. status = 100;
  1755. break;
  1756. default:
  1757. status = 0;
  1758. break;
  1759. }
  1760. return snprintf(buffer, PAGE_SIZE, "%d\n", status);
  1761. }
  1762. static ssize_t sony_nc_battery_care_health_show(struct device *dev,
  1763. struct device_attribute *attr, char *buffer)
  1764. {
  1765. ssize_t count = 0;
  1766. unsigned int health;
  1767. if (sony_call_snc_handle(bcare_ctl->handle, 0x0200, &health))
  1768. return -EIO;
  1769. count = snprintf(buffer, PAGE_SIZE, "%d\n", health & 0xff);
  1770. return count;
  1771. }
  1772. static int sony_nc_battery_care_setup(struct platform_device *pd,
  1773. unsigned int handle)
  1774. {
  1775. int ret = 0;
  1776. bcare_ctl = kzalloc(sizeof(struct battery_care_control), GFP_KERNEL);
  1777. if (!bcare_ctl)
  1778. return -ENOMEM;
  1779. bcare_ctl->handle = handle;
  1780. sysfs_attr_init(&bcare_ctl->attrs[0].attr);
  1781. bcare_ctl->attrs[0].attr.name = "battery_care_limiter";
  1782. bcare_ctl->attrs[0].attr.mode = S_IRUGO | S_IWUSR;
  1783. bcare_ctl->attrs[0].show = sony_nc_battery_care_limit_show;
  1784. bcare_ctl->attrs[0].store = sony_nc_battery_care_limit_store;
  1785. ret = device_create_file(&pd->dev, &bcare_ctl->attrs[0]);
  1786. if (ret)
  1787. goto outkzalloc;
  1788. /* 0x0115 is for models with no health reporting capability */
  1789. if (handle == 0x0115)
  1790. return 0;
  1791. sysfs_attr_init(&bcare_ctl->attrs[1].attr);
  1792. bcare_ctl->attrs[1].attr.name = "battery_care_health";
  1793. bcare_ctl->attrs[1].attr.mode = S_IRUGO;
  1794. bcare_ctl->attrs[1].show = sony_nc_battery_care_health_show;
  1795. ret = device_create_file(&pd->dev, &bcare_ctl->attrs[1]);
  1796. if (ret)
  1797. goto outlimiter;
  1798. return 0;
  1799. outlimiter:
  1800. device_remove_file(&pd->dev, &bcare_ctl->attrs[0]);
  1801. outkzalloc:
  1802. kfree(bcare_ctl);
  1803. bcare_ctl = NULL;
  1804. return ret;
  1805. }
  1806. static void sony_nc_battery_care_cleanup(struct platform_device *pd)
  1807. {
  1808. if (bcare_ctl) {
  1809. device_remove_file(&pd->dev, &bcare_ctl->attrs[0]);
  1810. if (bcare_ctl->handle != 0x0115)
  1811. device_remove_file(&pd->dev, &bcare_ctl->attrs[1]);
  1812. kfree(bcare_ctl);
  1813. bcare_ctl = NULL;
  1814. }
  1815. }
  1816. struct snc_thermal_ctrl {
  1817. unsigned int mode;
  1818. unsigned int profiles;
  1819. struct device_attribute mode_attr;
  1820. struct device_attribute profiles_attr;
  1821. };
  1822. static struct snc_thermal_ctrl *th_handle;
  1823. #define THM_PROFILE_MAX 3
  1824. static const char * const snc_thermal_profiles[] = {
  1825. "balanced",
  1826. "silent",
  1827. "performance"
  1828. };
  1829. static int sony_nc_thermal_mode_set(unsigned short mode)
  1830. {
  1831. unsigned int result;
  1832. /* the thermal profile seems to be a two bit bitmask:
  1833. * lsb -> silent
  1834. * msb -> performance
  1835. * no bit set is the normal operation and is always valid
  1836. * Some vaio models only have "balanced" and "performance"
  1837. */
  1838. if ((mode && !(th_handle->profiles & mode)) || mode >= THM_PROFILE_MAX)
  1839. return -EINVAL;
  1840. if (sony_call_snc_handle(0x0122, mode << 0x10 | 0x0200, &result))
  1841. return -EIO;
  1842. th_handle->mode = mode;
  1843. return 0;
  1844. }
  1845. static int sony_nc_thermal_mode_get(void)
  1846. {
  1847. unsigned int result;
  1848. if (sony_call_snc_handle(0x0122, 0x0100, &result))
  1849. return -EIO;
  1850. return result & 0xff;
  1851. }
  1852. static ssize_t sony_nc_thermal_profiles_show(struct device *dev,
  1853. struct device_attribute *attr, char *buffer)
  1854. {
  1855. short cnt;
  1856. size_t idx = 0;
  1857. for (cnt = 0; cnt < THM_PROFILE_MAX; cnt++) {
  1858. if (!cnt || (th_handle->profiles & cnt))
  1859. idx += snprintf(buffer + idx, PAGE_SIZE - idx, "%s ",
  1860. snc_thermal_profiles[cnt]);
  1861. }
  1862. idx += snprintf(buffer + idx, PAGE_SIZE - idx, "\n");
  1863. return idx;
  1864. }
  1865. static ssize_t sony_nc_thermal_mode_store(struct device *dev,
  1866. struct device_attribute *attr,
  1867. const char *buffer, size_t count)
  1868. {
  1869. unsigned short cmd;
  1870. size_t len = count;
  1871. if (count == 0)
  1872. return -EINVAL;
  1873. /* skip the newline if present */
  1874. if (buffer[len - 1] == '\n')
  1875. len--;
  1876. for (cmd = 0; cmd < THM_PROFILE_MAX; cmd++)
  1877. if (strncmp(buffer, snc_thermal_profiles[cmd], len) == 0)
  1878. break;
  1879. if (sony_nc_thermal_mode_set(cmd))
  1880. return -EIO;
  1881. return count;
  1882. }
  1883. static ssize_t sony_nc_thermal_mode_show(struct device *dev,
  1884. struct device_attribute *attr, char *buffer)
  1885. {
  1886. ssize_t count = 0;
  1887. int mode = sony_nc_thermal_mode_get();
  1888. if (mode < 0)
  1889. return mode;
  1890. count = snprintf(buffer, PAGE_SIZE, "%s\n", snc_thermal_profiles[mode]);
  1891. return count;
  1892. }
  1893. static int sony_nc_thermal_setup(struct platform_device *pd)
  1894. {
  1895. int ret = 0;
  1896. th_handle = kzalloc(sizeof(struct snc_thermal_ctrl), GFP_KERNEL);
  1897. if (!th_handle)
  1898. return -ENOMEM;
  1899. ret = sony_call_snc_handle(0x0122, 0x0000, &th_handle->profiles);
  1900. if (ret) {
  1901. pr_warn("couldn't to read the thermal profiles\n");
  1902. goto outkzalloc;
  1903. }
  1904. ret = sony_nc_thermal_mode_get();
  1905. if (ret < 0) {
  1906. pr_warn("couldn't to read the current thermal profile");
  1907. goto outkzalloc;
  1908. }
  1909. th_handle->mode = ret;
  1910. sysfs_attr_init(&th_handle->profiles_attr.attr);
  1911. th_handle->profiles_attr.attr.name = "thermal_profiles";
  1912. th_handle->profiles_attr.attr.mode = S_IRUGO;
  1913. th_handle->profiles_attr.show = sony_nc_thermal_profiles_show;
  1914. sysfs_attr_init(&th_handle->mode_attr.attr);
  1915. th_handle->mode_attr.attr.name = "thermal_control";
  1916. th_handle->mode_attr.attr.mode = S_IRUGO | S_IWUSR;
  1917. th_handle->mode_attr.show = sony_nc_thermal_mode_show;
  1918. th_handle->mode_attr.store = sony_nc_thermal_mode_store;
  1919. ret = device_create_file(&pd->dev, &th_handle->profiles_attr);
  1920. if (ret)
  1921. goto outkzalloc;
  1922. ret = device_create_file(&pd->dev, &th_handle->mode_attr);
  1923. if (ret)
  1924. goto outprofiles;
  1925. return 0;
  1926. outprofiles:
  1927. device_remove_file(&pd->dev, &th_handle->profiles_attr);
  1928. outkzalloc:
  1929. kfree(th_handle);
  1930. th_handle = NULL;
  1931. return ret;
  1932. }
  1933. static void sony_nc_thermal_cleanup(struct platform_device *pd)
  1934. {
  1935. if (th_handle) {
  1936. device_remove_file(&pd->dev, &th_handle->profiles_attr);
  1937. device_remove_file(&pd->dev, &th_handle->mode_attr);
  1938. kfree(th_handle);
  1939. th_handle = NULL;
  1940. }
  1941. }
  1942. #ifdef CONFIG_PM_SLEEP
  1943. static void sony_nc_thermal_resume(void)
  1944. {
  1945. unsigned int status = sony_nc_thermal_mode_get();
  1946. if (status != th_handle->mode)
  1947. sony_nc_thermal_mode_set(th_handle->mode);
  1948. }
  1949. #endif
  1950. /* resume on LID open */
  1951. #define LID_RESUME_S5 0
  1952. #define LID_RESUME_S4 1
  1953. #define LID_RESUME_S3 2
  1954. #define LID_RESUME_MAX 3
  1955. struct snc_lid_resume_control {
  1956. struct device_attribute attrs[LID_RESUME_MAX];
  1957. unsigned int status;
  1958. int handle;
  1959. };
  1960. static struct snc_lid_resume_control *lid_ctl;
  1961. static ssize_t sony_nc_lid_resume_store(struct device *dev,
  1962. struct device_attribute *attr,
  1963. const char *buffer, size_t count)
  1964. {
  1965. unsigned int result;
  1966. unsigned long value;
  1967. unsigned int pos = LID_RESUME_S5;
  1968. if (count > 31)
  1969. return -EINVAL;
  1970. if (kstrtoul(buffer, 10, &value) || value > 1)
  1971. return -EINVAL;
  1972. /* the value we have to write to SNC is a bitmask:
  1973. * +--------------+
  1974. * | S3 | S4 | S5 |
  1975. * +--------------+
  1976. * 2 1 0
  1977. */
  1978. while (pos < LID_RESUME_MAX) {
  1979. if (&lid_ctl->attrs[pos].attr == &attr->attr)
  1980. break;
  1981. pos++;
  1982. }
  1983. if (pos == LID_RESUME_MAX)
  1984. return -EINVAL;
  1985. if (value)
  1986. value = lid_ctl->status | (1 << pos);
  1987. else
  1988. value = lid_ctl->status & ~(1 << pos);
  1989. if (sony_call_snc_handle(lid_ctl->handle, value << 0x10 | 0x0100,
  1990. &result))
  1991. return -EIO;
  1992. lid_ctl->status = value;
  1993. return count;
  1994. }
  1995. static ssize_t sony_nc_lid_resume_show(struct device *dev,
  1996. struct device_attribute *attr,
  1997. char *buffer)
  1998. {
  1999. unsigned int pos = LID_RESUME_S5;
  2000. while (pos < LID_RESUME_MAX) {
  2001. if (&lid_ctl->attrs[pos].attr == &attr->attr)
  2002. return snprintf(buffer, PAGE_SIZE, "%d\n",
  2003. (lid_ctl->status >> pos) & 0x01);
  2004. pos++;
  2005. }
  2006. return -EINVAL;
  2007. }
  2008. static int sony_nc_lid_resume_setup(struct platform_device *pd,
  2009. unsigned int handle)
  2010. {
  2011. unsigned int result;
  2012. int i;
  2013. if (sony_call_snc_handle(handle, 0x0000, &result))
  2014. return -EIO;
  2015. lid_ctl = kzalloc(sizeof(struct snc_lid_resume_control), GFP_KERNEL);
  2016. if (!lid_ctl)
  2017. return -ENOMEM;
  2018. lid_ctl->status = result & 0x7;
  2019. lid_ctl->handle = handle;
  2020. sysfs_attr_init(&lid_ctl->attrs[0].attr);
  2021. lid_ctl->attrs[LID_RESUME_S5].attr.name = "lid_resume_S5";
  2022. lid_ctl->attrs[LID_RESUME_S5].attr.mode = S_IRUGO | S_IWUSR;
  2023. lid_ctl->attrs[LID_RESUME_S5].show = sony_nc_lid_resume_show;
  2024. lid_ctl->attrs[LID_RESUME_S5].store = sony_nc_lid_resume_store;
  2025. if (handle == 0x0119) {
  2026. sysfs_attr_init(&lid_ctl->attrs[1].attr);
  2027. lid_ctl->attrs[LID_RESUME_S4].attr.name = "lid_resume_S4";
  2028. lid_ctl->attrs[LID_RESUME_S4].attr.mode = S_IRUGO | S_IWUSR;
  2029. lid_ctl->attrs[LID_RESUME_S4].show = sony_nc_lid_resume_show;
  2030. lid_ctl->attrs[LID_RESUME_S4].store = sony_nc_lid_resume_store;
  2031. sysfs_attr_init(&lid_ctl->attrs[2].attr);
  2032. lid_ctl->attrs[LID_RESUME_S3].attr.name = "lid_resume_S3";
  2033. lid_ctl->attrs[LID_RESUME_S3].attr.mode = S_IRUGO | S_IWUSR;
  2034. lid_ctl->attrs[LID_RESUME_S3].show = sony_nc_lid_resume_show;
  2035. lid_ctl->attrs[LID_RESUME_S3].store = sony_nc_lid_resume_store;
  2036. }
  2037. for (i = 0; i < LID_RESUME_MAX &&
  2038. lid_ctl->attrs[i].attr.name; i++) {
  2039. result = device_create_file(&pd->dev, &lid_ctl->attrs[i]);
  2040. if (result)
  2041. goto liderror;
  2042. }
  2043. return 0;
  2044. liderror:
  2045. for (i--; i >= 0; i--)
  2046. device_remove_file(&pd->dev, &lid_ctl->attrs[i]);
  2047. kfree(lid_ctl);
  2048. lid_ctl = NULL;
  2049. return result;
  2050. }
  2051. static void sony_nc_lid_resume_cleanup(struct platform_device *pd)
  2052. {
  2053. int i;
  2054. if (lid_ctl) {
  2055. for (i = 0; i < LID_RESUME_MAX; i++) {
  2056. if (!lid_ctl->attrs[i].attr.name)
  2057. break;
  2058. device_remove_file(&pd->dev, &lid_ctl->attrs[i]);
  2059. }
  2060. kfree(lid_ctl);
  2061. lid_ctl = NULL;
  2062. }
  2063. }
  2064. /* GFX Switch position */
  2065. enum gfx_switch {
  2066. SPEED,
  2067. STAMINA,
  2068. AUTO
  2069. };
  2070. struct snc_gfx_switch_control {
  2071. struct device_attribute attr;
  2072. unsigned int handle;
  2073. };
  2074. static struct snc_gfx_switch_control *gfxs_ctl;
  2075. /* returns 0 for speed, 1 for stamina */
  2076. static int __sony_nc_gfx_switch_status_get(void)
  2077. {
  2078. unsigned int result;
  2079. if (sony_call_snc_handle(gfxs_ctl->handle,
  2080. gfxs_ctl->handle == 0x015B ? 0x0000 : 0x0100,
  2081. &result))
  2082. return -EIO;
  2083. switch (gfxs_ctl->handle) {
  2084. case 0x0146:
  2085. /* 1: discrete GFX (speed)
  2086. * 0: integrated GFX (stamina)
  2087. */
  2088. return result & 0x1 ? SPEED : STAMINA;
  2089. break;
  2090. case 0x015B:
  2091. /* 0: discrete GFX (speed)
  2092. * 1: integrated GFX (stamina)
  2093. */
  2094. return result & 0x1 ? STAMINA : SPEED;
  2095. break;
  2096. case 0x0128:
  2097. /* it's a more elaborated bitmask, for now:
  2098. * 2: integrated GFX (stamina)
  2099. * 0: discrete GFX (speed)
  2100. */
  2101. dprintk("GFX Status: 0x%x\n", result);
  2102. return result & 0x80 ? AUTO :
  2103. result & 0x02 ? STAMINA : SPEED;
  2104. break;
  2105. }
  2106. return -EINVAL;
  2107. }
  2108. static ssize_t sony_nc_gfx_switch_status_show(struct device *dev,
  2109. struct device_attribute *attr,
  2110. char *buffer)
  2111. {
  2112. int pos = __sony_nc_gfx_switch_status_get();
  2113. if (pos < 0)
  2114. return pos;
  2115. return snprintf(buffer, PAGE_SIZE, "%s\n",
  2116. pos == SPEED ? "speed" :
  2117. pos == STAMINA ? "stamina" :
  2118. pos == AUTO ? "auto" : "unknown");
  2119. }
  2120. static int sony_nc_gfx_switch_setup(struct platform_device *pd,
  2121. unsigned int handle)
  2122. {
  2123. unsigned int result;
  2124. gfxs_ctl = kzalloc(sizeof(struct snc_gfx_switch_control), GFP_KERNEL);
  2125. if (!gfxs_ctl)
  2126. return -ENOMEM;
  2127. gfxs_ctl->handle = handle;
  2128. sysfs_attr_init(&gfxs_ctl->attr.attr);
  2129. gfxs_ctl->attr.attr.name = "gfx_switch_status";
  2130. gfxs_ctl->attr.attr.mode = S_IRUGO;
  2131. gfxs_ctl->attr.show = sony_nc_gfx_switch_status_show;
  2132. result = device_create_file(&pd->dev, &gfxs_ctl->attr);
  2133. if (result)
  2134. goto gfxerror;
  2135. return 0;
  2136. gfxerror:
  2137. kfree(gfxs_ctl);
  2138. gfxs_ctl = NULL;
  2139. return result;
  2140. }
  2141. static void sony_nc_gfx_switch_cleanup(struct platform_device *pd)
  2142. {
  2143. if (gfxs_ctl) {
  2144. device_remove_file(&pd->dev, &gfxs_ctl->attr);
  2145. kfree(gfxs_ctl);
  2146. gfxs_ctl = NULL;
  2147. }
  2148. }
  2149. /* High speed charging function */
  2150. static struct device_attribute *hsc_handle;
  2151. static ssize_t sony_nc_highspeed_charging_store(struct device *dev,
  2152. struct device_attribute *attr,
  2153. const char *buffer, size_t count)
  2154. {
  2155. unsigned int result;
  2156. unsigned long value;
  2157. if (count > 31)
  2158. return -EINVAL;
  2159. if (kstrtoul(buffer, 10, &value) || value > 1)
  2160. return -EINVAL;
  2161. if (sony_call_snc_handle(0x0131, value << 0x10 | 0x0200, &result))
  2162. return -EIO;
  2163. return count;
  2164. }
  2165. static ssize_t sony_nc_highspeed_charging_show(struct device *dev,
  2166. struct device_attribute *attr, char *buffer)
  2167. {
  2168. unsigned int result;
  2169. if (sony_call_snc_handle(0x0131, 0x0100, &result))
  2170. return -EIO;
  2171. return snprintf(buffer, PAGE_SIZE, "%d\n", result & 0x01);
  2172. }
  2173. static int sony_nc_highspeed_charging_setup(struct platform_device *pd)
  2174. {
  2175. unsigned int result;
  2176. if (sony_call_snc_handle(0x0131, 0x0000, &result) || !(result & 0x01)) {
  2177. /* some models advertise the handle but have no implementation
  2178. * for it
  2179. */
  2180. pr_info("No High Speed Charging capability found\n");
  2181. return 0;
  2182. }
  2183. hsc_handle = kzalloc(sizeof(struct device_attribute), GFP_KERNEL);
  2184. if (!hsc_handle)
  2185. return -ENOMEM;
  2186. sysfs_attr_init(&hsc_handle->attr);
  2187. hsc_handle->attr.name = "battery_highspeed_charging";
  2188. hsc_handle->attr.mode = S_IRUGO | S_IWUSR;
  2189. hsc_handle->show = sony_nc_highspeed_charging_show;
  2190. hsc_handle->store = sony_nc_highspeed_charging_store;
  2191. result = device_create_file(&pd->dev, hsc_handle);
  2192. if (result) {
  2193. kfree(hsc_handle);
  2194. hsc_handle = NULL;
  2195. return result;
  2196. }
  2197. return 0;
  2198. }
  2199. static void sony_nc_highspeed_charging_cleanup(struct platform_device *pd)
  2200. {
  2201. if (hsc_handle) {
  2202. device_remove_file(&pd->dev, hsc_handle);
  2203. kfree(hsc_handle);
  2204. hsc_handle = NULL;
  2205. }
  2206. }
  2207. /* low battery function */
  2208. static struct device_attribute *lowbatt_handle;
  2209. static ssize_t sony_nc_lowbatt_store(struct device *dev,
  2210. struct device_attribute *attr,
  2211. const char *buffer, size_t count)
  2212. {
  2213. unsigned int result;
  2214. unsigned long value;
  2215. if (count > 31)
  2216. return -EINVAL;
  2217. if (kstrtoul(buffer, 10, &value) || value > 1)
  2218. return -EINVAL;
  2219. if (sony_call_snc_handle(0x0121, value << 8, &result))
  2220. return -EIO;
  2221. return count;
  2222. }
  2223. static ssize_t sony_nc_lowbatt_show(struct device *dev,
  2224. struct device_attribute *attr, char *buffer)
  2225. {
  2226. unsigned int result;
  2227. if (sony_call_snc_handle(0x0121, 0x0200, &result))
  2228. return -EIO;
  2229. return snprintf(buffer, PAGE_SIZE, "%d\n", result & 1);
  2230. }
  2231. static int sony_nc_lowbatt_setup(struct platform_device *pd)
  2232. {
  2233. unsigned int result;
  2234. lowbatt_handle = kzalloc(sizeof(struct device_attribute), GFP_KERNEL);
  2235. if (!lowbatt_handle)
  2236. return -ENOMEM;
  2237. sysfs_attr_init(&lowbatt_handle->attr);
  2238. lowbatt_handle->attr.name = "lowbatt_hibernate";
  2239. lowbatt_handle->attr.mode = S_IRUGO | S_IWUSR;
  2240. lowbatt_handle->show = sony_nc_lowbatt_show;
  2241. lowbatt_handle->store = sony_nc_lowbatt_store;
  2242. result = device_create_file(&pd->dev, lowbatt_handle);
  2243. if (result) {
  2244. kfree(lowbatt_handle);
  2245. lowbatt_handle = NULL;
  2246. return result;
  2247. }
  2248. return 0;
  2249. }
  2250. static void sony_nc_lowbatt_cleanup(struct platform_device *pd)
  2251. {
  2252. if (lowbatt_handle) {
  2253. device_remove_file(&pd->dev, lowbatt_handle);
  2254. kfree(lowbatt_handle);
  2255. lowbatt_handle = NULL;
  2256. }
  2257. }
  2258. /* fan speed function */
  2259. static struct device_attribute *fan_handle, *hsf_handle;
  2260. static ssize_t sony_nc_hsfan_store(struct device *dev,
  2261. struct device_attribute *attr,
  2262. const char *buffer, size_t count)
  2263. {
  2264. unsigned int result;
  2265. unsigned long value;
  2266. if (count > 31)
  2267. return -EINVAL;
  2268. if (kstrtoul(buffer, 10, &value) || value > 1)
  2269. return -EINVAL;
  2270. if (sony_call_snc_handle(0x0149, value << 0x10 | 0x0200, &result))
  2271. return -EIO;
  2272. return count;
  2273. }
  2274. static ssize_t sony_nc_hsfan_show(struct device *dev,
  2275. struct device_attribute *attr, char *buffer)
  2276. {
  2277. unsigned int result;
  2278. if (sony_call_snc_handle(0x0149, 0x0100, &result))
  2279. return -EIO;
  2280. return snprintf(buffer, PAGE_SIZE, "%d\n", result & 0x01);
  2281. }
  2282. static ssize_t sony_nc_fanspeed_show(struct device *dev,
  2283. struct device_attribute *attr, char *buffer)
  2284. {
  2285. unsigned int result;
  2286. if (sony_call_snc_handle(0x0149, 0x0300, &result))
  2287. return -EIO;
  2288. return snprintf(buffer, PAGE_SIZE, "%d\n", result & 0xff);
  2289. }
  2290. static int sony_nc_fanspeed_setup(struct platform_device *pd)
  2291. {
  2292. unsigned int result;
  2293. fan_handle = kzalloc(sizeof(struct device_attribute), GFP_KERNEL);
  2294. if (!fan_handle)
  2295. return -ENOMEM;
  2296. hsf_handle = kzalloc(sizeof(struct device_attribute), GFP_KERNEL);
  2297. if (!hsf_handle) {
  2298. result = -ENOMEM;
  2299. goto out_hsf_handle_alloc;
  2300. }
  2301. sysfs_attr_init(&fan_handle->attr);
  2302. fan_handle->attr.name = "fanspeed";
  2303. fan_handle->attr.mode = S_IRUGO;
  2304. fan_handle->show = sony_nc_fanspeed_show;
  2305. fan_handle->store = NULL;
  2306. sysfs_attr_init(&hsf_handle->attr);
  2307. hsf_handle->attr.name = "fan_forced";
  2308. hsf_handle->attr.mode = S_IRUGO | S_IWUSR;
  2309. hsf_handle->show = sony_nc_hsfan_show;
  2310. hsf_handle->store = sony_nc_hsfan_store;
  2311. result = device_create_file(&pd->dev, fan_handle);
  2312. if (result)
  2313. goto out_fan_handle;
  2314. result = device_create_file(&pd->dev, hsf_handle);
  2315. if (result)
  2316. goto out_hsf_handle;
  2317. return 0;
  2318. out_hsf_handle:
  2319. device_remove_file(&pd->dev, fan_handle);
  2320. out_fan_handle:
  2321. kfree(hsf_handle);
  2322. hsf_handle = NULL;
  2323. out_hsf_handle_alloc:
  2324. kfree(fan_handle);
  2325. fan_handle = NULL;
  2326. return result;
  2327. }
  2328. static void sony_nc_fanspeed_cleanup(struct platform_device *pd)
  2329. {
  2330. if (fan_handle) {
  2331. device_remove_file(&pd->dev, fan_handle);
  2332. kfree(fan_handle);
  2333. fan_handle = NULL;
  2334. }
  2335. if (hsf_handle) {
  2336. device_remove_file(&pd->dev, hsf_handle);
  2337. kfree(hsf_handle);
  2338. hsf_handle = NULL;
  2339. }
  2340. }
  2341. /* USB charge function */
  2342. static struct device_attribute *uc_handle;
  2343. static ssize_t sony_nc_usb_charge_store(struct device *dev,
  2344. struct device_attribute *attr,
  2345. const char *buffer, size_t count)
  2346. {
  2347. unsigned int result;
  2348. unsigned long value;
  2349. if (count > 31)
  2350. return -EINVAL;
  2351. if (kstrtoul(buffer, 10, &value) || value > 1)
  2352. return -EINVAL;
  2353. if (sony_call_snc_handle(0x0155, value << 0x10 | 0x0100, &result))
  2354. return -EIO;
  2355. return count;
  2356. }
  2357. static ssize_t sony_nc_usb_charge_show(struct device *dev,
  2358. struct device_attribute *attr, char *buffer)
  2359. {
  2360. unsigned int result;
  2361. if (sony_call_snc_handle(0x0155, 0x0000, &result))
  2362. return -EIO;
  2363. return snprintf(buffer, PAGE_SIZE, "%d\n", result & 0x01);
  2364. }
  2365. static int sony_nc_usb_charge_setup(struct platform_device *pd)
  2366. {
  2367. unsigned int result;
  2368. if (sony_call_snc_handle(0x0155, 0x0000, &result) || !(result & 0x01)) {
  2369. /* some models advertise the handle but have no implementation
  2370. * for it
  2371. */
  2372. pr_info("No USB Charge capability found\n");
  2373. return 0;
  2374. }
  2375. uc_handle = kzalloc(sizeof(struct device_attribute), GFP_KERNEL);
  2376. if (!uc_handle)
  2377. return -ENOMEM;
  2378. sysfs_attr_init(&uc_handle->attr);
  2379. uc_handle->attr.name = "usb_charge";
  2380. uc_handle->attr.mode = S_IRUGO | S_IWUSR;
  2381. uc_handle->show = sony_nc_usb_charge_show;
  2382. uc_handle->store = sony_nc_usb_charge_store;
  2383. result = device_create_file(&pd->dev, uc_handle);
  2384. if (result) {
  2385. kfree(uc_handle);
  2386. uc_handle = NULL;
  2387. return result;
  2388. }
  2389. return 0;
  2390. }
  2391. static void sony_nc_usb_charge_cleanup(struct platform_device *pd)
  2392. {
  2393. if (uc_handle) {
  2394. device_remove_file(&pd->dev, uc_handle);
  2395. kfree(uc_handle);
  2396. uc_handle = NULL;
  2397. }
  2398. }
  2399. /* Panel ID function */
  2400. static struct device_attribute *panel_handle;
  2401. static ssize_t sony_nc_panelid_show(struct device *dev,
  2402. struct device_attribute *attr, char *buffer)
  2403. {
  2404. unsigned int result;
  2405. if (sony_call_snc_handle(0x011D, 0x0000, &result))
  2406. return -EIO;
  2407. return snprintf(buffer, PAGE_SIZE, "%d\n", result);
  2408. }
  2409. static int sony_nc_panelid_setup(struct platform_device *pd)
  2410. {
  2411. unsigned int result;
  2412. panel_handle = kzalloc(sizeof(struct device_attribute), GFP_KERNEL);
  2413. if (!panel_handle)
  2414. return -ENOMEM;
  2415. sysfs_attr_init(&panel_handle->attr);
  2416. panel_handle->attr.name = "panel_id";
  2417. panel_handle->attr.mode = S_IRUGO;
  2418. panel_handle->show = sony_nc_panelid_show;
  2419. panel_handle->store = NULL;
  2420. result = device_create_file(&pd->dev, panel_handle);
  2421. if (result) {
  2422. kfree(panel_handle);
  2423. panel_handle = NULL;
  2424. return result;
  2425. }
  2426. return 0;
  2427. }
  2428. static void sony_nc_panelid_cleanup(struct platform_device *pd)
  2429. {
  2430. if (panel_handle) {
  2431. device_remove_file(&pd->dev, panel_handle);
  2432. kfree(panel_handle);
  2433. panel_handle = NULL;
  2434. }
  2435. }
  2436. /* smart connect function */
  2437. static struct device_attribute *sc_handle;
  2438. static ssize_t sony_nc_smart_conn_store(struct device *dev,
  2439. struct device_attribute *attr,
  2440. const char *buffer, size_t count)
  2441. {
  2442. unsigned int result;
  2443. unsigned long value;
  2444. if (count > 31)
  2445. return -EINVAL;
  2446. if (kstrtoul(buffer, 10, &value) || value > 1)
  2447. return -EINVAL;
  2448. if (sony_call_snc_handle(0x0168, value << 0x10, &result))
  2449. return -EIO;
  2450. return count;
  2451. }
  2452. static int sony_nc_smart_conn_setup(struct platform_device *pd)
  2453. {
  2454. unsigned int result;
  2455. sc_handle = kzalloc(sizeof(struct device_attribute), GFP_KERNEL);
  2456. if (!sc_handle)
  2457. return -ENOMEM;
  2458. sysfs_attr_init(&sc_handle->attr);
  2459. sc_handle->attr.name = "smart_connect";
  2460. sc_handle->attr.mode = S_IWUSR;
  2461. sc_handle->show = NULL;
  2462. sc_handle->store = sony_nc_smart_conn_store;
  2463. result = device_create_file(&pd->dev, sc_handle);
  2464. if (result) {
  2465. kfree(sc_handle);
  2466. sc_handle = NULL;
  2467. return result;
  2468. }
  2469. return 0;
  2470. }
  2471. static void sony_nc_smart_conn_cleanup(struct platform_device *pd)
  2472. {
  2473. if (sc_handle) {
  2474. device_remove_file(&pd->dev, sc_handle);
  2475. kfree(sc_handle);
  2476. sc_handle = NULL;
  2477. }
  2478. }
  2479. /* Touchpad enable/disable */
  2480. struct touchpad_control {
  2481. struct device_attribute attr;
  2482. int handle;
  2483. };
  2484. static struct touchpad_control *tp_ctl;
  2485. static ssize_t sony_nc_touchpad_store(struct device *dev,
  2486. struct device_attribute *attr, const char *buffer, size_t count)
  2487. {
  2488. unsigned int result;
  2489. unsigned long value;
  2490. if (count > 31)
  2491. return -EINVAL;
  2492. if (kstrtoul(buffer, 10, &value) || value > 1)
  2493. return -EINVAL;
  2494. /* sysfs: 0 disabled, 1 enabled
  2495. * EC: 0 enabled, 1 disabled
  2496. */
  2497. if (sony_call_snc_handle(tp_ctl->handle,
  2498. (!value << 0x10) | 0x100, &result))
  2499. return -EIO;
  2500. return count;
  2501. }
  2502. static ssize_t sony_nc_touchpad_show(struct device *dev,
  2503. struct device_attribute *attr, char *buffer)
  2504. {
  2505. unsigned int result;
  2506. if (sony_call_snc_handle(tp_ctl->handle, 0x000, &result))
  2507. return -EINVAL;
  2508. return snprintf(buffer, PAGE_SIZE, "%d\n", !(result & 0x01));
  2509. }
  2510. static int sony_nc_touchpad_setup(struct platform_device *pd,
  2511. unsigned int handle)
  2512. {
  2513. int ret = 0;
  2514. tp_ctl = kzalloc(sizeof(struct touchpad_control), GFP_KERNEL);
  2515. if (!tp_ctl)
  2516. return -ENOMEM;
  2517. tp_ctl->handle = handle;
  2518. sysfs_attr_init(&tp_ctl->attr.attr);
  2519. tp_ctl->attr.attr.name = "touchpad";
  2520. tp_ctl->attr.attr.mode = S_IRUGO | S_IWUSR;
  2521. tp_ctl->attr.show = sony_nc_touchpad_show;
  2522. tp_ctl->attr.store = sony_nc_touchpad_store;
  2523. ret = device_create_file(&pd->dev, &tp_ctl->attr);
  2524. if (ret) {
  2525. kfree(tp_ctl);
  2526. tp_ctl = NULL;
  2527. }
  2528. return ret;
  2529. }
  2530. static void sony_nc_touchpad_cleanup(struct platform_device *pd)
  2531. {
  2532. if (tp_ctl) {
  2533. device_remove_file(&pd->dev, &tp_ctl->attr);
  2534. kfree(tp_ctl);
  2535. tp_ctl = NULL;
  2536. }
  2537. }
  2538. static void sony_nc_backlight_ng_read_limits(int handle,
  2539. struct sony_backlight_props *props)
  2540. {
  2541. u64 offset;
  2542. int i;
  2543. int lvl_table_len = 0;
  2544. u8 min = 0xff, max = 0x00;
  2545. unsigned char buffer[32] = { 0 };
  2546. props->handle = handle;
  2547. props->offset = 0;
  2548. props->maxlvl = 0xff;
  2549. offset = sony_find_snc_handle(handle);
  2550. /* try to read the boundaries from ACPI tables, if we fail the above
  2551. * defaults should be reasonable
  2552. */
  2553. i = sony_nc_buffer_call(sony_nc_acpi_handle, "SN06", &offset, buffer,
  2554. 32);
  2555. if (i < 0)
  2556. return;
  2557. switch (handle) {
  2558. case 0x012f:
  2559. case 0x0137:
  2560. lvl_table_len = 9;
  2561. break;
  2562. case 0x143:
  2563. case 0x14b:
  2564. case 0x14c:
  2565. lvl_table_len = 16;
  2566. break;
  2567. }
  2568. /* the buffer lists brightness levels available, brightness levels are
  2569. * from position 0 to 8 in the array, other values are used by ALS
  2570. * control.
  2571. */
  2572. for (i = 0; i < lvl_table_len && i < ARRAY_SIZE(buffer); i++) {
  2573. dprintk("Brightness level: %d\n", buffer[i]);
  2574. if (!buffer[i])
  2575. break;
  2576. if (buffer[i] > max)
  2577. max = buffer[i];
  2578. if (buffer[i] < min)
  2579. min = buffer[i];
  2580. }
  2581. props->offset = min;
  2582. props->maxlvl = max;
  2583. dprintk("Brightness levels: min=%d max=%d\n", props->offset,
  2584. props->maxlvl);
  2585. }
  2586. static void sony_nc_backlight_setup(void)
  2587. {
  2588. int max_brightness = 0;
  2589. const struct backlight_ops *ops = NULL;
  2590. struct backlight_properties props;
  2591. if (sony_find_snc_handle(0x12f) >= 0) {
  2592. ops = &sony_backlight_ng_ops;
  2593. sony_bl_props.cmd_base = 0x0100;
  2594. sony_nc_backlight_ng_read_limits(0x12f, &sony_bl_props);
  2595. max_brightness = sony_bl_props.maxlvl - sony_bl_props.offset;
  2596. } else if (sony_find_snc_handle(0x137) >= 0) {
  2597. ops = &sony_backlight_ng_ops;
  2598. sony_bl_props.cmd_base = 0x0100;
  2599. sony_nc_backlight_ng_read_limits(0x137, &sony_bl_props);
  2600. max_brightness = sony_bl_props.maxlvl - sony_bl_props.offset;
  2601. } else if (sony_find_snc_handle(0x143) >= 0) {
  2602. ops = &sony_backlight_ng_ops;
  2603. sony_bl_props.cmd_base = 0x3000;
  2604. sony_nc_backlight_ng_read_limits(0x143, &sony_bl_props);
  2605. max_brightness = sony_bl_props.maxlvl - sony_bl_props.offset;
  2606. } else if (sony_find_snc_handle(0x14b) >= 0) {
  2607. ops = &sony_backlight_ng_ops;
  2608. sony_bl_props.cmd_base = 0x3000;
  2609. sony_nc_backlight_ng_read_limits(0x14b, &sony_bl_props);
  2610. max_brightness = sony_bl_props.maxlvl - sony_bl_props.offset;
  2611. } else if (sony_find_snc_handle(0x14c) >= 0) {
  2612. ops = &sony_backlight_ng_ops;
  2613. sony_bl_props.cmd_base = 0x3000;
  2614. sony_nc_backlight_ng_read_limits(0x14c, &sony_bl_props);
  2615. max_brightness = sony_bl_props.maxlvl - sony_bl_props.offset;
  2616. } else if (acpi_has_method(sony_nc_acpi_handle, "GBRT")) {
  2617. ops = &sony_backlight_ops;
  2618. max_brightness = SONY_MAX_BRIGHTNESS - 1;
  2619. } else
  2620. return;
  2621. memset(&props, 0, sizeof(struct backlight_properties));
  2622. props.type = BACKLIGHT_PLATFORM;
  2623. props.max_brightness = max_brightness;
  2624. sony_bl_props.dev = backlight_device_register("sony", NULL,
  2625. &sony_bl_props,
  2626. ops, &props);
  2627. if (IS_ERR(sony_bl_props.dev)) {
  2628. pr_warn("unable to register backlight device\n");
  2629. sony_bl_props.dev = NULL;
  2630. } else
  2631. sony_bl_props.dev->props.brightness =
  2632. ops->get_brightness(sony_bl_props.dev);
  2633. }
  2634. static void sony_nc_backlight_cleanup(void)
  2635. {
  2636. backlight_device_unregister(sony_bl_props.dev);
  2637. }
  2638. static int sony_nc_add(struct acpi_device *device)
  2639. {
  2640. acpi_status status;
  2641. int result = 0;
  2642. struct sony_nc_value *item;
  2643. sony_nc_acpi_device = device;
  2644. strcpy(acpi_device_class(device), "sony/hotkey");
  2645. sony_nc_acpi_handle = device->handle;
  2646. /* read device status */
  2647. result = acpi_bus_get_status(device);
  2648. /* bail IFF the above call was successful and the device is not present */
  2649. if (!result && !device->status.present) {
  2650. dprintk("Device not present\n");
  2651. result = -ENODEV;
  2652. goto outwalk;
  2653. }
  2654. result = sony_pf_add();
  2655. if (result)
  2656. goto outpresent;
  2657. if (debug) {
  2658. status = acpi_walk_namespace(ACPI_TYPE_METHOD,
  2659. sony_nc_acpi_handle, 1, sony_walk_callback,
  2660. NULL, NULL, NULL);
  2661. if (ACPI_FAILURE(status)) {
  2662. pr_warn("unable to walk acpi resources\n");
  2663. result = -ENODEV;
  2664. goto outpresent;
  2665. }
  2666. }
  2667. result = sony_laptop_setup_input(device);
  2668. if (result) {
  2669. pr_err("Unable to create input devices\n");
  2670. goto outplatform;
  2671. }
  2672. if (acpi_has_method(sony_nc_acpi_handle, "ECON")) {
  2673. int arg = 1;
  2674. if (sony_nc_int_call(sony_nc_acpi_handle, "ECON", &arg, NULL))
  2675. dprintk("ECON Method failed\n");
  2676. }
  2677. if (acpi_has_method(sony_nc_acpi_handle, "SN00")) {
  2678. dprintk("Doing SNC setup\n");
  2679. /* retrieve the available handles */
  2680. result = sony_nc_handles_setup(sony_pf_device);
  2681. if (!result)
  2682. sony_nc_function_setup(device, sony_pf_device);
  2683. }
  2684. if (acpi_video_get_backlight_type() == acpi_backlight_vendor)
  2685. sony_nc_backlight_setup();
  2686. /* create sony_pf sysfs attributes related to the SNC device */
  2687. for (item = sony_nc_values; item->name; ++item) {
  2688. if (!debug && item->debug)
  2689. continue;
  2690. /* find the available acpiget as described in the DSDT */
  2691. for (; item->acpiget && *item->acpiget; ++item->acpiget) {
  2692. if (acpi_has_method(sony_nc_acpi_handle,
  2693. *item->acpiget)) {
  2694. dprintk("Found %s getter: %s\n",
  2695. item->name, *item->acpiget);
  2696. item->devattr.attr.mode |= S_IRUGO;
  2697. break;
  2698. }
  2699. }
  2700. /* find the available acpiset as described in the DSDT */
  2701. for (; item->acpiset && *item->acpiset; ++item->acpiset) {
  2702. if (acpi_has_method(sony_nc_acpi_handle,
  2703. *item->acpiset)) {
  2704. dprintk("Found %s setter: %s\n",
  2705. item->name, *item->acpiset);
  2706. item->devattr.attr.mode |= S_IWUSR;
  2707. break;
  2708. }
  2709. }
  2710. if (item->devattr.attr.mode != 0) {
  2711. result =
  2712. device_create_file(&sony_pf_device->dev,
  2713. &item->devattr);
  2714. if (result)
  2715. goto out_sysfs;
  2716. }
  2717. }
  2718. pr_info("SNC setup done.\n");
  2719. return 0;
  2720. out_sysfs:
  2721. for (item = sony_nc_values; item->name; ++item) {
  2722. device_remove_file(&sony_pf_device->dev, &item->devattr);
  2723. }
  2724. sony_nc_backlight_cleanup();
  2725. sony_nc_function_cleanup(sony_pf_device);
  2726. sony_nc_handles_cleanup(sony_pf_device);
  2727. outplatform:
  2728. sony_laptop_remove_input();
  2729. outpresent:
  2730. sony_pf_remove();
  2731. outwalk:
  2732. sony_nc_rfkill_cleanup();
  2733. return result;
  2734. }
  2735. static int sony_nc_remove(struct acpi_device *device)
  2736. {
  2737. struct sony_nc_value *item;
  2738. sony_nc_backlight_cleanup();
  2739. sony_nc_acpi_device = NULL;
  2740. for (item = sony_nc_values; item->name; ++item) {
  2741. device_remove_file(&sony_pf_device->dev, &item->devattr);
  2742. }
  2743. sony_nc_function_cleanup(sony_pf_device);
  2744. sony_nc_handles_cleanup(sony_pf_device);
  2745. sony_pf_remove();
  2746. sony_laptop_remove_input();
  2747. dprintk(SONY_NC_DRIVER_NAME " removed.\n");
  2748. return 0;
  2749. }
  2750. static const struct acpi_device_id sony_device_ids[] = {
  2751. {SONY_NC_HID, 0},
  2752. {SONY_PIC_HID, 0},
  2753. {"", 0},
  2754. };
  2755. MODULE_DEVICE_TABLE(acpi, sony_device_ids);
  2756. static const struct acpi_device_id sony_nc_device_ids[] = {
  2757. {SONY_NC_HID, 0},
  2758. {"", 0},
  2759. };
  2760. static struct acpi_driver sony_nc_driver = {
  2761. .name = SONY_NC_DRIVER_NAME,
  2762. .class = SONY_NC_CLASS,
  2763. .ids = sony_nc_device_ids,
  2764. .owner = THIS_MODULE,
  2765. .ops = {
  2766. .add = sony_nc_add,
  2767. .remove = sony_nc_remove,
  2768. .notify = sony_nc_notify,
  2769. },
  2770. .drv.pm = &sony_nc_pm,
  2771. };
  2772. /*********** SPIC (SNY6001) Device ***********/
  2773. #define SONYPI_DEVICE_TYPE1 0x00000001
  2774. #define SONYPI_DEVICE_TYPE2 0x00000002
  2775. #define SONYPI_DEVICE_TYPE3 0x00000004
  2776. #define SONYPI_TYPE1_OFFSET 0x04
  2777. #define SONYPI_TYPE2_OFFSET 0x12
  2778. #define SONYPI_TYPE3_OFFSET 0x12
  2779. struct sony_pic_ioport {
  2780. struct acpi_resource_io io1;
  2781. struct acpi_resource_io io2;
  2782. struct list_head list;
  2783. };
  2784. struct sony_pic_irq {
  2785. struct acpi_resource_irq irq;
  2786. struct list_head list;
  2787. };
  2788. struct sonypi_eventtypes {
  2789. u8 data;
  2790. unsigned long mask;
  2791. struct sonypi_event *events;
  2792. };
  2793. struct sony_pic_dev {
  2794. struct acpi_device *acpi_dev;
  2795. struct sony_pic_irq *cur_irq;
  2796. struct sony_pic_ioport *cur_ioport;
  2797. struct list_head interrupts;
  2798. struct list_head ioports;
  2799. struct mutex lock;
  2800. struct sonypi_eventtypes *event_types;
  2801. int (*handle_irq)(const u8, const u8);
  2802. int model;
  2803. u16 evport_offset;
  2804. u8 camera_power;
  2805. u8 bluetooth_power;
  2806. u8 wwan_power;
  2807. };
  2808. static struct sony_pic_dev spic_dev = {
  2809. .interrupts = LIST_HEAD_INIT(spic_dev.interrupts),
  2810. .ioports = LIST_HEAD_INIT(spic_dev.ioports),
  2811. };
  2812. static int spic_drv_registered;
  2813. /* Event masks */
  2814. #define SONYPI_JOGGER_MASK 0x00000001
  2815. #define SONYPI_CAPTURE_MASK 0x00000002
  2816. #define SONYPI_FNKEY_MASK 0x00000004
  2817. #define SONYPI_BLUETOOTH_MASK 0x00000008
  2818. #define SONYPI_PKEY_MASK 0x00000010
  2819. #define SONYPI_BACK_MASK 0x00000020
  2820. #define SONYPI_HELP_MASK 0x00000040
  2821. #define SONYPI_LID_MASK 0x00000080
  2822. #define SONYPI_ZOOM_MASK 0x00000100
  2823. #define SONYPI_THUMBPHRASE_MASK 0x00000200
  2824. #define SONYPI_MEYE_MASK 0x00000400
  2825. #define SONYPI_MEMORYSTICK_MASK 0x00000800
  2826. #define SONYPI_BATTERY_MASK 0x00001000
  2827. #define SONYPI_WIRELESS_MASK 0x00002000
  2828. struct sonypi_event {
  2829. u8 data;
  2830. u8 event;
  2831. };
  2832. /* The set of possible button release events */
  2833. static struct sonypi_event sonypi_releaseev[] = {
  2834. { 0x00, SONYPI_EVENT_ANYBUTTON_RELEASED },
  2835. { 0, 0 }
  2836. };
  2837. /* The set of possible jogger events */
  2838. static struct sonypi_event sonypi_joggerev[] = {
  2839. { 0x1f, SONYPI_EVENT_JOGDIAL_UP },
  2840. { 0x01, SONYPI_EVENT_JOGDIAL_DOWN },
  2841. { 0x5f, SONYPI_EVENT_JOGDIAL_UP_PRESSED },
  2842. { 0x41, SONYPI_EVENT_JOGDIAL_DOWN_PRESSED },
  2843. { 0x1e, SONYPI_EVENT_JOGDIAL_FAST_UP },
  2844. { 0x02, SONYPI_EVENT_JOGDIAL_FAST_DOWN },
  2845. { 0x5e, SONYPI_EVENT_JOGDIAL_FAST_UP_PRESSED },
  2846. { 0x42, SONYPI_EVENT_JOGDIAL_FAST_DOWN_PRESSED },
  2847. { 0x1d, SONYPI_EVENT_JOGDIAL_VFAST_UP },
  2848. { 0x03, SONYPI_EVENT_JOGDIAL_VFAST_DOWN },
  2849. { 0x5d, SONYPI_EVENT_JOGDIAL_VFAST_UP_PRESSED },
  2850. { 0x43, SONYPI_EVENT_JOGDIAL_VFAST_DOWN_PRESSED },
  2851. { 0x40, SONYPI_EVENT_JOGDIAL_PRESSED },
  2852. { 0, 0 }
  2853. };
  2854. /* The set of possible capture button events */
  2855. static struct sonypi_event sonypi_captureev[] = {
  2856. { 0x05, SONYPI_EVENT_CAPTURE_PARTIALPRESSED },
  2857. { 0x07, SONYPI_EVENT_CAPTURE_PRESSED },
  2858. { 0x40, SONYPI_EVENT_CAPTURE_PRESSED },
  2859. { 0x01, SONYPI_EVENT_CAPTURE_PARTIALRELEASED },
  2860. { 0, 0 }
  2861. };
  2862. /* The set of possible fnkeys events */
  2863. static struct sonypi_event sonypi_fnkeyev[] = {
  2864. { 0x10, SONYPI_EVENT_FNKEY_ESC },
  2865. { 0x11, SONYPI_EVENT_FNKEY_F1 },
  2866. { 0x12, SONYPI_EVENT_FNKEY_F2 },
  2867. { 0x13, SONYPI_EVENT_FNKEY_F3 },
  2868. { 0x14, SONYPI_EVENT_FNKEY_F4 },
  2869. { 0x15, SONYPI_EVENT_FNKEY_F5 },
  2870. { 0x16, SONYPI_EVENT_FNKEY_F6 },
  2871. { 0x17, SONYPI_EVENT_FNKEY_F7 },
  2872. { 0x18, SONYPI_EVENT_FNKEY_F8 },
  2873. { 0x19, SONYPI_EVENT_FNKEY_F9 },
  2874. { 0x1a, SONYPI_EVENT_FNKEY_F10 },
  2875. { 0x1b, SONYPI_EVENT_FNKEY_F11 },
  2876. { 0x1c, SONYPI_EVENT_FNKEY_F12 },
  2877. { 0x1f, SONYPI_EVENT_FNKEY_RELEASED },
  2878. { 0x21, SONYPI_EVENT_FNKEY_1 },
  2879. { 0x22, SONYPI_EVENT_FNKEY_2 },
  2880. { 0x31, SONYPI_EVENT_FNKEY_D },
  2881. { 0x32, SONYPI_EVENT_FNKEY_E },
  2882. { 0x33, SONYPI_EVENT_FNKEY_F },
  2883. { 0x34, SONYPI_EVENT_FNKEY_S },
  2884. { 0x35, SONYPI_EVENT_FNKEY_B },
  2885. { 0x36, SONYPI_EVENT_FNKEY_ONLY },
  2886. { 0, 0 }
  2887. };
  2888. /* The set of possible program key events */
  2889. static struct sonypi_event sonypi_pkeyev[] = {
  2890. { 0x01, SONYPI_EVENT_PKEY_P1 },
  2891. { 0x02, SONYPI_EVENT_PKEY_P2 },
  2892. { 0x04, SONYPI_EVENT_PKEY_P3 },
  2893. { 0x20, SONYPI_EVENT_PKEY_P1 },
  2894. { 0, 0 }
  2895. };
  2896. /* The set of possible bluetooth events */
  2897. static struct sonypi_event sonypi_blueev[] = {
  2898. { 0x55, SONYPI_EVENT_BLUETOOTH_PRESSED },
  2899. { 0x59, SONYPI_EVENT_BLUETOOTH_ON },
  2900. { 0x5a, SONYPI_EVENT_BLUETOOTH_OFF },
  2901. { 0, 0 }
  2902. };
  2903. /* The set of possible wireless events */
  2904. static struct sonypi_event sonypi_wlessev[] = {
  2905. { 0x59, SONYPI_EVENT_IGNORE },
  2906. { 0x5a, SONYPI_EVENT_IGNORE },
  2907. { 0, 0 }
  2908. };
  2909. /* The set of possible back button events */
  2910. static struct sonypi_event sonypi_backev[] = {
  2911. { 0x20, SONYPI_EVENT_BACK_PRESSED },
  2912. { 0, 0 }
  2913. };
  2914. /* The set of possible help button events */
  2915. static struct sonypi_event sonypi_helpev[] = {
  2916. { 0x3b, SONYPI_EVENT_HELP_PRESSED },
  2917. { 0, 0 }
  2918. };
  2919. /* The set of possible lid events */
  2920. static struct sonypi_event sonypi_lidev[] = {
  2921. { 0x51, SONYPI_EVENT_LID_CLOSED },
  2922. { 0x50, SONYPI_EVENT_LID_OPENED },
  2923. { 0, 0 }
  2924. };
  2925. /* The set of possible zoom events */
  2926. static struct sonypi_event sonypi_zoomev[] = {
  2927. { 0x39, SONYPI_EVENT_ZOOM_PRESSED },
  2928. { 0x10, SONYPI_EVENT_ZOOM_IN_PRESSED },
  2929. { 0x20, SONYPI_EVENT_ZOOM_OUT_PRESSED },
  2930. { 0x04, SONYPI_EVENT_ZOOM_PRESSED },
  2931. { 0, 0 }
  2932. };
  2933. /* The set of possible thumbphrase events */
  2934. static struct sonypi_event sonypi_thumbphraseev[] = {
  2935. { 0x3a, SONYPI_EVENT_THUMBPHRASE_PRESSED },
  2936. { 0, 0 }
  2937. };
  2938. /* The set of possible motioneye camera events */
  2939. static struct sonypi_event sonypi_meyeev[] = {
  2940. { 0x00, SONYPI_EVENT_MEYE_FACE },
  2941. { 0x01, SONYPI_EVENT_MEYE_OPPOSITE },
  2942. { 0, 0 }
  2943. };
  2944. /* The set of possible memorystick events */
  2945. static struct sonypi_event sonypi_memorystickev[] = {
  2946. { 0x53, SONYPI_EVENT_MEMORYSTICK_INSERT },
  2947. { 0x54, SONYPI_EVENT_MEMORYSTICK_EJECT },
  2948. { 0, 0 }
  2949. };
  2950. /* The set of possible battery events */
  2951. static struct sonypi_event sonypi_batteryev[] = {
  2952. { 0x20, SONYPI_EVENT_BATTERY_INSERT },
  2953. { 0x30, SONYPI_EVENT_BATTERY_REMOVE },
  2954. { 0, 0 }
  2955. };
  2956. /* The set of possible volume events */
  2957. static struct sonypi_event sonypi_volumeev[] = {
  2958. { 0x01, SONYPI_EVENT_VOLUME_INC_PRESSED },
  2959. { 0x02, SONYPI_EVENT_VOLUME_DEC_PRESSED },
  2960. { 0, 0 }
  2961. };
  2962. /* The set of possible brightness events */
  2963. static struct sonypi_event sonypi_brightnessev[] = {
  2964. { 0x80, SONYPI_EVENT_BRIGHTNESS_PRESSED },
  2965. { 0, 0 }
  2966. };
  2967. static struct sonypi_eventtypes type1_events[] = {
  2968. { 0, 0xffffffff, sonypi_releaseev },
  2969. { 0x70, SONYPI_MEYE_MASK, sonypi_meyeev },
  2970. { 0x30, SONYPI_LID_MASK, sonypi_lidev },
  2971. { 0x60, SONYPI_CAPTURE_MASK, sonypi_captureev },
  2972. { 0x10, SONYPI_JOGGER_MASK, sonypi_joggerev },
  2973. { 0x20, SONYPI_FNKEY_MASK, sonypi_fnkeyev },
  2974. { 0x30, SONYPI_BLUETOOTH_MASK, sonypi_blueev },
  2975. { 0x40, SONYPI_PKEY_MASK, sonypi_pkeyev },
  2976. { 0x30, SONYPI_MEMORYSTICK_MASK, sonypi_memorystickev },
  2977. { 0x40, SONYPI_BATTERY_MASK, sonypi_batteryev },
  2978. { 0 },
  2979. };
  2980. static struct sonypi_eventtypes type2_events[] = {
  2981. { 0, 0xffffffff, sonypi_releaseev },
  2982. { 0x38, SONYPI_LID_MASK, sonypi_lidev },
  2983. { 0x11, SONYPI_JOGGER_MASK, sonypi_joggerev },
  2984. { 0x61, SONYPI_CAPTURE_MASK, sonypi_captureev },
  2985. { 0x21, SONYPI_FNKEY_MASK, sonypi_fnkeyev },
  2986. { 0x31, SONYPI_BLUETOOTH_MASK, sonypi_blueev },
  2987. { 0x08, SONYPI_PKEY_MASK, sonypi_pkeyev },
  2988. { 0x11, SONYPI_BACK_MASK, sonypi_backev },
  2989. { 0x21, SONYPI_HELP_MASK, sonypi_helpev },
  2990. { 0x21, SONYPI_ZOOM_MASK, sonypi_zoomev },
  2991. { 0x20, SONYPI_THUMBPHRASE_MASK, sonypi_thumbphraseev },
  2992. { 0x31, SONYPI_MEMORYSTICK_MASK, sonypi_memorystickev },
  2993. { 0x41, SONYPI_BATTERY_MASK, sonypi_batteryev },
  2994. { 0x31, SONYPI_PKEY_MASK, sonypi_pkeyev },
  2995. { 0 },
  2996. };
  2997. static struct sonypi_eventtypes type3_events[] = {
  2998. { 0, 0xffffffff, sonypi_releaseev },
  2999. { 0x21, SONYPI_FNKEY_MASK, sonypi_fnkeyev },
  3000. { 0x31, SONYPI_WIRELESS_MASK, sonypi_wlessev },
  3001. { 0x31, SONYPI_MEMORYSTICK_MASK, sonypi_memorystickev },
  3002. { 0x41, SONYPI_BATTERY_MASK, sonypi_batteryev },
  3003. { 0x31, SONYPI_PKEY_MASK, sonypi_pkeyev },
  3004. { 0x05, SONYPI_PKEY_MASK, sonypi_pkeyev },
  3005. { 0x05, SONYPI_ZOOM_MASK, sonypi_zoomev },
  3006. { 0x05, SONYPI_CAPTURE_MASK, sonypi_captureev },
  3007. { 0x05, SONYPI_PKEY_MASK, sonypi_volumeev },
  3008. { 0x05, SONYPI_PKEY_MASK, sonypi_brightnessev },
  3009. { 0 },
  3010. };
  3011. /* low level spic calls */
  3012. #define ITERATIONS_LONG 10000
  3013. #define ITERATIONS_SHORT 10
  3014. #define wait_on_command(command, iterations) { \
  3015. unsigned int n = iterations; \
  3016. while (--n && (command)) \
  3017. udelay(1); \
  3018. if (!n) \
  3019. dprintk("command failed at %s : %s (line %d)\n", \
  3020. __FILE__, __func__, __LINE__); \
  3021. }
  3022. static u8 sony_pic_call1(u8 dev)
  3023. {
  3024. u8 v1, v2;
  3025. wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2,
  3026. ITERATIONS_LONG);
  3027. outb(dev, spic_dev.cur_ioport->io1.minimum + 4);
  3028. v1 = inb_p(spic_dev.cur_ioport->io1.minimum + 4);
  3029. v2 = inb_p(spic_dev.cur_ioport->io1.minimum);
  3030. dprintk("sony_pic_call1(0x%.2x): 0x%.4x\n", dev, (v2 << 8) | v1);
  3031. return v2;
  3032. }
  3033. static u8 sony_pic_call2(u8 dev, u8 fn)
  3034. {
  3035. u8 v1;
  3036. wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2,
  3037. ITERATIONS_LONG);
  3038. outb(dev, spic_dev.cur_ioport->io1.minimum + 4);
  3039. wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2,
  3040. ITERATIONS_LONG);
  3041. outb(fn, spic_dev.cur_ioport->io1.minimum);
  3042. v1 = inb_p(spic_dev.cur_ioport->io1.minimum);
  3043. dprintk("sony_pic_call2(0x%.2x - 0x%.2x): 0x%.4x\n", dev, fn, v1);
  3044. return v1;
  3045. }
  3046. static u8 sony_pic_call3(u8 dev, u8 fn, u8 v)
  3047. {
  3048. u8 v1;
  3049. wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2, ITERATIONS_LONG);
  3050. outb(dev, spic_dev.cur_ioport->io1.minimum + 4);
  3051. wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2, ITERATIONS_LONG);
  3052. outb(fn, spic_dev.cur_ioport->io1.minimum);
  3053. wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2, ITERATIONS_LONG);
  3054. outb(v, spic_dev.cur_ioport->io1.minimum);
  3055. v1 = inb_p(spic_dev.cur_ioport->io1.minimum);
  3056. dprintk("sony_pic_call3(0x%.2x - 0x%.2x - 0x%.2x): 0x%.4x\n",
  3057. dev, fn, v, v1);
  3058. return v1;
  3059. }
  3060. /*
  3061. * minidrivers for SPIC models
  3062. */
  3063. static int type3_handle_irq(const u8 data_mask, const u8 ev)
  3064. {
  3065. /*
  3066. * 0x31 could mean we have to take some extra action and wait for
  3067. * the next irq for some Type3 models, it will generate a new
  3068. * irq and we can read new data from the device:
  3069. * - 0x5c and 0x5f requires 0xA0
  3070. * - 0x61 requires 0xB3
  3071. */
  3072. if (data_mask == 0x31) {
  3073. if (ev == 0x5c || ev == 0x5f)
  3074. sony_pic_call1(0xA0);
  3075. else if (ev == 0x61)
  3076. sony_pic_call1(0xB3);
  3077. return 0;
  3078. }
  3079. return 1;
  3080. }
  3081. static void sony_pic_detect_device_type(struct sony_pic_dev *dev)
  3082. {
  3083. struct pci_dev *pcidev;
  3084. pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
  3085. PCI_DEVICE_ID_INTEL_82371AB_3, NULL);
  3086. if (pcidev) {
  3087. dev->model = SONYPI_DEVICE_TYPE1;
  3088. dev->evport_offset = SONYPI_TYPE1_OFFSET;
  3089. dev->event_types = type1_events;
  3090. goto out;
  3091. }
  3092. pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
  3093. PCI_DEVICE_ID_INTEL_ICH6_1, NULL);
  3094. if (pcidev) {
  3095. dev->model = SONYPI_DEVICE_TYPE2;
  3096. dev->evport_offset = SONYPI_TYPE2_OFFSET;
  3097. dev->event_types = type2_events;
  3098. goto out;
  3099. }
  3100. pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
  3101. PCI_DEVICE_ID_INTEL_ICH7_1, NULL);
  3102. if (pcidev) {
  3103. dev->model = SONYPI_DEVICE_TYPE3;
  3104. dev->handle_irq = type3_handle_irq;
  3105. dev->evport_offset = SONYPI_TYPE3_OFFSET;
  3106. dev->event_types = type3_events;
  3107. goto out;
  3108. }
  3109. pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
  3110. PCI_DEVICE_ID_INTEL_ICH8_4, NULL);
  3111. if (pcidev) {
  3112. dev->model = SONYPI_DEVICE_TYPE3;
  3113. dev->handle_irq = type3_handle_irq;
  3114. dev->evport_offset = SONYPI_TYPE3_OFFSET;
  3115. dev->event_types = type3_events;
  3116. goto out;
  3117. }
  3118. pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
  3119. PCI_DEVICE_ID_INTEL_ICH9_1, NULL);
  3120. if (pcidev) {
  3121. dev->model = SONYPI_DEVICE_TYPE3;
  3122. dev->handle_irq = type3_handle_irq;
  3123. dev->evport_offset = SONYPI_TYPE3_OFFSET;
  3124. dev->event_types = type3_events;
  3125. goto out;
  3126. }
  3127. /* default */
  3128. dev->model = SONYPI_DEVICE_TYPE2;
  3129. dev->evport_offset = SONYPI_TYPE2_OFFSET;
  3130. dev->event_types = type2_events;
  3131. out:
  3132. pci_dev_put(pcidev);
  3133. pr_info("detected Type%d model\n",
  3134. dev->model == SONYPI_DEVICE_TYPE1 ? 1 :
  3135. dev->model == SONYPI_DEVICE_TYPE2 ? 2 : 3);
  3136. }
  3137. /* camera tests and poweron/poweroff */
  3138. #define SONYPI_CAMERA_PICTURE 5
  3139. #define SONYPI_CAMERA_CONTROL 0x10
  3140. #define SONYPI_CAMERA_BRIGHTNESS 0
  3141. #define SONYPI_CAMERA_CONTRAST 1
  3142. #define SONYPI_CAMERA_HUE 2
  3143. #define SONYPI_CAMERA_COLOR 3
  3144. #define SONYPI_CAMERA_SHARPNESS 4
  3145. #define SONYPI_CAMERA_EXPOSURE_MASK 0xC
  3146. #define SONYPI_CAMERA_WHITE_BALANCE_MASK 0x3
  3147. #define SONYPI_CAMERA_PICTURE_MODE_MASK 0x30
  3148. #define SONYPI_CAMERA_MUTE_MASK 0x40
  3149. /* the rest don't need a loop until not 0xff */
  3150. #define SONYPI_CAMERA_AGC 6
  3151. #define SONYPI_CAMERA_AGC_MASK 0x30
  3152. #define SONYPI_CAMERA_SHUTTER_MASK 0x7
  3153. #define SONYPI_CAMERA_SHUTDOWN_REQUEST 7
  3154. #define SONYPI_CAMERA_CONTROL 0x10
  3155. #define SONYPI_CAMERA_STATUS 7
  3156. #define SONYPI_CAMERA_STATUS_READY 0x2
  3157. #define SONYPI_CAMERA_STATUS_POSITION 0x4
  3158. #define SONYPI_DIRECTION_BACKWARDS 0x4
  3159. #define SONYPI_CAMERA_REVISION 8
  3160. #define SONYPI_CAMERA_ROMVERSION 9
  3161. static int __sony_pic_camera_ready(void)
  3162. {
  3163. u8 v;
  3164. v = sony_pic_call2(0x8f, SONYPI_CAMERA_STATUS);
  3165. return (v != 0xff && (v & SONYPI_CAMERA_STATUS_READY));
  3166. }
  3167. static int __sony_pic_camera_off(void)
  3168. {
  3169. if (!camera) {
  3170. pr_warn("camera control not enabled\n");
  3171. return -ENODEV;
  3172. }
  3173. wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_PICTURE,
  3174. SONYPI_CAMERA_MUTE_MASK),
  3175. ITERATIONS_SHORT);
  3176. if (spic_dev.camera_power) {
  3177. sony_pic_call2(0x91, 0);
  3178. spic_dev.camera_power = 0;
  3179. }
  3180. return 0;
  3181. }
  3182. static int __sony_pic_camera_on(void)
  3183. {
  3184. int i, j, x;
  3185. if (!camera) {
  3186. pr_warn("camera control not enabled\n");
  3187. return -ENODEV;
  3188. }
  3189. if (spic_dev.camera_power)
  3190. return 0;
  3191. for (j = 5; j > 0; j--) {
  3192. for (x = 0; x < 100 && sony_pic_call2(0x91, 0x1); x++)
  3193. msleep(10);
  3194. sony_pic_call1(0x93);
  3195. for (i = 400; i > 0; i--) {
  3196. if (__sony_pic_camera_ready())
  3197. break;
  3198. msleep(10);
  3199. }
  3200. if (i)
  3201. break;
  3202. }
  3203. if (j == 0) {
  3204. pr_warn("failed to power on camera\n");
  3205. return -ENODEV;
  3206. }
  3207. wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_CONTROL,
  3208. 0x5a),
  3209. ITERATIONS_SHORT);
  3210. spic_dev.camera_power = 1;
  3211. return 0;
  3212. }
  3213. /* External camera command (exported to the motion eye v4l driver) */
  3214. int sony_pic_camera_command(int command, u8 value)
  3215. {
  3216. if (!camera)
  3217. return -EIO;
  3218. mutex_lock(&spic_dev.lock);
  3219. switch (command) {
  3220. case SONY_PIC_COMMAND_SETCAMERA:
  3221. if (value)
  3222. __sony_pic_camera_on();
  3223. else
  3224. __sony_pic_camera_off();
  3225. break;
  3226. case SONY_PIC_COMMAND_SETCAMERABRIGHTNESS:
  3227. wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_BRIGHTNESS, value),
  3228. ITERATIONS_SHORT);
  3229. break;
  3230. case SONY_PIC_COMMAND_SETCAMERACONTRAST:
  3231. wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_CONTRAST, value),
  3232. ITERATIONS_SHORT);
  3233. break;
  3234. case SONY_PIC_COMMAND_SETCAMERAHUE:
  3235. wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_HUE, value),
  3236. ITERATIONS_SHORT);
  3237. break;
  3238. case SONY_PIC_COMMAND_SETCAMERACOLOR:
  3239. wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_COLOR, value),
  3240. ITERATIONS_SHORT);
  3241. break;
  3242. case SONY_PIC_COMMAND_SETCAMERASHARPNESS:
  3243. wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_SHARPNESS, value),
  3244. ITERATIONS_SHORT);
  3245. break;
  3246. case SONY_PIC_COMMAND_SETCAMERAPICTURE:
  3247. wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_PICTURE, value),
  3248. ITERATIONS_SHORT);
  3249. break;
  3250. case SONY_PIC_COMMAND_SETCAMERAAGC:
  3251. wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_AGC, value),
  3252. ITERATIONS_SHORT);
  3253. break;
  3254. default:
  3255. pr_err("sony_pic_camera_command invalid: %d\n", command);
  3256. break;
  3257. }
  3258. mutex_unlock(&spic_dev.lock);
  3259. return 0;
  3260. }
  3261. EXPORT_SYMBOL(sony_pic_camera_command);
  3262. /* gprs/edge modem (SZ460N and SZ210P), thanks to Joshua Wise */
  3263. static void __sony_pic_set_wwanpower(u8 state)
  3264. {
  3265. state = !!state;
  3266. if (spic_dev.wwan_power == state)
  3267. return;
  3268. sony_pic_call2(0xB0, state);
  3269. sony_pic_call1(0x82);
  3270. spic_dev.wwan_power = state;
  3271. }
  3272. static ssize_t sony_pic_wwanpower_store(struct device *dev,
  3273. struct device_attribute *attr,
  3274. const char *buffer, size_t count)
  3275. {
  3276. unsigned long value;
  3277. if (count > 31)
  3278. return -EINVAL;
  3279. if (kstrtoul(buffer, 10, &value))
  3280. return -EINVAL;
  3281. mutex_lock(&spic_dev.lock);
  3282. __sony_pic_set_wwanpower(value);
  3283. mutex_unlock(&spic_dev.lock);
  3284. return count;
  3285. }
  3286. static ssize_t sony_pic_wwanpower_show(struct device *dev,
  3287. struct device_attribute *attr, char *buffer)
  3288. {
  3289. ssize_t count;
  3290. mutex_lock(&spic_dev.lock);
  3291. count = snprintf(buffer, PAGE_SIZE, "%d\n", spic_dev.wwan_power);
  3292. mutex_unlock(&spic_dev.lock);
  3293. return count;
  3294. }
  3295. /* bluetooth subsystem power state */
  3296. static void __sony_pic_set_bluetoothpower(u8 state)
  3297. {
  3298. state = !!state;
  3299. if (spic_dev.bluetooth_power == state)
  3300. return;
  3301. sony_pic_call2(0x96, state);
  3302. sony_pic_call1(0x82);
  3303. spic_dev.bluetooth_power = state;
  3304. }
  3305. static ssize_t sony_pic_bluetoothpower_store(struct device *dev,
  3306. struct device_attribute *attr,
  3307. const char *buffer, size_t count)
  3308. {
  3309. unsigned long value;
  3310. if (count > 31)
  3311. return -EINVAL;
  3312. if (kstrtoul(buffer, 10, &value))
  3313. return -EINVAL;
  3314. mutex_lock(&spic_dev.lock);
  3315. __sony_pic_set_bluetoothpower(value);
  3316. mutex_unlock(&spic_dev.lock);
  3317. return count;
  3318. }
  3319. static ssize_t sony_pic_bluetoothpower_show(struct device *dev,
  3320. struct device_attribute *attr, char *buffer)
  3321. {
  3322. ssize_t count = 0;
  3323. mutex_lock(&spic_dev.lock);
  3324. count = snprintf(buffer, PAGE_SIZE, "%d\n", spic_dev.bluetooth_power);
  3325. mutex_unlock(&spic_dev.lock);
  3326. return count;
  3327. }
  3328. /* fan speed */
  3329. /* FAN0 information (reverse engineered from ACPI tables) */
  3330. #define SONY_PIC_FAN0_STATUS 0x93
  3331. static int sony_pic_set_fanspeed(unsigned long value)
  3332. {
  3333. return ec_write(SONY_PIC_FAN0_STATUS, value);
  3334. }
  3335. static int sony_pic_get_fanspeed(u8 *value)
  3336. {
  3337. return ec_read(SONY_PIC_FAN0_STATUS, value);
  3338. }
  3339. static ssize_t sony_pic_fanspeed_store(struct device *dev,
  3340. struct device_attribute *attr,
  3341. const char *buffer, size_t count)
  3342. {
  3343. unsigned long value;
  3344. if (count > 31)
  3345. return -EINVAL;
  3346. if (kstrtoul(buffer, 10, &value))
  3347. return -EINVAL;
  3348. if (sony_pic_set_fanspeed(value))
  3349. return -EIO;
  3350. return count;
  3351. }
  3352. static ssize_t sony_pic_fanspeed_show(struct device *dev,
  3353. struct device_attribute *attr, char *buffer)
  3354. {
  3355. u8 value = 0;
  3356. if (sony_pic_get_fanspeed(&value))
  3357. return -EIO;
  3358. return snprintf(buffer, PAGE_SIZE, "%d\n", value);
  3359. }
  3360. #define SPIC_ATTR(_name, _mode) \
  3361. struct device_attribute spic_attr_##_name = __ATTR(_name, \
  3362. _mode, sony_pic_## _name ##_show, \
  3363. sony_pic_## _name ##_store)
  3364. static SPIC_ATTR(bluetoothpower, 0644);
  3365. static SPIC_ATTR(wwanpower, 0644);
  3366. static SPIC_ATTR(fanspeed, 0644);
  3367. static struct attribute *spic_attributes[] = {
  3368. &spic_attr_bluetoothpower.attr,
  3369. &spic_attr_wwanpower.attr,
  3370. &spic_attr_fanspeed.attr,
  3371. NULL
  3372. };
  3373. static const struct attribute_group spic_attribute_group = {
  3374. .attrs = spic_attributes
  3375. };
  3376. /******** SONYPI compatibility **********/
  3377. #ifdef CONFIG_SONYPI_COMPAT
  3378. /* battery / brightness / temperature addresses */
  3379. #define SONYPI_BAT_FLAGS 0x81
  3380. #define SONYPI_LCD_LIGHT 0x96
  3381. #define SONYPI_BAT1_PCTRM 0xa0
  3382. #define SONYPI_BAT1_LEFT 0xa2
  3383. #define SONYPI_BAT1_MAXRT 0xa4
  3384. #define SONYPI_BAT2_PCTRM 0xa8
  3385. #define SONYPI_BAT2_LEFT 0xaa
  3386. #define SONYPI_BAT2_MAXRT 0xac
  3387. #define SONYPI_BAT1_MAXTK 0xb0
  3388. #define SONYPI_BAT1_FULL 0xb2
  3389. #define SONYPI_BAT2_MAXTK 0xb8
  3390. #define SONYPI_BAT2_FULL 0xba
  3391. #define SONYPI_TEMP_STATUS 0xC1
  3392. struct sonypi_compat_s {
  3393. struct fasync_struct *fifo_async;
  3394. struct kfifo fifo;
  3395. spinlock_t fifo_lock;
  3396. wait_queue_head_t fifo_proc_list;
  3397. atomic_t open_count;
  3398. };
  3399. static struct sonypi_compat_s sonypi_compat = {
  3400. .open_count = ATOMIC_INIT(0),
  3401. };
  3402. static int sonypi_misc_fasync(int fd, struct file *filp, int on)
  3403. {
  3404. return fasync_helper(fd, filp, on, &sonypi_compat.fifo_async);
  3405. }
  3406. static int sonypi_misc_release(struct inode *inode, struct file *file)
  3407. {
  3408. atomic_dec(&sonypi_compat.open_count);
  3409. return 0;
  3410. }
  3411. static int sonypi_misc_open(struct inode *inode, struct file *file)
  3412. {
  3413. /* Flush input queue on first open */
  3414. unsigned long flags;
  3415. spin_lock_irqsave(&sonypi_compat.fifo_lock, flags);
  3416. if (atomic_inc_return(&sonypi_compat.open_count) == 1)
  3417. kfifo_reset(&sonypi_compat.fifo);
  3418. spin_unlock_irqrestore(&sonypi_compat.fifo_lock, flags);
  3419. return 0;
  3420. }
  3421. static ssize_t sonypi_misc_read(struct file *file, char __user *buf,
  3422. size_t count, loff_t *pos)
  3423. {
  3424. ssize_t ret;
  3425. unsigned char c;
  3426. if ((kfifo_len(&sonypi_compat.fifo) == 0) &&
  3427. (file->f_flags & O_NONBLOCK))
  3428. return -EAGAIN;
  3429. ret = wait_event_interruptible(sonypi_compat.fifo_proc_list,
  3430. kfifo_len(&sonypi_compat.fifo) != 0);
  3431. if (ret)
  3432. return ret;
  3433. while (ret < count &&
  3434. (kfifo_out_locked(&sonypi_compat.fifo, &c, sizeof(c),
  3435. &sonypi_compat.fifo_lock) == sizeof(c))) {
  3436. if (put_user(c, buf++))
  3437. return -EFAULT;
  3438. ret++;
  3439. }
  3440. if (ret > 0) {
  3441. struct inode *inode = file_inode(file);
  3442. inode->i_atime = current_time(inode);
  3443. }
  3444. return ret;
  3445. }
  3446. static __poll_t sonypi_misc_poll(struct file *file, poll_table *wait)
  3447. {
  3448. poll_wait(file, &sonypi_compat.fifo_proc_list, wait);
  3449. if (kfifo_len(&sonypi_compat.fifo))
  3450. return EPOLLIN | EPOLLRDNORM;
  3451. return 0;
  3452. }
  3453. static int ec_read16(u8 addr, u16 *value)
  3454. {
  3455. u8 val_lb, val_hb;
  3456. if (ec_read(addr, &val_lb))
  3457. return -1;
  3458. if (ec_read(addr + 1, &val_hb))
  3459. return -1;
  3460. *value = val_lb | (val_hb << 8);
  3461. return 0;
  3462. }
  3463. static long sonypi_misc_ioctl(struct file *fp, unsigned int cmd,
  3464. unsigned long arg)
  3465. {
  3466. int ret = 0;
  3467. void __user *argp = (void __user *)arg;
  3468. u8 val8;
  3469. u16 val16;
  3470. int value;
  3471. mutex_lock(&spic_dev.lock);
  3472. switch (cmd) {
  3473. case SONYPI_IOCGBRT:
  3474. if (sony_bl_props.dev == NULL) {
  3475. ret = -EIO;
  3476. break;
  3477. }
  3478. if (sony_nc_int_call(sony_nc_acpi_handle, "GBRT", NULL,
  3479. &value)) {
  3480. ret = -EIO;
  3481. break;
  3482. }
  3483. val8 = ((value & 0xff) - 1) << 5;
  3484. if (copy_to_user(argp, &val8, sizeof(val8)))
  3485. ret = -EFAULT;
  3486. break;
  3487. case SONYPI_IOCSBRT:
  3488. if (sony_bl_props.dev == NULL) {
  3489. ret = -EIO;
  3490. break;
  3491. }
  3492. if (copy_from_user(&val8, argp, sizeof(val8))) {
  3493. ret = -EFAULT;
  3494. break;
  3495. }
  3496. value = (val8 >> 5) + 1;
  3497. if (sony_nc_int_call(sony_nc_acpi_handle, "SBRT", &value,
  3498. NULL)) {
  3499. ret = -EIO;
  3500. break;
  3501. }
  3502. /* sync the backlight device status */
  3503. sony_bl_props.dev->props.brightness =
  3504. sony_backlight_get_brightness(sony_bl_props.dev);
  3505. break;
  3506. case SONYPI_IOCGBAT1CAP:
  3507. if (ec_read16(SONYPI_BAT1_FULL, &val16)) {
  3508. ret = -EIO;
  3509. break;
  3510. }
  3511. if (copy_to_user(argp, &val16, sizeof(val16)))
  3512. ret = -EFAULT;
  3513. break;
  3514. case SONYPI_IOCGBAT1REM:
  3515. if (ec_read16(SONYPI_BAT1_LEFT, &val16)) {
  3516. ret = -EIO;
  3517. break;
  3518. }
  3519. if (copy_to_user(argp, &val16, sizeof(val16)))
  3520. ret = -EFAULT;
  3521. break;
  3522. case SONYPI_IOCGBAT2CAP:
  3523. if (ec_read16(SONYPI_BAT2_FULL, &val16)) {
  3524. ret = -EIO;
  3525. break;
  3526. }
  3527. if (copy_to_user(argp, &val16, sizeof(val16)))
  3528. ret = -EFAULT;
  3529. break;
  3530. case SONYPI_IOCGBAT2REM:
  3531. if (ec_read16(SONYPI_BAT2_LEFT, &val16)) {
  3532. ret = -EIO;
  3533. break;
  3534. }
  3535. if (copy_to_user(argp, &val16, sizeof(val16)))
  3536. ret = -EFAULT;
  3537. break;
  3538. case SONYPI_IOCGBATFLAGS:
  3539. if (ec_read(SONYPI_BAT_FLAGS, &val8)) {
  3540. ret = -EIO;
  3541. break;
  3542. }
  3543. val8 &= 0x07;
  3544. if (copy_to_user(argp, &val8, sizeof(val8)))
  3545. ret = -EFAULT;
  3546. break;
  3547. case SONYPI_IOCGBLUE:
  3548. val8 = spic_dev.bluetooth_power;
  3549. if (copy_to_user(argp, &val8, sizeof(val8)))
  3550. ret = -EFAULT;
  3551. break;
  3552. case SONYPI_IOCSBLUE:
  3553. if (copy_from_user(&val8, argp, sizeof(val8))) {
  3554. ret = -EFAULT;
  3555. break;
  3556. }
  3557. __sony_pic_set_bluetoothpower(val8);
  3558. break;
  3559. /* FAN Controls */
  3560. case SONYPI_IOCGFAN:
  3561. if (sony_pic_get_fanspeed(&val8)) {
  3562. ret = -EIO;
  3563. break;
  3564. }
  3565. if (copy_to_user(argp, &val8, sizeof(val8)))
  3566. ret = -EFAULT;
  3567. break;
  3568. case SONYPI_IOCSFAN:
  3569. if (copy_from_user(&val8, argp, sizeof(val8))) {
  3570. ret = -EFAULT;
  3571. break;
  3572. }
  3573. if (sony_pic_set_fanspeed(val8))
  3574. ret = -EIO;
  3575. break;
  3576. /* GET Temperature (useful under APM) */
  3577. case SONYPI_IOCGTEMP:
  3578. if (ec_read(SONYPI_TEMP_STATUS, &val8)) {
  3579. ret = -EIO;
  3580. break;
  3581. }
  3582. if (copy_to_user(argp, &val8, sizeof(val8)))
  3583. ret = -EFAULT;
  3584. break;
  3585. default:
  3586. ret = -EINVAL;
  3587. }
  3588. mutex_unlock(&spic_dev.lock);
  3589. return ret;
  3590. }
  3591. static const struct file_operations sonypi_misc_fops = {
  3592. .owner = THIS_MODULE,
  3593. .read = sonypi_misc_read,
  3594. .poll = sonypi_misc_poll,
  3595. .open = sonypi_misc_open,
  3596. .release = sonypi_misc_release,
  3597. .fasync = sonypi_misc_fasync,
  3598. .unlocked_ioctl = sonypi_misc_ioctl,
  3599. .llseek = noop_llseek,
  3600. };
  3601. static struct miscdevice sonypi_misc_device = {
  3602. .minor = MISC_DYNAMIC_MINOR,
  3603. .name = "sonypi",
  3604. .fops = &sonypi_misc_fops,
  3605. };
  3606. static void sonypi_compat_report_event(u8 event)
  3607. {
  3608. kfifo_in_locked(&sonypi_compat.fifo, (unsigned char *)&event,
  3609. sizeof(event), &sonypi_compat.fifo_lock);
  3610. kill_fasync(&sonypi_compat.fifo_async, SIGIO, POLL_IN);
  3611. wake_up_interruptible(&sonypi_compat.fifo_proc_list);
  3612. }
  3613. static int sonypi_compat_init(void)
  3614. {
  3615. int error;
  3616. spin_lock_init(&sonypi_compat.fifo_lock);
  3617. error =
  3618. kfifo_alloc(&sonypi_compat.fifo, SONY_LAPTOP_BUF_SIZE, GFP_KERNEL);
  3619. if (error) {
  3620. pr_err("kfifo_alloc failed\n");
  3621. return error;
  3622. }
  3623. init_waitqueue_head(&sonypi_compat.fifo_proc_list);
  3624. if (minor != -1)
  3625. sonypi_misc_device.minor = minor;
  3626. error = misc_register(&sonypi_misc_device);
  3627. if (error) {
  3628. pr_err("misc_register failed\n");
  3629. goto err_free_kfifo;
  3630. }
  3631. if (minor == -1)
  3632. pr_info("device allocated minor is %d\n",
  3633. sonypi_misc_device.minor);
  3634. return 0;
  3635. err_free_kfifo:
  3636. kfifo_free(&sonypi_compat.fifo);
  3637. return error;
  3638. }
  3639. static void sonypi_compat_exit(void)
  3640. {
  3641. misc_deregister(&sonypi_misc_device);
  3642. kfifo_free(&sonypi_compat.fifo);
  3643. }
  3644. #else
  3645. static int sonypi_compat_init(void) { return 0; }
  3646. static void sonypi_compat_exit(void) { }
  3647. static void sonypi_compat_report_event(u8 event) { }
  3648. #endif /* CONFIG_SONYPI_COMPAT */
  3649. /*
  3650. * ACPI callbacks
  3651. */
  3652. static acpi_status
  3653. sony_pic_read_possible_resource(struct acpi_resource *resource, void *context)
  3654. {
  3655. u32 i;
  3656. struct sony_pic_dev *dev = (struct sony_pic_dev *)context;
  3657. switch (resource->type) {
  3658. case ACPI_RESOURCE_TYPE_START_DEPENDENT:
  3659. {
  3660. /* start IO enumeration */
  3661. struct sony_pic_ioport *ioport = kzalloc(sizeof(*ioport), GFP_KERNEL);
  3662. if (!ioport)
  3663. return AE_ERROR;
  3664. list_add(&ioport->list, &dev->ioports);
  3665. return AE_OK;
  3666. }
  3667. case ACPI_RESOURCE_TYPE_END_DEPENDENT:
  3668. /* end IO enumeration */
  3669. return AE_OK;
  3670. case ACPI_RESOURCE_TYPE_IRQ:
  3671. {
  3672. struct acpi_resource_irq *p = &resource->data.irq;
  3673. struct sony_pic_irq *interrupt = NULL;
  3674. if (!p || !p->interrupt_count) {
  3675. /*
  3676. * IRQ descriptors may have no IRQ# bits set,
  3677. * particularly those those w/ _STA disabled
  3678. */
  3679. dprintk("Blank IRQ resource\n");
  3680. return AE_OK;
  3681. }
  3682. for (i = 0; i < p->interrupt_count; i++) {
  3683. if (!p->interrupts[i]) {
  3684. pr_warn("Invalid IRQ %d\n",
  3685. p->interrupts[i]);
  3686. continue;
  3687. }
  3688. interrupt = kzalloc(sizeof(*interrupt),
  3689. GFP_KERNEL);
  3690. if (!interrupt)
  3691. return AE_ERROR;
  3692. list_add(&interrupt->list, &dev->interrupts);
  3693. interrupt->irq.triggering = p->triggering;
  3694. interrupt->irq.polarity = p->polarity;
  3695. interrupt->irq.sharable = p->sharable;
  3696. interrupt->irq.interrupt_count = 1;
  3697. interrupt->irq.interrupts[0] = p->interrupts[i];
  3698. }
  3699. return AE_OK;
  3700. }
  3701. case ACPI_RESOURCE_TYPE_IO:
  3702. {
  3703. struct acpi_resource_io *io = &resource->data.io;
  3704. struct sony_pic_ioport *ioport =
  3705. list_first_entry(&dev->ioports, struct sony_pic_ioport, list);
  3706. if (!io) {
  3707. dprintk("Blank IO resource\n");
  3708. return AE_OK;
  3709. }
  3710. if (!ioport->io1.minimum) {
  3711. memcpy(&ioport->io1, io, sizeof(*io));
  3712. dprintk("IO1 at 0x%.4x (0x%.2x)\n", ioport->io1.minimum,
  3713. ioport->io1.address_length);
  3714. }
  3715. else if (!ioport->io2.minimum) {
  3716. memcpy(&ioport->io2, io, sizeof(*io));
  3717. dprintk("IO2 at 0x%.4x (0x%.2x)\n", ioport->io2.minimum,
  3718. ioport->io2.address_length);
  3719. }
  3720. else {
  3721. pr_err("Unknown SPIC Type, more than 2 IO Ports\n");
  3722. return AE_ERROR;
  3723. }
  3724. return AE_OK;
  3725. }
  3726. case ACPI_RESOURCE_TYPE_END_TAG:
  3727. return AE_OK;
  3728. default:
  3729. dprintk("Resource %d isn't an IRQ nor an IO port\n",
  3730. resource->type);
  3731. return AE_CTRL_TERMINATE;
  3732. }
  3733. }
  3734. static int sony_pic_possible_resources(struct acpi_device *device)
  3735. {
  3736. int result = 0;
  3737. acpi_status status = AE_OK;
  3738. if (!device)
  3739. return -EINVAL;
  3740. /* get device status */
  3741. /* see acpi_pci_link_get_current acpi_pci_link_get_possible */
  3742. dprintk("Evaluating _STA\n");
  3743. result = acpi_bus_get_status(device);
  3744. if (result) {
  3745. pr_warn("Unable to read status\n");
  3746. goto end;
  3747. }
  3748. if (!device->status.enabled)
  3749. dprintk("Device disabled\n");
  3750. else
  3751. dprintk("Device enabled\n");
  3752. /*
  3753. * Query and parse 'method'
  3754. */
  3755. dprintk("Evaluating %s\n", METHOD_NAME__PRS);
  3756. status = acpi_walk_resources(device->handle, METHOD_NAME__PRS,
  3757. sony_pic_read_possible_resource, &spic_dev);
  3758. if (ACPI_FAILURE(status)) {
  3759. pr_warn("Failure evaluating %s\n", METHOD_NAME__PRS);
  3760. result = -ENODEV;
  3761. }
  3762. end:
  3763. return result;
  3764. }
  3765. /*
  3766. * Disable the spic device by calling its _DIS method
  3767. */
  3768. static int sony_pic_disable(struct acpi_device *device)
  3769. {
  3770. acpi_status ret = acpi_evaluate_object(device->handle, "_DIS", NULL,
  3771. NULL);
  3772. if (ACPI_FAILURE(ret) && ret != AE_NOT_FOUND)
  3773. return -ENXIO;
  3774. dprintk("Device disabled\n");
  3775. return 0;
  3776. }
  3777. /*
  3778. * Based on drivers/acpi/pci_link.c:acpi_pci_link_set
  3779. *
  3780. * Call _SRS to set current resources
  3781. */
  3782. static int sony_pic_enable(struct acpi_device *device,
  3783. struct sony_pic_ioport *ioport, struct sony_pic_irq *irq)
  3784. {
  3785. acpi_status status;
  3786. int result = 0;
  3787. /* Type 1 resource layout is:
  3788. * IO
  3789. * IO
  3790. * IRQNoFlags
  3791. * End
  3792. *
  3793. * Type 2 and 3 resource layout is:
  3794. * IO
  3795. * IRQNoFlags
  3796. * End
  3797. */
  3798. struct {
  3799. struct acpi_resource res1;
  3800. struct acpi_resource res2;
  3801. struct acpi_resource res3;
  3802. struct acpi_resource res4;
  3803. } *resource;
  3804. struct acpi_buffer buffer = { 0, NULL };
  3805. if (!ioport || !irq)
  3806. return -EINVAL;
  3807. /* init acpi_buffer */
  3808. resource = kzalloc(sizeof(*resource) + 1, GFP_KERNEL);
  3809. if (!resource)
  3810. return -ENOMEM;
  3811. buffer.length = sizeof(*resource) + 1;
  3812. buffer.pointer = resource;
  3813. /* setup Type 1 resources */
  3814. if (spic_dev.model == SONYPI_DEVICE_TYPE1) {
  3815. /* setup io resources */
  3816. resource->res1.type = ACPI_RESOURCE_TYPE_IO;
  3817. resource->res1.length = sizeof(struct acpi_resource);
  3818. memcpy(&resource->res1.data.io, &ioport->io1,
  3819. sizeof(struct acpi_resource_io));
  3820. resource->res2.type = ACPI_RESOURCE_TYPE_IO;
  3821. resource->res2.length = sizeof(struct acpi_resource);
  3822. memcpy(&resource->res2.data.io, &ioport->io2,
  3823. sizeof(struct acpi_resource_io));
  3824. /* setup irq resource */
  3825. resource->res3.type = ACPI_RESOURCE_TYPE_IRQ;
  3826. resource->res3.length = sizeof(struct acpi_resource);
  3827. memcpy(&resource->res3.data.irq, &irq->irq,
  3828. sizeof(struct acpi_resource_irq));
  3829. /* we requested a shared irq */
  3830. resource->res3.data.irq.sharable = ACPI_SHARED;
  3831. resource->res4.type = ACPI_RESOURCE_TYPE_END_TAG;
  3832. resource->res4.length = sizeof(struct acpi_resource);
  3833. }
  3834. /* setup Type 2/3 resources */
  3835. else {
  3836. /* setup io resource */
  3837. resource->res1.type = ACPI_RESOURCE_TYPE_IO;
  3838. resource->res1.length = sizeof(struct acpi_resource);
  3839. memcpy(&resource->res1.data.io, &ioport->io1,
  3840. sizeof(struct acpi_resource_io));
  3841. /* setup irq resource */
  3842. resource->res2.type = ACPI_RESOURCE_TYPE_IRQ;
  3843. resource->res2.length = sizeof(struct acpi_resource);
  3844. memcpy(&resource->res2.data.irq, &irq->irq,
  3845. sizeof(struct acpi_resource_irq));
  3846. /* we requested a shared irq */
  3847. resource->res2.data.irq.sharable = ACPI_SHARED;
  3848. resource->res3.type = ACPI_RESOURCE_TYPE_END_TAG;
  3849. resource->res3.length = sizeof(struct acpi_resource);
  3850. }
  3851. /* Attempt to set the resource */
  3852. dprintk("Evaluating _SRS\n");
  3853. status = acpi_set_current_resources(device->handle, &buffer);
  3854. /* check for total failure */
  3855. if (ACPI_FAILURE(status)) {
  3856. pr_err("Error evaluating _SRS\n");
  3857. result = -ENODEV;
  3858. goto end;
  3859. }
  3860. /* Necessary device initializations calls (from sonypi) */
  3861. sony_pic_call1(0x82);
  3862. sony_pic_call2(0x81, 0xff);
  3863. sony_pic_call1(compat ? 0x92 : 0x82);
  3864. end:
  3865. kfree(resource);
  3866. return result;
  3867. }
  3868. /*****************
  3869. *
  3870. * ISR: some event is available
  3871. *
  3872. *****************/
  3873. static irqreturn_t sony_pic_irq(int irq, void *dev_id)
  3874. {
  3875. int i, j;
  3876. u8 ev = 0;
  3877. u8 data_mask = 0;
  3878. u8 device_event = 0;
  3879. struct sony_pic_dev *dev = (struct sony_pic_dev *) dev_id;
  3880. ev = inb_p(dev->cur_ioport->io1.minimum);
  3881. if (dev->cur_ioport->io2.minimum)
  3882. data_mask = inb_p(dev->cur_ioport->io2.minimum);
  3883. else
  3884. data_mask = inb_p(dev->cur_ioport->io1.minimum +
  3885. dev->evport_offset);
  3886. dprintk("event ([%.2x] [%.2x]) at port 0x%.4x(+0x%.2x)\n",
  3887. ev, data_mask, dev->cur_ioport->io1.minimum,
  3888. dev->evport_offset);
  3889. if (ev == 0x00 || ev == 0xff)
  3890. return IRQ_HANDLED;
  3891. for (i = 0; dev->event_types[i].mask; i++) {
  3892. if ((data_mask & dev->event_types[i].data) !=
  3893. dev->event_types[i].data)
  3894. continue;
  3895. if (!(mask & dev->event_types[i].mask))
  3896. continue;
  3897. for (j = 0; dev->event_types[i].events[j].event; j++) {
  3898. if (ev == dev->event_types[i].events[j].data) {
  3899. device_event =
  3900. dev->event_types[i].events[j].event;
  3901. /* some events may require ignoring */
  3902. if (!device_event)
  3903. return IRQ_HANDLED;
  3904. goto found;
  3905. }
  3906. }
  3907. }
  3908. /* Still not able to decode the event try to pass
  3909. * it over to the minidriver
  3910. */
  3911. if (dev->handle_irq && dev->handle_irq(data_mask, ev) == 0)
  3912. return IRQ_HANDLED;
  3913. dprintk("unknown event ([%.2x] [%.2x]) at port 0x%.4x(+0x%.2x)\n",
  3914. ev, data_mask, dev->cur_ioport->io1.minimum,
  3915. dev->evport_offset);
  3916. return IRQ_HANDLED;
  3917. found:
  3918. sony_laptop_report_input_event(device_event);
  3919. sonypi_compat_report_event(device_event);
  3920. return IRQ_HANDLED;
  3921. }
  3922. /*****************
  3923. *
  3924. * ACPI driver
  3925. *
  3926. *****************/
  3927. static int sony_pic_remove(struct acpi_device *device)
  3928. {
  3929. struct sony_pic_ioport *io, *tmp_io;
  3930. struct sony_pic_irq *irq, *tmp_irq;
  3931. if (sony_pic_disable(device)) {
  3932. pr_err("Couldn't disable device\n");
  3933. return -ENXIO;
  3934. }
  3935. free_irq(spic_dev.cur_irq->irq.interrupts[0], &spic_dev);
  3936. release_region(spic_dev.cur_ioport->io1.minimum,
  3937. spic_dev.cur_ioport->io1.address_length);
  3938. if (spic_dev.cur_ioport->io2.minimum)
  3939. release_region(spic_dev.cur_ioport->io2.minimum,
  3940. spic_dev.cur_ioport->io2.address_length);
  3941. sonypi_compat_exit();
  3942. sony_laptop_remove_input();
  3943. /* pf attrs */
  3944. sysfs_remove_group(&sony_pf_device->dev.kobj, &spic_attribute_group);
  3945. sony_pf_remove();
  3946. list_for_each_entry_safe(io, tmp_io, &spic_dev.ioports, list) {
  3947. list_del(&io->list);
  3948. kfree(io);
  3949. }
  3950. list_for_each_entry_safe(irq, tmp_irq, &spic_dev.interrupts, list) {
  3951. list_del(&irq->list);
  3952. kfree(irq);
  3953. }
  3954. spic_dev.cur_ioport = NULL;
  3955. spic_dev.cur_irq = NULL;
  3956. dprintk(SONY_PIC_DRIVER_NAME " removed.\n");
  3957. return 0;
  3958. }
  3959. static int sony_pic_add(struct acpi_device *device)
  3960. {
  3961. int result;
  3962. struct sony_pic_ioport *io, *tmp_io;
  3963. struct sony_pic_irq *irq, *tmp_irq;
  3964. spic_dev.acpi_dev = device;
  3965. strcpy(acpi_device_class(device), "sony/hotkey");
  3966. sony_pic_detect_device_type(&spic_dev);
  3967. mutex_init(&spic_dev.lock);
  3968. /* read _PRS resources */
  3969. result = sony_pic_possible_resources(device);
  3970. if (result) {
  3971. pr_err("Unable to read possible resources\n");
  3972. goto err_free_resources;
  3973. }
  3974. /* setup input devices and helper fifo */
  3975. result = sony_laptop_setup_input(device);
  3976. if (result) {
  3977. pr_err("Unable to create input devices\n");
  3978. goto err_free_resources;
  3979. }
  3980. result = sonypi_compat_init();
  3981. if (result)
  3982. goto err_remove_input;
  3983. /* request io port */
  3984. list_for_each_entry_reverse(io, &spic_dev.ioports, list) {
  3985. if (request_region(io->io1.minimum, io->io1.address_length,
  3986. "Sony Programmable I/O Device")) {
  3987. dprintk("I/O port1: 0x%.4x (0x%.4x) + 0x%.2x\n",
  3988. io->io1.minimum, io->io1.maximum,
  3989. io->io1.address_length);
  3990. /* Type 1 have 2 ioports */
  3991. if (io->io2.minimum) {
  3992. if (request_region(io->io2.minimum,
  3993. io->io2.address_length,
  3994. "Sony Programmable I/O Device")) {
  3995. dprintk("I/O port2: 0x%.4x (0x%.4x) + 0x%.2x\n",
  3996. io->io2.minimum, io->io2.maximum,
  3997. io->io2.address_length);
  3998. spic_dev.cur_ioport = io;
  3999. break;
  4000. }
  4001. else {
  4002. dprintk("Unable to get I/O port2: "
  4003. "0x%.4x (0x%.4x) + 0x%.2x\n",
  4004. io->io2.minimum, io->io2.maximum,
  4005. io->io2.address_length);
  4006. release_region(io->io1.minimum,
  4007. io->io1.address_length);
  4008. }
  4009. }
  4010. else {
  4011. spic_dev.cur_ioport = io;
  4012. break;
  4013. }
  4014. }
  4015. }
  4016. if (!spic_dev.cur_ioport) {
  4017. pr_err("Failed to request_region\n");
  4018. result = -ENODEV;
  4019. goto err_remove_compat;
  4020. }
  4021. /* request IRQ */
  4022. list_for_each_entry_reverse(irq, &spic_dev.interrupts, list) {
  4023. if (!request_irq(irq->irq.interrupts[0], sony_pic_irq,
  4024. 0, "sony-laptop", &spic_dev)) {
  4025. dprintk("IRQ: %d - triggering: %d - "
  4026. "polarity: %d - shr: %d\n",
  4027. irq->irq.interrupts[0],
  4028. irq->irq.triggering,
  4029. irq->irq.polarity,
  4030. irq->irq.sharable);
  4031. spic_dev.cur_irq = irq;
  4032. break;
  4033. }
  4034. }
  4035. if (!spic_dev.cur_irq) {
  4036. pr_err("Failed to request_irq\n");
  4037. result = -ENODEV;
  4038. goto err_release_region;
  4039. }
  4040. /* set resource status _SRS */
  4041. result = sony_pic_enable(device, spic_dev.cur_ioport, spic_dev.cur_irq);
  4042. if (result) {
  4043. pr_err("Couldn't enable device\n");
  4044. goto err_free_irq;
  4045. }
  4046. spic_dev.bluetooth_power = -1;
  4047. /* create device attributes */
  4048. result = sony_pf_add();
  4049. if (result)
  4050. goto err_disable_device;
  4051. result = sysfs_create_group(&sony_pf_device->dev.kobj, &spic_attribute_group);
  4052. if (result)
  4053. goto err_remove_pf;
  4054. pr_info("SPIC setup done.\n");
  4055. return 0;
  4056. err_remove_pf:
  4057. sony_pf_remove();
  4058. err_disable_device:
  4059. sony_pic_disable(device);
  4060. err_free_irq:
  4061. free_irq(spic_dev.cur_irq->irq.interrupts[0], &spic_dev);
  4062. err_release_region:
  4063. release_region(spic_dev.cur_ioport->io1.minimum,
  4064. spic_dev.cur_ioport->io1.address_length);
  4065. if (spic_dev.cur_ioport->io2.minimum)
  4066. release_region(spic_dev.cur_ioport->io2.minimum,
  4067. spic_dev.cur_ioport->io2.address_length);
  4068. err_remove_compat:
  4069. sonypi_compat_exit();
  4070. err_remove_input:
  4071. sony_laptop_remove_input();
  4072. err_free_resources:
  4073. list_for_each_entry_safe(io, tmp_io, &spic_dev.ioports, list) {
  4074. list_del(&io->list);
  4075. kfree(io);
  4076. }
  4077. list_for_each_entry_safe(irq, tmp_irq, &spic_dev.interrupts, list) {
  4078. list_del(&irq->list);
  4079. kfree(irq);
  4080. }
  4081. spic_dev.cur_ioport = NULL;
  4082. spic_dev.cur_irq = NULL;
  4083. return result;
  4084. }
  4085. #ifdef CONFIG_PM_SLEEP
  4086. static int sony_pic_suspend(struct device *dev)
  4087. {
  4088. if (sony_pic_disable(to_acpi_device(dev)))
  4089. return -ENXIO;
  4090. return 0;
  4091. }
  4092. static int sony_pic_resume(struct device *dev)
  4093. {
  4094. sony_pic_enable(to_acpi_device(dev),
  4095. spic_dev.cur_ioport, spic_dev.cur_irq);
  4096. return 0;
  4097. }
  4098. #endif
  4099. static SIMPLE_DEV_PM_OPS(sony_pic_pm, sony_pic_suspend, sony_pic_resume);
  4100. static const struct acpi_device_id sony_pic_device_ids[] = {
  4101. {SONY_PIC_HID, 0},
  4102. {"", 0},
  4103. };
  4104. static struct acpi_driver sony_pic_driver = {
  4105. .name = SONY_PIC_DRIVER_NAME,
  4106. .class = SONY_PIC_CLASS,
  4107. .ids = sony_pic_device_ids,
  4108. .owner = THIS_MODULE,
  4109. .ops = {
  4110. .add = sony_pic_add,
  4111. .remove = sony_pic_remove,
  4112. },
  4113. .drv.pm = &sony_pic_pm,
  4114. };
  4115. static const struct dmi_system_id sonypi_dmi_table[] __initconst = {
  4116. {
  4117. .ident = "Sony Vaio",
  4118. .matches = {
  4119. DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
  4120. DMI_MATCH(DMI_PRODUCT_NAME, "PCG-"),
  4121. },
  4122. },
  4123. {
  4124. .ident = "Sony Vaio",
  4125. .matches = {
  4126. DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
  4127. DMI_MATCH(DMI_PRODUCT_NAME, "VGN-"),
  4128. },
  4129. },
  4130. { }
  4131. };
  4132. static int __init sony_laptop_init(void)
  4133. {
  4134. int result;
  4135. if (!no_spic && dmi_check_system(sonypi_dmi_table)) {
  4136. result = acpi_bus_register_driver(&sony_pic_driver);
  4137. if (result) {
  4138. pr_err("Unable to register SPIC driver\n");
  4139. goto out;
  4140. }
  4141. spic_drv_registered = 1;
  4142. }
  4143. result = acpi_bus_register_driver(&sony_nc_driver);
  4144. if (result) {
  4145. pr_err("Unable to register SNC driver\n");
  4146. goto out_unregister_pic;
  4147. }
  4148. return 0;
  4149. out_unregister_pic:
  4150. if (spic_drv_registered)
  4151. acpi_bus_unregister_driver(&sony_pic_driver);
  4152. out:
  4153. return result;
  4154. }
  4155. static void __exit sony_laptop_exit(void)
  4156. {
  4157. acpi_bus_unregister_driver(&sony_nc_driver);
  4158. if (spic_drv_registered)
  4159. acpi_bus_unregister_driver(&sony_pic_driver);
  4160. }
  4161. module_init(sony_laptop_init);
  4162. module_exit(sony_laptop_exit);