sony-laptop.c 77 KB

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