sony-laptop.c 119 KB

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