sony-laptop.c 76 KB

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