sony-laptop.c 84 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334
  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. sysfs_attr_init(&handles->devattr.attr);
  660. handles->devattr.attr.name = "handles";
  661. handles->devattr.attr.mode = S_IRUGO;
  662. handles->devattr.show = sony_nc_handles_show;
  663. for (i = 0; i < ARRAY_SIZE(handles->cap); i++) {
  664. if (!acpi_callsetfunc(sony_nc_acpi_handle,
  665. "SN00", i + 0x20, &result)) {
  666. dprintk("caching handle 0x%.4x (offset: 0x%.2x)\n",
  667. result, i);
  668. handles->cap[i] = result;
  669. }
  670. }
  671. /* allow reading capabilities via sysfs */
  672. if (device_create_file(&pd->dev, &handles->devattr)) {
  673. kfree(handles);
  674. handles = NULL;
  675. return -1;
  676. }
  677. return 0;
  678. }
  679. static int sony_nc_handles_cleanup(struct platform_device *pd)
  680. {
  681. if (handles) {
  682. device_remove_file(&pd->dev, &handles->devattr);
  683. kfree(handles);
  684. handles = NULL;
  685. }
  686. return 0;
  687. }
  688. static int sony_find_snc_handle(int handle)
  689. {
  690. int i;
  691. /* not initialized yet, return early */
  692. if (!handles)
  693. return -1;
  694. for (i = 0; i < 0x10; i++) {
  695. if (handles->cap[i] == handle) {
  696. dprintk("found handle 0x%.4x (offset: 0x%.2x)\n",
  697. handle, i);
  698. return i;
  699. }
  700. }
  701. dprintk("handle 0x%.4x not found\n", handle);
  702. return -1;
  703. }
  704. static int sony_call_snc_handle(int handle, int argument, int *result)
  705. {
  706. int ret = 0;
  707. int offset = sony_find_snc_handle(handle);
  708. if (offset < 0)
  709. return -1;
  710. ret = acpi_callsetfunc(sony_nc_acpi_handle, "SN07", offset | argument,
  711. result);
  712. dprintk("called SN07 with 0x%.4x (result: 0x%.4x)\n", offset | argument,
  713. *result);
  714. return ret;
  715. }
  716. /*
  717. * sony_nc_values input/output validate functions
  718. */
  719. /* brightness_default_validate:
  720. *
  721. * manipulate input output values to keep consistency with the
  722. * backlight framework for which brightness values are 0-based.
  723. */
  724. static int brightness_default_validate(const int direction, const int value)
  725. {
  726. switch (direction) {
  727. case SNC_VALIDATE_OUT:
  728. return value - 1;
  729. case SNC_VALIDATE_IN:
  730. if (value >= 0 && value < SONY_MAX_BRIGHTNESS)
  731. return value + 1;
  732. }
  733. return -EINVAL;
  734. }
  735. /* boolean_validate:
  736. *
  737. * on input validate boolean values 0/1, on output just pass the
  738. * received value.
  739. */
  740. static int boolean_validate(const int direction, const int value)
  741. {
  742. if (direction == SNC_VALIDATE_IN) {
  743. if (value != 0 && value != 1)
  744. return -EINVAL;
  745. }
  746. return value;
  747. }
  748. /*
  749. * Sysfs show/store common to all sony_nc_values
  750. */
  751. static ssize_t sony_nc_sysfs_show(struct device *dev, struct device_attribute *attr,
  752. char *buffer)
  753. {
  754. int value;
  755. struct sony_nc_value *item =
  756. container_of(attr, struct sony_nc_value, devattr);
  757. if (!*item->acpiget)
  758. return -EIO;
  759. if (acpi_callgetfunc(sony_nc_acpi_handle, *item->acpiget, &value) < 0)
  760. return -EIO;
  761. if (item->validate)
  762. value = item->validate(SNC_VALIDATE_OUT, value);
  763. return snprintf(buffer, PAGE_SIZE, "%d\n", value);
  764. }
  765. static ssize_t sony_nc_sysfs_store(struct device *dev,
  766. struct device_attribute *attr,
  767. const char *buffer, size_t count)
  768. {
  769. int value;
  770. struct sony_nc_value *item =
  771. container_of(attr, struct sony_nc_value, devattr);
  772. if (!item->acpiset)
  773. return -EIO;
  774. if (count > 31)
  775. return -EINVAL;
  776. value = simple_strtoul(buffer, NULL, 10);
  777. if (item->validate)
  778. value = item->validate(SNC_VALIDATE_IN, value);
  779. if (value < 0)
  780. return value;
  781. if (acpi_callsetfunc(sony_nc_acpi_handle, *item->acpiset, value, NULL) < 0)
  782. return -EIO;
  783. item->value = value;
  784. item->valid = 1;
  785. return count;
  786. }
  787. /*
  788. * Backlight device
  789. */
  790. static int sony_backlight_update_status(struct backlight_device *bd)
  791. {
  792. return acpi_callsetfunc(sony_nc_acpi_handle, "SBRT",
  793. bd->props.brightness + 1, NULL);
  794. }
  795. static int sony_backlight_get_brightness(struct backlight_device *bd)
  796. {
  797. int value;
  798. if (acpi_callgetfunc(sony_nc_acpi_handle, "GBRT", &value))
  799. return 0;
  800. /* brightness levels are 1-based, while backlight ones are 0-based */
  801. return value - 1;
  802. }
  803. static int sony_nc_get_brightness_ng(struct backlight_device *bd)
  804. {
  805. int result;
  806. int *handle = (int *)bl_get_data(bd);
  807. sony_call_snc_handle(*handle, 0x0200, &result);
  808. return result & 0xff;
  809. }
  810. static int sony_nc_update_status_ng(struct backlight_device *bd)
  811. {
  812. int value, result;
  813. int *handle = (int *)bl_get_data(bd);
  814. value = bd->props.brightness;
  815. sony_call_snc_handle(*handle, 0x0100 | (value << 16), &result);
  816. return sony_nc_get_brightness_ng(bd);
  817. }
  818. static const struct backlight_ops sony_backlight_ops = {
  819. .options = BL_CORE_SUSPENDRESUME,
  820. .update_status = sony_backlight_update_status,
  821. .get_brightness = sony_backlight_get_brightness,
  822. };
  823. static const struct backlight_ops sony_backlight_ng_ops = {
  824. .options = BL_CORE_SUSPENDRESUME,
  825. .update_status = sony_nc_update_status_ng,
  826. .get_brightness = sony_nc_get_brightness_ng,
  827. };
  828. static int backlight_ng_handle;
  829. static struct backlight_device *sony_backlight_device;
  830. /*
  831. * New SNC-only Vaios event mapping to driver known keys
  832. */
  833. struct sony_nc_event {
  834. u8 data;
  835. u8 event;
  836. };
  837. static struct sony_nc_event sony_100_events[] = {
  838. { 0x90, SONYPI_EVENT_PKEY_P1 },
  839. { 0x10, SONYPI_EVENT_ANYBUTTON_RELEASED },
  840. { 0x91, SONYPI_EVENT_PKEY_P2 },
  841. { 0x11, SONYPI_EVENT_ANYBUTTON_RELEASED },
  842. { 0x81, SONYPI_EVENT_FNKEY_F1 },
  843. { 0x01, SONYPI_EVENT_FNKEY_RELEASED },
  844. { 0x82, SONYPI_EVENT_FNKEY_F2 },
  845. { 0x02, SONYPI_EVENT_FNKEY_RELEASED },
  846. { 0x83, SONYPI_EVENT_FNKEY_F3 },
  847. { 0x03, SONYPI_EVENT_FNKEY_RELEASED },
  848. { 0x84, SONYPI_EVENT_FNKEY_F4 },
  849. { 0x04, SONYPI_EVENT_FNKEY_RELEASED },
  850. { 0x85, SONYPI_EVENT_FNKEY_F5 },
  851. { 0x05, SONYPI_EVENT_FNKEY_RELEASED },
  852. { 0x86, SONYPI_EVENT_FNKEY_F6 },
  853. { 0x06, SONYPI_EVENT_FNKEY_RELEASED },
  854. { 0x87, SONYPI_EVENT_FNKEY_F7 },
  855. { 0x07, SONYPI_EVENT_FNKEY_RELEASED },
  856. { 0x89, SONYPI_EVENT_FNKEY_F9 },
  857. { 0x09, SONYPI_EVENT_FNKEY_RELEASED },
  858. { 0x8A, SONYPI_EVENT_FNKEY_F10 },
  859. { 0x0A, SONYPI_EVENT_FNKEY_RELEASED },
  860. { 0x8C, SONYPI_EVENT_FNKEY_F12 },
  861. { 0x0C, SONYPI_EVENT_FNKEY_RELEASED },
  862. { 0x9d, SONYPI_EVENT_ZOOM_PRESSED },
  863. { 0x1d, SONYPI_EVENT_ANYBUTTON_RELEASED },
  864. { 0x9f, SONYPI_EVENT_CD_EJECT_PRESSED },
  865. { 0x1f, SONYPI_EVENT_ANYBUTTON_RELEASED },
  866. { 0xa1, SONYPI_EVENT_MEDIA_PRESSED },
  867. { 0x21, SONYPI_EVENT_ANYBUTTON_RELEASED },
  868. { 0xa4, SONYPI_EVENT_CD_EJECT_PRESSED },
  869. { 0x24, SONYPI_EVENT_ANYBUTTON_RELEASED },
  870. { 0xa5, SONYPI_EVENT_VENDOR_PRESSED },
  871. { 0x25, SONYPI_EVENT_ANYBUTTON_RELEASED },
  872. { 0xa6, SONYPI_EVENT_HELP_PRESSED },
  873. { 0x26, SONYPI_EVENT_ANYBUTTON_RELEASED },
  874. { 0, 0 },
  875. };
  876. static struct sony_nc_event sony_127_events[] = {
  877. { 0x81, SONYPI_EVENT_MODEKEY_PRESSED },
  878. { 0x01, SONYPI_EVENT_ANYBUTTON_RELEASED },
  879. { 0x82, SONYPI_EVENT_PKEY_P1 },
  880. { 0x02, SONYPI_EVENT_ANYBUTTON_RELEASED },
  881. { 0x83, SONYPI_EVENT_PKEY_P2 },
  882. { 0x03, SONYPI_EVENT_ANYBUTTON_RELEASED },
  883. { 0x84, SONYPI_EVENT_PKEY_P3 },
  884. { 0x04, SONYPI_EVENT_ANYBUTTON_RELEASED },
  885. { 0x85, SONYPI_EVENT_PKEY_P4 },
  886. { 0x05, SONYPI_EVENT_ANYBUTTON_RELEASED },
  887. { 0x86, SONYPI_EVENT_PKEY_P5 },
  888. { 0x06, SONYPI_EVENT_ANYBUTTON_RELEASED },
  889. { 0x87, SONYPI_EVENT_SETTINGKEY_PRESSED },
  890. { 0x07, SONYPI_EVENT_ANYBUTTON_RELEASED },
  891. { 0, 0 },
  892. };
  893. /*
  894. * ACPI callbacks
  895. */
  896. static void sony_nc_notify(struct acpi_device *device, u32 event)
  897. {
  898. u32 ev = event;
  899. if (ev >= 0x90) {
  900. /* New-style event */
  901. int result;
  902. int key_handle = 0;
  903. ev -= 0x90;
  904. if (sony_find_snc_handle(0x100) == ev)
  905. key_handle = 0x100;
  906. if (sony_find_snc_handle(0x127) == ev)
  907. key_handle = 0x127;
  908. if (key_handle) {
  909. struct sony_nc_event *key_event;
  910. if (sony_call_snc_handle(key_handle, 0x200, &result)) {
  911. dprintk("sony_nc_notify, unable to decode"
  912. " event 0x%.2x 0x%.2x\n", key_handle,
  913. ev);
  914. /* restore the original event */
  915. ev = event;
  916. } else {
  917. ev = result & 0xFF;
  918. if (key_handle == 0x100)
  919. key_event = sony_100_events;
  920. else
  921. key_event = sony_127_events;
  922. for (; key_event->data; key_event++) {
  923. if (key_event->data == ev) {
  924. ev = key_event->event;
  925. break;
  926. }
  927. }
  928. if (!key_event->data)
  929. pr_info(DRV_PFX
  930. "Unknown event: 0x%x 0x%x\n",
  931. key_handle,
  932. ev);
  933. else
  934. sony_laptop_report_input_event(ev);
  935. }
  936. } else if (sony_find_snc_handle(sony_rfkill_handle) == ev) {
  937. sony_nc_rfkill_update();
  938. return;
  939. }
  940. } else
  941. sony_laptop_report_input_event(ev);
  942. dprintk("sony_nc_notify, event: 0x%.2x\n", ev);
  943. acpi_bus_generate_proc_event(sony_nc_acpi_device, 1, ev);
  944. }
  945. static acpi_status sony_walk_callback(acpi_handle handle, u32 level,
  946. void *context, void **return_value)
  947. {
  948. struct acpi_device_info *info;
  949. if (ACPI_SUCCESS(acpi_get_object_info(handle, &info))) {
  950. pr_warn(DRV_PFX "method: name: %4.4s, args %X\n",
  951. (char *)&info->name, info->param_count);
  952. kfree(info);
  953. }
  954. return AE_OK;
  955. }
  956. /*
  957. * ACPI device
  958. */
  959. static int sony_nc_function_setup(struct acpi_device *device)
  960. {
  961. int result;
  962. /* Enable all events */
  963. acpi_callsetfunc(sony_nc_acpi_handle, "SN02", 0xffff, &result);
  964. /* Setup hotkeys */
  965. sony_call_snc_handle(0x0100, 0, &result);
  966. sony_call_snc_handle(0x0101, 0, &result);
  967. sony_call_snc_handle(0x0102, 0x100, &result);
  968. sony_call_snc_handle(0x0127, 0, &result);
  969. return 0;
  970. }
  971. static int sony_nc_resume(struct acpi_device *device)
  972. {
  973. struct sony_nc_value *item;
  974. acpi_handle handle;
  975. for (item = sony_nc_values; item->name; item++) {
  976. int ret;
  977. if (!item->valid)
  978. continue;
  979. ret = acpi_callsetfunc(sony_nc_acpi_handle, *item->acpiset,
  980. item->value, NULL);
  981. if (ret < 0) {
  982. pr_err(DRV_PFX "%s: %d\n", __func__, ret);
  983. break;
  984. }
  985. }
  986. if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle, "ECON",
  987. &handle))) {
  988. if (acpi_callsetfunc(sony_nc_acpi_handle, "ECON", 1, NULL))
  989. dprintk("ECON Method failed\n");
  990. }
  991. if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle, "SN00",
  992. &handle))) {
  993. dprintk("Doing SNC setup\n");
  994. sony_nc_function_setup(device);
  995. }
  996. /* re-read rfkill state */
  997. sony_nc_rfkill_update();
  998. return 0;
  999. }
  1000. static void sony_nc_rfkill_cleanup(void)
  1001. {
  1002. int i;
  1003. for (i = 0; i < N_SONY_RFKILL; i++) {
  1004. if (sony_rfkill_devices[i]) {
  1005. rfkill_unregister(sony_rfkill_devices[i]);
  1006. rfkill_destroy(sony_rfkill_devices[i]);
  1007. }
  1008. }
  1009. }
  1010. static int sony_nc_rfkill_set(void *data, bool blocked)
  1011. {
  1012. int result;
  1013. int argument = sony_rfkill_address[(long) data] + 0x100;
  1014. if (!blocked)
  1015. argument |= 0xff0000;
  1016. return sony_call_snc_handle(sony_rfkill_handle, argument, &result);
  1017. }
  1018. static const struct rfkill_ops sony_rfkill_ops = {
  1019. .set_block = sony_nc_rfkill_set,
  1020. };
  1021. static int sony_nc_setup_rfkill(struct acpi_device *device,
  1022. enum sony_nc_rfkill nc_type)
  1023. {
  1024. int err = 0;
  1025. struct rfkill *rfk;
  1026. enum rfkill_type type;
  1027. const char *name;
  1028. int result;
  1029. bool hwblock;
  1030. switch (nc_type) {
  1031. case SONY_WIFI:
  1032. type = RFKILL_TYPE_WLAN;
  1033. name = "sony-wifi";
  1034. break;
  1035. case SONY_BLUETOOTH:
  1036. type = RFKILL_TYPE_BLUETOOTH;
  1037. name = "sony-bluetooth";
  1038. break;
  1039. case SONY_WWAN:
  1040. type = RFKILL_TYPE_WWAN;
  1041. name = "sony-wwan";
  1042. break;
  1043. case SONY_WIMAX:
  1044. type = RFKILL_TYPE_WIMAX;
  1045. name = "sony-wimax";
  1046. break;
  1047. default:
  1048. return -EINVAL;
  1049. }
  1050. rfk = rfkill_alloc(name, &device->dev, type,
  1051. &sony_rfkill_ops, (void *)nc_type);
  1052. if (!rfk)
  1053. return -ENOMEM;
  1054. sony_call_snc_handle(sony_rfkill_handle, 0x200, &result);
  1055. hwblock = !(result & 0x1);
  1056. rfkill_set_hw_state(rfk, hwblock);
  1057. err = rfkill_register(rfk);
  1058. if (err) {
  1059. rfkill_destroy(rfk);
  1060. return err;
  1061. }
  1062. sony_rfkill_devices[nc_type] = rfk;
  1063. return err;
  1064. }
  1065. static void sony_nc_rfkill_update(void)
  1066. {
  1067. enum sony_nc_rfkill i;
  1068. int result;
  1069. bool hwblock;
  1070. sony_call_snc_handle(sony_rfkill_handle, 0x200, &result);
  1071. hwblock = !(result & 0x1);
  1072. for (i = 0; i < N_SONY_RFKILL; i++) {
  1073. int argument = sony_rfkill_address[i];
  1074. if (!sony_rfkill_devices[i])
  1075. continue;
  1076. if (hwblock) {
  1077. if (rfkill_set_hw_state(sony_rfkill_devices[i], true)) {
  1078. /* we already know we're blocked */
  1079. }
  1080. continue;
  1081. }
  1082. sony_call_snc_handle(sony_rfkill_handle, argument, &result);
  1083. rfkill_set_states(sony_rfkill_devices[i],
  1084. !(result & 0xf), false);
  1085. }
  1086. }
  1087. static void sony_nc_rfkill_setup(struct acpi_device *device)
  1088. {
  1089. int offset;
  1090. u8 dev_code, i;
  1091. acpi_status status;
  1092. struct acpi_object_list params;
  1093. union acpi_object in_obj;
  1094. union acpi_object *device_enum;
  1095. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  1096. offset = sony_find_snc_handle(0x124);
  1097. if (offset == -1) {
  1098. offset = sony_find_snc_handle(0x135);
  1099. if (offset == -1)
  1100. return;
  1101. else
  1102. sony_rfkill_handle = 0x135;
  1103. } else
  1104. sony_rfkill_handle = 0x124;
  1105. dprintk("Found rkfill handle: 0x%.4x\n", sony_rfkill_handle);
  1106. /* need to read the whole buffer returned by the acpi call to SN06
  1107. * here otherwise we may miss some features
  1108. */
  1109. params.count = 1;
  1110. params.pointer = &in_obj;
  1111. in_obj.type = ACPI_TYPE_INTEGER;
  1112. in_obj.integer.value = offset;
  1113. status = acpi_evaluate_object(sony_nc_acpi_handle, "SN06", &params,
  1114. &buffer);
  1115. if (ACPI_FAILURE(status)) {
  1116. dprintk("Radio device enumeration failed\n");
  1117. return;
  1118. }
  1119. device_enum = (union acpi_object *) buffer.pointer;
  1120. if (!device_enum) {
  1121. pr_err(DRV_PFX "No SN06 return object.");
  1122. goto out_no_enum;
  1123. }
  1124. if (device_enum->type != ACPI_TYPE_BUFFER) {
  1125. pr_err(DRV_PFX "Invalid SN06 return object 0x%.2x\n",
  1126. device_enum->type);
  1127. goto out_no_enum;
  1128. }
  1129. /* the buffer is filled with magic numbers describing the devices
  1130. * available, 0xff terminates the enumeration
  1131. */
  1132. for (i = 0; i < device_enum->buffer.length; i++) {
  1133. dev_code = *(device_enum->buffer.pointer + i);
  1134. if (dev_code == 0xff)
  1135. break;
  1136. dprintk("Radio devices, looking at 0x%.2x\n", dev_code);
  1137. if (dev_code == 0 && !sony_rfkill_devices[SONY_WIFI])
  1138. sony_nc_setup_rfkill(device, SONY_WIFI);
  1139. if (dev_code == 0x10 && !sony_rfkill_devices[SONY_BLUETOOTH])
  1140. sony_nc_setup_rfkill(device, SONY_BLUETOOTH);
  1141. if ((0xf0 & dev_code) == 0x20 &&
  1142. !sony_rfkill_devices[SONY_WWAN])
  1143. sony_nc_setup_rfkill(device, SONY_WWAN);
  1144. if (dev_code == 0x30 && !sony_rfkill_devices[SONY_WIMAX])
  1145. sony_nc_setup_rfkill(device, SONY_WIMAX);
  1146. }
  1147. out_no_enum:
  1148. kfree(buffer.pointer);
  1149. return;
  1150. }
  1151. /* Keyboard backlight feature */
  1152. #define KBDBL_HANDLER 0x137
  1153. #define KBDBL_PRESENT 0xB00
  1154. #define SET_MODE 0xC00
  1155. #define SET_TIMEOUT 0xE00
  1156. struct kbd_backlight {
  1157. int mode;
  1158. int timeout;
  1159. struct device_attribute mode_attr;
  1160. struct device_attribute timeout_attr;
  1161. };
  1162. static struct kbd_backlight *kbdbl_handle;
  1163. static ssize_t __sony_nc_kbd_backlight_mode_set(u8 value)
  1164. {
  1165. int result;
  1166. if (value > 1)
  1167. return -EINVAL;
  1168. if (sony_call_snc_handle(KBDBL_HANDLER,
  1169. (value << 0x10) | SET_MODE, &result))
  1170. return -EIO;
  1171. kbdbl_handle->mode = value;
  1172. return 0;
  1173. }
  1174. static ssize_t sony_nc_kbd_backlight_mode_store(struct device *dev,
  1175. struct device_attribute *attr,
  1176. const char *buffer, size_t count)
  1177. {
  1178. int ret = 0;
  1179. unsigned long value;
  1180. if (count > 31)
  1181. return -EINVAL;
  1182. if (strict_strtoul(buffer, 10, &value))
  1183. return -EINVAL;
  1184. ret = __sony_nc_kbd_backlight_mode_set(value);
  1185. if (ret < 0)
  1186. return ret;
  1187. return count;
  1188. }
  1189. static ssize_t sony_nc_kbd_backlight_mode_show(struct device *dev,
  1190. struct device_attribute *attr, char *buffer)
  1191. {
  1192. ssize_t count = 0;
  1193. count = snprintf(buffer, PAGE_SIZE, "%d\n", kbdbl_handle->mode);
  1194. return count;
  1195. }
  1196. static int __sony_nc_kbd_backlight_timeout_set(u8 value)
  1197. {
  1198. int result;
  1199. if (value > 3)
  1200. return -EINVAL;
  1201. if (sony_call_snc_handle(KBDBL_HANDLER,
  1202. (value << 0x10) | SET_TIMEOUT, &result))
  1203. return -EIO;
  1204. kbdbl_handle->timeout = value;
  1205. return 0;
  1206. }
  1207. static ssize_t sony_nc_kbd_backlight_timeout_store(struct device *dev,
  1208. struct device_attribute *attr,
  1209. const char *buffer, size_t count)
  1210. {
  1211. int ret = 0;
  1212. unsigned long value;
  1213. if (count > 31)
  1214. return -EINVAL;
  1215. if (strict_strtoul(buffer, 10, &value))
  1216. return -EINVAL;
  1217. ret = __sony_nc_kbd_backlight_timeout_set(value);
  1218. if (ret < 0)
  1219. return ret;
  1220. return count;
  1221. }
  1222. static ssize_t sony_nc_kbd_backlight_timeout_show(struct device *dev,
  1223. struct device_attribute *attr, char *buffer)
  1224. {
  1225. ssize_t count = 0;
  1226. count = snprintf(buffer, PAGE_SIZE, "%d\n", kbdbl_handle->timeout);
  1227. return count;
  1228. }
  1229. static int sony_nc_kbd_backlight_setup(struct platform_device *pd)
  1230. {
  1231. int result;
  1232. if (sony_call_snc_handle(0x137, KBDBL_PRESENT, &result))
  1233. return 0;
  1234. if (!(result & 0x02))
  1235. return 0;
  1236. kbdbl_handle = kzalloc(sizeof(*kbdbl_handle), GFP_KERNEL);
  1237. if (!kbdbl_handle)
  1238. return -ENOMEM;
  1239. sysfs_attr_init(&kbdbl_handle->mode_attr.attr);
  1240. kbdbl_handle->mode_attr.attr.name = "kbd_backlight";
  1241. kbdbl_handle->mode_attr.attr.mode = S_IRUGO | S_IWUSR;
  1242. kbdbl_handle->mode_attr.show = sony_nc_kbd_backlight_mode_show;
  1243. kbdbl_handle->mode_attr.store = sony_nc_kbd_backlight_mode_store;
  1244. sysfs_attr_init(&kbdbl_handle->timeout_attr.attr);
  1245. kbdbl_handle->timeout_attr.attr.name = "kbd_backlight_timeout";
  1246. kbdbl_handle->timeout_attr.attr.mode = S_IRUGO | S_IWUSR;
  1247. kbdbl_handle->timeout_attr.show = sony_nc_kbd_backlight_timeout_show;
  1248. kbdbl_handle->timeout_attr.store = sony_nc_kbd_backlight_timeout_store;
  1249. if (device_create_file(&pd->dev, &kbdbl_handle->mode_attr))
  1250. goto outkzalloc;
  1251. if (device_create_file(&pd->dev, &kbdbl_handle->timeout_attr))
  1252. goto outmode;
  1253. __sony_nc_kbd_backlight_mode_set(kbd_backlight);
  1254. __sony_nc_kbd_backlight_timeout_set(kbd_backlight_timeout);
  1255. return 0;
  1256. outmode:
  1257. device_remove_file(&pd->dev, &kbdbl_handle->mode_attr);
  1258. outkzalloc:
  1259. kfree(kbdbl_handle);
  1260. kbdbl_handle = NULL;
  1261. return -1;
  1262. }
  1263. static int sony_nc_kbd_backlight_cleanup(struct platform_device *pd)
  1264. {
  1265. if (kbdbl_handle) {
  1266. device_remove_file(&pd->dev, &kbdbl_handle->mode_attr);
  1267. device_remove_file(&pd->dev, &kbdbl_handle->timeout_attr);
  1268. kfree(kbdbl_handle);
  1269. }
  1270. return 0;
  1271. }
  1272. static void sony_nc_backlight_setup(void)
  1273. {
  1274. acpi_handle unused;
  1275. int max_brightness = 0;
  1276. const struct backlight_ops *ops = NULL;
  1277. struct backlight_properties props;
  1278. if (sony_find_snc_handle(0x12f) != -1) {
  1279. backlight_ng_handle = 0x12f;
  1280. ops = &sony_backlight_ng_ops;
  1281. max_brightness = 0xff;
  1282. } else if (sony_find_snc_handle(0x137) != -1) {
  1283. backlight_ng_handle = 0x137;
  1284. ops = &sony_backlight_ng_ops;
  1285. max_brightness = 0xff;
  1286. } else if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle, "GBRT",
  1287. &unused))) {
  1288. ops = &sony_backlight_ops;
  1289. max_brightness = SONY_MAX_BRIGHTNESS - 1;
  1290. } else
  1291. return;
  1292. memset(&props, 0, sizeof(struct backlight_properties));
  1293. props.type = BACKLIGHT_PLATFORM;
  1294. props.max_brightness = max_brightness;
  1295. sony_backlight_device = backlight_device_register("sony", NULL,
  1296. &backlight_ng_handle,
  1297. ops, &props);
  1298. if (IS_ERR(sony_backlight_device)) {
  1299. pr_warning(DRV_PFX "unable to register backlight device\n");
  1300. sony_backlight_device = NULL;
  1301. } else
  1302. sony_backlight_device->props.brightness =
  1303. ops->get_brightness(sony_backlight_device);
  1304. }
  1305. static void sony_nc_backlight_cleanup(void)
  1306. {
  1307. if (sony_backlight_device)
  1308. backlight_device_unregister(sony_backlight_device);
  1309. }
  1310. static int sony_nc_add(struct acpi_device *device)
  1311. {
  1312. acpi_status status;
  1313. int result = 0;
  1314. acpi_handle handle;
  1315. struct sony_nc_value *item;
  1316. pr_info(DRV_PFX "%s v%s.\n", SONY_NC_DRIVER_NAME,
  1317. SONY_LAPTOP_DRIVER_VERSION);
  1318. sony_nc_acpi_device = device;
  1319. strcpy(acpi_device_class(device), "sony/hotkey");
  1320. sony_nc_acpi_handle = device->handle;
  1321. /* read device status */
  1322. result = acpi_bus_get_status(device);
  1323. /* bail IFF the above call was successful and the device is not present */
  1324. if (!result && !device->status.present) {
  1325. dprintk("Device not present\n");
  1326. result = -ENODEV;
  1327. goto outwalk;
  1328. }
  1329. result = sony_pf_add();
  1330. if (result)
  1331. goto outpresent;
  1332. if (debug) {
  1333. status = acpi_walk_namespace(ACPI_TYPE_METHOD,
  1334. sony_nc_acpi_handle, 1, sony_walk_callback,
  1335. NULL, NULL, NULL);
  1336. if (ACPI_FAILURE(status)) {
  1337. pr_warn(DRV_PFX "unable to walk acpi resources\n");
  1338. result = -ENODEV;
  1339. goto outpresent;
  1340. }
  1341. }
  1342. if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle, "ECON",
  1343. &handle))) {
  1344. if (acpi_callsetfunc(sony_nc_acpi_handle, "ECON", 1, NULL))
  1345. dprintk("ECON Method failed\n");
  1346. }
  1347. if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle, "SN00",
  1348. &handle))) {
  1349. dprintk("Doing SNC setup\n");
  1350. result = sony_nc_handles_setup(sony_pf_device);
  1351. if (result)
  1352. goto outpresent;
  1353. result = sony_nc_kbd_backlight_setup(sony_pf_device);
  1354. if (result)
  1355. goto outsnc;
  1356. sony_nc_function_setup(device);
  1357. sony_nc_rfkill_setup(device);
  1358. }
  1359. /* setup input devices and helper fifo */
  1360. result = sony_laptop_setup_input(device);
  1361. if (result) {
  1362. pr_err(DRV_PFX "Unable to create input devices.\n");
  1363. goto outkbdbacklight;
  1364. }
  1365. if (acpi_video_backlight_support()) {
  1366. pr_info(DRV_PFX "brightness ignored, must be "
  1367. "controlled by ACPI video driver\n");
  1368. } else {
  1369. sony_nc_backlight_setup();
  1370. }
  1371. /* create sony_pf sysfs attributes related to the SNC device */
  1372. for (item = sony_nc_values; item->name; ++item) {
  1373. if (!debug && item->debug)
  1374. continue;
  1375. /* find the available acpiget as described in the DSDT */
  1376. for (; item->acpiget && *item->acpiget; ++item->acpiget) {
  1377. if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle,
  1378. *item->acpiget,
  1379. &handle))) {
  1380. dprintk("Found %s getter: %s\n",
  1381. item->name, *item->acpiget);
  1382. item->devattr.attr.mode |= S_IRUGO;
  1383. break;
  1384. }
  1385. }
  1386. /* find the available acpiset as described in the DSDT */
  1387. for (; item->acpiset && *item->acpiset; ++item->acpiset) {
  1388. if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle,
  1389. *item->acpiset,
  1390. &handle))) {
  1391. dprintk("Found %s setter: %s\n",
  1392. item->name, *item->acpiset);
  1393. item->devattr.attr.mode |= S_IWUSR;
  1394. break;
  1395. }
  1396. }
  1397. if (item->devattr.attr.mode != 0) {
  1398. result =
  1399. device_create_file(&sony_pf_device->dev,
  1400. &item->devattr);
  1401. if (result)
  1402. goto out_sysfs;
  1403. }
  1404. }
  1405. return 0;
  1406. out_sysfs:
  1407. for (item = sony_nc_values; item->name; ++item) {
  1408. device_remove_file(&sony_pf_device->dev, &item->devattr);
  1409. }
  1410. sony_nc_backlight_cleanup();
  1411. sony_laptop_remove_input();
  1412. outkbdbacklight:
  1413. sony_nc_kbd_backlight_cleanup(sony_pf_device);
  1414. outsnc:
  1415. sony_nc_handles_cleanup(sony_pf_device);
  1416. outpresent:
  1417. sony_pf_remove();
  1418. outwalk:
  1419. sony_nc_rfkill_cleanup();
  1420. return result;
  1421. }
  1422. static int sony_nc_remove(struct acpi_device *device, int type)
  1423. {
  1424. struct sony_nc_value *item;
  1425. sony_nc_backlight_cleanup();
  1426. sony_nc_acpi_device = NULL;
  1427. for (item = sony_nc_values; item->name; ++item) {
  1428. device_remove_file(&sony_pf_device->dev, &item->devattr);
  1429. }
  1430. sony_nc_kbd_backlight_cleanup(sony_pf_device);
  1431. sony_nc_handles_cleanup(sony_pf_device);
  1432. sony_pf_remove();
  1433. sony_laptop_remove_input();
  1434. sony_nc_rfkill_cleanup();
  1435. dprintk(SONY_NC_DRIVER_NAME " removed.\n");
  1436. return 0;
  1437. }
  1438. static const struct acpi_device_id sony_device_ids[] = {
  1439. {SONY_NC_HID, 0},
  1440. {SONY_PIC_HID, 0},
  1441. {"", 0},
  1442. };
  1443. MODULE_DEVICE_TABLE(acpi, sony_device_ids);
  1444. static const struct acpi_device_id sony_nc_device_ids[] = {
  1445. {SONY_NC_HID, 0},
  1446. {"", 0},
  1447. };
  1448. static struct acpi_driver sony_nc_driver = {
  1449. .name = SONY_NC_DRIVER_NAME,
  1450. .class = SONY_NC_CLASS,
  1451. .ids = sony_nc_device_ids,
  1452. .owner = THIS_MODULE,
  1453. .ops = {
  1454. .add = sony_nc_add,
  1455. .remove = sony_nc_remove,
  1456. .resume = sony_nc_resume,
  1457. .notify = sony_nc_notify,
  1458. },
  1459. };
  1460. /*********** SPIC (SNY6001) Device ***********/
  1461. #define SONYPI_DEVICE_TYPE1 0x00000001
  1462. #define SONYPI_DEVICE_TYPE2 0x00000002
  1463. #define SONYPI_DEVICE_TYPE3 0x00000004
  1464. #define SONYPI_TYPE1_OFFSET 0x04
  1465. #define SONYPI_TYPE2_OFFSET 0x12
  1466. #define SONYPI_TYPE3_OFFSET 0x12
  1467. struct sony_pic_ioport {
  1468. struct acpi_resource_io io1;
  1469. struct acpi_resource_io io2;
  1470. struct list_head list;
  1471. };
  1472. struct sony_pic_irq {
  1473. struct acpi_resource_irq irq;
  1474. struct list_head list;
  1475. };
  1476. struct sonypi_eventtypes {
  1477. u8 data;
  1478. unsigned long mask;
  1479. struct sonypi_event *events;
  1480. };
  1481. struct sony_pic_dev {
  1482. struct acpi_device *acpi_dev;
  1483. struct sony_pic_irq *cur_irq;
  1484. struct sony_pic_ioport *cur_ioport;
  1485. struct list_head interrupts;
  1486. struct list_head ioports;
  1487. struct mutex lock;
  1488. struct sonypi_eventtypes *event_types;
  1489. int (*handle_irq)(const u8, const u8);
  1490. int model;
  1491. u16 evport_offset;
  1492. u8 camera_power;
  1493. u8 bluetooth_power;
  1494. u8 wwan_power;
  1495. };
  1496. static struct sony_pic_dev spic_dev = {
  1497. .interrupts = LIST_HEAD_INIT(spic_dev.interrupts),
  1498. .ioports = LIST_HEAD_INIT(spic_dev.ioports),
  1499. };
  1500. static int spic_drv_registered;
  1501. /* Event masks */
  1502. #define SONYPI_JOGGER_MASK 0x00000001
  1503. #define SONYPI_CAPTURE_MASK 0x00000002
  1504. #define SONYPI_FNKEY_MASK 0x00000004
  1505. #define SONYPI_BLUETOOTH_MASK 0x00000008
  1506. #define SONYPI_PKEY_MASK 0x00000010
  1507. #define SONYPI_BACK_MASK 0x00000020
  1508. #define SONYPI_HELP_MASK 0x00000040
  1509. #define SONYPI_LID_MASK 0x00000080
  1510. #define SONYPI_ZOOM_MASK 0x00000100
  1511. #define SONYPI_THUMBPHRASE_MASK 0x00000200
  1512. #define SONYPI_MEYE_MASK 0x00000400
  1513. #define SONYPI_MEMORYSTICK_MASK 0x00000800
  1514. #define SONYPI_BATTERY_MASK 0x00001000
  1515. #define SONYPI_WIRELESS_MASK 0x00002000
  1516. struct sonypi_event {
  1517. u8 data;
  1518. u8 event;
  1519. };
  1520. /* The set of possible button release events */
  1521. static struct sonypi_event sonypi_releaseev[] = {
  1522. { 0x00, SONYPI_EVENT_ANYBUTTON_RELEASED },
  1523. { 0, 0 }
  1524. };
  1525. /* The set of possible jogger events */
  1526. static struct sonypi_event sonypi_joggerev[] = {
  1527. { 0x1f, SONYPI_EVENT_JOGDIAL_UP },
  1528. { 0x01, SONYPI_EVENT_JOGDIAL_DOWN },
  1529. { 0x5f, SONYPI_EVENT_JOGDIAL_UP_PRESSED },
  1530. { 0x41, SONYPI_EVENT_JOGDIAL_DOWN_PRESSED },
  1531. { 0x1e, SONYPI_EVENT_JOGDIAL_FAST_UP },
  1532. { 0x02, SONYPI_EVENT_JOGDIAL_FAST_DOWN },
  1533. { 0x5e, SONYPI_EVENT_JOGDIAL_FAST_UP_PRESSED },
  1534. { 0x42, SONYPI_EVENT_JOGDIAL_FAST_DOWN_PRESSED },
  1535. { 0x1d, SONYPI_EVENT_JOGDIAL_VFAST_UP },
  1536. { 0x03, SONYPI_EVENT_JOGDIAL_VFAST_DOWN },
  1537. { 0x5d, SONYPI_EVENT_JOGDIAL_VFAST_UP_PRESSED },
  1538. { 0x43, SONYPI_EVENT_JOGDIAL_VFAST_DOWN_PRESSED },
  1539. { 0x40, SONYPI_EVENT_JOGDIAL_PRESSED },
  1540. { 0, 0 }
  1541. };
  1542. /* The set of possible capture button events */
  1543. static struct sonypi_event sonypi_captureev[] = {
  1544. { 0x05, SONYPI_EVENT_CAPTURE_PARTIALPRESSED },
  1545. { 0x07, SONYPI_EVENT_CAPTURE_PRESSED },
  1546. { 0x40, SONYPI_EVENT_CAPTURE_PRESSED },
  1547. { 0x01, SONYPI_EVENT_CAPTURE_PARTIALRELEASED },
  1548. { 0, 0 }
  1549. };
  1550. /* The set of possible fnkeys events */
  1551. static struct sonypi_event sonypi_fnkeyev[] = {
  1552. { 0x10, SONYPI_EVENT_FNKEY_ESC },
  1553. { 0x11, SONYPI_EVENT_FNKEY_F1 },
  1554. { 0x12, SONYPI_EVENT_FNKEY_F2 },
  1555. { 0x13, SONYPI_EVENT_FNKEY_F3 },
  1556. { 0x14, SONYPI_EVENT_FNKEY_F4 },
  1557. { 0x15, SONYPI_EVENT_FNKEY_F5 },
  1558. { 0x16, SONYPI_EVENT_FNKEY_F6 },
  1559. { 0x17, SONYPI_EVENT_FNKEY_F7 },
  1560. { 0x18, SONYPI_EVENT_FNKEY_F8 },
  1561. { 0x19, SONYPI_EVENT_FNKEY_F9 },
  1562. { 0x1a, SONYPI_EVENT_FNKEY_F10 },
  1563. { 0x1b, SONYPI_EVENT_FNKEY_F11 },
  1564. { 0x1c, SONYPI_EVENT_FNKEY_F12 },
  1565. { 0x1f, SONYPI_EVENT_FNKEY_RELEASED },
  1566. { 0x21, SONYPI_EVENT_FNKEY_1 },
  1567. { 0x22, SONYPI_EVENT_FNKEY_2 },
  1568. { 0x31, SONYPI_EVENT_FNKEY_D },
  1569. { 0x32, SONYPI_EVENT_FNKEY_E },
  1570. { 0x33, SONYPI_EVENT_FNKEY_F },
  1571. { 0x34, SONYPI_EVENT_FNKEY_S },
  1572. { 0x35, SONYPI_EVENT_FNKEY_B },
  1573. { 0x36, SONYPI_EVENT_FNKEY_ONLY },
  1574. { 0, 0 }
  1575. };
  1576. /* The set of possible program key events */
  1577. static struct sonypi_event sonypi_pkeyev[] = {
  1578. { 0x01, SONYPI_EVENT_PKEY_P1 },
  1579. { 0x02, SONYPI_EVENT_PKEY_P2 },
  1580. { 0x04, SONYPI_EVENT_PKEY_P3 },
  1581. { 0x20, SONYPI_EVENT_PKEY_P1 },
  1582. { 0, 0 }
  1583. };
  1584. /* The set of possible bluetooth events */
  1585. static struct sonypi_event sonypi_blueev[] = {
  1586. { 0x55, SONYPI_EVENT_BLUETOOTH_PRESSED },
  1587. { 0x59, SONYPI_EVENT_BLUETOOTH_ON },
  1588. { 0x5a, SONYPI_EVENT_BLUETOOTH_OFF },
  1589. { 0, 0 }
  1590. };
  1591. /* The set of possible wireless events */
  1592. static struct sonypi_event sonypi_wlessev[] = {
  1593. { 0x59, SONYPI_EVENT_IGNORE },
  1594. { 0x5a, SONYPI_EVENT_IGNORE },
  1595. { 0, 0 }
  1596. };
  1597. /* The set of possible back button events */
  1598. static struct sonypi_event sonypi_backev[] = {
  1599. { 0x20, SONYPI_EVENT_BACK_PRESSED },
  1600. { 0, 0 }
  1601. };
  1602. /* The set of possible help button events */
  1603. static struct sonypi_event sonypi_helpev[] = {
  1604. { 0x3b, SONYPI_EVENT_HELP_PRESSED },
  1605. { 0, 0 }
  1606. };
  1607. /* The set of possible lid events */
  1608. static struct sonypi_event sonypi_lidev[] = {
  1609. { 0x51, SONYPI_EVENT_LID_CLOSED },
  1610. { 0x50, SONYPI_EVENT_LID_OPENED },
  1611. { 0, 0 }
  1612. };
  1613. /* The set of possible zoom events */
  1614. static struct sonypi_event sonypi_zoomev[] = {
  1615. { 0x39, SONYPI_EVENT_ZOOM_PRESSED },
  1616. { 0x10, SONYPI_EVENT_ZOOM_IN_PRESSED },
  1617. { 0x20, SONYPI_EVENT_ZOOM_OUT_PRESSED },
  1618. { 0x04, SONYPI_EVENT_ZOOM_PRESSED },
  1619. { 0, 0 }
  1620. };
  1621. /* The set of possible thumbphrase events */
  1622. static struct sonypi_event sonypi_thumbphraseev[] = {
  1623. { 0x3a, SONYPI_EVENT_THUMBPHRASE_PRESSED },
  1624. { 0, 0 }
  1625. };
  1626. /* The set of possible motioneye camera events */
  1627. static struct sonypi_event sonypi_meyeev[] = {
  1628. { 0x00, SONYPI_EVENT_MEYE_FACE },
  1629. { 0x01, SONYPI_EVENT_MEYE_OPPOSITE },
  1630. { 0, 0 }
  1631. };
  1632. /* The set of possible memorystick events */
  1633. static struct sonypi_event sonypi_memorystickev[] = {
  1634. { 0x53, SONYPI_EVENT_MEMORYSTICK_INSERT },
  1635. { 0x54, SONYPI_EVENT_MEMORYSTICK_EJECT },
  1636. { 0, 0 }
  1637. };
  1638. /* The set of possible battery events */
  1639. static struct sonypi_event sonypi_batteryev[] = {
  1640. { 0x20, SONYPI_EVENT_BATTERY_INSERT },
  1641. { 0x30, SONYPI_EVENT_BATTERY_REMOVE },
  1642. { 0, 0 }
  1643. };
  1644. /* The set of possible volume events */
  1645. static struct sonypi_event sonypi_volumeev[] = {
  1646. { 0x01, SONYPI_EVENT_VOLUME_INC_PRESSED },
  1647. { 0x02, SONYPI_EVENT_VOLUME_DEC_PRESSED },
  1648. { 0, 0 }
  1649. };
  1650. /* The set of possible brightness events */
  1651. static struct sonypi_event sonypi_brightnessev[] = {
  1652. { 0x80, SONYPI_EVENT_BRIGHTNESS_PRESSED },
  1653. { 0, 0 }
  1654. };
  1655. static struct sonypi_eventtypes type1_events[] = {
  1656. { 0, 0xffffffff, sonypi_releaseev },
  1657. { 0x70, SONYPI_MEYE_MASK, sonypi_meyeev },
  1658. { 0x30, SONYPI_LID_MASK, sonypi_lidev },
  1659. { 0x60, SONYPI_CAPTURE_MASK, sonypi_captureev },
  1660. { 0x10, SONYPI_JOGGER_MASK, sonypi_joggerev },
  1661. { 0x20, SONYPI_FNKEY_MASK, sonypi_fnkeyev },
  1662. { 0x30, SONYPI_BLUETOOTH_MASK, sonypi_blueev },
  1663. { 0x40, SONYPI_PKEY_MASK, sonypi_pkeyev },
  1664. { 0x30, SONYPI_MEMORYSTICK_MASK, sonypi_memorystickev },
  1665. { 0x40, SONYPI_BATTERY_MASK, sonypi_batteryev },
  1666. { 0 },
  1667. };
  1668. static struct sonypi_eventtypes type2_events[] = {
  1669. { 0, 0xffffffff, sonypi_releaseev },
  1670. { 0x38, SONYPI_LID_MASK, sonypi_lidev },
  1671. { 0x11, SONYPI_JOGGER_MASK, sonypi_joggerev },
  1672. { 0x61, SONYPI_CAPTURE_MASK, sonypi_captureev },
  1673. { 0x21, SONYPI_FNKEY_MASK, sonypi_fnkeyev },
  1674. { 0x31, SONYPI_BLUETOOTH_MASK, sonypi_blueev },
  1675. { 0x08, SONYPI_PKEY_MASK, sonypi_pkeyev },
  1676. { 0x11, SONYPI_BACK_MASK, sonypi_backev },
  1677. { 0x21, SONYPI_HELP_MASK, sonypi_helpev },
  1678. { 0x21, SONYPI_ZOOM_MASK, sonypi_zoomev },
  1679. { 0x20, SONYPI_THUMBPHRASE_MASK, sonypi_thumbphraseev },
  1680. { 0x31, SONYPI_MEMORYSTICK_MASK, sonypi_memorystickev },
  1681. { 0x41, SONYPI_BATTERY_MASK, sonypi_batteryev },
  1682. { 0x31, SONYPI_PKEY_MASK, sonypi_pkeyev },
  1683. { 0 },
  1684. };
  1685. static struct sonypi_eventtypes type3_events[] = {
  1686. { 0, 0xffffffff, sonypi_releaseev },
  1687. { 0x21, SONYPI_FNKEY_MASK, sonypi_fnkeyev },
  1688. { 0x31, SONYPI_WIRELESS_MASK, sonypi_wlessev },
  1689. { 0x31, SONYPI_MEMORYSTICK_MASK, sonypi_memorystickev },
  1690. { 0x41, SONYPI_BATTERY_MASK, sonypi_batteryev },
  1691. { 0x31, SONYPI_PKEY_MASK, sonypi_pkeyev },
  1692. { 0x05, SONYPI_PKEY_MASK, sonypi_pkeyev },
  1693. { 0x05, SONYPI_ZOOM_MASK, sonypi_zoomev },
  1694. { 0x05, SONYPI_CAPTURE_MASK, sonypi_captureev },
  1695. { 0x05, SONYPI_PKEY_MASK, sonypi_volumeev },
  1696. { 0x05, SONYPI_PKEY_MASK, sonypi_brightnessev },
  1697. { 0 },
  1698. };
  1699. /* low level spic calls */
  1700. #define ITERATIONS_LONG 10000
  1701. #define ITERATIONS_SHORT 10
  1702. #define wait_on_command(command, iterations) { \
  1703. unsigned int n = iterations; \
  1704. while (--n && (command)) \
  1705. udelay(1); \
  1706. if (!n) \
  1707. dprintk("command failed at %s : %s (line %d)\n", \
  1708. __FILE__, __func__, __LINE__); \
  1709. }
  1710. static u8 sony_pic_call1(u8 dev)
  1711. {
  1712. u8 v1, v2;
  1713. wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2,
  1714. ITERATIONS_LONG);
  1715. outb(dev, spic_dev.cur_ioport->io1.minimum + 4);
  1716. v1 = inb_p(spic_dev.cur_ioport->io1.minimum + 4);
  1717. v2 = inb_p(spic_dev.cur_ioport->io1.minimum);
  1718. dprintk("sony_pic_call1(0x%.2x): 0x%.4x\n", dev, (v2 << 8) | v1);
  1719. return v2;
  1720. }
  1721. static u8 sony_pic_call2(u8 dev, u8 fn)
  1722. {
  1723. u8 v1;
  1724. wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2,
  1725. ITERATIONS_LONG);
  1726. outb(dev, spic_dev.cur_ioport->io1.minimum + 4);
  1727. wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2,
  1728. ITERATIONS_LONG);
  1729. outb(fn, spic_dev.cur_ioport->io1.minimum);
  1730. v1 = inb_p(spic_dev.cur_ioport->io1.minimum);
  1731. dprintk("sony_pic_call2(0x%.2x - 0x%.2x): 0x%.4x\n", dev, fn, v1);
  1732. return v1;
  1733. }
  1734. static u8 sony_pic_call3(u8 dev, u8 fn, u8 v)
  1735. {
  1736. u8 v1;
  1737. wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2, ITERATIONS_LONG);
  1738. outb(dev, spic_dev.cur_ioport->io1.minimum + 4);
  1739. wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2, ITERATIONS_LONG);
  1740. outb(fn, spic_dev.cur_ioport->io1.minimum);
  1741. wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2, ITERATIONS_LONG);
  1742. outb(v, spic_dev.cur_ioport->io1.minimum);
  1743. v1 = inb_p(spic_dev.cur_ioport->io1.minimum);
  1744. dprintk("sony_pic_call3(0x%.2x - 0x%.2x - 0x%.2x): 0x%.4x\n",
  1745. dev, fn, v, v1);
  1746. return v1;
  1747. }
  1748. /*
  1749. * minidrivers for SPIC models
  1750. */
  1751. static int type3_handle_irq(const u8 data_mask, const u8 ev)
  1752. {
  1753. /*
  1754. * 0x31 could mean we have to take some extra action and wait for
  1755. * the next irq for some Type3 models, it will generate a new
  1756. * irq and we can read new data from the device:
  1757. * - 0x5c and 0x5f requires 0xA0
  1758. * - 0x61 requires 0xB3
  1759. */
  1760. if (data_mask == 0x31) {
  1761. if (ev == 0x5c || ev == 0x5f)
  1762. sony_pic_call1(0xA0);
  1763. else if (ev == 0x61)
  1764. sony_pic_call1(0xB3);
  1765. return 0;
  1766. }
  1767. return 1;
  1768. }
  1769. static void sony_pic_detect_device_type(struct sony_pic_dev *dev)
  1770. {
  1771. struct pci_dev *pcidev;
  1772. pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
  1773. PCI_DEVICE_ID_INTEL_82371AB_3, NULL);
  1774. if (pcidev) {
  1775. dev->model = SONYPI_DEVICE_TYPE1;
  1776. dev->evport_offset = SONYPI_TYPE1_OFFSET;
  1777. dev->event_types = type1_events;
  1778. goto out;
  1779. }
  1780. pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
  1781. PCI_DEVICE_ID_INTEL_ICH6_1, NULL);
  1782. if (pcidev) {
  1783. dev->model = SONYPI_DEVICE_TYPE2;
  1784. dev->evport_offset = SONYPI_TYPE2_OFFSET;
  1785. dev->event_types = type2_events;
  1786. goto out;
  1787. }
  1788. pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
  1789. PCI_DEVICE_ID_INTEL_ICH7_1, NULL);
  1790. if (pcidev) {
  1791. dev->model = SONYPI_DEVICE_TYPE3;
  1792. dev->handle_irq = type3_handle_irq;
  1793. dev->evport_offset = SONYPI_TYPE3_OFFSET;
  1794. dev->event_types = type3_events;
  1795. goto out;
  1796. }
  1797. pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
  1798. PCI_DEVICE_ID_INTEL_ICH8_4, NULL);
  1799. if (pcidev) {
  1800. dev->model = SONYPI_DEVICE_TYPE3;
  1801. dev->handle_irq = type3_handle_irq;
  1802. dev->evport_offset = SONYPI_TYPE3_OFFSET;
  1803. dev->event_types = type3_events;
  1804. goto out;
  1805. }
  1806. pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
  1807. PCI_DEVICE_ID_INTEL_ICH9_1, NULL);
  1808. if (pcidev) {
  1809. dev->model = SONYPI_DEVICE_TYPE3;
  1810. dev->handle_irq = type3_handle_irq;
  1811. dev->evport_offset = SONYPI_TYPE3_OFFSET;
  1812. dev->event_types = type3_events;
  1813. goto out;
  1814. }
  1815. /* default */
  1816. dev->model = SONYPI_DEVICE_TYPE2;
  1817. dev->evport_offset = SONYPI_TYPE2_OFFSET;
  1818. dev->event_types = type2_events;
  1819. out:
  1820. if (pcidev)
  1821. pci_dev_put(pcidev);
  1822. pr_info(DRV_PFX "detected Type%d model\n",
  1823. dev->model == SONYPI_DEVICE_TYPE1 ? 1 :
  1824. dev->model == SONYPI_DEVICE_TYPE2 ? 2 : 3);
  1825. }
  1826. /* camera tests and poweron/poweroff */
  1827. #define SONYPI_CAMERA_PICTURE 5
  1828. #define SONYPI_CAMERA_CONTROL 0x10
  1829. #define SONYPI_CAMERA_BRIGHTNESS 0
  1830. #define SONYPI_CAMERA_CONTRAST 1
  1831. #define SONYPI_CAMERA_HUE 2
  1832. #define SONYPI_CAMERA_COLOR 3
  1833. #define SONYPI_CAMERA_SHARPNESS 4
  1834. #define SONYPI_CAMERA_EXPOSURE_MASK 0xC
  1835. #define SONYPI_CAMERA_WHITE_BALANCE_MASK 0x3
  1836. #define SONYPI_CAMERA_PICTURE_MODE_MASK 0x30
  1837. #define SONYPI_CAMERA_MUTE_MASK 0x40
  1838. /* the rest don't need a loop until not 0xff */
  1839. #define SONYPI_CAMERA_AGC 6
  1840. #define SONYPI_CAMERA_AGC_MASK 0x30
  1841. #define SONYPI_CAMERA_SHUTTER_MASK 0x7
  1842. #define SONYPI_CAMERA_SHUTDOWN_REQUEST 7
  1843. #define SONYPI_CAMERA_CONTROL 0x10
  1844. #define SONYPI_CAMERA_STATUS 7
  1845. #define SONYPI_CAMERA_STATUS_READY 0x2
  1846. #define SONYPI_CAMERA_STATUS_POSITION 0x4
  1847. #define SONYPI_DIRECTION_BACKWARDS 0x4
  1848. #define SONYPI_CAMERA_REVISION 8
  1849. #define SONYPI_CAMERA_ROMVERSION 9
  1850. static int __sony_pic_camera_ready(void)
  1851. {
  1852. u8 v;
  1853. v = sony_pic_call2(0x8f, SONYPI_CAMERA_STATUS);
  1854. return (v != 0xff && (v & SONYPI_CAMERA_STATUS_READY));
  1855. }
  1856. static int __sony_pic_camera_off(void)
  1857. {
  1858. if (!camera) {
  1859. pr_warn(DRV_PFX "camera control not enabled\n");
  1860. return -ENODEV;
  1861. }
  1862. wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_PICTURE,
  1863. SONYPI_CAMERA_MUTE_MASK),
  1864. ITERATIONS_SHORT);
  1865. if (spic_dev.camera_power) {
  1866. sony_pic_call2(0x91, 0);
  1867. spic_dev.camera_power = 0;
  1868. }
  1869. return 0;
  1870. }
  1871. static int __sony_pic_camera_on(void)
  1872. {
  1873. int i, j, x;
  1874. if (!camera) {
  1875. pr_warn(DRV_PFX "camera control not enabled\n");
  1876. return -ENODEV;
  1877. }
  1878. if (spic_dev.camera_power)
  1879. return 0;
  1880. for (j = 5; j > 0; j--) {
  1881. for (x = 0; x < 100 && sony_pic_call2(0x91, 0x1); x++)
  1882. msleep(10);
  1883. sony_pic_call1(0x93);
  1884. for (i = 400; i > 0; i--) {
  1885. if (__sony_pic_camera_ready())
  1886. break;
  1887. msleep(10);
  1888. }
  1889. if (i)
  1890. break;
  1891. }
  1892. if (j == 0) {
  1893. pr_warn(DRV_PFX "failed to power on camera\n");
  1894. return -ENODEV;
  1895. }
  1896. wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_CONTROL,
  1897. 0x5a),
  1898. ITERATIONS_SHORT);
  1899. spic_dev.camera_power = 1;
  1900. return 0;
  1901. }
  1902. /* External camera command (exported to the motion eye v4l driver) */
  1903. int sony_pic_camera_command(int command, u8 value)
  1904. {
  1905. if (!camera)
  1906. return -EIO;
  1907. mutex_lock(&spic_dev.lock);
  1908. switch (command) {
  1909. case SONY_PIC_COMMAND_SETCAMERA:
  1910. if (value)
  1911. __sony_pic_camera_on();
  1912. else
  1913. __sony_pic_camera_off();
  1914. break;
  1915. case SONY_PIC_COMMAND_SETCAMERABRIGHTNESS:
  1916. wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_BRIGHTNESS, value),
  1917. ITERATIONS_SHORT);
  1918. break;
  1919. case SONY_PIC_COMMAND_SETCAMERACONTRAST:
  1920. wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_CONTRAST, value),
  1921. ITERATIONS_SHORT);
  1922. break;
  1923. case SONY_PIC_COMMAND_SETCAMERAHUE:
  1924. wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_HUE, value),
  1925. ITERATIONS_SHORT);
  1926. break;
  1927. case SONY_PIC_COMMAND_SETCAMERACOLOR:
  1928. wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_COLOR, value),
  1929. ITERATIONS_SHORT);
  1930. break;
  1931. case SONY_PIC_COMMAND_SETCAMERASHARPNESS:
  1932. wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_SHARPNESS, value),
  1933. ITERATIONS_SHORT);
  1934. break;
  1935. case SONY_PIC_COMMAND_SETCAMERAPICTURE:
  1936. wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_PICTURE, value),
  1937. ITERATIONS_SHORT);
  1938. break;
  1939. case SONY_PIC_COMMAND_SETCAMERAAGC:
  1940. wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_AGC, value),
  1941. ITERATIONS_SHORT);
  1942. break;
  1943. default:
  1944. pr_err(DRV_PFX "sony_pic_camera_command invalid: %d\n",
  1945. command);
  1946. break;
  1947. }
  1948. mutex_unlock(&spic_dev.lock);
  1949. return 0;
  1950. }
  1951. EXPORT_SYMBOL(sony_pic_camera_command);
  1952. /* gprs/edge modem (SZ460N and SZ210P), thanks to Joshua Wise */
  1953. static void __sony_pic_set_wwanpower(u8 state)
  1954. {
  1955. state = !!state;
  1956. if (spic_dev.wwan_power == state)
  1957. return;
  1958. sony_pic_call2(0xB0, state);
  1959. sony_pic_call1(0x82);
  1960. spic_dev.wwan_power = state;
  1961. }
  1962. static ssize_t sony_pic_wwanpower_store(struct device *dev,
  1963. struct device_attribute *attr,
  1964. const char *buffer, size_t count)
  1965. {
  1966. unsigned long value;
  1967. if (count > 31)
  1968. return -EINVAL;
  1969. value = simple_strtoul(buffer, NULL, 10);
  1970. mutex_lock(&spic_dev.lock);
  1971. __sony_pic_set_wwanpower(value);
  1972. mutex_unlock(&spic_dev.lock);
  1973. return count;
  1974. }
  1975. static ssize_t sony_pic_wwanpower_show(struct device *dev,
  1976. struct device_attribute *attr, char *buffer)
  1977. {
  1978. ssize_t count;
  1979. mutex_lock(&spic_dev.lock);
  1980. count = snprintf(buffer, PAGE_SIZE, "%d\n", spic_dev.wwan_power);
  1981. mutex_unlock(&spic_dev.lock);
  1982. return count;
  1983. }
  1984. /* bluetooth subsystem power state */
  1985. static void __sony_pic_set_bluetoothpower(u8 state)
  1986. {
  1987. state = !!state;
  1988. if (spic_dev.bluetooth_power == state)
  1989. return;
  1990. sony_pic_call2(0x96, state);
  1991. sony_pic_call1(0x82);
  1992. spic_dev.bluetooth_power = state;
  1993. }
  1994. static ssize_t sony_pic_bluetoothpower_store(struct device *dev,
  1995. struct device_attribute *attr,
  1996. const char *buffer, size_t count)
  1997. {
  1998. unsigned long value;
  1999. if (count > 31)
  2000. return -EINVAL;
  2001. value = simple_strtoul(buffer, NULL, 10);
  2002. mutex_lock(&spic_dev.lock);
  2003. __sony_pic_set_bluetoothpower(value);
  2004. mutex_unlock(&spic_dev.lock);
  2005. return count;
  2006. }
  2007. static ssize_t sony_pic_bluetoothpower_show(struct device *dev,
  2008. struct device_attribute *attr, char *buffer)
  2009. {
  2010. ssize_t count = 0;
  2011. mutex_lock(&spic_dev.lock);
  2012. count = snprintf(buffer, PAGE_SIZE, "%d\n", spic_dev.bluetooth_power);
  2013. mutex_unlock(&spic_dev.lock);
  2014. return count;
  2015. }
  2016. /* fan speed */
  2017. /* FAN0 information (reverse engineered from ACPI tables) */
  2018. #define SONY_PIC_FAN0_STATUS 0x93
  2019. static int sony_pic_set_fanspeed(unsigned long value)
  2020. {
  2021. return ec_write(SONY_PIC_FAN0_STATUS, value);
  2022. }
  2023. static int sony_pic_get_fanspeed(u8 *value)
  2024. {
  2025. return ec_read(SONY_PIC_FAN0_STATUS, value);
  2026. }
  2027. static ssize_t sony_pic_fanspeed_store(struct device *dev,
  2028. struct device_attribute *attr,
  2029. const char *buffer, size_t count)
  2030. {
  2031. unsigned long value;
  2032. if (count > 31)
  2033. return -EINVAL;
  2034. value = simple_strtoul(buffer, NULL, 10);
  2035. if (sony_pic_set_fanspeed(value))
  2036. return -EIO;
  2037. return count;
  2038. }
  2039. static ssize_t sony_pic_fanspeed_show(struct device *dev,
  2040. struct device_attribute *attr, char *buffer)
  2041. {
  2042. u8 value = 0;
  2043. if (sony_pic_get_fanspeed(&value))
  2044. return -EIO;
  2045. return snprintf(buffer, PAGE_SIZE, "%d\n", value);
  2046. }
  2047. #define SPIC_ATTR(_name, _mode) \
  2048. struct device_attribute spic_attr_##_name = __ATTR(_name, \
  2049. _mode, sony_pic_## _name ##_show, \
  2050. sony_pic_## _name ##_store)
  2051. static SPIC_ATTR(bluetoothpower, 0644);
  2052. static SPIC_ATTR(wwanpower, 0644);
  2053. static SPIC_ATTR(fanspeed, 0644);
  2054. static struct attribute *spic_attributes[] = {
  2055. &spic_attr_bluetoothpower.attr,
  2056. &spic_attr_wwanpower.attr,
  2057. &spic_attr_fanspeed.attr,
  2058. NULL
  2059. };
  2060. static struct attribute_group spic_attribute_group = {
  2061. .attrs = spic_attributes
  2062. };
  2063. /******** SONYPI compatibility **********/
  2064. #ifdef CONFIG_SONYPI_COMPAT
  2065. /* battery / brightness / temperature addresses */
  2066. #define SONYPI_BAT_FLAGS 0x81
  2067. #define SONYPI_LCD_LIGHT 0x96
  2068. #define SONYPI_BAT1_PCTRM 0xa0
  2069. #define SONYPI_BAT1_LEFT 0xa2
  2070. #define SONYPI_BAT1_MAXRT 0xa4
  2071. #define SONYPI_BAT2_PCTRM 0xa8
  2072. #define SONYPI_BAT2_LEFT 0xaa
  2073. #define SONYPI_BAT2_MAXRT 0xac
  2074. #define SONYPI_BAT1_MAXTK 0xb0
  2075. #define SONYPI_BAT1_FULL 0xb2
  2076. #define SONYPI_BAT2_MAXTK 0xb8
  2077. #define SONYPI_BAT2_FULL 0xba
  2078. #define SONYPI_TEMP_STATUS 0xC1
  2079. struct sonypi_compat_s {
  2080. struct fasync_struct *fifo_async;
  2081. struct kfifo fifo;
  2082. spinlock_t fifo_lock;
  2083. wait_queue_head_t fifo_proc_list;
  2084. atomic_t open_count;
  2085. };
  2086. static struct sonypi_compat_s sonypi_compat = {
  2087. .open_count = ATOMIC_INIT(0),
  2088. };
  2089. static int sonypi_misc_fasync(int fd, struct file *filp, int on)
  2090. {
  2091. return fasync_helper(fd, filp, on, &sonypi_compat.fifo_async);
  2092. }
  2093. static int sonypi_misc_release(struct inode *inode, struct file *file)
  2094. {
  2095. atomic_dec(&sonypi_compat.open_count);
  2096. return 0;
  2097. }
  2098. static int sonypi_misc_open(struct inode *inode, struct file *file)
  2099. {
  2100. /* Flush input queue on first open */
  2101. unsigned long flags;
  2102. spin_lock_irqsave(&sonypi_compat.fifo_lock, flags);
  2103. if (atomic_inc_return(&sonypi_compat.open_count) == 1)
  2104. kfifo_reset(&sonypi_compat.fifo);
  2105. spin_unlock_irqrestore(&sonypi_compat.fifo_lock, flags);
  2106. return 0;
  2107. }
  2108. static ssize_t sonypi_misc_read(struct file *file, char __user *buf,
  2109. size_t count, loff_t *pos)
  2110. {
  2111. ssize_t ret;
  2112. unsigned char c;
  2113. if ((kfifo_len(&sonypi_compat.fifo) == 0) &&
  2114. (file->f_flags & O_NONBLOCK))
  2115. return -EAGAIN;
  2116. ret = wait_event_interruptible(sonypi_compat.fifo_proc_list,
  2117. kfifo_len(&sonypi_compat.fifo) != 0);
  2118. if (ret)
  2119. return ret;
  2120. while (ret < count &&
  2121. (kfifo_out_locked(&sonypi_compat.fifo, &c, sizeof(c),
  2122. &sonypi_compat.fifo_lock) == sizeof(c))) {
  2123. if (put_user(c, buf++))
  2124. return -EFAULT;
  2125. ret++;
  2126. }
  2127. if (ret > 0) {
  2128. struct inode *inode = file->f_path.dentry->d_inode;
  2129. inode->i_atime = current_fs_time(inode->i_sb);
  2130. }
  2131. return ret;
  2132. }
  2133. static unsigned int sonypi_misc_poll(struct file *file, poll_table *wait)
  2134. {
  2135. poll_wait(file, &sonypi_compat.fifo_proc_list, wait);
  2136. if (kfifo_len(&sonypi_compat.fifo))
  2137. return POLLIN | POLLRDNORM;
  2138. return 0;
  2139. }
  2140. static int ec_read16(u8 addr, u16 *value)
  2141. {
  2142. u8 val_lb, val_hb;
  2143. if (ec_read(addr, &val_lb))
  2144. return -1;
  2145. if (ec_read(addr + 1, &val_hb))
  2146. return -1;
  2147. *value = val_lb | (val_hb << 8);
  2148. return 0;
  2149. }
  2150. static long sonypi_misc_ioctl(struct file *fp, unsigned int cmd,
  2151. unsigned long arg)
  2152. {
  2153. int ret = 0;
  2154. void __user *argp = (void __user *)arg;
  2155. u8 val8;
  2156. u16 val16;
  2157. int value;
  2158. mutex_lock(&spic_dev.lock);
  2159. switch (cmd) {
  2160. case SONYPI_IOCGBRT:
  2161. if (sony_backlight_device == NULL) {
  2162. ret = -EIO;
  2163. break;
  2164. }
  2165. if (acpi_callgetfunc(sony_nc_acpi_handle, "GBRT", &value)) {
  2166. ret = -EIO;
  2167. break;
  2168. }
  2169. val8 = ((value & 0xff) - 1) << 5;
  2170. if (copy_to_user(argp, &val8, sizeof(val8)))
  2171. ret = -EFAULT;
  2172. break;
  2173. case SONYPI_IOCSBRT:
  2174. if (sony_backlight_device == NULL) {
  2175. ret = -EIO;
  2176. break;
  2177. }
  2178. if (copy_from_user(&val8, argp, sizeof(val8))) {
  2179. ret = -EFAULT;
  2180. break;
  2181. }
  2182. if (acpi_callsetfunc(sony_nc_acpi_handle, "SBRT",
  2183. (val8 >> 5) + 1, NULL)) {
  2184. ret = -EIO;
  2185. break;
  2186. }
  2187. /* sync the backlight device status */
  2188. sony_backlight_device->props.brightness =
  2189. sony_backlight_get_brightness(sony_backlight_device);
  2190. break;
  2191. case SONYPI_IOCGBAT1CAP:
  2192. if (ec_read16(SONYPI_BAT1_FULL, &val16)) {
  2193. ret = -EIO;
  2194. break;
  2195. }
  2196. if (copy_to_user(argp, &val16, sizeof(val16)))
  2197. ret = -EFAULT;
  2198. break;
  2199. case SONYPI_IOCGBAT1REM:
  2200. if (ec_read16(SONYPI_BAT1_LEFT, &val16)) {
  2201. ret = -EIO;
  2202. break;
  2203. }
  2204. if (copy_to_user(argp, &val16, sizeof(val16)))
  2205. ret = -EFAULT;
  2206. break;
  2207. case SONYPI_IOCGBAT2CAP:
  2208. if (ec_read16(SONYPI_BAT2_FULL, &val16)) {
  2209. ret = -EIO;
  2210. break;
  2211. }
  2212. if (copy_to_user(argp, &val16, sizeof(val16)))
  2213. ret = -EFAULT;
  2214. break;
  2215. case SONYPI_IOCGBAT2REM:
  2216. if (ec_read16(SONYPI_BAT2_LEFT, &val16)) {
  2217. ret = -EIO;
  2218. break;
  2219. }
  2220. if (copy_to_user(argp, &val16, sizeof(val16)))
  2221. ret = -EFAULT;
  2222. break;
  2223. case SONYPI_IOCGBATFLAGS:
  2224. if (ec_read(SONYPI_BAT_FLAGS, &val8)) {
  2225. ret = -EIO;
  2226. break;
  2227. }
  2228. val8 &= 0x07;
  2229. if (copy_to_user(argp, &val8, sizeof(val8)))
  2230. ret = -EFAULT;
  2231. break;
  2232. case SONYPI_IOCGBLUE:
  2233. val8 = spic_dev.bluetooth_power;
  2234. if (copy_to_user(argp, &val8, sizeof(val8)))
  2235. ret = -EFAULT;
  2236. break;
  2237. case SONYPI_IOCSBLUE:
  2238. if (copy_from_user(&val8, argp, sizeof(val8))) {
  2239. ret = -EFAULT;
  2240. break;
  2241. }
  2242. __sony_pic_set_bluetoothpower(val8);
  2243. break;
  2244. /* FAN Controls */
  2245. case SONYPI_IOCGFAN:
  2246. if (sony_pic_get_fanspeed(&val8)) {
  2247. ret = -EIO;
  2248. break;
  2249. }
  2250. if (copy_to_user(argp, &val8, sizeof(val8)))
  2251. ret = -EFAULT;
  2252. break;
  2253. case SONYPI_IOCSFAN:
  2254. if (copy_from_user(&val8, argp, sizeof(val8))) {
  2255. ret = -EFAULT;
  2256. break;
  2257. }
  2258. if (sony_pic_set_fanspeed(val8))
  2259. ret = -EIO;
  2260. break;
  2261. /* GET Temperature (useful under APM) */
  2262. case SONYPI_IOCGTEMP:
  2263. if (ec_read(SONYPI_TEMP_STATUS, &val8)) {
  2264. ret = -EIO;
  2265. break;
  2266. }
  2267. if (copy_to_user(argp, &val8, sizeof(val8)))
  2268. ret = -EFAULT;
  2269. break;
  2270. default:
  2271. ret = -EINVAL;
  2272. }
  2273. mutex_unlock(&spic_dev.lock);
  2274. return ret;
  2275. }
  2276. static const struct file_operations sonypi_misc_fops = {
  2277. .owner = THIS_MODULE,
  2278. .read = sonypi_misc_read,
  2279. .poll = sonypi_misc_poll,
  2280. .open = sonypi_misc_open,
  2281. .release = sonypi_misc_release,
  2282. .fasync = sonypi_misc_fasync,
  2283. .unlocked_ioctl = sonypi_misc_ioctl,
  2284. .llseek = noop_llseek,
  2285. };
  2286. static struct miscdevice sonypi_misc_device = {
  2287. .minor = MISC_DYNAMIC_MINOR,
  2288. .name = "sonypi",
  2289. .fops = &sonypi_misc_fops,
  2290. };
  2291. static void sonypi_compat_report_event(u8 event)
  2292. {
  2293. kfifo_in_locked(&sonypi_compat.fifo, (unsigned char *)&event,
  2294. sizeof(event), &sonypi_compat.fifo_lock);
  2295. kill_fasync(&sonypi_compat.fifo_async, SIGIO, POLL_IN);
  2296. wake_up_interruptible(&sonypi_compat.fifo_proc_list);
  2297. }
  2298. static int sonypi_compat_init(void)
  2299. {
  2300. int error;
  2301. spin_lock_init(&sonypi_compat.fifo_lock);
  2302. error =
  2303. kfifo_alloc(&sonypi_compat.fifo, SONY_LAPTOP_BUF_SIZE, GFP_KERNEL);
  2304. if (error) {
  2305. pr_err(DRV_PFX "kfifo_alloc failed\n");
  2306. return error;
  2307. }
  2308. init_waitqueue_head(&sonypi_compat.fifo_proc_list);
  2309. if (minor != -1)
  2310. sonypi_misc_device.minor = minor;
  2311. error = misc_register(&sonypi_misc_device);
  2312. if (error) {
  2313. pr_err(DRV_PFX "misc_register failed\n");
  2314. goto err_free_kfifo;
  2315. }
  2316. if (minor == -1)
  2317. pr_info(DRV_PFX "device allocated minor is %d\n",
  2318. sonypi_misc_device.minor);
  2319. return 0;
  2320. err_free_kfifo:
  2321. kfifo_free(&sonypi_compat.fifo);
  2322. return error;
  2323. }
  2324. static void sonypi_compat_exit(void)
  2325. {
  2326. misc_deregister(&sonypi_misc_device);
  2327. kfifo_free(&sonypi_compat.fifo);
  2328. }
  2329. #else
  2330. static int sonypi_compat_init(void) { return 0; }
  2331. static void sonypi_compat_exit(void) { }
  2332. static void sonypi_compat_report_event(u8 event) { }
  2333. #endif /* CONFIG_SONYPI_COMPAT */
  2334. /*
  2335. * ACPI callbacks
  2336. */
  2337. static acpi_status
  2338. sony_pic_read_possible_resource(struct acpi_resource *resource, void *context)
  2339. {
  2340. u32 i;
  2341. struct sony_pic_dev *dev = (struct sony_pic_dev *)context;
  2342. switch (resource->type) {
  2343. case ACPI_RESOURCE_TYPE_START_DEPENDENT:
  2344. {
  2345. /* start IO enumeration */
  2346. struct sony_pic_ioport *ioport = kzalloc(sizeof(*ioport), GFP_KERNEL);
  2347. if (!ioport)
  2348. return AE_ERROR;
  2349. list_add(&ioport->list, &dev->ioports);
  2350. return AE_OK;
  2351. }
  2352. case ACPI_RESOURCE_TYPE_END_DEPENDENT:
  2353. /* end IO enumeration */
  2354. return AE_OK;
  2355. case ACPI_RESOURCE_TYPE_IRQ:
  2356. {
  2357. struct acpi_resource_irq *p = &resource->data.irq;
  2358. struct sony_pic_irq *interrupt = NULL;
  2359. if (!p || !p->interrupt_count) {
  2360. /*
  2361. * IRQ descriptors may have no IRQ# bits set,
  2362. * particularly those those w/ _STA disabled
  2363. */
  2364. dprintk("Blank IRQ resource\n");
  2365. return AE_OK;
  2366. }
  2367. for (i = 0; i < p->interrupt_count; i++) {
  2368. if (!p->interrupts[i]) {
  2369. pr_warn(DRV_PFX "Invalid IRQ %d\n",
  2370. p->interrupts[i]);
  2371. continue;
  2372. }
  2373. interrupt = kzalloc(sizeof(*interrupt),
  2374. GFP_KERNEL);
  2375. if (!interrupt)
  2376. return AE_ERROR;
  2377. list_add(&interrupt->list, &dev->interrupts);
  2378. interrupt->irq.triggering = p->triggering;
  2379. interrupt->irq.polarity = p->polarity;
  2380. interrupt->irq.sharable = p->sharable;
  2381. interrupt->irq.interrupt_count = 1;
  2382. interrupt->irq.interrupts[0] = p->interrupts[i];
  2383. }
  2384. return AE_OK;
  2385. }
  2386. case ACPI_RESOURCE_TYPE_IO:
  2387. {
  2388. struct acpi_resource_io *io = &resource->data.io;
  2389. struct sony_pic_ioport *ioport =
  2390. list_first_entry(&dev->ioports, struct sony_pic_ioport, list);
  2391. if (!io) {
  2392. dprintk("Blank IO resource\n");
  2393. return AE_OK;
  2394. }
  2395. if (!ioport->io1.minimum) {
  2396. memcpy(&ioport->io1, io, sizeof(*io));
  2397. dprintk("IO1 at 0x%.4x (0x%.2x)\n", ioport->io1.minimum,
  2398. ioport->io1.address_length);
  2399. }
  2400. else if (!ioport->io2.minimum) {
  2401. memcpy(&ioport->io2, io, sizeof(*io));
  2402. dprintk("IO2 at 0x%.4x (0x%.2x)\n", ioport->io2.minimum,
  2403. ioport->io2.address_length);
  2404. }
  2405. else {
  2406. pr_err(DRV_PFX "Unknown SPIC Type, more than 2 IO Ports\n");
  2407. return AE_ERROR;
  2408. }
  2409. return AE_OK;
  2410. }
  2411. default:
  2412. dprintk("Resource %d isn't an IRQ nor an IO port\n",
  2413. resource->type);
  2414. case ACPI_RESOURCE_TYPE_END_TAG:
  2415. return AE_OK;
  2416. }
  2417. return AE_CTRL_TERMINATE;
  2418. }
  2419. static int sony_pic_possible_resources(struct acpi_device *device)
  2420. {
  2421. int result = 0;
  2422. acpi_status status = AE_OK;
  2423. if (!device)
  2424. return -EINVAL;
  2425. /* get device status */
  2426. /* see acpi_pci_link_get_current acpi_pci_link_get_possible */
  2427. dprintk("Evaluating _STA\n");
  2428. result = acpi_bus_get_status(device);
  2429. if (result) {
  2430. pr_warn(DRV_PFX "Unable to read status\n");
  2431. goto end;
  2432. }
  2433. if (!device->status.enabled)
  2434. dprintk("Device disabled\n");
  2435. else
  2436. dprintk("Device enabled\n");
  2437. /*
  2438. * Query and parse 'method'
  2439. */
  2440. dprintk("Evaluating %s\n", METHOD_NAME__PRS);
  2441. status = acpi_walk_resources(device->handle, METHOD_NAME__PRS,
  2442. sony_pic_read_possible_resource, &spic_dev);
  2443. if (ACPI_FAILURE(status)) {
  2444. pr_warn(DRV_PFX "Failure evaluating %s\n",
  2445. METHOD_NAME__PRS);
  2446. result = -ENODEV;
  2447. }
  2448. end:
  2449. return result;
  2450. }
  2451. /*
  2452. * Disable the spic device by calling its _DIS method
  2453. */
  2454. static int sony_pic_disable(struct acpi_device *device)
  2455. {
  2456. acpi_status ret = acpi_evaluate_object(device->handle, "_DIS", NULL,
  2457. NULL);
  2458. if (ACPI_FAILURE(ret) && ret != AE_NOT_FOUND)
  2459. return -ENXIO;
  2460. dprintk("Device disabled\n");
  2461. return 0;
  2462. }
  2463. /*
  2464. * Based on drivers/acpi/pci_link.c:acpi_pci_link_set
  2465. *
  2466. * Call _SRS to set current resources
  2467. */
  2468. static int sony_pic_enable(struct acpi_device *device,
  2469. struct sony_pic_ioport *ioport, struct sony_pic_irq *irq)
  2470. {
  2471. acpi_status status;
  2472. int result = 0;
  2473. /* Type 1 resource layout is:
  2474. * IO
  2475. * IO
  2476. * IRQNoFlags
  2477. * End
  2478. *
  2479. * Type 2 and 3 resource layout is:
  2480. * IO
  2481. * IRQNoFlags
  2482. * End
  2483. */
  2484. struct {
  2485. struct acpi_resource res1;
  2486. struct acpi_resource res2;
  2487. struct acpi_resource res3;
  2488. struct acpi_resource res4;
  2489. } *resource;
  2490. struct acpi_buffer buffer = { 0, NULL };
  2491. if (!ioport || !irq)
  2492. return -EINVAL;
  2493. /* init acpi_buffer */
  2494. resource = kzalloc(sizeof(*resource) + 1, GFP_KERNEL);
  2495. if (!resource)
  2496. return -ENOMEM;
  2497. buffer.length = sizeof(*resource) + 1;
  2498. buffer.pointer = resource;
  2499. /* setup Type 1 resources */
  2500. if (spic_dev.model == SONYPI_DEVICE_TYPE1) {
  2501. /* setup io resources */
  2502. resource->res1.type = ACPI_RESOURCE_TYPE_IO;
  2503. resource->res1.length = sizeof(struct acpi_resource);
  2504. memcpy(&resource->res1.data.io, &ioport->io1,
  2505. sizeof(struct acpi_resource_io));
  2506. resource->res2.type = ACPI_RESOURCE_TYPE_IO;
  2507. resource->res2.length = sizeof(struct acpi_resource);
  2508. memcpy(&resource->res2.data.io, &ioport->io2,
  2509. sizeof(struct acpi_resource_io));
  2510. /* setup irq resource */
  2511. resource->res3.type = ACPI_RESOURCE_TYPE_IRQ;
  2512. resource->res3.length = sizeof(struct acpi_resource);
  2513. memcpy(&resource->res3.data.irq, &irq->irq,
  2514. sizeof(struct acpi_resource_irq));
  2515. /* we requested a shared irq */
  2516. resource->res3.data.irq.sharable = ACPI_SHARED;
  2517. resource->res4.type = ACPI_RESOURCE_TYPE_END_TAG;
  2518. }
  2519. /* setup Type 2/3 resources */
  2520. else {
  2521. /* setup io resource */
  2522. resource->res1.type = ACPI_RESOURCE_TYPE_IO;
  2523. resource->res1.length = sizeof(struct acpi_resource);
  2524. memcpy(&resource->res1.data.io, &ioport->io1,
  2525. sizeof(struct acpi_resource_io));
  2526. /* setup irq resource */
  2527. resource->res2.type = ACPI_RESOURCE_TYPE_IRQ;
  2528. resource->res2.length = sizeof(struct acpi_resource);
  2529. memcpy(&resource->res2.data.irq, &irq->irq,
  2530. sizeof(struct acpi_resource_irq));
  2531. /* we requested a shared irq */
  2532. resource->res2.data.irq.sharable = ACPI_SHARED;
  2533. resource->res3.type = ACPI_RESOURCE_TYPE_END_TAG;
  2534. }
  2535. /* Attempt to set the resource */
  2536. dprintk("Evaluating _SRS\n");
  2537. status = acpi_set_current_resources(device->handle, &buffer);
  2538. /* check for total failure */
  2539. if (ACPI_FAILURE(status)) {
  2540. pr_err(DRV_PFX "Error evaluating _SRS\n");
  2541. result = -ENODEV;
  2542. goto end;
  2543. }
  2544. /* Necessary device initializations calls (from sonypi) */
  2545. sony_pic_call1(0x82);
  2546. sony_pic_call2(0x81, 0xff);
  2547. sony_pic_call1(compat ? 0x92 : 0x82);
  2548. end:
  2549. kfree(resource);
  2550. return result;
  2551. }
  2552. /*****************
  2553. *
  2554. * ISR: some event is available
  2555. *
  2556. *****************/
  2557. static irqreturn_t sony_pic_irq(int irq, void *dev_id)
  2558. {
  2559. int i, j;
  2560. u8 ev = 0;
  2561. u8 data_mask = 0;
  2562. u8 device_event = 0;
  2563. struct sony_pic_dev *dev = (struct sony_pic_dev *) dev_id;
  2564. ev = inb_p(dev->cur_ioport->io1.minimum);
  2565. if (dev->cur_ioport->io2.minimum)
  2566. data_mask = inb_p(dev->cur_ioport->io2.minimum);
  2567. else
  2568. data_mask = inb_p(dev->cur_ioport->io1.minimum +
  2569. dev->evport_offset);
  2570. dprintk("event ([%.2x] [%.2x]) at port 0x%.4x(+0x%.2x)\n",
  2571. ev, data_mask, dev->cur_ioport->io1.minimum,
  2572. dev->evport_offset);
  2573. if (ev == 0x00 || ev == 0xff)
  2574. return IRQ_HANDLED;
  2575. for (i = 0; dev->event_types[i].mask; i++) {
  2576. if ((data_mask & dev->event_types[i].data) !=
  2577. dev->event_types[i].data)
  2578. continue;
  2579. if (!(mask & dev->event_types[i].mask))
  2580. continue;
  2581. for (j = 0; dev->event_types[i].events[j].event; j++) {
  2582. if (ev == dev->event_types[i].events[j].data) {
  2583. device_event =
  2584. dev->event_types[i].events[j].event;
  2585. /* some events may require ignoring */
  2586. if (!device_event)
  2587. return IRQ_HANDLED;
  2588. goto found;
  2589. }
  2590. }
  2591. }
  2592. /* Still not able to decode the event try to pass
  2593. * it over to the minidriver
  2594. */
  2595. if (dev->handle_irq && dev->handle_irq(data_mask, ev) == 0)
  2596. return IRQ_HANDLED;
  2597. dprintk("unknown event ([%.2x] [%.2x]) at port 0x%.4x(+0x%.2x)\n",
  2598. ev, data_mask, dev->cur_ioport->io1.minimum,
  2599. dev->evport_offset);
  2600. return IRQ_HANDLED;
  2601. found:
  2602. sony_laptop_report_input_event(device_event);
  2603. acpi_bus_generate_proc_event(dev->acpi_dev, 1, device_event);
  2604. sonypi_compat_report_event(device_event);
  2605. return IRQ_HANDLED;
  2606. }
  2607. /*****************
  2608. *
  2609. * ACPI driver
  2610. *
  2611. *****************/
  2612. static int sony_pic_remove(struct acpi_device *device, int type)
  2613. {
  2614. struct sony_pic_ioport *io, *tmp_io;
  2615. struct sony_pic_irq *irq, *tmp_irq;
  2616. if (sony_pic_disable(device)) {
  2617. pr_err(DRV_PFX "Couldn't disable device.\n");
  2618. return -ENXIO;
  2619. }
  2620. free_irq(spic_dev.cur_irq->irq.interrupts[0], &spic_dev);
  2621. release_region(spic_dev.cur_ioport->io1.minimum,
  2622. spic_dev.cur_ioport->io1.address_length);
  2623. if (spic_dev.cur_ioport->io2.minimum)
  2624. release_region(spic_dev.cur_ioport->io2.minimum,
  2625. spic_dev.cur_ioport->io2.address_length);
  2626. sonypi_compat_exit();
  2627. sony_laptop_remove_input();
  2628. /* pf attrs */
  2629. sysfs_remove_group(&sony_pf_device->dev.kobj, &spic_attribute_group);
  2630. sony_pf_remove();
  2631. list_for_each_entry_safe(io, tmp_io, &spic_dev.ioports, list) {
  2632. list_del(&io->list);
  2633. kfree(io);
  2634. }
  2635. list_for_each_entry_safe(irq, tmp_irq, &spic_dev.interrupts, list) {
  2636. list_del(&irq->list);
  2637. kfree(irq);
  2638. }
  2639. spic_dev.cur_ioport = NULL;
  2640. spic_dev.cur_irq = NULL;
  2641. dprintk(SONY_PIC_DRIVER_NAME " removed.\n");
  2642. return 0;
  2643. }
  2644. static int sony_pic_add(struct acpi_device *device)
  2645. {
  2646. int result;
  2647. struct sony_pic_ioport *io, *tmp_io;
  2648. struct sony_pic_irq *irq, *tmp_irq;
  2649. pr_info(DRV_PFX "%s v%s.\n", SONY_PIC_DRIVER_NAME,
  2650. SONY_LAPTOP_DRIVER_VERSION);
  2651. spic_dev.acpi_dev = device;
  2652. strcpy(acpi_device_class(device), "sony/hotkey");
  2653. sony_pic_detect_device_type(&spic_dev);
  2654. mutex_init(&spic_dev.lock);
  2655. /* read _PRS resources */
  2656. result = sony_pic_possible_resources(device);
  2657. if (result) {
  2658. pr_err(DRV_PFX "Unable to read possible resources.\n");
  2659. goto err_free_resources;
  2660. }
  2661. /* setup input devices and helper fifo */
  2662. result = sony_laptop_setup_input(device);
  2663. if (result) {
  2664. pr_err(DRV_PFX "Unable to create input devices.\n");
  2665. goto err_free_resources;
  2666. }
  2667. if (sonypi_compat_init())
  2668. goto err_remove_input;
  2669. /* request io port */
  2670. list_for_each_entry_reverse(io, &spic_dev.ioports, list) {
  2671. if (request_region(io->io1.minimum, io->io1.address_length,
  2672. "Sony Programmable I/O Device")) {
  2673. dprintk("I/O port1: 0x%.4x (0x%.4x) + 0x%.2x\n",
  2674. io->io1.minimum, io->io1.maximum,
  2675. io->io1.address_length);
  2676. /* Type 1 have 2 ioports */
  2677. if (io->io2.minimum) {
  2678. if (request_region(io->io2.minimum,
  2679. io->io2.address_length,
  2680. "Sony Programmable I/O Device")) {
  2681. dprintk("I/O port2: 0x%.4x (0x%.4x) + 0x%.2x\n",
  2682. io->io2.minimum, io->io2.maximum,
  2683. io->io2.address_length);
  2684. spic_dev.cur_ioport = io;
  2685. break;
  2686. }
  2687. else {
  2688. dprintk("Unable to get I/O port2: "
  2689. "0x%.4x (0x%.4x) + 0x%.2x\n",
  2690. io->io2.minimum, io->io2.maximum,
  2691. io->io2.address_length);
  2692. release_region(io->io1.minimum,
  2693. io->io1.address_length);
  2694. }
  2695. }
  2696. else {
  2697. spic_dev.cur_ioport = io;
  2698. break;
  2699. }
  2700. }
  2701. }
  2702. if (!spic_dev.cur_ioport) {
  2703. pr_err(DRV_PFX "Failed to request_region.\n");
  2704. result = -ENODEV;
  2705. goto err_remove_compat;
  2706. }
  2707. /* request IRQ */
  2708. list_for_each_entry_reverse(irq, &spic_dev.interrupts, list) {
  2709. if (!request_irq(irq->irq.interrupts[0], sony_pic_irq,
  2710. IRQF_DISABLED, "sony-laptop", &spic_dev)) {
  2711. dprintk("IRQ: %d - triggering: %d - "
  2712. "polarity: %d - shr: %d\n",
  2713. irq->irq.interrupts[0],
  2714. irq->irq.triggering,
  2715. irq->irq.polarity,
  2716. irq->irq.sharable);
  2717. spic_dev.cur_irq = irq;
  2718. break;
  2719. }
  2720. }
  2721. if (!spic_dev.cur_irq) {
  2722. pr_err(DRV_PFX "Failed to request_irq.\n");
  2723. result = -ENODEV;
  2724. goto err_release_region;
  2725. }
  2726. /* set resource status _SRS */
  2727. result = sony_pic_enable(device, spic_dev.cur_ioport, spic_dev.cur_irq);
  2728. if (result) {
  2729. pr_err(DRV_PFX "Couldn't enable device.\n");
  2730. goto err_free_irq;
  2731. }
  2732. spic_dev.bluetooth_power = -1;
  2733. /* create device attributes */
  2734. result = sony_pf_add();
  2735. if (result)
  2736. goto err_disable_device;
  2737. result = sysfs_create_group(&sony_pf_device->dev.kobj, &spic_attribute_group);
  2738. if (result)
  2739. goto err_remove_pf;
  2740. return 0;
  2741. err_remove_pf:
  2742. sony_pf_remove();
  2743. err_disable_device:
  2744. sony_pic_disable(device);
  2745. err_free_irq:
  2746. free_irq(spic_dev.cur_irq->irq.interrupts[0], &spic_dev);
  2747. err_release_region:
  2748. release_region(spic_dev.cur_ioport->io1.minimum,
  2749. spic_dev.cur_ioport->io1.address_length);
  2750. if (spic_dev.cur_ioport->io2.minimum)
  2751. release_region(spic_dev.cur_ioport->io2.minimum,
  2752. spic_dev.cur_ioport->io2.address_length);
  2753. err_remove_compat:
  2754. sonypi_compat_exit();
  2755. err_remove_input:
  2756. sony_laptop_remove_input();
  2757. err_free_resources:
  2758. list_for_each_entry_safe(io, tmp_io, &spic_dev.ioports, list) {
  2759. list_del(&io->list);
  2760. kfree(io);
  2761. }
  2762. list_for_each_entry_safe(irq, tmp_irq, &spic_dev.interrupts, list) {
  2763. list_del(&irq->list);
  2764. kfree(irq);
  2765. }
  2766. spic_dev.cur_ioport = NULL;
  2767. spic_dev.cur_irq = NULL;
  2768. return result;
  2769. }
  2770. static int sony_pic_suspend(struct acpi_device *device, pm_message_t state)
  2771. {
  2772. if (sony_pic_disable(device))
  2773. return -ENXIO;
  2774. return 0;
  2775. }
  2776. static int sony_pic_resume(struct acpi_device *device)
  2777. {
  2778. sony_pic_enable(device, spic_dev.cur_ioport, spic_dev.cur_irq);
  2779. return 0;
  2780. }
  2781. static const struct acpi_device_id sony_pic_device_ids[] = {
  2782. {SONY_PIC_HID, 0},
  2783. {"", 0},
  2784. };
  2785. static struct acpi_driver sony_pic_driver = {
  2786. .name = SONY_PIC_DRIVER_NAME,
  2787. .class = SONY_PIC_CLASS,
  2788. .ids = sony_pic_device_ids,
  2789. .owner = THIS_MODULE,
  2790. .ops = {
  2791. .add = sony_pic_add,
  2792. .remove = sony_pic_remove,
  2793. .suspend = sony_pic_suspend,
  2794. .resume = sony_pic_resume,
  2795. },
  2796. };
  2797. static struct dmi_system_id __initdata sonypi_dmi_table[] = {
  2798. {
  2799. .ident = "Sony Vaio",
  2800. .matches = {
  2801. DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
  2802. DMI_MATCH(DMI_PRODUCT_NAME, "PCG-"),
  2803. },
  2804. },
  2805. {
  2806. .ident = "Sony Vaio",
  2807. .matches = {
  2808. DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
  2809. DMI_MATCH(DMI_PRODUCT_NAME, "VGN-"),
  2810. },
  2811. },
  2812. { }
  2813. };
  2814. static int __init sony_laptop_init(void)
  2815. {
  2816. int result;
  2817. if (!no_spic && dmi_check_system(sonypi_dmi_table)) {
  2818. result = acpi_bus_register_driver(&sony_pic_driver);
  2819. if (result) {
  2820. pr_err(DRV_PFX "Unable to register SPIC driver.");
  2821. goto out;
  2822. }
  2823. spic_drv_registered = 1;
  2824. }
  2825. result = acpi_bus_register_driver(&sony_nc_driver);
  2826. if (result) {
  2827. pr_err(DRV_PFX "Unable to register SNC driver.");
  2828. goto out_unregister_pic;
  2829. }
  2830. return 0;
  2831. out_unregister_pic:
  2832. if (spic_drv_registered)
  2833. acpi_bus_unregister_driver(&sony_pic_driver);
  2834. out:
  2835. return result;
  2836. }
  2837. static void __exit sony_laptop_exit(void)
  2838. {
  2839. acpi_bus_unregister_driver(&sony_nc_driver);
  2840. if (spic_drv_registered)
  2841. acpi_bus_unregister_driver(&sony_pic_driver);
  2842. }
  2843. module_init(sony_laptop_init);
  2844. module_exit(sony_laptop_exit);