sony-laptop.c 108 KB

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