sony-laptop.c 108 KB

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