sony-laptop.c 76 KB

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