sony-laptop.c 87 KB

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