sony-laptop.c 111 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511
  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. default:
  1071. dprintk("Unknown event 0x%x for handle 0x%x\n",
  1072. event, handle);
  1073. break;
  1074. }
  1075. /* clear the event (and the event reason when present) */
  1076. arg = 1 << offset;
  1077. sony_nc_int_call(sony_nc_acpi_handle, "SN05", &arg, &result);
  1078. } else {
  1079. /* old style event */
  1080. ev_type = HOTKEY;
  1081. sony_laptop_report_input_event(real_ev);
  1082. }
  1083. acpi_bus_generate_proc_event(sony_nc_acpi_device, ev_type, real_ev);
  1084. acpi_bus_generate_netlink_event(sony_nc_acpi_device->pnp.device_class,
  1085. dev_name(&sony_nc_acpi_device->dev), ev_type, real_ev);
  1086. }
  1087. static acpi_status sony_walk_callback(acpi_handle handle, u32 level,
  1088. void *context, void **return_value)
  1089. {
  1090. struct acpi_device_info *info;
  1091. if (ACPI_SUCCESS(acpi_get_object_info(handle, &info))) {
  1092. pr_warn("method: name: %4.4s, args %X\n",
  1093. (char *)&info->name, info->param_count);
  1094. kfree(info);
  1095. }
  1096. return AE_OK;
  1097. }
  1098. /*
  1099. * ACPI device
  1100. */
  1101. static void sony_nc_function_setup(struct acpi_device *device,
  1102. struct platform_device *pf_device)
  1103. {
  1104. unsigned int i, result, bitmask, arg;
  1105. if (!handles)
  1106. return;
  1107. /* setup found handles here */
  1108. for (i = 0; i < ARRAY_SIZE(handles->cap); i++) {
  1109. unsigned int handle = handles->cap[i];
  1110. if (!handle)
  1111. continue;
  1112. dprintk("setting up handle 0x%.4x\n", handle);
  1113. switch (handle) {
  1114. case 0x0100:
  1115. case 0x0101:
  1116. case 0x0127:
  1117. /* setup hotkeys */
  1118. sony_call_snc_handle(handle, 0, &result);
  1119. break;
  1120. case 0x0102:
  1121. /* setup hotkeys */
  1122. sony_call_snc_handle(handle, 0x100, &result);
  1123. break;
  1124. case 0x0105:
  1125. case 0x0148:
  1126. /* touchpad enable/disable */
  1127. result = sony_nc_touchpad_setup(pf_device, handle);
  1128. if (result)
  1129. pr_err("couldn't set up touchpad control function (%d)\n",
  1130. result);
  1131. break;
  1132. case 0x0115:
  1133. case 0x0136:
  1134. case 0x013f:
  1135. result = sony_nc_battery_care_setup(pf_device, handle);
  1136. if (result)
  1137. pr_err("couldn't set up battery care function (%d)\n",
  1138. result);
  1139. break;
  1140. case 0x0119:
  1141. result = sony_nc_lid_resume_setup(pf_device);
  1142. if (result)
  1143. pr_err("couldn't set up lid resume function (%d)\n",
  1144. result);
  1145. break;
  1146. case 0x0122:
  1147. result = sony_nc_thermal_setup(pf_device);
  1148. if (result)
  1149. pr_err("couldn't set up thermal profile function (%d)\n",
  1150. result);
  1151. break;
  1152. case 0x0128:
  1153. case 0x0146:
  1154. result = sony_nc_gfx_switch_setup(pf_device, handle);
  1155. if (result)
  1156. pr_err("couldn't set up GFX Switch status (%d)\n",
  1157. result);
  1158. break;
  1159. case 0x0131:
  1160. result = sony_nc_highspeed_charging_setup(pf_device);
  1161. if (result)
  1162. pr_err("couldn't set up high speed charging function (%d)\n",
  1163. result);
  1164. break;
  1165. case 0x0124:
  1166. case 0x0135:
  1167. result = sony_nc_rfkill_setup(device, handle);
  1168. if (result)
  1169. pr_err("couldn't set up rfkill support (%d)\n",
  1170. result);
  1171. break;
  1172. case 0x0137:
  1173. case 0x0143:
  1174. case 0x014b:
  1175. case 0x014c:
  1176. result = sony_nc_kbd_backlight_setup(pf_device, handle);
  1177. if (result)
  1178. pr_err("couldn't set up keyboard backlight function (%d)\n",
  1179. result);
  1180. break;
  1181. default:
  1182. continue;
  1183. }
  1184. }
  1185. /* Enable all events */
  1186. arg = 0x10;
  1187. if (!sony_nc_int_call(sony_nc_acpi_handle, "SN00", &arg, &bitmask))
  1188. sony_nc_int_call(sony_nc_acpi_handle, "SN02", &bitmask,
  1189. &result);
  1190. }
  1191. static void sony_nc_function_cleanup(struct platform_device *pd)
  1192. {
  1193. unsigned int i, result, bitmask, handle;
  1194. /* get enabled events and disable them */
  1195. sony_nc_int_call(sony_nc_acpi_handle, "SN01", NULL, &bitmask);
  1196. sony_nc_int_call(sony_nc_acpi_handle, "SN03", &bitmask, &result);
  1197. /* cleanup handles here */
  1198. for (i = 0; i < ARRAY_SIZE(handles->cap); i++) {
  1199. handle = handles->cap[i];
  1200. if (!handle)
  1201. continue;
  1202. switch (handle) {
  1203. case 0x0105:
  1204. case 0x0148:
  1205. sony_nc_touchpad_cleanup(pd);
  1206. break;
  1207. case 0x0115:
  1208. case 0x0136:
  1209. case 0x013f:
  1210. sony_nc_battery_care_cleanup(pd);
  1211. break;
  1212. case 0x0119:
  1213. sony_nc_lid_resume_cleanup(pd);
  1214. break;
  1215. case 0x0122:
  1216. sony_nc_thermal_cleanup(pd);
  1217. break;
  1218. case 0x0128:
  1219. case 0x0146:
  1220. sony_nc_gfx_switch_cleanup(pd);
  1221. break;
  1222. case 0x0131:
  1223. sony_nc_highspeed_charging_cleanup(pd);
  1224. break;
  1225. case 0x0124:
  1226. case 0x0135:
  1227. sony_nc_rfkill_cleanup();
  1228. break;
  1229. case 0x0137:
  1230. case 0x0143:
  1231. case 0x014b:
  1232. case 0x014c:
  1233. sony_nc_kbd_backlight_cleanup(pd);
  1234. break;
  1235. default:
  1236. continue;
  1237. }
  1238. }
  1239. /* finally cleanup the handles list */
  1240. sony_nc_handles_cleanup(pd);
  1241. }
  1242. #ifdef CONFIG_PM_SLEEP
  1243. static void sony_nc_function_resume(void)
  1244. {
  1245. unsigned int i, result, bitmask, arg;
  1246. dprintk("Resuming SNC device\n");
  1247. for (i = 0; i < ARRAY_SIZE(handles->cap); i++) {
  1248. unsigned int handle = handles->cap[i];
  1249. if (!handle)
  1250. continue;
  1251. switch (handle) {
  1252. case 0x0100:
  1253. case 0x0101:
  1254. case 0x0127:
  1255. /* re-enable hotkeys */
  1256. sony_call_snc_handle(handle, 0, &result);
  1257. break;
  1258. case 0x0102:
  1259. /* re-enable hotkeys */
  1260. sony_call_snc_handle(handle, 0x100, &result);
  1261. break;
  1262. case 0x0122:
  1263. sony_nc_thermal_resume();
  1264. break;
  1265. case 0x0124:
  1266. case 0x0135:
  1267. sony_nc_rfkill_update();
  1268. break;
  1269. case 0x0137:
  1270. case 0x0143:
  1271. case 0x014b:
  1272. case 0x014c:
  1273. sony_nc_kbd_backlight_resume();
  1274. break;
  1275. default:
  1276. continue;
  1277. }
  1278. }
  1279. /* Enable all events */
  1280. arg = 0x10;
  1281. if (!sony_nc_int_call(sony_nc_acpi_handle, "SN00", &arg, &bitmask))
  1282. sony_nc_int_call(sony_nc_acpi_handle, "SN02", &bitmask,
  1283. &result);
  1284. }
  1285. static int sony_nc_resume(struct device *dev)
  1286. {
  1287. struct sony_nc_value *item;
  1288. acpi_handle handle;
  1289. for (item = sony_nc_values; item->name; item++) {
  1290. int ret;
  1291. if (!item->valid)
  1292. continue;
  1293. ret = sony_nc_int_call(sony_nc_acpi_handle, *item->acpiset,
  1294. &item->value, NULL);
  1295. if (ret < 0) {
  1296. pr_err("%s: %d\n", __func__, ret);
  1297. break;
  1298. }
  1299. }
  1300. if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle, "ECON",
  1301. &handle))) {
  1302. int arg = 1;
  1303. if (sony_nc_int_call(sony_nc_acpi_handle, "ECON", &arg, NULL))
  1304. dprintk("ECON Method failed\n");
  1305. }
  1306. if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle, "SN00",
  1307. &handle)))
  1308. sony_nc_function_resume();
  1309. return 0;
  1310. }
  1311. #endif
  1312. static SIMPLE_DEV_PM_OPS(sony_nc_pm, NULL, sony_nc_resume);
  1313. static void sony_nc_rfkill_cleanup(void)
  1314. {
  1315. int i;
  1316. for (i = 0; i < N_SONY_RFKILL; i++) {
  1317. if (sony_rfkill_devices[i]) {
  1318. rfkill_unregister(sony_rfkill_devices[i]);
  1319. rfkill_destroy(sony_rfkill_devices[i]);
  1320. }
  1321. }
  1322. }
  1323. static int sony_nc_rfkill_set(void *data, bool blocked)
  1324. {
  1325. int result;
  1326. int argument = sony_rfkill_address[(long) data] + 0x100;
  1327. if (!blocked)
  1328. argument |= 0x070000;
  1329. return sony_call_snc_handle(sony_rfkill_handle, argument, &result);
  1330. }
  1331. static const struct rfkill_ops sony_rfkill_ops = {
  1332. .set_block = sony_nc_rfkill_set,
  1333. };
  1334. static int sony_nc_setup_rfkill(struct acpi_device *device,
  1335. enum sony_nc_rfkill nc_type)
  1336. {
  1337. int err = 0;
  1338. struct rfkill *rfk;
  1339. enum rfkill_type type;
  1340. const char *name;
  1341. int result;
  1342. bool hwblock, swblock;
  1343. switch (nc_type) {
  1344. case SONY_WIFI:
  1345. type = RFKILL_TYPE_WLAN;
  1346. name = "sony-wifi";
  1347. break;
  1348. case SONY_BLUETOOTH:
  1349. type = RFKILL_TYPE_BLUETOOTH;
  1350. name = "sony-bluetooth";
  1351. break;
  1352. case SONY_WWAN:
  1353. type = RFKILL_TYPE_WWAN;
  1354. name = "sony-wwan";
  1355. break;
  1356. case SONY_WIMAX:
  1357. type = RFKILL_TYPE_WIMAX;
  1358. name = "sony-wimax";
  1359. break;
  1360. default:
  1361. return -EINVAL;
  1362. }
  1363. rfk = rfkill_alloc(name, &device->dev, type,
  1364. &sony_rfkill_ops, (void *)nc_type);
  1365. if (!rfk)
  1366. return -ENOMEM;
  1367. if (sony_call_snc_handle(sony_rfkill_handle, 0x200, &result) < 0) {
  1368. rfkill_destroy(rfk);
  1369. return -1;
  1370. }
  1371. hwblock = !(result & 0x1);
  1372. if (sony_call_snc_handle(sony_rfkill_handle,
  1373. sony_rfkill_address[nc_type],
  1374. &result) < 0) {
  1375. rfkill_destroy(rfk);
  1376. return -1;
  1377. }
  1378. swblock = !(result & 0x2);
  1379. rfkill_init_sw_state(rfk, swblock);
  1380. rfkill_set_hw_state(rfk, hwblock);
  1381. err = rfkill_register(rfk);
  1382. if (err) {
  1383. rfkill_destroy(rfk);
  1384. return err;
  1385. }
  1386. sony_rfkill_devices[nc_type] = rfk;
  1387. return err;
  1388. }
  1389. static void sony_nc_rfkill_update(void)
  1390. {
  1391. enum sony_nc_rfkill i;
  1392. int result;
  1393. bool hwblock;
  1394. sony_call_snc_handle(sony_rfkill_handle, 0x200, &result);
  1395. hwblock = !(result & 0x1);
  1396. for (i = 0; i < N_SONY_RFKILL; i++) {
  1397. int argument = sony_rfkill_address[i];
  1398. if (!sony_rfkill_devices[i])
  1399. continue;
  1400. if (hwblock) {
  1401. if (rfkill_set_hw_state(sony_rfkill_devices[i], true)) {
  1402. /* we already know we're blocked */
  1403. }
  1404. continue;
  1405. }
  1406. sony_call_snc_handle(sony_rfkill_handle, argument, &result);
  1407. rfkill_set_states(sony_rfkill_devices[i],
  1408. !(result & 0x2), false);
  1409. }
  1410. }
  1411. static int sony_nc_rfkill_setup(struct acpi_device *device,
  1412. unsigned int handle)
  1413. {
  1414. u64 offset;
  1415. int i;
  1416. unsigned char buffer[32] = { 0 };
  1417. offset = sony_find_snc_handle(handle);
  1418. sony_rfkill_handle = handle;
  1419. i = sony_nc_buffer_call(sony_nc_acpi_handle, "SN06", &offset, buffer,
  1420. 32);
  1421. if (i < 0)
  1422. return i;
  1423. /* The buffer is filled with magic numbers describing the devices
  1424. * available, 0xff terminates the enumeration.
  1425. * Known codes:
  1426. * 0x00 WLAN
  1427. * 0x10 BLUETOOTH
  1428. * 0x20 WWAN GPRS-EDGE
  1429. * 0x21 WWAN HSDPA
  1430. * 0x22 WWAN EV-DO
  1431. * 0x23 WWAN GPS
  1432. * 0x25 Gobi WWAN no GPS
  1433. * 0x26 Gobi WWAN + GPS
  1434. * 0x28 Gobi WWAN no GPS
  1435. * 0x29 Gobi WWAN + GPS
  1436. * 0x30 WIMAX
  1437. * 0x50 Gobi WWAN no GPS
  1438. * 0x51 Gobi WWAN + GPS
  1439. * 0x70 no SIM card slot
  1440. * 0x71 SIM card slot
  1441. */
  1442. for (i = 0; i < ARRAY_SIZE(buffer); i++) {
  1443. if (buffer[i] == 0xff)
  1444. break;
  1445. dprintk("Radio devices, found 0x%.2x\n", buffer[i]);
  1446. if (buffer[i] == 0 && !sony_rfkill_devices[SONY_WIFI])
  1447. sony_nc_setup_rfkill(device, SONY_WIFI);
  1448. if (buffer[i] == 0x10 && !sony_rfkill_devices[SONY_BLUETOOTH])
  1449. sony_nc_setup_rfkill(device, SONY_BLUETOOTH);
  1450. if (((0xf0 & buffer[i]) == 0x20 ||
  1451. (0xf0 & buffer[i]) == 0x50) &&
  1452. !sony_rfkill_devices[SONY_WWAN])
  1453. sony_nc_setup_rfkill(device, SONY_WWAN);
  1454. if (buffer[i] == 0x30 && !sony_rfkill_devices[SONY_WIMAX])
  1455. sony_nc_setup_rfkill(device, SONY_WIMAX);
  1456. }
  1457. return 0;
  1458. }
  1459. /* Keyboard backlight feature */
  1460. struct kbd_backlight {
  1461. unsigned int handle;
  1462. unsigned int base;
  1463. unsigned int mode;
  1464. unsigned int timeout;
  1465. struct device_attribute mode_attr;
  1466. struct device_attribute timeout_attr;
  1467. };
  1468. static struct kbd_backlight *kbdbl_ctl;
  1469. static ssize_t __sony_nc_kbd_backlight_mode_set(u8 value)
  1470. {
  1471. int result;
  1472. if (value > 1)
  1473. return -EINVAL;
  1474. if (sony_call_snc_handle(kbdbl_ctl->handle,
  1475. (value << 0x10) | (kbdbl_ctl->base), &result))
  1476. return -EIO;
  1477. /* Try to turn the light on/off immediately */
  1478. sony_call_snc_handle(kbdbl_ctl->handle,
  1479. (value << 0x10) | (kbdbl_ctl->base + 0x100), &result);
  1480. kbdbl_ctl->mode = value;
  1481. return 0;
  1482. }
  1483. static ssize_t sony_nc_kbd_backlight_mode_store(struct device *dev,
  1484. struct device_attribute *attr,
  1485. const char *buffer, size_t count)
  1486. {
  1487. int ret = 0;
  1488. unsigned long value;
  1489. if (count > 31)
  1490. return -EINVAL;
  1491. if (kstrtoul(buffer, 10, &value))
  1492. return -EINVAL;
  1493. ret = __sony_nc_kbd_backlight_mode_set(value);
  1494. if (ret < 0)
  1495. return ret;
  1496. return count;
  1497. }
  1498. static ssize_t sony_nc_kbd_backlight_mode_show(struct device *dev,
  1499. struct device_attribute *attr, char *buffer)
  1500. {
  1501. ssize_t count = 0;
  1502. count = snprintf(buffer, PAGE_SIZE, "%d\n", kbdbl_ctl->mode);
  1503. return count;
  1504. }
  1505. static int __sony_nc_kbd_backlight_timeout_set(u8 value)
  1506. {
  1507. int result;
  1508. if (value > 3)
  1509. return -EINVAL;
  1510. if (sony_call_snc_handle(kbdbl_ctl->handle, (value << 0x10) |
  1511. (kbdbl_ctl->base + 0x200), &result))
  1512. return -EIO;
  1513. kbdbl_ctl->timeout = value;
  1514. return 0;
  1515. }
  1516. static ssize_t sony_nc_kbd_backlight_timeout_store(struct device *dev,
  1517. struct device_attribute *attr,
  1518. const char *buffer, size_t count)
  1519. {
  1520. int ret = 0;
  1521. unsigned long value;
  1522. if (count > 31)
  1523. return -EINVAL;
  1524. if (kstrtoul(buffer, 10, &value))
  1525. return -EINVAL;
  1526. ret = __sony_nc_kbd_backlight_timeout_set(value);
  1527. if (ret < 0)
  1528. return ret;
  1529. return count;
  1530. }
  1531. static ssize_t sony_nc_kbd_backlight_timeout_show(struct device *dev,
  1532. struct device_attribute *attr, char *buffer)
  1533. {
  1534. ssize_t count = 0;
  1535. count = snprintf(buffer, PAGE_SIZE, "%d\n", kbdbl_ctl->timeout);
  1536. return count;
  1537. }
  1538. static int sony_nc_kbd_backlight_setup(struct platform_device *pd,
  1539. unsigned int handle)
  1540. {
  1541. int result;
  1542. int ret = 0;
  1543. /* verify the kbd backlight presence, these handles are not used for
  1544. * keyboard backlight only
  1545. */
  1546. ret = sony_call_snc_handle(handle, handle == 0x0137 ? 0x0B00 : 0x0100,
  1547. &result);
  1548. if (ret)
  1549. return ret;
  1550. if ((handle == 0x0137 && !(result & 0x02)) ||
  1551. !(result & 0x01)) {
  1552. dprintk("no backlight keyboard found\n");
  1553. return 0;
  1554. }
  1555. kbdbl_ctl = kzalloc(sizeof(*kbdbl_ctl), GFP_KERNEL);
  1556. if (!kbdbl_ctl)
  1557. return -ENOMEM;
  1558. kbdbl_ctl->handle = handle;
  1559. if (handle == 0x0137)
  1560. kbdbl_ctl->base = 0x0C00;
  1561. else
  1562. kbdbl_ctl->base = 0x4000;
  1563. sysfs_attr_init(&kbdbl_ctl->mode_attr.attr);
  1564. kbdbl_ctl->mode_attr.attr.name = "kbd_backlight";
  1565. kbdbl_ctl->mode_attr.attr.mode = S_IRUGO | S_IWUSR;
  1566. kbdbl_ctl->mode_attr.show = sony_nc_kbd_backlight_mode_show;
  1567. kbdbl_ctl->mode_attr.store = sony_nc_kbd_backlight_mode_store;
  1568. sysfs_attr_init(&kbdbl_ctl->timeout_attr.attr);
  1569. kbdbl_ctl->timeout_attr.attr.name = "kbd_backlight_timeout";
  1570. kbdbl_ctl->timeout_attr.attr.mode = S_IRUGO | S_IWUSR;
  1571. kbdbl_ctl->timeout_attr.show = sony_nc_kbd_backlight_timeout_show;
  1572. kbdbl_ctl->timeout_attr.store = sony_nc_kbd_backlight_timeout_store;
  1573. ret = device_create_file(&pd->dev, &kbdbl_ctl->mode_attr);
  1574. if (ret)
  1575. goto outkzalloc;
  1576. ret = device_create_file(&pd->dev, &kbdbl_ctl->timeout_attr);
  1577. if (ret)
  1578. goto outmode;
  1579. __sony_nc_kbd_backlight_mode_set(kbd_backlight);
  1580. __sony_nc_kbd_backlight_timeout_set(kbd_backlight_timeout);
  1581. return 0;
  1582. outmode:
  1583. device_remove_file(&pd->dev, &kbdbl_ctl->mode_attr);
  1584. outkzalloc:
  1585. kfree(kbdbl_ctl);
  1586. kbdbl_ctl = NULL;
  1587. return ret;
  1588. }
  1589. static void sony_nc_kbd_backlight_cleanup(struct platform_device *pd)
  1590. {
  1591. if (kbdbl_ctl) {
  1592. int result;
  1593. device_remove_file(&pd->dev, &kbdbl_ctl->mode_attr);
  1594. device_remove_file(&pd->dev, &kbdbl_ctl->timeout_attr);
  1595. /* restore the default hw behaviour */
  1596. sony_call_snc_handle(kbdbl_ctl->handle,
  1597. kbdbl_ctl->base | 0x10000, &result);
  1598. sony_call_snc_handle(kbdbl_ctl->handle,
  1599. kbdbl_ctl->base + 0x200, &result);
  1600. kfree(kbdbl_ctl);
  1601. kbdbl_ctl = NULL;
  1602. }
  1603. }
  1604. #ifdef CONFIG_PM_SLEEP
  1605. static void sony_nc_kbd_backlight_resume(void)
  1606. {
  1607. int ignore = 0;
  1608. if (!kbdbl_ctl)
  1609. return;
  1610. if (kbdbl_ctl->mode == 0)
  1611. sony_call_snc_handle(kbdbl_ctl->handle, kbdbl_ctl->base,
  1612. &ignore);
  1613. if (kbdbl_ctl->timeout != 0)
  1614. sony_call_snc_handle(kbdbl_ctl->handle,
  1615. (kbdbl_ctl->base + 0x200) |
  1616. (kbdbl_ctl->timeout << 0x10), &ignore);
  1617. }
  1618. #endif
  1619. struct battery_care_control {
  1620. struct device_attribute attrs[2];
  1621. unsigned int handle;
  1622. };
  1623. static struct battery_care_control *bcare_ctl;
  1624. static ssize_t sony_nc_battery_care_limit_store(struct device *dev,
  1625. struct device_attribute *attr,
  1626. const char *buffer, size_t count)
  1627. {
  1628. unsigned int result, cmd;
  1629. unsigned long value;
  1630. if (count > 31)
  1631. return -EINVAL;
  1632. if (kstrtoul(buffer, 10, &value))
  1633. return -EINVAL;
  1634. /* limit values (2 bits):
  1635. * 00 - none
  1636. * 01 - 80%
  1637. * 10 - 50%
  1638. * 11 - 100%
  1639. *
  1640. * bit 0: 0 disable BCL, 1 enable BCL
  1641. * bit 1: 1 tell to store the battery limit (see bits 6,7) too
  1642. * bits 2,3: reserved
  1643. * bits 4,5: store the limit into the EC
  1644. * bits 6,7: store the limit into the battery
  1645. */
  1646. cmd = 0;
  1647. if (value > 0) {
  1648. if (value <= 50)
  1649. cmd = 0x20;
  1650. else if (value <= 80)
  1651. cmd = 0x10;
  1652. else if (value <= 100)
  1653. cmd = 0x30;
  1654. else
  1655. return -EINVAL;
  1656. /*
  1657. * handle 0x0115 should allow storing on battery too;
  1658. * handle 0x0136 same as 0x0115 + health status;
  1659. * handle 0x013f, same as 0x0136 but no storing on the battery
  1660. */
  1661. if (bcare_ctl->handle != 0x013f)
  1662. cmd = cmd | (cmd << 2);
  1663. cmd = (cmd | 0x1) << 0x10;
  1664. }
  1665. if (sony_call_snc_handle(bcare_ctl->handle, cmd | 0x0100, &result))
  1666. return -EIO;
  1667. return count;
  1668. }
  1669. static ssize_t sony_nc_battery_care_limit_show(struct device *dev,
  1670. struct device_attribute *attr, char *buffer)
  1671. {
  1672. unsigned int result, status;
  1673. if (sony_call_snc_handle(bcare_ctl->handle, 0x0000, &result))
  1674. return -EIO;
  1675. status = (result & 0x01) ? ((result & 0x30) >> 0x04) : 0;
  1676. switch (status) {
  1677. case 1:
  1678. status = 80;
  1679. break;
  1680. case 2:
  1681. status = 50;
  1682. break;
  1683. case 3:
  1684. status = 100;
  1685. break;
  1686. default:
  1687. status = 0;
  1688. break;
  1689. }
  1690. return snprintf(buffer, PAGE_SIZE, "%d\n", status);
  1691. }
  1692. static ssize_t sony_nc_battery_care_health_show(struct device *dev,
  1693. struct device_attribute *attr, char *buffer)
  1694. {
  1695. ssize_t count = 0;
  1696. unsigned int health;
  1697. if (sony_call_snc_handle(bcare_ctl->handle, 0x0200, &health))
  1698. return -EIO;
  1699. count = snprintf(buffer, PAGE_SIZE, "%d\n", health & 0xff);
  1700. return count;
  1701. }
  1702. static int sony_nc_battery_care_setup(struct platform_device *pd,
  1703. unsigned int handle)
  1704. {
  1705. int ret = 0;
  1706. bcare_ctl = kzalloc(sizeof(struct battery_care_control), GFP_KERNEL);
  1707. if (!bcare_ctl)
  1708. return -ENOMEM;
  1709. bcare_ctl->handle = handle;
  1710. sysfs_attr_init(&bcare_ctl->attrs[0].attr);
  1711. bcare_ctl->attrs[0].attr.name = "battery_care_limiter";
  1712. bcare_ctl->attrs[0].attr.mode = S_IRUGO | S_IWUSR;
  1713. bcare_ctl->attrs[0].show = sony_nc_battery_care_limit_show;
  1714. bcare_ctl->attrs[0].store = sony_nc_battery_care_limit_store;
  1715. ret = device_create_file(&pd->dev, &bcare_ctl->attrs[0]);
  1716. if (ret)
  1717. goto outkzalloc;
  1718. /* 0x0115 is for models with no health reporting capability */
  1719. if (handle == 0x0115)
  1720. return 0;
  1721. sysfs_attr_init(&bcare_ctl->attrs[1].attr);
  1722. bcare_ctl->attrs[1].attr.name = "battery_care_health";
  1723. bcare_ctl->attrs[1].attr.mode = S_IRUGO;
  1724. bcare_ctl->attrs[1].show = sony_nc_battery_care_health_show;
  1725. ret = device_create_file(&pd->dev, &bcare_ctl->attrs[1]);
  1726. if (ret)
  1727. goto outlimiter;
  1728. return 0;
  1729. outlimiter:
  1730. device_remove_file(&pd->dev, &bcare_ctl->attrs[0]);
  1731. outkzalloc:
  1732. kfree(bcare_ctl);
  1733. bcare_ctl = NULL;
  1734. return ret;
  1735. }
  1736. static void sony_nc_battery_care_cleanup(struct platform_device *pd)
  1737. {
  1738. if (bcare_ctl) {
  1739. device_remove_file(&pd->dev, &bcare_ctl->attrs[0]);
  1740. if (bcare_ctl->handle != 0x0115)
  1741. device_remove_file(&pd->dev, &bcare_ctl->attrs[1]);
  1742. kfree(bcare_ctl);
  1743. bcare_ctl = NULL;
  1744. }
  1745. }
  1746. struct snc_thermal_ctrl {
  1747. unsigned int mode;
  1748. unsigned int profiles;
  1749. struct device_attribute mode_attr;
  1750. struct device_attribute profiles_attr;
  1751. };
  1752. static struct snc_thermal_ctrl *th_handle;
  1753. #define THM_PROFILE_MAX 3
  1754. static const char * const snc_thermal_profiles[] = {
  1755. "balanced",
  1756. "silent",
  1757. "performance"
  1758. };
  1759. static int sony_nc_thermal_mode_set(unsigned short mode)
  1760. {
  1761. unsigned int result;
  1762. /* the thermal profile seems to be a two bit bitmask:
  1763. * lsb -> silent
  1764. * msb -> performance
  1765. * no bit set is the normal operation and is always valid
  1766. * Some vaio models only have "balanced" and "performance"
  1767. */
  1768. if ((mode && !(th_handle->profiles & mode)) || mode >= THM_PROFILE_MAX)
  1769. return -EINVAL;
  1770. if (sony_call_snc_handle(0x0122, mode << 0x10 | 0x0200, &result))
  1771. return -EIO;
  1772. th_handle->mode = mode;
  1773. return 0;
  1774. }
  1775. static int sony_nc_thermal_mode_get(void)
  1776. {
  1777. unsigned int result;
  1778. if (sony_call_snc_handle(0x0122, 0x0100, &result))
  1779. return -EIO;
  1780. return result & 0xff;
  1781. }
  1782. static ssize_t sony_nc_thermal_profiles_show(struct device *dev,
  1783. struct device_attribute *attr, char *buffer)
  1784. {
  1785. short cnt;
  1786. size_t idx = 0;
  1787. for (cnt = 0; cnt < THM_PROFILE_MAX; cnt++) {
  1788. if (!cnt || (th_handle->profiles & cnt))
  1789. idx += snprintf(buffer + idx, PAGE_SIZE - idx, "%s ",
  1790. snc_thermal_profiles[cnt]);
  1791. }
  1792. idx += snprintf(buffer + idx, PAGE_SIZE - idx, "\n");
  1793. return idx;
  1794. }
  1795. static ssize_t sony_nc_thermal_mode_store(struct device *dev,
  1796. struct device_attribute *attr,
  1797. const char *buffer, size_t count)
  1798. {
  1799. unsigned short cmd;
  1800. size_t len = count;
  1801. if (count == 0)
  1802. return -EINVAL;
  1803. /* skip the newline if present */
  1804. if (buffer[len - 1] == '\n')
  1805. len--;
  1806. for (cmd = 0; cmd < THM_PROFILE_MAX; cmd++)
  1807. if (strncmp(buffer, snc_thermal_profiles[cmd], len) == 0)
  1808. break;
  1809. if (sony_nc_thermal_mode_set(cmd))
  1810. return -EIO;
  1811. return count;
  1812. }
  1813. static ssize_t sony_nc_thermal_mode_show(struct device *dev,
  1814. struct device_attribute *attr, char *buffer)
  1815. {
  1816. ssize_t count = 0;
  1817. int mode = sony_nc_thermal_mode_get();
  1818. if (mode < 0)
  1819. return mode;
  1820. count = snprintf(buffer, PAGE_SIZE, "%s\n", snc_thermal_profiles[mode]);
  1821. return count;
  1822. }
  1823. static int sony_nc_thermal_setup(struct platform_device *pd)
  1824. {
  1825. int ret = 0;
  1826. th_handle = kzalloc(sizeof(struct snc_thermal_ctrl), GFP_KERNEL);
  1827. if (!th_handle)
  1828. return -ENOMEM;
  1829. ret = sony_call_snc_handle(0x0122, 0x0000, &th_handle->profiles);
  1830. if (ret) {
  1831. pr_warn("couldn't to read the thermal profiles\n");
  1832. goto outkzalloc;
  1833. }
  1834. ret = sony_nc_thermal_mode_get();
  1835. if (ret < 0) {
  1836. pr_warn("couldn't to read the current thermal profile");
  1837. goto outkzalloc;
  1838. }
  1839. th_handle->mode = ret;
  1840. sysfs_attr_init(&th_handle->profiles_attr.attr);
  1841. th_handle->profiles_attr.attr.name = "thermal_profiles";
  1842. th_handle->profiles_attr.attr.mode = S_IRUGO;
  1843. th_handle->profiles_attr.show = sony_nc_thermal_profiles_show;
  1844. sysfs_attr_init(&th_handle->mode_attr.attr);
  1845. th_handle->mode_attr.attr.name = "thermal_control";
  1846. th_handle->mode_attr.attr.mode = S_IRUGO | S_IWUSR;
  1847. th_handle->mode_attr.show = sony_nc_thermal_mode_show;
  1848. th_handle->mode_attr.store = sony_nc_thermal_mode_store;
  1849. ret = device_create_file(&pd->dev, &th_handle->profiles_attr);
  1850. if (ret)
  1851. goto outkzalloc;
  1852. ret = device_create_file(&pd->dev, &th_handle->mode_attr);
  1853. if (ret)
  1854. goto outprofiles;
  1855. return 0;
  1856. outprofiles:
  1857. device_remove_file(&pd->dev, &th_handle->profiles_attr);
  1858. outkzalloc:
  1859. kfree(th_handle);
  1860. th_handle = NULL;
  1861. return ret;
  1862. }
  1863. static void sony_nc_thermal_cleanup(struct platform_device *pd)
  1864. {
  1865. if (th_handle) {
  1866. device_remove_file(&pd->dev, &th_handle->profiles_attr);
  1867. device_remove_file(&pd->dev, &th_handle->mode_attr);
  1868. kfree(th_handle);
  1869. th_handle = NULL;
  1870. }
  1871. }
  1872. #ifdef CONFIG_PM_SLEEP
  1873. static void sony_nc_thermal_resume(void)
  1874. {
  1875. unsigned int status = sony_nc_thermal_mode_get();
  1876. if (status != th_handle->mode)
  1877. sony_nc_thermal_mode_set(th_handle->mode);
  1878. }
  1879. #endif
  1880. /* resume on LID open */
  1881. struct snc_lid_resume_control {
  1882. struct device_attribute attrs[3];
  1883. unsigned int status;
  1884. };
  1885. static struct snc_lid_resume_control *lid_ctl;
  1886. static ssize_t sony_nc_lid_resume_store(struct device *dev,
  1887. struct device_attribute *attr,
  1888. const char *buffer, size_t count)
  1889. {
  1890. unsigned int result, pos;
  1891. unsigned long value;
  1892. if (count > 31)
  1893. return -EINVAL;
  1894. if (kstrtoul(buffer, 10, &value) || value > 1)
  1895. return -EINVAL;
  1896. /* the value we have to write to SNC is a bitmask:
  1897. * +--------------+
  1898. * | S3 | S4 | S5 |
  1899. * +--------------+
  1900. * 2 1 0
  1901. */
  1902. if (strcmp(attr->attr.name, "lid_resume_S3") == 0)
  1903. pos = 2;
  1904. else if (strcmp(attr->attr.name, "lid_resume_S4") == 0)
  1905. pos = 1;
  1906. else if (strcmp(attr->attr.name, "lid_resume_S5") == 0)
  1907. pos = 0;
  1908. else
  1909. return -EINVAL;
  1910. if (value)
  1911. value = lid_ctl->status | (1 << pos);
  1912. else
  1913. value = lid_ctl->status & ~(1 << pos);
  1914. if (sony_call_snc_handle(0x0119, value << 0x10 | 0x0100, &result))
  1915. return -EIO;
  1916. lid_ctl->status = value;
  1917. return count;
  1918. }
  1919. static ssize_t sony_nc_lid_resume_show(struct device *dev,
  1920. struct device_attribute *attr, char *buffer)
  1921. {
  1922. unsigned int pos;
  1923. if (strcmp(attr->attr.name, "lid_resume_S3") == 0)
  1924. pos = 2;
  1925. else if (strcmp(attr->attr.name, "lid_resume_S4") == 0)
  1926. pos = 1;
  1927. else if (strcmp(attr->attr.name, "lid_resume_S5") == 0)
  1928. pos = 0;
  1929. else
  1930. return -EINVAL;
  1931. return snprintf(buffer, PAGE_SIZE, "%d\n",
  1932. (lid_ctl->status >> pos) & 0x01);
  1933. }
  1934. static int sony_nc_lid_resume_setup(struct platform_device *pd)
  1935. {
  1936. unsigned int result;
  1937. int i;
  1938. if (sony_call_snc_handle(0x0119, 0x0000, &result))
  1939. return -EIO;
  1940. lid_ctl = kzalloc(sizeof(struct snc_lid_resume_control), GFP_KERNEL);
  1941. if (!lid_ctl)
  1942. return -ENOMEM;
  1943. lid_ctl->status = result & 0x7;
  1944. sysfs_attr_init(&lid_ctl->attrs[0].attr);
  1945. lid_ctl->attrs[0].attr.name = "lid_resume_S3";
  1946. lid_ctl->attrs[0].attr.mode = S_IRUGO | S_IWUSR;
  1947. lid_ctl->attrs[0].show = sony_nc_lid_resume_show;
  1948. lid_ctl->attrs[0].store = sony_nc_lid_resume_store;
  1949. sysfs_attr_init(&lid_ctl->attrs[1].attr);
  1950. lid_ctl->attrs[1].attr.name = "lid_resume_S4";
  1951. lid_ctl->attrs[1].attr.mode = S_IRUGO | S_IWUSR;
  1952. lid_ctl->attrs[1].show = sony_nc_lid_resume_show;
  1953. lid_ctl->attrs[1].store = sony_nc_lid_resume_store;
  1954. sysfs_attr_init(&lid_ctl->attrs[2].attr);
  1955. lid_ctl->attrs[2].attr.name = "lid_resume_S5";
  1956. lid_ctl->attrs[2].attr.mode = S_IRUGO | S_IWUSR;
  1957. lid_ctl->attrs[2].show = sony_nc_lid_resume_show;
  1958. lid_ctl->attrs[2].store = sony_nc_lid_resume_store;
  1959. for (i = 0; i < 3; i++) {
  1960. result = device_create_file(&pd->dev, &lid_ctl->attrs[i]);
  1961. if (result)
  1962. goto liderror;
  1963. }
  1964. return 0;
  1965. liderror:
  1966. for (i--; i >= 0; i--)
  1967. device_remove_file(&pd->dev, &lid_ctl->attrs[i]);
  1968. kfree(lid_ctl);
  1969. lid_ctl = NULL;
  1970. return result;
  1971. }
  1972. static void sony_nc_lid_resume_cleanup(struct platform_device *pd)
  1973. {
  1974. int i;
  1975. if (lid_ctl) {
  1976. for (i = 0; i < 3; i++)
  1977. device_remove_file(&pd->dev, &lid_ctl->attrs[i]);
  1978. kfree(lid_ctl);
  1979. lid_ctl = NULL;
  1980. }
  1981. }
  1982. /* GFX Switch position */
  1983. enum gfx_switch {
  1984. SPEED,
  1985. STAMINA,
  1986. AUTO
  1987. };
  1988. struct snc_gfx_switch_control {
  1989. struct device_attribute attr;
  1990. unsigned int handle;
  1991. };
  1992. static struct snc_gfx_switch_control *gfxs_ctl;
  1993. /* returns 0 for speed, 1 for stamina */
  1994. static int __sony_nc_gfx_switch_status_get(void)
  1995. {
  1996. unsigned int result;
  1997. if (sony_call_snc_handle(gfxs_ctl->handle, 0x0100, &result))
  1998. return -EIO;
  1999. switch (gfxs_ctl->handle) {
  2000. case 0x0146:
  2001. /* 1: discrete GFX (speed)
  2002. * 0: integrated GFX (stamina)
  2003. */
  2004. return result & 0x1 ? SPEED : STAMINA;
  2005. break;
  2006. case 0x0128:
  2007. /* it's a more elaborated bitmask, for now:
  2008. * 2: integrated GFX (stamina)
  2009. * 0: discrete GFX (speed)
  2010. */
  2011. dprintk("GFX Status: 0x%x\n", result);
  2012. return result & 0x80 ? AUTO :
  2013. result & 0x02 ? STAMINA : SPEED;
  2014. break;
  2015. }
  2016. return -EINVAL;
  2017. }
  2018. static ssize_t sony_nc_gfx_switch_status_show(struct device *dev,
  2019. struct device_attribute *attr,
  2020. char *buffer)
  2021. {
  2022. int pos = __sony_nc_gfx_switch_status_get();
  2023. if (pos < 0)
  2024. return pos;
  2025. return snprintf(buffer, PAGE_SIZE, "%s\n", pos ? "speed" : "stamina");
  2026. }
  2027. static int sony_nc_gfx_switch_setup(struct platform_device *pd,
  2028. unsigned int handle)
  2029. {
  2030. unsigned int result;
  2031. gfxs_ctl = kzalloc(sizeof(struct snc_gfx_switch_control), GFP_KERNEL);
  2032. if (!gfxs_ctl)
  2033. return -ENOMEM;
  2034. gfxs_ctl->handle = handle;
  2035. sysfs_attr_init(&gfxs_ctl->attr.attr);
  2036. gfxs_ctl->attr.attr.name = "gfx_switch_status";
  2037. gfxs_ctl->attr.attr.mode = S_IRUGO;
  2038. gfxs_ctl->attr.show = sony_nc_gfx_switch_status_show;
  2039. result = device_create_file(&pd->dev, &gfxs_ctl->attr);
  2040. if (result)
  2041. goto gfxerror;
  2042. return 0;
  2043. gfxerror:
  2044. kfree(gfxs_ctl);
  2045. gfxs_ctl = NULL;
  2046. return result;
  2047. }
  2048. static void sony_nc_gfx_switch_cleanup(struct platform_device *pd)
  2049. {
  2050. if (gfxs_ctl) {
  2051. device_remove_file(&pd->dev, &gfxs_ctl->attr);
  2052. kfree(gfxs_ctl);
  2053. gfxs_ctl = NULL;
  2054. }
  2055. }
  2056. /* High speed charging function */
  2057. static struct device_attribute *hsc_handle;
  2058. static ssize_t sony_nc_highspeed_charging_store(struct device *dev,
  2059. struct device_attribute *attr,
  2060. const char *buffer, size_t count)
  2061. {
  2062. unsigned int result;
  2063. unsigned long value;
  2064. if (count > 31)
  2065. return -EINVAL;
  2066. if (kstrtoul(buffer, 10, &value) || value > 1)
  2067. return -EINVAL;
  2068. if (sony_call_snc_handle(0x0131, value << 0x10 | 0x0200, &result))
  2069. return -EIO;
  2070. return count;
  2071. }
  2072. static ssize_t sony_nc_highspeed_charging_show(struct device *dev,
  2073. struct device_attribute *attr, char *buffer)
  2074. {
  2075. unsigned int result;
  2076. if (sony_call_snc_handle(0x0131, 0x0100, &result))
  2077. return -EIO;
  2078. return snprintf(buffer, PAGE_SIZE, "%d\n", result & 0x01);
  2079. }
  2080. static int sony_nc_highspeed_charging_setup(struct platform_device *pd)
  2081. {
  2082. unsigned int result;
  2083. if (sony_call_snc_handle(0x0131, 0x0000, &result) || !(result & 0x01)) {
  2084. /* some models advertise the handle but have no implementation
  2085. * for it
  2086. */
  2087. pr_info("No High Speed Charging capability found\n");
  2088. return 0;
  2089. }
  2090. hsc_handle = kzalloc(sizeof(struct device_attribute), GFP_KERNEL);
  2091. if (!hsc_handle)
  2092. return -ENOMEM;
  2093. sysfs_attr_init(&hsc_handle->attr);
  2094. hsc_handle->attr.name = "battery_highspeed_charging";
  2095. hsc_handle->attr.mode = S_IRUGO | S_IWUSR;
  2096. hsc_handle->show = sony_nc_highspeed_charging_show;
  2097. hsc_handle->store = sony_nc_highspeed_charging_store;
  2098. result = device_create_file(&pd->dev, hsc_handle);
  2099. if (result) {
  2100. kfree(hsc_handle);
  2101. hsc_handle = NULL;
  2102. return result;
  2103. }
  2104. return 0;
  2105. }
  2106. static void sony_nc_highspeed_charging_cleanup(struct platform_device *pd)
  2107. {
  2108. if (hsc_handle) {
  2109. device_remove_file(&pd->dev, hsc_handle);
  2110. kfree(hsc_handle);
  2111. hsc_handle = NULL;
  2112. }
  2113. }
  2114. /* Touchpad enable/disable */
  2115. struct touchpad_control {
  2116. struct device_attribute attr;
  2117. int handle;
  2118. };
  2119. static struct touchpad_control *tp_ctl;
  2120. static ssize_t sony_nc_touchpad_store(struct device *dev,
  2121. struct device_attribute *attr, const char *buffer, size_t count)
  2122. {
  2123. unsigned int result;
  2124. unsigned long value;
  2125. if (count > 31)
  2126. return -EINVAL;
  2127. if (kstrtoul(buffer, 10, &value) || value > 1)
  2128. return -EINVAL;
  2129. /* sysfs: 0 disabled, 1 enabled
  2130. * EC: 0 enabled, 1 disabled
  2131. */
  2132. if (sony_call_snc_handle(tp_ctl->handle,
  2133. (!value << 0x10) | 0x100, &result))
  2134. return -EIO;
  2135. return count;
  2136. }
  2137. static ssize_t sony_nc_touchpad_show(struct device *dev,
  2138. struct device_attribute *attr, char *buffer)
  2139. {
  2140. unsigned int result;
  2141. if (sony_call_snc_handle(tp_ctl->handle, 0x000, &result))
  2142. return -EINVAL;
  2143. return snprintf(buffer, PAGE_SIZE, "%d\n", !(result & 0x01));
  2144. }
  2145. static int sony_nc_touchpad_setup(struct platform_device *pd,
  2146. unsigned int handle)
  2147. {
  2148. int ret = 0;
  2149. tp_ctl = kzalloc(sizeof(struct touchpad_control), GFP_KERNEL);
  2150. if (!tp_ctl)
  2151. return -ENOMEM;
  2152. tp_ctl->handle = handle;
  2153. sysfs_attr_init(&tp_ctl->attr.attr);
  2154. tp_ctl->attr.attr.name = "touchpad";
  2155. tp_ctl->attr.attr.mode = S_IRUGO | S_IWUSR;
  2156. tp_ctl->attr.show = sony_nc_touchpad_show;
  2157. tp_ctl->attr.store = sony_nc_touchpad_store;
  2158. ret = device_create_file(&pd->dev, &tp_ctl->attr);
  2159. if (ret) {
  2160. kfree(tp_ctl);
  2161. tp_ctl = NULL;
  2162. }
  2163. return ret;
  2164. }
  2165. static void sony_nc_touchpad_cleanup(struct platform_device *pd)
  2166. {
  2167. if (tp_ctl) {
  2168. device_remove_file(&pd->dev, &tp_ctl->attr);
  2169. kfree(tp_ctl);
  2170. tp_ctl = NULL;
  2171. }
  2172. }
  2173. static void sony_nc_backlight_ng_read_limits(int handle,
  2174. struct sony_backlight_props *props)
  2175. {
  2176. u64 offset;
  2177. int i;
  2178. int lvl_table_len = 0;
  2179. u8 min = 0xff, max = 0x00;
  2180. unsigned char buffer[32] = { 0 };
  2181. props->handle = handle;
  2182. props->offset = 0;
  2183. props->maxlvl = 0xff;
  2184. offset = sony_find_snc_handle(handle);
  2185. /* try to read the boundaries from ACPI tables, if we fail the above
  2186. * defaults should be reasonable
  2187. */
  2188. i = sony_nc_buffer_call(sony_nc_acpi_handle, "SN06", &offset, buffer,
  2189. 32);
  2190. if (i < 0)
  2191. return;
  2192. switch (handle) {
  2193. case 0x012f:
  2194. case 0x0137:
  2195. lvl_table_len = 9;
  2196. break;
  2197. case 0x143:
  2198. case 0x14b:
  2199. case 0x14c:
  2200. lvl_table_len = 16;
  2201. break;
  2202. }
  2203. /* the buffer lists brightness levels available, brightness levels are
  2204. * from position 0 to 8 in the array, other values are used by ALS
  2205. * control.
  2206. */
  2207. for (i = 0; i < lvl_table_len && i < ARRAY_SIZE(buffer); i++) {
  2208. dprintk("Brightness level: %d\n", buffer[i]);
  2209. if (!buffer[i])
  2210. break;
  2211. if (buffer[i] > max)
  2212. max = buffer[i];
  2213. if (buffer[i] < min)
  2214. min = buffer[i];
  2215. }
  2216. props->offset = min;
  2217. props->maxlvl = max;
  2218. dprintk("Brightness levels: min=%d max=%d\n", props->offset,
  2219. props->maxlvl);
  2220. }
  2221. static void sony_nc_backlight_setup(void)
  2222. {
  2223. acpi_handle unused;
  2224. int max_brightness = 0;
  2225. const struct backlight_ops *ops = NULL;
  2226. struct backlight_properties props;
  2227. if (sony_find_snc_handle(0x12f) >= 0) {
  2228. ops = &sony_backlight_ng_ops;
  2229. sony_bl_props.cmd_base = 0x0100;
  2230. sony_nc_backlight_ng_read_limits(0x12f, &sony_bl_props);
  2231. max_brightness = sony_bl_props.maxlvl - sony_bl_props.offset;
  2232. } else if (sony_find_snc_handle(0x137) >= 0) {
  2233. ops = &sony_backlight_ng_ops;
  2234. sony_bl_props.cmd_base = 0x0100;
  2235. sony_nc_backlight_ng_read_limits(0x137, &sony_bl_props);
  2236. max_brightness = sony_bl_props.maxlvl - sony_bl_props.offset;
  2237. } else if (sony_find_snc_handle(0x143) >= 0) {
  2238. ops = &sony_backlight_ng_ops;
  2239. sony_bl_props.cmd_base = 0x3000;
  2240. sony_nc_backlight_ng_read_limits(0x143, &sony_bl_props);
  2241. max_brightness = sony_bl_props.maxlvl - sony_bl_props.offset;
  2242. } else if (sony_find_snc_handle(0x14b) >= 0) {
  2243. ops = &sony_backlight_ng_ops;
  2244. sony_bl_props.cmd_base = 0x3000;
  2245. sony_nc_backlight_ng_read_limits(0x14b, &sony_bl_props);
  2246. max_brightness = sony_bl_props.maxlvl - sony_bl_props.offset;
  2247. } else if (sony_find_snc_handle(0x14c) >= 0) {
  2248. ops = &sony_backlight_ng_ops;
  2249. sony_bl_props.cmd_base = 0x3000;
  2250. sony_nc_backlight_ng_read_limits(0x14c, &sony_bl_props);
  2251. max_brightness = sony_bl_props.maxlvl - sony_bl_props.offset;
  2252. } else if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle, "GBRT",
  2253. &unused))) {
  2254. ops = &sony_backlight_ops;
  2255. max_brightness = SONY_MAX_BRIGHTNESS - 1;
  2256. } else
  2257. return;
  2258. memset(&props, 0, sizeof(struct backlight_properties));
  2259. props.type = BACKLIGHT_PLATFORM;
  2260. props.max_brightness = max_brightness;
  2261. sony_bl_props.dev = backlight_device_register("sony", NULL,
  2262. &sony_bl_props,
  2263. ops, &props);
  2264. if (IS_ERR(sony_bl_props.dev)) {
  2265. pr_warn("unable to register backlight device\n");
  2266. sony_bl_props.dev = NULL;
  2267. } else
  2268. sony_bl_props.dev->props.brightness =
  2269. ops->get_brightness(sony_bl_props.dev);
  2270. }
  2271. static void sony_nc_backlight_cleanup(void)
  2272. {
  2273. if (sony_bl_props.dev)
  2274. backlight_device_unregister(sony_bl_props.dev);
  2275. }
  2276. static int sony_nc_add(struct acpi_device *device)
  2277. {
  2278. acpi_status status;
  2279. int result = 0;
  2280. acpi_handle handle;
  2281. struct sony_nc_value *item;
  2282. pr_info("%s v%s\n", SONY_NC_DRIVER_NAME, SONY_LAPTOP_DRIVER_VERSION);
  2283. sony_nc_acpi_device = device;
  2284. strcpy(acpi_device_class(device), "sony/hotkey");
  2285. sony_nc_acpi_handle = device->handle;
  2286. /* read device status */
  2287. result = acpi_bus_get_status(device);
  2288. /* bail IFF the above call was successful and the device is not present */
  2289. if (!result && !device->status.present) {
  2290. dprintk("Device not present\n");
  2291. result = -ENODEV;
  2292. goto outwalk;
  2293. }
  2294. result = sony_pf_add();
  2295. if (result)
  2296. goto outpresent;
  2297. if (debug) {
  2298. status = acpi_walk_namespace(ACPI_TYPE_METHOD,
  2299. sony_nc_acpi_handle, 1, sony_walk_callback,
  2300. NULL, NULL, NULL);
  2301. if (ACPI_FAILURE(status)) {
  2302. pr_warn("unable to walk acpi resources\n");
  2303. result = -ENODEV;
  2304. goto outpresent;
  2305. }
  2306. }
  2307. result = sony_laptop_setup_input(device);
  2308. if (result) {
  2309. pr_err("Unable to create input devices\n");
  2310. goto outplatform;
  2311. }
  2312. if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle, "ECON",
  2313. &handle))) {
  2314. int arg = 1;
  2315. if (sony_nc_int_call(sony_nc_acpi_handle, "ECON", &arg, NULL))
  2316. dprintk("ECON Method failed\n");
  2317. }
  2318. if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle, "SN00",
  2319. &handle))) {
  2320. dprintk("Doing SNC setup\n");
  2321. /* retrieve the available handles */
  2322. result = sony_nc_handles_setup(sony_pf_device);
  2323. if (!result)
  2324. sony_nc_function_setup(device, sony_pf_device);
  2325. }
  2326. /* setup input devices and helper fifo */
  2327. if (acpi_video_backlight_support()) {
  2328. pr_info("brightness ignored, must be controlled by ACPI video driver\n");
  2329. } else {
  2330. sony_nc_backlight_setup();
  2331. }
  2332. /* create sony_pf sysfs attributes related to the SNC device */
  2333. for (item = sony_nc_values; item->name; ++item) {
  2334. if (!debug && item->debug)
  2335. continue;
  2336. /* find the available acpiget as described in the DSDT */
  2337. for (; item->acpiget && *item->acpiget; ++item->acpiget) {
  2338. if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle,
  2339. *item->acpiget,
  2340. &handle))) {
  2341. dprintk("Found %s getter: %s\n",
  2342. item->name, *item->acpiget);
  2343. item->devattr.attr.mode |= S_IRUGO;
  2344. break;
  2345. }
  2346. }
  2347. /* find the available acpiset as described in the DSDT */
  2348. for (; item->acpiset && *item->acpiset; ++item->acpiset) {
  2349. if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle,
  2350. *item->acpiset,
  2351. &handle))) {
  2352. dprintk("Found %s setter: %s\n",
  2353. item->name, *item->acpiset);
  2354. item->devattr.attr.mode |= S_IWUSR;
  2355. break;
  2356. }
  2357. }
  2358. if (item->devattr.attr.mode != 0) {
  2359. result =
  2360. device_create_file(&sony_pf_device->dev,
  2361. &item->devattr);
  2362. if (result)
  2363. goto out_sysfs;
  2364. }
  2365. }
  2366. return 0;
  2367. out_sysfs:
  2368. for (item = sony_nc_values; item->name; ++item) {
  2369. device_remove_file(&sony_pf_device->dev, &item->devattr);
  2370. }
  2371. sony_nc_backlight_cleanup();
  2372. sony_nc_function_cleanup(sony_pf_device);
  2373. sony_nc_handles_cleanup(sony_pf_device);
  2374. outplatform:
  2375. sony_laptop_remove_input();
  2376. outpresent:
  2377. sony_pf_remove();
  2378. outwalk:
  2379. sony_nc_rfkill_cleanup();
  2380. return result;
  2381. }
  2382. static int sony_nc_remove(struct acpi_device *device)
  2383. {
  2384. struct sony_nc_value *item;
  2385. sony_nc_backlight_cleanup();
  2386. sony_nc_acpi_device = NULL;
  2387. for (item = sony_nc_values; item->name; ++item) {
  2388. device_remove_file(&sony_pf_device->dev, &item->devattr);
  2389. }
  2390. sony_nc_function_cleanup(sony_pf_device);
  2391. sony_nc_handles_cleanup(sony_pf_device);
  2392. sony_pf_remove();
  2393. sony_laptop_remove_input();
  2394. dprintk(SONY_NC_DRIVER_NAME " removed.\n");
  2395. return 0;
  2396. }
  2397. static const struct acpi_device_id sony_device_ids[] = {
  2398. {SONY_NC_HID, 0},
  2399. {SONY_PIC_HID, 0},
  2400. {"", 0},
  2401. };
  2402. MODULE_DEVICE_TABLE(acpi, sony_device_ids);
  2403. static const struct acpi_device_id sony_nc_device_ids[] = {
  2404. {SONY_NC_HID, 0},
  2405. {"", 0},
  2406. };
  2407. static struct acpi_driver sony_nc_driver = {
  2408. .name = SONY_NC_DRIVER_NAME,
  2409. .class = SONY_NC_CLASS,
  2410. .ids = sony_nc_device_ids,
  2411. .owner = THIS_MODULE,
  2412. .ops = {
  2413. .add = sony_nc_add,
  2414. .remove = sony_nc_remove,
  2415. .notify = sony_nc_notify,
  2416. },
  2417. .drv.pm = &sony_nc_pm,
  2418. };
  2419. /*********** SPIC (SNY6001) Device ***********/
  2420. #define SONYPI_DEVICE_TYPE1 0x00000001
  2421. #define SONYPI_DEVICE_TYPE2 0x00000002
  2422. #define SONYPI_DEVICE_TYPE3 0x00000004
  2423. #define SONYPI_TYPE1_OFFSET 0x04
  2424. #define SONYPI_TYPE2_OFFSET 0x12
  2425. #define SONYPI_TYPE3_OFFSET 0x12
  2426. struct sony_pic_ioport {
  2427. struct acpi_resource_io io1;
  2428. struct acpi_resource_io io2;
  2429. struct list_head list;
  2430. };
  2431. struct sony_pic_irq {
  2432. struct acpi_resource_irq irq;
  2433. struct list_head list;
  2434. };
  2435. struct sonypi_eventtypes {
  2436. u8 data;
  2437. unsigned long mask;
  2438. struct sonypi_event *events;
  2439. };
  2440. struct sony_pic_dev {
  2441. struct acpi_device *acpi_dev;
  2442. struct sony_pic_irq *cur_irq;
  2443. struct sony_pic_ioport *cur_ioport;
  2444. struct list_head interrupts;
  2445. struct list_head ioports;
  2446. struct mutex lock;
  2447. struct sonypi_eventtypes *event_types;
  2448. int (*handle_irq)(const u8, const u8);
  2449. int model;
  2450. u16 evport_offset;
  2451. u8 camera_power;
  2452. u8 bluetooth_power;
  2453. u8 wwan_power;
  2454. };
  2455. static struct sony_pic_dev spic_dev = {
  2456. .interrupts = LIST_HEAD_INIT(spic_dev.interrupts),
  2457. .ioports = LIST_HEAD_INIT(spic_dev.ioports),
  2458. };
  2459. static int spic_drv_registered;
  2460. /* Event masks */
  2461. #define SONYPI_JOGGER_MASK 0x00000001
  2462. #define SONYPI_CAPTURE_MASK 0x00000002
  2463. #define SONYPI_FNKEY_MASK 0x00000004
  2464. #define SONYPI_BLUETOOTH_MASK 0x00000008
  2465. #define SONYPI_PKEY_MASK 0x00000010
  2466. #define SONYPI_BACK_MASK 0x00000020
  2467. #define SONYPI_HELP_MASK 0x00000040
  2468. #define SONYPI_LID_MASK 0x00000080
  2469. #define SONYPI_ZOOM_MASK 0x00000100
  2470. #define SONYPI_THUMBPHRASE_MASK 0x00000200
  2471. #define SONYPI_MEYE_MASK 0x00000400
  2472. #define SONYPI_MEMORYSTICK_MASK 0x00000800
  2473. #define SONYPI_BATTERY_MASK 0x00001000
  2474. #define SONYPI_WIRELESS_MASK 0x00002000
  2475. struct sonypi_event {
  2476. u8 data;
  2477. u8 event;
  2478. };
  2479. /* The set of possible button release events */
  2480. static struct sonypi_event sonypi_releaseev[] = {
  2481. { 0x00, SONYPI_EVENT_ANYBUTTON_RELEASED },
  2482. { 0, 0 }
  2483. };
  2484. /* The set of possible jogger events */
  2485. static struct sonypi_event sonypi_joggerev[] = {
  2486. { 0x1f, SONYPI_EVENT_JOGDIAL_UP },
  2487. { 0x01, SONYPI_EVENT_JOGDIAL_DOWN },
  2488. { 0x5f, SONYPI_EVENT_JOGDIAL_UP_PRESSED },
  2489. { 0x41, SONYPI_EVENT_JOGDIAL_DOWN_PRESSED },
  2490. { 0x1e, SONYPI_EVENT_JOGDIAL_FAST_UP },
  2491. { 0x02, SONYPI_EVENT_JOGDIAL_FAST_DOWN },
  2492. { 0x5e, SONYPI_EVENT_JOGDIAL_FAST_UP_PRESSED },
  2493. { 0x42, SONYPI_EVENT_JOGDIAL_FAST_DOWN_PRESSED },
  2494. { 0x1d, SONYPI_EVENT_JOGDIAL_VFAST_UP },
  2495. { 0x03, SONYPI_EVENT_JOGDIAL_VFAST_DOWN },
  2496. { 0x5d, SONYPI_EVENT_JOGDIAL_VFAST_UP_PRESSED },
  2497. { 0x43, SONYPI_EVENT_JOGDIAL_VFAST_DOWN_PRESSED },
  2498. { 0x40, SONYPI_EVENT_JOGDIAL_PRESSED },
  2499. { 0, 0 }
  2500. };
  2501. /* The set of possible capture button events */
  2502. static struct sonypi_event sonypi_captureev[] = {
  2503. { 0x05, SONYPI_EVENT_CAPTURE_PARTIALPRESSED },
  2504. { 0x07, SONYPI_EVENT_CAPTURE_PRESSED },
  2505. { 0x40, SONYPI_EVENT_CAPTURE_PRESSED },
  2506. { 0x01, SONYPI_EVENT_CAPTURE_PARTIALRELEASED },
  2507. { 0, 0 }
  2508. };
  2509. /* The set of possible fnkeys events */
  2510. static struct sonypi_event sonypi_fnkeyev[] = {
  2511. { 0x10, SONYPI_EVENT_FNKEY_ESC },
  2512. { 0x11, SONYPI_EVENT_FNKEY_F1 },
  2513. { 0x12, SONYPI_EVENT_FNKEY_F2 },
  2514. { 0x13, SONYPI_EVENT_FNKEY_F3 },
  2515. { 0x14, SONYPI_EVENT_FNKEY_F4 },
  2516. { 0x15, SONYPI_EVENT_FNKEY_F5 },
  2517. { 0x16, SONYPI_EVENT_FNKEY_F6 },
  2518. { 0x17, SONYPI_EVENT_FNKEY_F7 },
  2519. { 0x18, SONYPI_EVENT_FNKEY_F8 },
  2520. { 0x19, SONYPI_EVENT_FNKEY_F9 },
  2521. { 0x1a, SONYPI_EVENT_FNKEY_F10 },
  2522. { 0x1b, SONYPI_EVENT_FNKEY_F11 },
  2523. { 0x1c, SONYPI_EVENT_FNKEY_F12 },
  2524. { 0x1f, SONYPI_EVENT_FNKEY_RELEASED },
  2525. { 0x21, SONYPI_EVENT_FNKEY_1 },
  2526. { 0x22, SONYPI_EVENT_FNKEY_2 },
  2527. { 0x31, SONYPI_EVENT_FNKEY_D },
  2528. { 0x32, SONYPI_EVENT_FNKEY_E },
  2529. { 0x33, SONYPI_EVENT_FNKEY_F },
  2530. { 0x34, SONYPI_EVENT_FNKEY_S },
  2531. { 0x35, SONYPI_EVENT_FNKEY_B },
  2532. { 0x36, SONYPI_EVENT_FNKEY_ONLY },
  2533. { 0, 0 }
  2534. };
  2535. /* The set of possible program key events */
  2536. static struct sonypi_event sonypi_pkeyev[] = {
  2537. { 0x01, SONYPI_EVENT_PKEY_P1 },
  2538. { 0x02, SONYPI_EVENT_PKEY_P2 },
  2539. { 0x04, SONYPI_EVENT_PKEY_P3 },
  2540. { 0x20, SONYPI_EVENT_PKEY_P1 },
  2541. { 0, 0 }
  2542. };
  2543. /* The set of possible bluetooth events */
  2544. static struct sonypi_event sonypi_blueev[] = {
  2545. { 0x55, SONYPI_EVENT_BLUETOOTH_PRESSED },
  2546. { 0x59, SONYPI_EVENT_BLUETOOTH_ON },
  2547. { 0x5a, SONYPI_EVENT_BLUETOOTH_OFF },
  2548. { 0, 0 }
  2549. };
  2550. /* The set of possible wireless events */
  2551. static struct sonypi_event sonypi_wlessev[] = {
  2552. { 0x59, SONYPI_EVENT_IGNORE },
  2553. { 0x5a, SONYPI_EVENT_IGNORE },
  2554. { 0, 0 }
  2555. };
  2556. /* The set of possible back button events */
  2557. static struct sonypi_event sonypi_backev[] = {
  2558. { 0x20, SONYPI_EVENT_BACK_PRESSED },
  2559. { 0, 0 }
  2560. };
  2561. /* The set of possible help button events */
  2562. static struct sonypi_event sonypi_helpev[] = {
  2563. { 0x3b, SONYPI_EVENT_HELP_PRESSED },
  2564. { 0, 0 }
  2565. };
  2566. /* The set of possible lid events */
  2567. static struct sonypi_event sonypi_lidev[] = {
  2568. { 0x51, SONYPI_EVENT_LID_CLOSED },
  2569. { 0x50, SONYPI_EVENT_LID_OPENED },
  2570. { 0, 0 }
  2571. };
  2572. /* The set of possible zoom events */
  2573. static struct sonypi_event sonypi_zoomev[] = {
  2574. { 0x39, SONYPI_EVENT_ZOOM_PRESSED },
  2575. { 0x10, SONYPI_EVENT_ZOOM_IN_PRESSED },
  2576. { 0x20, SONYPI_EVENT_ZOOM_OUT_PRESSED },
  2577. { 0x04, SONYPI_EVENT_ZOOM_PRESSED },
  2578. { 0, 0 }
  2579. };
  2580. /* The set of possible thumbphrase events */
  2581. static struct sonypi_event sonypi_thumbphraseev[] = {
  2582. { 0x3a, SONYPI_EVENT_THUMBPHRASE_PRESSED },
  2583. { 0, 0 }
  2584. };
  2585. /* The set of possible motioneye camera events */
  2586. static struct sonypi_event sonypi_meyeev[] = {
  2587. { 0x00, SONYPI_EVENT_MEYE_FACE },
  2588. { 0x01, SONYPI_EVENT_MEYE_OPPOSITE },
  2589. { 0, 0 }
  2590. };
  2591. /* The set of possible memorystick events */
  2592. static struct sonypi_event sonypi_memorystickev[] = {
  2593. { 0x53, SONYPI_EVENT_MEMORYSTICK_INSERT },
  2594. { 0x54, SONYPI_EVENT_MEMORYSTICK_EJECT },
  2595. { 0, 0 }
  2596. };
  2597. /* The set of possible battery events */
  2598. static struct sonypi_event sonypi_batteryev[] = {
  2599. { 0x20, SONYPI_EVENT_BATTERY_INSERT },
  2600. { 0x30, SONYPI_EVENT_BATTERY_REMOVE },
  2601. { 0, 0 }
  2602. };
  2603. /* The set of possible volume events */
  2604. static struct sonypi_event sonypi_volumeev[] = {
  2605. { 0x01, SONYPI_EVENT_VOLUME_INC_PRESSED },
  2606. { 0x02, SONYPI_EVENT_VOLUME_DEC_PRESSED },
  2607. { 0, 0 }
  2608. };
  2609. /* The set of possible brightness events */
  2610. static struct sonypi_event sonypi_brightnessev[] = {
  2611. { 0x80, SONYPI_EVENT_BRIGHTNESS_PRESSED },
  2612. { 0, 0 }
  2613. };
  2614. static struct sonypi_eventtypes type1_events[] = {
  2615. { 0, 0xffffffff, sonypi_releaseev },
  2616. { 0x70, SONYPI_MEYE_MASK, sonypi_meyeev },
  2617. { 0x30, SONYPI_LID_MASK, sonypi_lidev },
  2618. { 0x60, SONYPI_CAPTURE_MASK, sonypi_captureev },
  2619. { 0x10, SONYPI_JOGGER_MASK, sonypi_joggerev },
  2620. { 0x20, SONYPI_FNKEY_MASK, sonypi_fnkeyev },
  2621. { 0x30, SONYPI_BLUETOOTH_MASK, sonypi_blueev },
  2622. { 0x40, SONYPI_PKEY_MASK, sonypi_pkeyev },
  2623. { 0x30, SONYPI_MEMORYSTICK_MASK, sonypi_memorystickev },
  2624. { 0x40, SONYPI_BATTERY_MASK, sonypi_batteryev },
  2625. { 0 },
  2626. };
  2627. static struct sonypi_eventtypes type2_events[] = {
  2628. { 0, 0xffffffff, sonypi_releaseev },
  2629. { 0x38, SONYPI_LID_MASK, sonypi_lidev },
  2630. { 0x11, SONYPI_JOGGER_MASK, sonypi_joggerev },
  2631. { 0x61, SONYPI_CAPTURE_MASK, sonypi_captureev },
  2632. { 0x21, SONYPI_FNKEY_MASK, sonypi_fnkeyev },
  2633. { 0x31, SONYPI_BLUETOOTH_MASK, sonypi_blueev },
  2634. { 0x08, SONYPI_PKEY_MASK, sonypi_pkeyev },
  2635. { 0x11, SONYPI_BACK_MASK, sonypi_backev },
  2636. { 0x21, SONYPI_HELP_MASK, sonypi_helpev },
  2637. { 0x21, SONYPI_ZOOM_MASK, sonypi_zoomev },
  2638. { 0x20, SONYPI_THUMBPHRASE_MASK, sonypi_thumbphraseev },
  2639. { 0x31, SONYPI_MEMORYSTICK_MASK, sonypi_memorystickev },
  2640. { 0x41, SONYPI_BATTERY_MASK, sonypi_batteryev },
  2641. { 0x31, SONYPI_PKEY_MASK, sonypi_pkeyev },
  2642. { 0 },
  2643. };
  2644. static struct sonypi_eventtypes type3_events[] = {
  2645. { 0, 0xffffffff, sonypi_releaseev },
  2646. { 0x21, SONYPI_FNKEY_MASK, sonypi_fnkeyev },
  2647. { 0x31, SONYPI_WIRELESS_MASK, sonypi_wlessev },
  2648. { 0x31, SONYPI_MEMORYSTICK_MASK, sonypi_memorystickev },
  2649. { 0x41, SONYPI_BATTERY_MASK, sonypi_batteryev },
  2650. { 0x31, SONYPI_PKEY_MASK, sonypi_pkeyev },
  2651. { 0x05, SONYPI_PKEY_MASK, sonypi_pkeyev },
  2652. { 0x05, SONYPI_ZOOM_MASK, sonypi_zoomev },
  2653. { 0x05, SONYPI_CAPTURE_MASK, sonypi_captureev },
  2654. { 0x05, SONYPI_PKEY_MASK, sonypi_volumeev },
  2655. { 0x05, SONYPI_PKEY_MASK, sonypi_brightnessev },
  2656. { 0 },
  2657. };
  2658. /* low level spic calls */
  2659. #define ITERATIONS_LONG 10000
  2660. #define ITERATIONS_SHORT 10
  2661. #define wait_on_command(command, iterations) { \
  2662. unsigned int n = iterations; \
  2663. while (--n && (command)) \
  2664. udelay(1); \
  2665. if (!n) \
  2666. dprintk("command failed at %s : %s (line %d)\n", \
  2667. __FILE__, __func__, __LINE__); \
  2668. }
  2669. static u8 sony_pic_call1(u8 dev)
  2670. {
  2671. u8 v1, v2;
  2672. wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2,
  2673. ITERATIONS_LONG);
  2674. outb(dev, spic_dev.cur_ioport->io1.minimum + 4);
  2675. v1 = inb_p(spic_dev.cur_ioport->io1.minimum + 4);
  2676. v2 = inb_p(spic_dev.cur_ioport->io1.minimum);
  2677. dprintk("sony_pic_call1(0x%.2x): 0x%.4x\n", dev, (v2 << 8) | v1);
  2678. return v2;
  2679. }
  2680. static u8 sony_pic_call2(u8 dev, u8 fn)
  2681. {
  2682. u8 v1;
  2683. wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2,
  2684. ITERATIONS_LONG);
  2685. outb(dev, spic_dev.cur_ioport->io1.minimum + 4);
  2686. wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2,
  2687. ITERATIONS_LONG);
  2688. outb(fn, spic_dev.cur_ioport->io1.minimum);
  2689. v1 = inb_p(spic_dev.cur_ioport->io1.minimum);
  2690. dprintk("sony_pic_call2(0x%.2x - 0x%.2x): 0x%.4x\n", dev, fn, v1);
  2691. return v1;
  2692. }
  2693. static u8 sony_pic_call3(u8 dev, u8 fn, u8 v)
  2694. {
  2695. u8 v1;
  2696. wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2, ITERATIONS_LONG);
  2697. outb(dev, spic_dev.cur_ioport->io1.minimum + 4);
  2698. wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2, ITERATIONS_LONG);
  2699. outb(fn, spic_dev.cur_ioport->io1.minimum);
  2700. wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2, ITERATIONS_LONG);
  2701. outb(v, spic_dev.cur_ioport->io1.minimum);
  2702. v1 = inb_p(spic_dev.cur_ioport->io1.minimum);
  2703. dprintk("sony_pic_call3(0x%.2x - 0x%.2x - 0x%.2x): 0x%.4x\n",
  2704. dev, fn, v, v1);
  2705. return v1;
  2706. }
  2707. /*
  2708. * minidrivers for SPIC models
  2709. */
  2710. static int type3_handle_irq(const u8 data_mask, const u8 ev)
  2711. {
  2712. /*
  2713. * 0x31 could mean we have to take some extra action and wait for
  2714. * the next irq for some Type3 models, it will generate a new
  2715. * irq and we can read new data from the device:
  2716. * - 0x5c and 0x5f requires 0xA0
  2717. * - 0x61 requires 0xB3
  2718. */
  2719. if (data_mask == 0x31) {
  2720. if (ev == 0x5c || ev == 0x5f)
  2721. sony_pic_call1(0xA0);
  2722. else if (ev == 0x61)
  2723. sony_pic_call1(0xB3);
  2724. return 0;
  2725. }
  2726. return 1;
  2727. }
  2728. static void sony_pic_detect_device_type(struct sony_pic_dev *dev)
  2729. {
  2730. struct pci_dev *pcidev;
  2731. pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
  2732. PCI_DEVICE_ID_INTEL_82371AB_3, NULL);
  2733. if (pcidev) {
  2734. dev->model = SONYPI_DEVICE_TYPE1;
  2735. dev->evport_offset = SONYPI_TYPE1_OFFSET;
  2736. dev->event_types = type1_events;
  2737. goto out;
  2738. }
  2739. pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
  2740. PCI_DEVICE_ID_INTEL_ICH6_1, NULL);
  2741. if (pcidev) {
  2742. dev->model = SONYPI_DEVICE_TYPE2;
  2743. dev->evport_offset = SONYPI_TYPE2_OFFSET;
  2744. dev->event_types = type2_events;
  2745. goto out;
  2746. }
  2747. pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
  2748. PCI_DEVICE_ID_INTEL_ICH7_1, NULL);
  2749. if (pcidev) {
  2750. dev->model = SONYPI_DEVICE_TYPE3;
  2751. dev->handle_irq = type3_handle_irq;
  2752. dev->evport_offset = SONYPI_TYPE3_OFFSET;
  2753. dev->event_types = type3_events;
  2754. goto out;
  2755. }
  2756. pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
  2757. PCI_DEVICE_ID_INTEL_ICH8_4, NULL);
  2758. if (pcidev) {
  2759. dev->model = SONYPI_DEVICE_TYPE3;
  2760. dev->handle_irq = type3_handle_irq;
  2761. dev->evport_offset = SONYPI_TYPE3_OFFSET;
  2762. dev->event_types = type3_events;
  2763. goto out;
  2764. }
  2765. pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
  2766. PCI_DEVICE_ID_INTEL_ICH9_1, NULL);
  2767. if (pcidev) {
  2768. dev->model = SONYPI_DEVICE_TYPE3;
  2769. dev->handle_irq = type3_handle_irq;
  2770. dev->evport_offset = SONYPI_TYPE3_OFFSET;
  2771. dev->event_types = type3_events;
  2772. goto out;
  2773. }
  2774. /* default */
  2775. dev->model = SONYPI_DEVICE_TYPE2;
  2776. dev->evport_offset = SONYPI_TYPE2_OFFSET;
  2777. dev->event_types = type2_events;
  2778. out:
  2779. if (pcidev)
  2780. pci_dev_put(pcidev);
  2781. pr_info("detected Type%d model\n",
  2782. dev->model == SONYPI_DEVICE_TYPE1 ? 1 :
  2783. dev->model == SONYPI_DEVICE_TYPE2 ? 2 : 3);
  2784. }
  2785. /* camera tests and poweron/poweroff */
  2786. #define SONYPI_CAMERA_PICTURE 5
  2787. #define SONYPI_CAMERA_CONTROL 0x10
  2788. #define SONYPI_CAMERA_BRIGHTNESS 0
  2789. #define SONYPI_CAMERA_CONTRAST 1
  2790. #define SONYPI_CAMERA_HUE 2
  2791. #define SONYPI_CAMERA_COLOR 3
  2792. #define SONYPI_CAMERA_SHARPNESS 4
  2793. #define SONYPI_CAMERA_EXPOSURE_MASK 0xC
  2794. #define SONYPI_CAMERA_WHITE_BALANCE_MASK 0x3
  2795. #define SONYPI_CAMERA_PICTURE_MODE_MASK 0x30
  2796. #define SONYPI_CAMERA_MUTE_MASK 0x40
  2797. /* the rest don't need a loop until not 0xff */
  2798. #define SONYPI_CAMERA_AGC 6
  2799. #define SONYPI_CAMERA_AGC_MASK 0x30
  2800. #define SONYPI_CAMERA_SHUTTER_MASK 0x7
  2801. #define SONYPI_CAMERA_SHUTDOWN_REQUEST 7
  2802. #define SONYPI_CAMERA_CONTROL 0x10
  2803. #define SONYPI_CAMERA_STATUS 7
  2804. #define SONYPI_CAMERA_STATUS_READY 0x2
  2805. #define SONYPI_CAMERA_STATUS_POSITION 0x4
  2806. #define SONYPI_DIRECTION_BACKWARDS 0x4
  2807. #define SONYPI_CAMERA_REVISION 8
  2808. #define SONYPI_CAMERA_ROMVERSION 9
  2809. static int __sony_pic_camera_ready(void)
  2810. {
  2811. u8 v;
  2812. v = sony_pic_call2(0x8f, SONYPI_CAMERA_STATUS);
  2813. return (v != 0xff && (v & SONYPI_CAMERA_STATUS_READY));
  2814. }
  2815. static int __sony_pic_camera_off(void)
  2816. {
  2817. if (!camera) {
  2818. pr_warn("camera control not enabled\n");
  2819. return -ENODEV;
  2820. }
  2821. wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_PICTURE,
  2822. SONYPI_CAMERA_MUTE_MASK),
  2823. ITERATIONS_SHORT);
  2824. if (spic_dev.camera_power) {
  2825. sony_pic_call2(0x91, 0);
  2826. spic_dev.camera_power = 0;
  2827. }
  2828. return 0;
  2829. }
  2830. static int __sony_pic_camera_on(void)
  2831. {
  2832. int i, j, x;
  2833. if (!camera) {
  2834. pr_warn("camera control not enabled\n");
  2835. return -ENODEV;
  2836. }
  2837. if (spic_dev.camera_power)
  2838. return 0;
  2839. for (j = 5; j > 0; j--) {
  2840. for (x = 0; x < 100 && sony_pic_call2(0x91, 0x1); x++)
  2841. msleep(10);
  2842. sony_pic_call1(0x93);
  2843. for (i = 400; i > 0; i--) {
  2844. if (__sony_pic_camera_ready())
  2845. break;
  2846. msleep(10);
  2847. }
  2848. if (i)
  2849. break;
  2850. }
  2851. if (j == 0) {
  2852. pr_warn("failed to power on camera\n");
  2853. return -ENODEV;
  2854. }
  2855. wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_CONTROL,
  2856. 0x5a),
  2857. ITERATIONS_SHORT);
  2858. spic_dev.camera_power = 1;
  2859. return 0;
  2860. }
  2861. /* External camera command (exported to the motion eye v4l driver) */
  2862. int sony_pic_camera_command(int command, u8 value)
  2863. {
  2864. if (!camera)
  2865. return -EIO;
  2866. mutex_lock(&spic_dev.lock);
  2867. switch (command) {
  2868. case SONY_PIC_COMMAND_SETCAMERA:
  2869. if (value)
  2870. __sony_pic_camera_on();
  2871. else
  2872. __sony_pic_camera_off();
  2873. break;
  2874. case SONY_PIC_COMMAND_SETCAMERABRIGHTNESS:
  2875. wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_BRIGHTNESS, value),
  2876. ITERATIONS_SHORT);
  2877. break;
  2878. case SONY_PIC_COMMAND_SETCAMERACONTRAST:
  2879. wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_CONTRAST, value),
  2880. ITERATIONS_SHORT);
  2881. break;
  2882. case SONY_PIC_COMMAND_SETCAMERAHUE:
  2883. wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_HUE, value),
  2884. ITERATIONS_SHORT);
  2885. break;
  2886. case SONY_PIC_COMMAND_SETCAMERACOLOR:
  2887. wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_COLOR, value),
  2888. ITERATIONS_SHORT);
  2889. break;
  2890. case SONY_PIC_COMMAND_SETCAMERASHARPNESS:
  2891. wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_SHARPNESS, value),
  2892. ITERATIONS_SHORT);
  2893. break;
  2894. case SONY_PIC_COMMAND_SETCAMERAPICTURE:
  2895. wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_PICTURE, value),
  2896. ITERATIONS_SHORT);
  2897. break;
  2898. case SONY_PIC_COMMAND_SETCAMERAAGC:
  2899. wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_AGC, value),
  2900. ITERATIONS_SHORT);
  2901. break;
  2902. default:
  2903. pr_err("sony_pic_camera_command invalid: %d\n", command);
  2904. break;
  2905. }
  2906. mutex_unlock(&spic_dev.lock);
  2907. return 0;
  2908. }
  2909. EXPORT_SYMBOL(sony_pic_camera_command);
  2910. /* gprs/edge modem (SZ460N and SZ210P), thanks to Joshua Wise */
  2911. static void __sony_pic_set_wwanpower(u8 state)
  2912. {
  2913. state = !!state;
  2914. if (spic_dev.wwan_power == state)
  2915. return;
  2916. sony_pic_call2(0xB0, state);
  2917. sony_pic_call1(0x82);
  2918. spic_dev.wwan_power = state;
  2919. }
  2920. static ssize_t sony_pic_wwanpower_store(struct device *dev,
  2921. struct device_attribute *attr,
  2922. const char *buffer, size_t count)
  2923. {
  2924. unsigned long value;
  2925. if (count > 31)
  2926. return -EINVAL;
  2927. if (kstrtoul(buffer, 10, &value))
  2928. return -EINVAL;
  2929. mutex_lock(&spic_dev.lock);
  2930. __sony_pic_set_wwanpower(value);
  2931. mutex_unlock(&spic_dev.lock);
  2932. return count;
  2933. }
  2934. static ssize_t sony_pic_wwanpower_show(struct device *dev,
  2935. struct device_attribute *attr, char *buffer)
  2936. {
  2937. ssize_t count;
  2938. mutex_lock(&spic_dev.lock);
  2939. count = snprintf(buffer, PAGE_SIZE, "%d\n", spic_dev.wwan_power);
  2940. mutex_unlock(&spic_dev.lock);
  2941. return count;
  2942. }
  2943. /* bluetooth subsystem power state */
  2944. static void __sony_pic_set_bluetoothpower(u8 state)
  2945. {
  2946. state = !!state;
  2947. if (spic_dev.bluetooth_power == state)
  2948. return;
  2949. sony_pic_call2(0x96, state);
  2950. sony_pic_call1(0x82);
  2951. spic_dev.bluetooth_power = state;
  2952. }
  2953. static ssize_t sony_pic_bluetoothpower_store(struct device *dev,
  2954. struct device_attribute *attr,
  2955. const char *buffer, size_t count)
  2956. {
  2957. unsigned long value;
  2958. if (count > 31)
  2959. return -EINVAL;
  2960. if (kstrtoul(buffer, 10, &value))
  2961. return -EINVAL;
  2962. mutex_lock(&spic_dev.lock);
  2963. __sony_pic_set_bluetoothpower(value);
  2964. mutex_unlock(&spic_dev.lock);
  2965. return count;
  2966. }
  2967. static ssize_t sony_pic_bluetoothpower_show(struct device *dev,
  2968. struct device_attribute *attr, char *buffer)
  2969. {
  2970. ssize_t count = 0;
  2971. mutex_lock(&spic_dev.lock);
  2972. count = snprintf(buffer, PAGE_SIZE, "%d\n", spic_dev.bluetooth_power);
  2973. mutex_unlock(&spic_dev.lock);
  2974. return count;
  2975. }
  2976. /* fan speed */
  2977. /* FAN0 information (reverse engineered from ACPI tables) */
  2978. #define SONY_PIC_FAN0_STATUS 0x93
  2979. static int sony_pic_set_fanspeed(unsigned long value)
  2980. {
  2981. return ec_write(SONY_PIC_FAN0_STATUS, value);
  2982. }
  2983. static int sony_pic_get_fanspeed(u8 *value)
  2984. {
  2985. return ec_read(SONY_PIC_FAN0_STATUS, value);
  2986. }
  2987. static ssize_t sony_pic_fanspeed_store(struct device *dev,
  2988. struct device_attribute *attr,
  2989. const char *buffer, size_t count)
  2990. {
  2991. unsigned long value;
  2992. if (count > 31)
  2993. return -EINVAL;
  2994. if (kstrtoul(buffer, 10, &value))
  2995. return -EINVAL;
  2996. if (sony_pic_set_fanspeed(value))
  2997. return -EIO;
  2998. return count;
  2999. }
  3000. static ssize_t sony_pic_fanspeed_show(struct device *dev,
  3001. struct device_attribute *attr, char *buffer)
  3002. {
  3003. u8 value = 0;
  3004. if (sony_pic_get_fanspeed(&value))
  3005. return -EIO;
  3006. return snprintf(buffer, PAGE_SIZE, "%d\n", value);
  3007. }
  3008. #define SPIC_ATTR(_name, _mode) \
  3009. struct device_attribute spic_attr_##_name = __ATTR(_name, \
  3010. _mode, sony_pic_## _name ##_show, \
  3011. sony_pic_## _name ##_store)
  3012. static SPIC_ATTR(bluetoothpower, 0644);
  3013. static SPIC_ATTR(wwanpower, 0644);
  3014. static SPIC_ATTR(fanspeed, 0644);
  3015. static struct attribute *spic_attributes[] = {
  3016. &spic_attr_bluetoothpower.attr,
  3017. &spic_attr_wwanpower.attr,
  3018. &spic_attr_fanspeed.attr,
  3019. NULL
  3020. };
  3021. static struct attribute_group spic_attribute_group = {
  3022. .attrs = spic_attributes
  3023. };
  3024. /******** SONYPI compatibility **********/
  3025. #ifdef CONFIG_SONYPI_COMPAT
  3026. /* battery / brightness / temperature addresses */
  3027. #define SONYPI_BAT_FLAGS 0x81
  3028. #define SONYPI_LCD_LIGHT 0x96
  3029. #define SONYPI_BAT1_PCTRM 0xa0
  3030. #define SONYPI_BAT1_LEFT 0xa2
  3031. #define SONYPI_BAT1_MAXRT 0xa4
  3032. #define SONYPI_BAT2_PCTRM 0xa8
  3033. #define SONYPI_BAT2_LEFT 0xaa
  3034. #define SONYPI_BAT2_MAXRT 0xac
  3035. #define SONYPI_BAT1_MAXTK 0xb0
  3036. #define SONYPI_BAT1_FULL 0xb2
  3037. #define SONYPI_BAT2_MAXTK 0xb8
  3038. #define SONYPI_BAT2_FULL 0xba
  3039. #define SONYPI_TEMP_STATUS 0xC1
  3040. struct sonypi_compat_s {
  3041. struct fasync_struct *fifo_async;
  3042. struct kfifo fifo;
  3043. spinlock_t fifo_lock;
  3044. wait_queue_head_t fifo_proc_list;
  3045. atomic_t open_count;
  3046. };
  3047. static struct sonypi_compat_s sonypi_compat = {
  3048. .open_count = ATOMIC_INIT(0),
  3049. };
  3050. static int sonypi_misc_fasync(int fd, struct file *filp, int on)
  3051. {
  3052. return fasync_helper(fd, filp, on, &sonypi_compat.fifo_async);
  3053. }
  3054. static int sonypi_misc_release(struct inode *inode, struct file *file)
  3055. {
  3056. atomic_dec(&sonypi_compat.open_count);
  3057. return 0;
  3058. }
  3059. static int sonypi_misc_open(struct inode *inode, struct file *file)
  3060. {
  3061. /* Flush input queue on first open */
  3062. unsigned long flags;
  3063. spin_lock_irqsave(&sonypi_compat.fifo_lock, flags);
  3064. if (atomic_inc_return(&sonypi_compat.open_count) == 1)
  3065. kfifo_reset(&sonypi_compat.fifo);
  3066. spin_unlock_irqrestore(&sonypi_compat.fifo_lock, flags);
  3067. return 0;
  3068. }
  3069. static ssize_t sonypi_misc_read(struct file *file, char __user *buf,
  3070. size_t count, loff_t *pos)
  3071. {
  3072. ssize_t ret;
  3073. unsigned char c;
  3074. if ((kfifo_len(&sonypi_compat.fifo) == 0) &&
  3075. (file->f_flags & O_NONBLOCK))
  3076. return -EAGAIN;
  3077. ret = wait_event_interruptible(sonypi_compat.fifo_proc_list,
  3078. kfifo_len(&sonypi_compat.fifo) != 0);
  3079. if (ret)
  3080. return ret;
  3081. while (ret < count &&
  3082. (kfifo_out_locked(&sonypi_compat.fifo, &c, sizeof(c),
  3083. &sonypi_compat.fifo_lock) == sizeof(c))) {
  3084. if (put_user(c, buf++))
  3085. return -EFAULT;
  3086. ret++;
  3087. }
  3088. if (ret > 0) {
  3089. struct inode *inode = file_inode(file);
  3090. inode->i_atime = current_fs_time(inode->i_sb);
  3091. }
  3092. return ret;
  3093. }
  3094. static unsigned int sonypi_misc_poll(struct file *file, poll_table *wait)
  3095. {
  3096. poll_wait(file, &sonypi_compat.fifo_proc_list, wait);
  3097. if (kfifo_len(&sonypi_compat.fifo))
  3098. return POLLIN | POLLRDNORM;
  3099. return 0;
  3100. }
  3101. static int ec_read16(u8 addr, u16 *value)
  3102. {
  3103. u8 val_lb, val_hb;
  3104. if (ec_read(addr, &val_lb))
  3105. return -1;
  3106. if (ec_read(addr + 1, &val_hb))
  3107. return -1;
  3108. *value = val_lb | (val_hb << 8);
  3109. return 0;
  3110. }
  3111. static long sonypi_misc_ioctl(struct file *fp, unsigned int cmd,
  3112. unsigned long arg)
  3113. {
  3114. int ret = 0;
  3115. void __user *argp = (void __user *)arg;
  3116. u8 val8;
  3117. u16 val16;
  3118. int value;
  3119. mutex_lock(&spic_dev.lock);
  3120. switch (cmd) {
  3121. case SONYPI_IOCGBRT:
  3122. if (sony_bl_props.dev == NULL) {
  3123. ret = -EIO;
  3124. break;
  3125. }
  3126. if (sony_nc_int_call(sony_nc_acpi_handle, "GBRT", NULL,
  3127. &value)) {
  3128. ret = -EIO;
  3129. break;
  3130. }
  3131. val8 = ((value & 0xff) - 1) << 5;
  3132. if (copy_to_user(argp, &val8, sizeof(val8)))
  3133. ret = -EFAULT;
  3134. break;
  3135. case SONYPI_IOCSBRT:
  3136. if (sony_bl_props.dev == NULL) {
  3137. ret = -EIO;
  3138. break;
  3139. }
  3140. if (copy_from_user(&val8, argp, sizeof(val8))) {
  3141. ret = -EFAULT;
  3142. break;
  3143. }
  3144. value = (val8 >> 5) + 1;
  3145. if (sony_nc_int_call(sony_nc_acpi_handle, "SBRT", &value,
  3146. NULL)) {
  3147. ret = -EIO;
  3148. break;
  3149. }
  3150. /* sync the backlight device status */
  3151. sony_bl_props.dev->props.brightness =
  3152. sony_backlight_get_brightness(sony_bl_props.dev);
  3153. break;
  3154. case SONYPI_IOCGBAT1CAP:
  3155. if (ec_read16(SONYPI_BAT1_FULL, &val16)) {
  3156. ret = -EIO;
  3157. break;
  3158. }
  3159. if (copy_to_user(argp, &val16, sizeof(val16)))
  3160. ret = -EFAULT;
  3161. break;
  3162. case SONYPI_IOCGBAT1REM:
  3163. if (ec_read16(SONYPI_BAT1_LEFT, &val16)) {
  3164. ret = -EIO;
  3165. break;
  3166. }
  3167. if (copy_to_user(argp, &val16, sizeof(val16)))
  3168. ret = -EFAULT;
  3169. break;
  3170. case SONYPI_IOCGBAT2CAP:
  3171. if (ec_read16(SONYPI_BAT2_FULL, &val16)) {
  3172. ret = -EIO;
  3173. break;
  3174. }
  3175. if (copy_to_user(argp, &val16, sizeof(val16)))
  3176. ret = -EFAULT;
  3177. break;
  3178. case SONYPI_IOCGBAT2REM:
  3179. if (ec_read16(SONYPI_BAT2_LEFT, &val16)) {
  3180. ret = -EIO;
  3181. break;
  3182. }
  3183. if (copy_to_user(argp, &val16, sizeof(val16)))
  3184. ret = -EFAULT;
  3185. break;
  3186. case SONYPI_IOCGBATFLAGS:
  3187. if (ec_read(SONYPI_BAT_FLAGS, &val8)) {
  3188. ret = -EIO;
  3189. break;
  3190. }
  3191. val8 &= 0x07;
  3192. if (copy_to_user(argp, &val8, sizeof(val8)))
  3193. ret = -EFAULT;
  3194. break;
  3195. case SONYPI_IOCGBLUE:
  3196. val8 = spic_dev.bluetooth_power;
  3197. if (copy_to_user(argp, &val8, sizeof(val8)))
  3198. ret = -EFAULT;
  3199. break;
  3200. case SONYPI_IOCSBLUE:
  3201. if (copy_from_user(&val8, argp, sizeof(val8))) {
  3202. ret = -EFAULT;
  3203. break;
  3204. }
  3205. __sony_pic_set_bluetoothpower(val8);
  3206. break;
  3207. /* FAN Controls */
  3208. case SONYPI_IOCGFAN:
  3209. if (sony_pic_get_fanspeed(&val8)) {
  3210. ret = -EIO;
  3211. break;
  3212. }
  3213. if (copy_to_user(argp, &val8, sizeof(val8)))
  3214. ret = -EFAULT;
  3215. break;
  3216. case SONYPI_IOCSFAN:
  3217. if (copy_from_user(&val8, argp, sizeof(val8))) {
  3218. ret = -EFAULT;
  3219. break;
  3220. }
  3221. if (sony_pic_set_fanspeed(val8))
  3222. ret = -EIO;
  3223. break;
  3224. /* GET Temperature (useful under APM) */
  3225. case SONYPI_IOCGTEMP:
  3226. if (ec_read(SONYPI_TEMP_STATUS, &val8)) {
  3227. ret = -EIO;
  3228. break;
  3229. }
  3230. if (copy_to_user(argp, &val8, sizeof(val8)))
  3231. ret = -EFAULT;
  3232. break;
  3233. default:
  3234. ret = -EINVAL;
  3235. }
  3236. mutex_unlock(&spic_dev.lock);
  3237. return ret;
  3238. }
  3239. static const struct file_operations sonypi_misc_fops = {
  3240. .owner = THIS_MODULE,
  3241. .read = sonypi_misc_read,
  3242. .poll = sonypi_misc_poll,
  3243. .open = sonypi_misc_open,
  3244. .release = sonypi_misc_release,
  3245. .fasync = sonypi_misc_fasync,
  3246. .unlocked_ioctl = sonypi_misc_ioctl,
  3247. .llseek = noop_llseek,
  3248. };
  3249. static struct miscdevice sonypi_misc_device = {
  3250. .minor = MISC_DYNAMIC_MINOR,
  3251. .name = "sonypi",
  3252. .fops = &sonypi_misc_fops,
  3253. };
  3254. static void sonypi_compat_report_event(u8 event)
  3255. {
  3256. kfifo_in_locked(&sonypi_compat.fifo, (unsigned char *)&event,
  3257. sizeof(event), &sonypi_compat.fifo_lock);
  3258. kill_fasync(&sonypi_compat.fifo_async, SIGIO, POLL_IN);
  3259. wake_up_interruptible(&sonypi_compat.fifo_proc_list);
  3260. }
  3261. static int sonypi_compat_init(void)
  3262. {
  3263. int error;
  3264. spin_lock_init(&sonypi_compat.fifo_lock);
  3265. error =
  3266. kfifo_alloc(&sonypi_compat.fifo, SONY_LAPTOP_BUF_SIZE, GFP_KERNEL);
  3267. if (error) {
  3268. pr_err("kfifo_alloc failed\n");
  3269. return error;
  3270. }
  3271. init_waitqueue_head(&sonypi_compat.fifo_proc_list);
  3272. if (minor != -1)
  3273. sonypi_misc_device.minor = minor;
  3274. error = misc_register(&sonypi_misc_device);
  3275. if (error) {
  3276. pr_err("misc_register failed\n");
  3277. goto err_free_kfifo;
  3278. }
  3279. if (minor == -1)
  3280. pr_info("device allocated minor is %d\n",
  3281. sonypi_misc_device.minor);
  3282. return 0;
  3283. err_free_kfifo:
  3284. kfifo_free(&sonypi_compat.fifo);
  3285. return error;
  3286. }
  3287. static void sonypi_compat_exit(void)
  3288. {
  3289. misc_deregister(&sonypi_misc_device);
  3290. kfifo_free(&sonypi_compat.fifo);
  3291. }
  3292. #else
  3293. static int sonypi_compat_init(void) { return 0; }
  3294. static void sonypi_compat_exit(void) { }
  3295. static void sonypi_compat_report_event(u8 event) { }
  3296. #endif /* CONFIG_SONYPI_COMPAT */
  3297. /*
  3298. * ACPI callbacks
  3299. */
  3300. static acpi_status
  3301. sony_pic_read_possible_resource(struct acpi_resource *resource, void *context)
  3302. {
  3303. u32 i;
  3304. struct sony_pic_dev *dev = (struct sony_pic_dev *)context;
  3305. switch (resource->type) {
  3306. case ACPI_RESOURCE_TYPE_START_DEPENDENT:
  3307. {
  3308. /* start IO enumeration */
  3309. struct sony_pic_ioport *ioport = kzalloc(sizeof(*ioport), GFP_KERNEL);
  3310. if (!ioport)
  3311. return AE_ERROR;
  3312. list_add(&ioport->list, &dev->ioports);
  3313. return AE_OK;
  3314. }
  3315. case ACPI_RESOURCE_TYPE_END_DEPENDENT:
  3316. /* end IO enumeration */
  3317. return AE_OK;
  3318. case ACPI_RESOURCE_TYPE_IRQ:
  3319. {
  3320. struct acpi_resource_irq *p = &resource->data.irq;
  3321. struct sony_pic_irq *interrupt = NULL;
  3322. if (!p || !p->interrupt_count) {
  3323. /*
  3324. * IRQ descriptors may have no IRQ# bits set,
  3325. * particularly those those w/ _STA disabled
  3326. */
  3327. dprintk("Blank IRQ resource\n");
  3328. return AE_OK;
  3329. }
  3330. for (i = 0; i < p->interrupt_count; i++) {
  3331. if (!p->interrupts[i]) {
  3332. pr_warn("Invalid IRQ %d\n",
  3333. p->interrupts[i]);
  3334. continue;
  3335. }
  3336. interrupt = kzalloc(sizeof(*interrupt),
  3337. GFP_KERNEL);
  3338. if (!interrupt)
  3339. return AE_ERROR;
  3340. list_add(&interrupt->list, &dev->interrupts);
  3341. interrupt->irq.triggering = p->triggering;
  3342. interrupt->irq.polarity = p->polarity;
  3343. interrupt->irq.sharable = p->sharable;
  3344. interrupt->irq.interrupt_count = 1;
  3345. interrupt->irq.interrupts[0] = p->interrupts[i];
  3346. }
  3347. return AE_OK;
  3348. }
  3349. case ACPI_RESOURCE_TYPE_IO:
  3350. {
  3351. struct acpi_resource_io *io = &resource->data.io;
  3352. struct sony_pic_ioport *ioport =
  3353. list_first_entry(&dev->ioports, struct sony_pic_ioport, list);
  3354. if (!io) {
  3355. dprintk("Blank IO resource\n");
  3356. return AE_OK;
  3357. }
  3358. if (!ioport->io1.minimum) {
  3359. memcpy(&ioport->io1, io, sizeof(*io));
  3360. dprintk("IO1 at 0x%.4x (0x%.2x)\n", ioport->io1.minimum,
  3361. ioport->io1.address_length);
  3362. }
  3363. else if (!ioport->io2.minimum) {
  3364. memcpy(&ioport->io2, io, sizeof(*io));
  3365. dprintk("IO2 at 0x%.4x (0x%.2x)\n", ioport->io2.minimum,
  3366. ioport->io2.address_length);
  3367. }
  3368. else {
  3369. pr_err("Unknown SPIC Type, more than 2 IO Ports\n");
  3370. return AE_ERROR;
  3371. }
  3372. return AE_OK;
  3373. }
  3374. default:
  3375. dprintk("Resource %d isn't an IRQ nor an IO port\n",
  3376. resource->type);
  3377. case ACPI_RESOURCE_TYPE_END_TAG:
  3378. return AE_OK;
  3379. }
  3380. return AE_CTRL_TERMINATE;
  3381. }
  3382. static int sony_pic_possible_resources(struct acpi_device *device)
  3383. {
  3384. int result = 0;
  3385. acpi_status status = AE_OK;
  3386. if (!device)
  3387. return -EINVAL;
  3388. /* get device status */
  3389. /* see acpi_pci_link_get_current acpi_pci_link_get_possible */
  3390. dprintk("Evaluating _STA\n");
  3391. result = acpi_bus_get_status(device);
  3392. if (result) {
  3393. pr_warn("Unable to read status\n");
  3394. goto end;
  3395. }
  3396. if (!device->status.enabled)
  3397. dprintk("Device disabled\n");
  3398. else
  3399. dprintk("Device enabled\n");
  3400. /*
  3401. * Query and parse 'method'
  3402. */
  3403. dprintk("Evaluating %s\n", METHOD_NAME__PRS);
  3404. status = acpi_walk_resources(device->handle, METHOD_NAME__PRS,
  3405. sony_pic_read_possible_resource, &spic_dev);
  3406. if (ACPI_FAILURE(status)) {
  3407. pr_warn("Failure evaluating %s\n", METHOD_NAME__PRS);
  3408. result = -ENODEV;
  3409. }
  3410. end:
  3411. return result;
  3412. }
  3413. /*
  3414. * Disable the spic device by calling its _DIS method
  3415. */
  3416. static int sony_pic_disable(struct acpi_device *device)
  3417. {
  3418. acpi_status ret = acpi_evaluate_object(device->handle, "_DIS", NULL,
  3419. NULL);
  3420. if (ACPI_FAILURE(ret) && ret != AE_NOT_FOUND)
  3421. return -ENXIO;
  3422. dprintk("Device disabled\n");
  3423. return 0;
  3424. }
  3425. /*
  3426. * Based on drivers/acpi/pci_link.c:acpi_pci_link_set
  3427. *
  3428. * Call _SRS to set current resources
  3429. */
  3430. static int sony_pic_enable(struct acpi_device *device,
  3431. struct sony_pic_ioport *ioport, struct sony_pic_irq *irq)
  3432. {
  3433. acpi_status status;
  3434. int result = 0;
  3435. /* Type 1 resource layout is:
  3436. * IO
  3437. * IO
  3438. * IRQNoFlags
  3439. * End
  3440. *
  3441. * Type 2 and 3 resource layout is:
  3442. * IO
  3443. * IRQNoFlags
  3444. * End
  3445. */
  3446. struct {
  3447. struct acpi_resource res1;
  3448. struct acpi_resource res2;
  3449. struct acpi_resource res3;
  3450. struct acpi_resource res4;
  3451. } *resource;
  3452. struct acpi_buffer buffer = { 0, NULL };
  3453. if (!ioport || !irq)
  3454. return -EINVAL;
  3455. /* init acpi_buffer */
  3456. resource = kzalloc(sizeof(*resource) + 1, GFP_KERNEL);
  3457. if (!resource)
  3458. return -ENOMEM;
  3459. buffer.length = sizeof(*resource) + 1;
  3460. buffer.pointer = resource;
  3461. /* setup Type 1 resources */
  3462. if (spic_dev.model == SONYPI_DEVICE_TYPE1) {
  3463. /* setup io resources */
  3464. resource->res1.type = ACPI_RESOURCE_TYPE_IO;
  3465. resource->res1.length = sizeof(struct acpi_resource);
  3466. memcpy(&resource->res1.data.io, &ioport->io1,
  3467. sizeof(struct acpi_resource_io));
  3468. resource->res2.type = ACPI_RESOURCE_TYPE_IO;
  3469. resource->res2.length = sizeof(struct acpi_resource);
  3470. memcpy(&resource->res2.data.io, &ioport->io2,
  3471. sizeof(struct acpi_resource_io));
  3472. /* setup irq resource */
  3473. resource->res3.type = ACPI_RESOURCE_TYPE_IRQ;
  3474. resource->res3.length = sizeof(struct acpi_resource);
  3475. memcpy(&resource->res3.data.irq, &irq->irq,
  3476. sizeof(struct acpi_resource_irq));
  3477. /* we requested a shared irq */
  3478. resource->res3.data.irq.sharable = ACPI_SHARED;
  3479. resource->res4.type = ACPI_RESOURCE_TYPE_END_TAG;
  3480. }
  3481. /* setup Type 2/3 resources */
  3482. else {
  3483. /* setup io resource */
  3484. resource->res1.type = ACPI_RESOURCE_TYPE_IO;
  3485. resource->res1.length = sizeof(struct acpi_resource);
  3486. memcpy(&resource->res1.data.io, &ioport->io1,
  3487. sizeof(struct acpi_resource_io));
  3488. /* setup irq resource */
  3489. resource->res2.type = ACPI_RESOURCE_TYPE_IRQ;
  3490. resource->res2.length = sizeof(struct acpi_resource);
  3491. memcpy(&resource->res2.data.irq, &irq->irq,
  3492. sizeof(struct acpi_resource_irq));
  3493. /* we requested a shared irq */
  3494. resource->res2.data.irq.sharable = ACPI_SHARED;
  3495. resource->res3.type = ACPI_RESOURCE_TYPE_END_TAG;
  3496. }
  3497. /* Attempt to set the resource */
  3498. dprintk("Evaluating _SRS\n");
  3499. status = acpi_set_current_resources(device->handle, &buffer);
  3500. /* check for total failure */
  3501. if (ACPI_FAILURE(status)) {
  3502. pr_err("Error evaluating _SRS\n");
  3503. result = -ENODEV;
  3504. goto end;
  3505. }
  3506. /* Necessary device initializations calls (from sonypi) */
  3507. sony_pic_call1(0x82);
  3508. sony_pic_call2(0x81, 0xff);
  3509. sony_pic_call1(compat ? 0x92 : 0x82);
  3510. end:
  3511. kfree(resource);
  3512. return result;
  3513. }
  3514. /*****************
  3515. *
  3516. * ISR: some event is available
  3517. *
  3518. *****************/
  3519. static irqreturn_t sony_pic_irq(int irq, void *dev_id)
  3520. {
  3521. int i, j;
  3522. u8 ev = 0;
  3523. u8 data_mask = 0;
  3524. u8 device_event = 0;
  3525. struct sony_pic_dev *dev = (struct sony_pic_dev *) dev_id;
  3526. ev = inb_p(dev->cur_ioport->io1.minimum);
  3527. if (dev->cur_ioport->io2.minimum)
  3528. data_mask = inb_p(dev->cur_ioport->io2.minimum);
  3529. else
  3530. data_mask = inb_p(dev->cur_ioport->io1.minimum +
  3531. dev->evport_offset);
  3532. dprintk("event ([%.2x] [%.2x]) at port 0x%.4x(+0x%.2x)\n",
  3533. ev, data_mask, dev->cur_ioport->io1.minimum,
  3534. dev->evport_offset);
  3535. if (ev == 0x00 || ev == 0xff)
  3536. return IRQ_HANDLED;
  3537. for (i = 0; dev->event_types[i].mask; i++) {
  3538. if ((data_mask & dev->event_types[i].data) !=
  3539. dev->event_types[i].data)
  3540. continue;
  3541. if (!(mask & dev->event_types[i].mask))
  3542. continue;
  3543. for (j = 0; dev->event_types[i].events[j].event; j++) {
  3544. if (ev == dev->event_types[i].events[j].data) {
  3545. device_event =
  3546. dev->event_types[i].events[j].event;
  3547. /* some events may require ignoring */
  3548. if (!device_event)
  3549. return IRQ_HANDLED;
  3550. goto found;
  3551. }
  3552. }
  3553. }
  3554. /* Still not able to decode the event try to pass
  3555. * it over to the minidriver
  3556. */
  3557. if (dev->handle_irq && dev->handle_irq(data_mask, ev) == 0)
  3558. return IRQ_HANDLED;
  3559. dprintk("unknown event ([%.2x] [%.2x]) at port 0x%.4x(+0x%.2x)\n",
  3560. ev, data_mask, dev->cur_ioport->io1.minimum,
  3561. dev->evport_offset);
  3562. return IRQ_HANDLED;
  3563. found:
  3564. sony_laptop_report_input_event(device_event);
  3565. acpi_bus_generate_proc_event(dev->acpi_dev, 1, device_event);
  3566. sonypi_compat_report_event(device_event);
  3567. return IRQ_HANDLED;
  3568. }
  3569. /*****************
  3570. *
  3571. * ACPI driver
  3572. *
  3573. *****************/
  3574. static int sony_pic_remove(struct acpi_device *device)
  3575. {
  3576. struct sony_pic_ioport *io, *tmp_io;
  3577. struct sony_pic_irq *irq, *tmp_irq;
  3578. if (sony_pic_disable(device)) {
  3579. pr_err("Couldn't disable device\n");
  3580. return -ENXIO;
  3581. }
  3582. free_irq(spic_dev.cur_irq->irq.interrupts[0], &spic_dev);
  3583. release_region(spic_dev.cur_ioport->io1.minimum,
  3584. spic_dev.cur_ioport->io1.address_length);
  3585. if (spic_dev.cur_ioport->io2.minimum)
  3586. release_region(spic_dev.cur_ioport->io2.minimum,
  3587. spic_dev.cur_ioport->io2.address_length);
  3588. sonypi_compat_exit();
  3589. sony_laptop_remove_input();
  3590. /* pf attrs */
  3591. sysfs_remove_group(&sony_pf_device->dev.kobj, &spic_attribute_group);
  3592. sony_pf_remove();
  3593. list_for_each_entry_safe(io, tmp_io, &spic_dev.ioports, list) {
  3594. list_del(&io->list);
  3595. kfree(io);
  3596. }
  3597. list_for_each_entry_safe(irq, tmp_irq, &spic_dev.interrupts, list) {
  3598. list_del(&irq->list);
  3599. kfree(irq);
  3600. }
  3601. spic_dev.cur_ioport = NULL;
  3602. spic_dev.cur_irq = NULL;
  3603. dprintk(SONY_PIC_DRIVER_NAME " removed.\n");
  3604. return 0;
  3605. }
  3606. static int sony_pic_add(struct acpi_device *device)
  3607. {
  3608. int result;
  3609. struct sony_pic_ioport *io, *tmp_io;
  3610. struct sony_pic_irq *irq, *tmp_irq;
  3611. pr_info("%s v%s\n", SONY_PIC_DRIVER_NAME, SONY_LAPTOP_DRIVER_VERSION);
  3612. spic_dev.acpi_dev = device;
  3613. strcpy(acpi_device_class(device), "sony/hotkey");
  3614. sony_pic_detect_device_type(&spic_dev);
  3615. mutex_init(&spic_dev.lock);
  3616. /* read _PRS resources */
  3617. result = sony_pic_possible_resources(device);
  3618. if (result) {
  3619. pr_err("Unable to read possible resources\n");
  3620. goto err_free_resources;
  3621. }
  3622. /* setup input devices and helper fifo */
  3623. result = sony_laptop_setup_input(device);
  3624. if (result) {
  3625. pr_err("Unable to create input devices\n");
  3626. goto err_free_resources;
  3627. }
  3628. if (sonypi_compat_init())
  3629. goto err_remove_input;
  3630. /* request io port */
  3631. list_for_each_entry_reverse(io, &spic_dev.ioports, list) {
  3632. if (request_region(io->io1.minimum, io->io1.address_length,
  3633. "Sony Programmable I/O Device")) {
  3634. dprintk("I/O port1: 0x%.4x (0x%.4x) + 0x%.2x\n",
  3635. io->io1.minimum, io->io1.maximum,
  3636. io->io1.address_length);
  3637. /* Type 1 have 2 ioports */
  3638. if (io->io2.minimum) {
  3639. if (request_region(io->io2.minimum,
  3640. io->io2.address_length,
  3641. "Sony Programmable I/O Device")) {
  3642. dprintk("I/O port2: 0x%.4x (0x%.4x) + 0x%.2x\n",
  3643. io->io2.minimum, io->io2.maximum,
  3644. io->io2.address_length);
  3645. spic_dev.cur_ioport = io;
  3646. break;
  3647. }
  3648. else {
  3649. dprintk("Unable to get I/O port2: "
  3650. "0x%.4x (0x%.4x) + 0x%.2x\n",
  3651. io->io2.minimum, io->io2.maximum,
  3652. io->io2.address_length);
  3653. release_region(io->io1.minimum,
  3654. io->io1.address_length);
  3655. }
  3656. }
  3657. else {
  3658. spic_dev.cur_ioport = io;
  3659. break;
  3660. }
  3661. }
  3662. }
  3663. if (!spic_dev.cur_ioport) {
  3664. pr_err("Failed to request_region\n");
  3665. result = -ENODEV;
  3666. goto err_remove_compat;
  3667. }
  3668. /* request IRQ */
  3669. list_for_each_entry_reverse(irq, &spic_dev.interrupts, list) {
  3670. if (!request_irq(irq->irq.interrupts[0], sony_pic_irq,
  3671. 0, "sony-laptop", &spic_dev)) {
  3672. dprintk("IRQ: %d - triggering: %d - "
  3673. "polarity: %d - shr: %d\n",
  3674. irq->irq.interrupts[0],
  3675. irq->irq.triggering,
  3676. irq->irq.polarity,
  3677. irq->irq.sharable);
  3678. spic_dev.cur_irq = irq;
  3679. break;
  3680. }
  3681. }
  3682. if (!spic_dev.cur_irq) {
  3683. pr_err("Failed to request_irq\n");
  3684. result = -ENODEV;
  3685. goto err_release_region;
  3686. }
  3687. /* set resource status _SRS */
  3688. result = sony_pic_enable(device, spic_dev.cur_ioport, spic_dev.cur_irq);
  3689. if (result) {
  3690. pr_err("Couldn't enable device\n");
  3691. goto err_free_irq;
  3692. }
  3693. spic_dev.bluetooth_power = -1;
  3694. /* create device attributes */
  3695. result = sony_pf_add();
  3696. if (result)
  3697. goto err_disable_device;
  3698. result = sysfs_create_group(&sony_pf_device->dev.kobj, &spic_attribute_group);
  3699. if (result)
  3700. goto err_remove_pf;
  3701. return 0;
  3702. err_remove_pf:
  3703. sony_pf_remove();
  3704. err_disable_device:
  3705. sony_pic_disable(device);
  3706. err_free_irq:
  3707. free_irq(spic_dev.cur_irq->irq.interrupts[0], &spic_dev);
  3708. err_release_region:
  3709. release_region(spic_dev.cur_ioport->io1.minimum,
  3710. spic_dev.cur_ioport->io1.address_length);
  3711. if (spic_dev.cur_ioport->io2.minimum)
  3712. release_region(spic_dev.cur_ioport->io2.minimum,
  3713. spic_dev.cur_ioport->io2.address_length);
  3714. err_remove_compat:
  3715. sonypi_compat_exit();
  3716. err_remove_input:
  3717. sony_laptop_remove_input();
  3718. err_free_resources:
  3719. list_for_each_entry_safe(io, tmp_io, &spic_dev.ioports, list) {
  3720. list_del(&io->list);
  3721. kfree(io);
  3722. }
  3723. list_for_each_entry_safe(irq, tmp_irq, &spic_dev.interrupts, list) {
  3724. list_del(&irq->list);
  3725. kfree(irq);
  3726. }
  3727. spic_dev.cur_ioport = NULL;
  3728. spic_dev.cur_irq = NULL;
  3729. return result;
  3730. }
  3731. #ifdef CONFIG_PM_SLEEP
  3732. static int sony_pic_suspend(struct device *dev)
  3733. {
  3734. if (sony_pic_disable(to_acpi_device(dev)))
  3735. return -ENXIO;
  3736. return 0;
  3737. }
  3738. static int sony_pic_resume(struct device *dev)
  3739. {
  3740. sony_pic_enable(to_acpi_device(dev),
  3741. spic_dev.cur_ioport, spic_dev.cur_irq);
  3742. return 0;
  3743. }
  3744. #endif
  3745. static SIMPLE_DEV_PM_OPS(sony_pic_pm, sony_pic_suspend, sony_pic_resume);
  3746. static const struct acpi_device_id sony_pic_device_ids[] = {
  3747. {SONY_PIC_HID, 0},
  3748. {"", 0},
  3749. };
  3750. static struct acpi_driver sony_pic_driver = {
  3751. .name = SONY_PIC_DRIVER_NAME,
  3752. .class = SONY_PIC_CLASS,
  3753. .ids = sony_pic_device_ids,
  3754. .owner = THIS_MODULE,
  3755. .ops = {
  3756. .add = sony_pic_add,
  3757. .remove = sony_pic_remove,
  3758. },
  3759. .drv.pm = &sony_pic_pm,
  3760. };
  3761. static struct dmi_system_id __initdata sonypi_dmi_table[] = {
  3762. {
  3763. .ident = "Sony Vaio",
  3764. .matches = {
  3765. DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
  3766. DMI_MATCH(DMI_PRODUCT_NAME, "PCG-"),
  3767. },
  3768. },
  3769. {
  3770. .ident = "Sony Vaio",
  3771. .matches = {
  3772. DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
  3773. DMI_MATCH(DMI_PRODUCT_NAME, "VGN-"),
  3774. },
  3775. },
  3776. { }
  3777. };
  3778. static int __init sony_laptop_init(void)
  3779. {
  3780. int result;
  3781. if (!no_spic && dmi_check_system(sonypi_dmi_table)) {
  3782. result = acpi_bus_register_driver(&sony_pic_driver);
  3783. if (result) {
  3784. pr_err("Unable to register SPIC driver\n");
  3785. goto out;
  3786. }
  3787. spic_drv_registered = 1;
  3788. }
  3789. result = acpi_bus_register_driver(&sony_nc_driver);
  3790. if (result) {
  3791. pr_err("Unable to register SNC driver\n");
  3792. goto out_unregister_pic;
  3793. }
  3794. return 0;
  3795. out_unregister_pic:
  3796. if (spic_drv_registered)
  3797. acpi_bus_unregister_driver(&sony_pic_driver);
  3798. out:
  3799. return result;
  3800. }
  3801. static void __exit sony_laptop_exit(void)
  3802. {
  3803. acpi_bus_unregister_driver(&sony_nc_driver);
  3804. if (spic_drv_registered)
  3805. acpi_bus_unregister_driver(&sony_pic_driver);
  3806. }
  3807. module_init(sony_laptop_init);
  3808. module_exit(sony_laptop_exit);