sony-laptop.c 78 KB

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