sonypi.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Sony Programmable I/O Control Device driver for VAIO
  4. *
  5. * Copyright (C) 2007 Mattia Dongili <malattia@linux.it>
  6. *
  7. * Copyright (C) 2001-2005 Stelian Pop <stelian@popies.net>
  8. *
  9. * Copyright (C) 2005 Narayanan R S <nars@kadamba.org>
  10. *
  11. * Copyright (C) 2001-2002 Alcôve <www.alcove.com>
  12. *
  13. * Copyright (C) 2001 Michael Ashley <m.ashley@unsw.edu.au>
  14. *
  15. * Copyright (C) 2001 Junichi Morita <jun1m@mars.dti.ne.jp>
  16. *
  17. * Copyright (C) 2000 Takaya Kinjo <t-kinjo@tc4.so-net.ne.jp>
  18. *
  19. * Copyright (C) 2000 Andrew Tridgell <tridge@valinux.com>
  20. *
  21. * Earlier work by Werner Almesberger, Paul `Rusty' Russell and Paul Mackerras.
  22. */
  23. #include <linux/module.h>
  24. #include <linux/sched.h>
  25. #include <linux/input.h>
  26. #include <linux/pci.h>
  27. #include <linux/init.h>
  28. #include <linux/interrupt.h>
  29. #include <linux/miscdevice.h>
  30. #include <linux/poll.h>
  31. #include <linux/delay.h>
  32. #include <linux/wait.h>
  33. #include <linux/acpi.h>
  34. #include <linux/dmi.h>
  35. #include <linux/err.h>
  36. #include <linux/kfifo.h>
  37. #include <linux/platform_device.h>
  38. #include <linux/gfp.h>
  39. #include <linux/string_choices.h>
  40. #include <linux/uaccess.h>
  41. #include <asm/io.h>
  42. #include <linux/sonypi.h>
  43. #define SONYPI_DRIVER_VERSION "1.26"
  44. MODULE_AUTHOR("Stelian Pop <stelian@popies.net>");
  45. MODULE_DESCRIPTION("Sony Programmable I/O Control Device driver");
  46. MODULE_LICENSE("GPL");
  47. MODULE_VERSION(SONYPI_DRIVER_VERSION);
  48. static int minor = -1;
  49. module_param(minor, int, 0);
  50. MODULE_PARM_DESC(minor,
  51. "minor number of the misc device, default is -1 (automatic)");
  52. static int verbose; /* = 0 */
  53. module_param(verbose, int, 0644);
  54. MODULE_PARM_DESC(verbose, "be verbose, default is 0 (no)");
  55. static int fnkeyinit; /* = 0 */
  56. module_param(fnkeyinit, int, 0444);
  57. MODULE_PARM_DESC(fnkeyinit,
  58. "set this if your Fn keys do not generate any event");
  59. static int camera; /* = 0 */
  60. module_param(camera, int, 0444);
  61. MODULE_PARM_DESC(camera,
  62. "set this if you have a MotionEye camera (PictureBook series)");
  63. static int compat; /* = 0 */
  64. module_param(compat, int, 0444);
  65. MODULE_PARM_DESC(compat,
  66. "set this if you want to enable backward compatibility mode");
  67. static unsigned long mask = 0xffffffff;
  68. module_param(mask, ulong, 0644);
  69. MODULE_PARM_DESC(mask,
  70. "set this to the mask of event you want to enable (see doc)");
  71. static int useinput = 1;
  72. module_param(useinput, int, 0444);
  73. MODULE_PARM_DESC(useinput,
  74. "set this if you would like sonypi to feed events to the input subsystem");
  75. static int check_ioport = 1;
  76. module_param(check_ioport, int, 0444);
  77. MODULE_PARM_DESC(check_ioport,
  78. "set this to 0 if you think the automatic ioport check for sony-laptop is wrong");
  79. #define SONYPI_DEVICE_MODEL_TYPE1 1
  80. #define SONYPI_DEVICE_MODEL_TYPE2 2
  81. #define SONYPI_DEVICE_MODEL_TYPE3 3
  82. /* type1 models use those */
  83. #define SONYPI_IRQ_PORT 0x8034
  84. #define SONYPI_IRQ_SHIFT 22
  85. #define SONYPI_TYPE1_BASE 0x50
  86. #define SONYPI_G10A (SONYPI_TYPE1_BASE+0x14)
  87. #define SONYPI_TYPE1_REGION_SIZE 0x08
  88. #define SONYPI_TYPE1_EVTYPE_OFFSET 0x04
  89. /* type2 series specifics */
  90. #define SONYPI_SIRQ 0x9b
  91. #define SONYPI_SLOB 0x9c
  92. #define SONYPI_SHIB 0x9d
  93. #define SONYPI_TYPE2_REGION_SIZE 0x20
  94. #define SONYPI_TYPE2_EVTYPE_OFFSET 0x12
  95. /* type3 series specifics */
  96. #define SONYPI_TYPE3_BASE 0x40
  97. #define SONYPI_TYPE3_GID2 (SONYPI_TYPE3_BASE+0x48) /* 16 bits */
  98. #define SONYPI_TYPE3_MISC (SONYPI_TYPE3_BASE+0x6d) /* 8 bits */
  99. #define SONYPI_TYPE3_REGION_SIZE 0x20
  100. #define SONYPI_TYPE3_EVTYPE_OFFSET 0x12
  101. /* battery / brightness addresses */
  102. #define SONYPI_BAT_FLAGS 0x81
  103. #define SONYPI_LCD_LIGHT 0x96
  104. #define SONYPI_BAT1_PCTRM 0xa0
  105. #define SONYPI_BAT1_LEFT 0xa2
  106. #define SONYPI_BAT1_MAXRT 0xa4
  107. #define SONYPI_BAT2_PCTRM 0xa8
  108. #define SONYPI_BAT2_LEFT 0xaa
  109. #define SONYPI_BAT2_MAXRT 0xac
  110. #define SONYPI_BAT1_MAXTK 0xb0
  111. #define SONYPI_BAT1_FULL 0xb2
  112. #define SONYPI_BAT2_MAXTK 0xb8
  113. #define SONYPI_BAT2_FULL 0xba
  114. /* FAN0 information (reverse engineered from ACPI tables) */
  115. #define SONYPI_FAN0_STATUS 0x93
  116. #define SONYPI_TEMP_STATUS 0xC1
  117. /* ioports used for brightness and type2 events */
  118. #define SONYPI_DATA_IOPORT 0x62
  119. #define SONYPI_CST_IOPORT 0x66
  120. /* The set of possible ioports */
  121. struct sonypi_ioport_list {
  122. u16 port1;
  123. u16 port2;
  124. };
  125. static struct sonypi_ioport_list sonypi_type1_ioport_list[] = {
  126. { 0x10c0, 0x10c4 }, /* looks like the default on C1Vx */
  127. { 0x1080, 0x1084 },
  128. { 0x1090, 0x1094 },
  129. { 0x10a0, 0x10a4 },
  130. { 0x10b0, 0x10b4 },
  131. { 0x0, 0x0 }
  132. };
  133. static struct sonypi_ioport_list sonypi_type2_ioport_list[] = {
  134. { 0x1080, 0x1084 },
  135. { 0x10a0, 0x10a4 },
  136. { 0x10c0, 0x10c4 },
  137. { 0x10e0, 0x10e4 },
  138. { 0x0, 0x0 }
  139. };
  140. /* same as in type 2 models */
  141. static struct sonypi_ioport_list *sonypi_type3_ioport_list =
  142. sonypi_type2_ioport_list;
  143. /* The set of possible interrupts */
  144. struct sonypi_irq_list {
  145. u16 irq;
  146. u16 bits;
  147. };
  148. static struct sonypi_irq_list sonypi_type1_irq_list[] = {
  149. { 11, 0x2 }, /* IRQ 11, GO22=0,GO23=1 in AML */
  150. { 10, 0x1 }, /* IRQ 10, GO22=1,GO23=0 in AML */
  151. { 5, 0x0 }, /* IRQ 5, GO22=0,GO23=0 in AML */
  152. { 0, 0x3 } /* no IRQ, GO22=1,GO23=1 in AML */
  153. };
  154. static struct sonypi_irq_list sonypi_type2_irq_list[] = {
  155. { 11, 0x80 }, /* IRQ 11, 0x80 in SIRQ in AML */
  156. { 10, 0x40 }, /* IRQ 10, 0x40 in SIRQ in AML */
  157. { 9, 0x20 }, /* IRQ 9, 0x20 in SIRQ in AML */
  158. { 6, 0x10 }, /* IRQ 6, 0x10 in SIRQ in AML */
  159. { 0, 0x00 } /* no IRQ, 0x00 in SIRQ in AML */
  160. };
  161. /* same as in type2 models */
  162. static struct sonypi_irq_list *sonypi_type3_irq_list = sonypi_type2_irq_list;
  163. #define SONYPI_CAMERA_BRIGHTNESS 0
  164. #define SONYPI_CAMERA_CONTRAST 1
  165. #define SONYPI_CAMERA_HUE 2
  166. #define SONYPI_CAMERA_COLOR 3
  167. #define SONYPI_CAMERA_SHARPNESS 4
  168. #define SONYPI_CAMERA_PICTURE 5
  169. #define SONYPI_CAMERA_EXPOSURE_MASK 0xC
  170. #define SONYPI_CAMERA_WHITE_BALANCE_MASK 0x3
  171. #define SONYPI_CAMERA_PICTURE_MODE_MASK 0x30
  172. #define SONYPI_CAMERA_MUTE_MASK 0x40
  173. /* the rest don't need a loop until not 0xff */
  174. #define SONYPI_CAMERA_AGC 6
  175. #define SONYPI_CAMERA_AGC_MASK 0x30
  176. #define SONYPI_CAMERA_SHUTTER_MASK 0x7
  177. #define SONYPI_CAMERA_SHUTDOWN_REQUEST 7
  178. #define SONYPI_CAMERA_CONTROL 0x10
  179. #define SONYPI_CAMERA_STATUS 7
  180. #define SONYPI_CAMERA_STATUS_READY 0x2
  181. #define SONYPI_CAMERA_STATUS_POSITION 0x4
  182. #define SONYPI_DIRECTION_BACKWARDS 0x4
  183. #define SONYPI_CAMERA_REVISION 8
  184. #define SONYPI_CAMERA_ROMVERSION 9
  185. /* Event masks */
  186. #define SONYPI_JOGGER_MASK 0x00000001
  187. #define SONYPI_CAPTURE_MASK 0x00000002
  188. #define SONYPI_FNKEY_MASK 0x00000004
  189. #define SONYPI_BLUETOOTH_MASK 0x00000008
  190. #define SONYPI_PKEY_MASK 0x00000010
  191. #define SONYPI_BACK_MASK 0x00000020
  192. #define SONYPI_HELP_MASK 0x00000040
  193. #define SONYPI_LID_MASK 0x00000080
  194. #define SONYPI_ZOOM_MASK 0x00000100
  195. #define SONYPI_THUMBPHRASE_MASK 0x00000200
  196. #define SONYPI_MEYE_MASK 0x00000400
  197. #define SONYPI_MEMORYSTICK_MASK 0x00000800
  198. #define SONYPI_BATTERY_MASK 0x00001000
  199. #define SONYPI_WIRELESS_MASK 0x00002000
  200. struct sonypi_event {
  201. u8 data;
  202. u8 event;
  203. };
  204. /* The set of possible button release events */
  205. static struct sonypi_event sonypi_releaseev[] = {
  206. { 0x00, SONYPI_EVENT_ANYBUTTON_RELEASED },
  207. { 0, 0 }
  208. };
  209. /* The set of possible jogger events */
  210. static struct sonypi_event sonypi_joggerev[] = {
  211. { 0x1f, SONYPI_EVENT_JOGDIAL_UP },
  212. { 0x01, SONYPI_EVENT_JOGDIAL_DOWN },
  213. { 0x5f, SONYPI_EVENT_JOGDIAL_UP_PRESSED },
  214. { 0x41, SONYPI_EVENT_JOGDIAL_DOWN_PRESSED },
  215. { 0x1e, SONYPI_EVENT_JOGDIAL_FAST_UP },
  216. { 0x02, SONYPI_EVENT_JOGDIAL_FAST_DOWN },
  217. { 0x5e, SONYPI_EVENT_JOGDIAL_FAST_UP_PRESSED },
  218. { 0x42, SONYPI_EVENT_JOGDIAL_FAST_DOWN_PRESSED },
  219. { 0x1d, SONYPI_EVENT_JOGDIAL_VFAST_UP },
  220. { 0x03, SONYPI_EVENT_JOGDIAL_VFAST_DOWN },
  221. { 0x5d, SONYPI_EVENT_JOGDIAL_VFAST_UP_PRESSED },
  222. { 0x43, SONYPI_EVENT_JOGDIAL_VFAST_DOWN_PRESSED },
  223. { 0x40, SONYPI_EVENT_JOGDIAL_PRESSED },
  224. { 0, 0 }
  225. };
  226. /* The set of possible capture button events */
  227. static struct sonypi_event sonypi_captureev[] = {
  228. { 0x05, SONYPI_EVENT_CAPTURE_PARTIALPRESSED },
  229. { 0x07, SONYPI_EVENT_CAPTURE_PRESSED },
  230. { 0x01, SONYPI_EVENT_CAPTURE_PARTIALRELEASED },
  231. { 0, 0 }
  232. };
  233. /* The set of possible fnkeys events */
  234. static struct sonypi_event sonypi_fnkeyev[] = {
  235. { 0x10, SONYPI_EVENT_FNKEY_ESC },
  236. { 0x11, SONYPI_EVENT_FNKEY_F1 },
  237. { 0x12, SONYPI_EVENT_FNKEY_F2 },
  238. { 0x13, SONYPI_EVENT_FNKEY_F3 },
  239. { 0x14, SONYPI_EVENT_FNKEY_F4 },
  240. { 0x15, SONYPI_EVENT_FNKEY_F5 },
  241. { 0x16, SONYPI_EVENT_FNKEY_F6 },
  242. { 0x17, SONYPI_EVENT_FNKEY_F7 },
  243. { 0x18, SONYPI_EVENT_FNKEY_F8 },
  244. { 0x19, SONYPI_EVENT_FNKEY_F9 },
  245. { 0x1a, SONYPI_EVENT_FNKEY_F10 },
  246. { 0x1b, SONYPI_EVENT_FNKEY_F11 },
  247. { 0x1c, SONYPI_EVENT_FNKEY_F12 },
  248. { 0x1f, SONYPI_EVENT_FNKEY_RELEASED },
  249. { 0x21, SONYPI_EVENT_FNKEY_1 },
  250. { 0x22, SONYPI_EVENT_FNKEY_2 },
  251. { 0x31, SONYPI_EVENT_FNKEY_D },
  252. { 0x32, SONYPI_EVENT_FNKEY_E },
  253. { 0x33, SONYPI_EVENT_FNKEY_F },
  254. { 0x34, SONYPI_EVENT_FNKEY_S },
  255. { 0x35, SONYPI_EVENT_FNKEY_B },
  256. { 0x36, SONYPI_EVENT_FNKEY_ONLY },
  257. { 0, 0 }
  258. };
  259. /* The set of possible program key events */
  260. static struct sonypi_event sonypi_pkeyev[] = {
  261. { 0x01, SONYPI_EVENT_PKEY_P1 },
  262. { 0x02, SONYPI_EVENT_PKEY_P2 },
  263. { 0x04, SONYPI_EVENT_PKEY_P3 },
  264. { 0x5c, SONYPI_EVENT_PKEY_P1 },
  265. { 0, 0 }
  266. };
  267. /* The set of possible bluetooth events */
  268. static struct sonypi_event sonypi_blueev[] = {
  269. { 0x55, SONYPI_EVENT_BLUETOOTH_PRESSED },
  270. { 0x59, SONYPI_EVENT_BLUETOOTH_ON },
  271. { 0x5a, SONYPI_EVENT_BLUETOOTH_OFF },
  272. { 0, 0 }
  273. };
  274. /* The set of possible wireless events */
  275. static struct sonypi_event sonypi_wlessev[] = {
  276. { 0x59, SONYPI_EVENT_WIRELESS_ON },
  277. { 0x5a, SONYPI_EVENT_WIRELESS_OFF },
  278. { 0, 0 }
  279. };
  280. /* The set of possible back button events */
  281. static struct sonypi_event sonypi_backev[] = {
  282. { 0x20, SONYPI_EVENT_BACK_PRESSED },
  283. { 0, 0 }
  284. };
  285. /* The set of possible help button events */
  286. static struct sonypi_event sonypi_helpev[] = {
  287. { 0x3b, SONYPI_EVENT_HELP_PRESSED },
  288. { 0, 0 }
  289. };
  290. /* The set of possible lid events */
  291. static struct sonypi_event sonypi_lidev[] = {
  292. { 0x51, SONYPI_EVENT_LID_CLOSED },
  293. { 0x50, SONYPI_EVENT_LID_OPENED },
  294. { 0, 0 }
  295. };
  296. /* The set of possible zoom events */
  297. static struct sonypi_event sonypi_zoomev[] = {
  298. { 0x39, SONYPI_EVENT_ZOOM_PRESSED },
  299. { 0, 0 }
  300. };
  301. /* The set of possible thumbphrase events */
  302. static struct sonypi_event sonypi_thumbphraseev[] = {
  303. { 0x3a, SONYPI_EVENT_THUMBPHRASE_PRESSED },
  304. { 0, 0 }
  305. };
  306. /* The set of possible motioneye camera events */
  307. static struct sonypi_event sonypi_meyeev[] = {
  308. { 0x00, SONYPI_EVENT_MEYE_FACE },
  309. { 0x01, SONYPI_EVENT_MEYE_OPPOSITE },
  310. { 0, 0 }
  311. };
  312. /* The set of possible memorystick events */
  313. static struct sonypi_event sonypi_memorystickev[] = {
  314. { 0x53, SONYPI_EVENT_MEMORYSTICK_INSERT },
  315. { 0x54, SONYPI_EVENT_MEMORYSTICK_EJECT },
  316. { 0, 0 }
  317. };
  318. /* The set of possible battery events */
  319. static struct sonypi_event sonypi_batteryev[] = {
  320. { 0x20, SONYPI_EVENT_BATTERY_INSERT },
  321. { 0x30, SONYPI_EVENT_BATTERY_REMOVE },
  322. { 0, 0 }
  323. };
  324. static struct sonypi_eventtypes {
  325. int model;
  326. u8 data;
  327. unsigned long mask;
  328. struct sonypi_event * events;
  329. } sonypi_eventtypes[] = {
  330. { SONYPI_DEVICE_MODEL_TYPE1, 0, 0xffffffff, sonypi_releaseev },
  331. { SONYPI_DEVICE_MODEL_TYPE1, 0x70, SONYPI_MEYE_MASK, sonypi_meyeev },
  332. { SONYPI_DEVICE_MODEL_TYPE1, 0x30, SONYPI_LID_MASK, sonypi_lidev },
  333. { SONYPI_DEVICE_MODEL_TYPE1, 0x60, SONYPI_CAPTURE_MASK, sonypi_captureev },
  334. { SONYPI_DEVICE_MODEL_TYPE1, 0x10, SONYPI_JOGGER_MASK, sonypi_joggerev },
  335. { SONYPI_DEVICE_MODEL_TYPE1, 0x20, SONYPI_FNKEY_MASK, sonypi_fnkeyev },
  336. { SONYPI_DEVICE_MODEL_TYPE1, 0x30, SONYPI_BLUETOOTH_MASK, sonypi_blueev },
  337. { SONYPI_DEVICE_MODEL_TYPE1, 0x40, SONYPI_PKEY_MASK, sonypi_pkeyev },
  338. { SONYPI_DEVICE_MODEL_TYPE1, 0x30, SONYPI_MEMORYSTICK_MASK, sonypi_memorystickev },
  339. { SONYPI_DEVICE_MODEL_TYPE1, 0x40, SONYPI_BATTERY_MASK, sonypi_batteryev },
  340. { SONYPI_DEVICE_MODEL_TYPE2, 0, 0xffffffff, sonypi_releaseev },
  341. { SONYPI_DEVICE_MODEL_TYPE2, 0x38, SONYPI_LID_MASK, sonypi_lidev },
  342. { SONYPI_DEVICE_MODEL_TYPE2, 0x11, SONYPI_JOGGER_MASK, sonypi_joggerev },
  343. { SONYPI_DEVICE_MODEL_TYPE2, 0x61, SONYPI_CAPTURE_MASK, sonypi_captureev },
  344. { SONYPI_DEVICE_MODEL_TYPE2, 0x21, SONYPI_FNKEY_MASK, sonypi_fnkeyev },
  345. { SONYPI_DEVICE_MODEL_TYPE2, 0x31, SONYPI_BLUETOOTH_MASK, sonypi_blueev },
  346. { SONYPI_DEVICE_MODEL_TYPE2, 0x08, SONYPI_PKEY_MASK, sonypi_pkeyev },
  347. { SONYPI_DEVICE_MODEL_TYPE2, 0x11, SONYPI_BACK_MASK, sonypi_backev },
  348. { SONYPI_DEVICE_MODEL_TYPE2, 0x21, SONYPI_HELP_MASK, sonypi_helpev },
  349. { SONYPI_DEVICE_MODEL_TYPE2, 0x21, SONYPI_ZOOM_MASK, sonypi_zoomev },
  350. { SONYPI_DEVICE_MODEL_TYPE2, 0x20, SONYPI_THUMBPHRASE_MASK, sonypi_thumbphraseev },
  351. { SONYPI_DEVICE_MODEL_TYPE2, 0x31, SONYPI_MEMORYSTICK_MASK, sonypi_memorystickev },
  352. { SONYPI_DEVICE_MODEL_TYPE2, 0x41, SONYPI_BATTERY_MASK, sonypi_batteryev },
  353. { SONYPI_DEVICE_MODEL_TYPE2, 0x31, SONYPI_PKEY_MASK, sonypi_pkeyev },
  354. { SONYPI_DEVICE_MODEL_TYPE3, 0, 0xffffffff, sonypi_releaseev },
  355. { SONYPI_DEVICE_MODEL_TYPE3, 0x21, SONYPI_FNKEY_MASK, sonypi_fnkeyev },
  356. { SONYPI_DEVICE_MODEL_TYPE3, 0x31, SONYPI_WIRELESS_MASK, sonypi_wlessev },
  357. { SONYPI_DEVICE_MODEL_TYPE3, 0x31, SONYPI_MEMORYSTICK_MASK, sonypi_memorystickev },
  358. { SONYPI_DEVICE_MODEL_TYPE3, 0x41, SONYPI_BATTERY_MASK, sonypi_batteryev },
  359. { SONYPI_DEVICE_MODEL_TYPE3, 0x31, SONYPI_PKEY_MASK, sonypi_pkeyev },
  360. { 0 }
  361. };
  362. #define SONYPI_BUF_SIZE 128
  363. /* Correspondance table between sonypi events and input layer events */
  364. static struct {
  365. int sonypiev;
  366. int inputev;
  367. } sonypi_inputkeys[] = {
  368. { SONYPI_EVENT_CAPTURE_PRESSED, KEY_CAMERA },
  369. { SONYPI_EVENT_FNKEY_ONLY, KEY_FN },
  370. { SONYPI_EVENT_FNKEY_ESC, KEY_FN_ESC },
  371. { SONYPI_EVENT_FNKEY_F1, KEY_FN_F1 },
  372. { SONYPI_EVENT_FNKEY_F2, KEY_FN_F2 },
  373. { SONYPI_EVENT_FNKEY_F3, KEY_FN_F3 },
  374. { SONYPI_EVENT_FNKEY_F4, KEY_FN_F4 },
  375. { SONYPI_EVENT_FNKEY_F5, KEY_FN_F5 },
  376. { SONYPI_EVENT_FNKEY_F6, KEY_FN_F6 },
  377. { SONYPI_EVENT_FNKEY_F7, KEY_FN_F7 },
  378. { SONYPI_EVENT_FNKEY_F8, KEY_FN_F8 },
  379. { SONYPI_EVENT_FNKEY_F9, KEY_FN_F9 },
  380. { SONYPI_EVENT_FNKEY_F10, KEY_FN_F10 },
  381. { SONYPI_EVENT_FNKEY_F11, KEY_FN_F11 },
  382. { SONYPI_EVENT_FNKEY_F12, KEY_FN_F12 },
  383. { SONYPI_EVENT_FNKEY_1, KEY_FN_1 },
  384. { SONYPI_EVENT_FNKEY_2, KEY_FN_2 },
  385. { SONYPI_EVENT_FNKEY_D, KEY_FN_D },
  386. { SONYPI_EVENT_FNKEY_E, KEY_FN_E },
  387. { SONYPI_EVENT_FNKEY_F, KEY_FN_F },
  388. { SONYPI_EVENT_FNKEY_S, KEY_FN_S },
  389. { SONYPI_EVENT_FNKEY_B, KEY_FN_B },
  390. { SONYPI_EVENT_BLUETOOTH_PRESSED, KEY_BLUE },
  391. { SONYPI_EVENT_BLUETOOTH_ON, KEY_BLUE },
  392. { SONYPI_EVENT_PKEY_P1, KEY_PROG1 },
  393. { SONYPI_EVENT_PKEY_P2, KEY_PROG2 },
  394. { SONYPI_EVENT_PKEY_P3, KEY_PROG3 },
  395. { SONYPI_EVENT_BACK_PRESSED, KEY_BACK },
  396. { SONYPI_EVENT_HELP_PRESSED, KEY_HELP },
  397. { SONYPI_EVENT_ZOOM_PRESSED, KEY_ZOOM },
  398. { SONYPI_EVENT_THUMBPHRASE_PRESSED, BTN_THUMB },
  399. { 0, 0 },
  400. };
  401. struct sonypi_keypress {
  402. struct input_dev *dev;
  403. int key;
  404. };
  405. static struct sonypi_device {
  406. struct pci_dev *dev;
  407. u16 irq;
  408. u16 bits;
  409. u16 ioport1;
  410. u16 ioport2;
  411. u16 region_size;
  412. u16 evtype_offset;
  413. int camera_power;
  414. int bluetooth_power;
  415. struct mutex lock;
  416. struct kfifo fifo;
  417. spinlock_t fifo_lock;
  418. wait_queue_head_t fifo_proc_list;
  419. struct fasync_struct *fifo_async;
  420. int open_count;
  421. int model;
  422. struct input_dev *input_jog_dev;
  423. struct input_dev *input_key_dev;
  424. struct work_struct input_work;
  425. struct kfifo input_fifo;
  426. spinlock_t input_fifo_lock;
  427. } sonypi_device;
  428. #define ITERATIONS_LONG 10000
  429. #define ITERATIONS_SHORT 10
  430. #define wait_on_command(quiet, command, iterations) { \
  431. unsigned int n = iterations; \
  432. while (--n && (command)) \
  433. udelay(1); \
  434. if (!n && (verbose || !quiet)) \
  435. printk(KERN_WARNING "sonypi command failed at %s : %s (line %d)\n", __FILE__, __func__, __LINE__); \
  436. }
  437. #ifdef CONFIG_ACPI
  438. #define SONYPI_ACPI_ACTIVE (!acpi_disabled)
  439. #else
  440. #define SONYPI_ACPI_ACTIVE 0
  441. #endif /* CONFIG_ACPI */
  442. #ifdef CONFIG_ACPI
  443. static struct acpi_device *sonypi_acpi_device;
  444. static int acpi_driver_registered;
  445. #endif
  446. static int sonypi_ec_write(u8 addr, u8 value)
  447. {
  448. #ifdef CONFIG_ACPI
  449. if (SONYPI_ACPI_ACTIVE)
  450. return ec_write(addr, value);
  451. #endif
  452. wait_on_command(1, inb_p(SONYPI_CST_IOPORT) & 3, ITERATIONS_LONG);
  453. outb_p(0x81, SONYPI_CST_IOPORT);
  454. wait_on_command(0, inb_p(SONYPI_CST_IOPORT) & 2, ITERATIONS_LONG);
  455. outb_p(addr, SONYPI_DATA_IOPORT);
  456. wait_on_command(0, inb_p(SONYPI_CST_IOPORT) & 2, ITERATIONS_LONG);
  457. outb_p(value, SONYPI_DATA_IOPORT);
  458. wait_on_command(0, inb_p(SONYPI_CST_IOPORT) & 2, ITERATIONS_LONG);
  459. return 0;
  460. }
  461. static int sonypi_ec_read(u8 addr, u8 *value)
  462. {
  463. #ifdef CONFIG_ACPI
  464. if (SONYPI_ACPI_ACTIVE)
  465. return ec_read(addr, value);
  466. #endif
  467. wait_on_command(1, inb_p(SONYPI_CST_IOPORT) & 3, ITERATIONS_LONG);
  468. outb_p(0x80, SONYPI_CST_IOPORT);
  469. wait_on_command(0, inb_p(SONYPI_CST_IOPORT) & 2, ITERATIONS_LONG);
  470. outb_p(addr, SONYPI_DATA_IOPORT);
  471. wait_on_command(0, inb_p(SONYPI_CST_IOPORT) & 2, ITERATIONS_LONG);
  472. *value = inb_p(SONYPI_DATA_IOPORT);
  473. return 0;
  474. }
  475. static int ec_read16(u8 addr, u16 *value)
  476. {
  477. u8 val_lb, val_hb;
  478. if (sonypi_ec_read(addr, &val_lb))
  479. return -1;
  480. if (sonypi_ec_read(addr + 1, &val_hb))
  481. return -1;
  482. *value = val_lb | (val_hb << 8);
  483. return 0;
  484. }
  485. /* Initializes the device - this comes from the AML code in the ACPI bios */
  486. static void sonypi_type1_srs(void)
  487. {
  488. u32 v;
  489. pci_read_config_dword(sonypi_device.dev, SONYPI_G10A, &v);
  490. v = (v & 0xFFFF0000) | ((u32) sonypi_device.ioport1);
  491. pci_write_config_dword(sonypi_device.dev, SONYPI_G10A, v);
  492. pci_read_config_dword(sonypi_device.dev, SONYPI_G10A, &v);
  493. v = (v & 0xFFF0FFFF) |
  494. (((u32) sonypi_device.ioport1 ^ sonypi_device.ioport2) << 16);
  495. pci_write_config_dword(sonypi_device.dev, SONYPI_G10A, v);
  496. v = inl(SONYPI_IRQ_PORT);
  497. v &= ~(((u32) 0x3) << SONYPI_IRQ_SHIFT);
  498. v |= (((u32) sonypi_device.bits) << SONYPI_IRQ_SHIFT);
  499. outl(v, SONYPI_IRQ_PORT);
  500. pci_read_config_dword(sonypi_device.dev, SONYPI_G10A, &v);
  501. v = (v & 0xFF1FFFFF) | 0x00C00000;
  502. pci_write_config_dword(sonypi_device.dev, SONYPI_G10A, v);
  503. }
  504. static void sonypi_type2_srs(void)
  505. {
  506. if (sonypi_ec_write(SONYPI_SHIB, (sonypi_device.ioport1 & 0xFF00) >> 8))
  507. printk(KERN_WARNING "ec_write failed\n");
  508. if (sonypi_ec_write(SONYPI_SLOB, sonypi_device.ioport1 & 0x00FF))
  509. printk(KERN_WARNING "ec_write failed\n");
  510. if (sonypi_ec_write(SONYPI_SIRQ, sonypi_device.bits))
  511. printk(KERN_WARNING "ec_write failed\n");
  512. udelay(10);
  513. }
  514. static void sonypi_type3_srs(void)
  515. {
  516. u16 v16;
  517. u8 v8;
  518. /* This model type uses the same initialization of
  519. * the embedded controller as the type2 models. */
  520. sonypi_type2_srs();
  521. /* Initialization of PCI config space of the LPC interface bridge. */
  522. v16 = (sonypi_device.ioport1 & 0xFFF0) | 0x01;
  523. pci_write_config_word(sonypi_device.dev, SONYPI_TYPE3_GID2, v16);
  524. pci_read_config_byte(sonypi_device.dev, SONYPI_TYPE3_MISC, &v8);
  525. v8 = (v8 & 0xCF) | 0x10;
  526. pci_write_config_byte(sonypi_device.dev, SONYPI_TYPE3_MISC, v8);
  527. }
  528. /* Disables the device - this comes from the AML code in the ACPI bios */
  529. static void sonypi_type1_dis(void)
  530. {
  531. u32 v;
  532. pci_read_config_dword(sonypi_device.dev, SONYPI_G10A, &v);
  533. v = v & 0xFF3FFFFF;
  534. pci_write_config_dword(sonypi_device.dev, SONYPI_G10A, v);
  535. v = inl(SONYPI_IRQ_PORT);
  536. v |= (0x3 << SONYPI_IRQ_SHIFT);
  537. outl(v, SONYPI_IRQ_PORT);
  538. }
  539. static void sonypi_type2_dis(void)
  540. {
  541. if (sonypi_ec_write(SONYPI_SHIB, 0))
  542. printk(KERN_WARNING "ec_write failed\n");
  543. if (sonypi_ec_write(SONYPI_SLOB, 0))
  544. printk(KERN_WARNING "ec_write failed\n");
  545. if (sonypi_ec_write(SONYPI_SIRQ, 0))
  546. printk(KERN_WARNING "ec_write failed\n");
  547. }
  548. static void sonypi_type3_dis(void)
  549. {
  550. sonypi_type2_dis();
  551. udelay(10);
  552. pci_write_config_word(sonypi_device.dev, SONYPI_TYPE3_GID2, 0);
  553. }
  554. static u8 sonypi_call1(u8 dev)
  555. {
  556. u8 v1, v2;
  557. wait_on_command(0, inb_p(sonypi_device.ioport2) & 2, ITERATIONS_LONG);
  558. outb(dev, sonypi_device.ioport2);
  559. v1 = inb_p(sonypi_device.ioport2);
  560. v2 = inb_p(sonypi_device.ioport1);
  561. return v2;
  562. }
  563. static u8 sonypi_call2(u8 dev, u8 fn)
  564. {
  565. u8 v1;
  566. wait_on_command(0, inb_p(sonypi_device.ioport2) & 2, ITERATIONS_LONG);
  567. outb(dev, sonypi_device.ioport2);
  568. wait_on_command(0, inb_p(sonypi_device.ioport2) & 2, ITERATIONS_LONG);
  569. outb(fn, sonypi_device.ioport1);
  570. v1 = inb_p(sonypi_device.ioport1);
  571. return v1;
  572. }
  573. static u8 sonypi_call3(u8 dev, u8 fn, u8 v)
  574. {
  575. u8 v1;
  576. wait_on_command(0, inb_p(sonypi_device.ioport2) & 2, ITERATIONS_LONG);
  577. outb(dev, sonypi_device.ioport2);
  578. wait_on_command(0, inb_p(sonypi_device.ioport2) & 2, ITERATIONS_LONG);
  579. outb(fn, sonypi_device.ioport1);
  580. wait_on_command(0, inb_p(sonypi_device.ioport2) & 2, ITERATIONS_LONG);
  581. outb(v, sonypi_device.ioport1);
  582. v1 = inb_p(sonypi_device.ioport1);
  583. return v1;
  584. }
  585. #if 0
  586. /* Get brightness, hue etc. Unreliable... */
  587. static u8 sonypi_read(u8 fn)
  588. {
  589. u8 v1, v2;
  590. int n = 100;
  591. while (n--) {
  592. v1 = sonypi_call2(0x8f, fn);
  593. v2 = sonypi_call2(0x8f, fn);
  594. if (v1 == v2 && v1 != 0xff)
  595. return v1;
  596. }
  597. return 0xff;
  598. }
  599. #endif
  600. /* Set brightness, hue etc */
  601. static void sonypi_set(u8 fn, u8 v)
  602. {
  603. wait_on_command(0, sonypi_call3(0x90, fn, v), ITERATIONS_SHORT);
  604. }
  605. /* Tests if the camera is ready */
  606. static int sonypi_camera_ready(void)
  607. {
  608. u8 v;
  609. v = sonypi_call2(0x8f, SONYPI_CAMERA_STATUS);
  610. return (v != 0xff && (v & SONYPI_CAMERA_STATUS_READY));
  611. }
  612. /* Turns the camera off */
  613. static void sonypi_camera_off(void)
  614. {
  615. sonypi_set(SONYPI_CAMERA_PICTURE, SONYPI_CAMERA_MUTE_MASK);
  616. if (!sonypi_device.camera_power)
  617. return;
  618. sonypi_call2(0x91, 0);
  619. sonypi_device.camera_power = 0;
  620. }
  621. /* Turns the camera on */
  622. static void sonypi_camera_on(void)
  623. {
  624. int i, j;
  625. if (sonypi_device.camera_power)
  626. return;
  627. for (j = 5; j > 0; j--) {
  628. while (sonypi_call2(0x91, 0x1))
  629. msleep(10);
  630. sonypi_call1(0x93);
  631. for (i = 400; i > 0; i--) {
  632. if (sonypi_camera_ready())
  633. break;
  634. msleep(10);
  635. }
  636. if (i)
  637. break;
  638. }
  639. if (j == 0) {
  640. printk(KERN_WARNING "sonypi: failed to power on camera\n");
  641. return;
  642. }
  643. sonypi_set(0x10, 0x5a);
  644. sonypi_device.camera_power = 1;
  645. }
  646. /* sets the bluetooth subsystem power state */
  647. static void sonypi_setbluetoothpower(u8 state)
  648. {
  649. state = !!state;
  650. if (sonypi_device.bluetooth_power == state)
  651. return;
  652. sonypi_call2(0x96, state);
  653. sonypi_call1(0x82);
  654. sonypi_device.bluetooth_power = state;
  655. }
  656. static void input_keyrelease(struct work_struct *work)
  657. {
  658. struct sonypi_keypress kp;
  659. while (kfifo_out_locked(&sonypi_device.input_fifo, (unsigned char *)&kp,
  660. sizeof(kp), &sonypi_device.input_fifo_lock)
  661. == sizeof(kp)) {
  662. msleep(10);
  663. input_report_key(kp.dev, kp.key, 0);
  664. input_sync(kp.dev);
  665. }
  666. }
  667. static void sonypi_report_input_event(u8 event)
  668. {
  669. struct input_dev *jog_dev = sonypi_device.input_jog_dev;
  670. struct input_dev *key_dev = sonypi_device.input_key_dev;
  671. struct sonypi_keypress kp = { NULL };
  672. int i;
  673. switch (event) {
  674. case SONYPI_EVENT_JOGDIAL_UP:
  675. case SONYPI_EVENT_JOGDIAL_UP_PRESSED:
  676. input_report_rel(jog_dev, REL_WHEEL, 1);
  677. input_sync(jog_dev);
  678. break;
  679. case SONYPI_EVENT_JOGDIAL_DOWN:
  680. case SONYPI_EVENT_JOGDIAL_DOWN_PRESSED:
  681. input_report_rel(jog_dev, REL_WHEEL, -1);
  682. input_sync(jog_dev);
  683. break;
  684. case SONYPI_EVENT_JOGDIAL_PRESSED:
  685. kp.key = BTN_MIDDLE;
  686. kp.dev = jog_dev;
  687. break;
  688. case SONYPI_EVENT_FNKEY_RELEASED:
  689. /* Nothing, not all VAIOs generate this event */
  690. break;
  691. default:
  692. for (i = 0; sonypi_inputkeys[i].sonypiev; i++)
  693. if (event == sonypi_inputkeys[i].sonypiev) {
  694. kp.dev = key_dev;
  695. kp.key = sonypi_inputkeys[i].inputev;
  696. break;
  697. }
  698. break;
  699. }
  700. if (kp.dev) {
  701. input_report_key(kp.dev, kp.key, 1);
  702. input_sync(kp.dev);
  703. kfifo_in_locked(&sonypi_device.input_fifo,
  704. (unsigned char *)&kp, sizeof(kp),
  705. &sonypi_device.input_fifo_lock);
  706. schedule_work(&sonypi_device.input_work);
  707. }
  708. }
  709. /* Interrupt handler: some event is available */
  710. static irqreturn_t sonypi_irq(int irq, void *dev_id)
  711. {
  712. u8 v1, v2, event = 0;
  713. int i, j;
  714. v1 = inb_p(sonypi_device.ioport1);
  715. v2 = inb_p(sonypi_device.ioport1 + sonypi_device.evtype_offset);
  716. for (i = 0; sonypi_eventtypes[i].model; i++) {
  717. if (sonypi_device.model != sonypi_eventtypes[i].model)
  718. continue;
  719. if ((v2 & sonypi_eventtypes[i].data) !=
  720. sonypi_eventtypes[i].data)
  721. continue;
  722. if (!(mask & sonypi_eventtypes[i].mask))
  723. continue;
  724. for (j = 0; sonypi_eventtypes[i].events[j].event; j++) {
  725. if (v1 == sonypi_eventtypes[i].events[j].data) {
  726. event = sonypi_eventtypes[i].events[j].event;
  727. goto found;
  728. }
  729. }
  730. }
  731. if (verbose)
  732. printk(KERN_WARNING
  733. "sonypi: unknown event port1=0x%02x,port2=0x%02x\n",
  734. v1, v2);
  735. /* We need to return IRQ_HANDLED here because there *are*
  736. * events belonging to the sonypi device we don't know about,
  737. * but we still don't want those to pollute the logs... */
  738. return IRQ_HANDLED;
  739. found:
  740. if (verbose > 1)
  741. printk(KERN_INFO
  742. "sonypi: event port1=0x%02x,port2=0x%02x\n", v1, v2);
  743. if (useinput)
  744. sonypi_report_input_event(event);
  745. kfifo_in_locked(&sonypi_device.fifo, (unsigned char *)&event,
  746. sizeof(event), &sonypi_device.fifo_lock);
  747. kill_fasync(&sonypi_device.fifo_async, SIGIO, POLL_IN);
  748. wake_up_interruptible(&sonypi_device.fifo_proc_list);
  749. return IRQ_HANDLED;
  750. }
  751. static int sonypi_misc_fasync(int fd, struct file *filp, int on)
  752. {
  753. return fasync_helper(fd, filp, on, &sonypi_device.fifo_async);
  754. }
  755. static int sonypi_misc_release(struct inode *inode, struct file *file)
  756. {
  757. mutex_lock(&sonypi_device.lock);
  758. sonypi_device.open_count--;
  759. mutex_unlock(&sonypi_device.lock);
  760. return 0;
  761. }
  762. static int sonypi_misc_open(struct inode *inode, struct file *file)
  763. {
  764. mutex_lock(&sonypi_device.lock);
  765. /* Flush input queue on first open */
  766. if (!sonypi_device.open_count)
  767. kfifo_reset(&sonypi_device.fifo);
  768. sonypi_device.open_count++;
  769. mutex_unlock(&sonypi_device.lock);
  770. return 0;
  771. }
  772. static ssize_t sonypi_misc_read(struct file *file, char __user *buf,
  773. size_t count, loff_t *pos)
  774. {
  775. ssize_t ret;
  776. unsigned char c;
  777. if ((kfifo_len(&sonypi_device.fifo) == 0) &&
  778. (file->f_flags & O_NONBLOCK))
  779. return -EAGAIN;
  780. ret = wait_event_interruptible(sonypi_device.fifo_proc_list,
  781. kfifo_len(&sonypi_device.fifo) != 0);
  782. if (ret)
  783. return ret;
  784. while (ret < count &&
  785. (kfifo_out_locked(&sonypi_device.fifo, &c, sizeof(c),
  786. &sonypi_device.fifo_lock) == sizeof(c))) {
  787. if (put_user(c, buf++))
  788. return -EFAULT;
  789. ret++;
  790. }
  791. if (ret > 0) {
  792. struct inode *inode = file_inode(file);
  793. inode_set_atime_to_ts(inode, current_time(inode));
  794. }
  795. return ret;
  796. }
  797. static __poll_t sonypi_misc_poll(struct file *file, poll_table *wait)
  798. {
  799. poll_wait(file, &sonypi_device.fifo_proc_list, wait);
  800. if (kfifo_len(&sonypi_device.fifo))
  801. return EPOLLIN | EPOLLRDNORM;
  802. return 0;
  803. }
  804. static long sonypi_misc_ioctl(struct file *fp,
  805. unsigned int cmd, unsigned long arg)
  806. {
  807. long ret = 0;
  808. void __user *argp = (void __user *)arg;
  809. u8 val8;
  810. u16 val16;
  811. mutex_lock(&sonypi_device.lock);
  812. switch (cmd) {
  813. case SONYPI_IOCGBRT:
  814. if (sonypi_ec_read(SONYPI_LCD_LIGHT, &val8)) {
  815. ret = -EIO;
  816. break;
  817. }
  818. if (copy_to_user(argp, &val8, sizeof(val8)))
  819. ret = -EFAULT;
  820. break;
  821. case SONYPI_IOCSBRT:
  822. if (copy_from_user(&val8, argp, sizeof(val8))) {
  823. ret = -EFAULT;
  824. break;
  825. }
  826. if (sonypi_ec_write(SONYPI_LCD_LIGHT, val8))
  827. ret = -EIO;
  828. break;
  829. case SONYPI_IOCGBAT1CAP:
  830. if (ec_read16(SONYPI_BAT1_FULL, &val16)) {
  831. ret = -EIO;
  832. break;
  833. }
  834. if (copy_to_user(argp, &val16, sizeof(val16)))
  835. ret = -EFAULT;
  836. break;
  837. case SONYPI_IOCGBAT1REM:
  838. if (ec_read16(SONYPI_BAT1_LEFT, &val16)) {
  839. ret = -EIO;
  840. break;
  841. }
  842. if (copy_to_user(argp, &val16, sizeof(val16)))
  843. ret = -EFAULT;
  844. break;
  845. case SONYPI_IOCGBAT2CAP:
  846. if (ec_read16(SONYPI_BAT2_FULL, &val16)) {
  847. ret = -EIO;
  848. break;
  849. }
  850. if (copy_to_user(argp, &val16, sizeof(val16)))
  851. ret = -EFAULT;
  852. break;
  853. case SONYPI_IOCGBAT2REM:
  854. if (ec_read16(SONYPI_BAT2_LEFT, &val16)) {
  855. ret = -EIO;
  856. break;
  857. }
  858. if (copy_to_user(argp, &val16, sizeof(val16)))
  859. ret = -EFAULT;
  860. break;
  861. case SONYPI_IOCGBATFLAGS:
  862. if (sonypi_ec_read(SONYPI_BAT_FLAGS, &val8)) {
  863. ret = -EIO;
  864. break;
  865. }
  866. val8 &= 0x07;
  867. if (copy_to_user(argp, &val8, sizeof(val8)))
  868. ret = -EFAULT;
  869. break;
  870. case SONYPI_IOCGBLUE:
  871. val8 = sonypi_device.bluetooth_power;
  872. if (copy_to_user(argp, &val8, sizeof(val8)))
  873. ret = -EFAULT;
  874. break;
  875. case SONYPI_IOCSBLUE:
  876. if (copy_from_user(&val8, argp, sizeof(val8))) {
  877. ret = -EFAULT;
  878. break;
  879. }
  880. sonypi_setbluetoothpower(val8);
  881. break;
  882. /* FAN Controls */
  883. case SONYPI_IOCGFAN:
  884. if (sonypi_ec_read(SONYPI_FAN0_STATUS, &val8)) {
  885. ret = -EIO;
  886. break;
  887. }
  888. if (copy_to_user(argp, &val8, sizeof(val8)))
  889. ret = -EFAULT;
  890. break;
  891. case SONYPI_IOCSFAN:
  892. if (copy_from_user(&val8, argp, sizeof(val8))) {
  893. ret = -EFAULT;
  894. break;
  895. }
  896. if (sonypi_ec_write(SONYPI_FAN0_STATUS, val8))
  897. ret = -EIO;
  898. break;
  899. /* GET Temperature (useful under APM) */
  900. case SONYPI_IOCGTEMP:
  901. if (sonypi_ec_read(SONYPI_TEMP_STATUS, &val8)) {
  902. ret = -EIO;
  903. break;
  904. }
  905. if (copy_to_user(argp, &val8, sizeof(val8)))
  906. ret = -EFAULT;
  907. break;
  908. default:
  909. ret = -EINVAL;
  910. }
  911. mutex_unlock(&sonypi_device.lock);
  912. return ret;
  913. }
  914. static const struct file_operations sonypi_misc_fops = {
  915. .owner = THIS_MODULE,
  916. .read = sonypi_misc_read,
  917. .poll = sonypi_misc_poll,
  918. .open = sonypi_misc_open,
  919. .release = sonypi_misc_release,
  920. .fasync = sonypi_misc_fasync,
  921. .unlocked_ioctl = sonypi_misc_ioctl,
  922. };
  923. static struct miscdevice sonypi_misc_device = {
  924. .minor = MISC_DYNAMIC_MINOR,
  925. .name = "sonypi",
  926. .fops = &sonypi_misc_fops,
  927. };
  928. static void sonypi_enable(unsigned int camera_on)
  929. {
  930. switch (sonypi_device.model) {
  931. case SONYPI_DEVICE_MODEL_TYPE1:
  932. sonypi_type1_srs();
  933. break;
  934. case SONYPI_DEVICE_MODEL_TYPE2:
  935. sonypi_type2_srs();
  936. break;
  937. case SONYPI_DEVICE_MODEL_TYPE3:
  938. sonypi_type3_srs();
  939. break;
  940. }
  941. sonypi_call1(0x82);
  942. sonypi_call2(0x81, 0xff);
  943. sonypi_call1(compat ? 0x92 : 0x82);
  944. /* Enable ACPI mode to get Fn key events */
  945. if (!SONYPI_ACPI_ACTIVE && fnkeyinit)
  946. outb(0xf0, 0xb2);
  947. if (camera && camera_on)
  948. sonypi_camera_on();
  949. }
  950. static int sonypi_disable(void)
  951. {
  952. sonypi_call2(0x81, 0); /* make sure we don't get any more events */
  953. if (camera)
  954. sonypi_camera_off();
  955. /* disable ACPI mode */
  956. if (!SONYPI_ACPI_ACTIVE && fnkeyinit)
  957. outb(0xf1, 0xb2);
  958. switch (sonypi_device.model) {
  959. case SONYPI_DEVICE_MODEL_TYPE1:
  960. sonypi_type1_dis();
  961. break;
  962. case SONYPI_DEVICE_MODEL_TYPE2:
  963. sonypi_type2_dis();
  964. break;
  965. case SONYPI_DEVICE_MODEL_TYPE3:
  966. sonypi_type3_dis();
  967. break;
  968. }
  969. return 0;
  970. }
  971. #ifdef CONFIG_ACPI
  972. static int sonypi_acpi_add(struct acpi_device *device)
  973. {
  974. sonypi_acpi_device = device;
  975. strcpy(acpi_device_name(device), "Sony laptop hotkeys");
  976. strcpy(acpi_device_class(device), "sony/hotkey");
  977. return 0;
  978. }
  979. static void sonypi_acpi_remove(struct acpi_device *device)
  980. {
  981. sonypi_acpi_device = NULL;
  982. }
  983. static const struct acpi_device_id sonypi_device_ids[] = {
  984. {"SNY6001", 0},
  985. {"", 0},
  986. };
  987. static struct acpi_driver sonypi_acpi_driver = {
  988. .name = "sonypi",
  989. .class = "hkey",
  990. .ids = sonypi_device_ids,
  991. .ops = {
  992. .add = sonypi_acpi_add,
  993. .remove = sonypi_acpi_remove,
  994. },
  995. };
  996. #endif
  997. static int sonypi_create_input_devices(struct platform_device *pdev)
  998. {
  999. struct input_dev *jog_dev;
  1000. struct input_dev *key_dev;
  1001. int i;
  1002. int error;
  1003. sonypi_device.input_jog_dev = jog_dev = input_allocate_device();
  1004. if (!jog_dev)
  1005. return -ENOMEM;
  1006. jog_dev->name = "Sony Vaio Jogdial";
  1007. jog_dev->id.bustype = BUS_ISA;
  1008. jog_dev->id.vendor = PCI_VENDOR_ID_SONY;
  1009. jog_dev->dev.parent = &pdev->dev;
  1010. jog_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REL);
  1011. jog_dev->keybit[BIT_WORD(BTN_MOUSE)] = BIT_MASK(BTN_MIDDLE);
  1012. jog_dev->relbit[0] = BIT_MASK(REL_WHEEL);
  1013. sonypi_device.input_key_dev = key_dev = input_allocate_device();
  1014. if (!key_dev) {
  1015. error = -ENOMEM;
  1016. goto err_free_jogdev;
  1017. }
  1018. key_dev->name = "Sony Vaio Keys";
  1019. key_dev->id.bustype = BUS_ISA;
  1020. key_dev->id.vendor = PCI_VENDOR_ID_SONY;
  1021. key_dev->dev.parent = &pdev->dev;
  1022. /* Initialize the Input Drivers: special keys */
  1023. key_dev->evbit[0] = BIT_MASK(EV_KEY);
  1024. for (i = 0; sonypi_inputkeys[i].sonypiev; i++)
  1025. if (sonypi_inputkeys[i].inputev)
  1026. set_bit(sonypi_inputkeys[i].inputev, key_dev->keybit);
  1027. error = input_register_device(jog_dev);
  1028. if (error)
  1029. goto err_free_keydev;
  1030. error = input_register_device(key_dev);
  1031. if (error)
  1032. goto err_unregister_jogdev;
  1033. return 0;
  1034. err_unregister_jogdev:
  1035. input_unregister_device(jog_dev);
  1036. /* Set to NULL so we don't free it again below */
  1037. jog_dev = NULL;
  1038. err_free_keydev:
  1039. input_free_device(key_dev);
  1040. sonypi_device.input_key_dev = NULL;
  1041. err_free_jogdev:
  1042. input_free_device(jog_dev);
  1043. sonypi_device.input_jog_dev = NULL;
  1044. return error;
  1045. }
  1046. static int sonypi_setup_ioports(struct sonypi_device *dev,
  1047. const struct sonypi_ioport_list *ioport_list)
  1048. {
  1049. /* try to detect if sony-laptop is being used and thus
  1050. * has already requested one of the known ioports.
  1051. * As in the deprecated check_region this is racy has we have
  1052. * multiple ioports available and one of them can be requested
  1053. * between this check and the subsequent request. Anyway, as an
  1054. * attempt to be some more user-friendly as we currently are,
  1055. * this is enough.
  1056. */
  1057. const struct sonypi_ioport_list *check = ioport_list;
  1058. while (check_ioport && check->port1) {
  1059. if (!request_region(check->port1,
  1060. sonypi_device.region_size,
  1061. "Sony Programmable I/O Device Check")) {
  1062. printk(KERN_ERR "sonypi: ioport 0x%.4x busy, using sony-laptop? "
  1063. "if not use check_ioport=0\n",
  1064. check->port1);
  1065. return -EBUSY;
  1066. }
  1067. release_region(check->port1, sonypi_device.region_size);
  1068. check++;
  1069. }
  1070. while (ioport_list->port1) {
  1071. if (request_region(ioport_list->port1,
  1072. sonypi_device.region_size,
  1073. "Sony Programmable I/O Device")) {
  1074. dev->ioport1 = ioport_list->port1;
  1075. dev->ioport2 = ioport_list->port2;
  1076. return 0;
  1077. }
  1078. ioport_list++;
  1079. }
  1080. return -EBUSY;
  1081. }
  1082. static int sonypi_setup_irq(struct sonypi_device *dev,
  1083. const struct sonypi_irq_list *irq_list)
  1084. {
  1085. while (irq_list->irq) {
  1086. if (!request_irq(irq_list->irq, sonypi_irq,
  1087. IRQF_SHARED, "sonypi", sonypi_irq)) {
  1088. dev->irq = irq_list->irq;
  1089. dev->bits = irq_list->bits;
  1090. return 0;
  1091. }
  1092. irq_list++;
  1093. }
  1094. return -EBUSY;
  1095. }
  1096. static void sonypi_display_info(void)
  1097. {
  1098. printk(KERN_INFO "sonypi: detected type%d model, "
  1099. "verbose = %d, fnkeyinit = %s, camera = %s, "
  1100. "compat = %s, mask = 0x%08lx, useinput = %s, acpi = %s\n",
  1101. sonypi_device.model,
  1102. verbose,
  1103. str_on_off(fnkeyinit),
  1104. str_on_off(camera),
  1105. str_on_off(compat),
  1106. mask,
  1107. str_on_off(useinput),
  1108. str_on_off(SONYPI_ACPI_ACTIVE));
  1109. printk(KERN_INFO "sonypi: enabled at irq=%d, port1=0x%x, port2=0x%x\n",
  1110. sonypi_device.irq,
  1111. sonypi_device.ioport1, sonypi_device.ioport2);
  1112. if (minor == -1)
  1113. printk(KERN_INFO "sonypi: device allocated minor is %d\n",
  1114. sonypi_misc_device.minor);
  1115. }
  1116. static int sonypi_probe(struct platform_device *dev)
  1117. {
  1118. const struct sonypi_ioport_list *ioport_list;
  1119. const struct sonypi_irq_list *irq_list;
  1120. struct pci_dev *pcidev;
  1121. int error;
  1122. printk(KERN_WARNING "sonypi: please try the sony-laptop module instead "
  1123. "and report failures, see also "
  1124. "http://www.linux.it/~malattia/wiki/index.php/Sony_drivers\n");
  1125. spin_lock_init(&sonypi_device.fifo_lock);
  1126. error = kfifo_alloc(&sonypi_device.fifo, SONYPI_BUF_SIZE, GFP_KERNEL);
  1127. if (error) {
  1128. printk(KERN_ERR "sonypi: kfifo_alloc failed\n");
  1129. return error;
  1130. }
  1131. init_waitqueue_head(&sonypi_device.fifo_proc_list);
  1132. mutex_init(&sonypi_device.lock);
  1133. sonypi_device.bluetooth_power = -1;
  1134. if ((pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
  1135. PCI_DEVICE_ID_INTEL_82371AB_3, NULL)))
  1136. sonypi_device.model = SONYPI_DEVICE_MODEL_TYPE1;
  1137. else if ((pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
  1138. PCI_DEVICE_ID_INTEL_ICH6_1, NULL)))
  1139. sonypi_device.model = SONYPI_DEVICE_MODEL_TYPE3;
  1140. else if ((pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
  1141. PCI_DEVICE_ID_INTEL_ICH7_1, NULL)))
  1142. sonypi_device.model = SONYPI_DEVICE_MODEL_TYPE3;
  1143. else
  1144. sonypi_device.model = SONYPI_DEVICE_MODEL_TYPE2;
  1145. if (pcidev && pci_enable_device(pcidev)) {
  1146. printk(KERN_ERR "sonypi: pci_enable_device failed\n");
  1147. error = -EIO;
  1148. goto err_put_pcidev;
  1149. }
  1150. sonypi_device.dev = pcidev;
  1151. if (sonypi_device.model == SONYPI_DEVICE_MODEL_TYPE1) {
  1152. ioport_list = sonypi_type1_ioport_list;
  1153. sonypi_device.region_size = SONYPI_TYPE1_REGION_SIZE;
  1154. sonypi_device.evtype_offset = SONYPI_TYPE1_EVTYPE_OFFSET;
  1155. irq_list = sonypi_type1_irq_list;
  1156. } else if (sonypi_device.model == SONYPI_DEVICE_MODEL_TYPE2) {
  1157. ioport_list = sonypi_type2_ioport_list;
  1158. sonypi_device.region_size = SONYPI_TYPE2_REGION_SIZE;
  1159. sonypi_device.evtype_offset = SONYPI_TYPE2_EVTYPE_OFFSET;
  1160. irq_list = sonypi_type2_irq_list;
  1161. } else {
  1162. ioport_list = sonypi_type3_ioport_list;
  1163. sonypi_device.region_size = SONYPI_TYPE3_REGION_SIZE;
  1164. sonypi_device.evtype_offset = SONYPI_TYPE3_EVTYPE_OFFSET;
  1165. irq_list = sonypi_type3_irq_list;
  1166. }
  1167. error = sonypi_setup_ioports(&sonypi_device, ioport_list);
  1168. if (error) {
  1169. printk(KERN_ERR "sonypi: failed to request ioports\n");
  1170. goto err_disable_pcidev;
  1171. }
  1172. error = sonypi_setup_irq(&sonypi_device, irq_list);
  1173. if (error) {
  1174. printk(KERN_ERR "sonypi: request_irq failed\n");
  1175. goto err_free_ioports;
  1176. }
  1177. if (minor != -1)
  1178. sonypi_misc_device.minor = minor;
  1179. error = misc_register(&sonypi_misc_device);
  1180. if (error) {
  1181. printk(KERN_ERR "sonypi: misc_register failed\n");
  1182. goto err_free_irq;
  1183. }
  1184. sonypi_display_info();
  1185. if (useinput) {
  1186. error = sonypi_create_input_devices(dev);
  1187. if (error) {
  1188. printk(KERN_ERR
  1189. "sonypi: failed to create input devices\n");
  1190. goto err_miscdev_unregister;
  1191. }
  1192. spin_lock_init(&sonypi_device.input_fifo_lock);
  1193. error = kfifo_alloc(&sonypi_device.input_fifo, SONYPI_BUF_SIZE,
  1194. GFP_KERNEL);
  1195. if (error) {
  1196. printk(KERN_ERR "sonypi: kfifo_alloc failed\n");
  1197. goto err_inpdev_unregister;
  1198. }
  1199. INIT_WORK(&sonypi_device.input_work, input_keyrelease);
  1200. }
  1201. sonypi_enable(0);
  1202. return 0;
  1203. err_inpdev_unregister:
  1204. input_unregister_device(sonypi_device.input_key_dev);
  1205. input_unregister_device(sonypi_device.input_jog_dev);
  1206. err_miscdev_unregister:
  1207. misc_deregister(&sonypi_misc_device);
  1208. err_free_irq:
  1209. free_irq(sonypi_device.irq, sonypi_irq);
  1210. err_free_ioports:
  1211. release_region(sonypi_device.ioport1, sonypi_device.region_size);
  1212. err_disable_pcidev:
  1213. if (pcidev)
  1214. pci_disable_device(pcidev);
  1215. err_put_pcidev:
  1216. pci_dev_put(pcidev);
  1217. kfifo_free(&sonypi_device.fifo);
  1218. return error;
  1219. }
  1220. static void sonypi_remove(struct platform_device *dev)
  1221. {
  1222. sonypi_disable();
  1223. synchronize_irq(sonypi_device.irq);
  1224. flush_work(&sonypi_device.input_work);
  1225. if (useinput) {
  1226. input_unregister_device(sonypi_device.input_key_dev);
  1227. input_unregister_device(sonypi_device.input_jog_dev);
  1228. kfifo_free(&sonypi_device.input_fifo);
  1229. }
  1230. misc_deregister(&sonypi_misc_device);
  1231. free_irq(sonypi_device.irq, sonypi_irq);
  1232. release_region(sonypi_device.ioport1, sonypi_device.region_size);
  1233. if (sonypi_device.dev) {
  1234. pci_disable_device(sonypi_device.dev);
  1235. pci_dev_put(sonypi_device.dev);
  1236. }
  1237. kfifo_free(&sonypi_device.fifo);
  1238. }
  1239. #ifdef CONFIG_PM_SLEEP
  1240. static int old_camera_power;
  1241. static int sonypi_suspend(struct device *dev)
  1242. {
  1243. old_camera_power = sonypi_device.camera_power;
  1244. sonypi_disable();
  1245. return 0;
  1246. }
  1247. static int sonypi_resume(struct device *dev)
  1248. {
  1249. sonypi_enable(old_camera_power);
  1250. return 0;
  1251. }
  1252. static SIMPLE_DEV_PM_OPS(sonypi_pm, sonypi_suspend, sonypi_resume);
  1253. #define SONYPI_PM (&sonypi_pm)
  1254. #else
  1255. #define SONYPI_PM NULL
  1256. #endif
  1257. static void sonypi_shutdown(struct platform_device *dev)
  1258. {
  1259. sonypi_disable();
  1260. }
  1261. static struct platform_driver sonypi_driver = {
  1262. .driver = {
  1263. .name = "sonypi",
  1264. .pm = SONYPI_PM,
  1265. },
  1266. .probe = sonypi_probe,
  1267. .remove = sonypi_remove,
  1268. .shutdown = sonypi_shutdown,
  1269. };
  1270. static struct platform_device *sonypi_platform_device;
  1271. static const struct dmi_system_id sonypi_dmi_table[] __initconst = {
  1272. {
  1273. .ident = "Sony Vaio",
  1274. .matches = {
  1275. DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
  1276. DMI_MATCH(DMI_PRODUCT_NAME, "PCG-"),
  1277. },
  1278. },
  1279. {
  1280. .ident = "Sony Vaio",
  1281. .matches = {
  1282. DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
  1283. DMI_MATCH(DMI_PRODUCT_NAME, "VGN-"),
  1284. },
  1285. },
  1286. { }
  1287. };
  1288. static int __init sonypi_init(void)
  1289. {
  1290. int error;
  1291. printk(KERN_INFO
  1292. "sonypi: Sony Programmable I/O Controller Driver v%s.\n",
  1293. SONYPI_DRIVER_VERSION);
  1294. if (!dmi_check_system(sonypi_dmi_table))
  1295. return -ENODEV;
  1296. error = platform_driver_register(&sonypi_driver);
  1297. if (error)
  1298. return error;
  1299. sonypi_platform_device = platform_device_alloc("sonypi", -1);
  1300. if (!sonypi_platform_device) {
  1301. error = -ENOMEM;
  1302. goto err_driver_unregister;
  1303. }
  1304. error = platform_device_add(sonypi_platform_device);
  1305. if (error)
  1306. goto err_free_device;
  1307. #ifdef CONFIG_ACPI
  1308. if (acpi_bus_register_driver(&sonypi_acpi_driver) >= 0)
  1309. acpi_driver_registered = 1;
  1310. #endif
  1311. return 0;
  1312. err_free_device:
  1313. platform_device_put(sonypi_platform_device);
  1314. err_driver_unregister:
  1315. platform_driver_unregister(&sonypi_driver);
  1316. return error;
  1317. }
  1318. static void __exit sonypi_exit(void)
  1319. {
  1320. #ifdef CONFIG_ACPI
  1321. if (acpi_driver_registered)
  1322. acpi_bus_unregister_driver(&sonypi_acpi_driver);
  1323. #endif
  1324. platform_device_unregister(sonypi_platform_device);
  1325. platform_driver_unregister(&sonypi_driver);
  1326. printk(KERN_INFO "sonypi: removed.\n");
  1327. }
  1328. module_init(sonypi_init);
  1329. module_exit(sonypi_exit);