sony-laptop.c 90 KB

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