sony-laptop.c 84 KB

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