sony-laptop.c 77 KB

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