sony-laptop.c 110 KB

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