sony-laptop.c 120 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938
  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 <asm/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 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(unsigned long 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. setup_timer(&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 = 0;
  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. if (sony_call_snc_handle(sony_rfkill_handle, 0x200, &result) < 0) {
  1427. rfkill_destroy(rfk);
  1428. return -1;
  1429. }
  1430. hwblock = !(result & 0x1);
  1431. if (sony_call_snc_handle(sony_rfkill_handle,
  1432. sony_rfkill_address[nc_type],
  1433. &result) < 0) {
  1434. rfkill_destroy(rfk);
  1435. return -1;
  1436. }
  1437. swblock = !(result & 0x2);
  1438. rfkill_init_sw_state(rfk, swblock);
  1439. rfkill_set_hw_state(rfk, hwblock);
  1440. err = rfkill_register(rfk);
  1441. if (err) {
  1442. rfkill_destroy(rfk);
  1443. return err;
  1444. }
  1445. sony_rfkill_devices[nc_type] = rfk;
  1446. return err;
  1447. }
  1448. static void sony_nc_rfkill_update(void)
  1449. {
  1450. enum sony_nc_rfkill i;
  1451. int result;
  1452. bool hwblock;
  1453. sony_call_snc_handle(sony_rfkill_handle, 0x200, &result);
  1454. hwblock = !(result & 0x1);
  1455. for (i = 0; i < N_SONY_RFKILL; i++) {
  1456. int argument = sony_rfkill_address[i];
  1457. if (!sony_rfkill_devices[i])
  1458. continue;
  1459. if (hwblock) {
  1460. if (rfkill_set_hw_state(sony_rfkill_devices[i], true)) {
  1461. /* we already know we're blocked */
  1462. }
  1463. continue;
  1464. }
  1465. sony_call_snc_handle(sony_rfkill_handle, argument, &result);
  1466. rfkill_set_states(sony_rfkill_devices[i],
  1467. !(result & 0x2), false);
  1468. }
  1469. }
  1470. static int sony_nc_rfkill_setup(struct acpi_device *device,
  1471. unsigned int handle)
  1472. {
  1473. u64 offset;
  1474. int i;
  1475. unsigned char buffer[32] = { 0 };
  1476. offset = sony_find_snc_handle(handle);
  1477. sony_rfkill_handle = handle;
  1478. i = sony_nc_buffer_call(sony_nc_acpi_handle, "SN06", &offset, buffer,
  1479. 32);
  1480. if (i < 0)
  1481. return i;
  1482. /* The buffer is filled with magic numbers describing the devices
  1483. * available, 0xff terminates the enumeration.
  1484. * Known codes:
  1485. * 0x00 WLAN
  1486. * 0x10 BLUETOOTH
  1487. * 0x20 WWAN GPRS-EDGE
  1488. * 0x21 WWAN HSDPA
  1489. * 0x22 WWAN EV-DO
  1490. * 0x23 WWAN GPS
  1491. * 0x25 Gobi WWAN no GPS
  1492. * 0x26 Gobi WWAN + GPS
  1493. * 0x28 Gobi WWAN no GPS
  1494. * 0x29 Gobi WWAN + GPS
  1495. * 0x30 WIMAX
  1496. * 0x50 Gobi WWAN no GPS
  1497. * 0x51 Gobi WWAN + GPS
  1498. * 0x70 no SIM card slot
  1499. * 0x71 SIM card slot
  1500. */
  1501. for (i = 0; i < ARRAY_SIZE(buffer); i++) {
  1502. if (buffer[i] == 0xff)
  1503. break;
  1504. dprintk("Radio devices, found 0x%.2x\n", buffer[i]);
  1505. if (buffer[i] == 0 && !sony_rfkill_devices[SONY_WIFI])
  1506. sony_nc_setup_rfkill(device, SONY_WIFI);
  1507. if (buffer[i] == 0x10 && !sony_rfkill_devices[SONY_BLUETOOTH])
  1508. sony_nc_setup_rfkill(device, SONY_BLUETOOTH);
  1509. if (((0xf0 & buffer[i]) == 0x20 ||
  1510. (0xf0 & buffer[i]) == 0x50) &&
  1511. !sony_rfkill_devices[SONY_WWAN])
  1512. sony_nc_setup_rfkill(device, SONY_WWAN);
  1513. if (buffer[i] == 0x30 && !sony_rfkill_devices[SONY_WIMAX])
  1514. sony_nc_setup_rfkill(device, SONY_WIMAX);
  1515. }
  1516. return 0;
  1517. }
  1518. /* Keyboard backlight feature */
  1519. struct kbd_backlight {
  1520. unsigned int handle;
  1521. unsigned int base;
  1522. unsigned int mode;
  1523. unsigned int timeout;
  1524. unsigned int has_timeout;
  1525. struct device_attribute mode_attr;
  1526. struct device_attribute timeout_attr;
  1527. };
  1528. static struct kbd_backlight *kbdbl_ctl;
  1529. static ssize_t __sony_nc_kbd_backlight_mode_set(u8 value)
  1530. {
  1531. int result;
  1532. if (value > 2)
  1533. return -EINVAL;
  1534. if (sony_call_snc_handle(kbdbl_ctl->handle,
  1535. (value << 0x10) | (kbdbl_ctl->base), &result))
  1536. return -EIO;
  1537. /* Try to turn the light on/off immediately */
  1538. if (value != 1)
  1539. sony_call_snc_handle(kbdbl_ctl->handle,
  1540. (value << 0x0f) | (kbdbl_ctl->base + 0x100),
  1541. &result);
  1542. kbdbl_ctl->mode = value;
  1543. return 0;
  1544. }
  1545. static ssize_t sony_nc_kbd_backlight_mode_store(struct device *dev,
  1546. struct device_attribute *attr,
  1547. const char *buffer, size_t count)
  1548. {
  1549. int ret = 0;
  1550. unsigned long value;
  1551. if (count > 31)
  1552. return -EINVAL;
  1553. if (kstrtoul(buffer, 10, &value))
  1554. return -EINVAL;
  1555. ret = __sony_nc_kbd_backlight_mode_set(value);
  1556. if (ret < 0)
  1557. return ret;
  1558. return count;
  1559. }
  1560. static ssize_t sony_nc_kbd_backlight_mode_show(struct device *dev,
  1561. struct device_attribute *attr, char *buffer)
  1562. {
  1563. ssize_t count = 0;
  1564. count = snprintf(buffer, PAGE_SIZE, "%d\n", kbdbl_ctl->mode);
  1565. return count;
  1566. }
  1567. static int __sony_nc_kbd_backlight_timeout_set(u8 value)
  1568. {
  1569. int result;
  1570. if (value > 3)
  1571. return -EINVAL;
  1572. if (sony_call_snc_handle(kbdbl_ctl->handle, (value << 0x10) |
  1573. (kbdbl_ctl->base + 0x200), &result))
  1574. return -EIO;
  1575. kbdbl_ctl->timeout = value;
  1576. return 0;
  1577. }
  1578. static ssize_t sony_nc_kbd_backlight_timeout_store(struct device *dev,
  1579. struct device_attribute *attr,
  1580. const char *buffer, size_t count)
  1581. {
  1582. int ret = 0;
  1583. unsigned long value;
  1584. if (count > 31)
  1585. return -EINVAL;
  1586. if (kstrtoul(buffer, 10, &value))
  1587. return -EINVAL;
  1588. ret = __sony_nc_kbd_backlight_timeout_set(value);
  1589. if (ret < 0)
  1590. return ret;
  1591. return count;
  1592. }
  1593. static ssize_t sony_nc_kbd_backlight_timeout_show(struct device *dev,
  1594. struct device_attribute *attr, char *buffer)
  1595. {
  1596. ssize_t count = 0;
  1597. count = snprintf(buffer, PAGE_SIZE, "%d\n", kbdbl_ctl->timeout);
  1598. return count;
  1599. }
  1600. static int sony_nc_kbd_backlight_setup(struct platform_device *pd,
  1601. unsigned int handle)
  1602. {
  1603. int result;
  1604. int probe_base = 0;
  1605. int ctl_base = 0;
  1606. int ret = 0;
  1607. if (kbdbl_ctl) {
  1608. pr_warn("handle 0x%.4x: keyboard backlight setup already done for 0x%.4x\n",
  1609. handle, kbdbl_ctl->handle);
  1610. return -EBUSY;
  1611. }
  1612. /* verify the kbd backlight presence, some of these handles are not used
  1613. * for keyboard backlight only
  1614. */
  1615. switch (handle) {
  1616. case 0x0153:
  1617. probe_base = 0x0;
  1618. ctl_base = 0x0;
  1619. break;
  1620. case 0x0137:
  1621. probe_base = 0x0B00;
  1622. ctl_base = 0x0C00;
  1623. break;
  1624. default:
  1625. probe_base = 0x0100;
  1626. ctl_base = 0x4000;
  1627. break;
  1628. }
  1629. ret = sony_call_snc_handle(handle, probe_base, &result);
  1630. if (ret)
  1631. return ret;
  1632. if ((handle == 0x0137 && !(result & 0x02)) ||
  1633. !(result & 0x01)) {
  1634. dprintk("no backlight keyboard found\n");
  1635. return 0;
  1636. }
  1637. kbdbl_ctl = kzalloc(sizeof(*kbdbl_ctl), GFP_KERNEL);
  1638. if (!kbdbl_ctl)
  1639. return -ENOMEM;
  1640. kbdbl_ctl->mode = kbd_backlight;
  1641. kbdbl_ctl->timeout = kbd_backlight_timeout;
  1642. kbdbl_ctl->handle = handle;
  1643. kbdbl_ctl->base = ctl_base;
  1644. /* Some models do not allow timeout control */
  1645. kbdbl_ctl->has_timeout = handle != 0x0153;
  1646. sysfs_attr_init(&kbdbl_ctl->mode_attr.attr);
  1647. kbdbl_ctl->mode_attr.attr.name = "kbd_backlight";
  1648. kbdbl_ctl->mode_attr.attr.mode = S_IRUGO | S_IWUSR;
  1649. kbdbl_ctl->mode_attr.show = sony_nc_kbd_backlight_mode_show;
  1650. kbdbl_ctl->mode_attr.store = sony_nc_kbd_backlight_mode_store;
  1651. ret = device_create_file(&pd->dev, &kbdbl_ctl->mode_attr);
  1652. if (ret)
  1653. goto outkzalloc;
  1654. __sony_nc_kbd_backlight_mode_set(kbdbl_ctl->mode);
  1655. if (kbdbl_ctl->has_timeout) {
  1656. sysfs_attr_init(&kbdbl_ctl->timeout_attr.attr);
  1657. kbdbl_ctl->timeout_attr.attr.name = "kbd_backlight_timeout";
  1658. kbdbl_ctl->timeout_attr.attr.mode = S_IRUGO | S_IWUSR;
  1659. kbdbl_ctl->timeout_attr.show =
  1660. sony_nc_kbd_backlight_timeout_show;
  1661. kbdbl_ctl->timeout_attr.store =
  1662. sony_nc_kbd_backlight_timeout_store;
  1663. ret = device_create_file(&pd->dev, &kbdbl_ctl->timeout_attr);
  1664. if (ret)
  1665. goto outmode;
  1666. __sony_nc_kbd_backlight_timeout_set(kbdbl_ctl->timeout);
  1667. }
  1668. return 0;
  1669. outmode:
  1670. device_remove_file(&pd->dev, &kbdbl_ctl->mode_attr);
  1671. outkzalloc:
  1672. kfree(kbdbl_ctl);
  1673. kbdbl_ctl = NULL;
  1674. return ret;
  1675. }
  1676. static void sony_nc_kbd_backlight_cleanup(struct platform_device *pd,
  1677. unsigned int handle)
  1678. {
  1679. if (kbdbl_ctl && handle == kbdbl_ctl->handle) {
  1680. device_remove_file(&pd->dev, &kbdbl_ctl->mode_attr);
  1681. if (kbdbl_ctl->has_timeout)
  1682. device_remove_file(&pd->dev, &kbdbl_ctl->timeout_attr);
  1683. kfree(kbdbl_ctl);
  1684. kbdbl_ctl = NULL;
  1685. }
  1686. }
  1687. struct battery_care_control {
  1688. struct device_attribute attrs[2];
  1689. unsigned int handle;
  1690. };
  1691. static struct battery_care_control *bcare_ctl;
  1692. static ssize_t sony_nc_battery_care_limit_store(struct device *dev,
  1693. struct device_attribute *attr,
  1694. const char *buffer, size_t count)
  1695. {
  1696. unsigned int result, cmd;
  1697. unsigned long value;
  1698. if (count > 31)
  1699. return -EINVAL;
  1700. if (kstrtoul(buffer, 10, &value))
  1701. return -EINVAL;
  1702. /* limit values (2 bits):
  1703. * 00 - none
  1704. * 01 - 80%
  1705. * 10 - 50%
  1706. * 11 - 100%
  1707. *
  1708. * bit 0: 0 disable BCL, 1 enable BCL
  1709. * bit 1: 1 tell to store the battery limit (see bits 6,7) too
  1710. * bits 2,3: reserved
  1711. * bits 4,5: store the limit into the EC
  1712. * bits 6,7: store the limit into the battery
  1713. */
  1714. cmd = 0;
  1715. if (value > 0) {
  1716. if (value <= 50)
  1717. cmd = 0x20;
  1718. else if (value <= 80)
  1719. cmd = 0x10;
  1720. else if (value <= 100)
  1721. cmd = 0x30;
  1722. else
  1723. return -EINVAL;
  1724. /*
  1725. * handle 0x0115 should allow storing on battery too;
  1726. * handle 0x0136 same as 0x0115 + health status;
  1727. * handle 0x013f, same as 0x0136 but no storing on the battery
  1728. */
  1729. if (bcare_ctl->handle != 0x013f)
  1730. cmd = cmd | (cmd << 2);
  1731. cmd = (cmd | 0x1) << 0x10;
  1732. }
  1733. if (sony_call_snc_handle(bcare_ctl->handle, cmd | 0x0100, &result))
  1734. return -EIO;
  1735. return count;
  1736. }
  1737. static ssize_t sony_nc_battery_care_limit_show(struct device *dev,
  1738. struct device_attribute *attr, char *buffer)
  1739. {
  1740. unsigned int result, status;
  1741. if (sony_call_snc_handle(bcare_ctl->handle, 0x0000, &result))
  1742. return -EIO;
  1743. status = (result & 0x01) ? ((result & 0x30) >> 0x04) : 0;
  1744. switch (status) {
  1745. case 1:
  1746. status = 80;
  1747. break;
  1748. case 2:
  1749. status = 50;
  1750. break;
  1751. case 3:
  1752. status = 100;
  1753. break;
  1754. default:
  1755. status = 0;
  1756. break;
  1757. }
  1758. return snprintf(buffer, PAGE_SIZE, "%d\n", status);
  1759. }
  1760. static ssize_t sony_nc_battery_care_health_show(struct device *dev,
  1761. struct device_attribute *attr, char *buffer)
  1762. {
  1763. ssize_t count = 0;
  1764. unsigned int health;
  1765. if (sony_call_snc_handle(bcare_ctl->handle, 0x0200, &health))
  1766. return -EIO;
  1767. count = snprintf(buffer, PAGE_SIZE, "%d\n", health & 0xff);
  1768. return count;
  1769. }
  1770. static int sony_nc_battery_care_setup(struct platform_device *pd,
  1771. unsigned int handle)
  1772. {
  1773. int ret = 0;
  1774. bcare_ctl = kzalloc(sizeof(struct battery_care_control), GFP_KERNEL);
  1775. if (!bcare_ctl)
  1776. return -ENOMEM;
  1777. bcare_ctl->handle = handle;
  1778. sysfs_attr_init(&bcare_ctl->attrs[0].attr);
  1779. bcare_ctl->attrs[0].attr.name = "battery_care_limiter";
  1780. bcare_ctl->attrs[0].attr.mode = S_IRUGO | S_IWUSR;
  1781. bcare_ctl->attrs[0].show = sony_nc_battery_care_limit_show;
  1782. bcare_ctl->attrs[0].store = sony_nc_battery_care_limit_store;
  1783. ret = device_create_file(&pd->dev, &bcare_ctl->attrs[0]);
  1784. if (ret)
  1785. goto outkzalloc;
  1786. /* 0x0115 is for models with no health reporting capability */
  1787. if (handle == 0x0115)
  1788. return 0;
  1789. sysfs_attr_init(&bcare_ctl->attrs[1].attr);
  1790. bcare_ctl->attrs[1].attr.name = "battery_care_health";
  1791. bcare_ctl->attrs[1].attr.mode = S_IRUGO;
  1792. bcare_ctl->attrs[1].show = sony_nc_battery_care_health_show;
  1793. ret = device_create_file(&pd->dev, &bcare_ctl->attrs[1]);
  1794. if (ret)
  1795. goto outlimiter;
  1796. return 0;
  1797. outlimiter:
  1798. device_remove_file(&pd->dev, &bcare_ctl->attrs[0]);
  1799. outkzalloc:
  1800. kfree(bcare_ctl);
  1801. bcare_ctl = NULL;
  1802. return ret;
  1803. }
  1804. static void sony_nc_battery_care_cleanup(struct platform_device *pd)
  1805. {
  1806. if (bcare_ctl) {
  1807. device_remove_file(&pd->dev, &bcare_ctl->attrs[0]);
  1808. if (bcare_ctl->handle != 0x0115)
  1809. device_remove_file(&pd->dev, &bcare_ctl->attrs[1]);
  1810. kfree(bcare_ctl);
  1811. bcare_ctl = NULL;
  1812. }
  1813. }
  1814. struct snc_thermal_ctrl {
  1815. unsigned int mode;
  1816. unsigned int profiles;
  1817. struct device_attribute mode_attr;
  1818. struct device_attribute profiles_attr;
  1819. };
  1820. static struct snc_thermal_ctrl *th_handle;
  1821. #define THM_PROFILE_MAX 3
  1822. static const char * const snc_thermal_profiles[] = {
  1823. "balanced",
  1824. "silent",
  1825. "performance"
  1826. };
  1827. static int sony_nc_thermal_mode_set(unsigned short mode)
  1828. {
  1829. unsigned int result;
  1830. /* the thermal profile seems to be a two bit bitmask:
  1831. * lsb -> silent
  1832. * msb -> performance
  1833. * no bit set is the normal operation and is always valid
  1834. * Some vaio models only have "balanced" and "performance"
  1835. */
  1836. if ((mode && !(th_handle->profiles & mode)) || mode >= THM_PROFILE_MAX)
  1837. return -EINVAL;
  1838. if (sony_call_snc_handle(0x0122, mode << 0x10 | 0x0200, &result))
  1839. return -EIO;
  1840. th_handle->mode = mode;
  1841. return 0;
  1842. }
  1843. static int sony_nc_thermal_mode_get(void)
  1844. {
  1845. unsigned int result;
  1846. if (sony_call_snc_handle(0x0122, 0x0100, &result))
  1847. return -EIO;
  1848. return result & 0xff;
  1849. }
  1850. static ssize_t sony_nc_thermal_profiles_show(struct device *dev,
  1851. struct device_attribute *attr, char *buffer)
  1852. {
  1853. short cnt;
  1854. size_t idx = 0;
  1855. for (cnt = 0; cnt < THM_PROFILE_MAX; cnt++) {
  1856. if (!cnt || (th_handle->profiles & cnt))
  1857. idx += snprintf(buffer + idx, PAGE_SIZE - idx, "%s ",
  1858. snc_thermal_profiles[cnt]);
  1859. }
  1860. idx += snprintf(buffer + idx, PAGE_SIZE - idx, "\n");
  1861. return idx;
  1862. }
  1863. static ssize_t sony_nc_thermal_mode_store(struct device *dev,
  1864. struct device_attribute *attr,
  1865. const char *buffer, size_t count)
  1866. {
  1867. unsigned short cmd;
  1868. size_t len = count;
  1869. if (count == 0)
  1870. return -EINVAL;
  1871. /* skip the newline if present */
  1872. if (buffer[len - 1] == '\n')
  1873. len--;
  1874. for (cmd = 0; cmd < THM_PROFILE_MAX; cmd++)
  1875. if (strncmp(buffer, snc_thermal_profiles[cmd], len) == 0)
  1876. break;
  1877. if (sony_nc_thermal_mode_set(cmd))
  1878. return -EIO;
  1879. return count;
  1880. }
  1881. static ssize_t sony_nc_thermal_mode_show(struct device *dev,
  1882. struct device_attribute *attr, char *buffer)
  1883. {
  1884. ssize_t count = 0;
  1885. int mode = sony_nc_thermal_mode_get();
  1886. if (mode < 0)
  1887. return mode;
  1888. count = snprintf(buffer, PAGE_SIZE, "%s\n", snc_thermal_profiles[mode]);
  1889. return count;
  1890. }
  1891. static int sony_nc_thermal_setup(struct platform_device *pd)
  1892. {
  1893. int ret = 0;
  1894. th_handle = kzalloc(sizeof(struct snc_thermal_ctrl), GFP_KERNEL);
  1895. if (!th_handle)
  1896. return -ENOMEM;
  1897. ret = sony_call_snc_handle(0x0122, 0x0000, &th_handle->profiles);
  1898. if (ret) {
  1899. pr_warn("couldn't to read the thermal profiles\n");
  1900. goto outkzalloc;
  1901. }
  1902. ret = sony_nc_thermal_mode_get();
  1903. if (ret < 0) {
  1904. pr_warn("couldn't to read the current thermal profile");
  1905. goto outkzalloc;
  1906. }
  1907. th_handle->mode = ret;
  1908. sysfs_attr_init(&th_handle->profiles_attr.attr);
  1909. th_handle->profiles_attr.attr.name = "thermal_profiles";
  1910. th_handle->profiles_attr.attr.mode = S_IRUGO;
  1911. th_handle->profiles_attr.show = sony_nc_thermal_profiles_show;
  1912. sysfs_attr_init(&th_handle->mode_attr.attr);
  1913. th_handle->mode_attr.attr.name = "thermal_control";
  1914. th_handle->mode_attr.attr.mode = S_IRUGO | S_IWUSR;
  1915. th_handle->mode_attr.show = sony_nc_thermal_mode_show;
  1916. th_handle->mode_attr.store = sony_nc_thermal_mode_store;
  1917. ret = device_create_file(&pd->dev, &th_handle->profiles_attr);
  1918. if (ret)
  1919. goto outkzalloc;
  1920. ret = device_create_file(&pd->dev, &th_handle->mode_attr);
  1921. if (ret)
  1922. goto outprofiles;
  1923. return 0;
  1924. outprofiles:
  1925. device_remove_file(&pd->dev, &th_handle->profiles_attr);
  1926. outkzalloc:
  1927. kfree(th_handle);
  1928. th_handle = NULL;
  1929. return ret;
  1930. }
  1931. static void sony_nc_thermal_cleanup(struct platform_device *pd)
  1932. {
  1933. if (th_handle) {
  1934. device_remove_file(&pd->dev, &th_handle->profiles_attr);
  1935. device_remove_file(&pd->dev, &th_handle->mode_attr);
  1936. kfree(th_handle);
  1937. th_handle = NULL;
  1938. }
  1939. }
  1940. #ifdef CONFIG_PM_SLEEP
  1941. static void sony_nc_thermal_resume(void)
  1942. {
  1943. unsigned int status = sony_nc_thermal_mode_get();
  1944. if (status != th_handle->mode)
  1945. sony_nc_thermal_mode_set(th_handle->mode);
  1946. }
  1947. #endif
  1948. /* resume on LID open */
  1949. #define LID_RESUME_S5 0
  1950. #define LID_RESUME_S4 1
  1951. #define LID_RESUME_S3 2
  1952. #define LID_RESUME_MAX 3
  1953. struct snc_lid_resume_control {
  1954. struct device_attribute attrs[LID_RESUME_MAX];
  1955. unsigned int status;
  1956. int handle;
  1957. };
  1958. static struct snc_lid_resume_control *lid_ctl;
  1959. static ssize_t sony_nc_lid_resume_store(struct device *dev,
  1960. struct device_attribute *attr,
  1961. const char *buffer, size_t count)
  1962. {
  1963. unsigned int result;
  1964. unsigned long value;
  1965. unsigned int pos = LID_RESUME_S5;
  1966. if (count > 31)
  1967. return -EINVAL;
  1968. if (kstrtoul(buffer, 10, &value) || value > 1)
  1969. return -EINVAL;
  1970. /* the value we have to write to SNC is a bitmask:
  1971. * +--------------+
  1972. * | S3 | S4 | S5 |
  1973. * +--------------+
  1974. * 2 1 0
  1975. */
  1976. while (pos < LID_RESUME_MAX) {
  1977. if (&lid_ctl->attrs[pos].attr == &attr->attr)
  1978. break;
  1979. pos++;
  1980. }
  1981. if (pos == LID_RESUME_MAX)
  1982. return -EINVAL;
  1983. if (value)
  1984. value = lid_ctl->status | (1 << pos);
  1985. else
  1986. value = lid_ctl->status & ~(1 << pos);
  1987. if (sony_call_snc_handle(lid_ctl->handle, value << 0x10 | 0x0100,
  1988. &result))
  1989. return -EIO;
  1990. lid_ctl->status = value;
  1991. return count;
  1992. }
  1993. static ssize_t sony_nc_lid_resume_show(struct device *dev,
  1994. struct device_attribute *attr,
  1995. char *buffer)
  1996. {
  1997. unsigned int pos = LID_RESUME_S5;
  1998. while (pos < LID_RESUME_MAX) {
  1999. if (&lid_ctl->attrs[pos].attr == &attr->attr)
  2000. return snprintf(buffer, PAGE_SIZE, "%d\n",
  2001. (lid_ctl->status >> pos) & 0x01);
  2002. pos++;
  2003. }
  2004. return -EINVAL;
  2005. }
  2006. static int sony_nc_lid_resume_setup(struct platform_device *pd,
  2007. unsigned int handle)
  2008. {
  2009. unsigned int result;
  2010. int i;
  2011. if (sony_call_snc_handle(handle, 0x0000, &result))
  2012. return -EIO;
  2013. lid_ctl = kzalloc(sizeof(struct snc_lid_resume_control), GFP_KERNEL);
  2014. if (!lid_ctl)
  2015. return -ENOMEM;
  2016. lid_ctl->status = result & 0x7;
  2017. lid_ctl->handle = handle;
  2018. sysfs_attr_init(&lid_ctl->attrs[0].attr);
  2019. lid_ctl->attrs[LID_RESUME_S5].attr.name = "lid_resume_S5";
  2020. lid_ctl->attrs[LID_RESUME_S5].attr.mode = S_IRUGO | S_IWUSR;
  2021. lid_ctl->attrs[LID_RESUME_S5].show = sony_nc_lid_resume_show;
  2022. lid_ctl->attrs[LID_RESUME_S5].store = sony_nc_lid_resume_store;
  2023. if (handle == 0x0119) {
  2024. sysfs_attr_init(&lid_ctl->attrs[1].attr);
  2025. lid_ctl->attrs[LID_RESUME_S4].attr.name = "lid_resume_S4";
  2026. lid_ctl->attrs[LID_RESUME_S4].attr.mode = S_IRUGO | S_IWUSR;
  2027. lid_ctl->attrs[LID_RESUME_S4].show = sony_nc_lid_resume_show;
  2028. lid_ctl->attrs[LID_RESUME_S4].store = sony_nc_lid_resume_store;
  2029. sysfs_attr_init(&lid_ctl->attrs[2].attr);
  2030. lid_ctl->attrs[LID_RESUME_S3].attr.name = "lid_resume_S3";
  2031. lid_ctl->attrs[LID_RESUME_S3].attr.mode = S_IRUGO | S_IWUSR;
  2032. lid_ctl->attrs[LID_RESUME_S3].show = sony_nc_lid_resume_show;
  2033. lid_ctl->attrs[LID_RESUME_S3].store = sony_nc_lid_resume_store;
  2034. }
  2035. for (i = 0; i < LID_RESUME_MAX &&
  2036. lid_ctl->attrs[i].attr.name; i++) {
  2037. result = device_create_file(&pd->dev, &lid_ctl->attrs[i]);
  2038. if (result)
  2039. goto liderror;
  2040. }
  2041. return 0;
  2042. liderror:
  2043. for (i--; i >= 0; i--)
  2044. device_remove_file(&pd->dev, &lid_ctl->attrs[i]);
  2045. kfree(lid_ctl);
  2046. lid_ctl = NULL;
  2047. return result;
  2048. }
  2049. static void sony_nc_lid_resume_cleanup(struct platform_device *pd)
  2050. {
  2051. int i;
  2052. if (lid_ctl) {
  2053. for (i = 0; i < LID_RESUME_MAX; i++) {
  2054. if (!lid_ctl->attrs[i].attr.name)
  2055. break;
  2056. device_remove_file(&pd->dev, &lid_ctl->attrs[i]);
  2057. }
  2058. kfree(lid_ctl);
  2059. lid_ctl = NULL;
  2060. }
  2061. }
  2062. /* GFX Switch position */
  2063. enum gfx_switch {
  2064. SPEED,
  2065. STAMINA,
  2066. AUTO
  2067. };
  2068. struct snc_gfx_switch_control {
  2069. struct device_attribute attr;
  2070. unsigned int handle;
  2071. };
  2072. static struct snc_gfx_switch_control *gfxs_ctl;
  2073. /* returns 0 for speed, 1 for stamina */
  2074. static int __sony_nc_gfx_switch_status_get(void)
  2075. {
  2076. unsigned int result;
  2077. if (sony_call_snc_handle(gfxs_ctl->handle,
  2078. gfxs_ctl->handle == 0x015B ? 0x0000 : 0x0100,
  2079. &result))
  2080. return -EIO;
  2081. switch (gfxs_ctl->handle) {
  2082. case 0x0146:
  2083. /* 1: discrete GFX (speed)
  2084. * 0: integrated GFX (stamina)
  2085. */
  2086. return result & 0x1 ? SPEED : STAMINA;
  2087. break;
  2088. case 0x015B:
  2089. /* 0: discrete GFX (speed)
  2090. * 1: integrated GFX (stamina)
  2091. */
  2092. return result & 0x1 ? STAMINA : SPEED;
  2093. break;
  2094. case 0x0128:
  2095. /* it's a more elaborated bitmask, for now:
  2096. * 2: integrated GFX (stamina)
  2097. * 0: discrete GFX (speed)
  2098. */
  2099. dprintk("GFX Status: 0x%x\n", result);
  2100. return result & 0x80 ? AUTO :
  2101. result & 0x02 ? STAMINA : SPEED;
  2102. break;
  2103. }
  2104. return -EINVAL;
  2105. }
  2106. static ssize_t sony_nc_gfx_switch_status_show(struct device *dev,
  2107. struct device_attribute *attr,
  2108. char *buffer)
  2109. {
  2110. int pos = __sony_nc_gfx_switch_status_get();
  2111. if (pos < 0)
  2112. return pos;
  2113. return snprintf(buffer, PAGE_SIZE, "%s\n",
  2114. pos == SPEED ? "speed" :
  2115. pos == STAMINA ? "stamina" :
  2116. pos == AUTO ? "auto" : "unknown");
  2117. }
  2118. static int sony_nc_gfx_switch_setup(struct platform_device *pd,
  2119. unsigned int handle)
  2120. {
  2121. unsigned int result;
  2122. gfxs_ctl = kzalloc(sizeof(struct snc_gfx_switch_control), GFP_KERNEL);
  2123. if (!gfxs_ctl)
  2124. return -ENOMEM;
  2125. gfxs_ctl->handle = handle;
  2126. sysfs_attr_init(&gfxs_ctl->attr.attr);
  2127. gfxs_ctl->attr.attr.name = "gfx_switch_status";
  2128. gfxs_ctl->attr.attr.mode = S_IRUGO;
  2129. gfxs_ctl->attr.show = sony_nc_gfx_switch_status_show;
  2130. result = device_create_file(&pd->dev, &gfxs_ctl->attr);
  2131. if (result)
  2132. goto gfxerror;
  2133. return 0;
  2134. gfxerror:
  2135. kfree(gfxs_ctl);
  2136. gfxs_ctl = NULL;
  2137. return result;
  2138. }
  2139. static void sony_nc_gfx_switch_cleanup(struct platform_device *pd)
  2140. {
  2141. if (gfxs_ctl) {
  2142. device_remove_file(&pd->dev, &gfxs_ctl->attr);
  2143. kfree(gfxs_ctl);
  2144. gfxs_ctl = NULL;
  2145. }
  2146. }
  2147. /* High speed charging function */
  2148. static struct device_attribute *hsc_handle;
  2149. static ssize_t sony_nc_highspeed_charging_store(struct device *dev,
  2150. struct device_attribute *attr,
  2151. const char *buffer, size_t count)
  2152. {
  2153. unsigned int result;
  2154. unsigned long value;
  2155. if (count > 31)
  2156. return -EINVAL;
  2157. if (kstrtoul(buffer, 10, &value) || value > 1)
  2158. return -EINVAL;
  2159. if (sony_call_snc_handle(0x0131, value << 0x10 | 0x0200, &result))
  2160. return -EIO;
  2161. return count;
  2162. }
  2163. static ssize_t sony_nc_highspeed_charging_show(struct device *dev,
  2164. struct device_attribute *attr, char *buffer)
  2165. {
  2166. unsigned int result;
  2167. if (sony_call_snc_handle(0x0131, 0x0100, &result))
  2168. return -EIO;
  2169. return snprintf(buffer, PAGE_SIZE, "%d\n", result & 0x01);
  2170. }
  2171. static int sony_nc_highspeed_charging_setup(struct platform_device *pd)
  2172. {
  2173. unsigned int result;
  2174. if (sony_call_snc_handle(0x0131, 0x0000, &result) || !(result & 0x01)) {
  2175. /* some models advertise the handle but have no implementation
  2176. * for it
  2177. */
  2178. pr_info("No High Speed Charging capability found\n");
  2179. return 0;
  2180. }
  2181. hsc_handle = kzalloc(sizeof(struct device_attribute), GFP_KERNEL);
  2182. if (!hsc_handle)
  2183. return -ENOMEM;
  2184. sysfs_attr_init(&hsc_handle->attr);
  2185. hsc_handle->attr.name = "battery_highspeed_charging";
  2186. hsc_handle->attr.mode = S_IRUGO | S_IWUSR;
  2187. hsc_handle->show = sony_nc_highspeed_charging_show;
  2188. hsc_handle->store = sony_nc_highspeed_charging_store;
  2189. result = device_create_file(&pd->dev, hsc_handle);
  2190. if (result) {
  2191. kfree(hsc_handle);
  2192. hsc_handle = NULL;
  2193. return result;
  2194. }
  2195. return 0;
  2196. }
  2197. static void sony_nc_highspeed_charging_cleanup(struct platform_device *pd)
  2198. {
  2199. if (hsc_handle) {
  2200. device_remove_file(&pd->dev, hsc_handle);
  2201. kfree(hsc_handle);
  2202. hsc_handle = NULL;
  2203. }
  2204. }
  2205. /* low battery function */
  2206. static struct device_attribute *lowbatt_handle;
  2207. static ssize_t sony_nc_lowbatt_store(struct device *dev,
  2208. struct device_attribute *attr,
  2209. const char *buffer, size_t count)
  2210. {
  2211. unsigned int result;
  2212. unsigned long value;
  2213. if (count > 31)
  2214. return -EINVAL;
  2215. if (kstrtoul(buffer, 10, &value) || value > 1)
  2216. return -EINVAL;
  2217. if (sony_call_snc_handle(0x0121, value << 8, &result))
  2218. return -EIO;
  2219. return count;
  2220. }
  2221. static ssize_t sony_nc_lowbatt_show(struct device *dev,
  2222. struct device_attribute *attr, char *buffer)
  2223. {
  2224. unsigned int result;
  2225. if (sony_call_snc_handle(0x0121, 0x0200, &result))
  2226. return -EIO;
  2227. return snprintf(buffer, PAGE_SIZE, "%d\n", result & 1);
  2228. }
  2229. static int sony_nc_lowbatt_setup(struct platform_device *pd)
  2230. {
  2231. unsigned int result;
  2232. lowbatt_handle = kzalloc(sizeof(struct device_attribute), GFP_KERNEL);
  2233. if (!lowbatt_handle)
  2234. return -ENOMEM;
  2235. sysfs_attr_init(&lowbatt_handle->attr);
  2236. lowbatt_handle->attr.name = "lowbatt_hibernate";
  2237. lowbatt_handle->attr.mode = S_IRUGO | S_IWUSR;
  2238. lowbatt_handle->show = sony_nc_lowbatt_show;
  2239. lowbatt_handle->store = sony_nc_lowbatt_store;
  2240. result = device_create_file(&pd->dev, lowbatt_handle);
  2241. if (result) {
  2242. kfree(lowbatt_handle);
  2243. lowbatt_handle = NULL;
  2244. return result;
  2245. }
  2246. return 0;
  2247. }
  2248. static void sony_nc_lowbatt_cleanup(struct platform_device *pd)
  2249. {
  2250. if (lowbatt_handle) {
  2251. device_remove_file(&pd->dev, lowbatt_handle);
  2252. kfree(lowbatt_handle);
  2253. lowbatt_handle = NULL;
  2254. }
  2255. }
  2256. /* fan speed function */
  2257. static struct device_attribute *fan_handle, *hsf_handle;
  2258. static ssize_t sony_nc_hsfan_store(struct device *dev,
  2259. struct device_attribute *attr,
  2260. const char *buffer, size_t count)
  2261. {
  2262. unsigned int result;
  2263. unsigned long value;
  2264. if (count > 31)
  2265. return -EINVAL;
  2266. if (kstrtoul(buffer, 10, &value) || value > 1)
  2267. return -EINVAL;
  2268. if (sony_call_snc_handle(0x0149, value << 0x10 | 0x0200, &result))
  2269. return -EIO;
  2270. return count;
  2271. }
  2272. static ssize_t sony_nc_hsfan_show(struct device *dev,
  2273. struct device_attribute *attr, char *buffer)
  2274. {
  2275. unsigned int result;
  2276. if (sony_call_snc_handle(0x0149, 0x0100, &result))
  2277. return -EIO;
  2278. return snprintf(buffer, PAGE_SIZE, "%d\n", result & 0x01);
  2279. }
  2280. static ssize_t sony_nc_fanspeed_show(struct device *dev,
  2281. struct device_attribute *attr, char *buffer)
  2282. {
  2283. unsigned int result;
  2284. if (sony_call_snc_handle(0x0149, 0x0300, &result))
  2285. return -EIO;
  2286. return snprintf(buffer, PAGE_SIZE, "%d\n", result & 0xff);
  2287. }
  2288. static int sony_nc_fanspeed_setup(struct platform_device *pd)
  2289. {
  2290. unsigned int result;
  2291. fan_handle = kzalloc(sizeof(struct device_attribute), GFP_KERNEL);
  2292. if (!fan_handle)
  2293. return -ENOMEM;
  2294. hsf_handle = kzalloc(sizeof(struct device_attribute), GFP_KERNEL);
  2295. if (!hsf_handle) {
  2296. result = -ENOMEM;
  2297. goto out_hsf_handle_alloc;
  2298. }
  2299. sysfs_attr_init(&fan_handle->attr);
  2300. fan_handle->attr.name = "fanspeed";
  2301. fan_handle->attr.mode = S_IRUGO;
  2302. fan_handle->show = sony_nc_fanspeed_show;
  2303. fan_handle->store = NULL;
  2304. sysfs_attr_init(&hsf_handle->attr);
  2305. hsf_handle->attr.name = "fan_forced";
  2306. hsf_handle->attr.mode = S_IRUGO | S_IWUSR;
  2307. hsf_handle->show = sony_nc_hsfan_show;
  2308. hsf_handle->store = sony_nc_hsfan_store;
  2309. result = device_create_file(&pd->dev, fan_handle);
  2310. if (result)
  2311. goto out_fan_handle;
  2312. result = device_create_file(&pd->dev, hsf_handle);
  2313. if (result)
  2314. goto out_hsf_handle;
  2315. return 0;
  2316. out_hsf_handle:
  2317. device_remove_file(&pd->dev, fan_handle);
  2318. out_fan_handle:
  2319. kfree(hsf_handle);
  2320. hsf_handle = NULL;
  2321. out_hsf_handle_alloc:
  2322. kfree(fan_handle);
  2323. fan_handle = NULL;
  2324. return result;
  2325. }
  2326. static void sony_nc_fanspeed_cleanup(struct platform_device *pd)
  2327. {
  2328. if (fan_handle) {
  2329. device_remove_file(&pd->dev, fan_handle);
  2330. kfree(fan_handle);
  2331. fan_handle = NULL;
  2332. }
  2333. if (hsf_handle) {
  2334. device_remove_file(&pd->dev, hsf_handle);
  2335. kfree(hsf_handle);
  2336. hsf_handle = NULL;
  2337. }
  2338. }
  2339. /* USB charge function */
  2340. static struct device_attribute *uc_handle;
  2341. static ssize_t sony_nc_usb_charge_store(struct device *dev,
  2342. struct device_attribute *attr,
  2343. const char *buffer, size_t count)
  2344. {
  2345. unsigned int result;
  2346. unsigned long value;
  2347. if (count > 31)
  2348. return -EINVAL;
  2349. if (kstrtoul(buffer, 10, &value) || value > 1)
  2350. return -EINVAL;
  2351. if (sony_call_snc_handle(0x0155, value << 0x10 | 0x0100, &result))
  2352. return -EIO;
  2353. return count;
  2354. }
  2355. static ssize_t sony_nc_usb_charge_show(struct device *dev,
  2356. struct device_attribute *attr, char *buffer)
  2357. {
  2358. unsigned int result;
  2359. if (sony_call_snc_handle(0x0155, 0x0000, &result))
  2360. return -EIO;
  2361. return snprintf(buffer, PAGE_SIZE, "%d\n", result & 0x01);
  2362. }
  2363. static int sony_nc_usb_charge_setup(struct platform_device *pd)
  2364. {
  2365. unsigned int result;
  2366. if (sony_call_snc_handle(0x0155, 0x0000, &result) || !(result & 0x01)) {
  2367. /* some models advertise the handle but have no implementation
  2368. * for it
  2369. */
  2370. pr_info("No USB Charge capability found\n");
  2371. return 0;
  2372. }
  2373. uc_handle = kzalloc(sizeof(struct device_attribute), GFP_KERNEL);
  2374. if (!uc_handle)
  2375. return -ENOMEM;
  2376. sysfs_attr_init(&uc_handle->attr);
  2377. uc_handle->attr.name = "usb_charge";
  2378. uc_handle->attr.mode = S_IRUGO | S_IWUSR;
  2379. uc_handle->show = sony_nc_usb_charge_show;
  2380. uc_handle->store = sony_nc_usb_charge_store;
  2381. result = device_create_file(&pd->dev, uc_handle);
  2382. if (result) {
  2383. kfree(uc_handle);
  2384. uc_handle = NULL;
  2385. return result;
  2386. }
  2387. return 0;
  2388. }
  2389. static void sony_nc_usb_charge_cleanup(struct platform_device *pd)
  2390. {
  2391. if (uc_handle) {
  2392. device_remove_file(&pd->dev, uc_handle);
  2393. kfree(uc_handle);
  2394. uc_handle = NULL;
  2395. }
  2396. }
  2397. /* Panel ID function */
  2398. static struct device_attribute *panel_handle;
  2399. static ssize_t sony_nc_panelid_show(struct device *dev,
  2400. struct device_attribute *attr, char *buffer)
  2401. {
  2402. unsigned int result;
  2403. if (sony_call_snc_handle(0x011D, 0x0000, &result))
  2404. return -EIO;
  2405. return snprintf(buffer, PAGE_SIZE, "%d\n", result);
  2406. }
  2407. static int sony_nc_panelid_setup(struct platform_device *pd)
  2408. {
  2409. unsigned int result;
  2410. panel_handle = kzalloc(sizeof(struct device_attribute), GFP_KERNEL);
  2411. if (!panel_handle)
  2412. return -ENOMEM;
  2413. sysfs_attr_init(&panel_handle->attr);
  2414. panel_handle->attr.name = "panel_id";
  2415. panel_handle->attr.mode = S_IRUGO;
  2416. panel_handle->show = sony_nc_panelid_show;
  2417. panel_handle->store = NULL;
  2418. result = device_create_file(&pd->dev, panel_handle);
  2419. if (result) {
  2420. kfree(panel_handle);
  2421. panel_handle = NULL;
  2422. return result;
  2423. }
  2424. return 0;
  2425. }
  2426. static void sony_nc_panelid_cleanup(struct platform_device *pd)
  2427. {
  2428. if (panel_handle) {
  2429. device_remove_file(&pd->dev, panel_handle);
  2430. kfree(panel_handle);
  2431. panel_handle = NULL;
  2432. }
  2433. }
  2434. /* smart connect function */
  2435. static struct device_attribute *sc_handle;
  2436. static ssize_t sony_nc_smart_conn_store(struct device *dev,
  2437. struct device_attribute *attr,
  2438. const char *buffer, size_t count)
  2439. {
  2440. unsigned int result;
  2441. unsigned long value;
  2442. if (count > 31)
  2443. return -EINVAL;
  2444. if (kstrtoul(buffer, 10, &value) || value > 1)
  2445. return -EINVAL;
  2446. if (sony_call_snc_handle(0x0168, value << 0x10, &result))
  2447. return -EIO;
  2448. return count;
  2449. }
  2450. static int sony_nc_smart_conn_setup(struct platform_device *pd)
  2451. {
  2452. unsigned int result;
  2453. sc_handle = kzalloc(sizeof(struct device_attribute), GFP_KERNEL);
  2454. if (!sc_handle)
  2455. return -ENOMEM;
  2456. sysfs_attr_init(&sc_handle->attr);
  2457. sc_handle->attr.name = "smart_connect";
  2458. sc_handle->attr.mode = S_IWUSR;
  2459. sc_handle->show = NULL;
  2460. sc_handle->store = sony_nc_smart_conn_store;
  2461. result = device_create_file(&pd->dev, sc_handle);
  2462. if (result) {
  2463. kfree(sc_handle);
  2464. sc_handle = NULL;
  2465. return result;
  2466. }
  2467. return 0;
  2468. }
  2469. static void sony_nc_smart_conn_cleanup(struct platform_device *pd)
  2470. {
  2471. if (sc_handle) {
  2472. device_remove_file(&pd->dev, sc_handle);
  2473. kfree(sc_handle);
  2474. sc_handle = NULL;
  2475. }
  2476. }
  2477. /* Touchpad enable/disable */
  2478. struct touchpad_control {
  2479. struct device_attribute attr;
  2480. int handle;
  2481. };
  2482. static struct touchpad_control *tp_ctl;
  2483. static ssize_t sony_nc_touchpad_store(struct device *dev,
  2484. struct device_attribute *attr, const char *buffer, size_t count)
  2485. {
  2486. unsigned int result;
  2487. unsigned long value;
  2488. if (count > 31)
  2489. return -EINVAL;
  2490. if (kstrtoul(buffer, 10, &value) || value > 1)
  2491. return -EINVAL;
  2492. /* sysfs: 0 disabled, 1 enabled
  2493. * EC: 0 enabled, 1 disabled
  2494. */
  2495. if (sony_call_snc_handle(tp_ctl->handle,
  2496. (!value << 0x10) | 0x100, &result))
  2497. return -EIO;
  2498. return count;
  2499. }
  2500. static ssize_t sony_nc_touchpad_show(struct device *dev,
  2501. struct device_attribute *attr, char *buffer)
  2502. {
  2503. unsigned int result;
  2504. if (sony_call_snc_handle(tp_ctl->handle, 0x000, &result))
  2505. return -EINVAL;
  2506. return snprintf(buffer, PAGE_SIZE, "%d\n", !(result & 0x01));
  2507. }
  2508. static int sony_nc_touchpad_setup(struct platform_device *pd,
  2509. unsigned int handle)
  2510. {
  2511. int ret = 0;
  2512. tp_ctl = kzalloc(sizeof(struct touchpad_control), GFP_KERNEL);
  2513. if (!tp_ctl)
  2514. return -ENOMEM;
  2515. tp_ctl->handle = handle;
  2516. sysfs_attr_init(&tp_ctl->attr.attr);
  2517. tp_ctl->attr.attr.name = "touchpad";
  2518. tp_ctl->attr.attr.mode = S_IRUGO | S_IWUSR;
  2519. tp_ctl->attr.show = sony_nc_touchpad_show;
  2520. tp_ctl->attr.store = sony_nc_touchpad_store;
  2521. ret = device_create_file(&pd->dev, &tp_ctl->attr);
  2522. if (ret) {
  2523. kfree(tp_ctl);
  2524. tp_ctl = NULL;
  2525. }
  2526. return ret;
  2527. }
  2528. static void sony_nc_touchpad_cleanup(struct platform_device *pd)
  2529. {
  2530. if (tp_ctl) {
  2531. device_remove_file(&pd->dev, &tp_ctl->attr);
  2532. kfree(tp_ctl);
  2533. tp_ctl = NULL;
  2534. }
  2535. }
  2536. static void sony_nc_backlight_ng_read_limits(int handle,
  2537. struct sony_backlight_props *props)
  2538. {
  2539. u64 offset;
  2540. int i;
  2541. int lvl_table_len = 0;
  2542. u8 min = 0xff, max = 0x00;
  2543. unsigned char buffer[32] = { 0 };
  2544. props->handle = handle;
  2545. props->offset = 0;
  2546. props->maxlvl = 0xff;
  2547. offset = sony_find_snc_handle(handle);
  2548. /* try to read the boundaries from ACPI tables, if we fail the above
  2549. * defaults should be reasonable
  2550. */
  2551. i = sony_nc_buffer_call(sony_nc_acpi_handle, "SN06", &offset, buffer,
  2552. 32);
  2553. if (i < 0)
  2554. return;
  2555. switch (handle) {
  2556. case 0x012f:
  2557. case 0x0137:
  2558. lvl_table_len = 9;
  2559. break;
  2560. case 0x143:
  2561. case 0x14b:
  2562. case 0x14c:
  2563. lvl_table_len = 16;
  2564. break;
  2565. }
  2566. /* the buffer lists brightness levels available, brightness levels are
  2567. * from position 0 to 8 in the array, other values are used by ALS
  2568. * control.
  2569. */
  2570. for (i = 0; i < lvl_table_len && i < ARRAY_SIZE(buffer); i++) {
  2571. dprintk("Brightness level: %d\n", buffer[i]);
  2572. if (!buffer[i])
  2573. break;
  2574. if (buffer[i] > max)
  2575. max = buffer[i];
  2576. if (buffer[i] < min)
  2577. min = buffer[i];
  2578. }
  2579. props->offset = min;
  2580. props->maxlvl = max;
  2581. dprintk("Brightness levels: min=%d max=%d\n", props->offset,
  2582. props->maxlvl);
  2583. }
  2584. static void sony_nc_backlight_setup(void)
  2585. {
  2586. int max_brightness = 0;
  2587. const struct backlight_ops *ops = NULL;
  2588. struct backlight_properties props;
  2589. if (sony_find_snc_handle(0x12f) >= 0) {
  2590. ops = &sony_backlight_ng_ops;
  2591. sony_bl_props.cmd_base = 0x0100;
  2592. sony_nc_backlight_ng_read_limits(0x12f, &sony_bl_props);
  2593. max_brightness = sony_bl_props.maxlvl - sony_bl_props.offset;
  2594. } else if (sony_find_snc_handle(0x137) >= 0) {
  2595. ops = &sony_backlight_ng_ops;
  2596. sony_bl_props.cmd_base = 0x0100;
  2597. sony_nc_backlight_ng_read_limits(0x137, &sony_bl_props);
  2598. max_brightness = sony_bl_props.maxlvl - sony_bl_props.offset;
  2599. } else if (sony_find_snc_handle(0x143) >= 0) {
  2600. ops = &sony_backlight_ng_ops;
  2601. sony_bl_props.cmd_base = 0x3000;
  2602. sony_nc_backlight_ng_read_limits(0x143, &sony_bl_props);
  2603. max_brightness = sony_bl_props.maxlvl - sony_bl_props.offset;
  2604. } else if (sony_find_snc_handle(0x14b) >= 0) {
  2605. ops = &sony_backlight_ng_ops;
  2606. sony_bl_props.cmd_base = 0x3000;
  2607. sony_nc_backlight_ng_read_limits(0x14b, &sony_bl_props);
  2608. max_brightness = sony_bl_props.maxlvl - sony_bl_props.offset;
  2609. } else if (sony_find_snc_handle(0x14c) >= 0) {
  2610. ops = &sony_backlight_ng_ops;
  2611. sony_bl_props.cmd_base = 0x3000;
  2612. sony_nc_backlight_ng_read_limits(0x14c, &sony_bl_props);
  2613. max_brightness = sony_bl_props.maxlvl - sony_bl_props.offset;
  2614. } else if (acpi_has_method(sony_nc_acpi_handle, "GBRT")) {
  2615. ops = &sony_backlight_ops;
  2616. max_brightness = SONY_MAX_BRIGHTNESS - 1;
  2617. } else
  2618. return;
  2619. memset(&props, 0, sizeof(struct backlight_properties));
  2620. props.type = BACKLIGHT_PLATFORM;
  2621. props.max_brightness = max_brightness;
  2622. sony_bl_props.dev = backlight_device_register("sony", NULL,
  2623. &sony_bl_props,
  2624. ops, &props);
  2625. if (IS_ERR(sony_bl_props.dev)) {
  2626. pr_warn("unable to register backlight device\n");
  2627. sony_bl_props.dev = NULL;
  2628. } else
  2629. sony_bl_props.dev->props.brightness =
  2630. ops->get_brightness(sony_bl_props.dev);
  2631. }
  2632. static void sony_nc_backlight_cleanup(void)
  2633. {
  2634. backlight_device_unregister(sony_bl_props.dev);
  2635. }
  2636. static int sony_nc_add(struct acpi_device *device)
  2637. {
  2638. acpi_status status;
  2639. int result = 0;
  2640. struct sony_nc_value *item;
  2641. sony_nc_acpi_device = device;
  2642. strcpy(acpi_device_class(device), "sony/hotkey");
  2643. sony_nc_acpi_handle = device->handle;
  2644. /* read device status */
  2645. result = acpi_bus_get_status(device);
  2646. /* bail IFF the above call was successful and the device is not present */
  2647. if (!result && !device->status.present) {
  2648. dprintk("Device not present\n");
  2649. result = -ENODEV;
  2650. goto outwalk;
  2651. }
  2652. result = sony_pf_add();
  2653. if (result)
  2654. goto outpresent;
  2655. if (debug) {
  2656. status = acpi_walk_namespace(ACPI_TYPE_METHOD,
  2657. sony_nc_acpi_handle, 1, sony_walk_callback,
  2658. NULL, NULL, NULL);
  2659. if (ACPI_FAILURE(status)) {
  2660. pr_warn("unable to walk acpi resources\n");
  2661. result = -ENODEV;
  2662. goto outpresent;
  2663. }
  2664. }
  2665. result = sony_laptop_setup_input(device);
  2666. if (result) {
  2667. pr_err("Unable to create input devices\n");
  2668. goto outplatform;
  2669. }
  2670. if (acpi_has_method(sony_nc_acpi_handle, "ECON")) {
  2671. int arg = 1;
  2672. if (sony_nc_int_call(sony_nc_acpi_handle, "ECON", &arg, NULL))
  2673. dprintk("ECON Method failed\n");
  2674. }
  2675. if (acpi_has_method(sony_nc_acpi_handle, "SN00")) {
  2676. dprintk("Doing SNC setup\n");
  2677. /* retrieve the available handles */
  2678. result = sony_nc_handles_setup(sony_pf_device);
  2679. if (!result)
  2680. sony_nc_function_setup(device, sony_pf_device);
  2681. }
  2682. if (acpi_video_get_backlight_type() == acpi_backlight_vendor)
  2683. sony_nc_backlight_setup();
  2684. /* create sony_pf sysfs attributes related to the SNC device */
  2685. for (item = sony_nc_values; item->name; ++item) {
  2686. if (!debug && item->debug)
  2687. continue;
  2688. /* find the available acpiget as described in the DSDT */
  2689. for (; item->acpiget && *item->acpiget; ++item->acpiget) {
  2690. if (acpi_has_method(sony_nc_acpi_handle,
  2691. *item->acpiget)) {
  2692. dprintk("Found %s getter: %s\n",
  2693. item->name, *item->acpiget);
  2694. item->devattr.attr.mode |= S_IRUGO;
  2695. break;
  2696. }
  2697. }
  2698. /* find the available acpiset as described in the DSDT */
  2699. for (; item->acpiset && *item->acpiset; ++item->acpiset) {
  2700. if (acpi_has_method(sony_nc_acpi_handle,
  2701. *item->acpiset)) {
  2702. dprintk("Found %s setter: %s\n",
  2703. item->name, *item->acpiset);
  2704. item->devattr.attr.mode |= S_IWUSR;
  2705. break;
  2706. }
  2707. }
  2708. if (item->devattr.attr.mode != 0) {
  2709. result =
  2710. device_create_file(&sony_pf_device->dev,
  2711. &item->devattr);
  2712. if (result)
  2713. goto out_sysfs;
  2714. }
  2715. }
  2716. pr_info("SNC setup done.\n");
  2717. return 0;
  2718. out_sysfs:
  2719. for (item = sony_nc_values; item->name; ++item) {
  2720. device_remove_file(&sony_pf_device->dev, &item->devattr);
  2721. }
  2722. sony_nc_backlight_cleanup();
  2723. sony_nc_function_cleanup(sony_pf_device);
  2724. sony_nc_handles_cleanup(sony_pf_device);
  2725. outplatform:
  2726. sony_laptop_remove_input();
  2727. outpresent:
  2728. sony_pf_remove();
  2729. outwalk:
  2730. sony_nc_rfkill_cleanup();
  2731. return result;
  2732. }
  2733. static int sony_nc_remove(struct acpi_device *device)
  2734. {
  2735. struct sony_nc_value *item;
  2736. sony_nc_backlight_cleanup();
  2737. sony_nc_acpi_device = NULL;
  2738. for (item = sony_nc_values; item->name; ++item) {
  2739. device_remove_file(&sony_pf_device->dev, &item->devattr);
  2740. }
  2741. sony_nc_function_cleanup(sony_pf_device);
  2742. sony_nc_handles_cleanup(sony_pf_device);
  2743. sony_pf_remove();
  2744. sony_laptop_remove_input();
  2745. dprintk(SONY_NC_DRIVER_NAME " removed.\n");
  2746. return 0;
  2747. }
  2748. static const struct acpi_device_id sony_device_ids[] = {
  2749. {SONY_NC_HID, 0},
  2750. {SONY_PIC_HID, 0},
  2751. {"", 0},
  2752. };
  2753. MODULE_DEVICE_TABLE(acpi, sony_device_ids);
  2754. static const struct acpi_device_id sony_nc_device_ids[] = {
  2755. {SONY_NC_HID, 0},
  2756. {"", 0},
  2757. };
  2758. static struct acpi_driver sony_nc_driver = {
  2759. .name = SONY_NC_DRIVER_NAME,
  2760. .class = SONY_NC_CLASS,
  2761. .ids = sony_nc_device_ids,
  2762. .owner = THIS_MODULE,
  2763. .ops = {
  2764. .add = sony_nc_add,
  2765. .remove = sony_nc_remove,
  2766. .notify = sony_nc_notify,
  2767. },
  2768. .drv.pm = &sony_nc_pm,
  2769. };
  2770. /*********** SPIC (SNY6001) Device ***********/
  2771. #define SONYPI_DEVICE_TYPE1 0x00000001
  2772. #define SONYPI_DEVICE_TYPE2 0x00000002
  2773. #define SONYPI_DEVICE_TYPE3 0x00000004
  2774. #define SONYPI_TYPE1_OFFSET 0x04
  2775. #define SONYPI_TYPE2_OFFSET 0x12
  2776. #define SONYPI_TYPE3_OFFSET 0x12
  2777. struct sony_pic_ioport {
  2778. struct acpi_resource_io io1;
  2779. struct acpi_resource_io io2;
  2780. struct list_head list;
  2781. };
  2782. struct sony_pic_irq {
  2783. struct acpi_resource_irq irq;
  2784. struct list_head list;
  2785. };
  2786. struct sonypi_eventtypes {
  2787. u8 data;
  2788. unsigned long mask;
  2789. struct sonypi_event *events;
  2790. };
  2791. struct sony_pic_dev {
  2792. struct acpi_device *acpi_dev;
  2793. struct sony_pic_irq *cur_irq;
  2794. struct sony_pic_ioport *cur_ioport;
  2795. struct list_head interrupts;
  2796. struct list_head ioports;
  2797. struct mutex lock;
  2798. struct sonypi_eventtypes *event_types;
  2799. int (*handle_irq)(const u8, const u8);
  2800. int model;
  2801. u16 evport_offset;
  2802. u8 camera_power;
  2803. u8 bluetooth_power;
  2804. u8 wwan_power;
  2805. };
  2806. static struct sony_pic_dev spic_dev = {
  2807. .interrupts = LIST_HEAD_INIT(spic_dev.interrupts),
  2808. .ioports = LIST_HEAD_INIT(spic_dev.ioports),
  2809. };
  2810. static int spic_drv_registered;
  2811. /* Event masks */
  2812. #define SONYPI_JOGGER_MASK 0x00000001
  2813. #define SONYPI_CAPTURE_MASK 0x00000002
  2814. #define SONYPI_FNKEY_MASK 0x00000004
  2815. #define SONYPI_BLUETOOTH_MASK 0x00000008
  2816. #define SONYPI_PKEY_MASK 0x00000010
  2817. #define SONYPI_BACK_MASK 0x00000020
  2818. #define SONYPI_HELP_MASK 0x00000040
  2819. #define SONYPI_LID_MASK 0x00000080
  2820. #define SONYPI_ZOOM_MASK 0x00000100
  2821. #define SONYPI_THUMBPHRASE_MASK 0x00000200
  2822. #define SONYPI_MEYE_MASK 0x00000400
  2823. #define SONYPI_MEMORYSTICK_MASK 0x00000800
  2824. #define SONYPI_BATTERY_MASK 0x00001000
  2825. #define SONYPI_WIRELESS_MASK 0x00002000
  2826. struct sonypi_event {
  2827. u8 data;
  2828. u8 event;
  2829. };
  2830. /* The set of possible button release events */
  2831. static struct sonypi_event sonypi_releaseev[] = {
  2832. { 0x00, SONYPI_EVENT_ANYBUTTON_RELEASED },
  2833. { 0, 0 }
  2834. };
  2835. /* The set of possible jogger events */
  2836. static struct sonypi_event sonypi_joggerev[] = {
  2837. { 0x1f, SONYPI_EVENT_JOGDIAL_UP },
  2838. { 0x01, SONYPI_EVENT_JOGDIAL_DOWN },
  2839. { 0x5f, SONYPI_EVENT_JOGDIAL_UP_PRESSED },
  2840. { 0x41, SONYPI_EVENT_JOGDIAL_DOWN_PRESSED },
  2841. { 0x1e, SONYPI_EVENT_JOGDIAL_FAST_UP },
  2842. { 0x02, SONYPI_EVENT_JOGDIAL_FAST_DOWN },
  2843. { 0x5e, SONYPI_EVENT_JOGDIAL_FAST_UP_PRESSED },
  2844. { 0x42, SONYPI_EVENT_JOGDIAL_FAST_DOWN_PRESSED },
  2845. { 0x1d, SONYPI_EVENT_JOGDIAL_VFAST_UP },
  2846. { 0x03, SONYPI_EVENT_JOGDIAL_VFAST_DOWN },
  2847. { 0x5d, SONYPI_EVENT_JOGDIAL_VFAST_UP_PRESSED },
  2848. { 0x43, SONYPI_EVENT_JOGDIAL_VFAST_DOWN_PRESSED },
  2849. { 0x40, SONYPI_EVENT_JOGDIAL_PRESSED },
  2850. { 0, 0 }
  2851. };
  2852. /* The set of possible capture button events */
  2853. static struct sonypi_event sonypi_captureev[] = {
  2854. { 0x05, SONYPI_EVENT_CAPTURE_PARTIALPRESSED },
  2855. { 0x07, SONYPI_EVENT_CAPTURE_PRESSED },
  2856. { 0x40, SONYPI_EVENT_CAPTURE_PRESSED },
  2857. { 0x01, SONYPI_EVENT_CAPTURE_PARTIALRELEASED },
  2858. { 0, 0 }
  2859. };
  2860. /* The set of possible fnkeys events */
  2861. static struct sonypi_event sonypi_fnkeyev[] = {
  2862. { 0x10, SONYPI_EVENT_FNKEY_ESC },
  2863. { 0x11, SONYPI_EVENT_FNKEY_F1 },
  2864. { 0x12, SONYPI_EVENT_FNKEY_F2 },
  2865. { 0x13, SONYPI_EVENT_FNKEY_F3 },
  2866. { 0x14, SONYPI_EVENT_FNKEY_F4 },
  2867. { 0x15, SONYPI_EVENT_FNKEY_F5 },
  2868. { 0x16, SONYPI_EVENT_FNKEY_F6 },
  2869. { 0x17, SONYPI_EVENT_FNKEY_F7 },
  2870. { 0x18, SONYPI_EVENT_FNKEY_F8 },
  2871. { 0x19, SONYPI_EVENT_FNKEY_F9 },
  2872. { 0x1a, SONYPI_EVENT_FNKEY_F10 },
  2873. { 0x1b, SONYPI_EVENT_FNKEY_F11 },
  2874. { 0x1c, SONYPI_EVENT_FNKEY_F12 },
  2875. { 0x1f, SONYPI_EVENT_FNKEY_RELEASED },
  2876. { 0x21, SONYPI_EVENT_FNKEY_1 },
  2877. { 0x22, SONYPI_EVENT_FNKEY_2 },
  2878. { 0x31, SONYPI_EVENT_FNKEY_D },
  2879. { 0x32, SONYPI_EVENT_FNKEY_E },
  2880. { 0x33, SONYPI_EVENT_FNKEY_F },
  2881. { 0x34, SONYPI_EVENT_FNKEY_S },
  2882. { 0x35, SONYPI_EVENT_FNKEY_B },
  2883. { 0x36, SONYPI_EVENT_FNKEY_ONLY },
  2884. { 0, 0 }
  2885. };
  2886. /* The set of possible program key events */
  2887. static struct sonypi_event sonypi_pkeyev[] = {
  2888. { 0x01, SONYPI_EVENT_PKEY_P1 },
  2889. { 0x02, SONYPI_EVENT_PKEY_P2 },
  2890. { 0x04, SONYPI_EVENT_PKEY_P3 },
  2891. { 0x20, SONYPI_EVENT_PKEY_P1 },
  2892. { 0, 0 }
  2893. };
  2894. /* The set of possible bluetooth events */
  2895. static struct sonypi_event sonypi_blueev[] = {
  2896. { 0x55, SONYPI_EVENT_BLUETOOTH_PRESSED },
  2897. { 0x59, SONYPI_EVENT_BLUETOOTH_ON },
  2898. { 0x5a, SONYPI_EVENT_BLUETOOTH_OFF },
  2899. { 0, 0 }
  2900. };
  2901. /* The set of possible wireless events */
  2902. static struct sonypi_event sonypi_wlessev[] = {
  2903. { 0x59, SONYPI_EVENT_IGNORE },
  2904. { 0x5a, SONYPI_EVENT_IGNORE },
  2905. { 0, 0 }
  2906. };
  2907. /* The set of possible back button events */
  2908. static struct sonypi_event sonypi_backev[] = {
  2909. { 0x20, SONYPI_EVENT_BACK_PRESSED },
  2910. { 0, 0 }
  2911. };
  2912. /* The set of possible help button events */
  2913. static struct sonypi_event sonypi_helpev[] = {
  2914. { 0x3b, SONYPI_EVENT_HELP_PRESSED },
  2915. { 0, 0 }
  2916. };
  2917. /* The set of possible lid events */
  2918. static struct sonypi_event sonypi_lidev[] = {
  2919. { 0x51, SONYPI_EVENT_LID_CLOSED },
  2920. { 0x50, SONYPI_EVENT_LID_OPENED },
  2921. { 0, 0 }
  2922. };
  2923. /* The set of possible zoom events */
  2924. static struct sonypi_event sonypi_zoomev[] = {
  2925. { 0x39, SONYPI_EVENT_ZOOM_PRESSED },
  2926. { 0x10, SONYPI_EVENT_ZOOM_IN_PRESSED },
  2927. { 0x20, SONYPI_EVENT_ZOOM_OUT_PRESSED },
  2928. { 0x04, SONYPI_EVENT_ZOOM_PRESSED },
  2929. { 0, 0 }
  2930. };
  2931. /* The set of possible thumbphrase events */
  2932. static struct sonypi_event sonypi_thumbphraseev[] = {
  2933. { 0x3a, SONYPI_EVENT_THUMBPHRASE_PRESSED },
  2934. { 0, 0 }
  2935. };
  2936. /* The set of possible motioneye camera events */
  2937. static struct sonypi_event sonypi_meyeev[] = {
  2938. { 0x00, SONYPI_EVENT_MEYE_FACE },
  2939. { 0x01, SONYPI_EVENT_MEYE_OPPOSITE },
  2940. { 0, 0 }
  2941. };
  2942. /* The set of possible memorystick events */
  2943. static struct sonypi_event sonypi_memorystickev[] = {
  2944. { 0x53, SONYPI_EVENT_MEMORYSTICK_INSERT },
  2945. { 0x54, SONYPI_EVENT_MEMORYSTICK_EJECT },
  2946. { 0, 0 }
  2947. };
  2948. /* The set of possible battery events */
  2949. static struct sonypi_event sonypi_batteryev[] = {
  2950. { 0x20, SONYPI_EVENT_BATTERY_INSERT },
  2951. { 0x30, SONYPI_EVENT_BATTERY_REMOVE },
  2952. { 0, 0 }
  2953. };
  2954. /* The set of possible volume events */
  2955. static struct sonypi_event sonypi_volumeev[] = {
  2956. { 0x01, SONYPI_EVENT_VOLUME_INC_PRESSED },
  2957. { 0x02, SONYPI_EVENT_VOLUME_DEC_PRESSED },
  2958. { 0, 0 }
  2959. };
  2960. /* The set of possible brightness events */
  2961. static struct sonypi_event sonypi_brightnessev[] = {
  2962. { 0x80, SONYPI_EVENT_BRIGHTNESS_PRESSED },
  2963. { 0, 0 }
  2964. };
  2965. static struct sonypi_eventtypes type1_events[] = {
  2966. { 0, 0xffffffff, sonypi_releaseev },
  2967. { 0x70, SONYPI_MEYE_MASK, sonypi_meyeev },
  2968. { 0x30, SONYPI_LID_MASK, sonypi_lidev },
  2969. { 0x60, SONYPI_CAPTURE_MASK, sonypi_captureev },
  2970. { 0x10, SONYPI_JOGGER_MASK, sonypi_joggerev },
  2971. { 0x20, SONYPI_FNKEY_MASK, sonypi_fnkeyev },
  2972. { 0x30, SONYPI_BLUETOOTH_MASK, sonypi_blueev },
  2973. { 0x40, SONYPI_PKEY_MASK, sonypi_pkeyev },
  2974. { 0x30, SONYPI_MEMORYSTICK_MASK, sonypi_memorystickev },
  2975. { 0x40, SONYPI_BATTERY_MASK, sonypi_batteryev },
  2976. { 0 },
  2977. };
  2978. static struct sonypi_eventtypes type2_events[] = {
  2979. { 0, 0xffffffff, sonypi_releaseev },
  2980. { 0x38, SONYPI_LID_MASK, sonypi_lidev },
  2981. { 0x11, SONYPI_JOGGER_MASK, sonypi_joggerev },
  2982. { 0x61, SONYPI_CAPTURE_MASK, sonypi_captureev },
  2983. { 0x21, SONYPI_FNKEY_MASK, sonypi_fnkeyev },
  2984. { 0x31, SONYPI_BLUETOOTH_MASK, sonypi_blueev },
  2985. { 0x08, SONYPI_PKEY_MASK, sonypi_pkeyev },
  2986. { 0x11, SONYPI_BACK_MASK, sonypi_backev },
  2987. { 0x21, SONYPI_HELP_MASK, sonypi_helpev },
  2988. { 0x21, SONYPI_ZOOM_MASK, sonypi_zoomev },
  2989. { 0x20, SONYPI_THUMBPHRASE_MASK, sonypi_thumbphraseev },
  2990. { 0x31, SONYPI_MEMORYSTICK_MASK, sonypi_memorystickev },
  2991. { 0x41, SONYPI_BATTERY_MASK, sonypi_batteryev },
  2992. { 0x31, SONYPI_PKEY_MASK, sonypi_pkeyev },
  2993. { 0 },
  2994. };
  2995. static struct sonypi_eventtypes type3_events[] = {
  2996. { 0, 0xffffffff, sonypi_releaseev },
  2997. { 0x21, SONYPI_FNKEY_MASK, sonypi_fnkeyev },
  2998. { 0x31, SONYPI_WIRELESS_MASK, sonypi_wlessev },
  2999. { 0x31, SONYPI_MEMORYSTICK_MASK, sonypi_memorystickev },
  3000. { 0x41, SONYPI_BATTERY_MASK, sonypi_batteryev },
  3001. { 0x31, SONYPI_PKEY_MASK, sonypi_pkeyev },
  3002. { 0x05, SONYPI_PKEY_MASK, sonypi_pkeyev },
  3003. { 0x05, SONYPI_ZOOM_MASK, sonypi_zoomev },
  3004. { 0x05, SONYPI_CAPTURE_MASK, sonypi_captureev },
  3005. { 0x05, SONYPI_PKEY_MASK, sonypi_volumeev },
  3006. { 0x05, SONYPI_PKEY_MASK, sonypi_brightnessev },
  3007. { 0 },
  3008. };
  3009. /* low level spic calls */
  3010. #define ITERATIONS_LONG 10000
  3011. #define ITERATIONS_SHORT 10
  3012. #define wait_on_command(command, iterations) { \
  3013. unsigned int n = iterations; \
  3014. while (--n && (command)) \
  3015. udelay(1); \
  3016. if (!n) \
  3017. dprintk("command failed at %s : %s (line %d)\n", \
  3018. __FILE__, __func__, __LINE__); \
  3019. }
  3020. static u8 sony_pic_call1(u8 dev)
  3021. {
  3022. u8 v1, v2;
  3023. wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2,
  3024. ITERATIONS_LONG);
  3025. outb(dev, spic_dev.cur_ioport->io1.minimum + 4);
  3026. v1 = inb_p(spic_dev.cur_ioport->io1.minimum + 4);
  3027. v2 = inb_p(spic_dev.cur_ioport->io1.minimum);
  3028. dprintk("sony_pic_call1(0x%.2x): 0x%.4x\n", dev, (v2 << 8) | v1);
  3029. return v2;
  3030. }
  3031. static u8 sony_pic_call2(u8 dev, u8 fn)
  3032. {
  3033. u8 v1;
  3034. wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2,
  3035. ITERATIONS_LONG);
  3036. outb(dev, spic_dev.cur_ioport->io1.minimum + 4);
  3037. wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2,
  3038. ITERATIONS_LONG);
  3039. outb(fn, spic_dev.cur_ioport->io1.minimum);
  3040. v1 = inb_p(spic_dev.cur_ioport->io1.minimum);
  3041. dprintk("sony_pic_call2(0x%.2x - 0x%.2x): 0x%.4x\n", dev, fn, v1);
  3042. return v1;
  3043. }
  3044. static u8 sony_pic_call3(u8 dev, u8 fn, u8 v)
  3045. {
  3046. u8 v1;
  3047. wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2, ITERATIONS_LONG);
  3048. outb(dev, spic_dev.cur_ioport->io1.minimum + 4);
  3049. wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2, ITERATIONS_LONG);
  3050. outb(fn, spic_dev.cur_ioport->io1.minimum);
  3051. wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2, ITERATIONS_LONG);
  3052. outb(v, spic_dev.cur_ioport->io1.minimum);
  3053. v1 = inb_p(spic_dev.cur_ioport->io1.minimum);
  3054. dprintk("sony_pic_call3(0x%.2x - 0x%.2x - 0x%.2x): 0x%.4x\n",
  3055. dev, fn, v, v1);
  3056. return v1;
  3057. }
  3058. /*
  3059. * minidrivers for SPIC models
  3060. */
  3061. static int type3_handle_irq(const u8 data_mask, const u8 ev)
  3062. {
  3063. /*
  3064. * 0x31 could mean we have to take some extra action and wait for
  3065. * the next irq for some Type3 models, it will generate a new
  3066. * irq and we can read new data from the device:
  3067. * - 0x5c and 0x5f requires 0xA0
  3068. * - 0x61 requires 0xB3
  3069. */
  3070. if (data_mask == 0x31) {
  3071. if (ev == 0x5c || ev == 0x5f)
  3072. sony_pic_call1(0xA0);
  3073. else if (ev == 0x61)
  3074. sony_pic_call1(0xB3);
  3075. return 0;
  3076. }
  3077. return 1;
  3078. }
  3079. static void sony_pic_detect_device_type(struct sony_pic_dev *dev)
  3080. {
  3081. struct pci_dev *pcidev;
  3082. pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
  3083. PCI_DEVICE_ID_INTEL_82371AB_3, NULL);
  3084. if (pcidev) {
  3085. dev->model = SONYPI_DEVICE_TYPE1;
  3086. dev->evport_offset = SONYPI_TYPE1_OFFSET;
  3087. dev->event_types = type1_events;
  3088. goto out;
  3089. }
  3090. pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
  3091. PCI_DEVICE_ID_INTEL_ICH6_1, NULL);
  3092. if (pcidev) {
  3093. dev->model = SONYPI_DEVICE_TYPE2;
  3094. dev->evport_offset = SONYPI_TYPE2_OFFSET;
  3095. dev->event_types = type2_events;
  3096. goto out;
  3097. }
  3098. pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
  3099. PCI_DEVICE_ID_INTEL_ICH7_1, NULL);
  3100. if (pcidev) {
  3101. dev->model = SONYPI_DEVICE_TYPE3;
  3102. dev->handle_irq = type3_handle_irq;
  3103. dev->evport_offset = SONYPI_TYPE3_OFFSET;
  3104. dev->event_types = type3_events;
  3105. goto out;
  3106. }
  3107. pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
  3108. PCI_DEVICE_ID_INTEL_ICH8_4, NULL);
  3109. if (pcidev) {
  3110. dev->model = SONYPI_DEVICE_TYPE3;
  3111. dev->handle_irq = type3_handle_irq;
  3112. dev->evport_offset = SONYPI_TYPE3_OFFSET;
  3113. dev->event_types = type3_events;
  3114. goto out;
  3115. }
  3116. pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
  3117. PCI_DEVICE_ID_INTEL_ICH9_1, NULL);
  3118. if (pcidev) {
  3119. dev->model = SONYPI_DEVICE_TYPE3;
  3120. dev->handle_irq = type3_handle_irq;
  3121. dev->evport_offset = SONYPI_TYPE3_OFFSET;
  3122. dev->event_types = type3_events;
  3123. goto out;
  3124. }
  3125. /* default */
  3126. dev->model = SONYPI_DEVICE_TYPE2;
  3127. dev->evport_offset = SONYPI_TYPE2_OFFSET;
  3128. dev->event_types = type2_events;
  3129. out:
  3130. pci_dev_put(pcidev);
  3131. pr_info("detected Type%d model\n",
  3132. dev->model == SONYPI_DEVICE_TYPE1 ? 1 :
  3133. dev->model == SONYPI_DEVICE_TYPE2 ? 2 : 3);
  3134. }
  3135. /* camera tests and poweron/poweroff */
  3136. #define SONYPI_CAMERA_PICTURE 5
  3137. #define SONYPI_CAMERA_CONTROL 0x10
  3138. #define SONYPI_CAMERA_BRIGHTNESS 0
  3139. #define SONYPI_CAMERA_CONTRAST 1
  3140. #define SONYPI_CAMERA_HUE 2
  3141. #define SONYPI_CAMERA_COLOR 3
  3142. #define SONYPI_CAMERA_SHARPNESS 4
  3143. #define SONYPI_CAMERA_EXPOSURE_MASK 0xC
  3144. #define SONYPI_CAMERA_WHITE_BALANCE_MASK 0x3
  3145. #define SONYPI_CAMERA_PICTURE_MODE_MASK 0x30
  3146. #define SONYPI_CAMERA_MUTE_MASK 0x40
  3147. /* the rest don't need a loop until not 0xff */
  3148. #define SONYPI_CAMERA_AGC 6
  3149. #define SONYPI_CAMERA_AGC_MASK 0x30
  3150. #define SONYPI_CAMERA_SHUTTER_MASK 0x7
  3151. #define SONYPI_CAMERA_SHUTDOWN_REQUEST 7
  3152. #define SONYPI_CAMERA_CONTROL 0x10
  3153. #define SONYPI_CAMERA_STATUS 7
  3154. #define SONYPI_CAMERA_STATUS_READY 0x2
  3155. #define SONYPI_CAMERA_STATUS_POSITION 0x4
  3156. #define SONYPI_DIRECTION_BACKWARDS 0x4
  3157. #define SONYPI_CAMERA_REVISION 8
  3158. #define SONYPI_CAMERA_ROMVERSION 9
  3159. static int __sony_pic_camera_ready(void)
  3160. {
  3161. u8 v;
  3162. v = sony_pic_call2(0x8f, SONYPI_CAMERA_STATUS);
  3163. return (v != 0xff && (v & SONYPI_CAMERA_STATUS_READY));
  3164. }
  3165. static int __sony_pic_camera_off(void)
  3166. {
  3167. if (!camera) {
  3168. pr_warn("camera control not enabled\n");
  3169. return -ENODEV;
  3170. }
  3171. wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_PICTURE,
  3172. SONYPI_CAMERA_MUTE_MASK),
  3173. ITERATIONS_SHORT);
  3174. if (spic_dev.camera_power) {
  3175. sony_pic_call2(0x91, 0);
  3176. spic_dev.camera_power = 0;
  3177. }
  3178. return 0;
  3179. }
  3180. static int __sony_pic_camera_on(void)
  3181. {
  3182. int i, j, x;
  3183. if (!camera) {
  3184. pr_warn("camera control not enabled\n");
  3185. return -ENODEV;
  3186. }
  3187. if (spic_dev.camera_power)
  3188. return 0;
  3189. for (j = 5; j > 0; j--) {
  3190. for (x = 0; x < 100 && sony_pic_call2(0x91, 0x1); x++)
  3191. msleep(10);
  3192. sony_pic_call1(0x93);
  3193. for (i = 400; i > 0; i--) {
  3194. if (__sony_pic_camera_ready())
  3195. break;
  3196. msleep(10);
  3197. }
  3198. if (i)
  3199. break;
  3200. }
  3201. if (j == 0) {
  3202. pr_warn("failed to power on camera\n");
  3203. return -ENODEV;
  3204. }
  3205. wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_CONTROL,
  3206. 0x5a),
  3207. ITERATIONS_SHORT);
  3208. spic_dev.camera_power = 1;
  3209. return 0;
  3210. }
  3211. /* External camera command (exported to the motion eye v4l driver) */
  3212. int sony_pic_camera_command(int command, u8 value)
  3213. {
  3214. if (!camera)
  3215. return -EIO;
  3216. mutex_lock(&spic_dev.lock);
  3217. switch (command) {
  3218. case SONY_PIC_COMMAND_SETCAMERA:
  3219. if (value)
  3220. __sony_pic_camera_on();
  3221. else
  3222. __sony_pic_camera_off();
  3223. break;
  3224. case SONY_PIC_COMMAND_SETCAMERABRIGHTNESS:
  3225. wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_BRIGHTNESS, value),
  3226. ITERATIONS_SHORT);
  3227. break;
  3228. case SONY_PIC_COMMAND_SETCAMERACONTRAST:
  3229. wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_CONTRAST, value),
  3230. ITERATIONS_SHORT);
  3231. break;
  3232. case SONY_PIC_COMMAND_SETCAMERAHUE:
  3233. wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_HUE, value),
  3234. ITERATIONS_SHORT);
  3235. break;
  3236. case SONY_PIC_COMMAND_SETCAMERACOLOR:
  3237. wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_COLOR, value),
  3238. ITERATIONS_SHORT);
  3239. break;
  3240. case SONY_PIC_COMMAND_SETCAMERASHARPNESS:
  3241. wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_SHARPNESS, value),
  3242. ITERATIONS_SHORT);
  3243. break;
  3244. case SONY_PIC_COMMAND_SETCAMERAPICTURE:
  3245. wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_PICTURE, value),
  3246. ITERATIONS_SHORT);
  3247. break;
  3248. case SONY_PIC_COMMAND_SETCAMERAAGC:
  3249. wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_AGC, value),
  3250. ITERATIONS_SHORT);
  3251. break;
  3252. default:
  3253. pr_err("sony_pic_camera_command invalid: %d\n", command);
  3254. break;
  3255. }
  3256. mutex_unlock(&spic_dev.lock);
  3257. return 0;
  3258. }
  3259. EXPORT_SYMBOL(sony_pic_camera_command);
  3260. /* gprs/edge modem (SZ460N and SZ210P), thanks to Joshua Wise */
  3261. static void __sony_pic_set_wwanpower(u8 state)
  3262. {
  3263. state = !!state;
  3264. if (spic_dev.wwan_power == state)
  3265. return;
  3266. sony_pic_call2(0xB0, state);
  3267. sony_pic_call1(0x82);
  3268. spic_dev.wwan_power = state;
  3269. }
  3270. static ssize_t sony_pic_wwanpower_store(struct device *dev,
  3271. struct device_attribute *attr,
  3272. const char *buffer, size_t count)
  3273. {
  3274. unsigned long value;
  3275. if (count > 31)
  3276. return -EINVAL;
  3277. if (kstrtoul(buffer, 10, &value))
  3278. return -EINVAL;
  3279. mutex_lock(&spic_dev.lock);
  3280. __sony_pic_set_wwanpower(value);
  3281. mutex_unlock(&spic_dev.lock);
  3282. return count;
  3283. }
  3284. static ssize_t sony_pic_wwanpower_show(struct device *dev,
  3285. struct device_attribute *attr, char *buffer)
  3286. {
  3287. ssize_t count;
  3288. mutex_lock(&spic_dev.lock);
  3289. count = snprintf(buffer, PAGE_SIZE, "%d\n", spic_dev.wwan_power);
  3290. mutex_unlock(&spic_dev.lock);
  3291. return count;
  3292. }
  3293. /* bluetooth subsystem power state */
  3294. static void __sony_pic_set_bluetoothpower(u8 state)
  3295. {
  3296. state = !!state;
  3297. if (spic_dev.bluetooth_power == state)
  3298. return;
  3299. sony_pic_call2(0x96, state);
  3300. sony_pic_call1(0x82);
  3301. spic_dev.bluetooth_power = state;
  3302. }
  3303. static ssize_t sony_pic_bluetoothpower_store(struct device *dev,
  3304. struct device_attribute *attr,
  3305. const char *buffer, size_t count)
  3306. {
  3307. unsigned long value;
  3308. if (count > 31)
  3309. return -EINVAL;
  3310. if (kstrtoul(buffer, 10, &value))
  3311. return -EINVAL;
  3312. mutex_lock(&spic_dev.lock);
  3313. __sony_pic_set_bluetoothpower(value);
  3314. mutex_unlock(&spic_dev.lock);
  3315. return count;
  3316. }
  3317. static ssize_t sony_pic_bluetoothpower_show(struct device *dev,
  3318. struct device_attribute *attr, char *buffer)
  3319. {
  3320. ssize_t count = 0;
  3321. mutex_lock(&spic_dev.lock);
  3322. count = snprintf(buffer, PAGE_SIZE, "%d\n", spic_dev.bluetooth_power);
  3323. mutex_unlock(&spic_dev.lock);
  3324. return count;
  3325. }
  3326. /* fan speed */
  3327. /* FAN0 information (reverse engineered from ACPI tables) */
  3328. #define SONY_PIC_FAN0_STATUS 0x93
  3329. static int sony_pic_set_fanspeed(unsigned long value)
  3330. {
  3331. return ec_write(SONY_PIC_FAN0_STATUS, value);
  3332. }
  3333. static int sony_pic_get_fanspeed(u8 *value)
  3334. {
  3335. return ec_read(SONY_PIC_FAN0_STATUS, value);
  3336. }
  3337. static ssize_t sony_pic_fanspeed_store(struct device *dev,
  3338. struct device_attribute *attr,
  3339. const char *buffer, size_t count)
  3340. {
  3341. unsigned long value;
  3342. if (count > 31)
  3343. return -EINVAL;
  3344. if (kstrtoul(buffer, 10, &value))
  3345. return -EINVAL;
  3346. if (sony_pic_set_fanspeed(value))
  3347. return -EIO;
  3348. return count;
  3349. }
  3350. static ssize_t sony_pic_fanspeed_show(struct device *dev,
  3351. struct device_attribute *attr, char *buffer)
  3352. {
  3353. u8 value = 0;
  3354. if (sony_pic_get_fanspeed(&value))
  3355. return -EIO;
  3356. return snprintf(buffer, PAGE_SIZE, "%d\n", value);
  3357. }
  3358. #define SPIC_ATTR(_name, _mode) \
  3359. struct device_attribute spic_attr_##_name = __ATTR(_name, \
  3360. _mode, sony_pic_## _name ##_show, \
  3361. sony_pic_## _name ##_store)
  3362. static SPIC_ATTR(bluetoothpower, 0644);
  3363. static SPIC_ATTR(wwanpower, 0644);
  3364. static SPIC_ATTR(fanspeed, 0644);
  3365. static struct attribute *spic_attributes[] = {
  3366. &spic_attr_bluetoothpower.attr,
  3367. &spic_attr_wwanpower.attr,
  3368. &spic_attr_fanspeed.attr,
  3369. NULL
  3370. };
  3371. static struct attribute_group spic_attribute_group = {
  3372. .attrs = spic_attributes
  3373. };
  3374. /******** SONYPI compatibility **********/
  3375. #ifdef CONFIG_SONYPI_COMPAT
  3376. /* battery / brightness / temperature addresses */
  3377. #define SONYPI_BAT_FLAGS 0x81
  3378. #define SONYPI_LCD_LIGHT 0x96
  3379. #define SONYPI_BAT1_PCTRM 0xa0
  3380. #define SONYPI_BAT1_LEFT 0xa2
  3381. #define SONYPI_BAT1_MAXRT 0xa4
  3382. #define SONYPI_BAT2_PCTRM 0xa8
  3383. #define SONYPI_BAT2_LEFT 0xaa
  3384. #define SONYPI_BAT2_MAXRT 0xac
  3385. #define SONYPI_BAT1_MAXTK 0xb0
  3386. #define SONYPI_BAT1_FULL 0xb2
  3387. #define SONYPI_BAT2_MAXTK 0xb8
  3388. #define SONYPI_BAT2_FULL 0xba
  3389. #define SONYPI_TEMP_STATUS 0xC1
  3390. struct sonypi_compat_s {
  3391. struct fasync_struct *fifo_async;
  3392. struct kfifo fifo;
  3393. spinlock_t fifo_lock;
  3394. wait_queue_head_t fifo_proc_list;
  3395. atomic_t open_count;
  3396. };
  3397. static struct sonypi_compat_s sonypi_compat = {
  3398. .open_count = ATOMIC_INIT(0),
  3399. };
  3400. static int sonypi_misc_fasync(int fd, struct file *filp, int on)
  3401. {
  3402. return fasync_helper(fd, filp, on, &sonypi_compat.fifo_async);
  3403. }
  3404. static int sonypi_misc_release(struct inode *inode, struct file *file)
  3405. {
  3406. atomic_dec(&sonypi_compat.open_count);
  3407. return 0;
  3408. }
  3409. static int sonypi_misc_open(struct inode *inode, struct file *file)
  3410. {
  3411. /* Flush input queue on first open */
  3412. unsigned long flags;
  3413. spin_lock_irqsave(&sonypi_compat.fifo_lock, flags);
  3414. if (atomic_inc_return(&sonypi_compat.open_count) == 1)
  3415. kfifo_reset(&sonypi_compat.fifo);
  3416. spin_unlock_irqrestore(&sonypi_compat.fifo_lock, flags);
  3417. return 0;
  3418. }
  3419. static ssize_t sonypi_misc_read(struct file *file, char __user *buf,
  3420. size_t count, loff_t *pos)
  3421. {
  3422. ssize_t ret;
  3423. unsigned char c;
  3424. if ((kfifo_len(&sonypi_compat.fifo) == 0) &&
  3425. (file->f_flags & O_NONBLOCK))
  3426. return -EAGAIN;
  3427. ret = wait_event_interruptible(sonypi_compat.fifo_proc_list,
  3428. kfifo_len(&sonypi_compat.fifo) != 0);
  3429. if (ret)
  3430. return ret;
  3431. while (ret < count &&
  3432. (kfifo_out_locked(&sonypi_compat.fifo, &c, sizeof(c),
  3433. &sonypi_compat.fifo_lock) == sizeof(c))) {
  3434. if (put_user(c, buf++))
  3435. return -EFAULT;
  3436. ret++;
  3437. }
  3438. if (ret > 0) {
  3439. struct inode *inode = file_inode(file);
  3440. inode->i_atime = current_time(inode);
  3441. }
  3442. return ret;
  3443. }
  3444. static unsigned int sonypi_misc_poll(struct file *file, poll_table *wait)
  3445. {
  3446. poll_wait(file, &sonypi_compat.fifo_proc_list, wait);
  3447. if (kfifo_len(&sonypi_compat.fifo))
  3448. return POLLIN | POLLRDNORM;
  3449. return 0;
  3450. }
  3451. static int ec_read16(u8 addr, u16 *value)
  3452. {
  3453. u8 val_lb, val_hb;
  3454. if (ec_read(addr, &val_lb))
  3455. return -1;
  3456. if (ec_read(addr + 1, &val_hb))
  3457. return -1;
  3458. *value = val_lb | (val_hb << 8);
  3459. return 0;
  3460. }
  3461. static long sonypi_misc_ioctl(struct file *fp, unsigned int cmd,
  3462. unsigned long arg)
  3463. {
  3464. int ret = 0;
  3465. void __user *argp = (void __user *)arg;
  3466. u8 val8;
  3467. u16 val16;
  3468. int value;
  3469. mutex_lock(&spic_dev.lock);
  3470. switch (cmd) {
  3471. case SONYPI_IOCGBRT:
  3472. if (sony_bl_props.dev == NULL) {
  3473. ret = -EIO;
  3474. break;
  3475. }
  3476. if (sony_nc_int_call(sony_nc_acpi_handle, "GBRT", NULL,
  3477. &value)) {
  3478. ret = -EIO;
  3479. break;
  3480. }
  3481. val8 = ((value & 0xff) - 1) << 5;
  3482. if (copy_to_user(argp, &val8, sizeof(val8)))
  3483. ret = -EFAULT;
  3484. break;
  3485. case SONYPI_IOCSBRT:
  3486. if (sony_bl_props.dev == NULL) {
  3487. ret = -EIO;
  3488. break;
  3489. }
  3490. if (copy_from_user(&val8, argp, sizeof(val8))) {
  3491. ret = -EFAULT;
  3492. break;
  3493. }
  3494. value = (val8 >> 5) + 1;
  3495. if (sony_nc_int_call(sony_nc_acpi_handle, "SBRT", &value,
  3496. NULL)) {
  3497. ret = -EIO;
  3498. break;
  3499. }
  3500. /* sync the backlight device status */
  3501. sony_bl_props.dev->props.brightness =
  3502. sony_backlight_get_brightness(sony_bl_props.dev);
  3503. break;
  3504. case SONYPI_IOCGBAT1CAP:
  3505. if (ec_read16(SONYPI_BAT1_FULL, &val16)) {
  3506. ret = -EIO;
  3507. break;
  3508. }
  3509. if (copy_to_user(argp, &val16, sizeof(val16)))
  3510. ret = -EFAULT;
  3511. break;
  3512. case SONYPI_IOCGBAT1REM:
  3513. if (ec_read16(SONYPI_BAT1_LEFT, &val16)) {
  3514. ret = -EIO;
  3515. break;
  3516. }
  3517. if (copy_to_user(argp, &val16, sizeof(val16)))
  3518. ret = -EFAULT;
  3519. break;
  3520. case SONYPI_IOCGBAT2CAP:
  3521. if (ec_read16(SONYPI_BAT2_FULL, &val16)) {
  3522. ret = -EIO;
  3523. break;
  3524. }
  3525. if (copy_to_user(argp, &val16, sizeof(val16)))
  3526. ret = -EFAULT;
  3527. break;
  3528. case SONYPI_IOCGBAT2REM:
  3529. if (ec_read16(SONYPI_BAT2_LEFT, &val16)) {
  3530. ret = -EIO;
  3531. break;
  3532. }
  3533. if (copy_to_user(argp, &val16, sizeof(val16)))
  3534. ret = -EFAULT;
  3535. break;
  3536. case SONYPI_IOCGBATFLAGS:
  3537. if (ec_read(SONYPI_BAT_FLAGS, &val8)) {
  3538. ret = -EIO;
  3539. break;
  3540. }
  3541. val8 &= 0x07;
  3542. if (copy_to_user(argp, &val8, sizeof(val8)))
  3543. ret = -EFAULT;
  3544. break;
  3545. case SONYPI_IOCGBLUE:
  3546. val8 = spic_dev.bluetooth_power;
  3547. if (copy_to_user(argp, &val8, sizeof(val8)))
  3548. ret = -EFAULT;
  3549. break;
  3550. case SONYPI_IOCSBLUE:
  3551. if (copy_from_user(&val8, argp, sizeof(val8))) {
  3552. ret = -EFAULT;
  3553. break;
  3554. }
  3555. __sony_pic_set_bluetoothpower(val8);
  3556. break;
  3557. /* FAN Controls */
  3558. case SONYPI_IOCGFAN:
  3559. if (sony_pic_get_fanspeed(&val8)) {
  3560. ret = -EIO;
  3561. break;
  3562. }
  3563. if (copy_to_user(argp, &val8, sizeof(val8)))
  3564. ret = -EFAULT;
  3565. break;
  3566. case SONYPI_IOCSFAN:
  3567. if (copy_from_user(&val8, argp, sizeof(val8))) {
  3568. ret = -EFAULT;
  3569. break;
  3570. }
  3571. if (sony_pic_set_fanspeed(val8))
  3572. ret = -EIO;
  3573. break;
  3574. /* GET Temperature (useful under APM) */
  3575. case SONYPI_IOCGTEMP:
  3576. if (ec_read(SONYPI_TEMP_STATUS, &val8)) {
  3577. ret = -EIO;
  3578. break;
  3579. }
  3580. if (copy_to_user(argp, &val8, sizeof(val8)))
  3581. ret = -EFAULT;
  3582. break;
  3583. default:
  3584. ret = -EINVAL;
  3585. }
  3586. mutex_unlock(&spic_dev.lock);
  3587. return ret;
  3588. }
  3589. static const struct file_operations sonypi_misc_fops = {
  3590. .owner = THIS_MODULE,
  3591. .read = sonypi_misc_read,
  3592. .poll = sonypi_misc_poll,
  3593. .open = sonypi_misc_open,
  3594. .release = sonypi_misc_release,
  3595. .fasync = sonypi_misc_fasync,
  3596. .unlocked_ioctl = sonypi_misc_ioctl,
  3597. .llseek = noop_llseek,
  3598. };
  3599. static struct miscdevice sonypi_misc_device = {
  3600. .minor = MISC_DYNAMIC_MINOR,
  3601. .name = "sonypi",
  3602. .fops = &sonypi_misc_fops,
  3603. };
  3604. static void sonypi_compat_report_event(u8 event)
  3605. {
  3606. kfifo_in_locked(&sonypi_compat.fifo, (unsigned char *)&event,
  3607. sizeof(event), &sonypi_compat.fifo_lock);
  3608. kill_fasync(&sonypi_compat.fifo_async, SIGIO, POLL_IN);
  3609. wake_up_interruptible(&sonypi_compat.fifo_proc_list);
  3610. }
  3611. static int sonypi_compat_init(void)
  3612. {
  3613. int error;
  3614. spin_lock_init(&sonypi_compat.fifo_lock);
  3615. error =
  3616. kfifo_alloc(&sonypi_compat.fifo, SONY_LAPTOP_BUF_SIZE, GFP_KERNEL);
  3617. if (error) {
  3618. pr_err("kfifo_alloc failed\n");
  3619. return error;
  3620. }
  3621. init_waitqueue_head(&sonypi_compat.fifo_proc_list);
  3622. if (minor != -1)
  3623. sonypi_misc_device.minor = minor;
  3624. error = misc_register(&sonypi_misc_device);
  3625. if (error) {
  3626. pr_err("misc_register failed\n");
  3627. goto err_free_kfifo;
  3628. }
  3629. if (minor == -1)
  3630. pr_info("device allocated minor is %d\n",
  3631. sonypi_misc_device.minor);
  3632. return 0;
  3633. err_free_kfifo:
  3634. kfifo_free(&sonypi_compat.fifo);
  3635. return error;
  3636. }
  3637. static void sonypi_compat_exit(void)
  3638. {
  3639. misc_deregister(&sonypi_misc_device);
  3640. kfifo_free(&sonypi_compat.fifo);
  3641. }
  3642. #else
  3643. static int sonypi_compat_init(void) { return 0; }
  3644. static void sonypi_compat_exit(void) { }
  3645. static void sonypi_compat_report_event(u8 event) { }
  3646. #endif /* CONFIG_SONYPI_COMPAT */
  3647. /*
  3648. * ACPI callbacks
  3649. */
  3650. static acpi_status
  3651. sony_pic_read_possible_resource(struct acpi_resource *resource, void *context)
  3652. {
  3653. u32 i;
  3654. struct sony_pic_dev *dev = (struct sony_pic_dev *)context;
  3655. switch (resource->type) {
  3656. case ACPI_RESOURCE_TYPE_START_DEPENDENT:
  3657. {
  3658. /* start IO enumeration */
  3659. struct sony_pic_ioport *ioport = kzalloc(sizeof(*ioport), GFP_KERNEL);
  3660. if (!ioport)
  3661. return AE_ERROR;
  3662. list_add(&ioport->list, &dev->ioports);
  3663. return AE_OK;
  3664. }
  3665. case ACPI_RESOURCE_TYPE_END_DEPENDENT:
  3666. /* end IO enumeration */
  3667. return AE_OK;
  3668. case ACPI_RESOURCE_TYPE_IRQ:
  3669. {
  3670. struct acpi_resource_irq *p = &resource->data.irq;
  3671. struct sony_pic_irq *interrupt = NULL;
  3672. if (!p || !p->interrupt_count) {
  3673. /*
  3674. * IRQ descriptors may have no IRQ# bits set,
  3675. * particularly those those w/ _STA disabled
  3676. */
  3677. dprintk("Blank IRQ resource\n");
  3678. return AE_OK;
  3679. }
  3680. for (i = 0; i < p->interrupt_count; i++) {
  3681. if (!p->interrupts[i]) {
  3682. pr_warn("Invalid IRQ %d\n",
  3683. p->interrupts[i]);
  3684. continue;
  3685. }
  3686. interrupt = kzalloc(sizeof(*interrupt),
  3687. GFP_KERNEL);
  3688. if (!interrupt)
  3689. return AE_ERROR;
  3690. list_add(&interrupt->list, &dev->interrupts);
  3691. interrupt->irq.triggering = p->triggering;
  3692. interrupt->irq.polarity = p->polarity;
  3693. interrupt->irq.sharable = p->sharable;
  3694. interrupt->irq.interrupt_count = 1;
  3695. interrupt->irq.interrupts[0] = p->interrupts[i];
  3696. }
  3697. return AE_OK;
  3698. }
  3699. case ACPI_RESOURCE_TYPE_IO:
  3700. {
  3701. struct acpi_resource_io *io = &resource->data.io;
  3702. struct sony_pic_ioport *ioport =
  3703. list_first_entry(&dev->ioports, struct sony_pic_ioport, list);
  3704. if (!io) {
  3705. dprintk("Blank IO resource\n");
  3706. return AE_OK;
  3707. }
  3708. if (!ioport->io1.minimum) {
  3709. memcpy(&ioport->io1, io, sizeof(*io));
  3710. dprintk("IO1 at 0x%.4x (0x%.2x)\n", ioport->io1.minimum,
  3711. ioport->io1.address_length);
  3712. }
  3713. else if (!ioport->io2.minimum) {
  3714. memcpy(&ioport->io2, io, sizeof(*io));
  3715. dprintk("IO2 at 0x%.4x (0x%.2x)\n", ioport->io2.minimum,
  3716. ioport->io2.address_length);
  3717. }
  3718. else {
  3719. pr_err("Unknown SPIC Type, more than 2 IO Ports\n");
  3720. return AE_ERROR;
  3721. }
  3722. return AE_OK;
  3723. }
  3724. default:
  3725. dprintk("Resource %d isn't an IRQ nor an IO port\n",
  3726. resource->type);
  3727. case ACPI_RESOURCE_TYPE_END_TAG:
  3728. return AE_OK;
  3729. }
  3730. return AE_CTRL_TERMINATE;
  3731. }
  3732. static int sony_pic_possible_resources(struct acpi_device *device)
  3733. {
  3734. int result = 0;
  3735. acpi_status status = AE_OK;
  3736. if (!device)
  3737. return -EINVAL;
  3738. /* get device status */
  3739. /* see acpi_pci_link_get_current acpi_pci_link_get_possible */
  3740. dprintk("Evaluating _STA\n");
  3741. result = acpi_bus_get_status(device);
  3742. if (result) {
  3743. pr_warn("Unable to read status\n");
  3744. goto end;
  3745. }
  3746. if (!device->status.enabled)
  3747. dprintk("Device disabled\n");
  3748. else
  3749. dprintk("Device enabled\n");
  3750. /*
  3751. * Query and parse 'method'
  3752. */
  3753. dprintk("Evaluating %s\n", METHOD_NAME__PRS);
  3754. status = acpi_walk_resources(device->handle, METHOD_NAME__PRS,
  3755. sony_pic_read_possible_resource, &spic_dev);
  3756. if (ACPI_FAILURE(status)) {
  3757. pr_warn("Failure evaluating %s\n", METHOD_NAME__PRS);
  3758. result = -ENODEV;
  3759. }
  3760. end:
  3761. return result;
  3762. }
  3763. /*
  3764. * Disable the spic device by calling its _DIS method
  3765. */
  3766. static int sony_pic_disable(struct acpi_device *device)
  3767. {
  3768. acpi_status ret = acpi_evaluate_object(device->handle, "_DIS", NULL,
  3769. NULL);
  3770. if (ACPI_FAILURE(ret) && ret != AE_NOT_FOUND)
  3771. return -ENXIO;
  3772. dprintk("Device disabled\n");
  3773. return 0;
  3774. }
  3775. /*
  3776. * Based on drivers/acpi/pci_link.c:acpi_pci_link_set
  3777. *
  3778. * Call _SRS to set current resources
  3779. */
  3780. static int sony_pic_enable(struct acpi_device *device,
  3781. struct sony_pic_ioport *ioport, struct sony_pic_irq *irq)
  3782. {
  3783. acpi_status status;
  3784. int result = 0;
  3785. /* Type 1 resource layout is:
  3786. * IO
  3787. * IO
  3788. * IRQNoFlags
  3789. * End
  3790. *
  3791. * Type 2 and 3 resource layout is:
  3792. * IO
  3793. * IRQNoFlags
  3794. * End
  3795. */
  3796. struct {
  3797. struct acpi_resource res1;
  3798. struct acpi_resource res2;
  3799. struct acpi_resource res3;
  3800. struct acpi_resource res4;
  3801. } *resource;
  3802. struct acpi_buffer buffer = { 0, NULL };
  3803. if (!ioport || !irq)
  3804. return -EINVAL;
  3805. /* init acpi_buffer */
  3806. resource = kzalloc(sizeof(*resource) + 1, GFP_KERNEL);
  3807. if (!resource)
  3808. return -ENOMEM;
  3809. buffer.length = sizeof(*resource) + 1;
  3810. buffer.pointer = resource;
  3811. /* setup Type 1 resources */
  3812. if (spic_dev.model == SONYPI_DEVICE_TYPE1) {
  3813. /* setup io resources */
  3814. resource->res1.type = ACPI_RESOURCE_TYPE_IO;
  3815. resource->res1.length = sizeof(struct acpi_resource);
  3816. memcpy(&resource->res1.data.io, &ioport->io1,
  3817. sizeof(struct acpi_resource_io));
  3818. resource->res2.type = ACPI_RESOURCE_TYPE_IO;
  3819. resource->res2.length = sizeof(struct acpi_resource);
  3820. memcpy(&resource->res2.data.io, &ioport->io2,
  3821. sizeof(struct acpi_resource_io));
  3822. /* setup irq resource */
  3823. resource->res3.type = ACPI_RESOURCE_TYPE_IRQ;
  3824. resource->res3.length = sizeof(struct acpi_resource);
  3825. memcpy(&resource->res3.data.irq, &irq->irq,
  3826. sizeof(struct acpi_resource_irq));
  3827. /* we requested a shared irq */
  3828. resource->res3.data.irq.sharable = ACPI_SHARED;
  3829. resource->res4.type = ACPI_RESOURCE_TYPE_END_TAG;
  3830. resource->res4.length = sizeof(struct acpi_resource);
  3831. }
  3832. /* setup Type 2/3 resources */
  3833. else {
  3834. /* setup io resource */
  3835. resource->res1.type = ACPI_RESOURCE_TYPE_IO;
  3836. resource->res1.length = sizeof(struct acpi_resource);
  3837. memcpy(&resource->res1.data.io, &ioport->io1,
  3838. sizeof(struct acpi_resource_io));
  3839. /* setup irq resource */
  3840. resource->res2.type = ACPI_RESOURCE_TYPE_IRQ;
  3841. resource->res2.length = sizeof(struct acpi_resource);
  3842. memcpy(&resource->res2.data.irq, &irq->irq,
  3843. sizeof(struct acpi_resource_irq));
  3844. /* we requested a shared irq */
  3845. resource->res2.data.irq.sharable = ACPI_SHARED;
  3846. resource->res3.type = ACPI_RESOURCE_TYPE_END_TAG;
  3847. resource->res3.length = sizeof(struct acpi_resource);
  3848. }
  3849. /* Attempt to set the resource */
  3850. dprintk("Evaluating _SRS\n");
  3851. status = acpi_set_current_resources(device->handle, &buffer);
  3852. /* check for total failure */
  3853. if (ACPI_FAILURE(status)) {
  3854. pr_err("Error evaluating _SRS\n");
  3855. result = -ENODEV;
  3856. goto end;
  3857. }
  3858. /* Necessary device initializations calls (from sonypi) */
  3859. sony_pic_call1(0x82);
  3860. sony_pic_call2(0x81, 0xff);
  3861. sony_pic_call1(compat ? 0x92 : 0x82);
  3862. end:
  3863. kfree(resource);
  3864. return result;
  3865. }
  3866. /*****************
  3867. *
  3868. * ISR: some event is available
  3869. *
  3870. *****************/
  3871. static irqreturn_t sony_pic_irq(int irq, void *dev_id)
  3872. {
  3873. int i, j;
  3874. u8 ev = 0;
  3875. u8 data_mask = 0;
  3876. u8 device_event = 0;
  3877. struct sony_pic_dev *dev = (struct sony_pic_dev *) dev_id;
  3878. ev = inb_p(dev->cur_ioport->io1.minimum);
  3879. if (dev->cur_ioport->io2.minimum)
  3880. data_mask = inb_p(dev->cur_ioport->io2.minimum);
  3881. else
  3882. data_mask = inb_p(dev->cur_ioport->io1.minimum +
  3883. dev->evport_offset);
  3884. dprintk("event ([%.2x] [%.2x]) at port 0x%.4x(+0x%.2x)\n",
  3885. ev, data_mask, dev->cur_ioport->io1.minimum,
  3886. dev->evport_offset);
  3887. if (ev == 0x00 || ev == 0xff)
  3888. return IRQ_HANDLED;
  3889. for (i = 0; dev->event_types[i].mask; i++) {
  3890. if ((data_mask & dev->event_types[i].data) !=
  3891. dev->event_types[i].data)
  3892. continue;
  3893. if (!(mask & dev->event_types[i].mask))
  3894. continue;
  3895. for (j = 0; dev->event_types[i].events[j].event; j++) {
  3896. if (ev == dev->event_types[i].events[j].data) {
  3897. device_event =
  3898. dev->event_types[i].events[j].event;
  3899. /* some events may require ignoring */
  3900. if (!device_event)
  3901. return IRQ_HANDLED;
  3902. goto found;
  3903. }
  3904. }
  3905. }
  3906. /* Still not able to decode the event try to pass
  3907. * it over to the minidriver
  3908. */
  3909. if (dev->handle_irq && dev->handle_irq(data_mask, ev) == 0)
  3910. return IRQ_HANDLED;
  3911. dprintk("unknown event ([%.2x] [%.2x]) at port 0x%.4x(+0x%.2x)\n",
  3912. ev, data_mask, dev->cur_ioport->io1.minimum,
  3913. dev->evport_offset);
  3914. return IRQ_HANDLED;
  3915. found:
  3916. sony_laptop_report_input_event(device_event);
  3917. sonypi_compat_report_event(device_event);
  3918. return IRQ_HANDLED;
  3919. }
  3920. /*****************
  3921. *
  3922. * ACPI driver
  3923. *
  3924. *****************/
  3925. static int sony_pic_remove(struct acpi_device *device)
  3926. {
  3927. struct sony_pic_ioport *io, *tmp_io;
  3928. struct sony_pic_irq *irq, *tmp_irq;
  3929. if (sony_pic_disable(device)) {
  3930. pr_err("Couldn't disable device\n");
  3931. return -ENXIO;
  3932. }
  3933. free_irq(spic_dev.cur_irq->irq.interrupts[0], &spic_dev);
  3934. release_region(spic_dev.cur_ioport->io1.minimum,
  3935. spic_dev.cur_ioport->io1.address_length);
  3936. if (spic_dev.cur_ioport->io2.minimum)
  3937. release_region(spic_dev.cur_ioport->io2.minimum,
  3938. spic_dev.cur_ioport->io2.address_length);
  3939. sonypi_compat_exit();
  3940. sony_laptop_remove_input();
  3941. /* pf attrs */
  3942. sysfs_remove_group(&sony_pf_device->dev.kobj, &spic_attribute_group);
  3943. sony_pf_remove();
  3944. list_for_each_entry_safe(io, tmp_io, &spic_dev.ioports, list) {
  3945. list_del(&io->list);
  3946. kfree(io);
  3947. }
  3948. list_for_each_entry_safe(irq, tmp_irq, &spic_dev.interrupts, list) {
  3949. list_del(&irq->list);
  3950. kfree(irq);
  3951. }
  3952. spic_dev.cur_ioport = NULL;
  3953. spic_dev.cur_irq = NULL;
  3954. dprintk(SONY_PIC_DRIVER_NAME " removed.\n");
  3955. return 0;
  3956. }
  3957. static int sony_pic_add(struct acpi_device *device)
  3958. {
  3959. int result;
  3960. struct sony_pic_ioport *io, *tmp_io;
  3961. struct sony_pic_irq *irq, *tmp_irq;
  3962. spic_dev.acpi_dev = device;
  3963. strcpy(acpi_device_class(device), "sony/hotkey");
  3964. sony_pic_detect_device_type(&spic_dev);
  3965. mutex_init(&spic_dev.lock);
  3966. /* read _PRS resources */
  3967. result = sony_pic_possible_resources(device);
  3968. if (result) {
  3969. pr_err("Unable to read possible resources\n");
  3970. goto err_free_resources;
  3971. }
  3972. /* setup input devices and helper fifo */
  3973. result = sony_laptop_setup_input(device);
  3974. if (result) {
  3975. pr_err("Unable to create input devices\n");
  3976. goto err_free_resources;
  3977. }
  3978. result = sonypi_compat_init();
  3979. if (result)
  3980. goto err_remove_input;
  3981. /* request io port */
  3982. list_for_each_entry_reverse(io, &spic_dev.ioports, list) {
  3983. if (request_region(io->io1.minimum, io->io1.address_length,
  3984. "Sony Programmable I/O Device")) {
  3985. dprintk("I/O port1: 0x%.4x (0x%.4x) + 0x%.2x\n",
  3986. io->io1.minimum, io->io1.maximum,
  3987. io->io1.address_length);
  3988. /* Type 1 have 2 ioports */
  3989. if (io->io2.minimum) {
  3990. if (request_region(io->io2.minimum,
  3991. io->io2.address_length,
  3992. "Sony Programmable I/O Device")) {
  3993. dprintk("I/O port2: 0x%.4x (0x%.4x) + 0x%.2x\n",
  3994. io->io2.minimum, io->io2.maximum,
  3995. io->io2.address_length);
  3996. spic_dev.cur_ioport = io;
  3997. break;
  3998. }
  3999. else {
  4000. dprintk("Unable to get I/O port2: "
  4001. "0x%.4x (0x%.4x) + 0x%.2x\n",
  4002. io->io2.minimum, io->io2.maximum,
  4003. io->io2.address_length);
  4004. release_region(io->io1.minimum,
  4005. io->io1.address_length);
  4006. }
  4007. }
  4008. else {
  4009. spic_dev.cur_ioport = io;
  4010. break;
  4011. }
  4012. }
  4013. }
  4014. if (!spic_dev.cur_ioport) {
  4015. pr_err("Failed to request_region\n");
  4016. result = -ENODEV;
  4017. goto err_remove_compat;
  4018. }
  4019. /* request IRQ */
  4020. list_for_each_entry_reverse(irq, &spic_dev.interrupts, list) {
  4021. if (!request_irq(irq->irq.interrupts[0], sony_pic_irq,
  4022. 0, "sony-laptop", &spic_dev)) {
  4023. dprintk("IRQ: %d - triggering: %d - "
  4024. "polarity: %d - shr: %d\n",
  4025. irq->irq.interrupts[0],
  4026. irq->irq.triggering,
  4027. irq->irq.polarity,
  4028. irq->irq.sharable);
  4029. spic_dev.cur_irq = irq;
  4030. break;
  4031. }
  4032. }
  4033. if (!spic_dev.cur_irq) {
  4034. pr_err("Failed to request_irq\n");
  4035. result = -ENODEV;
  4036. goto err_release_region;
  4037. }
  4038. /* set resource status _SRS */
  4039. result = sony_pic_enable(device, spic_dev.cur_ioport, spic_dev.cur_irq);
  4040. if (result) {
  4041. pr_err("Couldn't enable device\n");
  4042. goto err_free_irq;
  4043. }
  4044. spic_dev.bluetooth_power = -1;
  4045. /* create device attributes */
  4046. result = sony_pf_add();
  4047. if (result)
  4048. goto err_disable_device;
  4049. result = sysfs_create_group(&sony_pf_device->dev.kobj, &spic_attribute_group);
  4050. if (result)
  4051. goto err_remove_pf;
  4052. pr_info("SPIC setup done.\n");
  4053. return 0;
  4054. err_remove_pf:
  4055. sony_pf_remove();
  4056. err_disable_device:
  4057. sony_pic_disable(device);
  4058. err_free_irq:
  4059. free_irq(spic_dev.cur_irq->irq.interrupts[0], &spic_dev);
  4060. err_release_region:
  4061. release_region(spic_dev.cur_ioport->io1.minimum,
  4062. spic_dev.cur_ioport->io1.address_length);
  4063. if (spic_dev.cur_ioport->io2.minimum)
  4064. release_region(spic_dev.cur_ioport->io2.minimum,
  4065. spic_dev.cur_ioport->io2.address_length);
  4066. err_remove_compat:
  4067. sonypi_compat_exit();
  4068. err_remove_input:
  4069. sony_laptop_remove_input();
  4070. err_free_resources:
  4071. list_for_each_entry_safe(io, tmp_io, &spic_dev.ioports, list) {
  4072. list_del(&io->list);
  4073. kfree(io);
  4074. }
  4075. list_for_each_entry_safe(irq, tmp_irq, &spic_dev.interrupts, list) {
  4076. list_del(&irq->list);
  4077. kfree(irq);
  4078. }
  4079. spic_dev.cur_ioport = NULL;
  4080. spic_dev.cur_irq = NULL;
  4081. return result;
  4082. }
  4083. #ifdef CONFIG_PM_SLEEP
  4084. static int sony_pic_suspend(struct device *dev)
  4085. {
  4086. if (sony_pic_disable(to_acpi_device(dev)))
  4087. return -ENXIO;
  4088. return 0;
  4089. }
  4090. static int sony_pic_resume(struct device *dev)
  4091. {
  4092. sony_pic_enable(to_acpi_device(dev),
  4093. spic_dev.cur_ioport, spic_dev.cur_irq);
  4094. return 0;
  4095. }
  4096. #endif
  4097. static SIMPLE_DEV_PM_OPS(sony_pic_pm, sony_pic_suspend, sony_pic_resume);
  4098. static const struct acpi_device_id sony_pic_device_ids[] = {
  4099. {SONY_PIC_HID, 0},
  4100. {"", 0},
  4101. };
  4102. static struct acpi_driver sony_pic_driver = {
  4103. .name = SONY_PIC_DRIVER_NAME,
  4104. .class = SONY_PIC_CLASS,
  4105. .ids = sony_pic_device_ids,
  4106. .owner = THIS_MODULE,
  4107. .ops = {
  4108. .add = sony_pic_add,
  4109. .remove = sony_pic_remove,
  4110. },
  4111. .drv.pm = &sony_pic_pm,
  4112. };
  4113. static struct dmi_system_id __initdata sonypi_dmi_table[] = {
  4114. {
  4115. .ident = "Sony Vaio",
  4116. .matches = {
  4117. DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
  4118. DMI_MATCH(DMI_PRODUCT_NAME, "PCG-"),
  4119. },
  4120. },
  4121. {
  4122. .ident = "Sony Vaio",
  4123. .matches = {
  4124. DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
  4125. DMI_MATCH(DMI_PRODUCT_NAME, "VGN-"),
  4126. },
  4127. },
  4128. { }
  4129. };
  4130. static int __init sony_laptop_init(void)
  4131. {
  4132. int result;
  4133. if (!no_spic && dmi_check_system(sonypi_dmi_table)) {
  4134. result = acpi_bus_register_driver(&sony_pic_driver);
  4135. if (result) {
  4136. pr_err("Unable to register SPIC driver\n");
  4137. goto out;
  4138. }
  4139. spic_drv_registered = 1;
  4140. }
  4141. result = acpi_bus_register_driver(&sony_nc_driver);
  4142. if (result) {
  4143. pr_err("Unable to register SNC driver\n");
  4144. goto out_unregister_pic;
  4145. }
  4146. return 0;
  4147. out_unregister_pic:
  4148. if (spic_drv_registered)
  4149. acpi_bus_unregister_driver(&sony_pic_driver);
  4150. out:
  4151. return result;
  4152. }
  4153. static void __exit sony_laptop_exit(void)
  4154. {
  4155. acpi_bus_unregister_driver(&sony_nc_driver);
  4156. if (spic_drv_registered)
  4157. acpi_bus_unregister_driver(&sony_pic_driver);
  4158. }
  4159. module_init(sony_laptop_init);
  4160. module_exit(sony_laptop_exit);