sony-laptop.c 111 KB

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