sony-laptop.c 84 KB

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