sony-laptop.c 77 KB

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