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