sony-laptop.c 94 KB

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