sony-laptop.c 110 KB

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