sony-laptop.c 108 KB

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