sony-laptop.c 78 KB

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