sony-laptop.c 119 KB

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