sony-laptop.c 98 KB

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