sony-laptop.c 77 KB

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