sony-laptop.c 76 KB

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