sony-laptop.c 69 KB

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