sony-laptop.c 107 KB

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