sony-laptop.c 69 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666
  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 subsystem */
  261. static void sony_laptop_report_input_event(u8 event)
  262. {
  263. struct input_dev *jog_dev = sony_laptop_input.jog_dev;
  264. struct input_dev *key_dev = sony_laptop_input.key_dev;
  265. struct sony_laptop_keypress kp = { NULL };
  266. if (event == SONYPI_EVENT_FNKEY_RELEASED) {
  267. /* Nothing, not all VAIOs generate this event */
  268. return;
  269. }
  270. /* report events */
  271. switch (event) {
  272. /* jog_dev events */
  273. case SONYPI_EVENT_JOGDIAL_UP:
  274. case SONYPI_EVENT_JOGDIAL_UP_PRESSED:
  275. input_report_rel(jog_dev, REL_WHEEL, 1);
  276. input_sync(jog_dev);
  277. return;
  278. case SONYPI_EVENT_JOGDIAL_DOWN:
  279. case SONYPI_EVENT_JOGDIAL_DOWN_PRESSED:
  280. input_report_rel(jog_dev, REL_WHEEL, -1);
  281. input_sync(jog_dev);
  282. return;
  283. /* key_dev events */
  284. case SONYPI_EVENT_JOGDIAL_PRESSED:
  285. kp.key = BTN_MIDDLE;
  286. kp.dev = jog_dev;
  287. break;
  288. default:
  289. if (event > ARRAY_SIZE (sony_laptop_input_keycode_map)) {
  290. dprintk("sony_laptop_report_input_event, event not known: %d\n", event);
  291. break;
  292. }
  293. if (sony_laptop_input_index[event] != -1) {
  294. kp.key = sony_laptop_input_keycode_map[sony_laptop_input_index[event]];
  295. if (kp.key != KEY_UNKNOWN)
  296. kp.dev = key_dev;
  297. }
  298. break;
  299. }
  300. if (kp.dev) {
  301. input_report_key(kp.dev, kp.key, 1);
  302. /* we emit the scancode so we can always remap the key */
  303. input_event(kp.dev, EV_MSC, MSC_SCAN, event);
  304. input_sync(kp.dev);
  305. kfifo_put(sony_laptop_input.fifo,
  306. (unsigned char *)&kp, sizeof(kp));
  307. if (!work_pending(&sony_laptop_release_key_work))
  308. queue_work(sony_laptop_input.wq,
  309. &sony_laptop_release_key_work);
  310. } else
  311. dprintk("unknown input event %.2x\n", event);
  312. }
  313. static int sony_laptop_setup_input(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_MASK(EV_KEY) | BIT_MASK(EV_REL);
  376. jog_dev->keybit[BIT_WORD(BTN_MOUSE)] = BIT_MASK(BTN_MIDDLE);
  377. jog_dev->relbit[0] = BIT_MASK(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 io1;
  999. struct acpi_resource_io io2;
  1000. struct list_head list;
  1001. };
  1002. struct sony_pic_irq {
  1003. struct acpi_resource_irq irq;
  1004. struct list_head list;
  1005. };
  1006. struct sony_pic_dev {
  1007. int model;
  1008. u16 evport_offset;
  1009. u8 camera_power;
  1010. u8 bluetooth_power;
  1011. u8 wwan_power;
  1012. struct acpi_device *acpi_dev;
  1013. struct sony_pic_irq *cur_irq;
  1014. struct sony_pic_ioport *cur_ioport;
  1015. struct list_head interrupts;
  1016. struct list_head ioports;
  1017. struct mutex lock;
  1018. };
  1019. static struct sony_pic_dev spic_dev = {
  1020. .interrupts = LIST_HEAD_INIT(spic_dev.interrupts),
  1021. .ioports = LIST_HEAD_INIT(spic_dev.ioports),
  1022. };
  1023. /* Event masks */
  1024. #define SONYPI_JOGGER_MASK 0x00000001
  1025. #define SONYPI_CAPTURE_MASK 0x00000002
  1026. #define SONYPI_FNKEY_MASK 0x00000004
  1027. #define SONYPI_BLUETOOTH_MASK 0x00000008
  1028. #define SONYPI_PKEY_MASK 0x00000010
  1029. #define SONYPI_BACK_MASK 0x00000020
  1030. #define SONYPI_HELP_MASK 0x00000040
  1031. #define SONYPI_LID_MASK 0x00000080
  1032. #define SONYPI_ZOOM_MASK 0x00000100
  1033. #define SONYPI_THUMBPHRASE_MASK 0x00000200
  1034. #define SONYPI_MEYE_MASK 0x00000400
  1035. #define SONYPI_MEMORYSTICK_MASK 0x00000800
  1036. #define SONYPI_BATTERY_MASK 0x00001000
  1037. #define SONYPI_WIRELESS_MASK 0x00002000
  1038. struct sonypi_event {
  1039. u8 data;
  1040. u8 event;
  1041. };
  1042. /* The set of possible button release events */
  1043. static struct sonypi_event sonypi_releaseev[] = {
  1044. { 0x00, SONYPI_EVENT_ANYBUTTON_RELEASED },
  1045. { 0, 0 }
  1046. };
  1047. /* The set of possible jogger events */
  1048. static struct sonypi_event sonypi_joggerev[] = {
  1049. { 0x1f, SONYPI_EVENT_JOGDIAL_UP },
  1050. { 0x01, SONYPI_EVENT_JOGDIAL_DOWN },
  1051. { 0x5f, SONYPI_EVENT_JOGDIAL_UP_PRESSED },
  1052. { 0x41, SONYPI_EVENT_JOGDIAL_DOWN_PRESSED },
  1053. { 0x1e, SONYPI_EVENT_JOGDIAL_FAST_UP },
  1054. { 0x02, SONYPI_EVENT_JOGDIAL_FAST_DOWN },
  1055. { 0x5e, SONYPI_EVENT_JOGDIAL_FAST_UP_PRESSED },
  1056. { 0x42, SONYPI_EVENT_JOGDIAL_FAST_DOWN_PRESSED },
  1057. { 0x1d, SONYPI_EVENT_JOGDIAL_VFAST_UP },
  1058. { 0x03, SONYPI_EVENT_JOGDIAL_VFAST_DOWN },
  1059. { 0x5d, SONYPI_EVENT_JOGDIAL_VFAST_UP_PRESSED },
  1060. { 0x43, SONYPI_EVENT_JOGDIAL_VFAST_DOWN_PRESSED },
  1061. { 0x40, SONYPI_EVENT_JOGDIAL_PRESSED },
  1062. { 0, 0 }
  1063. };
  1064. /* The set of possible capture button events */
  1065. static struct sonypi_event sonypi_captureev[] = {
  1066. { 0x05, SONYPI_EVENT_CAPTURE_PARTIALPRESSED },
  1067. { 0x07, SONYPI_EVENT_CAPTURE_PRESSED },
  1068. { 0x01, SONYPI_EVENT_CAPTURE_PARTIALRELEASED },
  1069. { 0, 0 }
  1070. };
  1071. /* The set of possible fnkeys events */
  1072. static struct sonypi_event sonypi_fnkeyev[] = {
  1073. { 0x10, SONYPI_EVENT_FNKEY_ESC },
  1074. { 0x11, SONYPI_EVENT_FNKEY_F1 },
  1075. { 0x12, SONYPI_EVENT_FNKEY_F2 },
  1076. { 0x13, SONYPI_EVENT_FNKEY_F3 },
  1077. { 0x14, SONYPI_EVENT_FNKEY_F4 },
  1078. { 0x15, SONYPI_EVENT_FNKEY_F5 },
  1079. { 0x16, SONYPI_EVENT_FNKEY_F6 },
  1080. { 0x17, SONYPI_EVENT_FNKEY_F7 },
  1081. { 0x18, SONYPI_EVENT_FNKEY_F8 },
  1082. { 0x19, SONYPI_EVENT_FNKEY_F9 },
  1083. { 0x1a, SONYPI_EVENT_FNKEY_F10 },
  1084. { 0x1b, SONYPI_EVENT_FNKEY_F11 },
  1085. { 0x1c, SONYPI_EVENT_FNKEY_F12 },
  1086. { 0x1f, SONYPI_EVENT_FNKEY_RELEASED },
  1087. { 0x21, SONYPI_EVENT_FNKEY_1 },
  1088. { 0x22, SONYPI_EVENT_FNKEY_2 },
  1089. { 0x31, SONYPI_EVENT_FNKEY_D },
  1090. { 0x32, SONYPI_EVENT_FNKEY_E },
  1091. { 0x33, SONYPI_EVENT_FNKEY_F },
  1092. { 0x34, SONYPI_EVENT_FNKEY_S },
  1093. { 0x35, SONYPI_EVENT_FNKEY_B },
  1094. { 0x36, SONYPI_EVENT_FNKEY_ONLY },
  1095. { 0, 0 }
  1096. };
  1097. /* The set of possible program key events */
  1098. static struct sonypi_event sonypi_pkeyev[] = {
  1099. { 0x01, SONYPI_EVENT_PKEY_P1 },
  1100. { 0x02, SONYPI_EVENT_PKEY_P2 },
  1101. { 0x04, SONYPI_EVENT_PKEY_P3 },
  1102. { 0x5c, SONYPI_EVENT_PKEY_P1 },
  1103. { 0, 0 }
  1104. };
  1105. /* The set of possible bluetooth events */
  1106. static struct sonypi_event sonypi_blueev[] = {
  1107. { 0x55, SONYPI_EVENT_BLUETOOTH_PRESSED },
  1108. { 0x59, SONYPI_EVENT_BLUETOOTH_ON },
  1109. { 0x5a, SONYPI_EVENT_BLUETOOTH_OFF },
  1110. { 0, 0 }
  1111. };
  1112. /* The set of possible wireless events */
  1113. static struct sonypi_event sonypi_wlessev[] = {
  1114. { 0x59, SONYPI_EVENT_WIRELESS_ON },
  1115. { 0x5a, SONYPI_EVENT_WIRELESS_OFF },
  1116. { 0, 0 }
  1117. };
  1118. /* The set of possible back button events */
  1119. static struct sonypi_event sonypi_backev[] = {
  1120. { 0x20, SONYPI_EVENT_BACK_PRESSED },
  1121. { 0, 0 }
  1122. };
  1123. /* The set of possible help button events */
  1124. static struct sonypi_event sonypi_helpev[] = {
  1125. { 0x3b, SONYPI_EVENT_HELP_PRESSED },
  1126. { 0, 0 }
  1127. };
  1128. /* The set of possible lid events */
  1129. static struct sonypi_event sonypi_lidev[] = {
  1130. { 0x51, SONYPI_EVENT_LID_CLOSED },
  1131. { 0x50, SONYPI_EVENT_LID_OPENED },
  1132. { 0, 0 }
  1133. };
  1134. /* The set of possible zoom events */
  1135. static struct sonypi_event sonypi_zoomev[] = {
  1136. { 0x39, SONYPI_EVENT_ZOOM_PRESSED },
  1137. { 0, 0 }
  1138. };
  1139. /* The set of possible thumbphrase events */
  1140. static struct sonypi_event sonypi_thumbphraseev[] = {
  1141. { 0x3a, SONYPI_EVENT_THUMBPHRASE_PRESSED },
  1142. { 0, 0 }
  1143. };
  1144. /* The set of possible motioneye camera events */
  1145. static struct sonypi_event sonypi_meyeev[] = {
  1146. { 0x00, SONYPI_EVENT_MEYE_FACE },
  1147. { 0x01, SONYPI_EVENT_MEYE_OPPOSITE },
  1148. { 0, 0 }
  1149. };
  1150. /* The set of possible memorystick events */
  1151. static struct sonypi_event sonypi_memorystickev[] = {
  1152. { 0x53, SONYPI_EVENT_MEMORYSTICK_INSERT },
  1153. { 0x54, SONYPI_EVENT_MEMORYSTICK_EJECT },
  1154. { 0, 0 }
  1155. };
  1156. /* The set of possible battery events */
  1157. static struct sonypi_event sonypi_batteryev[] = {
  1158. { 0x20, SONYPI_EVENT_BATTERY_INSERT },
  1159. { 0x30, SONYPI_EVENT_BATTERY_REMOVE },
  1160. { 0, 0 }
  1161. };
  1162. static struct sonypi_eventtypes {
  1163. int model;
  1164. u8 data;
  1165. unsigned long mask;
  1166. struct sonypi_event * events;
  1167. } sony_pic_eventtypes[] = {
  1168. { SONYPI_DEVICE_TYPE1, 0, 0xffffffff, sonypi_releaseev },
  1169. { SONYPI_DEVICE_TYPE1, 0x70, SONYPI_MEYE_MASK, sonypi_meyeev },
  1170. { SONYPI_DEVICE_TYPE1, 0x30, SONYPI_LID_MASK, sonypi_lidev },
  1171. { SONYPI_DEVICE_TYPE1, 0x60, SONYPI_CAPTURE_MASK, sonypi_captureev },
  1172. { SONYPI_DEVICE_TYPE1, 0x10, SONYPI_JOGGER_MASK, sonypi_joggerev },
  1173. { SONYPI_DEVICE_TYPE1, 0x20, SONYPI_FNKEY_MASK, sonypi_fnkeyev },
  1174. { SONYPI_DEVICE_TYPE1, 0x30, SONYPI_BLUETOOTH_MASK, sonypi_blueev },
  1175. { SONYPI_DEVICE_TYPE1, 0x40, SONYPI_PKEY_MASK, sonypi_pkeyev },
  1176. { SONYPI_DEVICE_TYPE1, 0x30, SONYPI_MEMORYSTICK_MASK, sonypi_memorystickev },
  1177. { SONYPI_DEVICE_TYPE1, 0x40, SONYPI_BATTERY_MASK, sonypi_batteryev },
  1178. { SONYPI_DEVICE_TYPE2, 0, 0xffffffff, sonypi_releaseev },
  1179. { SONYPI_DEVICE_TYPE2, 0x38, SONYPI_LID_MASK, sonypi_lidev },
  1180. { SONYPI_DEVICE_TYPE2, 0x11, SONYPI_JOGGER_MASK, sonypi_joggerev },
  1181. { SONYPI_DEVICE_TYPE2, 0x61, SONYPI_CAPTURE_MASK, sonypi_captureev },
  1182. { SONYPI_DEVICE_TYPE2, 0x21, SONYPI_FNKEY_MASK, sonypi_fnkeyev },
  1183. { SONYPI_DEVICE_TYPE2, 0x31, SONYPI_BLUETOOTH_MASK, sonypi_blueev },
  1184. { SONYPI_DEVICE_TYPE2, 0x08, SONYPI_PKEY_MASK, sonypi_pkeyev },
  1185. { SONYPI_DEVICE_TYPE2, 0x11, SONYPI_BACK_MASK, sonypi_backev },
  1186. { SONYPI_DEVICE_TYPE2, 0x21, SONYPI_HELP_MASK, sonypi_helpev },
  1187. { SONYPI_DEVICE_TYPE2, 0x21, SONYPI_ZOOM_MASK, sonypi_zoomev },
  1188. { SONYPI_DEVICE_TYPE2, 0x20, SONYPI_THUMBPHRASE_MASK, sonypi_thumbphraseev },
  1189. { SONYPI_DEVICE_TYPE2, 0x31, SONYPI_MEMORYSTICK_MASK, sonypi_memorystickev },
  1190. { SONYPI_DEVICE_TYPE2, 0x41, SONYPI_BATTERY_MASK, sonypi_batteryev },
  1191. { SONYPI_DEVICE_TYPE2, 0x31, SONYPI_PKEY_MASK, sonypi_pkeyev },
  1192. { SONYPI_DEVICE_TYPE3, 0, 0xffffffff, sonypi_releaseev },
  1193. { SONYPI_DEVICE_TYPE3, 0x21, SONYPI_FNKEY_MASK, sonypi_fnkeyev },
  1194. { SONYPI_DEVICE_TYPE3, 0x31, SONYPI_WIRELESS_MASK, sonypi_wlessev },
  1195. { SONYPI_DEVICE_TYPE3, 0x31, SONYPI_MEMORYSTICK_MASK, sonypi_memorystickev },
  1196. { SONYPI_DEVICE_TYPE3, 0x41, SONYPI_BATTERY_MASK, sonypi_batteryev },
  1197. { SONYPI_DEVICE_TYPE3, 0x31, SONYPI_PKEY_MASK, sonypi_pkeyev },
  1198. { 0 }
  1199. };
  1200. static int sony_pic_detect_device_type(void)
  1201. {
  1202. struct pci_dev *pcidev;
  1203. int model = 0;
  1204. if ((pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
  1205. PCI_DEVICE_ID_INTEL_82371AB_3, NULL)))
  1206. model = SONYPI_DEVICE_TYPE1;
  1207. else if ((pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
  1208. PCI_DEVICE_ID_INTEL_ICH6_1, NULL)))
  1209. model = SONYPI_DEVICE_TYPE3;
  1210. else if ((pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
  1211. PCI_DEVICE_ID_INTEL_ICH7_1, NULL)))
  1212. model = SONYPI_DEVICE_TYPE3;
  1213. else
  1214. model = SONYPI_DEVICE_TYPE2;
  1215. if (pcidev)
  1216. pci_dev_put(pcidev);
  1217. printk(KERN_INFO DRV_PFX "detected Type%d model\n",
  1218. model == SONYPI_DEVICE_TYPE1 ? 1 :
  1219. model == SONYPI_DEVICE_TYPE2 ? 2 : 3);
  1220. return model;
  1221. }
  1222. #define ITERATIONS_LONG 10000
  1223. #define ITERATIONS_SHORT 10
  1224. #define wait_on_command(command, iterations) { \
  1225. unsigned int n = iterations; \
  1226. while (--n && (command)) \
  1227. udelay(1); \
  1228. if (!n) \
  1229. dprintk("command failed at %s : %s (line %d)\n", \
  1230. __FILE__, __FUNCTION__, __LINE__); \
  1231. }
  1232. static u8 sony_pic_call1(u8 dev)
  1233. {
  1234. u8 v1, v2;
  1235. wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2,
  1236. ITERATIONS_LONG);
  1237. outb(dev, spic_dev.cur_ioport->io1.minimum + 4);
  1238. v1 = inb_p(spic_dev.cur_ioport->io1.minimum + 4);
  1239. v2 = inb_p(spic_dev.cur_ioport->io1.minimum);
  1240. dprintk("sony_pic_call1: 0x%.4x\n", (v2 << 8) | v1);
  1241. return v2;
  1242. }
  1243. static u8 sony_pic_call2(u8 dev, u8 fn)
  1244. {
  1245. u8 v1;
  1246. wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2,
  1247. ITERATIONS_LONG);
  1248. outb(dev, spic_dev.cur_ioport->io1.minimum + 4);
  1249. wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2,
  1250. ITERATIONS_LONG);
  1251. outb(fn, spic_dev.cur_ioport->io1.minimum);
  1252. v1 = inb_p(spic_dev.cur_ioport->io1.minimum);
  1253. dprintk("sony_pic_call2: 0x%.4x\n", v1);
  1254. return v1;
  1255. }
  1256. static u8 sony_pic_call3(u8 dev, u8 fn, u8 v)
  1257. {
  1258. u8 v1;
  1259. wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2, ITERATIONS_LONG);
  1260. outb(dev, spic_dev.cur_ioport->io1.minimum + 4);
  1261. wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2, ITERATIONS_LONG);
  1262. outb(fn, spic_dev.cur_ioport->io1.minimum);
  1263. wait_on_command(inb_p(spic_dev.cur_ioport->io1.minimum + 4) & 2, ITERATIONS_LONG);
  1264. outb(v, spic_dev.cur_ioport->io1.minimum);
  1265. v1 = inb_p(spic_dev.cur_ioport->io1.minimum);
  1266. dprintk("sony_pic_call3: 0x%.4x\n", v1);
  1267. return v1;
  1268. }
  1269. /* camera tests and poweron/poweroff */
  1270. #define SONYPI_CAMERA_PICTURE 5
  1271. #define SONYPI_CAMERA_CONTROL 0x10
  1272. #define SONYPI_CAMERA_BRIGHTNESS 0
  1273. #define SONYPI_CAMERA_CONTRAST 1
  1274. #define SONYPI_CAMERA_HUE 2
  1275. #define SONYPI_CAMERA_COLOR 3
  1276. #define SONYPI_CAMERA_SHARPNESS 4
  1277. #define SONYPI_CAMERA_EXPOSURE_MASK 0xC
  1278. #define SONYPI_CAMERA_WHITE_BALANCE_MASK 0x3
  1279. #define SONYPI_CAMERA_PICTURE_MODE_MASK 0x30
  1280. #define SONYPI_CAMERA_MUTE_MASK 0x40
  1281. /* the rest don't need a loop until not 0xff */
  1282. #define SONYPI_CAMERA_AGC 6
  1283. #define SONYPI_CAMERA_AGC_MASK 0x30
  1284. #define SONYPI_CAMERA_SHUTTER_MASK 0x7
  1285. #define SONYPI_CAMERA_SHUTDOWN_REQUEST 7
  1286. #define SONYPI_CAMERA_CONTROL 0x10
  1287. #define SONYPI_CAMERA_STATUS 7
  1288. #define SONYPI_CAMERA_STATUS_READY 0x2
  1289. #define SONYPI_CAMERA_STATUS_POSITION 0x4
  1290. #define SONYPI_DIRECTION_BACKWARDS 0x4
  1291. #define SONYPI_CAMERA_REVISION 8
  1292. #define SONYPI_CAMERA_ROMVERSION 9
  1293. static int __sony_pic_camera_ready(void)
  1294. {
  1295. u8 v;
  1296. v = sony_pic_call2(0x8f, SONYPI_CAMERA_STATUS);
  1297. return (v != 0xff && (v & SONYPI_CAMERA_STATUS_READY));
  1298. }
  1299. static int __sony_pic_camera_off(void)
  1300. {
  1301. if (!camera) {
  1302. printk(KERN_WARNING DRV_PFX "camera control not enabled\n");
  1303. return -ENODEV;
  1304. }
  1305. wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_PICTURE,
  1306. SONYPI_CAMERA_MUTE_MASK),
  1307. ITERATIONS_SHORT);
  1308. if (spic_dev.camera_power) {
  1309. sony_pic_call2(0x91, 0);
  1310. spic_dev.camera_power = 0;
  1311. }
  1312. return 0;
  1313. }
  1314. static int __sony_pic_camera_on(void)
  1315. {
  1316. int i, j, x;
  1317. if (!camera) {
  1318. printk(KERN_WARNING DRV_PFX "camera control not enabled\n");
  1319. return -ENODEV;
  1320. }
  1321. if (spic_dev.camera_power)
  1322. return 0;
  1323. for (j = 5; j > 0; j--) {
  1324. for (x = 0; x < 100 && sony_pic_call2(0x91, 0x1); x++)
  1325. msleep(10);
  1326. sony_pic_call1(0x93);
  1327. for (i = 400; i > 0; i--) {
  1328. if (__sony_pic_camera_ready())
  1329. break;
  1330. msleep(10);
  1331. }
  1332. if (i)
  1333. break;
  1334. }
  1335. if (j == 0) {
  1336. printk(KERN_WARNING DRV_PFX "failed to power on camera\n");
  1337. return -ENODEV;
  1338. }
  1339. wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_CONTROL,
  1340. 0x5a),
  1341. ITERATIONS_SHORT);
  1342. spic_dev.camera_power = 1;
  1343. return 0;
  1344. }
  1345. /* External camera command (exported to the motion eye v4l driver) */
  1346. int sony_pic_camera_command(int command, u8 value)
  1347. {
  1348. if (!camera)
  1349. return -EIO;
  1350. mutex_lock(&spic_dev.lock);
  1351. switch (command) {
  1352. case SONY_PIC_COMMAND_SETCAMERA:
  1353. if (value)
  1354. __sony_pic_camera_on();
  1355. else
  1356. __sony_pic_camera_off();
  1357. break;
  1358. case SONY_PIC_COMMAND_SETCAMERABRIGHTNESS:
  1359. wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_BRIGHTNESS, value),
  1360. ITERATIONS_SHORT);
  1361. break;
  1362. case SONY_PIC_COMMAND_SETCAMERACONTRAST:
  1363. wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_CONTRAST, value),
  1364. ITERATIONS_SHORT);
  1365. break;
  1366. case SONY_PIC_COMMAND_SETCAMERAHUE:
  1367. wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_HUE, value),
  1368. ITERATIONS_SHORT);
  1369. break;
  1370. case SONY_PIC_COMMAND_SETCAMERACOLOR:
  1371. wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_COLOR, value),
  1372. ITERATIONS_SHORT);
  1373. break;
  1374. case SONY_PIC_COMMAND_SETCAMERASHARPNESS:
  1375. wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_SHARPNESS, value),
  1376. ITERATIONS_SHORT);
  1377. break;
  1378. case SONY_PIC_COMMAND_SETCAMERAPICTURE:
  1379. wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_PICTURE, value),
  1380. ITERATIONS_SHORT);
  1381. break;
  1382. case SONY_PIC_COMMAND_SETCAMERAAGC:
  1383. wait_on_command(sony_pic_call3(0x90, SONYPI_CAMERA_AGC, value),
  1384. ITERATIONS_SHORT);
  1385. break;
  1386. default:
  1387. printk(KERN_ERR DRV_PFX "sony_pic_camera_command invalid: %d\n",
  1388. command);
  1389. break;
  1390. }
  1391. mutex_unlock(&spic_dev.lock);
  1392. return 0;
  1393. }
  1394. EXPORT_SYMBOL(sony_pic_camera_command);
  1395. /* gprs/edge modem (SZ460N and SZ210P), thanks to Joshua Wise */
  1396. static void sony_pic_set_wwanpower(u8 state)
  1397. {
  1398. state = !!state;
  1399. mutex_lock(&spic_dev.lock);
  1400. if (spic_dev.wwan_power == state) {
  1401. mutex_unlock(&spic_dev.lock);
  1402. return;
  1403. }
  1404. sony_pic_call2(0xB0, state);
  1405. spic_dev.wwan_power = state;
  1406. mutex_unlock(&spic_dev.lock);
  1407. }
  1408. static ssize_t sony_pic_wwanpower_store(struct device *dev,
  1409. struct device_attribute *attr,
  1410. const char *buffer, size_t count)
  1411. {
  1412. unsigned long value;
  1413. if (count > 31)
  1414. return -EINVAL;
  1415. value = simple_strtoul(buffer, NULL, 10);
  1416. sony_pic_set_wwanpower(value);
  1417. return count;
  1418. }
  1419. static ssize_t sony_pic_wwanpower_show(struct device *dev,
  1420. struct device_attribute *attr, char *buffer)
  1421. {
  1422. ssize_t count;
  1423. mutex_lock(&spic_dev.lock);
  1424. count = snprintf(buffer, PAGE_SIZE, "%d\n", spic_dev.wwan_power);
  1425. mutex_unlock(&spic_dev.lock);
  1426. return count;
  1427. }
  1428. /* bluetooth subsystem power state */
  1429. static void __sony_pic_set_bluetoothpower(u8 state)
  1430. {
  1431. state = !!state;
  1432. if (spic_dev.bluetooth_power == state)
  1433. return;
  1434. sony_pic_call2(0x96, state);
  1435. sony_pic_call1(0x82);
  1436. spic_dev.bluetooth_power = state;
  1437. }
  1438. static ssize_t sony_pic_bluetoothpower_store(struct device *dev,
  1439. struct device_attribute *attr,
  1440. const char *buffer, size_t count)
  1441. {
  1442. unsigned long value;
  1443. if (count > 31)
  1444. return -EINVAL;
  1445. value = simple_strtoul(buffer, NULL, 10);
  1446. mutex_lock(&spic_dev.lock);
  1447. __sony_pic_set_bluetoothpower(value);
  1448. mutex_unlock(&spic_dev.lock);
  1449. return count;
  1450. }
  1451. static ssize_t sony_pic_bluetoothpower_show(struct device *dev,
  1452. struct device_attribute *attr, char *buffer)
  1453. {
  1454. ssize_t count = 0;
  1455. mutex_lock(&spic_dev.lock);
  1456. count = snprintf(buffer, PAGE_SIZE, "%d\n", spic_dev.bluetooth_power);
  1457. mutex_unlock(&spic_dev.lock);
  1458. return count;
  1459. }
  1460. /* fan speed */
  1461. /* FAN0 information (reverse engineered from ACPI tables) */
  1462. #define SONY_PIC_FAN0_STATUS 0x93
  1463. static int sony_pic_set_fanspeed(unsigned long value)
  1464. {
  1465. return ec_write(SONY_PIC_FAN0_STATUS, value);
  1466. }
  1467. static int sony_pic_get_fanspeed(u8 *value)
  1468. {
  1469. return ec_read(SONY_PIC_FAN0_STATUS, value);
  1470. }
  1471. static ssize_t sony_pic_fanspeed_store(struct device *dev,
  1472. struct device_attribute *attr,
  1473. const char *buffer, size_t count)
  1474. {
  1475. unsigned long value;
  1476. if (count > 31)
  1477. return -EINVAL;
  1478. value = simple_strtoul(buffer, NULL, 10);
  1479. if (sony_pic_set_fanspeed(value))
  1480. return -EIO;
  1481. return count;
  1482. }
  1483. static ssize_t sony_pic_fanspeed_show(struct device *dev,
  1484. struct device_attribute *attr, char *buffer)
  1485. {
  1486. u8 value = 0;
  1487. if (sony_pic_get_fanspeed(&value))
  1488. return -EIO;
  1489. return snprintf(buffer, PAGE_SIZE, "%d\n", value);
  1490. }
  1491. #define SPIC_ATTR(_name, _mode) \
  1492. struct device_attribute spic_attr_##_name = __ATTR(_name, \
  1493. _mode, sony_pic_## _name ##_show, \
  1494. sony_pic_## _name ##_store)
  1495. static SPIC_ATTR(bluetoothpower, 0644);
  1496. static SPIC_ATTR(wwanpower, 0644);
  1497. static SPIC_ATTR(fanspeed, 0644);
  1498. static struct attribute *spic_attributes[] = {
  1499. &spic_attr_bluetoothpower.attr,
  1500. &spic_attr_wwanpower.attr,
  1501. &spic_attr_fanspeed.attr,
  1502. NULL
  1503. };
  1504. static struct attribute_group spic_attribute_group = {
  1505. .attrs = spic_attributes
  1506. };
  1507. /******** SONYPI compatibility **********/
  1508. #ifdef CONFIG_SONYPI_COMPAT
  1509. /* battery / brightness / temperature addresses */
  1510. #define SONYPI_BAT_FLAGS 0x81
  1511. #define SONYPI_LCD_LIGHT 0x96
  1512. #define SONYPI_BAT1_PCTRM 0xa0
  1513. #define SONYPI_BAT1_LEFT 0xa2
  1514. #define SONYPI_BAT1_MAXRT 0xa4
  1515. #define SONYPI_BAT2_PCTRM 0xa8
  1516. #define SONYPI_BAT2_LEFT 0xaa
  1517. #define SONYPI_BAT2_MAXRT 0xac
  1518. #define SONYPI_BAT1_MAXTK 0xb0
  1519. #define SONYPI_BAT1_FULL 0xb2
  1520. #define SONYPI_BAT2_MAXTK 0xb8
  1521. #define SONYPI_BAT2_FULL 0xba
  1522. #define SONYPI_TEMP_STATUS 0xC1
  1523. struct sonypi_compat_s {
  1524. struct fasync_struct *fifo_async;
  1525. struct kfifo *fifo;
  1526. spinlock_t fifo_lock;
  1527. wait_queue_head_t fifo_proc_list;
  1528. atomic_t open_count;
  1529. };
  1530. static struct sonypi_compat_s sonypi_compat = {
  1531. .open_count = ATOMIC_INIT(0),
  1532. };
  1533. static int sonypi_misc_fasync(int fd, struct file *filp, int on)
  1534. {
  1535. int retval;
  1536. retval = fasync_helper(fd, filp, on, &sonypi_compat.fifo_async);
  1537. if (retval < 0)
  1538. return retval;
  1539. return 0;
  1540. }
  1541. static int sonypi_misc_release(struct inode *inode, struct file *file)
  1542. {
  1543. sonypi_misc_fasync(-1, file, 0);
  1544. atomic_dec(&sonypi_compat.open_count);
  1545. return 0;
  1546. }
  1547. static int sonypi_misc_open(struct inode *inode, struct file *file)
  1548. {
  1549. /* Flush input queue on first open */
  1550. if (atomic_inc_return(&sonypi_compat.open_count) == 1)
  1551. kfifo_reset(sonypi_compat.fifo);
  1552. return 0;
  1553. }
  1554. static ssize_t sonypi_misc_read(struct file *file, char __user *buf,
  1555. size_t count, loff_t *pos)
  1556. {
  1557. ssize_t ret;
  1558. unsigned char c;
  1559. if ((kfifo_len(sonypi_compat.fifo) == 0) &&
  1560. (file->f_flags & O_NONBLOCK))
  1561. return -EAGAIN;
  1562. ret = wait_event_interruptible(sonypi_compat.fifo_proc_list,
  1563. kfifo_len(sonypi_compat.fifo) != 0);
  1564. if (ret)
  1565. return ret;
  1566. while (ret < count &&
  1567. (kfifo_get(sonypi_compat.fifo, &c, sizeof(c)) == sizeof(c))) {
  1568. if (put_user(c, buf++))
  1569. return -EFAULT;
  1570. ret++;
  1571. }
  1572. if (ret > 0) {
  1573. struct inode *inode = file->f_path.dentry->d_inode;
  1574. inode->i_atime = current_fs_time(inode->i_sb);
  1575. }
  1576. return ret;
  1577. }
  1578. static unsigned int sonypi_misc_poll(struct file *file, poll_table *wait)
  1579. {
  1580. poll_wait(file, &sonypi_compat.fifo_proc_list, wait);
  1581. if (kfifo_len(sonypi_compat.fifo))
  1582. return POLLIN | POLLRDNORM;
  1583. return 0;
  1584. }
  1585. static int ec_read16(u8 addr, u16 *value)
  1586. {
  1587. u8 val_lb, val_hb;
  1588. if (ec_read(addr, &val_lb))
  1589. return -1;
  1590. if (ec_read(addr + 1, &val_hb))
  1591. return -1;
  1592. *value = val_lb | (val_hb << 8);
  1593. return 0;
  1594. }
  1595. static int sonypi_misc_ioctl(struct inode *ip, struct file *fp,
  1596. unsigned int cmd, unsigned long arg)
  1597. {
  1598. int ret = 0;
  1599. void __user *argp = (void __user *)arg;
  1600. u8 val8;
  1601. u16 val16;
  1602. int value;
  1603. mutex_lock(&spic_dev.lock);
  1604. switch (cmd) {
  1605. case SONYPI_IOCGBRT:
  1606. if (sony_backlight_device == NULL) {
  1607. ret = -EIO;
  1608. break;
  1609. }
  1610. if (acpi_callgetfunc(sony_nc_acpi_handle, "GBRT", &value)) {
  1611. ret = -EIO;
  1612. break;
  1613. }
  1614. val8 = ((value & 0xff) - 1) << 5;
  1615. if (copy_to_user(argp, &val8, sizeof(val8)))
  1616. ret = -EFAULT;
  1617. break;
  1618. case SONYPI_IOCSBRT:
  1619. if (sony_backlight_device == NULL) {
  1620. ret = -EIO;
  1621. break;
  1622. }
  1623. if (copy_from_user(&val8, argp, sizeof(val8))) {
  1624. ret = -EFAULT;
  1625. break;
  1626. }
  1627. if (acpi_callsetfunc(sony_nc_acpi_handle, "SBRT",
  1628. (val8 >> 5) + 1, NULL)) {
  1629. ret = -EIO;
  1630. break;
  1631. }
  1632. /* sync the backlight device status */
  1633. sony_backlight_device->props.brightness =
  1634. sony_backlight_get_brightness(sony_backlight_device);
  1635. break;
  1636. case SONYPI_IOCGBAT1CAP:
  1637. if (ec_read16(SONYPI_BAT1_FULL, &val16)) {
  1638. ret = -EIO;
  1639. break;
  1640. }
  1641. if (copy_to_user(argp, &val16, sizeof(val16)))
  1642. ret = -EFAULT;
  1643. break;
  1644. case SONYPI_IOCGBAT1REM:
  1645. if (ec_read16(SONYPI_BAT1_LEFT, &val16)) {
  1646. ret = -EIO;
  1647. break;
  1648. }
  1649. if (copy_to_user(argp, &val16, sizeof(val16)))
  1650. ret = -EFAULT;
  1651. break;
  1652. case SONYPI_IOCGBAT2CAP:
  1653. if (ec_read16(SONYPI_BAT2_FULL, &val16)) {
  1654. ret = -EIO;
  1655. break;
  1656. }
  1657. if (copy_to_user(argp, &val16, sizeof(val16)))
  1658. ret = -EFAULT;
  1659. break;
  1660. case SONYPI_IOCGBAT2REM:
  1661. if (ec_read16(SONYPI_BAT2_LEFT, &val16)) {
  1662. ret = -EIO;
  1663. break;
  1664. }
  1665. if (copy_to_user(argp, &val16, sizeof(val16)))
  1666. ret = -EFAULT;
  1667. break;
  1668. case SONYPI_IOCGBATFLAGS:
  1669. if (ec_read(SONYPI_BAT_FLAGS, &val8)) {
  1670. ret = -EIO;
  1671. break;
  1672. }
  1673. val8 &= 0x07;
  1674. if (copy_to_user(argp, &val8, sizeof(val8)))
  1675. ret = -EFAULT;
  1676. break;
  1677. case SONYPI_IOCGBLUE:
  1678. val8 = spic_dev.bluetooth_power;
  1679. if (copy_to_user(argp, &val8, sizeof(val8)))
  1680. ret = -EFAULT;
  1681. break;
  1682. case SONYPI_IOCSBLUE:
  1683. if (copy_from_user(&val8, argp, sizeof(val8))) {
  1684. ret = -EFAULT;
  1685. break;
  1686. }
  1687. __sony_pic_set_bluetoothpower(val8);
  1688. break;
  1689. /* FAN Controls */
  1690. case SONYPI_IOCGFAN:
  1691. if (sony_pic_get_fanspeed(&val8)) {
  1692. ret = -EIO;
  1693. break;
  1694. }
  1695. if (copy_to_user(argp, &val8, sizeof(val8)))
  1696. ret = -EFAULT;
  1697. break;
  1698. case SONYPI_IOCSFAN:
  1699. if (copy_from_user(&val8, argp, sizeof(val8))) {
  1700. ret = -EFAULT;
  1701. break;
  1702. }
  1703. if (sony_pic_set_fanspeed(val8))
  1704. ret = -EIO;
  1705. break;
  1706. /* GET Temperature (useful under APM) */
  1707. case SONYPI_IOCGTEMP:
  1708. if (ec_read(SONYPI_TEMP_STATUS, &val8)) {
  1709. ret = -EIO;
  1710. break;
  1711. }
  1712. if (copy_to_user(argp, &val8, sizeof(val8)))
  1713. ret = -EFAULT;
  1714. break;
  1715. default:
  1716. ret = -EINVAL;
  1717. }
  1718. mutex_unlock(&spic_dev.lock);
  1719. return ret;
  1720. }
  1721. static const struct file_operations sonypi_misc_fops = {
  1722. .owner = THIS_MODULE,
  1723. .read = sonypi_misc_read,
  1724. .poll = sonypi_misc_poll,
  1725. .open = sonypi_misc_open,
  1726. .release = sonypi_misc_release,
  1727. .fasync = sonypi_misc_fasync,
  1728. .ioctl = sonypi_misc_ioctl,
  1729. };
  1730. static struct miscdevice sonypi_misc_device = {
  1731. .minor = MISC_DYNAMIC_MINOR,
  1732. .name = "sonypi",
  1733. .fops = &sonypi_misc_fops,
  1734. };
  1735. static void sonypi_compat_report_event(u8 event)
  1736. {
  1737. kfifo_put(sonypi_compat.fifo, (unsigned char *)&event, sizeof(event));
  1738. kill_fasync(&sonypi_compat.fifo_async, SIGIO, POLL_IN);
  1739. wake_up_interruptible(&sonypi_compat.fifo_proc_list);
  1740. }
  1741. static int sonypi_compat_init(void)
  1742. {
  1743. int error;
  1744. spin_lock_init(&sonypi_compat.fifo_lock);
  1745. sonypi_compat.fifo = kfifo_alloc(SONY_LAPTOP_BUF_SIZE, GFP_KERNEL,
  1746. &sonypi_compat.fifo_lock);
  1747. if (IS_ERR(sonypi_compat.fifo)) {
  1748. printk(KERN_ERR DRV_PFX "kfifo_alloc failed\n");
  1749. return PTR_ERR(sonypi_compat.fifo);
  1750. }
  1751. init_waitqueue_head(&sonypi_compat.fifo_proc_list);
  1752. if (minor != -1)
  1753. sonypi_misc_device.minor = minor;
  1754. error = misc_register(&sonypi_misc_device);
  1755. if (error) {
  1756. printk(KERN_ERR DRV_PFX "misc_register failed\n");
  1757. goto err_free_kfifo;
  1758. }
  1759. if (minor == -1)
  1760. printk(KERN_INFO DRV_PFX "device allocated minor is %d\n",
  1761. sonypi_misc_device.minor);
  1762. return 0;
  1763. err_free_kfifo:
  1764. kfifo_free(sonypi_compat.fifo);
  1765. return error;
  1766. }
  1767. static void sonypi_compat_exit(void)
  1768. {
  1769. misc_deregister(&sonypi_misc_device);
  1770. kfifo_free(sonypi_compat.fifo);
  1771. }
  1772. #else
  1773. static int sonypi_compat_init(void) { return 0; }
  1774. static void sonypi_compat_exit(void) { }
  1775. static void sonypi_compat_report_event(u8 event) { }
  1776. #endif /* CONFIG_SONYPI_COMPAT */
  1777. /*
  1778. * ACPI callbacks
  1779. */
  1780. static acpi_status
  1781. sony_pic_read_possible_resource(struct acpi_resource *resource, void *context)
  1782. {
  1783. u32 i;
  1784. struct sony_pic_dev *dev = (struct sony_pic_dev *)context;
  1785. switch (resource->type) {
  1786. case ACPI_RESOURCE_TYPE_START_DEPENDENT:
  1787. {
  1788. /* start IO enumeration */
  1789. struct sony_pic_ioport *ioport = kzalloc(sizeof(*ioport), GFP_KERNEL);
  1790. if (!ioport)
  1791. return AE_ERROR;
  1792. list_add(&ioport->list, &dev->ioports);
  1793. return AE_OK;
  1794. }
  1795. case ACPI_RESOURCE_TYPE_END_DEPENDENT:
  1796. /* end IO enumeration */
  1797. return AE_OK;
  1798. case ACPI_RESOURCE_TYPE_IRQ:
  1799. {
  1800. struct acpi_resource_irq *p = &resource->data.irq;
  1801. struct sony_pic_irq *interrupt = NULL;
  1802. if (!p || !p->interrupt_count) {
  1803. /*
  1804. * IRQ descriptors may have no IRQ# bits set,
  1805. * particularly those those w/ _STA disabled
  1806. */
  1807. dprintk("Blank IRQ resource\n");
  1808. return AE_OK;
  1809. }
  1810. for (i = 0; i < p->interrupt_count; i++) {
  1811. if (!p->interrupts[i]) {
  1812. printk(KERN_WARNING DRV_PFX
  1813. "Invalid IRQ %d\n",
  1814. p->interrupts[i]);
  1815. continue;
  1816. }
  1817. interrupt = kzalloc(sizeof(*interrupt),
  1818. GFP_KERNEL);
  1819. if (!interrupt)
  1820. return AE_ERROR;
  1821. list_add(&interrupt->list, &dev->interrupts);
  1822. interrupt->irq.triggering = p->triggering;
  1823. interrupt->irq.polarity = p->polarity;
  1824. interrupt->irq.sharable = p->sharable;
  1825. interrupt->irq.interrupt_count = 1;
  1826. interrupt->irq.interrupts[0] = p->interrupts[i];
  1827. }
  1828. return AE_OK;
  1829. }
  1830. case ACPI_RESOURCE_TYPE_IO:
  1831. {
  1832. struct acpi_resource_io *io = &resource->data.io;
  1833. struct sony_pic_ioport *ioport =
  1834. list_first_entry(&dev->ioports, struct sony_pic_ioport, list);
  1835. if (!io) {
  1836. dprintk("Blank IO resource\n");
  1837. return AE_OK;
  1838. }
  1839. if (!ioport->io1.minimum) {
  1840. memcpy(&ioport->io1, io, sizeof(*io));
  1841. dprintk("IO1 at 0x%.4x (0x%.2x)\n", ioport->io1.minimum,
  1842. ioport->io1.address_length);
  1843. }
  1844. else if (!ioport->io2.minimum) {
  1845. memcpy(&ioport->io2, io, sizeof(*io));
  1846. dprintk("IO2 at 0x%.4x (0x%.2x)\n", ioport->io2.minimum,
  1847. ioport->io2.address_length);
  1848. }
  1849. else {
  1850. printk(KERN_ERR DRV_PFX "Unknown SPIC Type, more than 2 IO Ports\n");
  1851. return AE_ERROR;
  1852. }
  1853. return AE_OK;
  1854. }
  1855. default:
  1856. dprintk("Resource %d isn't an IRQ nor an IO port\n",
  1857. resource->type);
  1858. case ACPI_RESOURCE_TYPE_END_TAG:
  1859. return AE_OK;
  1860. }
  1861. return AE_CTRL_TERMINATE;
  1862. }
  1863. static int sony_pic_possible_resources(struct acpi_device *device)
  1864. {
  1865. int result = 0;
  1866. acpi_status status = AE_OK;
  1867. if (!device)
  1868. return -EINVAL;
  1869. /* get device status */
  1870. /* see acpi_pci_link_get_current acpi_pci_link_get_possible */
  1871. dprintk("Evaluating _STA\n");
  1872. result = acpi_bus_get_status(device);
  1873. if (result) {
  1874. printk(KERN_WARNING DRV_PFX "Unable to read status\n");
  1875. goto end;
  1876. }
  1877. if (!device->status.enabled)
  1878. dprintk("Device disabled\n");
  1879. else
  1880. dprintk("Device enabled\n");
  1881. /*
  1882. * Query and parse 'method'
  1883. */
  1884. dprintk("Evaluating %s\n", METHOD_NAME__PRS);
  1885. status = acpi_walk_resources(device->handle, METHOD_NAME__PRS,
  1886. sony_pic_read_possible_resource, &spic_dev);
  1887. if (ACPI_FAILURE(status)) {
  1888. printk(KERN_WARNING DRV_PFX
  1889. "Failure evaluating %s\n",
  1890. METHOD_NAME__PRS);
  1891. result = -ENODEV;
  1892. }
  1893. end:
  1894. return result;
  1895. }
  1896. /*
  1897. * Disable the spic device by calling its _DIS method
  1898. */
  1899. static int sony_pic_disable(struct acpi_device *device)
  1900. {
  1901. if (ACPI_FAILURE(acpi_evaluate_object(device->handle,
  1902. "_DIS", NULL, NULL)))
  1903. return -ENXIO;
  1904. dprintk("Device disabled\n");
  1905. return 0;
  1906. }
  1907. /*
  1908. * Based on drivers/acpi/pci_link.c:acpi_pci_link_set
  1909. *
  1910. * Call _SRS to set current resources
  1911. */
  1912. static int sony_pic_enable(struct acpi_device *device,
  1913. struct sony_pic_ioport *ioport, struct sony_pic_irq *irq)
  1914. {
  1915. acpi_status status;
  1916. int result = 0;
  1917. /* Type 1 resource layout is:
  1918. * IO
  1919. * IO
  1920. * IRQNoFlags
  1921. * End
  1922. *
  1923. * Type 2 and 3 resource layout is:
  1924. * IO
  1925. * IRQNoFlags
  1926. * End
  1927. */
  1928. struct {
  1929. struct acpi_resource res1;
  1930. struct acpi_resource res2;
  1931. struct acpi_resource res3;
  1932. struct acpi_resource res4;
  1933. } *resource;
  1934. struct acpi_buffer buffer = { 0, NULL };
  1935. if (!ioport || !irq)
  1936. return -EINVAL;
  1937. /* init acpi_buffer */
  1938. resource = kzalloc(sizeof(*resource) + 1, GFP_KERNEL);
  1939. if (!resource)
  1940. return -ENOMEM;
  1941. buffer.length = sizeof(*resource) + 1;
  1942. buffer.pointer = resource;
  1943. /* setup Type 1 resources */
  1944. if (spic_dev.model == SONYPI_DEVICE_TYPE1) {
  1945. /* setup io resources */
  1946. resource->res1.type = ACPI_RESOURCE_TYPE_IO;
  1947. resource->res1.length = sizeof(struct acpi_resource);
  1948. memcpy(&resource->res1.data.io, &ioport->io1,
  1949. sizeof(struct acpi_resource_io));
  1950. resource->res2.type = ACPI_RESOURCE_TYPE_IO;
  1951. resource->res2.length = sizeof(struct acpi_resource);
  1952. memcpy(&resource->res2.data.io, &ioport->io2,
  1953. sizeof(struct acpi_resource_io));
  1954. /* setup irq resource */
  1955. resource->res3.type = ACPI_RESOURCE_TYPE_IRQ;
  1956. resource->res3.length = sizeof(struct acpi_resource);
  1957. memcpy(&resource->res3.data.irq, &irq->irq,
  1958. sizeof(struct acpi_resource_irq));
  1959. /* we requested a shared irq */
  1960. resource->res3.data.irq.sharable = ACPI_SHARED;
  1961. resource->res4.type = ACPI_RESOURCE_TYPE_END_TAG;
  1962. }
  1963. /* setup Type 2/3 resources */
  1964. else {
  1965. /* setup io resource */
  1966. resource->res1.type = ACPI_RESOURCE_TYPE_IO;
  1967. resource->res1.length = sizeof(struct acpi_resource);
  1968. memcpy(&resource->res1.data.io, &ioport->io1,
  1969. sizeof(struct acpi_resource_io));
  1970. /* setup irq resource */
  1971. resource->res2.type = ACPI_RESOURCE_TYPE_IRQ;
  1972. resource->res2.length = sizeof(struct acpi_resource);
  1973. memcpy(&resource->res2.data.irq, &irq->irq,
  1974. sizeof(struct acpi_resource_irq));
  1975. /* we requested a shared irq */
  1976. resource->res2.data.irq.sharable = ACPI_SHARED;
  1977. resource->res3.type = ACPI_RESOURCE_TYPE_END_TAG;
  1978. }
  1979. /* Attempt to set the resource */
  1980. dprintk("Evaluating _SRS\n");
  1981. status = acpi_set_current_resources(device->handle, &buffer);
  1982. /* check for total failure */
  1983. if (ACPI_FAILURE(status)) {
  1984. printk(KERN_ERR DRV_PFX "Error evaluating _SRS\n");
  1985. result = -ENODEV;
  1986. goto end;
  1987. }
  1988. /* Necessary device initializations calls (from sonypi) */
  1989. sony_pic_call1(0x82);
  1990. sony_pic_call2(0x81, 0xff);
  1991. sony_pic_call1(compat ? 0x92 : 0x82);
  1992. end:
  1993. kfree(resource);
  1994. return result;
  1995. }
  1996. /*****************
  1997. *
  1998. * ISR: some event is available
  1999. *
  2000. *****************/
  2001. static irqreturn_t sony_pic_irq(int irq, void *dev_id)
  2002. {
  2003. int i, j;
  2004. u8 ev = 0;
  2005. u8 data_mask = 0;
  2006. u8 device_event = 0;
  2007. struct sony_pic_dev *dev = (struct sony_pic_dev *) dev_id;
  2008. ev = inb_p(dev->cur_ioport->io1.minimum);
  2009. if (dev->cur_ioport->io2.minimum)
  2010. data_mask = inb_p(dev->cur_ioport->io2.minimum);
  2011. else
  2012. data_mask = inb_p(dev->cur_ioport->io1.minimum + dev->evport_offset);
  2013. dprintk("event ([%.2x] [%.2x]) at port 0x%.4x(+0x%.2x)\n",
  2014. ev, data_mask, dev->cur_ioport->io1.minimum, dev->evport_offset);
  2015. if (ev == 0x00 || ev == 0xff)
  2016. return IRQ_HANDLED;
  2017. for (i = 0; sony_pic_eventtypes[i].model; i++) {
  2018. if (spic_dev.model != sony_pic_eventtypes[i].model)
  2019. continue;
  2020. if ((data_mask & sony_pic_eventtypes[i].data) !=
  2021. sony_pic_eventtypes[i].data)
  2022. continue;
  2023. if (!(mask & sony_pic_eventtypes[i].mask))
  2024. continue;
  2025. for (j = 0; sony_pic_eventtypes[i].events[j].event; j++) {
  2026. if (ev == sony_pic_eventtypes[i].events[j].data) {
  2027. device_event =
  2028. sony_pic_eventtypes[i].events[j].event;
  2029. goto found;
  2030. }
  2031. }
  2032. }
  2033. return IRQ_HANDLED;
  2034. found:
  2035. sony_laptop_report_input_event(device_event);
  2036. acpi_bus_generate_proc_event(spic_dev.acpi_dev, 1, device_event);
  2037. sonypi_compat_report_event(device_event);
  2038. return IRQ_HANDLED;
  2039. }
  2040. /*****************
  2041. *
  2042. * ACPI driver
  2043. *
  2044. *****************/
  2045. static int sony_pic_remove(struct acpi_device *device, int type)
  2046. {
  2047. struct sony_pic_ioport *io, *tmp_io;
  2048. struct sony_pic_irq *irq, *tmp_irq;
  2049. if (sony_pic_disable(device)) {
  2050. printk(KERN_ERR DRV_PFX "Couldn't disable device.\n");
  2051. return -ENXIO;
  2052. }
  2053. free_irq(spic_dev.cur_irq->irq.interrupts[0], &spic_dev);
  2054. release_region(spic_dev.cur_ioport->io1.minimum,
  2055. spic_dev.cur_ioport->io1.address_length);
  2056. if (spic_dev.cur_ioport->io2.minimum)
  2057. release_region(spic_dev.cur_ioport->io2.minimum,
  2058. spic_dev.cur_ioport->io2.address_length);
  2059. sonypi_compat_exit();
  2060. sony_laptop_remove_input();
  2061. /* pf attrs */
  2062. sysfs_remove_group(&sony_pf_device->dev.kobj, &spic_attribute_group);
  2063. sony_pf_remove();
  2064. list_for_each_entry_safe(io, tmp_io, &spic_dev.ioports, list) {
  2065. list_del(&io->list);
  2066. kfree(io);
  2067. }
  2068. list_for_each_entry_safe(irq, tmp_irq, &spic_dev.interrupts, list) {
  2069. list_del(&irq->list);
  2070. kfree(irq);
  2071. }
  2072. spic_dev.cur_ioport = NULL;
  2073. spic_dev.cur_irq = NULL;
  2074. dprintk(SONY_PIC_DRIVER_NAME " removed.\n");
  2075. return 0;
  2076. }
  2077. static int sony_pic_add(struct acpi_device *device)
  2078. {
  2079. int result;
  2080. struct sony_pic_ioport *io, *tmp_io;
  2081. struct sony_pic_irq *irq, *tmp_irq;
  2082. printk(KERN_INFO DRV_PFX "%s v%s.\n",
  2083. SONY_PIC_DRIVER_NAME, SONY_LAPTOP_DRIVER_VERSION);
  2084. spic_dev.acpi_dev = device;
  2085. strcpy(acpi_device_class(device), "sony/hotkey");
  2086. spic_dev.model = sony_pic_detect_device_type();
  2087. mutex_init(&spic_dev.lock);
  2088. /* model specific characteristics */
  2089. switch(spic_dev.model) {
  2090. case SONYPI_DEVICE_TYPE1:
  2091. spic_dev.evport_offset = SONYPI_TYPE1_OFFSET;
  2092. break;
  2093. case SONYPI_DEVICE_TYPE3:
  2094. spic_dev.evport_offset = SONYPI_TYPE3_OFFSET;
  2095. break;
  2096. case SONYPI_DEVICE_TYPE2:
  2097. default:
  2098. spic_dev.evport_offset = SONYPI_TYPE2_OFFSET;
  2099. break;
  2100. }
  2101. /* read _PRS resources */
  2102. result = sony_pic_possible_resources(device);
  2103. if (result) {
  2104. printk(KERN_ERR DRV_PFX
  2105. "Unabe to read possible resources.\n");
  2106. goto err_free_resources;
  2107. }
  2108. /* setup input devices and helper fifo */
  2109. result = sony_laptop_setup_input();
  2110. if (result) {
  2111. printk(KERN_ERR DRV_PFX
  2112. "Unabe to create input devices.\n");
  2113. goto err_free_resources;
  2114. }
  2115. if (sonypi_compat_init())
  2116. goto err_remove_input;
  2117. /* request io port */
  2118. list_for_each_entry_reverse(io, &spic_dev.ioports, list) {
  2119. if (request_region(io->io1.minimum, io->io1.address_length,
  2120. "Sony Programable I/O Device")) {
  2121. dprintk("I/O port1: 0x%.4x (0x%.4x) + 0x%.2x\n",
  2122. io->io1.minimum, io->io1.maximum,
  2123. io->io1.address_length);
  2124. /* Type 1 have 2 ioports */
  2125. if (io->io2.minimum) {
  2126. if (request_region(io->io2.minimum,
  2127. io->io2.address_length,
  2128. "Sony Programable I/O Device")) {
  2129. dprintk("I/O port2: 0x%.4x (0x%.4x) + 0x%.2x\n",
  2130. io->io2.minimum, io->io2.maximum,
  2131. io->io2.address_length);
  2132. spic_dev.cur_ioport = io;
  2133. break;
  2134. }
  2135. else {
  2136. dprintk("Unable to get I/O port2: "
  2137. "0x%.4x (0x%.4x) + 0x%.2x\n",
  2138. io->io2.minimum, io->io2.maximum,
  2139. io->io2.address_length);
  2140. release_region(io->io1.minimum,
  2141. io->io1.address_length);
  2142. }
  2143. }
  2144. else {
  2145. spic_dev.cur_ioport = io;
  2146. break;
  2147. }
  2148. }
  2149. }
  2150. if (!spic_dev.cur_ioport) {
  2151. printk(KERN_ERR DRV_PFX "Failed to request_region.\n");
  2152. result = -ENODEV;
  2153. goto err_remove_compat;
  2154. }
  2155. /* request IRQ */
  2156. list_for_each_entry_reverse(irq, &spic_dev.interrupts, list) {
  2157. if (!request_irq(irq->irq.interrupts[0], sony_pic_irq,
  2158. IRQF_SHARED, "sony-laptop", &spic_dev)) {
  2159. dprintk("IRQ: %d - triggering: %d - "
  2160. "polarity: %d - shr: %d\n",
  2161. irq->irq.interrupts[0],
  2162. irq->irq.triggering,
  2163. irq->irq.polarity,
  2164. irq->irq.sharable);
  2165. spic_dev.cur_irq = irq;
  2166. break;
  2167. }
  2168. }
  2169. if (!spic_dev.cur_irq) {
  2170. printk(KERN_ERR DRV_PFX "Failed to request_irq.\n");
  2171. result = -ENODEV;
  2172. goto err_release_region;
  2173. }
  2174. /* set resource status _SRS */
  2175. result = sony_pic_enable(device, spic_dev.cur_ioport, spic_dev.cur_irq);
  2176. if (result) {
  2177. printk(KERN_ERR DRV_PFX "Couldn't enable device.\n");
  2178. goto err_free_irq;
  2179. }
  2180. spic_dev.bluetooth_power = -1;
  2181. /* create device attributes */
  2182. result = sony_pf_add();
  2183. if (result)
  2184. goto err_disable_device;
  2185. result = sysfs_create_group(&sony_pf_device->dev.kobj, &spic_attribute_group);
  2186. if (result)
  2187. goto err_remove_pf;
  2188. return 0;
  2189. err_remove_pf:
  2190. sony_pf_remove();
  2191. err_disable_device:
  2192. sony_pic_disable(device);
  2193. err_free_irq:
  2194. free_irq(spic_dev.cur_irq->irq.interrupts[0], &spic_dev);
  2195. err_release_region:
  2196. release_region(spic_dev.cur_ioport->io1.minimum,
  2197. spic_dev.cur_ioport->io1.address_length);
  2198. if (spic_dev.cur_ioport->io2.minimum)
  2199. release_region(spic_dev.cur_ioport->io2.minimum,
  2200. spic_dev.cur_ioport->io2.address_length);
  2201. err_remove_compat:
  2202. sonypi_compat_exit();
  2203. err_remove_input:
  2204. sony_laptop_remove_input();
  2205. err_free_resources:
  2206. list_for_each_entry_safe(io, tmp_io, &spic_dev.ioports, list) {
  2207. list_del(&io->list);
  2208. kfree(io);
  2209. }
  2210. list_for_each_entry_safe(irq, tmp_irq, &spic_dev.interrupts, list) {
  2211. list_del(&irq->list);
  2212. kfree(irq);
  2213. }
  2214. spic_dev.cur_ioport = NULL;
  2215. spic_dev.cur_irq = NULL;
  2216. return result;
  2217. }
  2218. static int sony_pic_suspend(struct acpi_device *device, pm_message_t state)
  2219. {
  2220. if (sony_pic_disable(device))
  2221. return -ENXIO;
  2222. return 0;
  2223. }
  2224. static int sony_pic_resume(struct acpi_device *device)
  2225. {
  2226. sony_pic_enable(device, spic_dev.cur_ioport, spic_dev.cur_irq);
  2227. return 0;
  2228. }
  2229. static const struct acpi_device_id sony_pic_device_ids[] = {
  2230. {SONY_PIC_HID, 0},
  2231. {"", 0},
  2232. };
  2233. static struct acpi_driver sony_pic_driver = {
  2234. .name = SONY_PIC_DRIVER_NAME,
  2235. .class = SONY_PIC_CLASS,
  2236. .ids = sony_pic_device_ids,
  2237. .owner = THIS_MODULE,
  2238. .ops = {
  2239. .add = sony_pic_add,
  2240. .remove = sony_pic_remove,
  2241. .suspend = sony_pic_suspend,
  2242. .resume = sony_pic_resume,
  2243. },
  2244. };
  2245. static struct dmi_system_id __initdata sonypi_dmi_table[] = {
  2246. {
  2247. .ident = "Sony Vaio",
  2248. .matches = {
  2249. DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
  2250. DMI_MATCH(DMI_PRODUCT_NAME, "PCG-"),
  2251. },
  2252. },
  2253. {
  2254. .ident = "Sony Vaio",
  2255. .matches = {
  2256. DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
  2257. DMI_MATCH(DMI_PRODUCT_NAME, "VGN-"),
  2258. },
  2259. },
  2260. { }
  2261. };
  2262. static int __init sony_laptop_init(void)
  2263. {
  2264. int result;
  2265. if (!no_spic && dmi_check_system(sonypi_dmi_table)) {
  2266. result = acpi_bus_register_driver(&sony_pic_driver);
  2267. if (result) {
  2268. printk(KERN_ERR DRV_PFX
  2269. "Unable to register SPIC driver.");
  2270. goto out;
  2271. }
  2272. }
  2273. result = acpi_bus_register_driver(&sony_nc_driver);
  2274. if (result) {
  2275. printk(KERN_ERR DRV_PFX "Unable to register SNC driver.");
  2276. goto out_unregister_pic;
  2277. }
  2278. return 0;
  2279. out_unregister_pic:
  2280. if (!no_spic)
  2281. acpi_bus_unregister_driver(&sony_pic_driver);
  2282. out:
  2283. return result;
  2284. }
  2285. static void __exit sony_laptop_exit(void)
  2286. {
  2287. acpi_bus_unregister_driver(&sony_nc_driver);
  2288. if (!no_spic)
  2289. acpi_bus_unregister_driver(&sony_pic_driver);
  2290. }
  2291. module_init(sony_laptop_init);
  2292. module_exit(sony_laptop_exit);