sony-laptop.c 90 KB

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