sony-laptop.c 115 KB

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