sony-laptop.c 108 KB

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