sony-laptop.c 120 KB

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