sony-laptop.c 77 KB

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