sony-laptop.c 87 KB

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