sony-laptop.c 111 KB

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