sony-laptop.c 84 KB

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