sony-laptop.c 87 KB

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