sony-laptop.c 77 KB

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