sony-laptop.c 79 KB

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