sony-laptop.c 87 KB

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