sony-laptop.c 87 KB

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