sony-laptop.c 77 KB

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