sony-laptop.c 66 KB

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