sony-laptop.c 77 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009
  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. };
  133. static struct sony_laptop_input_s sony_laptop_input = {
  134. .users = ATOMIC_INIT(0),
  135. };
  136. struct sony_laptop_keypress {
  137. struct input_dev *dev;
  138. int key;
  139. };
  140. /* Correspondance table between sonypi events
  141. * and input layer indexes in the keymap
  142. */
  143. static int sony_laptop_input_index[] = {
  144. -1, /* 0 no event */
  145. -1, /* 1 SONYPI_EVENT_JOGDIAL_DOWN */
  146. -1, /* 2 SONYPI_EVENT_JOGDIAL_UP */
  147. -1, /* 3 SONYPI_EVENT_JOGDIAL_DOWN_PRESSED */
  148. -1, /* 4 SONYPI_EVENT_JOGDIAL_UP_PRESSED */
  149. -1, /* 5 SONYPI_EVENT_JOGDIAL_PRESSED */
  150. -1, /* 6 SONYPI_EVENT_JOGDIAL_RELEASED */
  151. 0, /* 7 SONYPI_EVENT_CAPTURE_PRESSED */
  152. 1, /* 8 SONYPI_EVENT_CAPTURE_RELEASED */
  153. 2, /* 9 SONYPI_EVENT_CAPTURE_PARTIALPRESSED */
  154. 3, /* 10 SONYPI_EVENT_CAPTURE_PARTIALRELEASED */
  155. 4, /* 11 SONYPI_EVENT_FNKEY_ESC */
  156. 5, /* 12 SONYPI_EVENT_FNKEY_F1 */
  157. 6, /* 13 SONYPI_EVENT_FNKEY_F2 */
  158. 7, /* 14 SONYPI_EVENT_FNKEY_F3 */
  159. 8, /* 15 SONYPI_EVENT_FNKEY_F4 */
  160. 9, /* 16 SONYPI_EVENT_FNKEY_F5 */
  161. 10, /* 17 SONYPI_EVENT_FNKEY_F6 */
  162. 11, /* 18 SONYPI_EVENT_FNKEY_F7 */
  163. 12, /* 19 SONYPI_EVENT_FNKEY_F8 */
  164. 13, /* 20 SONYPI_EVENT_FNKEY_F9 */
  165. 14, /* 21 SONYPI_EVENT_FNKEY_F10 */
  166. 15, /* 22 SONYPI_EVENT_FNKEY_F11 */
  167. 16, /* 23 SONYPI_EVENT_FNKEY_F12 */
  168. 17, /* 24 SONYPI_EVENT_FNKEY_1 */
  169. 18, /* 25 SONYPI_EVENT_FNKEY_2 */
  170. 19, /* 26 SONYPI_EVENT_FNKEY_D */
  171. 20, /* 27 SONYPI_EVENT_FNKEY_E */
  172. 21, /* 28 SONYPI_EVENT_FNKEY_F */
  173. 22, /* 29 SONYPI_EVENT_FNKEY_S */
  174. 23, /* 30 SONYPI_EVENT_FNKEY_B */
  175. 24, /* 31 SONYPI_EVENT_BLUETOOTH_PRESSED */
  176. 25, /* 32 SONYPI_EVENT_PKEY_P1 */
  177. 26, /* 33 SONYPI_EVENT_PKEY_P2 */
  178. 27, /* 34 SONYPI_EVENT_PKEY_P3 */
  179. 28, /* 35 SONYPI_EVENT_BACK_PRESSED */
  180. -1, /* 36 SONYPI_EVENT_LID_CLOSED */
  181. -1, /* 37 SONYPI_EVENT_LID_OPENED */
  182. 29, /* 38 SONYPI_EVENT_BLUETOOTH_ON */
  183. 30, /* 39 SONYPI_EVENT_BLUETOOTH_OFF */
  184. 31, /* 40 SONYPI_EVENT_HELP_PRESSED */
  185. 32, /* 41 SONYPI_EVENT_FNKEY_ONLY */
  186. 33, /* 42 SONYPI_EVENT_JOGDIAL_FAST_DOWN */
  187. 34, /* 43 SONYPI_EVENT_JOGDIAL_FAST_UP */
  188. 35, /* 44 SONYPI_EVENT_JOGDIAL_FAST_DOWN_PRESSED */
  189. 36, /* 45 SONYPI_EVENT_JOGDIAL_FAST_UP_PRESSED */
  190. 37, /* 46 SONYPI_EVENT_JOGDIAL_VFAST_DOWN */
  191. 38, /* 47 SONYPI_EVENT_JOGDIAL_VFAST_UP */
  192. 39, /* 48 SONYPI_EVENT_JOGDIAL_VFAST_DOWN_PRESSED */
  193. 40, /* 49 SONYPI_EVENT_JOGDIAL_VFAST_UP_PRESSED */
  194. 41, /* 50 SONYPI_EVENT_ZOOM_PRESSED */
  195. 42, /* 51 SONYPI_EVENT_THUMBPHRASE_PRESSED */
  196. 43, /* 52 SONYPI_EVENT_MEYE_FACE */
  197. 44, /* 53 SONYPI_EVENT_MEYE_OPPOSITE */
  198. 45, /* 54 SONYPI_EVENT_MEMORYSTICK_INSERT */
  199. 46, /* 55 SONYPI_EVENT_MEMORYSTICK_EJECT */
  200. -1, /* 56 SONYPI_EVENT_ANYBUTTON_RELEASED */
  201. -1, /* 57 SONYPI_EVENT_BATTERY_INSERT */
  202. -1, /* 58 SONYPI_EVENT_BATTERY_REMOVE */
  203. -1, /* 59 SONYPI_EVENT_FNKEY_RELEASED */
  204. 47, /* 60 SONYPI_EVENT_WIRELESS_ON */
  205. 48, /* 61 SONYPI_EVENT_WIRELESS_OFF */
  206. 49, /* 62 SONYPI_EVENT_ZOOM_IN_PRESSED */
  207. 50, /* 63 SONYPI_EVENT_ZOOM_OUT_PRESSED */
  208. 51, /* 64 SONYPI_EVENT_CD_EJECT_PRESSED */
  209. 52, /* 65 SONYPI_EVENT_MODEKEY_PRESSED */
  210. 53, /* 66 SONYPI_EVENT_PKEY_P4 */
  211. 54, /* 67 SONYPI_EVENT_PKEY_P5 */
  212. 55, /* 68 SONYPI_EVENT_SETTINGKEY_PRESSED */
  213. 56, /* 69 SONYPI_EVENT_VOLUME_INC_PRESSED */
  214. 57, /* 70 SONYPI_EVENT_VOLUME_DEC_PRESSED */
  215. -1, /* 71 SONYPI_EVENT_BRIGHTNESS_PRESSED */
  216. 58, /* 72 SONYPI_EVENT_MEDIA_PRESSED */
  217. };
  218. static int sony_laptop_input_keycode_map[] = {
  219. KEY_CAMERA, /* 0 SONYPI_EVENT_CAPTURE_PRESSED */
  220. KEY_RESERVED, /* 1 SONYPI_EVENT_CAPTURE_RELEASED */
  221. KEY_RESERVED, /* 2 SONYPI_EVENT_CAPTURE_PARTIALPRESSED */
  222. KEY_RESERVED, /* 3 SONYPI_EVENT_CAPTURE_PARTIALRELEASED */
  223. KEY_FN_ESC, /* 4 SONYPI_EVENT_FNKEY_ESC */
  224. KEY_FN_F1, /* 5 SONYPI_EVENT_FNKEY_F1 */
  225. KEY_FN_F2, /* 6 SONYPI_EVENT_FNKEY_F2 */
  226. KEY_FN_F3, /* 7 SONYPI_EVENT_FNKEY_F3 */
  227. KEY_FN_F4, /* 8 SONYPI_EVENT_FNKEY_F4 */
  228. KEY_FN_F5, /* 9 SONYPI_EVENT_FNKEY_F5 */
  229. KEY_FN_F6, /* 10 SONYPI_EVENT_FNKEY_F6 */
  230. KEY_FN_F7, /* 11 SONYPI_EVENT_FNKEY_F7 */
  231. KEY_FN_F8, /* 12 SONYPI_EVENT_FNKEY_F8 */
  232. KEY_FN_F9, /* 13 SONYPI_EVENT_FNKEY_F9 */
  233. KEY_FN_F10, /* 14 SONYPI_EVENT_FNKEY_F10 */
  234. KEY_FN_F11, /* 15 SONYPI_EVENT_FNKEY_F11 */
  235. KEY_FN_F12, /* 16 SONYPI_EVENT_FNKEY_F12 */
  236. KEY_FN_F1, /* 17 SONYPI_EVENT_FNKEY_1 */
  237. KEY_FN_F2, /* 18 SONYPI_EVENT_FNKEY_2 */
  238. KEY_FN_D, /* 19 SONYPI_EVENT_FNKEY_D */
  239. KEY_FN_E, /* 20 SONYPI_EVENT_FNKEY_E */
  240. KEY_FN_F, /* 21 SONYPI_EVENT_FNKEY_F */
  241. KEY_FN_S, /* 22 SONYPI_EVENT_FNKEY_S */
  242. KEY_FN_B, /* 23 SONYPI_EVENT_FNKEY_B */
  243. KEY_BLUETOOTH, /* 24 SONYPI_EVENT_BLUETOOTH_PRESSED */
  244. KEY_PROG1, /* 25 SONYPI_EVENT_PKEY_P1 */
  245. KEY_PROG2, /* 26 SONYPI_EVENT_PKEY_P2 */
  246. KEY_PROG3, /* 27 SONYPI_EVENT_PKEY_P3 */
  247. KEY_BACK, /* 28 SONYPI_EVENT_BACK_PRESSED */
  248. KEY_BLUETOOTH, /* 29 SONYPI_EVENT_BLUETOOTH_ON */
  249. KEY_BLUETOOTH, /* 30 SONYPI_EVENT_BLUETOOTH_OFF */
  250. KEY_HELP, /* 31 SONYPI_EVENT_HELP_PRESSED */
  251. KEY_FN, /* 32 SONYPI_EVENT_FNKEY_ONLY */
  252. KEY_RESERVED, /* 33 SONYPI_EVENT_JOGDIAL_FAST_DOWN */
  253. KEY_RESERVED, /* 34 SONYPI_EVENT_JOGDIAL_FAST_UP */
  254. KEY_RESERVED, /* 35 SONYPI_EVENT_JOGDIAL_FAST_DOWN_PRESSED */
  255. KEY_RESERVED, /* 36 SONYPI_EVENT_JOGDIAL_FAST_UP_PRESSED */
  256. KEY_RESERVED, /* 37 SONYPI_EVENT_JOGDIAL_VFAST_DOWN */
  257. KEY_RESERVED, /* 38 SONYPI_EVENT_JOGDIAL_VFAST_UP */
  258. KEY_RESERVED, /* 39 SONYPI_EVENT_JOGDIAL_VFAST_DOWN_PRESSED */
  259. KEY_RESERVED, /* 40 SONYPI_EVENT_JOGDIAL_VFAST_UP_PRESSED */
  260. KEY_ZOOM, /* 41 SONYPI_EVENT_ZOOM_PRESSED */
  261. BTN_THUMB, /* 42 SONYPI_EVENT_THUMBPHRASE_PRESSED */
  262. KEY_RESERVED, /* 43 SONYPI_EVENT_MEYE_FACE */
  263. KEY_RESERVED, /* 44 SONYPI_EVENT_MEYE_OPPOSITE */
  264. KEY_RESERVED, /* 45 SONYPI_EVENT_MEMORYSTICK_INSERT */
  265. KEY_RESERVED, /* 46 SONYPI_EVENT_MEMORYSTICK_EJECT */
  266. KEY_WLAN, /* 47 SONYPI_EVENT_WIRELESS_ON */
  267. KEY_WLAN, /* 48 SONYPI_EVENT_WIRELESS_OFF */
  268. KEY_ZOOMIN, /* 49 SONYPI_EVENT_ZOOM_IN_PRESSED */
  269. KEY_ZOOMOUT, /* 50 SONYPI_EVENT_ZOOM_OUT_PRESSED */
  270. KEY_EJECTCD, /* 51 SONYPI_EVENT_CD_EJECT_PRESSED */
  271. KEY_F13, /* 52 SONYPI_EVENT_MODEKEY_PRESSED */
  272. KEY_PROG4, /* 53 SONYPI_EVENT_PKEY_P4 */
  273. KEY_F14, /* 54 SONYPI_EVENT_PKEY_P5 */
  274. KEY_F15, /* 55 SONYPI_EVENT_SETTINGKEY_PRESSED */
  275. KEY_VOLUMEUP, /* 56 SONYPI_EVENT_VOLUME_INC_PRESSED */
  276. KEY_VOLUMEDOWN, /* 57 SONYPI_EVENT_VOLUME_DEC_PRESSED */
  277. KEY_MEDIA, /* 58 SONYPI_EVENT_MEDIA_PRESSED */
  278. };
  279. /* release buttons after a short delay if pressed */
  280. static void do_sony_laptop_release_key(struct work_struct *work)
  281. {
  282. struct delayed_work *dwork =
  283. container_of(work, struct delayed_work, work);
  284. struct sony_laptop_keypress kp;
  285. unsigned long flags;
  286. spin_lock_irqsave(&sony_laptop_input.fifo_lock, flags);
  287. if (kfifo_out(&sony_laptop_input.fifo,
  288. (unsigned char *)&kp, sizeof(kp)) == sizeof(kp)) {
  289. input_report_key(kp.dev, kp.key, 0);
  290. input_sync(kp.dev);
  291. }
  292. /* If there is something in the fifo schedule next release. */
  293. if (kfifo_len(&sony_laptop_input.fifo) != 0)
  294. schedule_delayed_work(dwork, msecs_to_jiffies(10));
  295. spin_unlock_irqrestore(&sony_laptop_input.fifo_lock, flags);
  296. }
  297. static DECLARE_DELAYED_WORK(sony_laptop_release_key_work,
  298. do_sony_laptop_release_key);
  299. /* forward event to the input subsystem */
  300. static void sony_laptop_report_input_event(u8 event)
  301. {
  302. struct input_dev *jog_dev = sony_laptop_input.jog_dev;
  303. struct input_dev *key_dev = sony_laptop_input.key_dev;
  304. struct sony_laptop_keypress kp = { NULL };
  305. if (event == SONYPI_EVENT_FNKEY_RELEASED ||
  306. event == SONYPI_EVENT_ANYBUTTON_RELEASED) {
  307. /* Nothing, not all VAIOs generate this event */
  308. return;
  309. }
  310. /* report events */
  311. switch (event) {
  312. /* jog_dev events */
  313. case SONYPI_EVENT_JOGDIAL_UP:
  314. case SONYPI_EVENT_JOGDIAL_UP_PRESSED:
  315. input_report_rel(jog_dev, REL_WHEEL, 1);
  316. input_sync(jog_dev);
  317. return;
  318. case SONYPI_EVENT_JOGDIAL_DOWN:
  319. case SONYPI_EVENT_JOGDIAL_DOWN_PRESSED:
  320. input_report_rel(jog_dev, REL_WHEEL, -1);
  321. input_sync(jog_dev);
  322. return;
  323. /* key_dev events */
  324. case SONYPI_EVENT_JOGDIAL_PRESSED:
  325. kp.key = BTN_MIDDLE;
  326. kp.dev = jog_dev;
  327. break;
  328. default:
  329. if (event >= ARRAY_SIZE(sony_laptop_input_index)) {
  330. dprintk("sony_laptop_report_input_event, event not known: %d\n", event);
  331. break;
  332. }
  333. if (sony_laptop_input_index[event] != -1) {
  334. kp.key = sony_laptop_input_keycode_map[sony_laptop_input_index[event]];
  335. if (kp.key != KEY_UNKNOWN)
  336. kp.dev = key_dev;
  337. }
  338. break;
  339. }
  340. if (kp.dev) {
  341. input_report_key(kp.dev, kp.key, 1);
  342. /* we emit the scancode so we can always remap the key */
  343. input_event(kp.dev, EV_MSC, MSC_SCAN, event);
  344. input_sync(kp.dev);
  345. /* schedule key release */
  346. kfifo_in_locked(&sony_laptop_input.fifo,
  347. (unsigned char *)&kp, sizeof(kp),
  348. &sony_laptop_input.fifo_lock);
  349. schedule_delayed_work(&sony_laptop_release_key_work,
  350. msecs_to_jiffies(10));
  351. } else
  352. dprintk("unknown input event %.2x\n", event);
  353. }
  354. static int sony_laptop_setup_input(struct acpi_device *acpi_device)
  355. {
  356. struct input_dev *jog_dev;
  357. struct input_dev *key_dev;
  358. int i;
  359. int error;
  360. /* don't run again if already initialized */
  361. if (atomic_add_return(1, &sony_laptop_input.users) > 1)
  362. return 0;
  363. /* kfifo */
  364. spin_lock_init(&sony_laptop_input.fifo_lock);
  365. error = kfifo_alloc(&sony_laptop_input.fifo,
  366. SONY_LAPTOP_BUF_SIZE, GFP_KERNEL);
  367. if (error) {
  368. printk(KERN_ERR DRV_PFX "kfifo_alloc failed\n");
  369. goto err_dec_users;
  370. }
  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. cancel_delayed_work_sync(&sony_laptop_release_key_work);
  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. while ((dev_code = *(device_enum->buffer.pointer + i)) != 0xff &&
  1027. i < device_enum->buffer.length) {
  1028. i++;
  1029. dprintk("Radio devices, looking at 0x%.2x\n", dev_code);
  1030. if (dev_code == 0 && !sony_rfkill_devices[SONY_WIFI])
  1031. sony_nc_setup_rfkill(device, SONY_WIFI);
  1032. if (dev_code == 0x10 && !sony_rfkill_devices[SONY_BLUETOOTH])
  1033. sony_nc_setup_rfkill(device, SONY_BLUETOOTH);
  1034. if ((0xf0 & dev_code) == 0x20 &&
  1035. !sony_rfkill_devices[SONY_WWAN])
  1036. sony_nc_setup_rfkill(device, SONY_WWAN);
  1037. if (dev_code == 0x30 && !sony_rfkill_devices[SONY_WIMAX])
  1038. sony_nc_setup_rfkill(device, SONY_WIMAX);
  1039. }
  1040. out_no_enum:
  1041. kfree(buffer.pointer);
  1042. return;
  1043. }
  1044. static int sony_nc_add(struct acpi_device *device)
  1045. {
  1046. acpi_status status;
  1047. int result = 0;
  1048. acpi_handle handle;
  1049. struct sony_nc_value *item;
  1050. printk(KERN_INFO DRV_PFX "%s v%s.\n",
  1051. SONY_NC_DRIVER_NAME, SONY_LAPTOP_DRIVER_VERSION);
  1052. sony_nc_acpi_device = device;
  1053. strcpy(acpi_device_class(device), "sony/hotkey");
  1054. sony_nc_acpi_handle = device->handle;
  1055. /* read device status */
  1056. result = acpi_bus_get_status(device);
  1057. /* bail IFF the above call was successful and the device is not present */
  1058. if (!result && !device->status.present) {
  1059. dprintk("Device not present\n");
  1060. result = -ENODEV;
  1061. goto outwalk;
  1062. }
  1063. if (debug) {
  1064. status = acpi_walk_namespace(ACPI_TYPE_METHOD, sony_nc_acpi_handle,
  1065. 1, sony_walk_callback, NULL, NULL, NULL);
  1066. if (ACPI_FAILURE(status)) {
  1067. printk(KERN_WARNING DRV_PFX "unable to walk acpi resources\n");
  1068. result = -ENODEV;
  1069. goto outwalk;
  1070. }
  1071. }
  1072. if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle, "ECON",
  1073. &handle))) {
  1074. if (acpi_callsetfunc(sony_nc_acpi_handle, "ECON", 1, NULL))
  1075. dprintk("ECON Method failed\n");
  1076. }
  1077. if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle, "SN00",
  1078. &handle))) {
  1079. dprintk("Doing SNC setup\n");
  1080. sony_nc_function_setup(device);
  1081. sony_nc_rfkill_setup(device);
  1082. }
  1083. /* setup input devices and helper fifo */
  1084. result = sony_laptop_setup_input(device);
  1085. if (result) {
  1086. printk(KERN_ERR DRV_PFX
  1087. "Unable to create input devices.\n");
  1088. goto outwalk;
  1089. }
  1090. if (acpi_video_backlight_support()) {
  1091. printk(KERN_INFO DRV_PFX "brightness ignored, must be "
  1092. "controlled by ACPI video driver\n");
  1093. } else if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle, "GBRT",
  1094. &handle))) {
  1095. sony_backlight_device = backlight_device_register("sony", NULL,
  1096. NULL,
  1097. &sony_backlight_ops);
  1098. if (IS_ERR(sony_backlight_device)) {
  1099. printk(KERN_WARNING DRV_PFX "unable to register backlight device\n");
  1100. sony_backlight_device = NULL;
  1101. } else {
  1102. sony_backlight_device->props.brightness =
  1103. sony_backlight_get_brightness
  1104. (sony_backlight_device);
  1105. sony_backlight_device->props.max_brightness =
  1106. SONY_MAX_BRIGHTNESS - 1;
  1107. }
  1108. }
  1109. result = sony_pf_add();
  1110. if (result)
  1111. goto outbacklight;
  1112. /* create sony_pf sysfs attributes related to the SNC device */
  1113. for (item = sony_nc_values; item->name; ++item) {
  1114. if (!debug && item->debug)
  1115. continue;
  1116. /* find the available acpiget as described in the DSDT */
  1117. for (; item->acpiget && *item->acpiget; ++item->acpiget) {
  1118. if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle,
  1119. *item->acpiget,
  1120. &handle))) {
  1121. dprintk("Found %s getter: %s\n",
  1122. item->name, *item->acpiget);
  1123. item->devattr.attr.mode |= S_IRUGO;
  1124. break;
  1125. }
  1126. }
  1127. /* find the available acpiset as described in the DSDT */
  1128. for (; item->acpiset && *item->acpiset; ++item->acpiset) {
  1129. if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle,
  1130. *item->acpiset,
  1131. &handle))) {
  1132. dprintk("Found %s setter: %s\n",
  1133. item->name, *item->acpiset);
  1134. item->devattr.attr.mode |= S_IWUSR;
  1135. break;
  1136. }
  1137. }
  1138. if (item->devattr.attr.mode != 0) {
  1139. result =
  1140. device_create_file(&sony_pf_device->dev,
  1141. &item->devattr);
  1142. if (result)
  1143. goto out_sysfs;
  1144. }
  1145. }
  1146. return 0;
  1147. out_sysfs:
  1148. for (item = sony_nc_values; item->name; ++item) {
  1149. device_remove_file(&sony_pf_device->dev, &item->devattr);
  1150. }
  1151. sony_pf_remove();
  1152. outbacklight:
  1153. if (sony_backlight_device)
  1154. backlight_device_unregister(sony_backlight_device);
  1155. sony_laptop_remove_input();
  1156. outwalk:
  1157. sony_nc_rfkill_cleanup();
  1158. return result;
  1159. }
  1160. static int sony_nc_remove(struct acpi_device *device, int type)
  1161. {
  1162. struct sony_nc_value *item;
  1163. if (sony_backlight_device)
  1164. backlight_device_unregister(sony_backlight_device);
  1165. sony_nc_acpi_device = NULL;
  1166. for (item = sony_nc_values; item->name; ++item) {
  1167. device_remove_file(&sony_pf_device->dev, &item->devattr);
  1168. }
  1169. sony_pf_remove();
  1170. sony_laptop_remove_input();
  1171. sony_nc_rfkill_cleanup();
  1172. dprintk(SONY_NC_DRIVER_NAME " removed.\n");
  1173. return 0;
  1174. }
  1175. static const struct acpi_device_id sony_device_ids[] = {
  1176. {SONY_NC_HID, 0},
  1177. {SONY_PIC_HID, 0},
  1178. {"", 0},
  1179. };
  1180. MODULE_DEVICE_TABLE(acpi, sony_device_ids);
  1181. static const struct acpi_device_id sony_nc_device_ids[] = {
  1182. {SONY_NC_HID, 0},
  1183. {"", 0},
  1184. };
  1185. static struct acpi_driver sony_nc_driver = {
  1186. .name = SONY_NC_DRIVER_NAME,
  1187. .class = SONY_NC_CLASS,
  1188. .ids = sony_nc_device_ids,
  1189. .owner = THIS_MODULE,
  1190. .ops = {
  1191. .add = sony_nc_add,
  1192. .remove = sony_nc_remove,
  1193. .resume = sony_nc_resume,
  1194. .notify = sony_nc_notify,
  1195. },
  1196. };
  1197. /*********** SPIC (SNY6001) Device ***********/
  1198. #define SONYPI_DEVICE_TYPE1 0x00000001
  1199. #define SONYPI_DEVICE_TYPE2 0x00000002
  1200. #define SONYPI_DEVICE_TYPE3 0x00000004
  1201. #define SONYPI_DEVICE_TYPE4 0x00000008
  1202. #define SONYPI_TYPE1_OFFSET 0x04
  1203. #define SONYPI_TYPE2_OFFSET 0x12
  1204. #define SONYPI_TYPE3_OFFSET 0x12
  1205. struct sony_pic_ioport {
  1206. struct acpi_resource_io io1;
  1207. struct acpi_resource_io io2;
  1208. struct list_head list;
  1209. };
  1210. struct sony_pic_irq {
  1211. struct acpi_resource_irq irq;
  1212. struct list_head list;
  1213. };
  1214. struct sonypi_eventtypes {
  1215. u8 data;
  1216. unsigned long mask;
  1217. struct sonypi_event *events;
  1218. };
  1219. struct sony_pic_dev {
  1220. struct acpi_device *acpi_dev;
  1221. struct sony_pic_irq *cur_irq;
  1222. struct sony_pic_ioport *cur_ioport;
  1223. struct list_head interrupts;
  1224. struct list_head ioports;
  1225. struct mutex lock;
  1226. struct sonypi_eventtypes *event_types;
  1227. int (*handle_irq)(const u8, const u8);
  1228. int model;
  1229. u16 evport_offset;
  1230. u8 camera_power;
  1231. u8 bluetooth_power;
  1232. u8 wwan_power;
  1233. };
  1234. static struct sony_pic_dev spic_dev = {
  1235. .interrupts = LIST_HEAD_INIT(spic_dev.interrupts),
  1236. .ioports = LIST_HEAD_INIT(spic_dev.ioports),
  1237. };
  1238. static int spic_drv_registered;
  1239. /* Event masks */
  1240. #define SONYPI_JOGGER_MASK 0x00000001
  1241. #define SONYPI_CAPTURE_MASK 0x00000002
  1242. #define SONYPI_FNKEY_MASK 0x00000004
  1243. #define SONYPI_BLUETOOTH_MASK 0x00000008
  1244. #define SONYPI_PKEY_MASK 0x00000010
  1245. #define SONYPI_BACK_MASK 0x00000020
  1246. #define SONYPI_HELP_MASK 0x00000040
  1247. #define SONYPI_LID_MASK 0x00000080
  1248. #define SONYPI_ZOOM_MASK 0x00000100
  1249. #define SONYPI_THUMBPHRASE_MASK 0x00000200
  1250. #define SONYPI_MEYE_MASK 0x00000400
  1251. #define SONYPI_MEMORYSTICK_MASK 0x00000800
  1252. #define SONYPI_BATTERY_MASK 0x00001000
  1253. #define SONYPI_WIRELESS_MASK 0x00002000
  1254. struct sonypi_event {
  1255. u8 data;
  1256. u8 event;
  1257. };
  1258. /* The set of possible button release events */
  1259. static struct sonypi_event sonypi_releaseev[] = {
  1260. { 0x00, SONYPI_EVENT_ANYBUTTON_RELEASED },
  1261. { 0, 0 }
  1262. };
  1263. /* The set of possible jogger events */
  1264. static struct sonypi_event sonypi_joggerev[] = {
  1265. { 0x1f, SONYPI_EVENT_JOGDIAL_UP },
  1266. { 0x01, SONYPI_EVENT_JOGDIAL_DOWN },
  1267. { 0x5f, SONYPI_EVENT_JOGDIAL_UP_PRESSED },
  1268. { 0x41, SONYPI_EVENT_JOGDIAL_DOWN_PRESSED },
  1269. { 0x1e, SONYPI_EVENT_JOGDIAL_FAST_UP },
  1270. { 0x02, SONYPI_EVENT_JOGDIAL_FAST_DOWN },
  1271. { 0x5e, SONYPI_EVENT_JOGDIAL_FAST_UP_PRESSED },
  1272. { 0x42, SONYPI_EVENT_JOGDIAL_FAST_DOWN_PRESSED },
  1273. { 0x1d, SONYPI_EVENT_JOGDIAL_VFAST_UP },
  1274. { 0x03, SONYPI_EVENT_JOGDIAL_VFAST_DOWN },
  1275. { 0x5d, SONYPI_EVENT_JOGDIAL_VFAST_UP_PRESSED },
  1276. { 0x43, SONYPI_EVENT_JOGDIAL_VFAST_DOWN_PRESSED },
  1277. { 0x40, SONYPI_EVENT_JOGDIAL_PRESSED },
  1278. { 0, 0 }
  1279. };
  1280. /* The set of possible capture button events */
  1281. static struct sonypi_event sonypi_captureev[] = {
  1282. { 0x05, SONYPI_EVENT_CAPTURE_PARTIALPRESSED },
  1283. { 0x07, SONYPI_EVENT_CAPTURE_PRESSED },
  1284. { 0x40, SONYPI_EVENT_CAPTURE_PRESSED },
  1285. { 0x01, SONYPI_EVENT_CAPTURE_PARTIALRELEASED },
  1286. { 0, 0 }
  1287. };
  1288. /* The set of possible fnkeys events */
  1289. static struct sonypi_event sonypi_fnkeyev[] = {
  1290. { 0x10, SONYPI_EVENT_FNKEY_ESC },
  1291. { 0x11, SONYPI_EVENT_FNKEY_F1 },
  1292. { 0x12, SONYPI_EVENT_FNKEY_F2 },
  1293. { 0x13, SONYPI_EVENT_FNKEY_F3 },
  1294. { 0x14, SONYPI_EVENT_FNKEY_F4 },
  1295. { 0x15, SONYPI_EVENT_FNKEY_F5 },
  1296. { 0x16, SONYPI_EVENT_FNKEY_F6 },
  1297. { 0x17, SONYPI_EVENT_FNKEY_F7 },
  1298. { 0x18, SONYPI_EVENT_FNKEY_F8 },
  1299. { 0x19, SONYPI_EVENT_FNKEY_F9 },
  1300. { 0x1a, SONYPI_EVENT_FNKEY_F10 },
  1301. { 0x1b, SONYPI_EVENT_FNKEY_F11 },
  1302. { 0x1c, SONYPI_EVENT_FNKEY_F12 },
  1303. { 0x1f, SONYPI_EVENT_FNKEY_RELEASED },
  1304. { 0x21, SONYPI_EVENT_FNKEY_1 },
  1305. { 0x22, SONYPI_EVENT_FNKEY_2 },
  1306. { 0x31, SONYPI_EVENT_FNKEY_D },
  1307. { 0x32, SONYPI_EVENT_FNKEY_E },
  1308. { 0x33, SONYPI_EVENT_FNKEY_F },
  1309. { 0x34, SONYPI_EVENT_FNKEY_S },
  1310. { 0x35, SONYPI_EVENT_FNKEY_B },
  1311. { 0x36, SONYPI_EVENT_FNKEY_ONLY },
  1312. { 0, 0 }
  1313. };
  1314. /* The set of possible program key events */
  1315. static struct sonypi_event sonypi_pkeyev[] = {
  1316. { 0x01, SONYPI_EVENT_PKEY_P1 },
  1317. { 0x02, SONYPI_EVENT_PKEY_P2 },
  1318. { 0x04, SONYPI_EVENT_PKEY_P3 },
  1319. { 0x20, SONYPI_EVENT_PKEY_P1 },
  1320. { 0, 0 }
  1321. };
  1322. /* The set of possible bluetooth events */
  1323. static struct sonypi_event sonypi_blueev[] = {
  1324. { 0x55, SONYPI_EVENT_BLUETOOTH_PRESSED },
  1325. { 0x59, SONYPI_EVENT_BLUETOOTH_ON },
  1326. { 0x5a, SONYPI_EVENT_BLUETOOTH_OFF },
  1327. { 0, 0 }
  1328. };
  1329. /* The set of possible wireless events */
  1330. static struct sonypi_event sonypi_wlessev[] = {
  1331. { 0x59, SONYPI_EVENT_WIRELESS_ON },
  1332. { 0x5a, SONYPI_EVENT_WIRELESS_OFF },
  1333. { 0, 0 }
  1334. };
  1335. /* The set of possible back button events */
  1336. static struct sonypi_event sonypi_backev[] = {
  1337. { 0x20, SONYPI_EVENT_BACK_PRESSED },
  1338. { 0, 0 }
  1339. };
  1340. /* The set of possible help button events */
  1341. static struct sonypi_event sonypi_helpev[] = {
  1342. { 0x3b, SONYPI_EVENT_HELP_PRESSED },
  1343. { 0, 0 }
  1344. };
  1345. /* The set of possible lid events */
  1346. static struct sonypi_event sonypi_lidev[] = {
  1347. { 0x51, SONYPI_EVENT_LID_CLOSED },
  1348. { 0x50, SONYPI_EVENT_LID_OPENED },
  1349. { 0, 0 }
  1350. };
  1351. /* The set of possible zoom events */
  1352. static struct sonypi_event sonypi_zoomev[] = {
  1353. { 0x39, SONYPI_EVENT_ZOOM_PRESSED },
  1354. { 0x10, SONYPI_EVENT_ZOOM_IN_PRESSED },
  1355. { 0x20, SONYPI_EVENT_ZOOM_OUT_PRESSED },
  1356. { 0x04, SONYPI_EVENT_ZOOM_PRESSED },
  1357. { 0, 0 }
  1358. };
  1359. /* The set of possible thumbphrase events */
  1360. static struct sonypi_event sonypi_thumbphraseev[] = {
  1361. { 0x3a, SONYPI_EVENT_THUMBPHRASE_PRESSED },
  1362. { 0, 0 }
  1363. };
  1364. /* The set of possible motioneye camera events */
  1365. static struct sonypi_event sonypi_meyeev[] = {
  1366. { 0x00, SONYPI_EVENT_MEYE_FACE },
  1367. { 0x01, SONYPI_EVENT_MEYE_OPPOSITE },
  1368. { 0, 0 }
  1369. };
  1370. /* The set of possible memorystick events */
  1371. static struct sonypi_event sonypi_memorystickev[] = {
  1372. { 0x53, SONYPI_EVENT_MEMORYSTICK_INSERT },
  1373. { 0x54, SONYPI_EVENT_MEMORYSTICK_EJECT },
  1374. { 0, 0 }
  1375. };
  1376. /* The set of possible battery events */
  1377. static struct sonypi_event sonypi_batteryev[] = {
  1378. { 0x20, SONYPI_EVENT_BATTERY_INSERT },
  1379. { 0x30, SONYPI_EVENT_BATTERY_REMOVE },
  1380. { 0, 0 }
  1381. };
  1382. /* The set of possible volume events */
  1383. static struct sonypi_event sonypi_volumeev[] = {
  1384. { 0x01, SONYPI_EVENT_VOLUME_INC_PRESSED },
  1385. { 0x02, SONYPI_EVENT_VOLUME_DEC_PRESSED },
  1386. { 0, 0 }
  1387. };
  1388. /* The set of possible brightness events */
  1389. static struct sonypi_event sonypi_brightnessev[] = {
  1390. { 0x80, SONYPI_EVENT_BRIGHTNESS_PRESSED },
  1391. { 0, 0 }
  1392. };
  1393. static struct sonypi_eventtypes type1_events[] = {
  1394. { 0, 0xffffffff, sonypi_releaseev },
  1395. { 0x70, SONYPI_MEYE_MASK, sonypi_meyeev },
  1396. { 0x30, SONYPI_LID_MASK, sonypi_lidev },
  1397. { 0x60, SONYPI_CAPTURE_MASK, sonypi_captureev },
  1398. { 0x10, SONYPI_JOGGER_MASK, sonypi_joggerev },
  1399. { 0x20, SONYPI_FNKEY_MASK, sonypi_fnkeyev },
  1400. { 0x30, SONYPI_BLUETOOTH_MASK, sonypi_blueev },
  1401. { 0x40, SONYPI_PKEY_MASK, sonypi_pkeyev },
  1402. { 0x30, SONYPI_MEMORYSTICK_MASK, sonypi_memorystickev },
  1403. { 0x40, SONYPI_BATTERY_MASK, sonypi_batteryev },
  1404. { 0 },
  1405. };
  1406. static struct sonypi_eventtypes type2_events[] = {
  1407. { 0, 0xffffffff, sonypi_releaseev },
  1408. { 0x38, SONYPI_LID_MASK, sonypi_lidev },
  1409. { 0x11, SONYPI_JOGGER_MASK, sonypi_joggerev },
  1410. { 0x61, SONYPI_CAPTURE_MASK, sonypi_captureev },
  1411. { 0x21, SONYPI_FNKEY_MASK, sonypi_fnkeyev },
  1412. { 0x31, SONYPI_BLUETOOTH_MASK, sonypi_blueev },
  1413. { 0x08, SONYPI_PKEY_MASK, sonypi_pkeyev },
  1414. { 0x11, SONYPI_BACK_MASK, sonypi_backev },
  1415. { 0x21, SONYPI_HELP_MASK, sonypi_helpev },
  1416. { 0x21, SONYPI_ZOOM_MASK, sonypi_zoomev },
  1417. { 0x20, SONYPI_THUMBPHRASE_MASK, sonypi_thumbphraseev },
  1418. { 0x31, SONYPI_MEMORYSTICK_MASK, sonypi_memorystickev },
  1419. { 0x41, SONYPI_BATTERY_MASK, sonypi_batteryev },
  1420. { 0x31, SONYPI_PKEY_MASK, sonypi_pkeyev },
  1421. { 0 },
  1422. };
  1423. static struct sonypi_eventtypes type3_events[] = {
  1424. { 0, 0xffffffff, sonypi_releaseev },
  1425. { 0x21, SONYPI_FNKEY_MASK, sonypi_fnkeyev },
  1426. { 0x31, SONYPI_WIRELESS_MASK, sonypi_wlessev },
  1427. { 0x31, SONYPI_MEMORYSTICK_MASK, sonypi_memorystickev },
  1428. { 0x41, SONYPI_BATTERY_MASK, sonypi_batteryev },
  1429. { 0x31, SONYPI_PKEY_MASK, sonypi_pkeyev },
  1430. { 0x05, SONYPI_PKEY_MASK, sonypi_pkeyev },
  1431. { 0x05, SONYPI_ZOOM_MASK, sonypi_zoomev },
  1432. { 0x05, SONYPI_CAPTURE_MASK, sonypi_captureev },
  1433. { 0x05, SONYPI_PKEY_MASK, sonypi_volumeev },
  1434. { 0x05, SONYPI_PKEY_MASK, sonypi_brightnessev },
  1435. { 0 },
  1436. };
  1437. /* low level spic calls */
  1438. #define ITERATIONS_LONG 10000
  1439. #define ITERATIONS_SHORT 10
  1440. #define wait_on_command(command, iterations) { \
  1441. unsigned int n = iterations; \
  1442. while (--n && (command)) \
  1443. udelay(1); \
  1444. if (!n) \
  1445. dprintk("command failed at %s : %s (line %d)\n", \
  1446. __FILE__, __func__, __LINE__); \
  1447. }
  1448. static u8 sony_pic_call1(u8 dev)
  1449. {
  1450. u8 v1, v2;
  1451. wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2,
  1452. ITERATIONS_LONG);
  1453. outb(dev, spic_dev.cur_ioport->io1.minimum + 4);
  1454. v1 = inb_p(spic_dev.cur_ioport->io1.minimum + 4);
  1455. v2 = inb_p(spic_dev.cur_ioport->io1.minimum);
  1456. dprintk("sony_pic_call1(0x%.2x): 0x%.4x\n", dev, (v2 << 8) | v1);
  1457. return v2;
  1458. }
  1459. static u8 sony_pic_call2(u8 dev, u8 fn)
  1460. {
  1461. u8 v1;
  1462. wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2,
  1463. ITERATIONS_LONG);
  1464. outb(dev, spic_dev.cur_ioport->io1.minimum + 4);
  1465. wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2,
  1466. ITERATIONS_LONG);
  1467. outb(fn, spic_dev.cur_ioport->io1.minimum);
  1468. v1 = inb_p(spic_dev.cur_ioport->io1.minimum);
  1469. dprintk("sony_pic_call2(0x%.2x - 0x%.2x): 0x%.4x\n", dev, fn, v1);
  1470. return v1;
  1471. }
  1472. static u8 sony_pic_call3(u8 dev, u8 fn, u8 v)
  1473. {
  1474. u8 v1;
  1475. wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2, ITERATIONS_LONG);
  1476. outb(dev, spic_dev.cur_ioport->io1.minimum + 4);
  1477. wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2, ITERATIONS_LONG);
  1478. outb(fn, spic_dev.cur_ioport->io1.minimum);
  1479. wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2, ITERATIONS_LONG);
  1480. outb(v, spic_dev.cur_ioport->io1.minimum);
  1481. v1 = inb_p(spic_dev.cur_ioport->io1.minimum);
  1482. dprintk("sony_pic_call3(0x%.2x - 0x%.2x - 0x%.2x): 0x%.4x\n",
  1483. dev, fn, v, v1);
  1484. return v1;
  1485. }
  1486. /*
  1487. * minidrivers for SPIC models
  1488. */
  1489. static int type3_handle_irq(const u8 data_mask, const u8 ev)
  1490. {
  1491. /*
  1492. * 0x31 could mean we have to take some extra action and wait for
  1493. * the next irq for some Type3 models, it will generate a new
  1494. * irq and we can read new data from the device:
  1495. * - 0x5c and 0x5f requires 0xA0
  1496. * - 0x61 requires 0xB3
  1497. */
  1498. if (data_mask == 0x31) {
  1499. if (ev == 0x5c || ev == 0x5f)
  1500. sony_pic_call1(0xA0);
  1501. else if (ev == 0x61)
  1502. sony_pic_call1(0xB3);
  1503. return 0;
  1504. }
  1505. return 1;
  1506. }
  1507. static void sony_pic_detect_device_type(struct sony_pic_dev *dev)
  1508. {
  1509. struct pci_dev *pcidev;
  1510. pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
  1511. PCI_DEVICE_ID_INTEL_82371AB_3, NULL);
  1512. if (pcidev) {
  1513. dev->model = SONYPI_DEVICE_TYPE1;
  1514. dev->evport_offset = SONYPI_TYPE1_OFFSET;
  1515. dev->event_types = type1_events;
  1516. goto out;
  1517. }
  1518. pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
  1519. PCI_DEVICE_ID_INTEL_ICH6_1, NULL);
  1520. if (pcidev) {
  1521. dev->model = SONYPI_DEVICE_TYPE2;
  1522. dev->evport_offset = SONYPI_TYPE2_OFFSET;
  1523. dev->event_types = type2_events;
  1524. goto out;
  1525. }
  1526. pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
  1527. PCI_DEVICE_ID_INTEL_ICH7_1, NULL);
  1528. if (pcidev) {
  1529. dev->model = SONYPI_DEVICE_TYPE3;
  1530. dev->handle_irq = type3_handle_irq;
  1531. dev->evport_offset = SONYPI_TYPE3_OFFSET;
  1532. dev->event_types = type3_events;
  1533. goto out;
  1534. }
  1535. pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
  1536. PCI_DEVICE_ID_INTEL_ICH8_4, NULL);
  1537. if (pcidev) {
  1538. dev->model = SONYPI_DEVICE_TYPE3;
  1539. dev->handle_irq = type3_handle_irq;
  1540. dev->evport_offset = SONYPI_TYPE3_OFFSET;
  1541. dev->event_types = type3_events;
  1542. goto out;
  1543. }
  1544. pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
  1545. PCI_DEVICE_ID_INTEL_ICH9_1, NULL);
  1546. if (pcidev) {
  1547. dev->model = SONYPI_DEVICE_TYPE3;
  1548. dev->handle_irq = type3_handle_irq;
  1549. dev->evport_offset = SONYPI_TYPE3_OFFSET;
  1550. dev->event_types = type3_events;
  1551. goto out;
  1552. }
  1553. /* default */
  1554. dev->model = SONYPI_DEVICE_TYPE2;
  1555. dev->evport_offset = SONYPI_TYPE2_OFFSET;
  1556. dev->event_types = type2_events;
  1557. out:
  1558. if (pcidev)
  1559. pci_dev_put(pcidev);
  1560. printk(KERN_INFO DRV_PFX "detected Type%d model\n",
  1561. dev->model == SONYPI_DEVICE_TYPE1 ? 1 :
  1562. dev->model == SONYPI_DEVICE_TYPE2 ? 2 : 3);
  1563. }
  1564. /* camera tests and poweron/poweroff */
  1565. #define SONYPI_CAMERA_PICTURE 5
  1566. #define SONYPI_CAMERA_CONTROL 0x10
  1567. #define SONYPI_CAMERA_BRIGHTNESS 0
  1568. #define SONYPI_CAMERA_CONTRAST 1
  1569. #define SONYPI_CAMERA_HUE 2
  1570. #define SONYPI_CAMERA_COLOR 3
  1571. #define SONYPI_CAMERA_SHARPNESS 4
  1572. #define SONYPI_CAMERA_EXPOSURE_MASK 0xC
  1573. #define SONYPI_CAMERA_WHITE_BALANCE_MASK 0x3
  1574. #define SONYPI_CAMERA_PICTURE_MODE_MASK 0x30
  1575. #define SONYPI_CAMERA_MUTE_MASK 0x40
  1576. /* the rest don't need a loop until not 0xff */
  1577. #define SONYPI_CAMERA_AGC 6
  1578. #define SONYPI_CAMERA_AGC_MASK 0x30
  1579. #define SONYPI_CAMERA_SHUTTER_MASK 0x7
  1580. #define SONYPI_CAMERA_SHUTDOWN_REQUEST 7
  1581. #define SONYPI_CAMERA_CONTROL 0x10
  1582. #define SONYPI_CAMERA_STATUS 7
  1583. #define SONYPI_CAMERA_STATUS_READY 0x2
  1584. #define SONYPI_CAMERA_STATUS_POSITION 0x4
  1585. #define SONYPI_DIRECTION_BACKWARDS 0x4
  1586. #define SONYPI_CAMERA_REVISION 8
  1587. #define SONYPI_CAMERA_ROMVERSION 9
  1588. static int __sony_pic_camera_ready(void)
  1589. {
  1590. u8 v;
  1591. v = sony_pic_call2(0x8f, SONYPI_CAMERA_STATUS);
  1592. return (v != 0xff && (v & SONYPI_CAMERA_STATUS_READY));
  1593. }
  1594. static int __sony_pic_camera_off(void)
  1595. {
  1596. if (!camera) {
  1597. printk(KERN_WARNING DRV_PFX "camera control not enabled\n");
  1598. return -ENODEV;
  1599. }
  1600. wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_PICTURE,
  1601. SONYPI_CAMERA_MUTE_MASK),
  1602. ITERATIONS_SHORT);
  1603. if (spic_dev.camera_power) {
  1604. sony_pic_call2(0x91, 0);
  1605. spic_dev.camera_power = 0;
  1606. }
  1607. return 0;
  1608. }
  1609. static int __sony_pic_camera_on(void)
  1610. {
  1611. int i, j, x;
  1612. if (!camera) {
  1613. printk(KERN_WARNING DRV_PFX "camera control not enabled\n");
  1614. return -ENODEV;
  1615. }
  1616. if (spic_dev.camera_power)
  1617. return 0;
  1618. for (j = 5; j > 0; j--) {
  1619. for (x = 0; x < 100 && sony_pic_call2(0x91, 0x1); x++)
  1620. msleep(10);
  1621. sony_pic_call1(0x93);
  1622. for (i = 400; i > 0; i--) {
  1623. if (__sony_pic_camera_ready())
  1624. break;
  1625. msleep(10);
  1626. }
  1627. if (i)
  1628. break;
  1629. }
  1630. if (j == 0) {
  1631. printk(KERN_WARNING DRV_PFX "failed to power on camera\n");
  1632. return -ENODEV;
  1633. }
  1634. wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_CONTROL,
  1635. 0x5a),
  1636. ITERATIONS_SHORT);
  1637. spic_dev.camera_power = 1;
  1638. return 0;
  1639. }
  1640. /* External camera command (exported to the motion eye v4l driver) */
  1641. int sony_pic_camera_command(int command, u8 value)
  1642. {
  1643. if (!camera)
  1644. return -EIO;
  1645. mutex_lock(&spic_dev.lock);
  1646. switch (command) {
  1647. case SONY_PIC_COMMAND_SETCAMERA:
  1648. if (value)
  1649. __sony_pic_camera_on();
  1650. else
  1651. __sony_pic_camera_off();
  1652. break;
  1653. case SONY_PIC_COMMAND_SETCAMERABRIGHTNESS:
  1654. wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_BRIGHTNESS, value),
  1655. ITERATIONS_SHORT);
  1656. break;
  1657. case SONY_PIC_COMMAND_SETCAMERACONTRAST:
  1658. wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_CONTRAST, value),
  1659. ITERATIONS_SHORT);
  1660. break;
  1661. case SONY_PIC_COMMAND_SETCAMERAHUE:
  1662. wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_HUE, value),
  1663. ITERATIONS_SHORT);
  1664. break;
  1665. case SONY_PIC_COMMAND_SETCAMERACOLOR:
  1666. wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_COLOR, value),
  1667. ITERATIONS_SHORT);
  1668. break;
  1669. case SONY_PIC_COMMAND_SETCAMERASHARPNESS:
  1670. wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_SHARPNESS, value),
  1671. ITERATIONS_SHORT);
  1672. break;
  1673. case SONY_PIC_COMMAND_SETCAMERAPICTURE:
  1674. wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_PICTURE, value),
  1675. ITERATIONS_SHORT);
  1676. break;
  1677. case SONY_PIC_COMMAND_SETCAMERAAGC:
  1678. wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_AGC, value),
  1679. ITERATIONS_SHORT);
  1680. break;
  1681. default:
  1682. printk(KERN_ERR DRV_PFX "sony_pic_camera_command invalid: %d\n",
  1683. command);
  1684. break;
  1685. }
  1686. mutex_unlock(&spic_dev.lock);
  1687. return 0;
  1688. }
  1689. EXPORT_SYMBOL(sony_pic_camera_command);
  1690. /* gprs/edge modem (SZ460N and SZ210P), thanks to Joshua Wise */
  1691. static void __sony_pic_set_wwanpower(u8 state)
  1692. {
  1693. state = !!state;
  1694. if (spic_dev.wwan_power == state)
  1695. return;
  1696. sony_pic_call2(0xB0, state);
  1697. sony_pic_call1(0x82);
  1698. spic_dev.wwan_power = state;
  1699. }
  1700. static ssize_t sony_pic_wwanpower_store(struct device *dev,
  1701. struct device_attribute *attr,
  1702. const char *buffer, size_t count)
  1703. {
  1704. unsigned long value;
  1705. if (count > 31)
  1706. return -EINVAL;
  1707. value = simple_strtoul(buffer, NULL, 10);
  1708. mutex_lock(&spic_dev.lock);
  1709. __sony_pic_set_wwanpower(value);
  1710. mutex_unlock(&spic_dev.lock);
  1711. return count;
  1712. }
  1713. static ssize_t sony_pic_wwanpower_show(struct device *dev,
  1714. struct device_attribute *attr, char *buffer)
  1715. {
  1716. ssize_t count;
  1717. mutex_lock(&spic_dev.lock);
  1718. count = snprintf(buffer, PAGE_SIZE, "%d\n", spic_dev.wwan_power);
  1719. mutex_unlock(&spic_dev.lock);
  1720. return count;
  1721. }
  1722. /* bluetooth subsystem power state */
  1723. static void __sony_pic_set_bluetoothpower(u8 state)
  1724. {
  1725. state = !!state;
  1726. if (spic_dev.bluetooth_power == state)
  1727. return;
  1728. sony_pic_call2(0x96, state);
  1729. sony_pic_call1(0x82);
  1730. spic_dev.bluetooth_power = state;
  1731. }
  1732. static ssize_t sony_pic_bluetoothpower_store(struct device *dev,
  1733. struct device_attribute *attr,
  1734. const char *buffer, size_t count)
  1735. {
  1736. unsigned long value;
  1737. if (count > 31)
  1738. return -EINVAL;
  1739. value = simple_strtoul(buffer, NULL, 10);
  1740. mutex_lock(&spic_dev.lock);
  1741. __sony_pic_set_bluetoothpower(value);
  1742. mutex_unlock(&spic_dev.lock);
  1743. return count;
  1744. }
  1745. static ssize_t sony_pic_bluetoothpower_show(struct device *dev,
  1746. struct device_attribute *attr, char *buffer)
  1747. {
  1748. ssize_t count = 0;
  1749. mutex_lock(&spic_dev.lock);
  1750. count = snprintf(buffer, PAGE_SIZE, "%d\n", spic_dev.bluetooth_power);
  1751. mutex_unlock(&spic_dev.lock);
  1752. return count;
  1753. }
  1754. /* fan speed */
  1755. /* FAN0 information (reverse engineered from ACPI tables) */
  1756. #define SONY_PIC_FAN0_STATUS 0x93
  1757. static int sony_pic_set_fanspeed(unsigned long value)
  1758. {
  1759. return ec_write(SONY_PIC_FAN0_STATUS, value);
  1760. }
  1761. static int sony_pic_get_fanspeed(u8 *value)
  1762. {
  1763. return ec_read(SONY_PIC_FAN0_STATUS, value);
  1764. }
  1765. static ssize_t sony_pic_fanspeed_store(struct device *dev,
  1766. struct device_attribute *attr,
  1767. const char *buffer, size_t count)
  1768. {
  1769. unsigned long value;
  1770. if (count > 31)
  1771. return -EINVAL;
  1772. value = simple_strtoul(buffer, NULL, 10);
  1773. if (sony_pic_set_fanspeed(value))
  1774. return -EIO;
  1775. return count;
  1776. }
  1777. static ssize_t sony_pic_fanspeed_show(struct device *dev,
  1778. struct device_attribute *attr, char *buffer)
  1779. {
  1780. u8 value = 0;
  1781. if (sony_pic_get_fanspeed(&value))
  1782. return -EIO;
  1783. return snprintf(buffer, PAGE_SIZE, "%d\n", value);
  1784. }
  1785. #define SPIC_ATTR(_name, _mode) \
  1786. struct device_attribute spic_attr_##_name = __ATTR(_name, \
  1787. _mode, sony_pic_## _name ##_show, \
  1788. sony_pic_## _name ##_store)
  1789. static SPIC_ATTR(bluetoothpower, 0644);
  1790. static SPIC_ATTR(wwanpower, 0644);
  1791. static SPIC_ATTR(fanspeed, 0644);
  1792. static struct attribute *spic_attributes[] = {
  1793. &spic_attr_bluetoothpower.attr,
  1794. &spic_attr_wwanpower.attr,
  1795. &spic_attr_fanspeed.attr,
  1796. NULL
  1797. };
  1798. static struct attribute_group spic_attribute_group = {
  1799. .attrs = spic_attributes
  1800. };
  1801. /******** SONYPI compatibility **********/
  1802. #ifdef CONFIG_SONYPI_COMPAT
  1803. /* battery / brightness / temperature addresses */
  1804. #define SONYPI_BAT_FLAGS 0x81
  1805. #define SONYPI_LCD_LIGHT 0x96
  1806. #define SONYPI_BAT1_PCTRM 0xa0
  1807. #define SONYPI_BAT1_LEFT 0xa2
  1808. #define SONYPI_BAT1_MAXRT 0xa4
  1809. #define SONYPI_BAT2_PCTRM 0xa8
  1810. #define SONYPI_BAT2_LEFT 0xaa
  1811. #define SONYPI_BAT2_MAXRT 0xac
  1812. #define SONYPI_BAT1_MAXTK 0xb0
  1813. #define SONYPI_BAT1_FULL 0xb2
  1814. #define SONYPI_BAT2_MAXTK 0xb8
  1815. #define SONYPI_BAT2_FULL 0xba
  1816. #define SONYPI_TEMP_STATUS 0xC1
  1817. struct sonypi_compat_s {
  1818. struct fasync_struct *fifo_async;
  1819. struct kfifo fifo;
  1820. spinlock_t fifo_lock;
  1821. wait_queue_head_t fifo_proc_list;
  1822. atomic_t open_count;
  1823. };
  1824. static struct sonypi_compat_s sonypi_compat = {
  1825. .open_count = ATOMIC_INIT(0),
  1826. };
  1827. static int sonypi_misc_fasync(int fd, struct file *filp, int on)
  1828. {
  1829. return fasync_helper(fd, filp, on, &sonypi_compat.fifo_async);
  1830. }
  1831. static int sonypi_misc_release(struct inode *inode, struct file *file)
  1832. {
  1833. atomic_dec(&sonypi_compat.open_count);
  1834. return 0;
  1835. }
  1836. static int sonypi_misc_open(struct inode *inode, struct file *file)
  1837. {
  1838. /* Flush input queue on first open */
  1839. unsigned long flags;
  1840. spin_lock_irqsave(&sonypi_compat.fifo_lock, flags);
  1841. if (atomic_inc_return(&sonypi_compat.open_count) == 1)
  1842. kfifo_reset(&sonypi_compat.fifo);
  1843. spin_unlock_irqrestore(&sonypi_compat.fifo_lock, flags);
  1844. return 0;
  1845. }
  1846. static ssize_t sonypi_misc_read(struct file *file, char __user *buf,
  1847. size_t count, loff_t *pos)
  1848. {
  1849. ssize_t ret;
  1850. unsigned char c;
  1851. if ((kfifo_len(&sonypi_compat.fifo) == 0) &&
  1852. (file->f_flags & O_NONBLOCK))
  1853. return -EAGAIN;
  1854. ret = wait_event_interruptible(sonypi_compat.fifo_proc_list,
  1855. kfifo_len(&sonypi_compat.fifo) != 0);
  1856. if (ret)
  1857. return ret;
  1858. while (ret < count &&
  1859. (kfifo_out_locked(&sonypi_compat.fifo, &c, sizeof(c),
  1860. &sonypi_compat.fifo_lock) == sizeof(c))) {
  1861. if (put_user(c, buf++))
  1862. return -EFAULT;
  1863. ret++;
  1864. }
  1865. if (ret > 0) {
  1866. struct inode *inode = file->f_path.dentry->d_inode;
  1867. inode->i_atime = current_fs_time(inode->i_sb);
  1868. }
  1869. return ret;
  1870. }
  1871. static unsigned int sonypi_misc_poll(struct file *file, poll_table *wait)
  1872. {
  1873. poll_wait(file, &sonypi_compat.fifo_proc_list, wait);
  1874. if (kfifo_len(&sonypi_compat.fifo))
  1875. return POLLIN | POLLRDNORM;
  1876. return 0;
  1877. }
  1878. static int ec_read16(u8 addr, u16 *value)
  1879. {
  1880. u8 val_lb, val_hb;
  1881. if (ec_read(addr, &val_lb))
  1882. return -1;
  1883. if (ec_read(addr + 1, &val_hb))
  1884. return -1;
  1885. *value = val_lb | (val_hb << 8);
  1886. return 0;
  1887. }
  1888. static long sonypi_misc_ioctl(struct file *fp, unsigned int cmd,
  1889. unsigned long arg)
  1890. {
  1891. int ret = 0;
  1892. void __user *argp = (void __user *)arg;
  1893. u8 val8;
  1894. u16 val16;
  1895. int value;
  1896. mutex_lock(&spic_dev.lock);
  1897. switch (cmd) {
  1898. case SONYPI_IOCGBRT:
  1899. if (sony_backlight_device == NULL) {
  1900. ret = -EIO;
  1901. break;
  1902. }
  1903. if (acpi_callgetfunc(sony_nc_acpi_handle, "GBRT", &value)) {
  1904. ret = -EIO;
  1905. break;
  1906. }
  1907. val8 = ((value & 0xff) - 1) << 5;
  1908. if (copy_to_user(argp, &val8, sizeof(val8)))
  1909. ret = -EFAULT;
  1910. break;
  1911. case SONYPI_IOCSBRT:
  1912. if (sony_backlight_device == NULL) {
  1913. ret = -EIO;
  1914. break;
  1915. }
  1916. if (copy_from_user(&val8, argp, sizeof(val8))) {
  1917. ret = -EFAULT;
  1918. break;
  1919. }
  1920. if (acpi_callsetfunc(sony_nc_acpi_handle, "SBRT",
  1921. (val8 >> 5) + 1, NULL)) {
  1922. ret = -EIO;
  1923. break;
  1924. }
  1925. /* sync the backlight device status */
  1926. sony_backlight_device->props.brightness =
  1927. sony_backlight_get_brightness(sony_backlight_device);
  1928. break;
  1929. case SONYPI_IOCGBAT1CAP:
  1930. if (ec_read16(SONYPI_BAT1_FULL, &val16)) {
  1931. ret = -EIO;
  1932. break;
  1933. }
  1934. if (copy_to_user(argp, &val16, sizeof(val16)))
  1935. ret = -EFAULT;
  1936. break;
  1937. case SONYPI_IOCGBAT1REM:
  1938. if (ec_read16(SONYPI_BAT1_LEFT, &val16)) {
  1939. ret = -EIO;
  1940. break;
  1941. }
  1942. if (copy_to_user(argp, &val16, sizeof(val16)))
  1943. ret = -EFAULT;
  1944. break;
  1945. case SONYPI_IOCGBAT2CAP:
  1946. if (ec_read16(SONYPI_BAT2_FULL, &val16)) {
  1947. ret = -EIO;
  1948. break;
  1949. }
  1950. if (copy_to_user(argp, &val16, sizeof(val16)))
  1951. ret = -EFAULT;
  1952. break;
  1953. case SONYPI_IOCGBAT2REM:
  1954. if (ec_read16(SONYPI_BAT2_LEFT, &val16)) {
  1955. ret = -EIO;
  1956. break;
  1957. }
  1958. if (copy_to_user(argp, &val16, sizeof(val16)))
  1959. ret = -EFAULT;
  1960. break;
  1961. case SONYPI_IOCGBATFLAGS:
  1962. if (ec_read(SONYPI_BAT_FLAGS, &val8)) {
  1963. ret = -EIO;
  1964. break;
  1965. }
  1966. val8 &= 0x07;
  1967. if (copy_to_user(argp, &val8, sizeof(val8)))
  1968. ret = -EFAULT;
  1969. break;
  1970. case SONYPI_IOCGBLUE:
  1971. val8 = spic_dev.bluetooth_power;
  1972. if (copy_to_user(argp, &val8, sizeof(val8)))
  1973. ret = -EFAULT;
  1974. break;
  1975. case SONYPI_IOCSBLUE:
  1976. if (copy_from_user(&val8, argp, sizeof(val8))) {
  1977. ret = -EFAULT;
  1978. break;
  1979. }
  1980. __sony_pic_set_bluetoothpower(val8);
  1981. break;
  1982. /* FAN Controls */
  1983. case SONYPI_IOCGFAN:
  1984. if (sony_pic_get_fanspeed(&val8)) {
  1985. ret = -EIO;
  1986. break;
  1987. }
  1988. if (copy_to_user(argp, &val8, sizeof(val8)))
  1989. ret = -EFAULT;
  1990. break;
  1991. case SONYPI_IOCSFAN:
  1992. if (copy_from_user(&val8, argp, sizeof(val8))) {
  1993. ret = -EFAULT;
  1994. break;
  1995. }
  1996. if (sony_pic_set_fanspeed(val8))
  1997. ret = -EIO;
  1998. break;
  1999. /* GET Temperature (useful under APM) */
  2000. case SONYPI_IOCGTEMP:
  2001. if (ec_read(SONYPI_TEMP_STATUS, &val8)) {
  2002. ret = -EIO;
  2003. break;
  2004. }
  2005. if (copy_to_user(argp, &val8, sizeof(val8)))
  2006. ret = -EFAULT;
  2007. break;
  2008. default:
  2009. ret = -EINVAL;
  2010. }
  2011. mutex_unlock(&spic_dev.lock);
  2012. return ret;
  2013. }
  2014. static const struct file_operations sonypi_misc_fops = {
  2015. .owner = THIS_MODULE,
  2016. .read = sonypi_misc_read,
  2017. .poll = sonypi_misc_poll,
  2018. .open = sonypi_misc_open,
  2019. .release = sonypi_misc_release,
  2020. .fasync = sonypi_misc_fasync,
  2021. .unlocked_ioctl = sonypi_misc_ioctl,
  2022. };
  2023. static struct miscdevice sonypi_misc_device = {
  2024. .minor = MISC_DYNAMIC_MINOR,
  2025. .name = "sonypi",
  2026. .fops = &sonypi_misc_fops,
  2027. };
  2028. static void sonypi_compat_report_event(u8 event)
  2029. {
  2030. kfifo_in_locked(&sonypi_compat.fifo, (unsigned char *)&event,
  2031. sizeof(event), &sonypi_compat.fifo_lock);
  2032. kill_fasync(&sonypi_compat.fifo_async, SIGIO, POLL_IN);
  2033. wake_up_interruptible(&sonypi_compat.fifo_proc_list);
  2034. }
  2035. static int sonypi_compat_init(void)
  2036. {
  2037. int error;
  2038. spin_lock_init(&sonypi_compat.fifo_lock);
  2039. error =
  2040. kfifo_alloc(&sonypi_compat.fifo, SONY_LAPTOP_BUF_SIZE, GFP_KERNEL);
  2041. if (error) {
  2042. printk(KERN_ERR DRV_PFX "kfifo_alloc failed\n");
  2043. return error;
  2044. }
  2045. init_waitqueue_head(&sonypi_compat.fifo_proc_list);
  2046. if (minor != -1)
  2047. sonypi_misc_device.minor = minor;
  2048. error = misc_register(&sonypi_misc_device);
  2049. if (error) {
  2050. printk(KERN_ERR DRV_PFX "misc_register failed\n");
  2051. goto err_free_kfifo;
  2052. }
  2053. if (minor == -1)
  2054. printk(KERN_INFO DRV_PFX "device allocated minor is %d\n",
  2055. sonypi_misc_device.minor);
  2056. return 0;
  2057. err_free_kfifo:
  2058. kfifo_free(&sonypi_compat.fifo);
  2059. return error;
  2060. }
  2061. static void sonypi_compat_exit(void)
  2062. {
  2063. misc_deregister(&sonypi_misc_device);
  2064. kfifo_free(&sonypi_compat.fifo);
  2065. }
  2066. #else
  2067. static int sonypi_compat_init(void) { return 0; }
  2068. static void sonypi_compat_exit(void) { }
  2069. static void sonypi_compat_report_event(u8 event) { }
  2070. #endif /* CONFIG_SONYPI_COMPAT */
  2071. /*
  2072. * ACPI callbacks
  2073. */
  2074. static acpi_status
  2075. sony_pic_read_possible_resource(struct acpi_resource *resource, void *context)
  2076. {
  2077. u32 i;
  2078. struct sony_pic_dev *dev = (struct sony_pic_dev *)context;
  2079. switch (resource->type) {
  2080. case ACPI_RESOURCE_TYPE_START_DEPENDENT:
  2081. {
  2082. /* start IO enumeration */
  2083. struct sony_pic_ioport *ioport = kzalloc(sizeof(*ioport), GFP_KERNEL);
  2084. if (!ioport)
  2085. return AE_ERROR;
  2086. list_add(&ioport->list, &dev->ioports);
  2087. return AE_OK;
  2088. }
  2089. case ACPI_RESOURCE_TYPE_END_DEPENDENT:
  2090. /* end IO enumeration */
  2091. return AE_OK;
  2092. case ACPI_RESOURCE_TYPE_IRQ:
  2093. {
  2094. struct acpi_resource_irq *p = &resource->data.irq;
  2095. struct sony_pic_irq *interrupt = NULL;
  2096. if (!p || !p->interrupt_count) {
  2097. /*
  2098. * IRQ descriptors may have no IRQ# bits set,
  2099. * particularly those those w/ _STA disabled
  2100. */
  2101. dprintk("Blank IRQ resource\n");
  2102. return AE_OK;
  2103. }
  2104. for (i = 0; i < p->interrupt_count; i++) {
  2105. if (!p->interrupts[i]) {
  2106. printk(KERN_WARNING DRV_PFX
  2107. "Invalid IRQ %d\n",
  2108. p->interrupts[i]);
  2109. continue;
  2110. }
  2111. interrupt = kzalloc(sizeof(*interrupt),
  2112. GFP_KERNEL);
  2113. if (!interrupt)
  2114. return AE_ERROR;
  2115. list_add(&interrupt->list, &dev->interrupts);
  2116. interrupt->irq.triggering = p->triggering;
  2117. interrupt->irq.polarity = p->polarity;
  2118. interrupt->irq.sharable = p->sharable;
  2119. interrupt->irq.interrupt_count = 1;
  2120. interrupt->irq.interrupts[0] = p->interrupts[i];
  2121. }
  2122. return AE_OK;
  2123. }
  2124. case ACPI_RESOURCE_TYPE_IO:
  2125. {
  2126. struct acpi_resource_io *io = &resource->data.io;
  2127. struct sony_pic_ioport *ioport =
  2128. list_first_entry(&dev->ioports, struct sony_pic_ioport, list);
  2129. if (!io) {
  2130. dprintk("Blank IO resource\n");
  2131. return AE_OK;
  2132. }
  2133. if (!ioport->io1.minimum) {
  2134. memcpy(&ioport->io1, io, sizeof(*io));
  2135. dprintk("IO1 at 0x%.4x (0x%.2x)\n", ioport->io1.minimum,
  2136. ioport->io1.address_length);
  2137. }
  2138. else if (!ioport->io2.minimum) {
  2139. memcpy(&ioport->io2, io, sizeof(*io));
  2140. dprintk("IO2 at 0x%.4x (0x%.2x)\n", ioport->io2.minimum,
  2141. ioport->io2.address_length);
  2142. }
  2143. else {
  2144. printk(KERN_ERR DRV_PFX "Unknown SPIC Type, more than 2 IO Ports\n");
  2145. return AE_ERROR;
  2146. }
  2147. return AE_OK;
  2148. }
  2149. default:
  2150. dprintk("Resource %d isn't an IRQ nor an IO port\n",
  2151. resource->type);
  2152. case ACPI_RESOURCE_TYPE_END_TAG:
  2153. return AE_OK;
  2154. }
  2155. return AE_CTRL_TERMINATE;
  2156. }
  2157. static int sony_pic_possible_resources(struct acpi_device *device)
  2158. {
  2159. int result = 0;
  2160. acpi_status status = AE_OK;
  2161. if (!device)
  2162. return -EINVAL;
  2163. /* get device status */
  2164. /* see acpi_pci_link_get_current acpi_pci_link_get_possible */
  2165. dprintk("Evaluating _STA\n");
  2166. result = acpi_bus_get_status(device);
  2167. if (result) {
  2168. printk(KERN_WARNING DRV_PFX "Unable to read status\n");
  2169. goto end;
  2170. }
  2171. if (!device->status.enabled)
  2172. dprintk("Device disabled\n");
  2173. else
  2174. dprintk("Device enabled\n");
  2175. /*
  2176. * Query and parse 'method'
  2177. */
  2178. dprintk("Evaluating %s\n", METHOD_NAME__PRS);
  2179. status = acpi_walk_resources(device->handle, METHOD_NAME__PRS,
  2180. sony_pic_read_possible_resource, &spic_dev);
  2181. if (ACPI_FAILURE(status)) {
  2182. printk(KERN_WARNING DRV_PFX
  2183. "Failure evaluating %s\n",
  2184. METHOD_NAME__PRS);
  2185. result = -ENODEV;
  2186. }
  2187. end:
  2188. return result;
  2189. }
  2190. /*
  2191. * Disable the spic device by calling its _DIS method
  2192. */
  2193. static int sony_pic_disable(struct acpi_device *device)
  2194. {
  2195. acpi_status ret = acpi_evaluate_object(device->handle, "_DIS", NULL,
  2196. NULL);
  2197. if (ACPI_FAILURE(ret) && ret != AE_NOT_FOUND)
  2198. return -ENXIO;
  2199. dprintk("Device disabled\n");
  2200. return 0;
  2201. }
  2202. /*
  2203. * Based on drivers/acpi/pci_link.c:acpi_pci_link_set
  2204. *
  2205. * Call _SRS to set current resources
  2206. */
  2207. static int sony_pic_enable(struct acpi_device *device,
  2208. struct sony_pic_ioport *ioport, struct sony_pic_irq *irq)
  2209. {
  2210. acpi_status status;
  2211. int result = 0;
  2212. /* Type 1 resource layout is:
  2213. * IO
  2214. * IO
  2215. * IRQNoFlags
  2216. * End
  2217. *
  2218. * Type 2 and 3 resource layout is:
  2219. * IO
  2220. * IRQNoFlags
  2221. * End
  2222. */
  2223. struct {
  2224. struct acpi_resource res1;
  2225. struct acpi_resource res2;
  2226. struct acpi_resource res3;
  2227. struct acpi_resource res4;
  2228. } *resource;
  2229. struct acpi_buffer buffer = { 0, NULL };
  2230. if (!ioport || !irq)
  2231. return -EINVAL;
  2232. /* init acpi_buffer */
  2233. resource = kzalloc(sizeof(*resource) + 1, GFP_KERNEL);
  2234. if (!resource)
  2235. return -ENOMEM;
  2236. buffer.length = sizeof(*resource) + 1;
  2237. buffer.pointer = resource;
  2238. /* setup Type 1 resources */
  2239. if (spic_dev.model == SONYPI_DEVICE_TYPE1) {
  2240. /* setup io resources */
  2241. resource->res1.type = ACPI_RESOURCE_TYPE_IO;
  2242. resource->res1.length = sizeof(struct acpi_resource);
  2243. memcpy(&resource->res1.data.io, &ioport->io1,
  2244. sizeof(struct acpi_resource_io));
  2245. resource->res2.type = ACPI_RESOURCE_TYPE_IO;
  2246. resource->res2.length = sizeof(struct acpi_resource);
  2247. memcpy(&resource->res2.data.io, &ioport->io2,
  2248. sizeof(struct acpi_resource_io));
  2249. /* setup irq resource */
  2250. resource->res3.type = ACPI_RESOURCE_TYPE_IRQ;
  2251. resource->res3.length = sizeof(struct acpi_resource);
  2252. memcpy(&resource->res3.data.irq, &irq->irq,
  2253. sizeof(struct acpi_resource_irq));
  2254. /* we requested a shared irq */
  2255. resource->res3.data.irq.sharable = ACPI_SHARED;
  2256. resource->res4.type = ACPI_RESOURCE_TYPE_END_TAG;
  2257. }
  2258. /* setup Type 2/3 resources */
  2259. else {
  2260. /* setup io resource */
  2261. resource->res1.type = ACPI_RESOURCE_TYPE_IO;
  2262. resource->res1.length = sizeof(struct acpi_resource);
  2263. memcpy(&resource->res1.data.io, &ioport->io1,
  2264. sizeof(struct acpi_resource_io));
  2265. /* setup irq resource */
  2266. resource->res2.type = ACPI_RESOURCE_TYPE_IRQ;
  2267. resource->res2.length = sizeof(struct acpi_resource);
  2268. memcpy(&resource->res2.data.irq, &irq->irq,
  2269. sizeof(struct acpi_resource_irq));
  2270. /* we requested a shared irq */
  2271. resource->res2.data.irq.sharable = ACPI_SHARED;
  2272. resource->res3.type = ACPI_RESOURCE_TYPE_END_TAG;
  2273. }
  2274. /* Attempt to set the resource */
  2275. dprintk("Evaluating _SRS\n");
  2276. status = acpi_set_current_resources(device->handle, &buffer);
  2277. /* check for total failure */
  2278. if (ACPI_FAILURE(status)) {
  2279. printk(KERN_ERR DRV_PFX "Error evaluating _SRS\n");
  2280. result = -ENODEV;
  2281. goto end;
  2282. }
  2283. /* Necessary device initializations calls (from sonypi) */
  2284. sony_pic_call1(0x82);
  2285. sony_pic_call2(0x81, 0xff);
  2286. sony_pic_call1(compat ? 0x92 : 0x82);
  2287. end:
  2288. kfree(resource);
  2289. return result;
  2290. }
  2291. /*****************
  2292. *
  2293. * ISR: some event is available
  2294. *
  2295. *****************/
  2296. static irqreturn_t sony_pic_irq(int irq, void *dev_id)
  2297. {
  2298. int i, j;
  2299. u8 ev = 0;
  2300. u8 data_mask = 0;
  2301. u8 device_event = 0;
  2302. struct sony_pic_dev *dev = (struct sony_pic_dev *) dev_id;
  2303. ev = inb_p(dev->cur_ioport->io1.minimum);
  2304. if (dev->cur_ioport->io2.minimum)
  2305. data_mask = inb_p(dev->cur_ioport->io2.minimum);
  2306. else
  2307. data_mask = inb_p(dev->cur_ioport->io1.minimum +
  2308. dev->evport_offset);
  2309. dprintk("event ([%.2x] [%.2x]) at port 0x%.4x(+0x%.2x)\n",
  2310. ev, data_mask, dev->cur_ioport->io1.minimum,
  2311. dev->evport_offset);
  2312. if (ev == 0x00 || ev == 0xff)
  2313. return IRQ_HANDLED;
  2314. for (i = 0; dev->event_types[i].mask; i++) {
  2315. if ((data_mask & dev->event_types[i].data) !=
  2316. dev->event_types[i].data)
  2317. continue;
  2318. if (!(mask & dev->event_types[i].mask))
  2319. continue;
  2320. for (j = 0; dev->event_types[i].events[j].event; j++) {
  2321. if (ev == dev->event_types[i].events[j].data) {
  2322. device_event =
  2323. dev->event_types[i].events[j].event;
  2324. goto found;
  2325. }
  2326. }
  2327. }
  2328. /* Still not able to decode the event try to pass
  2329. * it over to the minidriver
  2330. */
  2331. if (dev->handle_irq && dev->handle_irq(data_mask, ev) == 0)
  2332. return IRQ_HANDLED;
  2333. dprintk("unknown event ([%.2x] [%.2x]) at port 0x%.4x(+0x%.2x)\n",
  2334. ev, data_mask, dev->cur_ioport->io1.minimum,
  2335. dev->evport_offset);
  2336. return IRQ_HANDLED;
  2337. found:
  2338. sony_laptop_report_input_event(device_event);
  2339. acpi_bus_generate_proc_event(dev->acpi_dev, 1, device_event);
  2340. sonypi_compat_report_event(device_event);
  2341. return IRQ_HANDLED;
  2342. }
  2343. /*****************
  2344. *
  2345. * ACPI driver
  2346. *
  2347. *****************/
  2348. static int sony_pic_remove(struct acpi_device *device, int type)
  2349. {
  2350. struct sony_pic_ioport *io, *tmp_io;
  2351. struct sony_pic_irq *irq, *tmp_irq;
  2352. if (sony_pic_disable(device)) {
  2353. printk(KERN_ERR DRV_PFX "Couldn't disable device.\n");
  2354. return -ENXIO;
  2355. }
  2356. free_irq(spic_dev.cur_irq->irq.interrupts[0], &spic_dev);
  2357. release_region(spic_dev.cur_ioport->io1.minimum,
  2358. spic_dev.cur_ioport->io1.address_length);
  2359. if (spic_dev.cur_ioport->io2.minimum)
  2360. release_region(spic_dev.cur_ioport->io2.minimum,
  2361. spic_dev.cur_ioport->io2.address_length);
  2362. sonypi_compat_exit();
  2363. sony_laptop_remove_input();
  2364. /* pf attrs */
  2365. sysfs_remove_group(&sony_pf_device->dev.kobj, &spic_attribute_group);
  2366. sony_pf_remove();
  2367. list_for_each_entry_safe(io, tmp_io, &spic_dev.ioports, list) {
  2368. list_del(&io->list);
  2369. kfree(io);
  2370. }
  2371. list_for_each_entry_safe(irq, tmp_irq, &spic_dev.interrupts, list) {
  2372. list_del(&irq->list);
  2373. kfree(irq);
  2374. }
  2375. spic_dev.cur_ioport = NULL;
  2376. spic_dev.cur_irq = NULL;
  2377. dprintk(SONY_PIC_DRIVER_NAME " removed.\n");
  2378. return 0;
  2379. }
  2380. static int sony_pic_add(struct acpi_device *device)
  2381. {
  2382. int result;
  2383. struct sony_pic_ioport *io, *tmp_io;
  2384. struct sony_pic_irq *irq, *tmp_irq;
  2385. printk(KERN_INFO DRV_PFX "%s v%s.\n",
  2386. SONY_PIC_DRIVER_NAME, SONY_LAPTOP_DRIVER_VERSION);
  2387. spic_dev.acpi_dev = device;
  2388. strcpy(acpi_device_class(device), "sony/hotkey");
  2389. sony_pic_detect_device_type(&spic_dev);
  2390. mutex_init(&spic_dev.lock);
  2391. /* read _PRS resources */
  2392. result = sony_pic_possible_resources(device);
  2393. if (result) {
  2394. printk(KERN_ERR DRV_PFX
  2395. "Unable to read possible resources.\n");
  2396. goto err_free_resources;
  2397. }
  2398. /* setup input devices and helper fifo */
  2399. result = sony_laptop_setup_input(device);
  2400. if (result) {
  2401. printk(KERN_ERR DRV_PFX
  2402. "Unable to create input devices.\n");
  2403. goto err_free_resources;
  2404. }
  2405. if (sonypi_compat_init())
  2406. goto err_remove_input;
  2407. /* request io port */
  2408. list_for_each_entry_reverse(io, &spic_dev.ioports, list) {
  2409. if (request_region(io->io1.minimum, io->io1.address_length,
  2410. "Sony Programable I/O Device")) {
  2411. dprintk("I/O port1: 0x%.4x (0x%.4x) + 0x%.2x\n",
  2412. io->io1.minimum, io->io1.maximum,
  2413. io->io1.address_length);
  2414. /* Type 1 have 2 ioports */
  2415. if (io->io2.minimum) {
  2416. if (request_region(io->io2.minimum,
  2417. io->io2.address_length,
  2418. "Sony Programable I/O Device")) {
  2419. dprintk("I/O port2: 0x%.4x (0x%.4x) + 0x%.2x\n",
  2420. io->io2.minimum, io->io2.maximum,
  2421. io->io2.address_length);
  2422. spic_dev.cur_ioport = io;
  2423. break;
  2424. }
  2425. else {
  2426. dprintk("Unable to get I/O port2: "
  2427. "0x%.4x (0x%.4x) + 0x%.2x\n",
  2428. io->io2.minimum, io->io2.maximum,
  2429. io->io2.address_length);
  2430. release_region(io->io1.minimum,
  2431. io->io1.address_length);
  2432. }
  2433. }
  2434. else {
  2435. spic_dev.cur_ioport = io;
  2436. break;
  2437. }
  2438. }
  2439. }
  2440. if (!spic_dev.cur_ioport) {
  2441. printk(KERN_ERR DRV_PFX "Failed to request_region.\n");
  2442. result = -ENODEV;
  2443. goto err_remove_compat;
  2444. }
  2445. /* request IRQ */
  2446. list_for_each_entry_reverse(irq, &spic_dev.interrupts, list) {
  2447. if (!request_irq(irq->irq.interrupts[0], sony_pic_irq,
  2448. IRQF_DISABLED, "sony-laptop", &spic_dev)) {
  2449. dprintk("IRQ: %d - triggering: %d - "
  2450. "polarity: %d - shr: %d\n",
  2451. irq->irq.interrupts[0],
  2452. irq->irq.triggering,
  2453. irq->irq.polarity,
  2454. irq->irq.sharable);
  2455. spic_dev.cur_irq = irq;
  2456. break;
  2457. }
  2458. }
  2459. if (!spic_dev.cur_irq) {
  2460. printk(KERN_ERR DRV_PFX "Failed to request_irq.\n");
  2461. result = -ENODEV;
  2462. goto err_release_region;
  2463. }
  2464. /* set resource status _SRS */
  2465. result = sony_pic_enable(device, spic_dev.cur_ioport, spic_dev.cur_irq);
  2466. if (result) {
  2467. printk(KERN_ERR DRV_PFX "Couldn't enable device.\n");
  2468. goto err_free_irq;
  2469. }
  2470. spic_dev.bluetooth_power = -1;
  2471. /* create device attributes */
  2472. result = sony_pf_add();
  2473. if (result)
  2474. goto err_disable_device;
  2475. result = sysfs_create_group(&sony_pf_device->dev.kobj, &spic_attribute_group);
  2476. if (result)
  2477. goto err_remove_pf;
  2478. return 0;
  2479. err_remove_pf:
  2480. sony_pf_remove();
  2481. err_disable_device:
  2482. sony_pic_disable(device);
  2483. err_free_irq:
  2484. free_irq(spic_dev.cur_irq->irq.interrupts[0], &spic_dev);
  2485. err_release_region:
  2486. release_region(spic_dev.cur_ioport->io1.minimum,
  2487. spic_dev.cur_ioport->io1.address_length);
  2488. if (spic_dev.cur_ioport->io2.minimum)
  2489. release_region(spic_dev.cur_ioport->io2.minimum,
  2490. spic_dev.cur_ioport->io2.address_length);
  2491. err_remove_compat:
  2492. sonypi_compat_exit();
  2493. err_remove_input:
  2494. sony_laptop_remove_input();
  2495. err_free_resources:
  2496. list_for_each_entry_safe(io, tmp_io, &spic_dev.ioports, list) {
  2497. list_del(&io->list);
  2498. kfree(io);
  2499. }
  2500. list_for_each_entry_safe(irq, tmp_irq, &spic_dev.interrupts, list) {
  2501. list_del(&irq->list);
  2502. kfree(irq);
  2503. }
  2504. spic_dev.cur_ioport = NULL;
  2505. spic_dev.cur_irq = NULL;
  2506. return result;
  2507. }
  2508. static int sony_pic_suspend(struct acpi_device *device, pm_message_t state)
  2509. {
  2510. if (sony_pic_disable(device))
  2511. return -ENXIO;
  2512. return 0;
  2513. }
  2514. static int sony_pic_resume(struct acpi_device *device)
  2515. {
  2516. sony_pic_enable(device, spic_dev.cur_ioport, spic_dev.cur_irq);
  2517. return 0;
  2518. }
  2519. static const struct acpi_device_id sony_pic_device_ids[] = {
  2520. {SONY_PIC_HID, 0},
  2521. {"", 0},
  2522. };
  2523. static struct acpi_driver sony_pic_driver = {
  2524. .name = SONY_PIC_DRIVER_NAME,
  2525. .class = SONY_PIC_CLASS,
  2526. .ids = sony_pic_device_ids,
  2527. .owner = THIS_MODULE,
  2528. .ops = {
  2529. .add = sony_pic_add,
  2530. .remove = sony_pic_remove,
  2531. .suspend = sony_pic_suspend,
  2532. .resume = sony_pic_resume,
  2533. },
  2534. };
  2535. static struct dmi_system_id __initdata sonypi_dmi_table[] = {
  2536. {
  2537. .ident = "Sony Vaio",
  2538. .matches = {
  2539. DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
  2540. DMI_MATCH(DMI_PRODUCT_NAME, "PCG-"),
  2541. },
  2542. },
  2543. {
  2544. .ident = "Sony Vaio",
  2545. .matches = {
  2546. DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
  2547. DMI_MATCH(DMI_PRODUCT_NAME, "VGN-"),
  2548. },
  2549. },
  2550. { }
  2551. };
  2552. static int __init sony_laptop_init(void)
  2553. {
  2554. int result;
  2555. if (!no_spic && dmi_check_system(sonypi_dmi_table)) {
  2556. result = acpi_bus_register_driver(&sony_pic_driver);
  2557. if (result) {
  2558. printk(KERN_ERR DRV_PFX
  2559. "Unable to register SPIC driver.");
  2560. goto out;
  2561. }
  2562. spic_drv_registered = 1;
  2563. }
  2564. result = acpi_bus_register_driver(&sony_nc_driver);
  2565. if (result) {
  2566. printk(KERN_ERR DRV_PFX "Unable to register SNC driver.");
  2567. goto out_unregister_pic;
  2568. }
  2569. return 0;
  2570. out_unregister_pic:
  2571. if (spic_drv_registered)
  2572. acpi_bus_unregister_driver(&sony_pic_driver);
  2573. out:
  2574. return result;
  2575. }
  2576. static void __exit sony_laptop_exit(void)
  2577. {
  2578. acpi_bus_unregister_driver(&sony_nc_driver);
  2579. if (spic_drv_registered)
  2580. acpi_bus_unregister_driver(&sony_pic_driver);
  2581. }
  2582. module_init(sony_laptop_init);
  2583. module_exit(sony_laptop_exit);