sony-laptop.c 77 KB

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