sony-laptop.c 101 KB

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