sony-laptop.c 78 KB

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