sony-laptop.c 83 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270
  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. 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 struct backlight_device *sony_backlight_device;
  799. static const struct backlight_ops sony_backlight_ops = {
  800. .update_status = sony_backlight_update_status,
  801. .get_brightness = sony_backlight_get_brightness,
  802. };
  803. /*
  804. * New SNC-only Vaios event mapping to driver known keys
  805. */
  806. struct sony_nc_event {
  807. u8 data;
  808. u8 event;
  809. };
  810. static struct sony_nc_event sony_100_events[] = {
  811. { 0x90, SONYPI_EVENT_PKEY_P1 },
  812. { 0x10, SONYPI_EVENT_ANYBUTTON_RELEASED },
  813. { 0x91, SONYPI_EVENT_PKEY_P2 },
  814. { 0x11, SONYPI_EVENT_ANYBUTTON_RELEASED },
  815. { 0x81, SONYPI_EVENT_FNKEY_F1 },
  816. { 0x01, SONYPI_EVENT_FNKEY_RELEASED },
  817. { 0x82, SONYPI_EVENT_FNKEY_F2 },
  818. { 0x02, SONYPI_EVENT_FNKEY_RELEASED },
  819. { 0x83, SONYPI_EVENT_FNKEY_F3 },
  820. { 0x03, SONYPI_EVENT_FNKEY_RELEASED },
  821. { 0x84, SONYPI_EVENT_FNKEY_F4 },
  822. { 0x04, SONYPI_EVENT_FNKEY_RELEASED },
  823. { 0x85, SONYPI_EVENT_FNKEY_F5 },
  824. { 0x05, SONYPI_EVENT_FNKEY_RELEASED },
  825. { 0x86, SONYPI_EVENT_FNKEY_F6 },
  826. { 0x06, SONYPI_EVENT_FNKEY_RELEASED },
  827. { 0x87, SONYPI_EVENT_FNKEY_F7 },
  828. { 0x07, SONYPI_EVENT_FNKEY_RELEASED },
  829. { 0x89, SONYPI_EVENT_FNKEY_F9 },
  830. { 0x09, SONYPI_EVENT_FNKEY_RELEASED },
  831. { 0x8A, SONYPI_EVENT_FNKEY_F10 },
  832. { 0x0A, SONYPI_EVENT_FNKEY_RELEASED },
  833. { 0x8C, SONYPI_EVENT_FNKEY_F12 },
  834. { 0x0C, SONYPI_EVENT_FNKEY_RELEASED },
  835. { 0x9d, SONYPI_EVENT_ZOOM_PRESSED },
  836. { 0x1d, SONYPI_EVENT_ANYBUTTON_RELEASED },
  837. { 0x9f, SONYPI_EVENT_CD_EJECT_PRESSED },
  838. { 0x1f, SONYPI_EVENT_ANYBUTTON_RELEASED },
  839. { 0xa1, SONYPI_EVENT_MEDIA_PRESSED },
  840. { 0x21, SONYPI_EVENT_ANYBUTTON_RELEASED },
  841. { 0xa4, SONYPI_EVENT_CD_EJECT_PRESSED },
  842. { 0x24, SONYPI_EVENT_ANYBUTTON_RELEASED },
  843. { 0xa5, SONYPI_EVENT_VENDOR_PRESSED },
  844. { 0x25, SONYPI_EVENT_ANYBUTTON_RELEASED },
  845. { 0xa6, SONYPI_EVENT_HELP_PRESSED },
  846. { 0x26, SONYPI_EVENT_ANYBUTTON_RELEASED },
  847. { 0, 0 },
  848. };
  849. static struct sony_nc_event sony_127_events[] = {
  850. { 0x81, SONYPI_EVENT_MODEKEY_PRESSED },
  851. { 0x01, SONYPI_EVENT_ANYBUTTON_RELEASED },
  852. { 0x82, SONYPI_EVENT_PKEY_P1 },
  853. { 0x02, SONYPI_EVENT_ANYBUTTON_RELEASED },
  854. { 0x83, SONYPI_EVENT_PKEY_P2 },
  855. { 0x03, SONYPI_EVENT_ANYBUTTON_RELEASED },
  856. { 0x84, SONYPI_EVENT_PKEY_P3 },
  857. { 0x04, SONYPI_EVENT_ANYBUTTON_RELEASED },
  858. { 0x85, SONYPI_EVENT_PKEY_P4 },
  859. { 0x05, SONYPI_EVENT_ANYBUTTON_RELEASED },
  860. { 0x86, SONYPI_EVENT_PKEY_P5 },
  861. { 0x06, SONYPI_EVENT_ANYBUTTON_RELEASED },
  862. { 0x87, SONYPI_EVENT_SETTINGKEY_PRESSED },
  863. { 0x07, SONYPI_EVENT_ANYBUTTON_RELEASED },
  864. { 0, 0 },
  865. };
  866. /*
  867. * ACPI callbacks
  868. */
  869. static void sony_nc_notify(struct acpi_device *device, u32 event)
  870. {
  871. u32 ev = event;
  872. if (ev >= 0x90) {
  873. /* New-style event */
  874. int result;
  875. int key_handle = 0;
  876. ev -= 0x90;
  877. if (sony_find_snc_handle(0x100) == ev)
  878. key_handle = 0x100;
  879. if (sony_find_snc_handle(0x127) == ev)
  880. key_handle = 0x127;
  881. if (key_handle) {
  882. struct sony_nc_event *key_event;
  883. if (sony_call_snc_handle(key_handle, 0x200, &result)) {
  884. dprintk("sony_nc_notify, unable to decode"
  885. " event 0x%.2x 0x%.2x\n", key_handle,
  886. ev);
  887. /* restore the original event */
  888. ev = event;
  889. } else {
  890. ev = result & 0xFF;
  891. if (key_handle == 0x100)
  892. key_event = sony_100_events;
  893. else
  894. key_event = sony_127_events;
  895. for (; key_event->data; key_event++) {
  896. if (key_event->data == ev) {
  897. ev = key_event->event;
  898. break;
  899. }
  900. }
  901. if (!key_event->data)
  902. pr_info(DRV_PFX
  903. "Unknown event: 0x%x 0x%x\n",
  904. key_handle,
  905. ev);
  906. else
  907. sony_laptop_report_input_event(ev);
  908. }
  909. } else if (sony_find_snc_handle(sony_rfkill_handle) == ev) {
  910. sony_nc_rfkill_update();
  911. return;
  912. }
  913. } else
  914. sony_laptop_report_input_event(ev);
  915. dprintk("sony_nc_notify, event: 0x%.2x\n", ev);
  916. acpi_bus_generate_proc_event(sony_nc_acpi_device, 1, ev);
  917. }
  918. static acpi_status sony_walk_callback(acpi_handle handle, u32 level,
  919. void *context, void **return_value)
  920. {
  921. struct acpi_device_info *info;
  922. if (ACPI_SUCCESS(acpi_get_object_info(handle, &info))) {
  923. pr_warn(DRV_PFX "method: name: %4.4s, args %X\n",
  924. (char *)&info->name, info->param_count);
  925. kfree(info);
  926. }
  927. return AE_OK;
  928. }
  929. /*
  930. * ACPI device
  931. */
  932. static int sony_nc_function_setup(struct acpi_device *device)
  933. {
  934. int result;
  935. /* Enable all events */
  936. acpi_callsetfunc(sony_nc_acpi_handle, "SN02", 0xffff, &result);
  937. /* Setup hotkeys */
  938. sony_call_snc_handle(0x0100, 0, &result);
  939. sony_call_snc_handle(0x0101, 0, &result);
  940. sony_call_snc_handle(0x0102, 0x100, &result);
  941. sony_call_snc_handle(0x0127, 0, &result);
  942. return 0;
  943. }
  944. static int sony_nc_resume(struct acpi_device *device)
  945. {
  946. struct sony_nc_value *item;
  947. acpi_handle handle;
  948. for (item = sony_nc_values; item->name; item++) {
  949. int ret;
  950. if (!item->valid)
  951. continue;
  952. ret = acpi_callsetfunc(sony_nc_acpi_handle, *item->acpiset,
  953. item->value, NULL);
  954. if (ret < 0) {
  955. pr_err(DRV_PFX "%s: %d\n", __func__, ret);
  956. break;
  957. }
  958. }
  959. if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle, "ECON",
  960. &handle))) {
  961. if (acpi_callsetfunc(sony_nc_acpi_handle, "ECON", 1, NULL))
  962. dprintk("ECON Method failed\n");
  963. }
  964. if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle, "SN00",
  965. &handle))) {
  966. dprintk("Doing SNC setup\n");
  967. sony_nc_function_setup(device);
  968. }
  969. /* set the last requested brightness level */
  970. if (sony_backlight_device &&
  971. sony_backlight_update_status(sony_backlight_device) < 0)
  972. pr_warn(DRV_PFX "unable to restore brightness level\n");
  973. /* re-read rfkill state */
  974. sony_nc_rfkill_update();
  975. return 0;
  976. }
  977. static void sony_nc_rfkill_cleanup(void)
  978. {
  979. int i;
  980. for (i = 0; i < N_SONY_RFKILL; i++) {
  981. if (sony_rfkill_devices[i]) {
  982. rfkill_unregister(sony_rfkill_devices[i]);
  983. rfkill_destroy(sony_rfkill_devices[i]);
  984. }
  985. }
  986. }
  987. static int sony_nc_rfkill_set(void *data, bool blocked)
  988. {
  989. int result;
  990. int argument = sony_rfkill_address[(long) data] + 0x100;
  991. if (!blocked)
  992. argument |= 0xff0000;
  993. return sony_call_snc_handle(sony_rfkill_handle, argument, &result);
  994. }
  995. static const struct rfkill_ops sony_rfkill_ops = {
  996. .set_block = sony_nc_rfkill_set,
  997. };
  998. static int sony_nc_setup_rfkill(struct acpi_device *device,
  999. enum sony_nc_rfkill nc_type)
  1000. {
  1001. int err = 0;
  1002. struct rfkill *rfk;
  1003. enum rfkill_type type;
  1004. const char *name;
  1005. int result;
  1006. bool hwblock;
  1007. switch (nc_type) {
  1008. case SONY_WIFI:
  1009. type = RFKILL_TYPE_WLAN;
  1010. name = "sony-wifi";
  1011. break;
  1012. case SONY_BLUETOOTH:
  1013. type = RFKILL_TYPE_BLUETOOTH;
  1014. name = "sony-bluetooth";
  1015. break;
  1016. case SONY_WWAN:
  1017. type = RFKILL_TYPE_WWAN;
  1018. name = "sony-wwan";
  1019. break;
  1020. case SONY_WIMAX:
  1021. type = RFKILL_TYPE_WIMAX;
  1022. name = "sony-wimax";
  1023. break;
  1024. default:
  1025. return -EINVAL;
  1026. }
  1027. rfk = rfkill_alloc(name, &device->dev, type,
  1028. &sony_rfkill_ops, (void *)nc_type);
  1029. if (!rfk)
  1030. return -ENOMEM;
  1031. sony_call_snc_handle(sony_rfkill_handle, 0x200, &result);
  1032. hwblock = !(result & 0x1);
  1033. rfkill_set_hw_state(rfk, hwblock);
  1034. err = rfkill_register(rfk);
  1035. if (err) {
  1036. rfkill_destroy(rfk);
  1037. return err;
  1038. }
  1039. sony_rfkill_devices[nc_type] = rfk;
  1040. return err;
  1041. }
  1042. static void sony_nc_rfkill_update(void)
  1043. {
  1044. enum sony_nc_rfkill i;
  1045. int result;
  1046. bool hwblock;
  1047. sony_call_snc_handle(sony_rfkill_handle, 0x200, &result);
  1048. hwblock = !(result & 0x1);
  1049. for (i = 0; i < N_SONY_RFKILL; i++) {
  1050. int argument = sony_rfkill_address[i];
  1051. if (!sony_rfkill_devices[i])
  1052. continue;
  1053. if (hwblock) {
  1054. if (rfkill_set_hw_state(sony_rfkill_devices[i], true)) {
  1055. /* we already know we're blocked */
  1056. }
  1057. continue;
  1058. }
  1059. sony_call_snc_handle(sony_rfkill_handle, argument, &result);
  1060. rfkill_set_states(sony_rfkill_devices[i],
  1061. !(result & 0xf), false);
  1062. }
  1063. }
  1064. static void sony_nc_rfkill_setup(struct acpi_device *device)
  1065. {
  1066. int offset;
  1067. u8 dev_code, i;
  1068. acpi_status status;
  1069. struct acpi_object_list params;
  1070. union acpi_object in_obj;
  1071. union acpi_object *device_enum;
  1072. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  1073. offset = sony_find_snc_handle(0x124);
  1074. if (offset == -1) {
  1075. offset = sony_find_snc_handle(0x135);
  1076. if (offset == -1)
  1077. return;
  1078. else
  1079. sony_rfkill_handle = 0x135;
  1080. } else
  1081. sony_rfkill_handle = 0x124;
  1082. dprintk("Found rkfill handle: 0x%.4x\n", sony_rfkill_handle);
  1083. /* need to read the whole buffer returned by the acpi call to SN06
  1084. * here otherwise we may miss some features
  1085. */
  1086. params.count = 1;
  1087. params.pointer = &in_obj;
  1088. in_obj.type = ACPI_TYPE_INTEGER;
  1089. in_obj.integer.value = offset;
  1090. status = acpi_evaluate_object(sony_nc_acpi_handle, "SN06", &params,
  1091. &buffer);
  1092. if (ACPI_FAILURE(status)) {
  1093. dprintk("Radio device enumeration failed\n");
  1094. return;
  1095. }
  1096. device_enum = (union acpi_object *) buffer.pointer;
  1097. if (!device_enum || device_enum->type != ACPI_TYPE_BUFFER) {
  1098. pr_err(DRV_PFX "Invalid SN06 return object 0x%.2x\n",
  1099. device_enum->type);
  1100. goto out_no_enum;
  1101. }
  1102. /* the buffer is filled with magic numbers describing the devices
  1103. * available, 0xff terminates the enumeration
  1104. */
  1105. for (i = 0; i < device_enum->buffer.length; i++) {
  1106. dev_code = *(device_enum->buffer.pointer + i);
  1107. if (dev_code == 0xff)
  1108. break;
  1109. dprintk("Radio devices, looking at 0x%.2x\n", dev_code);
  1110. if (dev_code == 0 && !sony_rfkill_devices[SONY_WIFI])
  1111. sony_nc_setup_rfkill(device, SONY_WIFI);
  1112. if (dev_code == 0x10 && !sony_rfkill_devices[SONY_BLUETOOTH])
  1113. sony_nc_setup_rfkill(device, SONY_BLUETOOTH);
  1114. if ((0xf0 & dev_code) == 0x20 &&
  1115. !sony_rfkill_devices[SONY_WWAN])
  1116. sony_nc_setup_rfkill(device, SONY_WWAN);
  1117. if (dev_code == 0x30 && !sony_rfkill_devices[SONY_WIMAX])
  1118. sony_nc_setup_rfkill(device, SONY_WIMAX);
  1119. }
  1120. out_no_enum:
  1121. kfree(buffer.pointer);
  1122. return;
  1123. }
  1124. /* Keyboard backlight feature */
  1125. #define KBDBL_HANDLER 0x137
  1126. #define KBDBL_PRESENT 0xB00
  1127. #define SET_MODE 0xC00
  1128. #define SET_TIMEOUT 0xE00
  1129. struct kbd_backlight {
  1130. int mode;
  1131. int timeout;
  1132. struct device_attribute mode_attr;
  1133. struct device_attribute timeout_attr;
  1134. };
  1135. struct kbd_backlight *kbdbl_handle;
  1136. static ssize_t __sony_nc_kbd_backlight_mode_set(u8 value)
  1137. {
  1138. int result;
  1139. if (value > 1)
  1140. return -EINVAL;
  1141. if (sony_call_snc_handle(KBDBL_HANDLER,
  1142. (value << 0x10) | SET_MODE, &result))
  1143. return -EIO;
  1144. kbdbl_handle->mode = value;
  1145. return 0;
  1146. }
  1147. static ssize_t sony_nc_kbd_backlight_mode_store(struct device *dev,
  1148. struct device_attribute *attr,
  1149. const char *buffer, size_t count)
  1150. {
  1151. int ret = 0;
  1152. unsigned long value;
  1153. if (count > 31)
  1154. return -EINVAL;
  1155. if (strict_strtoul(buffer, 10, &value))
  1156. return -EINVAL;
  1157. ret = __sony_nc_kbd_backlight_mode_set(value);
  1158. if (ret < 0)
  1159. return ret;
  1160. return count;
  1161. }
  1162. static ssize_t sony_nc_kbd_backlight_mode_show(struct device *dev,
  1163. struct device_attribute *attr, char *buffer)
  1164. {
  1165. ssize_t count = 0;
  1166. count = snprintf(buffer, PAGE_SIZE, "%d\n", kbdbl_handle->mode);
  1167. return count;
  1168. }
  1169. static int __sony_nc_kbd_backlight_timeout_set(u8 value)
  1170. {
  1171. int result;
  1172. if (value > 3)
  1173. return -EINVAL;
  1174. if (sony_call_snc_handle(KBDBL_HANDLER,
  1175. (value << 0x10) | SET_TIMEOUT, &result))
  1176. return -EIO;
  1177. kbdbl_handle->timeout = value;
  1178. return 0;
  1179. }
  1180. static ssize_t sony_nc_kbd_backlight_timeout_store(struct device *dev,
  1181. struct device_attribute *attr,
  1182. const char *buffer, size_t count)
  1183. {
  1184. int ret = 0;
  1185. unsigned long value;
  1186. if (count > 31)
  1187. return -EINVAL;
  1188. if (strict_strtoul(buffer, 10, &value))
  1189. return -EINVAL;
  1190. ret = __sony_nc_kbd_backlight_timeout_set(value);
  1191. if (ret < 0)
  1192. return ret;
  1193. return count;
  1194. }
  1195. static ssize_t sony_nc_kbd_backlight_timeout_show(struct device *dev,
  1196. struct device_attribute *attr, char *buffer)
  1197. {
  1198. ssize_t count = 0;
  1199. count = snprintf(buffer, PAGE_SIZE, "%d\n", kbdbl_handle->timeout);
  1200. return count;
  1201. }
  1202. static int sony_nc_kbd_backlight_setup(struct platform_device *pd)
  1203. {
  1204. int result;
  1205. if (sony_call_snc_handle(0x137, KBDBL_PRESENT, &result))
  1206. return 0;
  1207. if (!(result & 0x02))
  1208. return 0;
  1209. kbdbl_handle = kzalloc(sizeof(*kbdbl_handle), GFP_KERNEL);
  1210. sysfs_attr_init(&kbdbl_handle->mode_attr.attr);
  1211. kbdbl_handle->mode_attr.attr.name = "kbd_backlight";
  1212. kbdbl_handle->mode_attr.attr.mode = S_IRUGO | S_IWUSR;
  1213. kbdbl_handle->mode_attr.show = sony_nc_kbd_backlight_mode_show;
  1214. kbdbl_handle->mode_attr.store = sony_nc_kbd_backlight_mode_store;
  1215. sysfs_attr_init(&kbdbl_handle->timeout_attr.attr);
  1216. kbdbl_handle->timeout_attr.attr.name = "kbd_backlight_timeout";
  1217. kbdbl_handle->timeout_attr.attr.mode = S_IRUGO | S_IWUSR;
  1218. kbdbl_handle->timeout_attr.show = sony_nc_kbd_backlight_timeout_show;
  1219. kbdbl_handle->timeout_attr.store = sony_nc_kbd_backlight_timeout_store;
  1220. if (device_create_file(&pd->dev, &kbdbl_handle->mode_attr))
  1221. goto outkzalloc;
  1222. if (device_create_file(&pd->dev, &kbdbl_handle->timeout_attr))
  1223. goto outmode;
  1224. __sony_nc_kbd_backlight_mode_set(kbd_backlight);
  1225. __sony_nc_kbd_backlight_timeout_set(kbd_backlight_timeout);
  1226. return 0;
  1227. outmode:
  1228. device_remove_file(&pd->dev, &kbdbl_handle->mode_attr);
  1229. outkzalloc:
  1230. kfree(kbdbl_handle);
  1231. kbdbl_handle = NULL;
  1232. return -1;
  1233. }
  1234. static int sony_nc_kbd_backlight_cleanup(struct platform_device *pd)
  1235. {
  1236. if (kbdbl_handle) {
  1237. device_remove_file(&pd->dev, &kbdbl_handle->mode_attr);
  1238. device_remove_file(&pd->dev, &kbdbl_handle->timeout_attr);
  1239. kfree(kbdbl_handle);
  1240. }
  1241. return 0;
  1242. }
  1243. static int sony_nc_add(struct acpi_device *device)
  1244. {
  1245. acpi_status status;
  1246. int result = 0;
  1247. acpi_handle handle;
  1248. struct sony_nc_value *item;
  1249. pr_info(DRV_PFX "%s v%s.\n", SONY_NC_DRIVER_NAME,
  1250. SONY_LAPTOP_DRIVER_VERSION);
  1251. sony_nc_acpi_device = device;
  1252. strcpy(acpi_device_class(device), "sony/hotkey");
  1253. sony_nc_acpi_handle = device->handle;
  1254. /* read device status */
  1255. result = acpi_bus_get_status(device);
  1256. /* bail IFF the above call was successful and the device is not present */
  1257. if (!result && !device->status.present) {
  1258. dprintk("Device not present\n");
  1259. result = -ENODEV;
  1260. goto outwalk;
  1261. }
  1262. result = sony_pf_add();
  1263. if (result)
  1264. goto outpresent;
  1265. if (debug) {
  1266. status = acpi_walk_namespace(ACPI_TYPE_METHOD,
  1267. sony_nc_acpi_handle, 1, sony_walk_callback,
  1268. NULL, NULL, NULL);
  1269. if (ACPI_FAILURE(status)) {
  1270. pr_warn(DRV_PFX "unable to walk acpi resources\n");
  1271. result = -ENODEV;
  1272. goto outpresent;
  1273. }
  1274. }
  1275. if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle, "ECON",
  1276. &handle))) {
  1277. if (acpi_callsetfunc(sony_nc_acpi_handle, "ECON", 1, NULL))
  1278. dprintk("ECON Method failed\n");
  1279. }
  1280. if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle, "SN00",
  1281. &handle))) {
  1282. dprintk("Doing SNC setup\n");
  1283. if (sony_nc_handles_setup(sony_pf_device))
  1284. goto outpresent;
  1285. if (sony_nc_kbd_backlight_setup(sony_pf_device))
  1286. goto outsnc;
  1287. sony_nc_function_setup(device);
  1288. sony_nc_rfkill_setup(device);
  1289. }
  1290. /* setup input devices and helper fifo */
  1291. result = sony_laptop_setup_input(device);
  1292. if (result) {
  1293. pr_err(DRV_PFX "Unable to create input devices.\n");
  1294. goto outkbdbacklight;
  1295. }
  1296. if (acpi_video_backlight_support()) {
  1297. pr_info(DRV_PFX "brightness ignored, must be "
  1298. "controlled by ACPI video driver\n");
  1299. } else if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle, "GBRT",
  1300. &handle))) {
  1301. struct backlight_properties props;
  1302. memset(&props, 0, sizeof(struct backlight_properties));
  1303. props.type = BACKLIGHT_PLATFORM;
  1304. props.max_brightness = SONY_MAX_BRIGHTNESS - 1;
  1305. sony_backlight_device = backlight_device_register("sony", NULL,
  1306. NULL,
  1307. &sony_backlight_ops,
  1308. &props);
  1309. if (IS_ERR(sony_backlight_device)) {
  1310. pr_warning(DRV_PFX "unable to register backlight device\n");
  1311. sony_backlight_device = NULL;
  1312. } else {
  1313. sony_backlight_device->props.brightness =
  1314. sony_backlight_get_brightness
  1315. (sony_backlight_device);
  1316. }
  1317. }
  1318. /* create sony_pf sysfs attributes related to the SNC device */
  1319. for (item = sony_nc_values; item->name; ++item) {
  1320. if (!debug && item->debug)
  1321. continue;
  1322. /* find the available acpiget as described in the DSDT */
  1323. for (; item->acpiget && *item->acpiget; ++item->acpiget) {
  1324. if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle,
  1325. *item->acpiget,
  1326. &handle))) {
  1327. dprintk("Found %s getter: %s\n",
  1328. item->name, *item->acpiget);
  1329. item->devattr.attr.mode |= S_IRUGO;
  1330. break;
  1331. }
  1332. }
  1333. /* find the available acpiset as described in the DSDT */
  1334. for (; item->acpiset && *item->acpiset; ++item->acpiset) {
  1335. if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle,
  1336. *item->acpiset,
  1337. &handle))) {
  1338. dprintk("Found %s setter: %s\n",
  1339. item->name, *item->acpiset);
  1340. item->devattr.attr.mode |= S_IWUSR;
  1341. break;
  1342. }
  1343. }
  1344. if (item->devattr.attr.mode != 0) {
  1345. result =
  1346. device_create_file(&sony_pf_device->dev,
  1347. &item->devattr);
  1348. if (result)
  1349. goto out_sysfs;
  1350. }
  1351. }
  1352. return 0;
  1353. out_sysfs:
  1354. for (item = sony_nc_values; item->name; ++item) {
  1355. device_remove_file(&sony_pf_device->dev, &item->devattr);
  1356. }
  1357. if (sony_backlight_device)
  1358. backlight_device_unregister(sony_backlight_device);
  1359. sony_laptop_remove_input();
  1360. outkbdbacklight:
  1361. sony_nc_kbd_backlight_cleanup(sony_pf_device);
  1362. outsnc:
  1363. sony_nc_handles_cleanup(sony_pf_device);
  1364. outpresent:
  1365. sony_pf_remove();
  1366. outwalk:
  1367. sony_nc_rfkill_cleanup();
  1368. return result;
  1369. }
  1370. static int sony_nc_remove(struct acpi_device *device, int type)
  1371. {
  1372. struct sony_nc_value *item;
  1373. if (sony_backlight_device)
  1374. backlight_device_unregister(sony_backlight_device);
  1375. sony_nc_acpi_device = NULL;
  1376. for (item = sony_nc_values; item->name; ++item) {
  1377. device_remove_file(&sony_pf_device->dev, &item->devattr);
  1378. }
  1379. sony_nc_kbd_backlight_cleanup(sony_pf_device);
  1380. sony_nc_handles_cleanup(sony_pf_device);
  1381. sony_pf_remove();
  1382. sony_laptop_remove_input();
  1383. sony_nc_rfkill_cleanup();
  1384. dprintk(SONY_NC_DRIVER_NAME " removed.\n");
  1385. return 0;
  1386. }
  1387. static const struct acpi_device_id sony_device_ids[] = {
  1388. {SONY_NC_HID, 0},
  1389. {SONY_PIC_HID, 0},
  1390. {"", 0},
  1391. };
  1392. MODULE_DEVICE_TABLE(acpi, sony_device_ids);
  1393. static const struct acpi_device_id sony_nc_device_ids[] = {
  1394. {SONY_NC_HID, 0},
  1395. {"", 0},
  1396. };
  1397. static struct acpi_driver sony_nc_driver = {
  1398. .name = SONY_NC_DRIVER_NAME,
  1399. .class = SONY_NC_CLASS,
  1400. .ids = sony_nc_device_ids,
  1401. .owner = THIS_MODULE,
  1402. .ops = {
  1403. .add = sony_nc_add,
  1404. .remove = sony_nc_remove,
  1405. .resume = sony_nc_resume,
  1406. .notify = sony_nc_notify,
  1407. },
  1408. };
  1409. /*********** SPIC (SNY6001) Device ***********/
  1410. #define SONYPI_DEVICE_TYPE1 0x00000001
  1411. #define SONYPI_DEVICE_TYPE2 0x00000002
  1412. #define SONYPI_DEVICE_TYPE3 0x00000004
  1413. #define SONYPI_TYPE1_OFFSET 0x04
  1414. #define SONYPI_TYPE2_OFFSET 0x12
  1415. #define SONYPI_TYPE3_OFFSET 0x12
  1416. struct sony_pic_ioport {
  1417. struct acpi_resource_io io1;
  1418. struct acpi_resource_io io2;
  1419. struct list_head list;
  1420. };
  1421. struct sony_pic_irq {
  1422. struct acpi_resource_irq irq;
  1423. struct list_head list;
  1424. };
  1425. struct sonypi_eventtypes {
  1426. u8 data;
  1427. unsigned long mask;
  1428. struct sonypi_event *events;
  1429. };
  1430. struct sony_pic_dev {
  1431. struct acpi_device *acpi_dev;
  1432. struct sony_pic_irq *cur_irq;
  1433. struct sony_pic_ioport *cur_ioport;
  1434. struct list_head interrupts;
  1435. struct list_head ioports;
  1436. struct mutex lock;
  1437. struct sonypi_eventtypes *event_types;
  1438. int (*handle_irq)(const u8, const u8);
  1439. int model;
  1440. u16 evport_offset;
  1441. u8 camera_power;
  1442. u8 bluetooth_power;
  1443. u8 wwan_power;
  1444. };
  1445. static struct sony_pic_dev spic_dev = {
  1446. .interrupts = LIST_HEAD_INIT(spic_dev.interrupts),
  1447. .ioports = LIST_HEAD_INIT(spic_dev.ioports),
  1448. };
  1449. static int spic_drv_registered;
  1450. /* Event masks */
  1451. #define SONYPI_JOGGER_MASK 0x00000001
  1452. #define SONYPI_CAPTURE_MASK 0x00000002
  1453. #define SONYPI_FNKEY_MASK 0x00000004
  1454. #define SONYPI_BLUETOOTH_MASK 0x00000008
  1455. #define SONYPI_PKEY_MASK 0x00000010
  1456. #define SONYPI_BACK_MASK 0x00000020
  1457. #define SONYPI_HELP_MASK 0x00000040
  1458. #define SONYPI_LID_MASK 0x00000080
  1459. #define SONYPI_ZOOM_MASK 0x00000100
  1460. #define SONYPI_THUMBPHRASE_MASK 0x00000200
  1461. #define SONYPI_MEYE_MASK 0x00000400
  1462. #define SONYPI_MEMORYSTICK_MASK 0x00000800
  1463. #define SONYPI_BATTERY_MASK 0x00001000
  1464. #define SONYPI_WIRELESS_MASK 0x00002000
  1465. struct sonypi_event {
  1466. u8 data;
  1467. u8 event;
  1468. };
  1469. /* The set of possible button release events */
  1470. static struct sonypi_event sonypi_releaseev[] = {
  1471. { 0x00, SONYPI_EVENT_ANYBUTTON_RELEASED },
  1472. { 0, 0 }
  1473. };
  1474. /* The set of possible jogger events */
  1475. static struct sonypi_event sonypi_joggerev[] = {
  1476. { 0x1f, SONYPI_EVENT_JOGDIAL_UP },
  1477. { 0x01, SONYPI_EVENT_JOGDIAL_DOWN },
  1478. { 0x5f, SONYPI_EVENT_JOGDIAL_UP_PRESSED },
  1479. { 0x41, SONYPI_EVENT_JOGDIAL_DOWN_PRESSED },
  1480. { 0x1e, SONYPI_EVENT_JOGDIAL_FAST_UP },
  1481. { 0x02, SONYPI_EVENT_JOGDIAL_FAST_DOWN },
  1482. { 0x5e, SONYPI_EVENT_JOGDIAL_FAST_UP_PRESSED },
  1483. { 0x42, SONYPI_EVENT_JOGDIAL_FAST_DOWN_PRESSED },
  1484. { 0x1d, SONYPI_EVENT_JOGDIAL_VFAST_UP },
  1485. { 0x03, SONYPI_EVENT_JOGDIAL_VFAST_DOWN },
  1486. { 0x5d, SONYPI_EVENT_JOGDIAL_VFAST_UP_PRESSED },
  1487. { 0x43, SONYPI_EVENT_JOGDIAL_VFAST_DOWN_PRESSED },
  1488. { 0x40, SONYPI_EVENT_JOGDIAL_PRESSED },
  1489. { 0, 0 }
  1490. };
  1491. /* The set of possible capture button events */
  1492. static struct sonypi_event sonypi_captureev[] = {
  1493. { 0x05, SONYPI_EVENT_CAPTURE_PARTIALPRESSED },
  1494. { 0x07, SONYPI_EVENT_CAPTURE_PRESSED },
  1495. { 0x40, SONYPI_EVENT_CAPTURE_PRESSED },
  1496. { 0x01, SONYPI_EVENT_CAPTURE_PARTIALRELEASED },
  1497. { 0, 0 }
  1498. };
  1499. /* The set of possible fnkeys events */
  1500. static struct sonypi_event sonypi_fnkeyev[] = {
  1501. { 0x10, SONYPI_EVENT_FNKEY_ESC },
  1502. { 0x11, SONYPI_EVENT_FNKEY_F1 },
  1503. { 0x12, SONYPI_EVENT_FNKEY_F2 },
  1504. { 0x13, SONYPI_EVENT_FNKEY_F3 },
  1505. { 0x14, SONYPI_EVENT_FNKEY_F4 },
  1506. { 0x15, SONYPI_EVENT_FNKEY_F5 },
  1507. { 0x16, SONYPI_EVENT_FNKEY_F6 },
  1508. { 0x17, SONYPI_EVENT_FNKEY_F7 },
  1509. { 0x18, SONYPI_EVENT_FNKEY_F8 },
  1510. { 0x19, SONYPI_EVENT_FNKEY_F9 },
  1511. { 0x1a, SONYPI_EVENT_FNKEY_F10 },
  1512. { 0x1b, SONYPI_EVENT_FNKEY_F11 },
  1513. { 0x1c, SONYPI_EVENT_FNKEY_F12 },
  1514. { 0x1f, SONYPI_EVENT_FNKEY_RELEASED },
  1515. { 0x21, SONYPI_EVENT_FNKEY_1 },
  1516. { 0x22, SONYPI_EVENT_FNKEY_2 },
  1517. { 0x31, SONYPI_EVENT_FNKEY_D },
  1518. { 0x32, SONYPI_EVENT_FNKEY_E },
  1519. { 0x33, SONYPI_EVENT_FNKEY_F },
  1520. { 0x34, SONYPI_EVENT_FNKEY_S },
  1521. { 0x35, SONYPI_EVENT_FNKEY_B },
  1522. { 0x36, SONYPI_EVENT_FNKEY_ONLY },
  1523. { 0, 0 }
  1524. };
  1525. /* The set of possible program key events */
  1526. static struct sonypi_event sonypi_pkeyev[] = {
  1527. { 0x01, SONYPI_EVENT_PKEY_P1 },
  1528. { 0x02, SONYPI_EVENT_PKEY_P2 },
  1529. { 0x04, SONYPI_EVENT_PKEY_P3 },
  1530. { 0x20, SONYPI_EVENT_PKEY_P1 },
  1531. { 0, 0 }
  1532. };
  1533. /* The set of possible bluetooth events */
  1534. static struct sonypi_event sonypi_blueev[] = {
  1535. { 0x55, SONYPI_EVENT_BLUETOOTH_PRESSED },
  1536. { 0x59, SONYPI_EVENT_BLUETOOTH_ON },
  1537. { 0x5a, SONYPI_EVENT_BLUETOOTH_OFF },
  1538. { 0, 0 }
  1539. };
  1540. /* The set of possible wireless events */
  1541. static struct sonypi_event sonypi_wlessev[] = {
  1542. { 0x59, SONYPI_EVENT_IGNORE },
  1543. { 0x5a, SONYPI_EVENT_IGNORE },
  1544. { 0, 0 }
  1545. };
  1546. /* The set of possible back button events */
  1547. static struct sonypi_event sonypi_backev[] = {
  1548. { 0x20, SONYPI_EVENT_BACK_PRESSED },
  1549. { 0, 0 }
  1550. };
  1551. /* The set of possible help button events */
  1552. static struct sonypi_event sonypi_helpev[] = {
  1553. { 0x3b, SONYPI_EVENT_HELP_PRESSED },
  1554. { 0, 0 }
  1555. };
  1556. /* The set of possible lid events */
  1557. static struct sonypi_event sonypi_lidev[] = {
  1558. { 0x51, SONYPI_EVENT_LID_CLOSED },
  1559. { 0x50, SONYPI_EVENT_LID_OPENED },
  1560. { 0, 0 }
  1561. };
  1562. /* The set of possible zoom events */
  1563. static struct sonypi_event sonypi_zoomev[] = {
  1564. { 0x39, SONYPI_EVENT_ZOOM_PRESSED },
  1565. { 0x10, SONYPI_EVENT_ZOOM_IN_PRESSED },
  1566. { 0x20, SONYPI_EVENT_ZOOM_OUT_PRESSED },
  1567. { 0x04, SONYPI_EVENT_ZOOM_PRESSED },
  1568. { 0, 0 }
  1569. };
  1570. /* The set of possible thumbphrase events */
  1571. static struct sonypi_event sonypi_thumbphraseev[] = {
  1572. { 0x3a, SONYPI_EVENT_THUMBPHRASE_PRESSED },
  1573. { 0, 0 }
  1574. };
  1575. /* The set of possible motioneye camera events */
  1576. static struct sonypi_event sonypi_meyeev[] = {
  1577. { 0x00, SONYPI_EVENT_MEYE_FACE },
  1578. { 0x01, SONYPI_EVENT_MEYE_OPPOSITE },
  1579. { 0, 0 }
  1580. };
  1581. /* The set of possible memorystick events */
  1582. static struct sonypi_event sonypi_memorystickev[] = {
  1583. { 0x53, SONYPI_EVENT_MEMORYSTICK_INSERT },
  1584. { 0x54, SONYPI_EVENT_MEMORYSTICK_EJECT },
  1585. { 0, 0 }
  1586. };
  1587. /* The set of possible battery events */
  1588. static struct sonypi_event sonypi_batteryev[] = {
  1589. { 0x20, SONYPI_EVENT_BATTERY_INSERT },
  1590. { 0x30, SONYPI_EVENT_BATTERY_REMOVE },
  1591. { 0, 0 }
  1592. };
  1593. /* The set of possible volume events */
  1594. static struct sonypi_event sonypi_volumeev[] = {
  1595. { 0x01, SONYPI_EVENT_VOLUME_INC_PRESSED },
  1596. { 0x02, SONYPI_EVENT_VOLUME_DEC_PRESSED },
  1597. { 0, 0 }
  1598. };
  1599. /* The set of possible brightness events */
  1600. static struct sonypi_event sonypi_brightnessev[] = {
  1601. { 0x80, SONYPI_EVENT_BRIGHTNESS_PRESSED },
  1602. { 0, 0 }
  1603. };
  1604. static struct sonypi_eventtypes type1_events[] = {
  1605. { 0, 0xffffffff, sonypi_releaseev },
  1606. { 0x70, SONYPI_MEYE_MASK, sonypi_meyeev },
  1607. { 0x30, SONYPI_LID_MASK, sonypi_lidev },
  1608. { 0x60, SONYPI_CAPTURE_MASK, sonypi_captureev },
  1609. { 0x10, SONYPI_JOGGER_MASK, sonypi_joggerev },
  1610. { 0x20, SONYPI_FNKEY_MASK, sonypi_fnkeyev },
  1611. { 0x30, SONYPI_BLUETOOTH_MASK, sonypi_blueev },
  1612. { 0x40, SONYPI_PKEY_MASK, sonypi_pkeyev },
  1613. { 0x30, SONYPI_MEMORYSTICK_MASK, sonypi_memorystickev },
  1614. { 0x40, SONYPI_BATTERY_MASK, sonypi_batteryev },
  1615. { 0 },
  1616. };
  1617. static struct sonypi_eventtypes type2_events[] = {
  1618. { 0, 0xffffffff, sonypi_releaseev },
  1619. { 0x38, SONYPI_LID_MASK, sonypi_lidev },
  1620. { 0x11, SONYPI_JOGGER_MASK, sonypi_joggerev },
  1621. { 0x61, SONYPI_CAPTURE_MASK, sonypi_captureev },
  1622. { 0x21, SONYPI_FNKEY_MASK, sonypi_fnkeyev },
  1623. { 0x31, SONYPI_BLUETOOTH_MASK, sonypi_blueev },
  1624. { 0x08, SONYPI_PKEY_MASK, sonypi_pkeyev },
  1625. { 0x11, SONYPI_BACK_MASK, sonypi_backev },
  1626. { 0x21, SONYPI_HELP_MASK, sonypi_helpev },
  1627. { 0x21, SONYPI_ZOOM_MASK, sonypi_zoomev },
  1628. { 0x20, SONYPI_THUMBPHRASE_MASK, sonypi_thumbphraseev },
  1629. { 0x31, SONYPI_MEMORYSTICK_MASK, sonypi_memorystickev },
  1630. { 0x41, SONYPI_BATTERY_MASK, sonypi_batteryev },
  1631. { 0x31, SONYPI_PKEY_MASK, sonypi_pkeyev },
  1632. { 0 },
  1633. };
  1634. static struct sonypi_eventtypes type3_events[] = {
  1635. { 0, 0xffffffff, sonypi_releaseev },
  1636. { 0x21, SONYPI_FNKEY_MASK, sonypi_fnkeyev },
  1637. { 0x31, SONYPI_WIRELESS_MASK, sonypi_wlessev },
  1638. { 0x31, SONYPI_MEMORYSTICK_MASK, sonypi_memorystickev },
  1639. { 0x41, SONYPI_BATTERY_MASK, sonypi_batteryev },
  1640. { 0x31, SONYPI_PKEY_MASK, sonypi_pkeyev },
  1641. { 0x05, SONYPI_PKEY_MASK, sonypi_pkeyev },
  1642. { 0x05, SONYPI_ZOOM_MASK, sonypi_zoomev },
  1643. { 0x05, SONYPI_CAPTURE_MASK, sonypi_captureev },
  1644. { 0x05, SONYPI_PKEY_MASK, sonypi_volumeev },
  1645. { 0x05, SONYPI_PKEY_MASK, sonypi_brightnessev },
  1646. { 0 },
  1647. };
  1648. /* low level spic calls */
  1649. #define ITERATIONS_LONG 10000
  1650. #define ITERATIONS_SHORT 10
  1651. #define wait_on_command(command, iterations) { \
  1652. unsigned int n = iterations; \
  1653. while (--n && (command)) \
  1654. udelay(1); \
  1655. if (!n) \
  1656. dprintk("command failed at %s : %s (line %d)\n", \
  1657. __FILE__, __func__, __LINE__); \
  1658. }
  1659. static u8 sony_pic_call1(u8 dev)
  1660. {
  1661. u8 v1, v2;
  1662. wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2,
  1663. ITERATIONS_LONG);
  1664. outb(dev, spic_dev.cur_ioport->io1.minimum + 4);
  1665. v1 = inb_p(spic_dev.cur_ioport->io1.minimum + 4);
  1666. v2 = inb_p(spic_dev.cur_ioport->io1.minimum);
  1667. dprintk("sony_pic_call1(0x%.2x): 0x%.4x\n", dev, (v2 << 8) | v1);
  1668. return v2;
  1669. }
  1670. static u8 sony_pic_call2(u8 dev, u8 fn)
  1671. {
  1672. u8 v1;
  1673. wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2,
  1674. ITERATIONS_LONG);
  1675. outb(dev, spic_dev.cur_ioport->io1.minimum + 4);
  1676. wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2,
  1677. ITERATIONS_LONG);
  1678. outb(fn, spic_dev.cur_ioport->io1.minimum);
  1679. v1 = inb_p(spic_dev.cur_ioport->io1.minimum);
  1680. dprintk("sony_pic_call2(0x%.2x - 0x%.2x): 0x%.4x\n", dev, fn, v1);
  1681. return v1;
  1682. }
  1683. static u8 sony_pic_call3(u8 dev, u8 fn, u8 v)
  1684. {
  1685. u8 v1;
  1686. wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2, ITERATIONS_LONG);
  1687. outb(dev, spic_dev.cur_ioport->io1.minimum + 4);
  1688. wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2, ITERATIONS_LONG);
  1689. outb(fn, spic_dev.cur_ioport->io1.minimum);
  1690. wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2, ITERATIONS_LONG);
  1691. outb(v, spic_dev.cur_ioport->io1.minimum);
  1692. v1 = inb_p(spic_dev.cur_ioport->io1.minimum);
  1693. dprintk("sony_pic_call3(0x%.2x - 0x%.2x - 0x%.2x): 0x%.4x\n",
  1694. dev, fn, v, v1);
  1695. return v1;
  1696. }
  1697. /*
  1698. * minidrivers for SPIC models
  1699. */
  1700. static int type3_handle_irq(const u8 data_mask, const u8 ev)
  1701. {
  1702. /*
  1703. * 0x31 could mean we have to take some extra action and wait for
  1704. * the next irq for some Type3 models, it will generate a new
  1705. * irq and we can read new data from the device:
  1706. * - 0x5c and 0x5f requires 0xA0
  1707. * - 0x61 requires 0xB3
  1708. */
  1709. if (data_mask == 0x31) {
  1710. if (ev == 0x5c || ev == 0x5f)
  1711. sony_pic_call1(0xA0);
  1712. else if (ev == 0x61)
  1713. sony_pic_call1(0xB3);
  1714. return 0;
  1715. }
  1716. return 1;
  1717. }
  1718. static void sony_pic_detect_device_type(struct sony_pic_dev *dev)
  1719. {
  1720. struct pci_dev *pcidev;
  1721. pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
  1722. PCI_DEVICE_ID_INTEL_82371AB_3, NULL);
  1723. if (pcidev) {
  1724. dev->model = SONYPI_DEVICE_TYPE1;
  1725. dev->evport_offset = SONYPI_TYPE1_OFFSET;
  1726. dev->event_types = type1_events;
  1727. goto out;
  1728. }
  1729. pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
  1730. PCI_DEVICE_ID_INTEL_ICH6_1, NULL);
  1731. if (pcidev) {
  1732. dev->model = SONYPI_DEVICE_TYPE2;
  1733. dev->evport_offset = SONYPI_TYPE2_OFFSET;
  1734. dev->event_types = type2_events;
  1735. goto out;
  1736. }
  1737. pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
  1738. PCI_DEVICE_ID_INTEL_ICH7_1, NULL);
  1739. if (pcidev) {
  1740. dev->model = SONYPI_DEVICE_TYPE3;
  1741. dev->handle_irq = type3_handle_irq;
  1742. dev->evport_offset = SONYPI_TYPE3_OFFSET;
  1743. dev->event_types = type3_events;
  1744. goto out;
  1745. }
  1746. pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
  1747. PCI_DEVICE_ID_INTEL_ICH8_4, NULL);
  1748. if (pcidev) {
  1749. dev->model = SONYPI_DEVICE_TYPE3;
  1750. dev->handle_irq = type3_handle_irq;
  1751. dev->evport_offset = SONYPI_TYPE3_OFFSET;
  1752. dev->event_types = type3_events;
  1753. goto out;
  1754. }
  1755. pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
  1756. PCI_DEVICE_ID_INTEL_ICH9_1, NULL);
  1757. if (pcidev) {
  1758. dev->model = SONYPI_DEVICE_TYPE3;
  1759. dev->handle_irq = type3_handle_irq;
  1760. dev->evport_offset = SONYPI_TYPE3_OFFSET;
  1761. dev->event_types = type3_events;
  1762. goto out;
  1763. }
  1764. /* default */
  1765. dev->model = SONYPI_DEVICE_TYPE2;
  1766. dev->evport_offset = SONYPI_TYPE2_OFFSET;
  1767. dev->event_types = type2_events;
  1768. out:
  1769. if (pcidev)
  1770. pci_dev_put(pcidev);
  1771. pr_info(DRV_PFX "detected Type%d model\n",
  1772. dev->model == SONYPI_DEVICE_TYPE1 ? 1 :
  1773. dev->model == SONYPI_DEVICE_TYPE2 ? 2 : 3);
  1774. }
  1775. /* camera tests and poweron/poweroff */
  1776. #define SONYPI_CAMERA_PICTURE 5
  1777. #define SONYPI_CAMERA_CONTROL 0x10
  1778. #define SONYPI_CAMERA_BRIGHTNESS 0
  1779. #define SONYPI_CAMERA_CONTRAST 1
  1780. #define SONYPI_CAMERA_HUE 2
  1781. #define SONYPI_CAMERA_COLOR 3
  1782. #define SONYPI_CAMERA_SHARPNESS 4
  1783. #define SONYPI_CAMERA_EXPOSURE_MASK 0xC
  1784. #define SONYPI_CAMERA_WHITE_BALANCE_MASK 0x3
  1785. #define SONYPI_CAMERA_PICTURE_MODE_MASK 0x30
  1786. #define SONYPI_CAMERA_MUTE_MASK 0x40
  1787. /* the rest don't need a loop until not 0xff */
  1788. #define SONYPI_CAMERA_AGC 6
  1789. #define SONYPI_CAMERA_AGC_MASK 0x30
  1790. #define SONYPI_CAMERA_SHUTTER_MASK 0x7
  1791. #define SONYPI_CAMERA_SHUTDOWN_REQUEST 7
  1792. #define SONYPI_CAMERA_CONTROL 0x10
  1793. #define SONYPI_CAMERA_STATUS 7
  1794. #define SONYPI_CAMERA_STATUS_READY 0x2
  1795. #define SONYPI_CAMERA_STATUS_POSITION 0x4
  1796. #define SONYPI_DIRECTION_BACKWARDS 0x4
  1797. #define SONYPI_CAMERA_REVISION 8
  1798. #define SONYPI_CAMERA_ROMVERSION 9
  1799. static int __sony_pic_camera_ready(void)
  1800. {
  1801. u8 v;
  1802. v = sony_pic_call2(0x8f, SONYPI_CAMERA_STATUS);
  1803. return (v != 0xff && (v & SONYPI_CAMERA_STATUS_READY));
  1804. }
  1805. static int __sony_pic_camera_off(void)
  1806. {
  1807. if (!camera) {
  1808. pr_warn(DRV_PFX "camera control not enabled\n");
  1809. return -ENODEV;
  1810. }
  1811. wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_PICTURE,
  1812. SONYPI_CAMERA_MUTE_MASK),
  1813. ITERATIONS_SHORT);
  1814. if (spic_dev.camera_power) {
  1815. sony_pic_call2(0x91, 0);
  1816. spic_dev.camera_power = 0;
  1817. }
  1818. return 0;
  1819. }
  1820. static int __sony_pic_camera_on(void)
  1821. {
  1822. int i, j, x;
  1823. if (!camera) {
  1824. pr_warn(DRV_PFX "camera control not enabled\n");
  1825. return -ENODEV;
  1826. }
  1827. if (spic_dev.camera_power)
  1828. return 0;
  1829. for (j = 5; j > 0; j--) {
  1830. for (x = 0; x < 100 && sony_pic_call2(0x91, 0x1); x++)
  1831. msleep(10);
  1832. sony_pic_call1(0x93);
  1833. for (i = 400; i > 0; i--) {
  1834. if (__sony_pic_camera_ready())
  1835. break;
  1836. msleep(10);
  1837. }
  1838. if (i)
  1839. break;
  1840. }
  1841. if (j == 0) {
  1842. pr_warn(DRV_PFX "failed to power on camera\n");
  1843. return -ENODEV;
  1844. }
  1845. wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_CONTROL,
  1846. 0x5a),
  1847. ITERATIONS_SHORT);
  1848. spic_dev.camera_power = 1;
  1849. return 0;
  1850. }
  1851. /* External camera command (exported to the motion eye v4l driver) */
  1852. int sony_pic_camera_command(int command, u8 value)
  1853. {
  1854. if (!camera)
  1855. return -EIO;
  1856. mutex_lock(&spic_dev.lock);
  1857. switch (command) {
  1858. case SONY_PIC_COMMAND_SETCAMERA:
  1859. if (value)
  1860. __sony_pic_camera_on();
  1861. else
  1862. __sony_pic_camera_off();
  1863. break;
  1864. case SONY_PIC_COMMAND_SETCAMERABRIGHTNESS:
  1865. wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_BRIGHTNESS, value),
  1866. ITERATIONS_SHORT);
  1867. break;
  1868. case SONY_PIC_COMMAND_SETCAMERACONTRAST:
  1869. wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_CONTRAST, value),
  1870. ITERATIONS_SHORT);
  1871. break;
  1872. case SONY_PIC_COMMAND_SETCAMERAHUE:
  1873. wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_HUE, value),
  1874. ITERATIONS_SHORT);
  1875. break;
  1876. case SONY_PIC_COMMAND_SETCAMERACOLOR:
  1877. wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_COLOR, value),
  1878. ITERATIONS_SHORT);
  1879. break;
  1880. case SONY_PIC_COMMAND_SETCAMERASHARPNESS:
  1881. wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_SHARPNESS, value),
  1882. ITERATIONS_SHORT);
  1883. break;
  1884. case SONY_PIC_COMMAND_SETCAMERAPICTURE:
  1885. wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_PICTURE, value),
  1886. ITERATIONS_SHORT);
  1887. break;
  1888. case SONY_PIC_COMMAND_SETCAMERAAGC:
  1889. wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_AGC, value),
  1890. ITERATIONS_SHORT);
  1891. break;
  1892. default:
  1893. pr_err(DRV_PFX "sony_pic_camera_command invalid: %d\n",
  1894. command);
  1895. break;
  1896. }
  1897. mutex_unlock(&spic_dev.lock);
  1898. return 0;
  1899. }
  1900. EXPORT_SYMBOL(sony_pic_camera_command);
  1901. /* gprs/edge modem (SZ460N and SZ210P), thanks to Joshua Wise */
  1902. static void __sony_pic_set_wwanpower(u8 state)
  1903. {
  1904. state = !!state;
  1905. if (spic_dev.wwan_power == state)
  1906. return;
  1907. sony_pic_call2(0xB0, state);
  1908. sony_pic_call1(0x82);
  1909. spic_dev.wwan_power = state;
  1910. }
  1911. static ssize_t sony_pic_wwanpower_store(struct device *dev,
  1912. struct device_attribute *attr,
  1913. const char *buffer, size_t count)
  1914. {
  1915. unsigned long value;
  1916. if (count > 31)
  1917. return -EINVAL;
  1918. value = simple_strtoul(buffer, NULL, 10);
  1919. mutex_lock(&spic_dev.lock);
  1920. __sony_pic_set_wwanpower(value);
  1921. mutex_unlock(&spic_dev.lock);
  1922. return count;
  1923. }
  1924. static ssize_t sony_pic_wwanpower_show(struct device *dev,
  1925. struct device_attribute *attr, char *buffer)
  1926. {
  1927. ssize_t count;
  1928. mutex_lock(&spic_dev.lock);
  1929. count = snprintf(buffer, PAGE_SIZE, "%d\n", spic_dev.wwan_power);
  1930. mutex_unlock(&spic_dev.lock);
  1931. return count;
  1932. }
  1933. /* bluetooth subsystem power state */
  1934. static void __sony_pic_set_bluetoothpower(u8 state)
  1935. {
  1936. state = !!state;
  1937. if (spic_dev.bluetooth_power == state)
  1938. return;
  1939. sony_pic_call2(0x96, state);
  1940. sony_pic_call1(0x82);
  1941. spic_dev.bluetooth_power = state;
  1942. }
  1943. static ssize_t sony_pic_bluetoothpower_store(struct device *dev,
  1944. struct device_attribute *attr,
  1945. const char *buffer, size_t count)
  1946. {
  1947. unsigned long value;
  1948. if (count > 31)
  1949. return -EINVAL;
  1950. value = simple_strtoul(buffer, NULL, 10);
  1951. mutex_lock(&spic_dev.lock);
  1952. __sony_pic_set_bluetoothpower(value);
  1953. mutex_unlock(&spic_dev.lock);
  1954. return count;
  1955. }
  1956. static ssize_t sony_pic_bluetoothpower_show(struct device *dev,
  1957. struct device_attribute *attr, char *buffer)
  1958. {
  1959. ssize_t count = 0;
  1960. mutex_lock(&spic_dev.lock);
  1961. count = snprintf(buffer, PAGE_SIZE, "%d\n", spic_dev.bluetooth_power);
  1962. mutex_unlock(&spic_dev.lock);
  1963. return count;
  1964. }
  1965. /* fan speed */
  1966. /* FAN0 information (reverse engineered from ACPI tables) */
  1967. #define SONY_PIC_FAN0_STATUS 0x93
  1968. static int sony_pic_set_fanspeed(unsigned long value)
  1969. {
  1970. return ec_write(SONY_PIC_FAN0_STATUS, value);
  1971. }
  1972. static int sony_pic_get_fanspeed(u8 *value)
  1973. {
  1974. return ec_read(SONY_PIC_FAN0_STATUS, value);
  1975. }
  1976. static ssize_t sony_pic_fanspeed_store(struct device *dev,
  1977. struct device_attribute *attr,
  1978. const char *buffer, size_t count)
  1979. {
  1980. unsigned long value;
  1981. if (count > 31)
  1982. return -EINVAL;
  1983. value = simple_strtoul(buffer, NULL, 10);
  1984. if (sony_pic_set_fanspeed(value))
  1985. return -EIO;
  1986. return count;
  1987. }
  1988. static ssize_t sony_pic_fanspeed_show(struct device *dev,
  1989. struct device_attribute *attr, char *buffer)
  1990. {
  1991. u8 value = 0;
  1992. if (sony_pic_get_fanspeed(&value))
  1993. return -EIO;
  1994. return snprintf(buffer, PAGE_SIZE, "%d\n", value);
  1995. }
  1996. #define SPIC_ATTR(_name, _mode) \
  1997. struct device_attribute spic_attr_##_name = __ATTR(_name, \
  1998. _mode, sony_pic_## _name ##_show, \
  1999. sony_pic_## _name ##_store)
  2000. static SPIC_ATTR(bluetoothpower, 0644);
  2001. static SPIC_ATTR(wwanpower, 0644);
  2002. static SPIC_ATTR(fanspeed, 0644);
  2003. static struct attribute *spic_attributes[] = {
  2004. &spic_attr_bluetoothpower.attr,
  2005. &spic_attr_wwanpower.attr,
  2006. &spic_attr_fanspeed.attr,
  2007. NULL
  2008. };
  2009. static struct attribute_group spic_attribute_group = {
  2010. .attrs = spic_attributes
  2011. };
  2012. /******** SONYPI compatibility **********/
  2013. #ifdef CONFIG_SONYPI_COMPAT
  2014. /* battery / brightness / temperature addresses */
  2015. #define SONYPI_BAT_FLAGS 0x81
  2016. #define SONYPI_LCD_LIGHT 0x96
  2017. #define SONYPI_BAT1_PCTRM 0xa0
  2018. #define SONYPI_BAT1_LEFT 0xa2
  2019. #define SONYPI_BAT1_MAXRT 0xa4
  2020. #define SONYPI_BAT2_PCTRM 0xa8
  2021. #define SONYPI_BAT2_LEFT 0xaa
  2022. #define SONYPI_BAT2_MAXRT 0xac
  2023. #define SONYPI_BAT1_MAXTK 0xb0
  2024. #define SONYPI_BAT1_FULL 0xb2
  2025. #define SONYPI_BAT2_MAXTK 0xb8
  2026. #define SONYPI_BAT2_FULL 0xba
  2027. #define SONYPI_TEMP_STATUS 0xC1
  2028. struct sonypi_compat_s {
  2029. struct fasync_struct *fifo_async;
  2030. struct kfifo fifo;
  2031. spinlock_t fifo_lock;
  2032. wait_queue_head_t fifo_proc_list;
  2033. atomic_t open_count;
  2034. };
  2035. static struct sonypi_compat_s sonypi_compat = {
  2036. .open_count = ATOMIC_INIT(0),
  2037. };
  2038. static int sonypi_misc_fasync(int fd, struct file *filp, int on)
  2039. {
  2040. return fasync_helper(fd, filp, on, &sonypi_compat.fifo_async);
  2041. }
  2042. static int sonypi_misc_release(struct inode *inode, struct file *file)
  2043. {
  2044. atomic_dec(&sonypi_compat.open_count);
  2045. return 0;
  2046. }
  2047. static int sonypi_misc_open(struct inode *inode, struct file *file)
  2048. {
  2049. /* Flush input queue on first open */
  2050. unsigned long flags;
  2051. spin_lock_irqsave(&sonypi_compat.fifo_lock, flags);
  2052. if (atomic_inc_return(&sonypi_compat.open_count) == 1)
  2053. kfifo_reset(&sonypi_compat.fifo);
  2054. spin_unlock_irqrestore(&sonypi_compat.fifo_lock, flags);
  2055. return 0;
  2056. }
  2057. static ssize_t sonypi_misc_read(struct file *file, char __user *buf,
  2058. size_t count, loff_t *pos)
  2059. {
  2060. ssize_t ret;
  2061. unsigned char c;
  2062. if ((kfifo_len(&sonypi_compat.fifo) == 0) &&
  2063. (file->f_flags & O_NONBLOCK))
  2064. return -EAGAIN;
  2065. ret = wait_event_interruptible(sonypi_compat.fifo_proc_list,
  2066. kfifo_len(&sonypi_compat.fifo) != 0);
  2067. if (ret)
  2068. return ret;
  2069. while (ret < count &&
  2070. (kfifo_out_locked(&sonypi_compat.fifo, &c, sizeof(c),
  2071. &sonypi_compat.fifo_lock) == sizeof(c))) {
  2072. if (put_user(c, buf++))
  2073. return -EFAULT;
  2074. ret++;
  2075. }
  2076. if (ret > 0) {
  2077. struct inode *inode = file->f_path.dentry->d_inode;
  2078. inode->i_atime = current_fs_time(inode->i_sb);
  2079. }
  2080. return ret;
  2081. }
  2082. static unsigned int sonypi_misc_poll(struct file *file, poll_table *wait)
  2083. {
  2084. poll_wait(file, &sonypi_compat.fifo_proc_list, wait);
  2085. if (kfifo_len(&sonypi_compat.fifo))
  2086. return POLLIN | POLLRDNORM;
  2087. return 0;
  2088. }
  2089. static int ec_read16(u8 addr, u16 *value)
  2090. {
  2091. u8 val_lb, val_hb;
  2092. if (ec_read(addr, &val_lb))
  2093. return -1;
  2094. if (ec_read(addr + 1, &val_hb))
  2095. return -1;
  2096. *value = val_lb | (val_hb << 8);
  2097. return 0;
  2098. }
  2099. static long sonypi_misc_ioctl(struct file *fp, unsigned int cmd,
  2100. unsigned long arg)
  2101. {
  2102. int ret = 0;
  2103. void __user *argp = (void __user *)arg;
  2104. u8 val8;
  2105. u16 val16;
  2106. int value;
  2107. mutex_lock(&spic_dev.lock);
  2108. switch (cmd) {
  2109. case SONYPI_IOCGBRT:
  2110. if (sony_backlight_device == NULL) {
  2111. ret = -EIO;
  2112. break;
  2113. }
  2114. if (acpi_callgetfunc(sony_nc_acpi_handle, "GBRT", &value)) {
  2115. ret = -EIO;
  2116. break;
  2117. }
  2118. val8 = ((value & 0xff) - 1) << 5;
  2119. if (copy_to_user(argp, &val8, sizeof(val8)))
  2120. ret = -EFAULT;
  2121. break;
  2122. case SONYPI_IOCSBRT:
  2123. if (sony_backlight_device == NULL) {
  2124. ret = -EIO;
  2125. break;
  2126. }
  2127. if (copy_from_user(&val8, argp, sizeof(val8))) {
  2128. ret = -EFAULT;
  2129. break;
  2130. }
  2131. if (acpi_callsetfunc(sony_nc_acpi_handle, "SBRT",
  2132. (val8 >> 5) + 1, NULL)) {
  2133. ret = -EIO;
  2134. break;
  2135. }
  2136. /* sync the backlight device status */
  2137. sony_backlight_device->props.brightness =
  2138. sony_backlight_get_brightness(sony_backlight_device);
  2139. break;
  2140. case SONYPI_IOCGBAT1CAP:
  2141. if (ec_read16(SONYPI_BAT1_FULL, &val16)) {
  2142. ret = -EIO;
  2143. break;
  2144. }
  2145. if (copy_to_user(argp, &val16, sizeof(val16)))
  2146. ret = -EFAULT;
  2147. break;
  2148. case SONYPI_IOCGBAT1REM:
  2149. if (ec_read16(SONYPI_BAT1_LEFT, &val16)) {
  2150. ret = -EIO;
  2151. break;
  2152. }
  2153. if (copy_to_user(argp, &val16, sizeof(val16)))
  2154. ret = -EFAULT;
  2155. break;
  2156. case SONYPI_IOCGBAT2CAP:
  2157. if (ec_read16(SONYPI_BAT2_FULL, &val16)) {
  2158. ret = -EIO;
  2159. break;
  2160. }
  2161. if (copy_to_user(argp, &val16, sizeof(val16)))
  2162. ret = -EFAULT;
  2163. break;
  2164. case SONYPI_IOCGBAT2REM:
  2165. if (ec_read16(SONYPI_BAT2_LEFT, &val16)) {
  2166. ret = -EIO;
  2167. break;
  2168. }
  2169. if (copy_to_user(argp, &val16, sizeof(val16)))
  2170. ret = -EFAULT;
  2171. break;
  2172. case SONYPI_IOCGBATFLAGS:
  2173. if (ec_read(SONYPI_BAT_FLAGS, &val8)) {
  2174. ret = -EIO;
  2175. break;
  2176. }
  2177. val8 &= 0x07;
  2178. if (copy_to_user(argp, &val8, sizeof(val8)))
  2179. ret = -EFAULT;
  2180. break;
  2181. case SONYPI_IOCGBLUE:
  2182. val8 = spic_dev.bluetooth_power;
  2183. if (copy_to_user(argp, &val8, sizeof(val8)))
  2184. ret = -EFAULT;
  2185. break;
  2186. case SONYPI_IOCSBLUE:
  2187. if (copy_from_user(&val8, argp, sizeof(val8))) {
  2188. ret = -EFAULT;
  2189. break;
  2190. }
  2191. __sony_pic_set_bluetoothpower(val8);
  2192. break;
  2193. /* FAN Controls */
  2194. case SONYPI_IOCGFAN:
  2195. if (sony_pic_get_fanspeed(&val8)) {
  2196. ret = -EIO;
  2197. break;
  2198. }
  2199. if (copy_to_user(argp, &val8, sizeof(val8)))
  2200. ret = -EFAULT;
  2201. break;
  2202. case SONYPI_IOCSFAN:
  2203. if (copy_from_user(&val8, argp, sizeof(val8))) {
  2204. ret = -EFAULT;
  2205. break;
  2206. }
  2207. if (sony_pic_set_fanspeed(val8))
  2208. ret = -EIO;
  2209. break;
  2210. /* GET Temperature (useful under APM) */
  2211. case SONYPI_IOCGTEMP:
  2212. if (ec_read(SONYPI_TEMP_STATUS, &val8)) {
  2213. ret = -EIO;
  2214. break;
  2215. }
  2216. if (copy_to_user(argp, &val8, sizeof(val8)))
  2217. ret = -EFAULT;
  2218. break;
  2219. default:
  2220. ret = -EINVAL;
  2221. }
  2222. mutex_unlock(&spic_dev.lock);
  2223. return ret;
  2224. }
  2225. static const struct file_operations sonypi_misc_fops = {
  2226. .owner = THIS_MODULE,
  2227. .read = sonypi_misc_read,
  2228. .poll = sonypi_misc_poll,
  2229. .open = sonypi_misc_open,
  2230. .release = sonypi_misc_release,
  2231. .fasync = sonypi_misc_fasync,
  2232. .unlocked_ioctl = sonypi_misc_ioctl,
  2233. .llseek = noop_llseek,
  2234. };
  2235. static struct miscdevice sonypi_misc_device = {
  2236. .minor = MISC_DYNAMIC_MINOR,
  2237. .name = "sonypi",
  2238. .fops = &sonypi_misc_fops,
  2239. };
  2240. static void sonypi_compat_report_event(u8 event)
  2241. {
  2242. kfifo_in_locked(&sonypi_compat.fifo, (unsigned char *)&event,
  2243. sizeof(event), &sonypi_compat.fifo_lock);
  2244. kill_fasync(&sonypi_compat.fifo_async, SIGIO, POLL_IN);
  2245. wake_up_interruptible(&sonypi_compat.fifo_proc_list);
  2246. }
  2247. static int sonypi_compat_init(void)
  2248. {
  2249. int error;
  2250. spin_lock_init(&sonypi_compat.fifo_lock);
  2251. error =
  2252. kfifo_alloc(&sonypi_compat.fifo, SONY_LAPTOP_BUF_SIZE, GFP_KERNEL);
  2253. if (error) {
  2254. pr_err(DRV_PFX "kfifo_alloc failed\n");
  2255. return error;
  2256. }
  2257. init_waitqueue_head(&sonypi_compat.fifo_proc_list);
  2258. if (minor != -1)
  2259. sonypi_misc_device.minor = minor;
  2260. error = misc_register(&sonypi_misc_device);
  2261. if (error) {
  2262. pr_err(DRV_PFX "misc_register failed\n");
  2263. goto err_free_kfifo;
  2264. }
  2265. if (minor == -1)
  2266. pr_info(DRV_PFX "device allocated minor is %d\n",
  2267. sonypi_misc_device.minor);
  2268. return 0;
  2269. err_free_kfifo:
  2270. kfifo_free(&sonypi_compat.fifo);
  2271. return error;
  2272. }
  2273. static void sonypi_compat_exit(void)
  2274. {
  2275. misc_deregister(&sonypi_misc_device);
  2276. kfifo_free(&sonypi_compat.fifo);
  2277. }
  2278. #else
  2279. static int sonypi_compat_init(void) { return 0; }
  2280. static void sonypi_compat_exit(void) { }
  2281. static void sonypi_compat_report_event(u8 event) { }
  2282. #endif /* CONFIG_SONYPI_COMPAT */
  2283. /*
  2284. * ACPI callbacks
  2285. */
  2286. static acpi_status
  2287. sony_pic_read_possible_resource(struct acpi_resource *resource, void *context)
  2288. {
  2289. u32 i;
  2290. struct sony_pic_dev *dev = (struct sony_pic_dev *)context;
  2291. switch (resource->type) {
  2292. case ACPI_RESOURCE_TYPE_START_DEPENDENT:
  2293. {
  2294. /* start IO enumeration */
  2295. struct sony_pic_ioport *ioport = kzalloc(sizeof(*ioport), GFP_KERNEL);
  2296. if (!ioport)
  2297. return AE_ERROR;
  2298. list_add(&ioport->list, &dev->ioports);
  2299. return AE_OK;
  2300. }
  2301. case ACPI_RESOURCE_TYPE_END_DEPENDENT:
  2302. /* end IO enumeration */
  2303. return AE_OK;
  2304. case ACPI_RESOURCE_TYPE_IRQ:
  2305. {
  2306. struct acpi_resource_irq *p = &resource->data.irq;
  2307. struct sony_pic_irq *interrupt = NULL;
  2308. if (!p || !p->interrupt_count) {
  2309. /*
  2310. * IRQ descriptors may have no IRQ# bits set,
  2311. * particularly those those w/ _STA disabled
  2312. */
  2313. dprintk("Blank IRQ resource\n");
  2314. return AE_OK;
  2315. }
  2316. for (i = 0; i < p->interrupt_count; i++) {
  2317. if (!p->interrupts[i]) {
  2318. pr_warn(DRV_PFX "Invalid IRQ %d\n",
  2319. p->interrupts[i]);
  2320. continue;
  2321. }
  2322. interrupt = kzalloc(sizeof(*interrupt),
  2323. GFP_KERNEL);
  2324. if (!interrupt)
  2325. return AE_ERROR;
  2326. list_add(&interrupt->list, &dev->interrupts);
  2327. interrupt->irq.triggering = p->triggering;
  2328. interrupt->irq.polarity = p->polarity;
  2329. interrupt->irq.sharable = p->sharable;
  2330. interrupt->irq.interrupt_count = 1;
  2331. interrupt->irq.interrupts[0] = p->interrupts[i];
  2332. }
  2333. return AE_OK;
  2334. }
  2335. case ACPI_RESOURCE_TYPE_IO:
  2336. {
  2337. struct acpi_resource_io *io = &resource->data.io;
  2338. struct sony_pic_ioport *ioport =
  2339. list_first_entry(&dev->ioports, struct sony_pic_ioport, list);
  2340. if (!io) {
  2341. dprintk("Blank IO resource\n");
  2342. return AE_OK;
  2343. }
  2344. if (!ioport->io1.minimum) {
  2345. memcpy(&ioport->io1, io, sizeof(*io));
  2346. dprintk("IO1 at 0x%.4x (0x%.2x)\n", ioport->io1.minimum,
  2347. ioport->io1.address_length);
  2348. }
  2349. else if (!ioport->io2.minimum) {
  2350. memcpy(&ioport->io2, io, sizeof(*io));
  2351. dprintk("IO2 at 0x%.4x (0x%.2x)\n", ioport->io2.minimum,
  2352. ioport->io2.address_length);
  2353. }
  2354. else {
  2355. pr_err(DRV_PFX "Unknown SPIC Type, more than 2 IO Ports\n");
  2356. return AE_ERROR;
  2357. }
  2358. return AE_OK;
  2359. }
  2360. default:
  2361. dprintk("Resource %d isn't an IRQ nor an IO port\n",
  2362. resource->type);
  2363. case ACPI_RESOURCE_TYPE_END_TAG:
  2364. return AE_OK;
  2365. }
  2366. return AE_CTRL_TERMINATE;
  2367. }
  2368. static int sony_pic_possible_resources(struct acpi_device *device)
  2369. {
  2370. int result = 0;
  2371. acpi_status status = AE_OK;
  2372. if (!device)
  2373. return -EINVAL;
  2374. /* get device status */
  2375. /* see acpi_pci_link_get_current acpi_pci_link_get_possible */
  2376. dprintk("Evaluating _STA\n");
  2377. result = acpi_bus_get_status(device);
  2378. if (result) {
  2379. pr_warn(DRV_PFX "Unable to read status\n");
  2380. goto end;
  2381. }
  2382. if (!device->status.enabled)
  2383. dprintk("Device disabled\n");
  2384. else
  2385. dprintk("Device enabled\n");
  2386. /*
  2387. * Query and parse 'method'
  2388. */
  2389. dprintk("Evaluating %s\n", METHOD_NAME__PRS);
  2390. status = acpi_walk_resources(device->handle, METHOD_NAME__PRS,
  2391. sony_pic_read_possible_resource, &spic_dev);
  2392. if (ACPI_FAILURE(status)) {
  2393. pr_warn(DRV_PFX "Failure evaluating %s\n",
  2394. METHOD_NAME__PRS);
  2395. result = -ENODEV;
  2396. }
  2397. end:
  2398. return result;
  2399. }
  2400. /*
  2401. * Disable the spic device by calling its _DIS method
  2402. */
  2403. static int sony_pic_disable(struct acpi_device *device)
  2404. {
  2405. acpi_status ret = acpi_evaluate_object(device->handle, "_DIS", NULL,
  2406. NULL);
  2407. if (ACPI_FAILURE(ret) && ret != AE_NOT_FOUND)
  2408. return -ENXIO;
  2409. dprintk("Device disabled\n");
  2410. return 0;
  2411. }
  2412. /*
  2413. * Based on drivers/acpi/pci_link.c:acpi_pci_link_set
  2414. *
  2415. * Call _SRS to set current resources
  2416. */
  2417. static int sony_pic_enable(struct acpi_device *device,
  2418. struct sony_pic_ioport *ioport, struct sony_pic_irq *irq)
  2419. {
  2420. acpi_status status;
  2421. int result = 0;
  2422. /* Type 1 resource layout is:
  2423. * IO
  2424. * IO
  2425. * IRQNoFlags
  2426. * End
  2427. *
  2428. * Type 2 and 3 resource layout is:
  2429. * IO
  2430. * IRQNoFlags
  2431. * End
  2432. */
  2433. struct {
  2434. struct acpi_resource res1;
  2435. struct acpi_resource res2;
  2436. struct acpi_resource res3;
  2437. struct acpi_resource res4;
  2438. } *resource;
  2439. struct acpi_buffer buffer = { 0, NULL };
  2440. if (!ioport || !irq)
  2441. return -EINVAL;
  2442. /* init acpi_buffer */
  2443. resource = kzalloc(sizeof(*resource) + 1, GFP_KERNEL);
  2444. if (!resource)
  2445. return -ENOMEM;
  2446. buffer.length = sizeof(*resource) + 1;
  2447. buffer.pointer = resource;
  2448. /* setup Type 1 resources */
  2449. if (spic_dev.model == SONYPI_DEVICE_TYPE1) {
  2450. /* setup io resources */
  2451. resource->res1.type = ACPI_RESOURCE_TYPE_IO;
  2452. resource->res1.length = sizeof(struct acpi_resource);
  2453. memcpy(&resource->res1.data.io, &ioport->io1,
  2454. sizeof(struct acpi_resource_io));
  2455. resource->res2.type = ACPI_RESOURCE_TYPE_IO;
  2456. resource->res2.length = sizeof(struct acpi_resource);
  2457. memcpy(&resource->res2.data.io, &ioport->io2,
  2458. sizeof(struct acpi_resource_io));
  2459. /* setup irq resource */
  2460. resource->res3.type = ACPI_RESOURCE_TYPE_IRQ;
  2461. resource->res3.length = sizeof(struct acpi_resource);
  2462. memcpy(&resource->res3.data.irq, &irq->irq,
  2463. sizeof(struct acpi_resource_irq));
  2464. /* we requested a shared irq */
  2465. resource->res3.data.irq.sharable = ACPI_SHARED;
  2466. resource->res4.type = ACPI_RESOURCE_TYPE_END_TAG;
  2467. }
  2468. /* setup Type 2/3 resources */
  2469. else {
  2470. /* setup io resource */
  2471. resource->res1.type = ACPI_RESOURCE_TYPE_IO;
  2472. resource->res1.length = sizeof(struct acpi_resource);
  2473. memcpy(&resource->res1.data.io, &ioport->io1,
  2474. sizeof(struct acpi_resource_io));
  2475. /* setup irq resource */
  2476. resource->res2.type = ACPI_RESOURCE_TYPE_IRQ;
  2477. resource->res2.length = sizeof(struct acpi_resource);
  2478. memcpy(&resource->res2.data.irq, &irq->irq,
  2479. sizeof(struct acpi_resource_irq));
  2480. /* we requested a shared irq */
  2481. resource->res2.data.irq.sharable = ACPI_SHARED;
  2482. resource->res3.type = ACPI_RESOURCE_TYPE_END_TAG;
  2483. }
  2484. /* Attempt to set the resource */
  2485. dprintk("Evaluating _SRS\n");
  2486. status = acpi_set_current_resources(device->handle, &buffer);
  2487. /* check for total failure */
  2488. if (ACPI_FAILURE(status)) {
  2489. pr_err(DRV_PFX "Error evaluating _SRS\n");
  2490. result = -ENODEV;
  2491. goto end;
  2492. }
  2493. /* Necessary device initializations calls (from sonypi) */
  2494. sony_pic_call1(0x82);
  2495. sony_pic_call2(0x81, 0xff);
  2496. sony_pic_call1(compat ? 0x92 : 0x82);
  2497. end:
  2498. kfree(resource);
  2499. return result;
  2500. }
  2501. /*****************
  2502. *
  2503. * ISR: some event is available
  2504. *
  2505. *****************/
  2506. static irqreturn_t sony_pic_irq(int irq, void *dev_id)
  2507. {
  2508. int i, j;
  2509. u8 ev = 0;
  2510. u8 data_mask = 0;
  2511. u8 device_event = 0;
  2512. struct sony_pic_dev *dev = (struct sony_pic_dev *) dev_id;
  2513. ev = inb_p(dev->cur_ioport->io1.minimum);
  2514. if (dev->cur_ioport->io2.minimum)
  2515. data_mask = inb_p(dev->cur_ioport->io2.minimum);
  2516. else
  2517. data_mask = inb_p(dev->cur_ioport->io1.minimum +
  2518. dev->evport_offset);
  2519. dprintk("event ([%.2x] [%.2x]) at port 0x%.4x(+0x%.2x)\n",
  2520. ev, data_mask, dev->cur_ioport->io1.minimum,
  2521. dev->evport_offset);
  2522. if (ev == 0x00 || ev == 0xff)
  2523. return IRQ_HANDLED;
  2524. for (i = 0; dev->event_types[i].mask; i++) {
  2525. if ((data_mask & dev->event_types[i].data) !=
  2526. dev->event_types[i].data)
  2527. continue;
  2528. if (!(mask & dev->event_types[i].mask))
  2529. continue;
  2530. for (j = 0; dev->event_types[i].events[j].event; j++) {
  2531. if (ev == dev->event_types[i].events[j].data) {
  2532. device_event =
  2533. dev->event_types[i].events[j].event;
  2534. /* some events may require ignoring */
  2535. if (!device_event)
  2536. return IRQ_HANDLED;
  2537. goto found;
  2538. }
  2539. }
  2540. }
  2541. /* Still not able to decode the event try to pass
  2542. * it over to the minidriver
  2543. */
  2544. if (dev->handle_irq && dev->handle_irq(data_mask, ev) == 0)
  2545. return IRQ_HANDLED;
  2546. dprintk("unknown event ([%.2x] [%.2x]) at port 0x%.4x(+0x%.2x)\n",
  2547. ev, data_mask, dev->cur_ioport->io1.minimum,
  2548. dev->evport_offset);
  2549. return IRQ_HANDLED;
  2550. found:
  2551. sony_laptop_report_input_event(device_event);
  2552. acpi_bus_generate_proc_event(dev->acpi_dev, 1, device_event);
  2553. sonypi_compat_report_event(device_event);
  2554. return IRQ_HANDLED;
  2555. }
  2556. /*****************
  2557. *
  2558. * ACPI driver
  2559. *
  2560. *****************/
  2561. static int sony_pic_remove(struct acpi_device *device, int type)
  2562. {
  2563. struct sony_pic_ioport *io, *tmp_io;
  2564. struct sony_pic_irq *irq, *tmp_irq;
  2565. if (sony_pic_disable(device)) {
  2566. pr_err(DRV_PFX "Couldn't disable device.\n");
  2567. return -ENXIO;
  2568. }
  2569. free_irq(spic_dev.cur_irq->irq.interrupts[0], &spic_dev);
  2570. release_region(spic_dev.cur_ioport->io1.minimum,
  2571. spic_dev.cur_ioport->io1.address_length);
  2572. if (spic_dev.cur_ioport->io2.minimum)
  2573. release_region(spic_dev.cur_ioport->io2.minimum,
  2574. spic_dev.cur_ioport->io2.address_length);
  2575. sonypi_compat_exit();
  2576. sony_laptop_remove_input();
  2577. /* pf attrs */
  2578. sysfs_remove_group(&sony_pf_device->dev.kobj, &spic_attribute_group);
  2579. sony_pf_remove();
  2580. list_for_each_entry_safe(io, tmp_io, &spic_dev.ioports, list) {
  2581. list_del(&io->list);
  2582. kfree(io);
  2583. }
  2584. list_for_each_entry_safe(irq, tmp_irq, &spic_dev.interrupts, list) {
  2585. list_del(&irq->list);
  2586. kfree(irq);
  2587. }
  2588. spic_dev.cur_ioport = NULL;
  2589. spic_dev.cur_irq = NULL;
  2590. dprintk(SONY_PIC_DRIVER_NAME " removed.\n");
  2591. return 0;
  2592. }
  2593. static int sony_pic_add(struct acpi_device *device)
  2594. {
  2595. int result;
  2596. struct sony_pic_ioport *io, *tmp_io;
  2597. struct sony_pic_irq *irq, *tmp_irq;
  2598. pr_info(DRV_PFX "%s v%s.\n", SONY_PIC_DRIVER_NAME,
  2599. SONY_LAPTOP_DRIVER_VERSION);
  2600. spic_dev.acpi_dev = device;
  2601. strcpy(acpi_device_class(device), "sony/hotkey");
  2602. sony_pic_detect_device_type(&spic_dev);
  2603. mutex_init(&spic_dev.lock);
  2604. /* read _PRS resources */
  2605. result = sony_pic_possible_resources(device);
  2606. if (result) {
  2607. pr_err(DRV_PFX "Unable to read possible resources.\n");
  2608. goto err_free_resources;
  2609. }
  2610. /* setup input devices and helper fifo */
  2611. result = sony_laptop_setup_input(device);
  2612. if (result) {
  2613. pr_err(DRV_PFX "Unable to create input devices.\n");
  2614. goto err_free_resources;
  2615. }
  2616. if (sonypi_compat_init())
  2617. goto err_remove_input;
  2618. /* request io port */
  2619. list_for_each_entry_reverse(io, &spic_dev.ioports, list) {
  2620. if (request_region(io->io1.minimum, io->io1.address_length,
  2621. "Sony Programable I/O Device")) {
  2622. dprintk("I/O port1: 0x%.4x (0x%.4x) + 0x%.2x\n",
  2623. io->io1.minimum, io->io1.maximum,
  2624. io->io1.address_length);
  2625. /* Type 1 have 2 ioports */
  2626. if (io->io2.minimum) {
  2627. if (request_region(io->io2.minimum,
  2628. io->io2.address_length,
  2629. "Sony Programable I/O Device")) {
  2630. dprintk("I/O port2: 0x%.4x (0x%.4x) + 0x%.2x\n",
  2631. io->io2.minimum, io->io2.maximum,
  2632. io->io2.address_length);
  2633. spic_dev.cur_ioport = io;
  2634. break;
  2635. }
  2636. else {
  2637. dprintk("Unable to get I/O port2: "
  2638. "0x%.4x (0x%.4x) + 0x%.2x\n",
  2639. io->io2.minimum, io->io2.maximum,
  2640. io->io2.address_length);
  2641. release_region(io->io1.minimum,
  2642. io->io1.address_length);
  2643. }
  2644. }
  2645. else {
  2646. spic_dev.cur_ioport = io;
  2647. break;
  2648. }
  2649. }
  2650. }
  2651. if (!spic_dev.cur_ioport) {
  2652. pr_err(DRV_PFX "Failed to request_region.\n");
  2653. result = -ENODEV;
  2654. goto err_remove_compat;
  2655. }
  2656. /* request IRQ */
  2657. list_for_each_entry_reverse(irq, &spic_dev.interrupts, list) {
  2658. if (!request_irq(irq->irq.interrupts[0], sony_pic_irq,
  2659. IRQF_DISABLED, "sony-laptop", &spic_dev)) {
  2660. dprintk("IRQ: %d - triggering: %d - "
  2661. "polarity: %d - shr: %d\n",
  2662. irq->irq.interrupts[0],
  2663. irq->irq.triggering,
  2664. irq->irq.polarity,
  2665. irq->irq.sharable);
  2666. spic_dev.cur_irq = irq;
  2667. break;
  2668. }
  2669. }
  2670. if (!spic_dev.cur_irq) {
  2671. pr_err(DRV_PFX "Failed to request_irq.\n");
  2672. result = -ENODEV;
  2673. goto err_release_region;
  2674. }
  2675. /* set resource status _SRS */
  2676. result = sony_pic_enable(device, spic_dev.cur_ioport, spic_dev.cur_irq);
  2677. if (result) {
  2678. pr_err(DRV_PFX "Couldn't enable device.\n");
  2679. goto err_free_irq;
  2680. }
  2681. spic_dev.bluetooth_power = -1;
  2682. /* create device attributes */
  2683. result = sony_pf_add();
  2684. if (result)
  2685. goto err_disable_device;
  2686. result = sysfs_create_group(&sony_pf_device->dev.kobj, &spic_attribute_group);
  2687. if (result)
  2688. goto err_remove_pf;
  2689. return 0;
  2690. err_remove_pf:
  2691. sony_pf_remove();
  2692. err_disable_device:
  2693. sony_pic_disable(device);
  2694. err_free_irq:
  2695. free_irq(spic_dev.cur_irq->irq.interrupts[0], &spic_dev);
  2696. err_release_region:
  2697. release_region(spic_dev.cur_ioport->io1.minimum,
  2698. spic_dev.cur_ioport->io1.address_length);
  2699. if (spic_dev.cur_ioport->io2.minimum)
  2700. release_region(spic_dev.cur_ioport->io2.minimum,
  2701. spic_dev.cur_ioport->io2.address_length);
  2702. err_remove_compat:
  2703. sonypi_compat_exit();
  2704. err_remove_input:
  2705. sony_laptop_remove_input();
  2706. err_free_resources:
  2707. list_for_each_entry_safe(io, tmp_io, &spic_dev.ioports, list) {
  2708. list_del(&io->list);
  2709. kfree(io);
  2710. }
  2711. list_for_each_entry_safe(irq, tmp_irq, &spic_dev.interrupts, list) {
  2712. list_del(&irq->list);
  2713. kfree(irq);
  2714. }
  2715. spic_dev.cur_ioport = NULL;
  2716. spic_dev.cur_irq = NULL;
  2717. return result;
  2718. }
  2719. static int sony_pic_suspend(struct acpi_device *device, pm_message_t state)
  2720. {
  2721. if (sony_pic_disable(device))
  2722. return -ENXIO;
  2723. return 0;
  2724. }
  2725. static int sony_pic_resume(struct acpi_device *device)
  2726. {
  2727. sony_pic_enable(device, spic_dev.cur_ioport, spic_dev.cur_irq);
  2728. return 0;
  2729. }
  2730. static const struct acpi_device_id sony_pic_device_ids[] = {
  2731. {SONY_PIC_HID, 0},
  2732. {"", 0},
  2733. };
  2734. static struct acpi_driver sony_pic_driver = {
  2735. .name = SONY_PIC_DRIVER_NAME,
  2736. .class = SONY_PIC_CLASS,
  2737. .ids = sony_pic_device_ids,
  2738. .owner = THIS_MODULE,
  2739. .ops = {
  2740. .add = sony_pic_add,
  2741. .remove = sony_pic_remove,
  2742. .suspend = sony_pic_suspend,
  2743. .resume = sony_pic_resume,
  2744. },
  2745. };
  2746. static struct dmi_system_id __initdata sonypi_dmi_table[] = {
  2747. {
  2748. .ident = "Sony Vaio",
  2749. .matches = {
  2750. DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
  2751. DMI_MATCH(DMI_PRODUCT_NAME, "PCG-"),
  2752. },
  2753. },
  2754. {
  2755. .ident = "Sony Vaio",
  2756. .matches = {
  2757. DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
  2758. DMI_MATCH(DMI_PRODUCT_NAME, "VGN-"),
  2759. },
  2760. },
  2761. { }
  2762. };
  2763. static int __init sony_laptop_init(void)
  2764. {
  2765. int result;
  2766. if (!no_spic && dmi_check_system(sonypi_dmi_table)) {
  2767. result = acpi_bus_register_driver(&sony_pic_driver);
  2768. if (result) {
  2769. pr_err(DRV_PFX "Unable to register SPIC driver.");
  2770. goto out;
  2771. }
  2772. spic_drv_registered = 1;
  2773. }
  2774. result = acpi_bus_register_driver(&sony_nc_driver);
  2775. if (result) {
  2776. pr_err(DRV_PFX "Unable to register SNC driver.");
  2777. goto out_unregister_pic;
  2778. }
  2779. return 0;
  2780. out_unregister_pic:
  2781. if (spic_drv_registered)
  2782. acpi_bus_unregister_driver(&sony_pic_driver);
  2783. out:
  2784. return result;
  2785. }
  2786. static void __exit sony_laptop_exit(void)
  2787. {
  2788. acpi_bus_unregister_driver(&sony_nc_driver);
  2789. if (spic_drv_registered)
  2790. acpi_bus_unregister_driver(&sony_pic_driver);
  2791. }
  2792. module_init(sony_laptop_init);
  2793. module_exit(sony_laptop_exit);