sony-laptop.c 120 KB

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