sony-laptop.c 78 KB

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