sony-laptop.c 66 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540
  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(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. 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 C 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-C"),
  740. },
  741. },
  742. { }
  743. };
  744. /*
  745. * ACPI callbacks
  746. */
  747. static void sony_acpi_notify(acpi_handle handle, u32 event, void *data)
  748. {
  749. struct sony_nc_event *evmap;
  750. u32 ev = event;
  751. int result;
  752. if (ev == 0x92) {
  753. /* read the key pressed from EC.GECR
  754. * A call to SN07 with 0x0202 will do it as well respecting
  755. * the current protocol on different OSes
  756. *
  757. * Note: the path for GECR may be
  758. * \_SB.PCI0.LPCB.EC (C, FE, AR, N and friends)
  759. * \_SB.PCI0.PIB.EC0 (VGN-FR notifications are sent directly, no GECR)
  760. *
  761. * TODO: we may want to do the same for the older GHKE -need
  762. * dmi list- so this snippet may become one more callback.
  763. */
  764. if (acpi_callsetfunc(handle, "SN07", 0x0202, &result) < 0)
  765. dprintk("sony_acpi_notify, unable to decode event 0x%.2x\n", ev);
  766. else
  767. ev = result & 0xFF;
  768. }
  769. if (sony_nc_events)
  770. for (evmap = sony_nc_events; evmap->event; evmap++) {
  771. if (evmap->data == ev) {
  772. ev = evmap->event;
  773. break;
  774. }
  775. }
  776. dprintk("sony_acpi_notify, event: 0x%.2x\n", ev);
  777. sony_laptop_report_input_event(ev);
  778. acpi_bus_generate_event(sony_nc_acpi_device, 1, ev);
  779. }
  780. static acpi_status sony_walk_callback(acpi_handle handle, u32 level,
  781. void *context, void **return_value)
  782. {
  783. struct acpi_namespace_node *node;
  784. union acpi_operand_object *operand;
  785. node = (struct acpi_namespace_node *)handle;
  786. operand = (union acpi_operand_object *)node->object;
  787. printk(KERN_WARNING DRV_PFX "method: name: %4.4s, args %X\n", node->name.ascii,
  788. (u32) operand->method.param_count);
  789. return AE_OK;
  790. }
  791. /*
  792. * ACPI device
  793. */
  794. static int sony_nc_resume(struct acpi_device *device)
  795. {
  796. struct sony_nc_value *item;
  797. for (item = sony_nc_values; item->name; item++) {
  798. int ret;
  799. if (!item->valid)
  800. continue;
  801. ret = acpi_callsetfunc(sony_nc_acpi_handle, *item->acpiset,
  802. item->value, NULL);
  803. if (ret < 0) {
  804. printk("%s: %d\n", __FUNCTION__, ret);
  805. break;
  806. }
  807. }
  808. /* re-initialize models with specific requirements */
  809. dmi_check_system(sony_nc_ids);
  810. return 0;
  811. }
  812. static int sony_nc_add(struct acpi_device *device)
  813. {
  814. acpi_status status;
  815. int result = 0;
  816. acpi_handle handle;
  817. struct sony_nc_value *item;
  818. printk(KERN_INFO DRV_PFX "%s v%s.\n",
  819. SONY_NC_DRIVER_NAME, SONY_LAPTOP_DRIVER_VERSION);
  820. sony_nc_acpi_device = device;
  821. strcpy(acpi_device_class(device), "sony/hotkey");
  822. sony_nc_acpi_handle = device->handle;
  823. /* read device status */
  824. result = acpi_bus_get_status(device);
  825. /* bail IFF the above call was successful and the device is not present */
  826. if (!result && !device->status.present) {
  827. dprintk("Device not present\n");
  828. result = -ENODEV;
  829. goto outwalk;
  830. }
  831. if (debug) {
  832. status = acpi_walk_namespace(ACPI_TYPE_METHOD, sony_nc_acpi_handle,
  833. 1, sony_walk_callback, NULL, NULL);
  834. if (ACPI_FAILURE(status)) {
  835. printk(KERN_WARNING DRV_PFX "unable to walk acpi resources\n");
  836. result = -ENODEV;
  837. goto outwalk;
  838. }
  839. }
  840. /* try to _INI the device if such method exists (ACPI spec 3.0-6.5.1
  841. * should be respected as we already checked for the device presence above */
  842. if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle, METHOD_NAME__INI, &handle))) {
  843. dprintk("Invoking _INI\n");
  844. if (ACPI_FAILURE(acpi_evaluate_object(sony_nc_acpi_handle, METHOD_NAME__INI,
  845. NULL, NULL)))
  846. dprintk("_INI Method failed\n");
  847. }
  848. /* setup input devices and helper fifo */
  849. result = sony_laptop_setup_input();
  850. if (result) {
  851. printk(KERN_ERR DRV_PFX
  852. "Unabe to create input devices.\n");
  853. goto outwalk;
  854. }
  855. status = acpi_install_notify_handler(sony_nc_acpi_handle,
  856. ACPI_DEVICE_NOTIFY,
  857. sony_acpi_notify, NULL);
  858. if (ACPI_FAILURE(status)) {
  859. printk(KERN_WARNING DRV_PFX "unable to install notify handler (%u)\n", status);
  860. result = -ENODEV;
  861. goto outinput;
  862. }
  863. if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle, "GBRT", &handle))) {
  864. sony_backlight_device = backlight_device_register("sony", NULL,
  865. NULL,
  866. &sony_backlight_ops);
  867. if (IS_ERR(sony_backlight_device)) {
  868. printk(KERN_WARNING DRV_PFX "unable to register backlight device\n");
  869. sony_backlight_device = NULL;
  870. } else {
  871. sony_backlight_device->props.brightness =
  872. sony_backlight_get_brightness
  873. (sony_backlight_device);
  874. sony_backlight_device->props.max_brightness =
  875. SONY_MAX_BRIGHTNESS - 1;
  876. }
  877. }
  878. /* initialize models with specific requirements */
  879. dmi_check_system(sony_nc_ids);
  880. result = sony_pf_add();
  881. if (result)
  882. goto outbacklight;
  883. /* create sony_pf sysfs attributes related to the SNC device */
  884. for (item = sony_nc_values; item->name; ++item) {
  885. if (!debug && item->debug)
  886. continue;
  887. /* find the available acpiget as described in the DSDT */
  888. for (; item->acpiget && *item->acpiget; ++item->acpiget) {
  889. if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle,
  890. *item->acpiget,
  891. &handle))) {
  892. dprintk("Found %s getter: %s\n",
  893. item->name, *item->acpiget);
  894. item->devattr.attr.mode |= S_IRUGO;
  895. break;
  896. }
  897. }
  898. /* find the available acpiset as described in the DSDT */
  899. for (; item->acpiset && *item->acpiset; ++item->acpiset) {
  900. if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle,
  901. *item->acpiset,
  902. &handle))) {
  903. dprintk("Found %s setter: %s\n",
  904. item->name, *item->acpiset);
  905. item->devattr.attr.mode |= S_IWUSR;
  906. break;
  907. }
  908. }
  909. if (item->devattr.attr.mode != 0) {
  910. result =
  911. device_create_file(&sony_pf_device->dev,
  912. &item->devattr);
  913. if (result)
  914. goto out_sysfs;
  915. }
  916. }
  917. return 0;
  918. out_sysfs:
  919. for (item = sony_nc_values; item->name; ++item) {
  920. device_remove_file(&sony_pf_device->dev, &item->devattr);
  921. }
  922. sony_pf_remove();
  923. outbacklight:
  924. if (sony_backlight_device)
  925. backlight_device_unregister(sony_backlight_device);
  926. status = acpi_remove_notify_handler(sony_nc_acpi_handle,
  927. ACPI_DEVICE_NOTIFY,
  928. sony_acpi_notify);
  929. if (ACPI_FAILURE(status))
  930. printk(KERN_WARNING DRV_PFX "unable to remove notify handler\n");
  931. outinput:
  932. sony_laptop_remove_input();
  933. outwalk:
  934. return result;
  935. }
  936. static int sony_nc_remove(struct acpi_device *device, int type)
  937. {
  938. acpi_status status;
  939. struct sony_nc_value *item;
  940. if (sony_backlight_device)
  941. backlight_device_unregister(sony_backlight_device);
  942. sony_nc_acpi_device = NULL;
  943. status = acpi_remove_notify_handler(sony_nc_acpi_handle,
  944. ACPI_DEVICE_NOTIFY,
  945. sony_acpi_notify);
  946. if (ACPI_FAILURE(status))
  947. printk(KERN_WARNING DRV_PFX "unable to remove notify handler\n");
  948. for (item = sony_nc_values; item->name; ++item) {
  949. device_remove_file(&sony_pf_device->dev, &item->devattr);
  950. }
  951. sony_pf_remove();
  952. sony_laptop_remove_input();
  953. dprintk(SONY_NC_DRIVER_NAME " removed.\n");
  954. return 0;
  955. }
  956. static struct acpi_driver sony_nc_driver = {
  957. .name = SONY_NC_DRIVER_NAME,
  958. .class = SONY_NC_CLASS,
  959. .ids = SONY_NC_HID,
  960. .owner = THIS_MODULE,
  961. .ops = {
  962. .add = sony_nc_add,
  963. .remove = sony_nc_remove,
  964. .resume = sony_nc_resume,
  965. },
  966. };
  967. /*********** SPIC (SNY6001) Device ***********/
  968. #define SONYPI_DEVICE_TYPE1 0x00000001
  969. #define SONYPI_DEVICE_TYPE2 0x00000002
  970. #define SONYPI_DEVICE_TYPE3 0x00000004
  971. #define SONYPI_TYPE1_OFFSET 0x04
  972. #define SONYPI_TYPE2_OFFSET 0x12
  973. #define SONYPI_TYPE3_OFFSET 0x12
  974. struct sony_pic_ioport {
  975. struct acpi_resource_io io;
  976. struct list_head list;
  977. };
  978. struct sony_pic_irq {
  979. struct acpi_resource_irq irq;
  980. struct list_head list;
  981. };
  982. struct sony_pic_dev {
  983. int model;
  984. u16 evport_offset;
  985. u8 camera_power;
  986. u8 bluetooth_power;
  987. u8 wwan_power;
  988. struct acpi_device *acpi_dev;
  989. struct sony_pic_irq *cur_irq;
  990. struct sony_pic_ioport *cur_ioport;
  991. struct list_head interrupts;
  992. struct list_head ioports;
  993. struct mutex lock;
  994. };
  995. static struct sony_pic_dev spic_dev = {
  996. .interrupts = LIST_HEAD_INIT(spic_dev.interrupts),
  997. .ioports = LIST_HEAD_INIT(spic_dev.ioports),
  998. };
  999. /* Event masks */
  1000. #define SONYPI_JOGGER_MASK 0x00000001
  1001. #define SONYPI_CAPTURE_MASK 0x00000002
  1002. #define SONYPI_FNKEY_MASK 0x00000004
  1003. #define SONYPI_BLUETOOTH_MASK 0x00000008
  1004. #define SONYPI_PKEY_MASK 0x00000010
  1005. #define SONYPI_BACK_MASK 0x00000020
  1006. #define SONYPI_HELP_MASK 0x00000040
  1007. #define SONYPI_LID_MASK 0x00000080
  1008. #define SONYPI_ZOOM_MASK 0x00000100
  1009. #define SONYPI_THUMBPHRASE_MASK 0x00000200
  1010. #define SONYPI_MEYE_MASK 0x00000400
  1011. #define SONYPI_MEMORYSTICK_MASK 0x00000800
  1012. #define SONYPI_BATTERY_MASK 0x00001000
  1013. #define SONYPI_WIRELESS_MASK 0x00002000
  1014. struct sonypi_event {
  1015. u8 data;
  1016. u8 event;
  1017. };
  1018. /* The set of possible button release events */
  1019. static struct sonypi_event sonypi_releaseev[] = {
  1020. { 0x00, SONYPI_EVENT_ANYBUTTON_RELEASED },
  1021. { 0, 0 }
  1022. };
  1023. /* The set of possible jogger events */
  1024. static struct sonypi_event sonypi_joggerev[] = {
  1025. { 0x1f, SONYPI_EVENT_JOGDIAL_UP },
  1026. { 0x01, SONYPI_EVENT_JOGDIAL_DOWN },
  1027. { 0x5f, SONYPI_EVENT_JOGDIAL_UP_PRESSED },
  1028. { 0x41, SONYPI_EVENT_JOGDIAL_DOWN_PRESSED },
  1029. { 0x1e, SONYPI_EVENT_JOGDIAL_FAST_UP },
  1030. { 0x02, SONYPI_EVENT_JOGDIAL_FAST_DOWN },
  1031. { 0x5e, SONYPI_EVENT_JOGDIAL_FAST_UP_PRESSED },
  1032. { 0x42, SONYPI_EVENT_JOGDIAL_FAST_DOWN_PRESSED },
  1033. { 0x1d, SONYPI_EVENT_JOGDIAL_VFAST_UP },
  1034. { 0x03, SONYPI_EVENT_JOGDIAL_VFAST_DOWN },
  1035. { 0x5d, SONYPI_EVENT_JOGDIAL_VFAST_UP_PRESSED },
  1036. { 0x43, SONYPI_EVENT_JOGDIAL_VFAST_DOWN_PRESSED },
  1037. { 0x40, SONYPI_EVENT_JOGDIAL_PRESSED },
  1038. { 0, 0 }
  1039. };
  1040. /* The set of possible capture button events */
  1041. static struct sonypi_event sonypi_captureev[] = {
  1042. { 0x05, SONYPI_EVENT_CAPTURE_PARTIALPRESSED },
  1043. { 0x07, SONYPI_EVENT_CAPTURE_PRESSED },
  1044. { 0x01, SONYPI_EVENT_CAPTURE_PARTIALRELEASED },
  1045. { 0, 0 }
  1046. };
  1047. /* The set of possible fnkeys events */
  1048. static struct sonypi_event sonypi_fnkeyev[] = {
  1049. { 0x10, SONYPI_EVENT_FNKEY_ESC },
  1050. { 0x11, SONYPI_EVENT_FNKEY_F1 },
  1051. { 0x12, SONYPI_EVENT_FNKEY_F2 },
  1052. { 0x13, SONYPI_EVENT_FNKEY_F3 },
  1053. { 0x14, SONYPI_EVENT_FNKEY_F4 },
  1054. { 0x15, SONYPI_EVENT_FNKEY_F5 },
  1055. { 0x16, SONYPI_EVENT_FNKEY_F6 },
  1056. { 0x17, SONYPI_EVENT_FNKEY_F7 },
  1057. { 0x18, SONYPI_EVENT_FNKEY_F8 },
  1058. { 0x19, SONYPI_EVENT_FNKEY_F9 },
  1059. { 0x1a, SONYPI_EVENT_FNKEY_F10 },
  1060. { 0x1b, SONYPI_EVENT_FNKEY_F11 },
  1061. { 0x1c, SONYPI_EVENT_FNKEY_F12 },
  1062. { 0x1f, SONYPI_EVENT_FNKEY_RELEASED },
  1063. { 0x21, SONYPI_EVENT_FNKEY_1 },
  1064. { 0x22, SONYPI_EVENT_FNKEY_2 },
  1065. { 0x31, SONYPI_EVENT_FNKEY_D },
  1066. { 0x32, SONYPI_EVENT_FNKEY_E },
  1067. { 0x33, SONYPI_EVENT_FNKEY_F },
  1068. { 0x34, SONYPI_EVENT_FNKEY_S },
  1069. { 0x35, SONYPI_EVENT_FNKEY_B },
  1070. { 0x36, SONYPI_EVENT_FNKEY_ONLY },
  1071. { 0, 0 }
  1072. };
  1073. /* The set of possible program key events */
  1074. static struct sonypi_event sonypi_pkeyev[] = {
  1075. { 0x01, SONYPI_EVENT_PKEY_P1 },
  1076. { 0x02, SONYPI_EVENT_PKEY_P2 },
  1077. { 0x04, SONYPI_EVENT_PKEY_P3 },
  1078. { 0x5c, SONYPI_EVENT_PKEY_P1 },
  1079. { 0, 0 }
  1080. };
  1081. /* The set of possible bluetooth events */
  1082. static struct sonypi_event sonypi_blueev[] = {
  1083. { 0x55, SONYPI_EVENT_BLUETOOTH_PRESSED },
  1084. { 0x59, SONYPI_EVENT_BLUETOOTH_ON },
  1085. { 0x5a, SONYPI_EVENT_BLUETOOTH_OFF },
  1086. { 0, 0 }
  1087. };
  1088. /* The set of possible wireless events */
  1089. static struct sonypi_event sonypi_wlessev[] = {
  1090. { 0x59, SONYPI_EVENT_WIRELESS_ON },
  1091. { 0x5a, SONYPI_EVENT_WIRELESS_OFF },
  1092. { 0, 0 }
  1093. };
  1094. /* The set of possible back button events */
  1095. static struct sonypi_event sonypi_backev[] = {
  1096. { 0x20, SONYPI_EVENT_BACK_PRESSED },
  1097. { 0, 0 }
  1098. };
  1099. /* The set of possible help button events */
  1100. static struct sonypi_event sonypi_helpev[] = {
  1101. { 0x3b, SONYPI_EVENT_HELP_PRESSED },
  1102. { 0, 0 }
  1103. };
  1104. /* The set of possible lid events */
  1105. static struct sonypi_event sonypi_lidev[] = {
  1106. { 0x51, SONYPI_EVENT_LID_CLOSED },
  1107. { 0x50, SONYPI_EVENT_LID_OPENED },
  1108. { 0, 0 }
  1109. };
  1110. /* The set of possible zoom events */
  1111. static struct sonypi_event sonypi_zoomev[] = {
  1112. { 0x39, SONYPI_EVENT_ZOOM_PRESSED },
  1113. { 0, 0 }
  1114. };
  1115. /* The set of possible thumbphrase events */
  1116. static struct sonypi_event sonypi_thumbphraseev[] = {
  1117. { 0x3a, SONYPI_EVENT_THUMBPHRASE_PRESSED },
  1118. { 0, 0 }
  1119. };
  1120. /* The set of possible motioneye camera events */
  1121. static struct sonypi_event sonypi_meyeev[] = {
  1122. { 0x00, SONYPI_EVENT_MEYE_FACE },
  1123. { 0x01, SONYPI_EVENT_MEYE_OPPOSITE },
  1124. { 0, 0 }
  1125. };
  1126. /* The set of possible memorystick events */
  1127. static struct sonypi_event sonypi_memorystickev[] = {
  1128. { 0x53, SONYPI_EVENT_MEMORYSTICK_INSERT },
  1129. { 0x54, SONYPI_EVENT_MEMORYSTICK_EJECT },
  1130. { 0, 0 }
  1131. };
  1132. /* The set of possible battery events */
  1133. static struct sonypi_event sonypi_batteryev[] = {
  1134. { 0x20, SONYPI_EVENT_BATTERY_INSERT },
  1135. { 0x30, SONYPI_EVENT_BATTERY_REMOVE },
  1136. { 0, 0 }
  1137. };
  1138. static struct sonypi_eventtypes {
  1139. int model;
  1140. u8 data;
  1141. unsigned long mask;
  1142. struct sonypi_event * events;
  1143. } sony_pic_eventtypes[] = {
  1144. { SONYPI_DEVICE_TYPE1, 0, 0xffffffff, sonypi_releaseev },
  1145. { SONYPI_DEVICE_TYPE1, 0x70, SONYPI_MEYE_MASK, sonypi_meyeev },
  1146. { SONYPI_DEVICE_TYPE1, 0x30, SONYPI_LID_MASK, sonypi_lidev },
  1147. { SONYPI_DEVICE_TYPE1, 0x60, SONYPI_CAPTURE_MASK, sonypi_captureev },
  1148. { SONYPI_DEVICE_TYPE1, 0x10, SONYPI_JOGGER_MASK, sonypi_joggerev },
  1149. { SONYPI_DEVICE_TYPE1, 0x20, SONYPI_FNKEY_MASK, sonypi_fnkeyev },
  1150. { SONYPI_DEVICE_TYPE1, 0x30, SONYPI_BLUETOOTH_MASK, sonypi_blueev },
  1151. { SONYPI_DEVICE_TYPE1, 0x40, SONYPI_PKEY_MASK, sonypi_pkeyev },
  1152. { SONYPI_DEVICE_TYPE1, 0x30, SONYPI_MEMORYSTICK_MASK, sonypi_memorystickev },
  1153. { SONYPI_DEVICE_TYPE1, 0x40, SONYPI_BATTERY_MASK, sonypi_batteryev },
  1154. { SONYPI_DEVICE_TYPE2, 0, 0xffffffff, sonypi_releaseev },
  1155. { SONYPI_DEVICE_TYPE2, 0x38, SONYPI_LID_MASK, sonypi_lidev },
  1156. { SONYPI_DEVICE_TYPE2, 0x11, SONYPI_JOGGER_MASK, sonypi_joggerev },
  1157. { SONYPI_DEVICE_TYPE2, 0x61, SONYPI_CAPTURE_MASK, sonypi_captureev },
  1158. { SONYPI_DEVICE_TYPE2, 0x21, SONYPI_FNKEY_MASK, sonypi_fnkeyev },
  1159. { SONYPI_DEVICE_TYPE2, 0x31, SONYPI_BLUETOOTH_MASK, sonypi_blueev },
  1160. { SONYPI_DEVICE_TYPE2, 0x08, SONYPI_PKEY_MASK, sonypi_pkeyev },
  1161. { SONYPI_DEVICE_TYPE2, 0x11, SONYPI_BACK_MASK, sonypi_backev },
  1162. { SONYPI_DEVICE_TYPE2, 0x21, SONYPI_HELP_MASK, sonypi_helpev },
  1163. { SONYPI_DEVICE_TYPE2, 0x21, SONYPI_ZOOM_MASK, sonypi_zoomev },
  1164. { SONYPI_DEVICE_TYPE2, 0x20, SONYPI_THUMBPHRASE_MASK, sonypi_thumbphraseev },
  1165. { SONYPI_DEVICE_TYPE2, 0x31, SONYPI_MEMORYSTICK_MASK, sonypi_memorystickev },
  1166. { SONYPI_DEVICE_TYPE2, 0x41, SONYPI_BATTERY_MASK, sonypi_batteryev },
  1167. { SONYPI_DEVICE_TYPE2, 0x31, SONYPI_PKEY_MASK, sonypi_pkeyev },
  1168. { SONYPI_DEVICE_TYPE3, 0, 0xffffffff, sonypi_releaseev },
  1169. { SONYPI_DEVICE_TYPE3, 0x21, SONYPI_FNKEY_MASK, sonypi_fnkeyev },
  1170. { SONYPI_DEVICE_TYPE3, 0x31, SONYPI_WIRELESS_MASK, sonypi_wlessev },
  1171. { SONYPI_DEVICE_TYPE3, 0x31, SONYPI_MEMORYSTICK_MASK, sonypi_memorystickev },
  1172. { SONYPI_DEVICE_TYPE3, 0x41, SONYPI_BATTERY_MASK, sonypi_batteryev },
  1173. { SONYPI_DEVICE_TYPE3, 0x31, SONYPI_PKEY_MASK, sonypi_pkeyev },
  1174. { 0 }
  1175. };
  1176. static int sony_pic_detect_device_type(void)
  1177. {
  1178. struct pci_dev *pcidev;
  1179. int model = 0;
  1180. if ((pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
  1181. PCI_DEVICE_ID_INTEL_82371AB_3, NULL)))
  1182. model = SONYPI_DEVICE_TYPE1;
  1183. else if ((pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
  1184. PCI_DEVICE_ID_INTEL_ICH6_1, NULL)))
  1185. model = SONYPI_DEVICE_TYPE3;
  1186. else if ((pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
  1187. PCI_DEVICE_ID_INTEL_ICH7_1, NULL)))
  1188. model = SONYPI_DEVICE_TYPE3;
  1189. else
  1190. model = SONYPI_DEVICE_TYPE2;
  1191. if (pcidev)
  1192. pci_dev_put(pcidev);
  1193. printk(KERN_INFO DRV_PFX "detected Type%d model\n",
  1194. model == SONYPI_DEVICE_TYPE1 ? 1 :
  1195. model == SONYPI_DEVICE_TYPE2 ? 2 : 3);
  1196. return model;
  1197. }
  1198. #define ITERATIONS_LONG 10000
  1199. #define ITERATIONS_SHORT 10
  1200. #define wait_on_command(command, iterations) { \
  1201. unsigned int n = iterations; \
  1202. while (--n && (command)) \
  1203. udelay(1); \
  1204. if (!n) \
  1205. dprintk("command failed at %s : %s (line %d)\n", \
  1206. __FILE__, __FUNCTION__, __LINE__); \
  1207. }
  1208. static u8 sony_pic_call1(u8 dev)
  1209. {
  1210. u8 v1, v2;
  1211. wait_on_command(inb_p(spic_dev.cur_ioport->io.minimum + 4) & 2,
  1212. ITERATIONS_LONG);
  1213. outb(dev, spic_dev.cur_ioport->io.minimum + 4);
  1214. v1 = inb_p(spic_dev.cur_ioport->io.minimum + 4);
  1215. v2 = inb_p(spic_dev.cur_ioport->io.minimum);
  1216. dprintk("sony_pic_call1: 0x%.4x\n", (v2 << 8) | v1);
  1217. return v2;
  1218. }
  1219. static u8 sony_pic_call2(u8 dev, u8 fn)
  1220. {
  1221. u8 v1;
  1222. wait_on_command(inb_p(spic_dev.cur_ioport->io.minimum + 4) & 2,
  1223. ITERATIONS_LONG);
  1224. outb(dev, spic_dev.cur_ioport->io.minimum + 4);
  1225. wait_on_command(inb_p(spic_dev.cur_ioport->io.minimum + 4) & 2,
  1226. ITERATIONS_LONG);
  1227. outb(fn, spic_dev.cur_ioport->io.minimum);
  1228. v1 = inb_p(spic_dev.cur_ioport->io.minimum);
  1229. dprintk("sony_pic_call2: 0x%.4x\n", v1);
  1230. return v1;
  1231. }
  1232. static u8 sony_pic_call3(u8 dev, u8 fn, u8 v)
  1233. {
  1234. u8 v1;
  1235. wait_on_command(inb_p(spic_dev.cur_ioport->io.minimum + 4) & 2, ITERATIONS_LONG);
  1236. outb(dev, spic_dev.cur_ioport->io.minimum + 4);
  1237. wait_on_command(inb_p(spic_dev.cur_ioport->io.minimum + 4) & 2, ITERATIONS_LONG);
  1238. outb(fn, spic_dev.cur_ioport->io.minimum);
  1239. wait_on_command(inb_p(spic_dev.cur_ioport->io.minimum + 4) & 2, ITERATIONS_LONG);
  1240. outb(v, spic_dev.cur_ioport->io.minimum);
  1241. v1 = inb_p(spic_dev.cur_ioport->io.minimum);
  1242. dprintk("sony_pic_call3: 0x%.4x\n", v1);
  1243. return v1;
  1244. }
  1245. /* camera tests and poweron/poweroff */
  1246. #define SONYPI_CAMERA_PICTURE 5
  1247. #define SONYPI_CAMERA_CONTROL 0x10
  1248. #define SONYPI_CAMERA_BRIGHTNESS 0
  1249. #define SONYPI_CAMERA_CONTRAST 1
  1250. #define SONYPI_CAMERA_HUE 2
  1251. #define SONYPI_CAMERA_COLOR 3
  1252. #define SONYPI_CAMERA_SHARPNESS 4
  1253. #define SONYPI_CAMERA_EXPOSURE_MASK 0xC
  1254. #define SONYPI_CAMERA_WHITE_BALANCE_MASK 0x3
  1255. #define SONYPI_CAMERA_PICTURE_MODE_MASK 0x30
  1256. #define SONYPI_CAMERA_MUTE_MASK 0x40
  1257. /* the rest don't need a loop until not 0xff */
  1258. #define SONYPI_CAMERA_AGC 6
  1259. #define SONYPI_CAMERA_AGC_MASK 0x30
  1260. #define SONYPI_CAMERA_SHUTTER_MASK 0x7
  1261. #define SONYPI_CAMERA_SHUTDOWN_REQUEST 7
  1262. #define SONYPI_CAMERA_CONTROL 0x10
  1263. #define SONYPI_CAMERA_STATUS 7
  1264. #define SONYPI_CAMERA_STATUS_READY 0x2
  1265. #define SONYPI_CAMERA_STATUS_POSITION 0x4
  1266. #define SONYPI_DIRECTION_BACKWARDS 0x4
  1267. #define SONYPI_CAMERA_REVISION 8
  1268. #define SONYPI_CAMERA_ROMVERSION 9
  1269. static int __sony_pic_camera_ready(void)
  1270. {
  1271. u8 v;
  1272. v = sony_pic_call2(0x8f, SONYPI_CAMERA_STATUS);
  1273. return (v != 0xff && (v & SONYPI_CAMERA_STATUS_READY));
  1274. }
  1275. static int __sony_pic_camera_off(void)
  1276. {
  1277. if (!camera) {
  1278. printk(KERN_WARNING DRV_PFX "camera control not enabled\n");
  1279. return -ENODEV;
  1280. }
  1281. wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_PICTURE,
  1282. SONYPI_CAMERA_MUTE_MASK),
  1283. ITERATIONS_SHORT);
  1284. if (spic_dev.camera_power) {
  1285. sony_pic_call2(0x91, 0);
  1286. spic_dev.camera_power = 0;
  1287. }
  1288. return 0;
  1289. }
  1290. static int __sony_pic_camera_on(void)
  1291. {
  1292. int i, j, x;
  1293. if (!camera) {
  1294. printk(KERN_WARNING DRV_PFX "camera control not enabled\n");
  1295. return -ENODEV;
  1296. }
  1297. if (spic_dev.camera_power)
  1298. return 0;
  1299. for (j = 5; j > 0; j--) {
  1300. for (x = 0; x < 100 && sony_pic_call2(0x91, 0x1); x++)
  1301. msleep(10);
  1302. sony_pic_call1(0x93);
  1303. for (i = 400; i > 0; i--) {
  1304. if (__sony_pic_camera_ready())
  1305. break;
  1306. msleep(10);
  1307. }
  1308. if (i)
  1309. break;
  1310. }
  1311. if (j == 0) {
  1312. printk(KERN_WARNING DRV_PFX "failed to power on camera\n");
  1313. return -ENODEV;
  1314. }
  1315. wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_CONTROL,
  1316. 0x5a),
  1317. ITERATIONS_SHORT);
  1318. spic_dev.camera_power = 1;
  1319. return 0;
  1320. }
  1321. /* External camera command (exported to the motion eye v4l driver) */
  1322. int sony_pic_camera_command(int command, u8 value)
  1323. {
  1324. if (!camera)
  1325. return -EIO;
  1326. mutex_lock(&spic_dev.lock);
  1327. switch (command) {
  1328. case SONY_PIC_COMMAND_SETCAMERA:
  1329. if (value)
  1330. __sony_pic_camera_on();
  1331. else
  1332. __sony_pic_camera_off();
  1333. break;
  1334. case SONY_PIC_COMMAND_SETCAMERABRIGHTNESS:
  1335. wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_BRIGHTNESS, value),
  1336. ITERATIONS_SHORT);
  1337. break;
  1338. case SONY_PIC_COMMAND_SETCAMERACONTRAST:
  1339. wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_CONTRAST, value),
  1340. ITERATIONS_SHORT);
  1341. break;
  1342. case SONY_PIC_COMMAND_SETCAMERAHUE:
  1343. wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_HUE, value),
  1344. ITERATIONS_SHORT);
  1345. break;
  1346. case SONY_PIC_COMMAND_SETCAMERACOLOR:
  1347. wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_COLOR, value),
  1348. ITERATIONS_SHORT);
  1349. break;
  1350. case SONY_PIC_COMMAND_SETCAMERASHARPNESS:
  1351. wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_SHARPNESS, value),
  1352. ITERATIONS_SHORT);
  1353. break;
  1354. case SONY_PIC_COMMAND_SETCAMERAPICTURE:
  1355. wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_PICTURE, value),
  1356. ITERATIONS_SHORT);
  1357. break;
  1358. case SONY_PIC_COMMAND_SETCAMERAAGC:
  1359. wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_AGC, value),
  1360. ITERATIONS_SHORT);
  1361. break;
  1362. default:
  1363. printk(KERN_ERR DRV_PFX "sony_pic_camera_command invalid: %d\n",
  1364. command);
  1365. break;
  1366. }
  1367. mutex_unlock(&spic_dev.lock);
  1368. return 0;
  1369. }
  1370. EXPORT_SYMBOL(sony_pic_camera_command);
  1371. /* gprs/edge modem (SZ460N and SZ210P), thanks to Joshua Wise */
  1372. static void sony_pic_set_wwanpower(u8 state)
  1373. {
  1374. state = !!state;
  1375. mutex_lock(&spic_dev.lock);
  1376. if (spic_dev.wwan_power == state) {
  1377. mutex_unlock(&spic_dev.lock);
  1378. return;
  1379. }
  1380. sony_pic_call2(0xB0, state);
  1381. spic_dev.wwan_power = state;
  1382. mutex_unlock(&spic_dev.lock);
  1383. }
  1384. static ssize_t sony_pic_wwanpower_store(struct device *dev,
  1385. struct device_attribute *attr,
  1386. const char *buffer, size_t count)
  1387. {
  1388. unsigned long value;
  1389. if (count > 31)
  1390. return -EINVAL;
  1391. value = simple_strtoul(buffer, NULL, 10);
  1392. sony_pic_set_wwanpower(value);
  1393. return count;
  1394. }
  1395. static ssize_t sony_pic_wwanpower_show(struct device *dev,
  1396. struct device_attribute *attr, char *buffer)
  1397. {
  1398. ssize_t count;
  1399. mutex_lock(&spic_dev.lock);
  1400. count = snprintf(buffer, PAGE_SIZE, "%d\n", spic_dev.wwan_power);
  1401. mutex_unlock(&spic_dev.lock);
  1402. return count;
  1403. }
  1404. /* bluetooth subsystem power state */
  1405. static void __sony_pic_set_bluetoothpower(u8 state)
  1406. {
  1407. state = !!state;
  1408. if (spic_dev.bluetooth_power == state)
  1409. return;
  1410. sony_pic_call2(0x96, state);
  1411. sony_pic_call1(0x82);
  1412. spic_dev.bluetooth_power = state;
  1413. }
  1414. static ssize_t sony_pic_bluetoothpower_store(struct device *dev,
  1415. struct device_attribute *attr,
  1416. const char *buffer, size_t count)
  1417. {
  1418. unsigned long value;
  1419. if (count > 31)
  1420. return -EINVAL;
  1421. value = simple_strtoul(buffer, NULL, 10);
  1422. mutex_lock(&spic_dev.lock);
  1423. __sony_pic_set_bluetoothpower(value);
  1424. mutex_unlock(&spic_dev.lock);
  1425. return count;
  1426. }
  1427. static ssize_t sony_pic_bluetoothpower_show(struct device *dev,
  1428. struct device_attribute *attr, char *buffer)
  1429. {
  1430. ssize_t count = 0;
  1431. mutex_lock(&spic_dev.lock);
  1432. count = snprintf(buffer, PAGE_SIZE, "%d\n", spic_dev.bluetooth_power);
  1433. mutex_unlock(&spic_dev.lock);
  1434. return count;
  1435. }
  1436. /* fan speed */
  1437. /* FAN0 information (reverse engineered from ACPI tables) */
  1438. #define SONY_PIC_FAN0_STATUS 0x93
  1439. static int sony_pic_set_fanspeed(unsigned long value)
  1440. {
  1441. return ec_write(SONY_PIC_FAN0_STATUS, value);
  1442. }
  1443. static int sony_pic_get_fanspeed(u8 *value)
  1444. {
  1445. return ec_read(SONY_PIC_FAN0_STATUS, value);
  1446. }
  1447. static ssize_t sony_pic_fanspeed_store(struct device *dev,
  1448. struct device_attribute *attr,
  1449. const char *buffer, size_t count)
  1450. {
  1451. unsigned long value;
  1452. if (count > 31)
  1453. return -EINVAL;
  1454. value = simple_strtoul(buffer, NULL, 10);
  1455. if (sony_pic_set_fanspeed(value))
  1456. return -EIO;
  1457. return count;
  1458. }
  1459. static ssize_t sony_pic_fanspeed_show(struct device *dev,
  1460. struct device_attribute *attr, char *buffer)
  1461. {
  1462. u8 value = 0;
  1463. if (sony_pic_get_fanspeed(&value))
  1464. return -EIO;
  1465. return snprintf(buffer, PAGE_SIZE, "%d\n", value);
  1466. }
  1467. #define SPIC_ATTR(_name, _mode) \
  1468. struct device_attribute spic_attr_##_name = __ATTR(_name, \
  1469. _mode, sony_pic_## _name ##_show, \
  1470. sony_pic_## _name ##_store)
  1471. static SPIC_ATTR(bluetoothpower, 0644);
  1472. static SPIC_ATTR(wwanpower, 0644);
  1473. static SPIC_ATTR(fanspeed, 0644);
  1474. static struct attribute *spic_attributes[] = {
  1475. &spic_attr_bluetoothpower.attr,
  1476. &spic_attr_wwanpower.attr,
  1477. &spic_attr_fanspeed.attr,
  1478. NULL
  1479. };
  1480. static struct attribute_group spic_attribute_group = {
  1481. .attrs = spic_attributes
  1482. };
  1483. /******** SONYPI compatibility **********/
  1484. #ifdef CONFIG_SONYPI_COMPAT
  1485. /* battery / brightness / temperature addresses */
  1486. #define SONYPI_BAT_FLAGS 0x81
  1487. #define SONYPI_LCD_LIGHT 0x96
  1488. #define SONYPI_BAT1_PCTRM 0xa0
  1489. #define SONYPI_BAT1_LEFT 0xa2
  1490. #define SONYPI_BAT1_MAXRT 0xa4
  1491. #define SONYPI_BAT2_PCTRM 0xa8
  1492. #define SONYPI_BAT2_LEFT 0xaa
  1493. #define SONYPI_BAT2_MAXRT 0xac
  1494. #define SONYPI_BAT1_MAXTK 0xb0
  1495. #define SONYPI_BAT1_FULL 0xb2
  1496. #define SONYPI_BAT2_MAXTK 0xb8
  1497. #define SONYPI_BAT2_FULL 0xba
  1498. #define SONYPI_TEMP_STATUS 0xC1
  1499. struct sonypi_compat_s {
  1500. struct fasync_struct *fifo_async;
  1501. struct kfifo *fifo;
  1502. spinlock_t fifo_lock;
  1503. wait_queue_head_t fifo_proc_list;
  1504. atomic_t open_count;
  1505. };
  1506. static struct sonypi_compat_s sonypi_compat = {
  1507. .open_count = ATOMIC_INIT(0),
  1508. };
  1509. static int sonypi_misc_fasync(int fd, struct file *filp, int on)
  1510. {
  1511. int retval;
  1512. retval = fasync_helper(fd, filp, on, &sonypi_compat.fifo_async);
  1513. if (retval < 0)
  1514. return retval;
  1515. return 0;
  1516. }
  1517. static int sonypi_misc_release(struct inode *inode, struct file *file)
  1518. {
  1519. sonypi_misc_fasync(-1, file, 0);
  1520. atomic_dec(&sonypi_compat.open_count);
  1521. return 0;
  1522. }
  1523. static int sonypi_misc_open(struct inode *inode, struct file *file)
  1524. {
  1525. /* Flush input queue on first open */
  1526. if (atomic_inc_return(&sonypi_compat.open_count) == 1)
  1527. kfifo_reset(sonypi_compat.fifo);
  1528. return 0;
  1529. }
  1530. static ssize_t sonypi_misc_read(struct file *file, char __user *buf,
  1531. size_t count, loff_t *pos)
  1532. {
  1533. ssize_t ret;
  1534. unsigned char c;
  1535. if ((kfifo_len(sonypi_compat.fifo) == 0) &&
  1536. (file->f_flags & O_NONBLOCK))
  1537. return -EAGAIN;
  1538. ret = wait_event_interruptible(sonypi_compat.fifo_proc_list,
  1539. kfifo_len(sonypi_compat.fifo) != 0);
  1540. if (ret)
  1541. return ret;
  1542. while (ret < count &&
  1543. (kfifo_get(sonypi_compat.fifo, &c, sizeof(c)) == sizeof(c))) {
  1544. if (put_user(c, buf++))
  1545. return -EFAULT;
  1546. ret++;
  1547. }
  1548. if (ret > 0) {
  1549. struct inode *inode = file->f_path.dentry->d_inode;
  1550. inode->i_atime = current_fs_time(inode->i_sb);
  1551. }
  1552. return ret;
  1553. }
  1554. static unsigned int sonypi_misc_poll(struct file *file, poll_table *wait)
  1555. {
  1556. poll_wait(file, &sonypi_compat.fifo_proc_list, wait);
  1557. if (kfifo_len(sonypi_compat.fifo))
  1558. return POLLIN | POLLRDNORM;
  1559. return 0;
  1560. }
  1561. static int ec_read16(u8 addr, u16 *value)
  1562. {
  1563. u8 val_lb, val_hb;
  1564. if (ec_read(addr, &val_lb))
  1565. return -1;
  1566. if (ec_read(addr + 1, &val_hb))
  1567. return -1;
  1568. *value = val_lb | (val_hb << 8);
  1569. return 0;
  1570. }
  1571. static int sonypi_misc_ioctl(struct inode *ip, struct file *fp,
  1572. unsigned int cmd, unsigned long arg)
  1573. {
  1574. int ret = 0;
  1575. void __user *argp = (void __user *)arg;
  1576. u8 val8;
  1577. u16 val16;
  1578. int value;
  1579. mutex_lock(&spic_dev.lock);
  1580. switch (cmd) {
  1581. case SONYPI_IOCGBRT:
  1582. if (sony_backlight_device == NULL) {
  1583. ret = -EIO;
  1584. break;
  1585. }
  1586. if (acpi_callgetfunc(sony_nc_acpi_handle, "GBRT", &value)) {
  1587. ret = -EIO;
  1588. break;
  1589. }
  1590. val8 = ((value & 0xff) - 1) << 5;
  1591. if (copy_to_user(argp, &val8, sizeof(val8)))
  1592. ret = -EFAULT;
  1593. break;
  1594. case SONYPI_IOCSBRT:
  1595. if (sony_backlight_device == NULL) {
  1596. ret = -EIO;
  1597. break;
  1598. }
  1599. if (copy_from_user(&val8, argp, sizeof(val8))) {
  1600. ret = -EFAULT;
  1601. break;
  1602. }
  1603. if (acpi_callsetfunc(sony_nc_acpi_handle, "SBRT",
  1604. (val8 >> 5) + 1, NULL)) {
  1605. ret = -EIO;
  1606. break;
  1607. }
  1608. /* sync the backlight device status */
  1609. sony_backlight_device->props.brightness =
  1610. sony_backlight_get_brightness(sony_backlight_device);
  1611. break;
  1612. case SONYPI_IOCGBAT1CAP:
  1613. if (ec_read16(SONYPI_BAT1_FULL, &val16)) {
  1614. ret = -EIO;
  1615. break;
  1616. }
  1617. if (copy_to_user(argp, &val16, sizeof(val16)))
  1618. ret = -EFAULT;
  1619. break;
  1620. case SONYPI_IOCGBAT1REM:
  1621. if (ec_read16(SONYPI_BAT1_LEFT, &val16)) {
  1622. ret = -EIO;
  1623. break;
  1624. }
  1625. if (copy_to_user(argp, &val16, sizeof(val16)))
  1626. ret = -EFAULT;
  1627. break;
  1628. case SONYPI_IOCGBAT2CAP:
  1629. if (ec_read16(SONYPI_BAT2_FULL, &val16)) {
  1630. ret = -EIO;
  1631. break;
  1632. }
  1633. if (copy_to_user(argp, &val16, sizeof(val16)))
  1634. ret = -EFAULT;
  1635. break;
  1636. case SONYPI_IOCGBAT2REM:
  1637. if (ec_read16(SONYPI_BAT2_LEFT, &val16)) {
  1638. ret = -EIO;
  1639. break;
  1640. }
  1641. if (copy_to_user(argp, &val16, sizeof(val16)))
  1642. ret = -EFAULT;
  1643. break;
  1644. case SONYPI_IOCGBATFLAGS:
  1645. if (ec_read(SONYPI_BAT_FLAGS, &val8)) {
  1646. ret = -EIO;
  1647. break;
  1648. }
  1649. val8 &= 0x07;
  1650. if (copy_to_user(argp, &val8, sizeof(val8)))
  1651. ret = -EFAULT;
  1652. break;
  1653. case SONYPI_IOCGBLUE:
  1654. val8 = spic_dev.bluetooth_power;
  1655. if (copy_to_user(argp, &val8, sizeof(val8)))
  1656. ret = -EFAULT;
  1657. break;
  1658. case SONYPI_IOCSBLUE:
  1659. if (copy_from_user(&val8, argp, sizeof(val8))) {
  1660. ret = -EFAULT;
  1661. break;
  1662. }
  1663. __sony_pic_set_bluetoothpower(val8);
  1664. break;
  1665. /* FAN Controls */
  1666. case SONYPI_IOCGFAN:
  1667. if (sony_pic_get_fanspeed(&val8)) {
  1668. ret = -EIO;
  1669. break;
  1670. }
  1671. if (copy_to_user(argp, &val8, sizeof(val8)))
  1672. ret = -EFAULT;
  1673. break;
  1674. case SONYPI_IOCSFAN:
  1675. if (copy_from_user(&val8, argp, sizeof(val8))) {
  1676. ret = -EFAULT;
  1677. break;
  1678. }
  1679. if (sony_pic_set_fanspeed(val8))
  1680. ret = -EIO;
  1681. break;
  1682. /* GET Temperature (useful under APM) */
  1683. case SONYPI_IOCGTEMP:
  1684. if (ec_read(SONYPI_TEMP_STATUS, &val8)) {
  1685. ret = -EIO;
  1686. break;
  1687. }
  1688. if (copy_to_user(argp, &val8, sizeof(val8)))
  1689. ret = -EFAULT;
  1690. break;
  1691. default:
  1692. ret = -EINVAL;
  1693. }
  1694. mutex_unlock(&spic_dev.lock);
  1695. return ret;
  1696. }
  1697. static const struct file_operations sonypi_misc_fops = {
  1698. .owner = THIS_MODULE,
  1699. .read = sonypi_misc_read,
  1700. .poll = sonypi_misc_poll,
  1701. .open = sonypi_misc_open,
  1702. .release = sonypi_misc_release,
  1703. .fasync = sonypi_misc_fasync,
  1704. .ioctl = sonypi_misc_ioctl,
  1705. };
  1706. static struct miscdevice sonypi_misc_device = {
  1707. .minor = MISC_DYNAMIC_MINOR,
  1708. .name = "sonypi",
  1709. .fops = &sonypi_misc_fops,
  1710. };
  1711. static void sonypi_compat_report_event(u8 event)
  1712. {
  1713. kfifo_put(sonypi_compat.fifo, (unsigned char *)&event, sizeof(event));
  1714. kill_fasync(&sonypi_compat.fifo_async, SIGIO, POLL_IN);
  1715. wake_up_interruptible(&sonypi_compat.fifo_proc_list);
  1716. }
  1717. static int sonypi_compat_init(void)
  1718. {
  1719. int error;
  1720. spin_lock_init(&sonypi_compat.fifo_lock);
  1721. sonypi_compat.fifo = kfifo_alloc(SONY_LAPTOP_BUF_SIZE, GFP_KERNEL,
  1722. &sonypi_compat.fifo_lock);
  1723. if (IS_ERR(sonypi_compat.fifo)) {
  1724. printk(KERN_ERR DRV_PFX "kfifo_alloc failed\n");
  1725. return PTR_ERR(sonypi_compat.fifo);
  1726. }
  1727. init_waitqueue_head(&sonypi_compat.fifo_proc_list);
  1728. if (minor != -1)
  1729. sonypi_misc_device.minor = minor;
  1730. error = misc_register(&sonypi_misc_device);
  1731. if (error) {
  1732. printk(KERN_ERR DRV_PFX "misc_register failed\n");
  1733. goto err_free_kfifo;
  1734. }
  1735. if (minor == -1)
  1736. printk(KERN_INFO DRV_PFX "device allocated minor is %d\n",
  1737. sonypi_misc_device.minor);
  1738. return 0;
  1739. err_free_kfifo:
  1740. kfifo_free(sonypi_compat.fifo);
  1741. return error;
  1742. }
  1743. static void sonypi_compat_exit(void)
  1744. {
  1745. misc_deregister(&sonypi_misc_device);
  1746. kfifo_free(sonypi_compat.fifo);
  1747. }
  1748. #else
  1749. static int sonypi_compat_init(void) { return 0; }
  1750. static void sonypi_compat_exit(void) { }
  1751. static void sonypi_compat_report_event(u8 event) { }
  1752. #endif /* CONFIG_SONYPI_COMPAT */
  1753. /*
  1754. * ACPI callbacks
  1755. */
  1756. static acpi_status
  1757. sony_pic_read_possible_resource(struct acpi_resource *resource, void *context)
  1758. {
  1759. u32 i;
  1760. struct sony_pic_dev *dev = (struct sony_pic_dev *)context;
  1761. switch (resource->type) {
  1762. case ACPI_RESOURCE_TYPE_START_DEPENDENT:
  1763. case ACPI_RESOURCE_TYPE_END_DEPENDENT:
  1764. return AE_OK;
  1765. case ACPI_RESOURCE_TYPE_IRQ:
  1766. {
  1767. struct acpi_resource_irq *p = &resource->data.irq;
  1768. struct sony_pic_irq *interrupt = NULL;
  1769. if (!p || !p->interrupt_count) {
  1770. /*
  1771. * IRQ descriptors may have no IRQ# bits set,
  1772. * particularly those those w/ _STA disabled
  1773. */
  1774. dprintk("Blank IRQ resource\n");
  1775. return AE_OK;
  1776. }
  1777. for (i = 0; i < p->interrupt_count; i++) {
  1778. if (!p->interrupts[i]) {
  1779. printk(KERN_WARNING DRV_PFX
  1780. "Invalid IRQ %d\n",
  1781. p->interrupts[i]);
  1782. continue;
  1783. }
  1784. interrupt = kzalloc(sizeof(*interrupt),
  1785. GFP_KERNEL);
  1786. if (!interrupt)
  1787. return AE_ERROR;
  1788. list_add_tail(&interrupt->list, &dev->interrupts);
  1789. interrupt->irq.triggering = p->triggering;
  1790. interrupt->irq.polarity = p->polarity;
  1791. interrupt->irq.sharable = p->sharable;
  1792. interrupt->irq.interrupt_count = 1;
  1793. interrupt->irq.interrupts[0] = p->interrupts[i];
  1794. }
  1795. return AE_OK;
  1796. }
  1797. case ACPI_RESOURCE_TYPE_IO:
  1798. {
  1799. struct acpi_resource_io *io = &resource->data.io;
  1800. struct sony_pic_ioport *ioport = NULL;
  1801. if (!io) {
  1802. dprintk("Blank IO resource\n");
  1803. return AE_OK;
  1804. }
  1805. ioport = kzalloc(sizeof(*ioport), GFP_KERNEL);
  1806. if (!ioport)
  1807. return AE_ERROR;
  1808. list_add_tail(&ioport->list, &dev->ioports);
  1809. memcpy(&ioport->io, io, sizeof(*io));
  1810. return AE_OK;
  1811. }
  1812. default:
  1813. dprintk("Resource %d isn't an IRQ nor an IO port\n",
  1814. resource->type);
  1815. case ACPI_RESOURCE_TYPE_END_TAG:
  1816. return AE_OK;
  1817. }
  1818. return AE_CTRL_TERMINATE;
  1819. }
  1820. static int sony_pic_possible_resources(struct acpi_device *device)
  1821. {
  1822. int result = 0;
  1823. acpi_status status = AE_OK;
  1824. if (!device)
  1825. return -EINVAL;
  1826. /* get device status */
  1827. /* see acpi_pci_link_get_current acpi_pci_link_get_possible */
  1828. dprintk("Evaluating _STA\n");
  1829. result = acpi_bus_get_status(device);
  1830. if (result) {
  1831. printk(KERN_WARNING DRV_PFX "Unable to read status\n");
  1832. goto end;
  1833. }
  1834. if (!device->status.enabled)
  1835. dprintk("Device disabled\n");
  1836. else
  1837. dprintk("Device enabled\n");
  1838. /*
  1839. * Query and parse 'method'
  1840. */
  1841. dprintk("Evaluating %s\n", METHOD_NAME__PRS);
  1842. status = acpi_walk_resources(device->handle, METHOD_NAME__PRS,
  1843. sony_pic_read_possible_resource, &spic_dev);
  1844. if (ACPI_FAILURE(status)) {
  1845. printk(KERN_WARNING DRV_PFX
  1846. "Failure evaluating %s\n",
  1847. METHOD_NAME__PRS);
  1848. result = -ENODEV;
  1849. }
  1850. end:
  1851. return result;
  1852. }
  1853. /*
  1854. * Disable the spic device by calling its _DIS method
  1855. */
  1856. static int sony_pic_disable(struct acpi_device *device)
  1857. {
  1858. if (ACPI_FAILURE(acpi_evaluate_object(device->handle,
  1859. "_DIS", NULL, NULL)))
  1860. return -ENXIO;
  1861. dprintk("Device disabled\n");
  1862. return 0;
  1863. }
  1864. /*
  1865. * Based on drivers/acpi/pci_link.c:acpi_pci_link_set
  1866. *
  1867. * Call _SRS to set current resources
  1868. */
  1869. static int sony_pic_enable(struct acpi_device *device,
  1870. struct sony_pic_ioport *ioport, struct sony_pic_irq *irq)
  1871. {
  1872. acpi_status status;
  1873. int result = 0;
  1874. struct {
  1875. struct acpi_resource io_res;
  1876. struct acpi_resource irq_res;
  1877. struct acpi_resource end;
  1878. } *resource;
  1879. struct acpi_buffer buffer = { 0, NULL };
  1880. if (!ioport || !irq)
  1881. return -EINVAL;
  1882. /* init acpi_buffer */
  1883. resource = kzalloc(sizeof(*resource) + 1, GFP_KERNEL);
  1884. if (!resource)
  1885. return -ENOMEM;
  1886. buffer.length = sizeof(*resource) + 1;
  1887. buffer.pointer = resource;
  1888. /* setup io resource */
  1889. resource->io_res.type = ACPI_RESOURCE_TYPE_IO;
  1890. resource->io_res.length = sizeof(struct acpi_resource);
  1891. memcpy(&resource->io_res.data.io, &ioport->io,
  1892. sizeof(struct acpi_resource_io));
  1893. /* setup irq resource */
  1894. resource->irq_res.type = ACPI_RESOURCE_TYPE_IRQ;
  1895. resource->irq_res.length = sizeof(struct acpi_resource);
  1896. memcpy(&resource->irq_res.data.irq, &irq->irq,
  1897. sizeof(struct acpi_resource_irq));
  1898. /* we requested a shared irq */
  1899. resource->irq_res.data.irq.sharable = ACPI_SHARED;
  1900. resource->end.type = ACPI_RESOURCE_TYPE_END_TAG;
  1901. /* Attempt to set the resource */
  1902. dprintk("Evaluating _SRS\n");
  1903. status = acpi_set_current_resources(device->handle, &buffer);
  1904. /* check for total failure */
  1905. if (ACPI_FAILURE(status)) {
  1906. printk(KERN_ERR DRV_PFX "Error evaluating _SRS");
  1907. result = -ENODEV;
  1908. goto end;
  1909. }
  1910. /* Necessary device initializations calls (from sonypi) */
  1911. sony_pic_call1(0x82);
  1912. sony_pic_call2(0x81, 0xff);
  1913. sony_pic_call1(compat ? 0x92 : 0x82);
  1914. end:
  1915. kfree(resource);
  1916. return result;
  1917. }
  1918. /*****************
  1919. *
  1920. * ISR: some event is available
  1921. *
  1922. *****************/
  1923. static irqreturn_t sony_pic_irq(int irq, void *dev_id)
  1924. {
  1925. int i, j;
  1926. u8 ev = 0;
  1927. u8 data_mask = 0;
  1928. u8 device_event = 0;
  1929. struct sony_pic_dev *dev = (struct sony_pic_dev *) dev_id;
  1930. ev = inb_p(dev->cur_ioport->io.minimum);
  1931. data_mask = inb_p(dev->cur_ioport->io.minimum + dev->evport_offset);
  1932. dprintk("event ([%.2x] [%.2x]) at port 0x%.4x(+0x%.2x)\n",
  1933. ev, data_mask, dev->cur_ioport->io.minimum, dev->evport_offset);
  1934. if (ev == 0x00 || ev == 0xff)
  1935. return IRQ_HANDLED;
  1936. for (i = 0; sony_pic_eventtypes[i].model; i++) {
  1937. if (spic_dev.model != sony_pic_eventtypes[i].model)
  1938. continue;
  1939. if ((data_mask & sony_pic_eventtypes[i].data) !=
  1940. sony_pic_eventtypes[i].data)
  1941. continue;
  1942. if (!(mask & sony_pic_eventtypes[i].mask))
  1943. continue;
  1944. for (j = 0; sony_pic_eventtypes[i].events[j].event; j++) {
  1945. if (ev == sony_pic_eventtypes[i].events[j].data) {
  1946. device_event =
  1947. sony_pic_eventtypes[i].events[j].event;
  1948. goto found;
  1949. }
  1950. }
  1951. }
  1952. return IRQ_HANDLED;
  1953. found:
  1954. sony_laptop_report_input_event(device_event);
  1955. acpi_bus_generate_event(spic_dev.acpi_dev, 1, device_event);
  1956. sonypi_compat_report_event(device_event);
  1957. return IRQ_HANDLED;
  1958. }
  1959. /*****************
  1960. *
  1961. * ACPI driver
  1962. *
  1963. *****************/
  1964. static int sony_pic_remove(struct acpi_device *device, int type)
  1965. {
  1966. struct sony_pic_ioport *io, *tmp_io;
  1967. struct sony_pic_irq *irq, *tmp_irq;
  1968. sonypi_compat_exit();
  1969. if (sony_pic_disable(device)) {
  1970. printk(KERN_ERR DRV_PFX "Couldn't disable device.\n");
  1971. return -ENXIO;
  1972. }
  1973. free_irq(spic_dev.cur_irq->irq.interrupts[0], &spic_dev);
  1974. release_region(spic_dev.cur_ioport->io.minimum,
  1975. spic_dev.cur_ioport->io.address_length);
  1976. sony_laptop_remove_input();
  1977. /* pf attrs */
  1978. sysfs_remove_group(&sony_pf_device->dev.kobj, &spic_attribute_group);
  1979. sony_pf_remove();
  1980. list_for_each_entry_safe(io, tmp_io, &spic_dev.ioports, list) {
  1981. list_del(&io->list);
  1982. kfree(io);
  1983. }
  1984. list_for_each_entry_safe(irq, tmp_irq, &spic_dev.interrupts, list) {
  1985. list_del(&irq->list);
  1986. kfree(irq);
  1987. }
  1988. spic_dev.cur_ioport = NULL;
  1989. spic_dev.cur_irq = NULL;
  1990. dprintk(SONY_PIC_DRIVER_NAME " removed.\n");
  1991. return 0;
  1992. }
  1993. static int sony_pic_add(struct acpi_device *device)
  1994. {
  1995. int result;
  1996. struct sony_pic_ioport *io, *tmp_io;
  1997. struct sony_pic_irq *irq, *tmp_irq;
  1998. printk(KERN_INFO DRV_PFX "%s v%s.\n",
  1999. SONY_PIC_DRIVER_NAME, SONY_LAPTOP_DRIVER_VERSION);
  2000. spic_dev.acpi_dev = device;
  2001. strcpy(acpi_device_class(device), "sony/hotkey");
  2002. spic_dev.model = sony_pic_detect_device_type();
  2003. mutex_init(&spic_dev.lock);
  2004. /* model specific characteristics */
  2005. switch(spic_dev.model) {
  2006. case SONYPI_DEVICE_TYPE1:
  2007. spic_dev.evport_offset = SONYPI_TYPE1_OFFSET;
  2008. break;
  2009. case SONYPI_DEVICE_TYPE3:
  2010. spic_dev.evport_offset = SONYPI_TYPE3_OFFSET;
  2011. break;
  2012. case SONYPI_DEVICE_TYPE2:
  2013. default:
  2014. spic_dev.evport_offset = SONYPI_TYPE2_OFFSET;
  2015. break;
  2016. }
  2017. /* read _PRS resources */
  2018. result = sony_pic_possible_resources(device);
  2019. if (result) {
  2020. printk(KERN_ERR DRV_PFX
  2021. "Unabe to read possible resources.\n");
  2022. goto err_free_resources;
  2023. }
  2024. /* setup input devices and helper fifo */
  2025. result = sony_laptop_setup_input();
  2026. if (result) {
  2027. printk(KERN_ERR DRV_PFX
  2028. "Unabe to create input devices.\n");
  2029. goto err_free_resources;
  2030. }
  2031. /* request io port */
  2032. list_for_each_entry(io, &spic_dev.ioports, list) {
  2033. if (request_region(io->io.minimum, io->io.address_length,
  2034. "Sony Programable I/O Device")) {
  2035. dprintk("I/O port: 0x%.4x (0x%.4x) + 0x%.2x\n",
  2036. io->io.minimum, io->io.maximum,
  2037. io->io.address_length);
  2038. spic_dev.cur_ioport = io;
  2039. break;
  2040. }
  2041. }
  2042. if (!spic_dev.cur_ioport) {
  2043. printk(KERN_ERR DRV_PFX "Failed to request_region.\n");
  2044. result = -ENODEV;
  2045. goto err_remove_input;
  2046. }
  2047. /* request IRQ */
  2048. list_for_each_entry(irq, &spic_dev.interrupts, list) {
  2049. if (!request_irq(irq->irq.interrupts[0], sony_pic_irq,
  2050. IRQF_SHARED, "sony-laptop", &spic_dev)) {
  2051. dprintk("IRQ: %d - triggering: %d - "
  2052. "polarity: %d - shr: %d\n",
  2053. irq->irq.interrupts[0],
  2054. irq->irq.triggering,
  2055. irq->irq.polarity,
  2056. irq->irq.sharable);
  2057. spic_dev.cur_irq = irq;
  2058. break;
  2059. }
  2060. }
  2061. if (!spic_dev.cur_irq) {
  2062. printk(KERN_ERR DRV_PFX "Failed to request_irq.\n");
  2063. result = -ENODEV;
  2064. goto err_release_region;
  2065. }
  2066. /* set resource status _SRS */
  2067. result = sony_pic_enable(device, spic_dev.cur_ioport, spic_dev.cur_irq);
  2068. if (result) {
  2069. printk(KERN_ERR DRV_PFX "Couldn't enable device.\n");
  2070. goto err_free_irq;
  2071. }
  2072. spic_dev.bluetooth_power = -1;
  2073. /* create device attributes */
  2074. result = sony_pf_add();
  2075. if (result)
  2076. goto err_disable_device;
  2077. result = sysfs_create_group(&sony_pf_device->dev.kobj, &spic_attribute_group);
  2078. if (result)
  2079. goto err_remove_pf;
  2080. if (sonypi_compat_init())
  2081. goto err_remove_pf;
  2082. return 0;
  2083. err_remove_pf:
  2084. sony_pf_remove();
  2085. err_disable_device:
  2086. sony_pic_disable(device);
  2087. err_free_irq:
  2088. free_irq(spic_dev.cur_irq->irq.interrupts[0], &spic_dev);
  2089. err_release_region:
  2090. release_region(spic_dev.cur_ioport->io.minimum,
  2091. spic_dev.cur_ioport->io.address_length);
  2092. err_remove_input:
  2093. sony_laptop_remove_input();
  2094. err_free_resources:
  2095. list_for_each_entry_safe(io, tmp_io, &spic_dev.ioports, list) {
  2096. list_del(&io->list);
  2097. kfree(io);
  2098. }
  2099. list_for_each_entry_safe(irq, tmp_irq, &spic_dev.interrupts, list) {
  2100. list_del(&irq->list);
  2101. kfree(irq);
  2102. }
  2103. spic_dev.cur_ioport = NULL;
  2104. spic_dev.cur_irq = NULL;
  2105. return result;
  2106. }
  2107. static int sony_pic_suspend(struct acpi_device *device, pm_message_t state)
  2108. {
  2109. if (sony_pic_disable(device))
  2110. return -ENXIO;
  2111. return 0;
  2112. }
  2113. static int sony_pic_resume(struct acpi_device *device)
  2114. {
  2115. sony_pic_enable(device, spic_dev.cur_ioport, spic_dev.cur_irq);
  2116. return 0;
  2117. }
  2118. static struct acpi_driver sony_pic_driver = {
  2119. .name = SONY_PIC_DRIVER_NAME,
  2120. .class = SONY_PIC_CLASS,
  2121. .ids = SONY_PIC_HID,
  2122. .owner = THIS_MODULE,
  2123. .ops = {
  2124. .add = sony_pic_add,
  2125. .remove = sony_pic_remove,
  2126. .suspend = sony_pic_suspend,
  2127. .resume = sony_pic_resume,
  2128. },
  2129. };
  2130. static struct dmi_system_id __initdata sonypi_dmi_table[] = {
  2131. {
  2132. .ident = "Sony Vaio",
  2133. .matches = {
  2134. DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
  2135. DMI_MATCH(DMI_PRODUCT_NAME, "PCG-"),
  2136. },
  2137. },
  2138. {
  2139. .ident = "Sony Vaio",
  2140. .matches = {
  2141. DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
  2142. DMI_MATCH(DMI_PRODUCT_NAME, "VGN-"),
  2143. },
  2144. },
  2145. { }
  2146. };
  2147. static int __init sony_laptop_init(void)
  2148. {
  2149. int result;
  2150. if (!no_spic && dmi_check_system(sonypi_dmi_table)) {
  2151. result = acpi_bus_register_driver(&sony_pic_driver);
  2152. if (result) {
  2153. printk(KERN_ERR DRV_PFX
  2154. "Unable to register SPIC driver.");
  2155. goto out;
  2156. }
  2157. }
  2158. result = acpi_bus_register_driver(&sony_nc_driver);
  2159. if (result) {
  2160. printk(KERN_ERR DRV_PFX "Unable to register SNC driver.");
  2161. goto out_unregister_pic;
  2162. }
  2163. return 0;
  2164. out_unregister_pic:
  2165. if (!no_spic)
  2166. acpi_bus_unregister_driver(&sony_pic_driver);
  2167. out:
  2168. return result;
  2169. }
  2170. static void __exit sony_laptop_exit(void)
  2171. {
  2172. acpi_bus_unregister_driver(&sony_nc_driver);
  2173. if (!no_spic)
  2174. acpi_bus_unregister_driver(&sony_pic_driver);
  2175. }
  2176. module_init(sony_laptop_init);
  2177. module_exit(sony_laptop_exit);