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