sony-laptop.c 111 KB

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