sony-laptop.c 84 KB

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